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