Code

Fix an incorrect reference to --set-all.
[git.git] / setup.c
1 #include "cache.h"
2 #include "dir.h"
4 static int inside_git_dir = -1;
5 static int inside_work_tree = -1;
7 char *prefix_path(const char *prefix, int len, const char *path)
8 {
9         const char *orig = path;
10         char *sanitized;
11         if (is_absolute_path(orig)) {
12                 const char *temp = real_path(path);
13                 sanitized = xmalloc(len + strlen(temp) + 1);
14                 strcpy(sanitized, temp);
15         } else {
16                 sanitized = xmalloc(len + strlen(path) + 1);
17                 if (len)
18                         memcpy(sanitized, prefix, len);
19                 strcpy(sanitized + len, path);
20         }
21         if (normalize_path_copy(sanitized, sanitized))
22                 goto error_out;
23         if (is_absolute_path(orig)) {
24                 size_t root_len, len, total;
25                 const char *work_tree = get_git_work_tree();
26                 if (!work_tree)
27                         goto error_out;
28                 len = strlen(work_tree);
29                 root_len = offset_1st_component(work_tree);
30                 total = strlen(sanitized) + 1;
31                 if (strncmp(sanitized, work_tree, len) ||
32                     (len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) {
33                 error_out:
34                         die("'%s' is outside repository", orig);
35                 }
36                 if (sanitized[len] == '/')
37                         len++;
38                 memmove(sanitized, sanitized + len, total - len);
39         }
40         return sanitized;
41 }
43 /*
44  * Unlike prefix_path, this should be used if the named file does
45  * not have to interact with index entry; i.e. name of a random file
46  * on the filesystem.
47  */
48 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
49 {
50         static char path[PATH_MAX];
51 #ifndef WIN32
52         if (!pfx_len || is_absolute_path(arg))
53                 return arg;
54         memcpy(path, pfx, pfx_len);
55         strcpy(path + pfx_len, arg);
56 #else
57         char *p;
58         /* don't add prefix to absolute paths, but still replace '\' by '/' */
59         if (is_absolute_path(arg))
60                 pfx_len = 0;
61         else if (pfx_len)
62                 memcpy(path, pfx, pfx_len);
63         strcpy(path + pfx_len, arg);
64         for (p = path + pfx_len; *p; p++)
65                 if (*p == '\\')
66                         *p = '/';
67 #endif
68         return path;
69 }
71 int check_filename(const char *prefix, const char *arg)
72 {
73         const char *name;
74         struct stat st;
76         name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
77         if (!lstat(name, &st))
78                 return 1; /* file exists */
79         if (errno == ENOENT || errno == ENOTDIR)
80                 return 0; /* file does not exist */
81         die_errno("failed to stat '%s'", arg);
82 }
84 static void NORETURN die_verify_filename(const char *prefix, const char *arg)
85 {
86         unsigned char sha1[20];
87         unsigned mode;
88         /* try a detailed diagnostic ... */
89         get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
90         /* ... or fall back the most general message. */
91         die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
92             "Use '--' to separate paths from revisions", arg);
94 }
96 /*
97  * Verify a filename that we got as an argument for a pathspec
98  * entry. Note that a filename that begins with "-" never verifies
99  * as true, because even if such a filename were to exist, we want
100  * it to be preceded by the "--" marker (or we want the user to
101  * use a format like "./-filename")
102  */
103 void verify_filename(const char *prefix, const char *arg)
105         if (*arg == '-')
106                 die("bad flag '%s' used after filename", arg);
107         if (check_filename(prefix, arg))
108                 return;
109         die_verify_filename(prefix, arg);
112 /*
113  * Opposite of the above: the command line did not have -- marker
114  * and we parsed the arg as a refname.  It should not be interpretable
115  * as a filename.
116  */
117 void verify_non_filename(const char *prefix, const char *arg)
119         if (!is_inside_work_tree() || is_inside_git_dir())
120                 return;
121         if (*arg == '-')
122                 return; /* flag */
123         if (!check_filename(prefix, arg))
124                 return;
125         die("ambiguous argument '%s': both revision and filename\n"
126             "Use '--' to separate filenames from revisions", arg);
129 const char **get_pathspec(const char *prefix, const char **pathspec)
131         const char *entry = *pathspec;
132         const char **src, **dst;
133         int prefixlen;
135         if (!prefix && !entry)
136                 return NULL;
138         if (!entry) {
139                 static const char *spec[2];
140                 spec[0] = prefix;
141                 spec[1] = NULL;
142                 return spec;
143         }
145         /* Otherwise we have to re-write the entries.. */
146         src = pathspec;
147         dst = pathspec;
148         prefixlen = prefix ? strlen(prefix) : 0;
149         while (*src) {
150                 const char *p = prefix_path(prefix, prefixlen, *src);
151                 *(dst++) = p;
152                 src++;
153         }
154         *dst = NULL;
155         if (!*pathspec)
156                 return NULL;
157         return pathspec;
160 /*
161  * Test if it looks like we're at a git directory.
162  * We want to see:
163  *
164  *  - either an objects/ directory _or_ the proper
165  *    GIT_OBJECT_DIRECTORY environment variable
166  *  - a refs/ directory
167  *  - either a HEAD symlink or a HEAD file that is formatted as
168  *    a proper "ref:", or a regular file HEAD that has a properly
169  *    formatted sha1 object name.
170  */
171 static int is_git_directory(const char *suspect)
173         char path[PATH_MAX];
174         size_t len = strlen(suspect);
176         if (PATH_MAX <= len + strlen("/objects"))
177                 die("Too long path: %.*s", 60, suspect);
178         strcpy(path, suspect);
179         if (getenv(DB_ENVIRONMENT)) {
180                 if (access(getenv(DB_ENVIRONMENT), X_OK))
181                         return 0;
182         }
183         else {
184                 strcpy(path + len, "/objects");
185                 if (access(path, X_OK))
186                         return 0;
187         }
189         strcpy(path + len, "/refs");
190         if (access(path, X_OK))
191                 return 0;
193         strcpy(path + len, "/HEAD");
194         if (validate_headref(path))
195                 return 0;
197         return 1;
200 int is_inside_git_dir(void)
202         if (inside_git_dir < 0)
203                 inside_git_dir = is_inside_dir(get_git_dir());
204         return inside_git_dir;
207 int is_inside_work_tree(void)
209         if (inside_work_tree < 0)
210                 inside_work_tree = is_inside_dir(get_git_work_tree());
211         return inside_work_tree;
214 void setup_work_tree(void)
216         const char *work_tree, *git_dir;
217         static int initialized = 0;
219         if (initialized)
220                 return;
221         work_tree = get_git_work_tree();
222         git_dir = get_git_dir();
223         if (!is_absolute_path(git_dir))
224                 git_dir = real_path(get_git_dir());
225         if (!work_tree || chdir(work_tree))
226                 die("This operation must be run in a work tree");
228         /*
229          * Make sure subsequent git processes find correct worktree
230          * if $GIT_WORK_TREE is set relative
231          */
232         if (getenv(GIT_WORK_TREE_ENVIRONMENT))
233                 setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
235         set_git_dir(relative_path(git_dir, work_tree));
236         initialized = 1;
239 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
241         char repo_config[PATH_MAX+1];
243         /*
244          * git_config() can't be used here because it calls git_pathdup()
245          * to get $GIT_CONFIG/config. That call will make setup_git_env()
246          * set git_dir to ".git".
247          *
248          * We are in gitdir setup, no git dir has been found useable yet.
249          * Use a gentler version of git_config() to check if this repo
250          * is a good one.
251          */
252         snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
253         git_config_early(check_repository_format_version, NULL, repo_config);
254         if (GIT_REPO_VERSION < repository_format_version) {
255                 if (!nongit_ok)
256                         die ("Expected git repo version <= %d, found %d",
257                              GIT_REPO_VERSION, repository_format_version);
258                 warning("Expected git repo version <= %d, found %d",
259                         GIT_REPO_VERSION, repository_format_version);
260                 warning("Please upgrade Git");
261                 *nongit_ok = -1;
262                 return -1;
263         }
264         return 0;
267 /*
268  * Try to read the location of the git directory from the .git file,
269  * return path to git directory if found.
270  */
271 const char *read_gitfile_gently(const char *path)
273         char *buf;
274         char *dir;
275         const char *slash;
276         struct stat st;
277         int fd;
278         size_t len;
280         if (stat(path, &st))
281                 return NULL;
282         if (!S_ISREG(st.st_mode))
283                 return NULL;
284         fd = open(path, O_RDONLY);
285         if (fd < 0)
286                 die_errno("Error opening '%s'", path);
287         buf = xmalloc(st.st_size + 1);
288         len = read_in_full(fd, buf, st.st_size);
289         close(fd);
290         if (len != st.st_size)
291                 die("Error reading %s", path);
292         buf[len] = '\0';
293         if (prefixcmp(buf, "gitdir: "))
294                 die("Invalid gitfile format: %s", path);
295         while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
296                 len--;
297         if (len < 9)
298                 die("No path in gitfile: %s", path);
299         buf[len] = '\0';
300         dir = buf + 8;
302         if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
303                 size_t pathlen = slash+1 - path;
304                 size_t dirlen = pathlen + len - 8;
305                 dir = xmalloc(dirlen + 1);
306                 strncpy(dir, path, pathlen);
307                 strncpy(dir + pathlen, buf + 8, len - 8);
308                 dir[dirlen] = '\0';
309                 free(buf);
310                 buf = dir;
311         }
313         if (!is_git_directory(dir))
314                 die("Not a git repository: %s", dir);
315         path = real_path(dir);
317         free(buf);
318         return path;
321 static const char *setup_explicit_git_dir(const char *gitdirenv,
322                                           char *cwd, int len,
323                                           int *nongit_ok)
325         const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
326         const char *worktree;
327         char *gitfile;
328         int offset;
330         if (PATH_MAX - 40 < strlen(gitdirenv))
331                 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
333         gitfile = (char*)read_gitfile_gently(gitdirenv);
334         if (gitfile) {
335                 gitfile = xstrdup(gitfile);
336                 gitdirenv = gitfile;
337         }
339         if (!is_git_directory(gitdirenv)) {
340                 if (nongit_ok) {
341                         *nongit_ok = 1;
342                         free(gitfile);
343                         return NULL;
344                 }
345                 die("Not a git repository: '%s'", gitdirenv);
346         }
348         if (check_repository_format_gently(gitdirenv, nongit_ok)) {
349                 free(gitfile);
350                 return NULL;
351         }
353         /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
354         if (work_tree_env)
355                 set_git_work_tree(work_tree_env);
356         else if (is_bare_repository_cfg > 0) {
357                 if (git_work_tree_cfg) /* #22.2, #30 */
358                         die("core.bare and core.worktree do not make sense");
360                 /* #18, #26 */
361                 set_git_dir(gitdirenv);
362                 free(gitfile);
363                 return NULL;
364         }
365         else if (git_work_tree_cfg) { /* #6, #14 */
366                 if (is_absolute_path(git_work_tree_cfg))
367                         set_git_work_tree(git_work_tree_cfg);
368                 else {
369                         char core_worktree[PATH_MAX];
370                         if (chdir(gitdirenv))
371                                 die_errno("Could not chdir to '%s'", gitdirenv);
372                         if (chdir(git_work_tree_cfg))
373                                 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
374                         if (!getcwd(core_worktree, PATH_MAX))
375                                 die_errno("Could not get directory '%s'", git_work_tree_cfg);
376                         if (chdir(cwd))
377                                 die_errno("Could not come back to cwd");
378                         set_git_work_tree(core_worktree);
379                 }
380         }
381         else /* #2, #10 */
382                 set_git_work_tree(".");
384         /* set_git_work_tree() must have been called by now */
385         worktree = get_git_work_tree();
387         /* both get_git_work_tree() and cwd are already normalized */
388         if (!strcmp(cwd, worktree)) { /* cwd == worktree */
389                 set_git_dir(gitdirenv);
390                 free(gitfile);
391                 return NULL;
392         }
394         offset = dir_inside_of(cwd, worktree);
395         if (offset >= 0) {      /* cwd inside worktree? */
396                 set_git_dir(real_path(gitdirenv));
397                 if (chdir(worktree))
398                         die_errno("Could not chdir to '%s'", worktree);
399                 cwd[len++] = '/';
400                 cwd[len] = '\0';
401                 free(gitfile);
402                 return cwd + offset;
403         }
405         /* cwd outside worktree */
406         set_git_dir(gitdirenv);
407         free(gitfile);
408         return NULL;
411 static const char *setup_discovered_git_dir(const char *gitdir,
412                                             char *cwd, int offset, int len,
413                                             int *nongit_ok)
415         if (check_repository_format_gently(gitdir, nongit_ok))
416                 return NULL;
418         /* --work-tree is set without --git-dir; use discovered one */
419         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
420                 if (offset != len && !is_absolute_path(gitdir))
421                         gitdir = xstrdup(real_path(gitdir));
422                 if (chdir(cwd))
423                         die_errno("Could not come back to cwd");
424                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
425         }
427         /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
428         if (is_bare_repository_cfg > 0) {
429                 set_git_dir(offset == len ? gitdir : real_path(gitdir));
430                 if (chdir(cwd))
431                         die_errno("Could not come back to cwd");
432                 return NULL;
433         }
435         /* #0, #1, #5, #8, #9, #12, #13 */
436         set_git_work_tree(".");
437         if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
438                 set_git_dir(gitdir);
439         inside_git_dir = 0;
440         inside_work_tree = 1;
441         if (offset == len)
442                 return NULL;
444         /* Make "offset" point to past the '/', and add a '/' at the end */
445         offset++;
446         cwd[len++] = '/';
447         cwd[len] = 0;
448         return cwd + offset;
451 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
452 static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
454         int root_len;
456         if (check_repository_format_gently(".", nongit_ok))
457                 return NULL;
459         /* --work-tree is set without --git-dir; use discovered one */
460         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
461                 const char *gitdir;
463                 gitdir = offset == len ? "." : xmemdupz(cwd, offset);
464                 if (chdir(cwd))
465                         die_errno("Could not come back to cwd");
466                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
467         }
469         inside_git_dir = 1;
470         inside_work_tree = 0;
471         if (offset != len) {
472                 if (chdir(cwd))
473                         die_errno("Cannot come back to cwd");
474                 root_len = offset_1st_component(cwd);
475                 cwd[offset > root_len ? offset : root_len] = '\0';
476                 set_git_dir(cwd);
477         }
478         else
479                 set_git_dir(".");
480         return NULL;
483 static const char *setup_nongit(const char *cwd, int *nongit_ok)
485         if (!nongit_ok)
486                 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
487         if (chdir(cwd))
488                 die_errno("Cannot come back to cwd");
489         *nongit_ok = 1;
490         return NULL;
493 static dev_t get_device_or_die(const char *path, const char *prefix)
495         struct stat buf;
496         if (stat(path, &buf))
497                 die_errno("failed to stat '%s%s%s'",
498                                 prefix ? prefix : "",
499                                 prefix ? "/" : "", path);
500         return buf.st_dev;
503 /*
504  * We cannot decide in this function whether we are in the work tree or
505  * not, since the config can only be read _after_ this function was called.
506  */
507 static const char *setup_git_directory_gently_1(int *nongit_ok)
509         const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
510         static char cwd[PATH_MAX+1];
511         const char *gitdirenv, *ret;
512         char *gitfile;
513         int len, offset, ceil_offset;
514         dev_t current_device = 0;
515         int one_filesystem = 1;
517         /*
518          * Let's assume that we are in a git repository.
519          * If it turns out later that we are somewhere else, the value will be
520          * updated accordingly.
521          */
522         if (nongit_ok)
523                 *nongit_ok = 0;
525         if (!getcwd(cwd, sizeof(cwd)-1))
526                 die_errno("Unable to read current working directory");
527         offset = len = strlen(cwd);
529         /*
530          * If GIT_DIR is set explicitly, we're not going
531          * to do any discovery, but we still do repository
532          * validation.
533          */
534         gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
535         if (gitdirenv)
536                 return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
538         ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
539         if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
540                 ceil_offset = 1;
542         /*
543          * Test in the following order (relative to the cwd):
544          * - .git (file containing "gitdir: <path>")
545          * - .git/
546          * - ./ (bare)
547          * - ../.git
548          * - ../.git/
549          * - ../ (bare)
550          * - ../../.git/
551          *   etc.
552          */
553         one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
554         if (one_filesystem)
555                 current_device = get_device_or_die(".", NULL);
556         for (;;) {
557                 gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
558                 if (gitfile)
559                         gitdirenv = gitfile = xstrdup(gitfile);
560                 else {
561                         if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
562                                 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
563                 }
565                 if (gitdirenv) {
566                         ret = setup_discovered_git_dir(gitdirenv,
567                                                        cwd, offset, len,
568                                                        nongit_ok);
569                         free(gitfile);
570                         return ret;
571                 }
572                 free(gitfile);
574                 if (is_git_directory("."))
575                         return setup_bare_git_dir(cwd, offset, len, nongit_ok);
577                 while (--offset > ceil_offset && cwd[offset] != '/');
578                 if (offset <= ceil_offset)
579                         return setup_nongit(cwd, nongit_ok);
580                 if (one_filesystem) {
581                         dev_t parent_device = get_device_or_die("..", cwd);
582                         if (parent_device != current_device) {
583                                 if (nongit_ok) {
584                                         if (chdir(cwd))
585                                                 die_errno("Cannot come back to cwd");
586                                         *nongit_ok = 1;
587                                         return NULL;
588                                 }
589                                 cwd[offset] = '\0';
590                                 die("Not a git repository (or any parent up to mount parent %s)\n"
591                                 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
592                         }
593                 }
594                 if (chdir("..")) {
595                         cwd[offset] = '\0';
596                         die_errno("Cannot change to '%s/..'", cwd);
597                 }
598         }
601 const char *setup_git_directory_gently(int *nongit_ok)
603         const char *prefix;
605         prefix = setup_git_directory_gently_1(nongit_ok);
606         if (startup_info) {
607                 startup_info->have_repository = !nongit_ok || !*nongit_ok;
608                 startup_info->prefix = prefix;
609         }
610         return prefix;
613 int git_config_perm(const char *var, const char *value)
615         int i;
616         char *endptr;
618         if (value == NULL)
619                 return PERM_GROUP;
621         if (!strcmp(value, "umask"))
622                 return PERM_UMASK;
623         if (!strcmp(value, "group"))
624                 return PERM_GROUP;
625         if (!strcmp(value, "all") ||
626             !strcmp(value, "world") ||
627             !strcmp(value, "everybody"))
628                 return PERM_EVERYBODY;
630         /* Parse octal numbers */
631         i = strtol(value, &endptr, 8);
633         /* If not an octal number, maybe true/false? */
634         if (*endptr != 0)
635                 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
637         /*
638          * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
639          * a chmod value to restrict to.
640          */
641         switch (i) {
642         case PERM_UMASK:               /* 0 */
643                 return PERM_UMASK;
644         case OLD_PERM_GROUP:           /* 1 */
645                 return PERM_GROUP;
646         case OLD_PERM_EVERYBODY:       /* 2 */
647                 return PERM_EVERYBODY;
648         }
650         /* A filemode value was given: 0xxx */
652         if ((i & 0600) != 0600)
653                 die("Problem with core.sharedRepository filemode value "
654                     "(0%.3o).\nThe owner of files must always have "
655                     "read and write permissions.", i);
657         /*
658          * Mask filemode value. Others can not get write permission.
659          * x flags for directories are handled separately.
660          */
661         return -(i & 0666);
664 int check_repository_format_version(const char *var, const char *value, void *cb)
666         if (strcmp(var, "core.repositoryformatversion") == 0)
667                 repository_format_version = git_config_int(var, value);
668         else if (strcmp(var, "core.sharedrepository") == 0)
669                 shared_repository = git_config_perm(var, value);
670         else if (strcmp(var, "core.bare") == 0) {
671                 is_bare_repository_cfg = git_config_bool(var, value);
672                 if (is_bare_repository_cfg == 1)
673                         inside_work_tree = -1;
674         } else if (strcmp(var, "core.worktree") == 0) {
675                 if (!value)
676                         return config_error_nonbool(var);
677                 free(git_work_tree_cfg);
678                 git_work_tree_cfg = xstrdup(value);
679                 inside_work_tree = -1;
680         }
681         return 0;
684 int check_repository_format(void)
686         return check_repository_format_gently(get_git_dir(), NULL);
689 /*
690  * Returns the "prefix", a path to the current working directory
691  * relative to the work tree root, or NULL, if the current working
692  * directory is not a strict subdirectory of the work tree root. The
693  * prefix always ends with a '/' character.
694  */
695 const char *setup_git_directory(void)
697         return setup_git_directory_gently(NULL);