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