Code

Revert "magic pathspec: add ":(icase)path" to match case insensitively"
[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 /*
130  * Magic pathspec
131  *
132  * NEEDSWORK: These need to be moved to dir.h or even to a new
133  * pathspec.h when we restructure get_pathspec() users to use the
134  * "struct pathspec" interface.
135  *
136  * Possible future magic semantics include stuff like:
137  *
138  *      { PATHSPEC_NOGLOB, '!', "noglob" },
139  *      { PATHSPEC_ICASE, '\0', "icase" },
140  *      { PATHSPEC_RECURSIVE, '*', "recursive" },
141  *      { PATHSPEC_REGEXP, '\0', "regexp" },
142  *
143  */
144 #define PATHSPEC_FROMTOP    (1<<0)
146 struct pathspec_magic {
147         unsigned bit;
148         char mnemonic; /* this cannot be ':'! */
149         const char *name;
150 } pathspec_magic[] = {
151         { PATHSPEC_FROMTOP, '/', "top" },
152 };
154 /*
155  * Take an element of a pathspec and check for magic signatures.
156  * Append the result to the prefix.
157  *
158  * For now, we only parse the syntax and throw out anything other than
159  * "top" magic.
160  *
161  * NEEDSWORK: This needs to be rewritten when we start migrating
162  * get_pathspec() users to use the "struct pathspec" interface.  For
163  * example, a pathspec element may be marked as case-insensitive, but
164  * the prefix part must always match literally, and a single stupid
165  * string cannot express such a case.
166  */
167 const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)
169         unsigned magic = 0;
170         const char *copyfrom = elt;
171         int i;
173         if (elt[0] != ':') {
174                 ; /* nothing to do */
175         } else if (elt[1] == '(') {
176                 /* longhand */
177                 const char *nextat;
178                 for (copyfrom = elt + 2;
179                      *copyfrom && *copyfrom != ')';
180                      copyfrom = nextat) {
181                         size_t len = strcspn(copyfrom, ",)");
182                         if (copyfrom[len] == ')')
183                                 nextat = copyfrom + len;
184                         else
185                                 nextat = copyfrom + len + 1;
186                         if (!len)
187                                 continue;
188                         for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
189                                 if (strlen(pathspec_magic[i].name) == len &&
190                                     !strncmp(pathspec_magic[i].name, copyfrom, len)) {
191                                         magic |= pathspec_magic[i].bit;
192                                         break;
193                                 }
194                         if (ARRAY_SIZE(pathspec_magic) <= i)
195                                 die("Invalid pathspec magic '%.*s' in '%s'",
196                                     (int) len, copyfrom, elt);
197                 }
198                 if (*copyfrom == ')')
199                         copyfrom++;
200         } else if (!elt[1]) {
201                 /* Just ':' -- no element! */
202                 return NULL;
203         } else {
204                 /* shorthand */
205                 for (copyfrom = elt + 1;
206                      *copyfrom && *copyfrom != ':';
207                      copyfrom++) {
208                         char ch = *copyfrom;
210                         if (!is_pathspec_magic(ch))
211                                 break;
212                         for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
213                                 if (pathspec_magic[i].mnemonic == ch) {
214                                         magic |= pathspec_magic[i].bit;
215                                         break;
216                                 }
217                         if (ARRAY_SIZE(pathspec_magic) <= i)
218                                 die("Unimplemented pathspec magic '%c' in '%s'",
219                                     ch, elt);
220                 }
221                 if (*copyfrom == ':')
222                         copyfrom++;
223         }
225         if (magic & PATHSPEC_FROMTOP)
226                 return xstrdup(copyfrom);
227         else
228                 return prefix_path(prefix, prefixlen, copyfrom);
231 const char **get_pathspec(const char *prefix, const char **pathspec)
233         const char *entry = *pathspec;
234         const char **src, **dst;
235         int prefixlen;
237         if (!prefix && !entry)
238                 return NULL;
240         if (!entry) {
241                 static const char *spec[2];
242                 spec[0] = prefix;
243                 spec[1] = NULL;
244                 return spec;
245         }
247         /* Otherwise we have to re-write the entries.. */
248         src = pathspec;
249         dst = pathspec;
250         prefixlen = prefix ? strlen(prefix) : 0;
251         while (*src) {
252                 *(dst++) = prefix_pathspec(prefix, prefixlen, *src);
253                 src++;
254         }
255         *dst = NULL;
256         if (!*pathspec)
257                 return NULL;
258         return pathspec;
261 /*
262  * Test if it looks like we're at a git directory.
263  * We want to see:
264  *
265  *  - either an objects/ directory _or_ the proper
266  *    GIT_OBJECT_DIRECTORY environment variable
267  *  - a refs/ directory
268  *  - either a HEAD symlink or a HEAD file that is formatted as
269  *    a proper "ref:", or a regular file HEAD that has a properly
270  *    formatted sha1 object name.
271  */
272 static int is_git_directory(const char *suspect)
274         char path[PATH_MAX];
275         size_t len = strlen(suspect);
277         if (PATH_MAX <= len + strlen("/objects"))
278                 die("Too long path: %.*s", 60, suspect);
279         strcpy(path, suspect);
280         if (getenv(DB_ENVIRONMENT)) {
281                 if (access(getenv(DB_ENVIRONMENT), X_OK))
282                         return 0;
283         }
284         else {
285                 strcpy(path + len, "/objects");
286                 if (access(path, X_OK))
287                         return 0;
288         }
290         strcpy(path + len, "/refs");
291         if (access(path, X_OK))
292                 return 0;
294         strcpy(path + len, "/HEAD");
295         if (validate_headref(path))
296                 return 0;
298         return 1;
301 int is_inside_git_dir(void)
303         if (inside_git_dir < 0)
304                 inside_git_dir = is_inside_dir(get_git_dir());
305         return inside_git_dir;
308 int is_inside_work_tree(void)
310         if (inside_work_tree < 0)
311                 inside_work_tree = is_inside_dir(get_git_work_tree());
312         return inside_work_tree;
315 void setup_work_tree(void)
317         const char *work_tree, *git_dir;
318         static int initialized = 0;
320         if (initialized)
321                 return;
322         work_tree = get_git_work_tree();
323         git_dir = get_git_dir();
324         if (!is_absolute_path(git_dir))
325                 git_dir = real_path(get_git_dir());
326         if (!work_tree || chdir(work_tree))
327                 die("This operation must be run in a work tree");
329         /*
330          * Make sure subsequent git processes find correct worktree
331          * if $GIT_WORK_TREE is set relative
332          */
333         if (getenv(GIT_WORK_TREE_ENVIRONMENT))
334                 setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
336         set_git_dir(relative_path(git_dir, work_tree));
337         initialized = 1;
340 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
342         char repo_config[PATH_MAX+1];
344         /*
345          * git_config() can't be used here because it calls git_pathdup()
346          * to get $GIT_CONFIG/config. That call will make setup_git_env()
347          * set git_dir to ".git".
348          *
349          * We are in gitdir setup, no git dir has been found useable yet.
350          * Use a gentler version of git_config() to check if this repo
351          * is a good one.
352          */
353         snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
354         git_config_early(check_repository_format_version, NULL, repo_config);
355         if (GIT_REPO_VERSION < repository_format_version) {
356                 if (!nongit_ok)
357                         die ("Expected git repo version <= %d, found %d",
358                              GIT_REPO_VERSION, repository_format_version);
359                 warning("Expected git repo version <= %d, found %d",
360                         GIT_REPO_VERSION, repository_format_version);
361                 warning("Please upgrade Git");
362                 *nongit_ok = -1;
363                 return -1;
364         }
365         return 0;
368 /*
369  * Try to read the location of the git directory from the .git file,
370  * return path to git directory if found.
371  */
372 const char *read_gitfile_gently(const char *path)
374         char *buf;
375         char *dir;
376         const char *slash;
377         struct stat st;
378         int fd;
379         size_t len;
381         if (stat(path, &st))
382                 return NULL;
383         if (!S_ISREG(st.st_mode))
384                 return NULL;
385         fd = open(path, O_RDONLY);
386         if (fd < 0)
387                 die_errno("Error opening '%s'", path);
388         buf = xmalloc(st.st_size + 1);
389         len = read_in_full(fd, buf, st.st_size);
390         close(fd);
391         if (len != st.st_size)
392                 die("Error reading %s", path);
393         buf[len] = '\0';
394         if (prefixcmp(buf, "gitdir: "))
395                 die("Invalid gitfile format: %s", path);
396         while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
397                 len--;
398         if (len < 9)
399                 die("No path in gitfile: %s", path);
400         buf[len] = '\0';
401         dir = buf + 8;
403         if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
404                 size_t pathlen = slash+1 - path;
405                 size_t dirlen = pathlen + len - 8;
406                 dir = xmalloc(dirlen + 1);
407                 strncpy(dir, path, pathlen);
408                 strncpy(dir + pathlen, buf + 8, len - 8);
409                 dir[dirlen] = '\0';
410                 free(buf);
411                 buf = dir;
412         }
414         if (!is_git_directory(dir))
415                 die("Not a git repository: %s", dir);
416         path = real_path(dir);
418         free(buf);
419         return path;
422 static const char *setup_explicit_git_dir(const char *gitdirenv,
423                                           char *cwd, int len,
424                                           int *nongit_ok)
426         const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
427         const char *worktree;
428         char *gitfile;
430         if (PATH_MAX - 40 < strlen(gitdirenv))
431                 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
433         gitfile = (char*)read_gitfile_gently(gitdirenv);
434         if (gitfile) {
435                 gitfile = xstrdup(gitfile);
436                 gitdirenv = gitfile;
437         }
439         if (!is_git_directory(gitdirenv)) {
440                 if (nongit_ok) {
441                         *nongit_ok = 1;
442                         free(gitfile);
443                         return NULL;
444                 }
445                 die("Not a git repository: '%s'", gitdirenv);
446         }
448         if (check_repository_format_gently(gitdirenv, nongit_ok)) {
449                 free(gitfile);
450                 return NULL;
451         }
453         /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
454         if (work_tree_env)
455                 set_git_work_tree(work_tree_env);
456         else if (is_bare_repository_cfg > 0) {
457                 if (git_work_tree_cfg) /* #22.2, #30 */
458                         die("core.bare and core.worktree do not make sense");
460                 /* #18, #26 */
461                 set_git_dir(gitdirenv);
462                 free(gitfile);
463                 return NULL;
464         }
465         else if (git_work_tree_cfg) { /* #6, #14 */
466                 if (is_absolute_path(git_work_tree_cfg))
467                         set_git_work_tree(git_work_tree_cfg);
468                 else {
469                         char core_worktree[PATH_MAX];
470                         if (chdir(gitdirenv))
471                                 die_errno("Could not chdir to '%s'", gitdirenv);
472                         if (chdir(git_work_tree_cfg))
473                                 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
474                         if (!getcwd(core_worktree, PATH_MAX))
475                                 die_errno("Could not get directory '%s'", git_work_tree_cfg);
476                         if (chdir(cwd))
477                                 die_errno("Could not come back to cwd");
478                         set_git_work_tree(core_worktree);
479                 }
480         }
481         else /* #2, #10 */
482                 set_git_work_tree(".");
484         /* set_git_work_tree() must have been called by now */
485         worktree = get_git_work_tree();
487         /* both get_git_work_tree() and cwd are already normalized */
488         if (!strcmp(cwd, worktree)) { /* cwd == worktree */
489                 set_git_dir(gitdirenv);
490                 free(gitfile);
491                 return NULL;
492         }
494         if (!prefixcmp(cwd, worktree) &&
495             cwd[strlen(worktree)] == '/') { /* cwd inside worktree */
496                 set_git_dir(real_path(gitdirenv));
497                 if (chdir(worktree))
498                         die_errno("Could not chdir to '%s'", worktree);
499                 cwd[len++] = '/';
500                 cwd[len] = '\0';
501                 free(gitfile);
502                 return cwd + strlen(worktree) + 1;
503         }
505         /* cwd outside worktree */
506         set_git_dir(gitdirenv);
507         free(gitfile);
508         return NULL;
511 static const char *setup_discovered_git_dir(const char *gitdir,
512                                             char *cwd, int offset, int len,
513                                             int *nongit_ok)
515         if (check_repository_format_gently(gitdir, nongit_ok))
516                 return NULL;
518         /* --work-tree is set without --git-dir; use discovered one */
519         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
520                 if (offset != len && !is_absolute_path(gitdir))
521                         gitdir = xstrdup(real_path(gitdir));
522                 if (chdir(cwd))
523                         die_errno("Could not come back to cwd");
524                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
525         }
527         /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
528         if (is_bare_repository_cfg > 0) {
529                 set_git_dir(offset == len ? gitdir : real_path(gitdir));
530                 if (chdir(cwd))
531                         die_errno("Could not come back to cwd");
532                 return NULL;
533         }
535         /* #0, #1, #5, #8, #9, #12, #13 */
536         set_git_work_tree(".");
537         if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
538                 set_git_dir(gitdir);
539         inside_git_dir = 0;
540         inside_work_tree = 1;
541         if (offset == len)
542                 return NULL;
544         /* Make "offset" point to past the '/', and add a '/' at the end */
545         offset++;
546         cwd[len++] = '/';
547         cwd[len] = 0;
548         return cwd + offset;
551 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
552 static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
554         int root_len;
556         if (check_repository_format_gently(".", nongit_ok))
557                 return NULL;
559         /* --work-tree is set without --git-dir; use discovered one */
560         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
561                 const char *gitdir;
563                 gitdir = offset == len ? "." : xmemdupz(cwd, offset);
564                 if (chdir(cwd))
565                         die_errno("Could not come back to cwd");
566                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
567         }
569         inside_git_dir = 1;
570         inside_work_tree = 0;
571         if (offset != len) {
572                 if (chdir(cwd))
573                         die_errno("Cannot come back to cwd");
574                 root_len = offset_1st_component(cwd);
575                 cwd[offset > root_len ? offset : root_len] = '\0';
576                 set_git_dir(cwd);
577         }
578         else
579                 set_git_dir(".");
580         return NULL;
583 static const char *setup_nongit(const char *cwd, int *nongit_ok)
585         if (!nongit_ok)
586                 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
587         if (chdir(cwd))
588                 die_errno("Cannot come back to cwd");
589         *nongit_ok = 1;
590         return NULL;
593 static dev_t get_device_or_die(const char *path, const char *prefix)
595         struct stat buf;
596         if (stat(path, &buf))
597                 die_errno("failed to stat '%s%s%s'",
598                                 prefix ? prefix : "",
599                                 prefix ? "/" : "", path);
600         return buf.st_dev;
603 /*
604  * We cannot decide in this function whether we are in the work tree or
605  * not, since the config can only be read _after_ this function was called.
606  */
607 static const char *setup_git_directory_gently_1(int *nongit_ok)
609         const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
610         static char cwd[PATH_MAX+1];
611         const char *gitdirenv, *ret;
612         char *gitfile;
613         int len, offset, ceil_offset;
614         dev_t current_device = 0;
615         int one_filesystem = 1;
617         /*
618          * Let's assume that we are in a git repository.
619          * If it turns out later that we are somewhere else, the value will be
620          * updated accordingly.
621          */
622         if (nongit_ok)
623                 *nongit_ok = 0;
625         if (!getcwd(cwd, sizeof(cwd)-1))
626                 die_errno("Unable to read current working directory");
627         offset = len = strlen(cwd);
629         /*
630          * If GIT_DIR is set explicitly, we're not going
631          * to do any discovery, but we still do repository
632          * validation.
633          */
634         gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
635         if (gitdirenv)
636                 return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
638         ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
639         if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
640                 ceil_offset = 1;
642         /*
643          * Test in the following order (relative to the cwd):
644          * - .git (file containing "gitdir: <path>")
645          * - .git/
646          * - ./ (bare)
647          * - ../.git
648          * - ../.git/
649          * - ../ (bare)
650          * - ../../.git/
651          *   etc.
652          */
653         one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
654         if (one_filesystem)
655                 current_device = get_device_or_die(".", NULL);
656         for (;;) {
657                 gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
658                 if (gitfile)
659                         gitdirenv = gitfile = xstrdup(gitfile);
660                 else {
661                         if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
662                                 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
663                 }
665                 if (gitdirenv) {
666                         ret = setup_discovered_git_dir(gitdirenv,
667                                                        cwd, offset, len,
668                                                        nongit_ok);
669                         free(gitfile);
670                         return ret;
671                 }
672                 free(gitfile);
674                 if (is_git_directory("."))
675                         return setup_bare_git_dir(cwd, offset, len, nongit_ok);
677                 while (--offset > ceil_offset && cwd[offset] != '/');
678                 if (offset <= ceil_offset)
679                         return setup_nongit(cwd, nongit_ok);
680                 if (one_filesystem) {
681                         dev_t parent_device = get_device_or_die("..", cwd);
682                         if (parent_device != current_device) {
683                                 if (nongit_ok) {
684                                         if (chdir(cwd))
685                                                 die_errno("Cannot come back to cwd");
686                                         *nongit_ok = 1;
687                                         return NULL;
688                                 }
689                                 cwd[offset] = '\0';
690                                 die("Not a git repository (or any parent up to mount parent %s)\n"
691                                 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
692                         }
693                 }
694                 if (chdir("..")) {
695                         cwd[offset] = '\0';
696                         die_errno("Cannot change to '%s/..'", cwd);
697                 }
698         }
701 const char *setup_git_directory_gently(int *nongit_ok)
703         const char *prefix;
705         prefix = setup_git_directory_gently_1(nongit_ok);
706         if (startup_info) {
707                 startup_info->have_repository = !nongit_ok || !*nongit_ok;
708                 startup_info->prefix = prefix;
709         }
710         return prefix;
713 int git_config_perm(const char *var, const char *value)
715         int i;
716         char *endptr;
718         if (value == NULL)
719                 return PERM_GROUP;
721         if (!strcmp(value, "umask"))
722                 return PERM_UMASK;
723         if (!strcmp(value, "group"))
724                 return PERM_GROUP;
725         if (!strcmp(value, "all") ||
726             !strcmp(value, "world") ||
727             !strcmp(value, "everybody"))
728                 return PERM_EVERYBODY;
730         /* Parse octal numbers */
731         i = strtol(value, &endptr, 8);
733         /* If not an octal number, maybe true/false? */
734         if (*endptr != 0)
735                 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
737         /*
738          * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
739          * a chmod value to restrict to.
740          */
741         switch (i) {
742         case PERM_UMASK:               /* 0 */
743                 return PERM_UMASK;
744         case OLD_PERM_GROUP:           /* 1 */
745                 return PERM_GROUP;
746         case OLD_PERM_EVERYBODY:       /* 2 */
747                 return PERM_EVERYBODY;
748         }
750         /* A filemode value was given: 0xxx */
752         if ((i & 0600) != 0600)
753                 die("Problem with core.sharedRepository filemode value "
754                     "(0%.3o).\nThe owner of files must always have "
755                     "read and write permissions.", i);
757         /*
758          * Mask filemode value. Others can not get write permission.
759          * x flags for directories are handled separately.
760          */
761         return -(i & 0666);
764 int check_repository_format_version(const char *var, const char *value, void *cb)
766         if (strcmp(var, "core.repositoryformatversion") == 0)
767                 repository_format_version = git_config_int(var, value);
768         else if (strcmp(var, "core.sharedrepository") == 0)
769                 shared_repository = git_config_perm(var, value);
770         else if (strcmp(var, "core.bare") == 0) {
771                 is_bare_repository_cfg = git_config_bool(var, value);
772                 if (is_bare_repository_cfg == 1)
773                         inside_work_tree = -1;
774         } else if (strcmp(var, "core.worktree") == 0) {
775                 if (!value)
776                         return config_error_nonbool(var);
777                 free(git_work_tree_cfg);
778                 git_work_tree_cfg = xstrdup(value);
779                 inside_work_tree = -1;
780         }
781         return 0;
784 int check_repository_format(void)
786         return check_repository_format_gently(get_git_dir(), NULL);
789 /*
790  * Returns the "prefix", a path to the current working directory
791  * relative to the work tree root, or NULL, if the current working
792  * directory is not a strict subdirectory of the work tree root. The
793  * prefix always ends with a '/' character.
794  */
795 const char *setup_git_directory(void)
797         return setup_git_directory_gently(NULL);