Code

consolidate pathspec_prefix and common_prefix
[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;
89         /*
90          * Saying "'(icase)foo' does not exist in the index" when the
91          * user gave us ":(icase)foo" is just stupid.  A magic pathspec
92          * begins with a colon and is followed by a non-alnum; do not
93          * let get_sha1_with_mode_1(only_to_die=1) to even trigger.
94          */
95         if (!(arg[0] == ':' && !isalnum(arg[1])))
96                 /* try a detailed diagnostic ... */
97                 get_sha1_with_mode_1(arg, sha1, &mode, 1, prefix);
99         /* ... or fall back the most general message. */
100         die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
101             "Use '--' to separate paths from revisions", arg);
105 /*
106  * Verify a filename that we got as an argument for a pathspec
107  * entry. Note that a filename that begins with "-" never verifies
108  * as true, because even if such a filename were to exist, we want
109  * it to be preceded by the "--" marker (or we want the user to
110  * use a format like "./-filename")
111  */
112 void verify_filename(const char *prefix, const char *arg)
114         if (*arg == '-')
115                 die("bad flag '%s' used after filename", arg);
116         if (check_filename(prefix, arg))
117                 return;
118         die_verify_filename(prefix, arg);
121 /*
122  * Opposite of the above: the command line did not have -- marker
123  * and we parsed the arg as a refname.  It should not be interpretable
124  * as a filename.
125  */
126 void verify_non_filename(const char *prefix, const char *arg)
128         if (!is_inside_work_tree() || is_inside_git_dir())
129                 return;
130         if (*arg == '-')
131                 return; /* flag */
132         if (!check_filename(prefix, arg))
133                 return;
134         die("ambiguous argument '%s': both revision and filename\n"
135             "Use '--' to separate filenames from revisions", arg);
138 /*
139  * Magic pathspec
140  *
141  * NEEDSWORK: These need to be moved to dir.h or even to a new
142  * pathspec.h when we restructure get_pathspec() users to use the
143  * "struct pathspec" interface.
144  *
145  * Possible future magic semantics include stuff like:
146  *
147  *      { PATHSPEC_NOGLOB, '!', "noglob" },
148  *      { PATHSPEC_ICASE, '\0', "icase" },
149  *      { PATHSPEC_RECURSIVE, '*', "recursive" },
150  *      { PATHSPEC_REGEXP, '\0', "regexp" },
151  *
152  */
153 #define PATHSPEC_FROMTOP    (1<<0)
155 static struct pathspec_magic {
156         unsigned bit;
157         char mnemonic; /* this cannot be ':'! */
158         const char *name;
159 } pathspec_magic[] = {
160         { PATHSPEC_FROMTOP, '/', "top" },
161 };
163 /*
164  * Take an element of a pathspec and check for magic signatures.
165  * Append the result to the prefix.
166  *
167  * For now, we only parse the syntax and throw out anything other than
168  * "top" magic.
169  *
170  * NEEDSWORK: This needs to be rewritten when we start migrating
171  * get_pathspec() users to use the "struct pathspec" interface.  For
172  * example, a pathspec element may be marked as case-insensitive, but
173  * the prefix part must always match literally, and a single stupid
174  * string cannot express such a case.
175  */
176 static const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)
178         unsigned magic = 0;
179         const char *copyfrom = elt;
180         int i;
182         if (elt[0] != ':') {
183                 ; /* nothing to do */
184         } else if (elt[1] == '(') {
185                 /* longhand */
186                 const char *nextat;
187                 for (copyfrom = elt + 2;
188                      *copyfrom && *copyfrom != ')';
189                      copyfrom = nextat) {
190                         size_t len = strcspn(copyfrom, ",)");
191                         if (copyfrom[len] == ')')
192                                 nextat = copyfrom + len;
193                         else
194                                 nextat = copyfrom + len + 1;
195                         if (!len)
196                                 continue;
197                         for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
198                                 if (strlen(pathspec_magic[i].name) == len &&
199                                     !strncmp(pathspec_magic[i].name, copyfrom, len)) {
200                                         magic |= pathspec_magic[i].bit;
201                                         break;
202                                 }
203                         if (ARRAY_SIZE(pathspec_magic) <= i)
204                                 die("Invalid pathspec magic '%.*s' in '%s'",
205                                     (int) len, copyfrom, elt);
206                 }
207                 if (*copyfrom == ')')
208                         copyfrom++;
209         } else {
210                 /* shorthand */
211                 for (copyfrom = elt + 1;
212                      *copyfrom && *copyfrom != ':';
213                      copyfrom++) {
214                         char ch = *copyfrom;
216                         if (!is_pathspec_magic(ch))
217                                 break;
218                         for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
219                                 if (pathspec_magic[i].mnemonic == ch) {
220                                         magic |= pathspec_magic[i].bit;
221                                         break;
222                                 }
223                         if (ARRAY_SIZE(pathspec_magic) <= i)
224                                 die("Unimplemented pathspec magic '%c' in '%s'",
225                                     ch, elt);
226                 }
227                 if (*copyfrom == ':')
228                         copyfrom++;
229         }
231         if (magic & PATHSPEC_FROMTOP)
232                 return xstrdup(copyfrom);
233         else
234                 return prefix_path(prefix, prefixlen, copyfrom);
237 const char **get_pathspec(const char *prefix, const char **pathspec)
239         const char *entry = *pathspec;
240         const char **src, **dst;
241         int prefixlen;
243         if (!prefix && !entry)
244                 return NULL;
246         if (!entry) {
247                 static const char *spec[2];
248                 spec[0] = prefix;
249                 spec[1] = NULL;
250                 return spec;
251         }
253         /* Otherwise we have to re-write the entries.. */
254         src = pathspec;
255         dst = pathspec;
256         prefixlen = prefix ? strlen(prefix) : 0;
257         while (*src) {
258                 *(dst++) = prefix_pathspec(prefix, prefixlen, *src);
259                 src++;
260         }
261         *dst = NULL;
262         if (!*pathspec)
263                 return NULL;
264         return pathspec;
267 char *pathspec_prefix(const char **pathspec)
269         size_t len = common_prefix_len(pathspec);
271         return len ? xmemdupz(*pathspec, len) : NULL;
274 /*
275  * Test if it looks like we're at a git directory.
276  * We want to see:
277  *
278  *  - either an objects/ directory _or_ the proper
279  *    GIT_OBJECT_DIRECTORY environment variable
280  *  - a refs/ directory
281  *  - either a HEAD symlink or a HEAD file that is formatted as
282  *    a proper "ref:", or a regular file HEAD that has a properly
283  *    formatted sha1 object name.
284  */
285 static int is_git_directory(const char *suspect)
287         char path[PATH_MAX];
288         size_t len = strlen(suspect);
290         if (PATH_MAX <= len + strlen("/objects"))
291                 die("Too long path: %.*s", 60, suspect);
292         strcpy(path, suspect);
293         if (getenv(DB_ENVIRONMENT)) {
294                 if (access(getenv(DB_ENVIRONMENT), X_OK))
295                         return 0;
296         }
297         else {
298                 strcpy(path + len, "/objects");
299                 if (access(path, X_OK))
300                         return 0;
301         }
303         strcpy(path + len, "/refs");
304         if (access(path, X_OK))
305                 return 0;
307         strcpy(path + len, "/HEAD");
308         if (validate_headref(path))
309                 return 0;
311         return 1;
314 int is_inside_git_dir(void)
316         if (inside_git_dir < 0)
317                 inside_git_dir = is_inside_dir(get_git_dir());
318         return inside_git_dir;
321 int is_inside_work_tree(void)
323         if (inside_work_tree < 0)
324                 inside_work_tree = is_inside_dir(get_git_work_tree());
325         return inside_work_tree;
328 void setup_work_tree(void)
330         const char *work_tree, *git_dir;
331         static int initialized = 0;
333         if (initialized)
334                 return;
335         work_tree = get_git_work_tree();
336         git_dir = get_git_dir();
337         if (!is_absolute_path(git_dir))
338                 git_dir = real_path(get_git_dir());
339         if (!work_tree || chdir(work_tree))
340                 die("This operation must be run in a work tree");
342         /*
343          * Make sure subsequent git processes find correct worktree
344          * if $GIT_WORK_TREE is set relative
345          */
346         if (getenv(GIT_WORK_TREE_ENVIRONMENT))
347                 setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
349         set_git_dir(relative_path(git_dir, work_tree));
350         initialized = 1;
353 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
355         char repo_config[PATH_MAX+1];
357         /*
358          * git_config() can't be used here because it calls git_pathdup()
359          * to get $GIT_CONFIG/config. That call will make setup_git_env()
360          * set git_dir to ".git".
361          *
362          * We are in gitdir setup, no git dir has been found useable yet.
363          * Use a gentler version of git_config() to check if this repo
364          * is a good one.
365          */
366         snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
367         git_config_early(check_repository_format_version, NULL, repo_config);
368         if (GIT_REPO_VERSION < repository_format_version) {
369                 if (!nongit_ok)
370                         die ("Expected git repo version <= %d, found %d",
371                              GIT_REPO_VERSION, repository_format_version);
372                 warning("Expected git repo version <= %d, found %d",
373                         GIT_REPO_VERSION, repository_format_version);
374                 warning("Please upgrade Git");
375                 *nongit_ok = -1;
376                 return -1;
377         }
378         return 0;
381 /*
382  * Try to read the location of the git directory from the .git file,
383  * return path to git directory if found.
384  */
385 const char *read_gitfile_gently(const char *path)
387         char *buf;
388         char *dir;
389         const char *slash;
390         struct stat st;
391         int fd;
392         ssize_t len;
394         if (stat(path, &st))
395                 return NULL;
396         if (!S_ISREG(st.st_mode))
397                 return NULL;
398         fd = open(path, O_RDONLY);
399         if (fd < 0)
400                 die_errno("Error opening '%s'", path);
401         buf = xmalloc(st.st_size + 1);
402         len = read_in_full(fd, buf, st.st_size);
403         close(fd);
404         if (len != st.st_size)
405                 die("Error reading %s", path);
406         buf[len] = '\0';
407         if (prefixcmp(buf, "gitdir: "))
408                 die("Invalid gitfile format: %s", path);
409         while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
410                 len--;
411         if (len < 9)
412                 die("No path in gitfile: %s", path);
413         buf[len] = '\0';
414         dir = buf + 8;
416         if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
417                 size_t pathlen = slash+1 - path;
418                 size_t dirlen = pathlen + len - 8;
419                 dir = xmalloc(dirlen + 1);
420                 strncpy(dir, path, pathlen);
421                 strncpy(dir + pathlen, buf + 8, len - 8);
422                 dir[dirlen] = '\0';
423                 free(buf);
424                 buf = dir;
425         }
427         if (!is_git_directory(dir))
428                 die("Not a git repository: %s", dir);
429         path = real_path(dir);
431         free(buf);
432         return path;
435 static const char *setup_explicit_git_dir(const char *gitdirenv,
436                                           char *cwd, int len,
437                                           int *nongit_ok)
439         const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
440         const char *worktree;
441         char *gitfile;
442         int offset;
444         if (PATH_MAX - 40 < strlen(gitdirenv))
445                 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
447         gitfile = (char*)read_gitfile_gently(gitdirenv);
448         if (gitfile) {
449                 gitfile = xstrdup(gitfile);
450                 gitdirenv = gitfile;
451         }
453         if (!is_git_directory(gitdirenv)) {
454                 if (nongit_ok) {
455                         *nongit_ok = 1;
456                         free(gitfile);
457                         return NULL;
458                 }
459                 die("Not a git repository: '%s'", gitdirenv);
460         }
462         if (check_repository_format_gently(gitdirenv, nongit_ok)) {
463                 free(gitfile);
464                 return NULL;
465         }
467         /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
468         if (work_tree_env)
469                 set_git_work_tree(work_tree_env);
470         else if (is_bare_repository_cfg > 0) {
471                 if (git_work_tree_cfg) /* #22.2, #30 */
472                         die("core.bare and core.worktree do not make sense");
474                 /* #18, #26 */
475                 set_git_dir(gitdirenv);
476                 free(gitfile);
477                 return NULL;
478         }
479         else if (git_work_tree_cfg) { /* #6, #14 */
480                 if (is_absolute_path(git_work_tree_cfg))
481                         set_git_work_tree(git_work_tree_cfg);
482                 else {
483                         char core_worktree[PATH_MAX];
484                         if (chdir(gitdirenv))
485                                 die_errno("Could not chdir to '%s'", gitdirenv);
486                         if (chdir(git_work_tree_cfg))
487                                 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
488                         if (!getcwd(core_worktree, PATH_MAX))
489                                 die_errno("Could not get directory '%s'", git_work_tree_cfg);
490                         if (chdir(cwd))
491                                 die_errno("Could not come back to cwd");
492                         set_git_work_tree(core_worktree);
493                 }
494         }
495         else /* #2, #10 */
496                 set_git_work_tree(".");
498         /* set_git_work_tree() must have been called by now */
499         worktree = get_git_work_tree();
501         /* both get_git_work_tree() and cwd are already normalized */
502         if (!strcmp(cwd, worktree)) { /* cwd == worktree */
503                 set_git_dir(gitdirenv);
504                 free(gitfile);
505                 return NULL;
506         }
508         offset = dir_inside_of(cwd, worktree);
509         if (offset >= 0) {      /* cwd inside worktree? */
510                 set_git_dir(real_path(gitdirenv));
511                 if (chdir(worktree))
512                         die_errno("Could not chdir to '%s'", worktree);
513                 cwd[len++] = '/';
514                 cwd[len] = '\0';
515                 free(gitfile);
516                 return cwd + offset;
517         }
519         /* cwd outside worktree */
520         set_git_dir(gitdirenv);
521         free(gitfile);
522         return NULL;
525 static const char *setup_discovered_git_dir(const char *gitdir,
526                                             char *cwd, int offset, int len,
527                                             int *nongit_ok)
529         if (check_repository_format_gently(gitdir, nongit_ok))
530                 return NULL;
532         /* --work-tree is set without --git-dir; use discovered one */
533         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
534                 if (offset != len && !is_absolute_path(gitdir))
535                         gitdir = xstrdup(real_path(gitdir));
536                 if (chdir(cwd))
537                         die_errno("Could not come back to cwd");
538                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
539         }
541         /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
542         if (is_bare_repository_cfg > 0) {
543                 set_git_dir(offset == len ? gitdir : real_path(gitdir));
544                 if (chdir(cwd))
545                         die_errno("Could not come back to cwd");
546                 return NULL;
547         }
549         /* #0, #1, #5, #8, #9, #12, #13 */
550         set_git_work_tree(".");
551         if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
552                 set_git_dir(gitdir);
553         inside_git_dir = 0;
554         inside_work_tree = 1;
555         if (offset == len)
556                 return NULL;
558         /* Make "offset" point to past the '/', and add a '/' at the end */
559         offset++;
560         cwd[len++] = '/';
561         cwd[len] = 0;
562         return cwd + offset;
565 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
566 static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
568         int root_len;
570         if (check_repository_format_gently(".", nongit_ok))
571                 return NULL;
573         /* --work-tree is set without --git-dir; use discovered one */
574         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
575                 const char *gitdir;
577                 gitdir = offset == len ? "." : xmemdupz(cwd, offset);
578                 if (chdir(cwd))
579                         die_errno("Could not come back to cwd");
580                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
581         }
583         inside_git_dir = 1;
584         inside_work_tree = 0;
585         if (offset != len) {
586                 if (chdir(cwd))
587                         die_errno("Cannot come back to cwd");
588                 root_len = offset_1st_component(cwd);
589                 cwd[offset > root_len ? offset : root_len] = '\0';
590                 set_git_dir(cwd);
591         }
592         else
593                 set_git_dir(".");
594         return NULL;
597 static const char *setup_nongit(const char *cwd, int *nongit_ok)
599         if (!nongit_ok)
600                 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
601         if (chdir(cwd))
602                 die_errno("Cannot come back to cwd");
603         *nongit_ok = 1;
604         return NULL;
607 static dev_t get_device_or_die(const char *path, const char *prefix)
609         struct stat buf;
610         if (stat(path, &buf))
611                 die_errno("failed to stat '%s%s%s'",
612                                 prefix ? prefix : "",
613                                 prefix ? "/" : "", path);
614         return buf.st_dev;
617 /*
618  * We cannot decide in this function whether we are in the work tree or
619  * not, since the config can only be read _after_ this function was called.
620  */
621 static const char *setup_git_directory_gently_1(int *nongit_ok)
623         const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
624         static char cwd[PATH_MAX+1];
625         const char *gitdirenv, *ret;
626         char *gitfile;
627         int len, offset, ceil_offset;
628         dev_t current_device = 0;
629         int one_filesystem = 1;
631         /*
632          * Let's assume that we are in a git repository.
633          * If it turns out later that we are somewhere else, the value will be
634          * updated accordingly.
635          */
636         if (nongit_ok)
637                 *nongit_ok = 0;
639         if (!getcwd(cwd, sizeof(cwd)-1))
640                 die_errno("Unable to read current working directory");
641         offset = len = strlen(cwd);
643         /*
644          * If GIT_DIR is set explicitly, we're not going
645          * to do any discovery, but we still do repository
646          * validation.
647          */
648         gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
649         if (gitdirenv)
650                 return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
652         ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
653         if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
654                 ceil_offset = 1;
656         /*
657          * Test in the following order (relative to the cwd):
658          * - .git (file containing "gitdir: <path>")
659          * - .git/
660          * - ./ (bare)
661          * - ../.git
662          * - ../.git/
663          * - ../ (bare)
664          * - ../../.git/
665          *   etc.
666          */
667         one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
668         if (one_filesystem)
669                 current_device = get_device_or_die(".", NULL);
670         for (;;) {
671                 gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
672                 if (gitfile)
673                         gitdirenv = gitfile = xstrdup(gitfile);
674                 else {
675                         if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
676                                 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
677                 }
679                 if (gitdirenv) {
680                         ret = setup_discovered_git_dir(gitdirenv,
681                                                        cwd, offset, len,
682                                                        nongit_ok);
683                         free(gitfile);
684                         return ret;
685                 }
686                 free(gitfile);
688                 if (is_git_directory("."))
689                         return setup_bare_git_dir(cwd, offset, len, nongit_ok);
691                 while (--offset > ceil_offset && cwd[offset] != '/');
692                 if (offset <= ceil_offset)
693                         return setup_nongit(cwd, nongit_ok);
694                 if (one_filesystem) {
695                         dev_t parent_device = get_device_or_die("..", cwd);
696                         if (parent_device != current_device) {
697                                 if (nongit_ok) {
698                                         if (chdir(cwd))
699                                                 die_errno("Cannot come back to cwd");
700                                         *nongit_ok = 1;
701                                         return NULL;
702                                 }
703                                 cwd[offset] = '\0';
704                                 die("Not a git repository (or any parent up to mount parent %s)\n"
705                                 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
706                         }
707                 }
708                 if (chdir("..")) {
709                         cwd[offset] = '\0';
710                         die_errno("Cannot change to '%s/..'", cwd);
711                 }
712         }
715 const char *setup_git_directory_gently(int *nongit_ok)
717         const char *prefix;
719         prefix = setup_git_directory_gently_1(nongit_ok);
720         if (startup_info) {
721                 startup_info->have_repository = !nongit_ok || !*nongit_ok;
722                 startup_info->prefix = prefix;
723         }
724         return prefix;
727 int git_config_perm(const char *var, const char *value)
729         int i;
730         char *endptr;
732         if (value == NULL)
733                 return PERM_GROUP;
735         if (!strcmp(value, "umask"))
736                 return PERM_UMASK;
737         if (!strcmp(value, "group"))
738                 return PERM_GROUP;
739         if (!strcmp(value, "all") ||
740             !strcmp(value, "world") ||
741             !strcmp(value, "everybody"))
742                 return PERM_EVERYBODY;
744         /* Parse octal numbers */
745         i = strtol(value, &endptr, 8);
747         /* If not an octal number, maybe true/false? */
748         if (*endptr != 0)
749                 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
751         /*
752          * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
753          * a chmod value to restrict to.
754          */
755         switch (i) {
756         case PERM_UMASK:               /* 0 */
757                 return PERM_UMASK;
758         case OLD_PERM_GROUP:           /* 1 */
759                 return PERM_GROUP;
760         case OLD_PERM_EVERYBODY:       /* 2 */
761                 return PERM_EVERYBODY;
762         }
764         /* A filemode value was given: 0xxx */
766         if ((i & 0600) != 0600)
767                 die("Problem with core.sharedRepository filemode value "
768                     "(0%.3o).\nThe owner of files must always have "
769                     "read and write permissions.", i);
771         /*
772          * Mask filemode value. Others can not get write permission.
773          * x flags for directories are handled separately.
774          */
775         return -(i & 0666);
778 int check_repository_format_version(const char *var, const char *value, void *cb)
780         if (strcmp(var, "core.repositoryformatversion") == 0)
781                 repository_format_version = git_config_int(var, value);
782         else if (strcmp(var, "core.sharedrepository") == 0)
783                 shared_repository = git_config_perm(var, value);
784         else if (strcmp(var, "core.bare") == 0) {
785                 is_bare_repository_cfg = git_config_bool(var, value);
786                 if (is_bare_repository_cfg == 1)
787                         inside_work_tree = -1;
788         } else if (strcmp(var, "core.worktree") == 0) {
789                 if (!value)
790                         return config_error_nonbool(var);
791                 free(git_work_tree_cfg);
792                 git_work_tree_cfg = xstrdup(value);
793                 inside_work_tree = -1;
794         }
795         return 0;
798 int check_repository_format(void)
800         return check_repository_format_gently(get_git_dir(), NULL);
803 /*
804  * Returns the "prefix", a path to the current working directory
805  * relative to the work tree root, or NULL, if the current working
806  * directory is not a strict subdirectory of the work tree root. The
807  * prefix always ends with a '/' character.
808  */
809 const char *setup_git_directory(void)
811         return setup_git_directory_gently(NULL);