Code

fix overslow :/no-such-string-ever-existed diagnostics
[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, 1, 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 {
201                 /* shorthand */
202                 for (copyfrom = elt + 1;
203                      *copyfrom && *copyfrom != ':';
204                      copyfrom++) {
205                         char ch = *copyfrom;
207                         if (!is_pathspec_magic(ch))
208                                 break;
209                         for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
210                                 if (pathspec_magic[i].mnemonic == ch) {
211                                         magic |= pathspec_magic[i].bit;
212                                         break;
213                                 }
214                         if (ARRAY_SIZE(pathspec_magic) <= i)
215                                 die("Unimplemented pathspec magic '%c' in '%s'",
216                                     ch, elt);
217                 }
218                 if (*copyfrom == ':')
219                         copyfrom++;
220         }
222         if (magic & PATHSPEC_FROMTOP)
223                 return xstrdup(copyfrom);
224         else
225                 return prefix_path(prefix, prefixlen, copyfrom);
228 const char **get_pathspec(const char *prefix, const char **pathspec)
230         const char *entry = *pathspec;
231         const char **src, **dst;
232         int prefixlen;
234         if (!prefix && !entry)
235                 return NULL;
237         if (!entry) {
238                 static const char *spec[2];
239                 spec[0] = prefix;
240                 spec[1] = NULL;
241                 return spec;
242         }
244         /* Otherwise we have to re-write the entries.. */
245         src = pathspec;
246         dst = pathspec;
247         prefixlen = prefix ? strlen(prefix) : 0;
248         while (*src) {
249                 *(dst++) = prefix_pathspec(prefix, prefixlen, *src);
250                 src++;
251         }
252         *dst = NULL;
253         if (!*pathspec)
254                 return NULL;
255         return pathspec;
258 /*
259  * Test if it looks like we're at a git directory.
260  * We want to see:
261  *
262  *  - either an objects/ directory _or_ the proper
263  *    GIT_OBJECT_DIRECTORY environment variable
264  *  - a refs/ directory
265  *  - either a HEAD symlink or a HEAD file that is formatted as
266  *    a proper "ref:", or a regular file HEAD that has a properly
267  *    formatted sha1 object name.
268  */
269 static int is_git_directory(const char *suspect)
271         char path[PATH_MAX];
272         size_t len = strlen(suspect);
274         if (PATH_MAX <= len + strlen("/objects"))
275                 die("Too long path: %.*s", 60, suspect);
276         strcpy(path, suspect);
277         if (getenv(DB_ENVIRONMENT)) {
278                 if (access(getenv(DB_ENVIRONMENT), X_OK))
279                         return 0;
280         }
281         else {
282                 strcpy(path + len, "/objects");
283                 if (access(path, X_OK))
284                         return 0;
285         }
287         strcpy(path + len, "/refs");
288         if (access(path, X_OK))
289                 return 0;
291         strcpy(path + len, "/HEAD");
292         if (validate_headref(path))
293                 return 0;
295         return 1;
298 int is_inside_git_dir(void)
300         if (inside_git_dir < 0)
301                 inside_git_dir = is_inside_dir(get_git_dir());
302         return inside_git_dir;
305 int is_inside_work_tree(void)
307         if (inside_work_tree < 0)
308                 inside_work_tree = is_inside_dir(get_git_work_tree());
309         return inside_work_tree;
312 void setup_work_tree(void)
314         const char *work_tree, *git_dir;
315         static int initialized = 0;
317         if (initialized)
318                 return;
319         work_tree = get_git_work_tree();
320         git_dir = get_git_dir();
321         if (!is_absolute_path(git_dir))
322                 git_dir = real_path(get_git_dir());
323         if (!work_tree || chdir(work_tree))
324                 die("This operation must be run in a work tree");
326         /*
327          * Make sure subsequent git processes find correct worktree
328          * if $GIT_WORK_TREE is set relative
329          */
330         if (getenv(GIT_WORK_TREE_ENVIRONMENT))
331                 setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
333         set_git_dir(relative_path(git_dir, work_tree));
334         initialized = 1;
337 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
339         char repo_config[PATH_MAX+1];
341         /*
342          * git_config() can't be used here because it calls git_pathdup()
343          * to get $GIT_CONFIG/config. That call will make setup_git_env()
344          * set git_dir to ".git".
345          *
346          * We are in gitdir setup, no git dir has been found useable yet.
347          * Use a gentler version of git_config() to check if this repo
348          * is a good one.
349          */
350         snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
351         git_config_early(check_repository_format_version, NULL, repo_config);
352         if (GIT_REPO_VERSION < repository_format_version) {
353                 if (!nongit_ok)
354                         die ("Expected git repo version <= %d, found %d",
355                              GIT_REPO_VERSION, repository_format_version);
356                 warning("Expected git repo version <= %d, found %d",
357                         GIT_REPO_VERSION, repository_format_version);
358                 warning("Please upgrade Git");
359                 *nongit_ok = -1;
360                 return -1;
361         }
362         return 0;
365 /*
366  * Try to read the location of the git directory from the .git file,
367  * return path to git directory if found.
368  */
369 const char *read_gitfile_gently(const char *path)
371         char *buf;
372         char *dir;
373         const char *slash;
374         struct stat st;
375         int fd;
376         size_t len;
378         if (stat(path, &st))
379                 return NULL;
380         if (!S_ISREG(st.st_mode))
381                 return NULL;
382         fd = open(path, O_RDONLY);
383         if (fd < 0)
384                 die_errno("Error opening '%s'", path);
385         buf = xmalloc(st.st_size + 1);
386         len = read_in_full(fd, buf, st.st_size);
387         close(fd);
388         if (len != st.st_size)
389                 die("Error reading %s", path);
390         buf[len] = '\0';
391         if (prefixcmp(buf, "gitdir: "))
392                 die("Invalid gitfile format: %s", path);
393         while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
394                 len--;
395         if (len < 9)
396                 die("No path in gitfile: %s", path);
397         buf[len] = '\0';
398         dir = buf + 8;
400         if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
401                 size_t pathlen = slash+1 - path;
402                 size_t dirlen = pathlen + len - 8;
403                 dir = xmalloc(dirlen + 1);
404                 strncpy(dir, path, pathlen);
405                 strncpy(dir + pathlen, buf + 8, len - 8);
406                 dir[dirlen] = '\0';
407                 free(buf);
408                 buf = dir;
409         }
411         if (!is_git_directory(dir))
412                 die("Not a git repository: %s", dir);
413         path = real_path(dir);
415         free(buf);
416         return path;
419 static const char *setup_explicit_git_dir(const char *gitdirenv,
420                                           char *cwd, int len,
421                                           int *nongit_ok)
423         const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
424         const char *worktree;
425         char *gitfile;
427         if (PATH_MAX - 40 < strlen(gitdirenv))
428                 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
430         gitfile = (char*)read_gitfile_gently(gitdirenv);
431         if (gitfile) {
432                 gitfile = xstrdup(gitfile);
433                 gitdirenv = gitfile;
434         }
436         if (!is_git_directory(gitdirenv)) {
437                 if (nongit_ok) {
438                         *nongit_ok = 1;
439                         free(gitfile);
440                         return NULL;
441                 }
442                 die("Not a git repository: '%s'", gitdirenv);
443         }
445         if (check_repository_format_gently(gitdirenv, nongit_ok)) {
446                 free(gitfile);
447                 return NULL;
448         }
450         /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
451         if (work_tree_env)
452                 set_git_work_tree(work_tree_env);
453         else if (is_bare_repository_cfg > 0) {
454                 if (git_work_tree_cfg) /* #22.2, #30 */
455                         die("core.bare and core.worktree do not make sense");
457                 /* #18, #26 */
458                 set_git_dir(gitdirenv);
459                 free(gitfile);
460                 return NULL;
461         }
462         else if (git_work_tree_cfg) { /* #6, #14 */
463                 if (is_absolute_path(git_work_tree_cfg))
464                         set_git_work_tree(git_work_tree_cfg);
465                 else {
466                         char core_worktree[PATH_MAX];
467                         if (chdir(gitdirenv))
468                                 die_errno("Could not chdir to '%s'", gitdirenv);
469                         if (chdir(git_work_tree_cfg))
470                                 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
471                         if (!getcwd(core_worktree, PATH_MAX))
472                                 die_errno("Could not get directory '%s'", git_work_tree_cfg);
473                         if (chdir(cwd))
474                                 die_errno("Could not come back to cwd");
475                         set_git_work_tree(core_worktree);
476                 }
477         }
478         else /* #2, #10 */
479                 set_git_work_tree(".");
481         /* set_git_work_tree() must have been called by now */
482         worktree = get_git_work_tree();
484         /* both get_git_work_tree() and cwd are already normalized */
485         if (!strcmp(cwd, worktree)) { /* cwd == worktree */
486                 set_git_dir(gitdirenv);
487                 free(gitfile);
488                 return NULL;
489         }
491         if (!prefixcmp(cwd, worktree) &&
492             cwd[strlen(worktree)] == '/') { /* cwd inside worktree */
493                 set_git_dir(real_path(gitdirenv));
494                 if (chdir(worktree))
495                         die_errno("Could not chdir to '%s'", worktree);
496                 cwd[len++] = '/';
497                 cwd[len] = '\0';
498                 free(gitfile);
499                 return cwd + strlen(worktree) + 1;
500         }
502         /* cwd outside worktree */
503         set_git_dir(gitdirenv);
504         free(gitfile);
505         return NULL;
508 static const char *setup_discovered_git_dir(const char *gitdir,
509                                             char *cwd, int offset, int len,
510                                             int *nongit_ok)
512         if (check_repository_format_gently(gitdir, nongit_ok))
513                 return NULL;
515         /* --work-tree is set without --git-dir; use discovered one */
516         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
517                 if (offset != len && !is_absolute_path(gitdir))
518                         gitdir = xstrdup(real_path(gitdir));
519                 if (chdir(cwd))
520                         die_errno("Could not come back to cwd");
521                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
522         }
524         /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
525         if (is_bare_repository_cfg > 0) {
526                 set_git_dir(offset == len ? gitdir : real_path(gitdir));
527                 if (chdir(cwd))
528                         die_errno("Could not come back to cwd");
529                 return NULL;
530         }
532         /* #0, #1, #5, #8, #9, #12, #13 */
533         set_git_work_tree(".");
534         if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
535                 set_git_dir(gitdir);
536         inside_git_dir = 0;
537         inside_work_tree = 1;
538         if (offset == len)
539                 return NULL;
541         /* Make "offset" point to past the '/', and add a '/' at the end */
542         offset++;
543         cwd[len++] = '/';
544         cwd[len] = 0;
545         return cwd + offset;
548 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
549 static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
551         int root_len;
553         if (check_repository_format_gently(".", nongit_ok))
554                 return NULL;
556         /* --work-tree is set without --git-dir; use discovered one */
557         if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
558                 const char *gitdir;
560                 gitdir = offset == len ? "." : xmemdupz(cwd, offset);
561                 if (chdir(cwd))
562                         die_errno("Could not come back to cwd");
563                 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
564         }
566         inside_git_dir = 1;
567         inside_work_tree = 0;
568         if (offset != len) {
569                 if (chdir(cwd))
570                         die_errno("Cannot come back to cwd");
571                 root_len = offset_1st_component(cwd);
572                 cwd[offset > root_len ? offset : root_len] = '\0';
573                 set_git_dir(cwd);
574         }
575         else
576                 set_git_dir(".");
577         return NULL;
580 static const char *setup_nongit(const char *cwd, int *nongit_ok)
582         if (!nongit_ok)
583                 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
584         if (chdir(cwd))
585                 die_errno("Cannot come back to cwd");
586         *nongit_ok = 1;
587         return NULL;
590 static dev_t get_device_or_die(const char *path, const char *prefix)
592         struct stat buf;
593         if (stat(path, &buf))
594                 die_errno("failed to stat '%s%s%s'",
595                                 prefix ? prefix : "",
596                                 prefix ? "/" : "", path);
597         return buf.st_dev;
600 /*
601  * We cannot decide in this function whether we are in the work tree or
602  * not, since the config can only be read _after_ this function was called.
603  */
604 static const char *setup_git_directory_gently_1(int *nongit_ok)
606         const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
607         static char cwd[PATH_MAX+1];
608         const char *gitdirenv, *ret;
609         char *gitfile;
610         int len, offset, ceil_offset;
611         dev_t current_device = 0;
612         int one_filesystem = 1;
614         /*
615          * Let's assume that we are in a git repository.
616          * If it turns out later that we are somewhere else, the value will be
617          * updated accordingly.
618          */
619         if (nongit_ok)
620                 *nongit_ok = 0;
622         if (!getcwd(cwd, sizeof(cwd)-1))
623                 die_errno("Unable to read current working directory");
624         offset = len = strlen(cwd);
626         /*
627          * If GIT_DIR is set explicitly, we're not going
628          * to do any discovery, but we still do repository
629          * validation.
630          */
631         gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
632         if (gitdirenv)
633                 return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
635         ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
636         if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
637                 ceil_offset = 1;
639         /*
640          * Test in the following order (relative to the cwd):
641          * - .git (file containing "gitdir: <path>")
642          * - .git/
643          * - ./ (bare)
644          * - ../.git
645          * - ../.git/
646          * - ../ (bare)
647          * - ../../.git/
648          *   etc.
649          */
650         one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
651         if (one_filesystem)
652                 current_device = get_device_or_die(".", NULL);
653         for (;;) {
654                 gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
655                 if (gitfile)
656                         gitdirenv = gitfile = xstrdup(gitfile);
657                 else {
658                         if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
659                                 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
660                 }
662                 if (gitdirenv) {
663                         ret = setup_discovered_git_dir(gitdirenv,
664                                                        cwd, offset, len,
665                                                        nongit_ok);
666                         free(gitfile);
667                         return ret;
668                 }
669                 free(gitfile);
671                 if (is_git_directory("."))
672                         return setup_bare_git_dir(cwd, offset, len, nongit_ok);
674                 while (--offset > ceil_offset && cwd[offset] != '/');
675                 if (offset <= ceil_offset)
676                         return setup_nongit(cwd, nongit_ok);
677                 if (one_filesystem) {
678                         dev_t parent_device = get_device_or_die("..", cwd);
679                         if (parent_device != current_device) {
680                                 if (nongit_ok) {
681                                         if (chdir(cwd))
682                                                 die_errno("Cannot come back to cwd");
683                                         *nongit_ok = 1;
684                                         return NULL;
685                                 }
686                                 cwd[offset] = '\0';
687                                 die("Not a git repository (or any parent up to mount parent %s)\n"
688                                 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
689                         }
690                 }
691                 if (chdir("..")) {
692                         cwd[offset] = '\0';
693                         die_errno("Cannot change to '%s/..'", cwd);
694                 }
695         }
698 const char *setup_git_directory_gently(int *nongit_ok)
700         const char *prefix;
702         prefix = setup_git_directory_gently_1(nongit_ok);
703         if (startup_info) {
704                 startup_info->have_repository = !nongit_ok || !*nongit_ok;
705                 startup_info->prefix = prefix;
706         }
707         return prefix;
710 int git_config_perm(const char *var, const char *value)
712         int i;
713         char *endptr;
715         if (value == NULL)
716                 return PERM_GROUP;
718         if (!strcmp(value, "umask"))
719                 return PERM_UMASK;
720         if (!strcmp(value, "group"))
721                 return PERM_GROUP;
722         if (!strcmp(value, "all") ||
723             !strcmp(value, "world") ||
724             !strcmp(value, "everybody"))
725                 return PERM_EVERYBODY;
727         /* Parse octal numbers */
728         i = strtol(value, &endptr, 8);
730         /* If not an octal number, maybe true/false? */
731         if (*endptr != 0)
732                 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
734         /*
735          * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
736          * a chmod value to restrict to.
737          */
738         switch (i) {
739         case PERM_UMASK:               /* 0 */
740                 return PERM_UMASK;
741         case OLD_PERM_GROUP:           /* 1 */
742                 return PERM_GROUP;
743         case OLD_PERM_EVERYBODY:       /* 2 */
744                 return PERM_EVERYBODY;
745         }
747         /* A filemode value was given: 0xxx */
749         if ((i & 0600) != 0600)
750                 die("Problem with core.sharedRepository filemode value "
751                     "(0%.3o).\nThe owner of files must always have "
752                     "read and write permissions.", i);
754         /*
755          * Mask filemode value. Others can not get write permission.
756          * x flags for directories are handled separately.
757          */
758         return -(i & 0666);
761 int check_repository_format_version(const char *var, const char *value, void *cb)
763         if (strcmp(var, "core.repositoryformatversion") == 0)
764                 repository_format_version = git_config_int(var, value);
765         else if (strcmp(var, "core.sharedrepository") == 0)
766                 shared_repository = git_config_perm(var, value);
767         else if (strcmp(var, "core.bare") == 0) {
768                 is_bare_repository_cfg = git_config_bool(var, value);
769                 if (is_bare_repository_cfg == 1)
770                         inside_work_tree = -1;
771         } else if (strcmp(var, "core.worktree") == 0) {
772                 if (!value)
773                         return config_error_nonbool(var);
774                 free(git_work_tree_cfg);
775                 git_work_tree_cfg = xstrdup(value);
776                 inside_work_tree = -1;
777         }
778         return 0;
781 int check_repository_format(void)
783         return check_repository_format_gently(get_git_dir(), NULL);
786 /*
787  * Returns the "prefix", a path to the current working directory
788  * relative to the work tree root, or NULL, if the current working
789  * directory is not a strict subdirectory of the work tree root. The
790  * prefix always ends with a '/' character.
791  */
792 const char *setup_git_directory(void)
794         return setup_git_directory_gently(NULL);