Git 1.7.10.5
[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;
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
5f5608bc 239/*
ad1a382f 240 * Test if it looks like we're at a git directory.
5e7bfe25 241 * We want to see:
5f5608bc 242 *
790296fd 243 * - either an objects/ directory _or_ the proper
5f5608bc 244 * GIT_OBJECT_DIRECTORY environment variable
ad1a382f 245 * - a refs/ directory
8098a178 246 * - either a HEAD symlink or a HEAD file that is formatted as
c847f537
JH
247 * a proper "ref:", or a regular file HEAD that has a properly
248 * formatted sha1 object name.
5f5608bc 249 */
b3256eb8 250int is_git_directory(const char *suspect)
5f5608bc 251{
ad1a382f
SP
252 char path[PATH_MAX];
253 size_t len = strlen(suspect);
254
3c9d0414
GB
255 if (PATH_MAX <= len + strlen("/objects"))
256 die("Too long path: %.*s", 60, suspect);
ad1a382f
SP
257 strcpy(path, suspect);
258 if (getenv(DB_ENVIRONMENT)) {
259 if (access(getenv(DB_ENVIRONMENT), X_OK))
260 return 0;
261 }
262 else {
263 strcpy(path + len, "/objects");
264 if (access(path, X_OK))
265 return 0;
266 }
267
268 strcpy(path + len, "/refs");
269 if (access(path, X_OK))
8098a178 270 return 0;
ad1a382f
SP
271
272 strcpy(path + len, "/HEAD");
c847f537 273 if (validate_headref(path))
ad1a382f
SP
274 return 0;
275
8098a178 276 return 1;
5f5608bc
LT
277}
278
68025633
JS
279int is_inside_git_dir(void)
280{
e90fdc39
JS
281 if (inside_git_dir < 0)
282 inside_git_dir = is_inside_dir(get_git_dir());
283 return inside_git_dir;
892c41b9
ML
284}
285
892c41b9
ML
286int is_inside_work_tree(void)
287{
e90fdc39
JS
288 if (inside_work_tree < 0)
289 inside_work_tree = is_inside_dir(get_git_work_tree());
290 return inside_work_tree;
892c41b9
ML
291}
292
f3fa1838
JH
293void setup_work_tree(void)
294{
354e6534
JS
295 const char *work_tree, *git_dir;
296 static int initialized = 0;
297
298 if (initialized)
299 return;
300 work_tree = get_git_work_tree();
301 git_dir = get_git_dir();
59f0f2f3 302 if (!is_absolute_path(git_dir))
e2a57aac 303 git_dir = real_path(get_git_dir());
59f0f2f3
MH
304 if (!work_tree || chdir(work_tree))
305 die("This operation must be run in a work tree");
0ed74813
NTND
306
307 /*
308 * Make sure subsequent git processes find correct worktree
309 * if $GIT_WORK_TREE is set relative
310 */
311 if (getenv(GIT_WORK_TREE_ENVIRONMENT))
312 setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
313
e2a57aac 314 set_git_dir(relative_path(git_dir, work_tree));
354e6534 315 initialized = 1;
59f0f2f3
MH
316}
317
337e51ce 318static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
9459aa77 319{
337e51ce
NTND
320 char repo_config[PATH_MAX+1];
321
322 /*
323 * git_config() can't be used here because it calls git_pathdup()
324 * to get $GIT_CONFIG/config. That call will make setup_git_env()
325 * set git_dir to ".git".
326 *
327 * We are in gitdir setup, no git dir has been found useable yet.
328 * Use a gentler version of git_config() to check if this repo
329 * is a good one.
330 */
331 snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
332 git_config_early(check_repository_format_version, NULL, repo_config);
9459aa77
NTND
333 if (GIT_REPO_VERSION < repository_format_version) {
334 if (!nongit_ok)
335 die ("Expected git repo version <= %d, found %d",
336 GIT_REPO_VERSION, repository_format_version);
337 warning("Expected git repo version <= %d, found %d",
338 GIT_REPO_VERSION, repository_format_version);
339 warning("Please upgrade Git");
340 *nongit_ok = -1;
341 return -1;
342 }
343 return 0;
344}
345
b44ebb19
LH
346/*
347 * Try to read the location of the git directory from the .git file,
348 * return path to git directory if found.
349 */
13d6ec91 350const char *read_gitfile(const char *path)
b44ebb19
LH
351{
352 char *buf;
40c813e0
BK
353 char *dir;
354 const char *slash;
b44ebb19
LH
355 struct stat st;
356 int fd;
b1905aea 357 ssize_t len;
b44ebb19
LH
358
359 if (stat(path, &st))
360 return NULL;
361 if (!S_ISREG(st.st_mode))
362 return NULL;
363 fd = open(path, O_RDONLY);
364 if (fd < 0)
d824cbba 365 die_errno("Error opening '%s'", path);
b44ebb19
LH
366 buf = xmalloc(st.st_size + 1);
367 len = read_in_full(fd, buf, st.st_size);
368 close(fd);
369 if (len != st.st_size)
370 die("Error reading %s", path);
371 buf[len] = '\0';
372 if (prefixcmp(buf, "gitdir: "))
373 die("Invalid gitfile format: %s", path);
374 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
375 len--;
376 if (len < 9)
377 die("No path in gitfile: %s", path);
378 buf[len] = '\0';
40c813e0
BK
379 dir = buf + 8;
380
381 if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
382 size_t pathlen = slash+1 - path;
383 size_t dirlen = pathlen + len - 8;
384 dir = xmalloc(dirlen + 1);
385 strncpy(dir, path, pathlen);
386 strncpy(dir + pathlen, buf + 8, len - 8);
387 dir[dirlen] = '\0';
388 free(buf);
389 buf = dir;
390 }
391
392 if (!is_git_directory(dir))
393 die("Not a git repository: %s", dir);
e2a57aac 394 path = real_path(dir);
40c813e0 395
b44ebb19
LH
396 free(buf);
397 return path;
398}
399
e4e30347 400static const char *setup_explicit_git_dir(const char *gitdirenv,
b3f66fd3
NTND
401 char *cwd, int len,
402 int *nongit_ok)
e4e30347 403{
b3f66fd3
NTND
404 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
405 const char *worktree;
406 char *gitfile;
9b125da4 407 int offset;
e4e30347
JN
408
409 if (PATH_MAX - 40 < strlen(gitdirenv))
410 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
b3f66fd3 411
13d6ec91 412 gitfile = (char*)read_gitfile(gitdirenv);
b3f66fd3
NTND
413 if (gitfile) {
414 gitfile = xstrdup(gitfile);
415 gitdirenv = gitfile;
416 }
417
e4e30347
JN
418 if (!is_git_directory(gitdirenv)) {
419 if (nongit_ok) {
420 *nongit_ok = 1;
b3f66fd3 421 free(gitfile);
e4e30347
JN
422 return NULL;
423 }
424 die("Not a git repository: '%s'", gitdirenv);
425 }
b3f66fd3
NTND
426
427 if (check_repository_format_gently(gitdirenv, nongit_ok)) {
428 free(gitfile);
429 return NULL;
e4e30347 430 }
b3f66fd3
NTND
431
432 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
433 if (work_tree_env)
434 set_git_work_tree(work_tree_env);
435 else if (is_bare_repository_cfg > 0) {
436 if (git_work_tree_cfg) /* #22.2, #30 */
437 die("core.bare and core.worktree do not make sense");
438
439 /* #18, #26 */
440 set_git_dir(gitdirenv);
441 free(gitfile);
e4e30347 442 return NULL;
b3f66fd3
NTND
443 }
444 else if (git_work_tree_cfg) { /* #6, #14 */
445 if (is_absolute_path(git_work_tree_cfg))
446 set_git_work_tree(git_work_tree_cfg);
447 else {
448 char core_worktree[PATH_MAX];
449 if (chdir(gitdirenv))
450 die_errno("Could not chdir to '%s'", gitdirenv);
451 if (chdir(git_work_tree_cfg))
452 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
453 if (!getcwd(core_worktree, PATH_MAX))
454 die_errno("Could not get directory '%s'", git_work_tree_cfg);
455 if (chdir(cwd))
456 die_errno("Could not come back to cwd");
457 set_git_work_tree(core_worktree);
458 }
459 }
460 else /* #2, #10 */
461 set_git_work_tree(".");
462
463 /* set_git_work_tree() must have been called by now */
464 worktree = get_git_work_tree();
465
466 /* both get_git_work_tree() and cwd are already normalized */
467 if (!strcmp(cwd, worktree)) { /* cwd == worktree */
468 set_git_dir(gitdirenv);
469 free(gitfile);
e4e30347 470 return NULL;
b3f66fd3 471 }
e4e30347 472
9b125da4
NTND
473 offset = dir_inside_of(cwd, worktree);
474 if (offset >= 0) { /* cwd inside worktree? */
e2a57aac 475 set_git_dir(real_path(gitdirenv));
b3f66fd3
NTND
476 if (chdir(worktree))
477 die_errno("Could not chdir to '%s'", worktree);
478 cwd[len++] = '/';
479 cwd[len] = '\0';
480 free(gitfile);
9b125da4 481 return cwd + offset;
93a00542 482 }
b3f66fd3
NTND
483
484 /* cwd outside worktree */
485 set_git_dir(gitdirenv);
486 free(gitfile);
487 return NULL;
93a00542
JN
488}
489
9951d3b3
NTND
490static const char *setup_discovered_git_dir(const char *gitdir,
491 char *cwd, int offset, int len,
492 int *nongit_ok)
98937bef 493{
9951d3b3
NTND
494 if (check_repository_format_gently(gitdir, nongit_ok))
495 return NULL;
98937bef 496
4868b2ea
JN
497 /* --work-tree is set without --git-dir; use discovered one */
498 if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
499 if (offset != len && !is_absolute_path(gitdir))
e2a57aac 500 gitdir = xstrdup(real_path(gitdir));
4868b2ea
JN
501 if (chdir(cwd))
502 die_errno("Could not come back to cwd");
503 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
504 }
505
9951d3b3
NTND
506 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
507 if (is_bare_repository_cfg > 0) {
e2a57aac 508 set_git_dir(offset == len ? gitdir : real_path(gitdir));
9951d3b3
NTND
509 if (chdir(cwd))
510 die_errno("Could not come back to cwd");
98937bef 511 return NULL;
9951d3b3 512 }
98937bef 513
9951d3b3
NTND
514 /* #0, #1, #5, #8, #9, #12, #13 */
515 set_git_work_tree(".");
516 if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
517 set_git_dir(gitdir);
98937bef 518 inside_git_dir = 0;
9951d3b3 519 inside_work_tree = 1;
98937bef
NTND
520 if (offset == len)
521 return NULL;
522
523 /* Make "offset" point to past the '/', and add a '/' at the end */
524 offset++;
525 cwd[len++] = '/';
526 cwd[len] = 0;
527 return cwd + offset;
528}
529
1cd8031b
NTND
530/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
531static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
68698da5
JN
532{
533 int root_len;
534
1cd8031b
NTND
535 if (check_repository_format_gently(".", nongit_ok))
536 return NULL;
537
4868b2ea
JN
538 /* --work-tree is set without --git-dir; use discovered one */
539 if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
540 const char *gitdir;
541
542 gitdir = offset == len ? "." : xmemdupz(cwd, offset);
543 if (chdir(cwd))
544 die_errno("Could not come back to cwd");
545 return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
546 }
547
68698da5 548 inside_git_dir = 1;
1cd8031b 549 inside_work_tree = 0;
68698da5 550 if (offset != len) {
8fc0ae80
NTND
551 if (chdir(cwd))
552 die_errno("Cannot come back to cwd");
68698da5
JN
553 root_len = offset_1st_component(cwd);
554 cwd[offset > root_len ? offset : root_len] = '\0';
555 set_git_dir(cwd);
337e51ce 556 }
1cd8031b 557 else
68698da5 558 set_git_dir(".");
68698da5
JN
559 return NULL;
560}
561
f161edeb
JN
562static const char *setup_nongit(const char *cwd, int *nongit_ok)
563{
564 if (!nongit_ok)
565 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
566 if (chdir(cwd))
567 die_errno("Cannot come back to cwd");
568 *nongit_ok = 1;
569 return NULL;
570}
571
2565b43b 572static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len)
60c98d1e
JN
573{
574 struct stat buf;
2565b43b
CB
575 if (stat(path, &buf)) {
576 die_errno("failed to stat '%*s%s%s'",
577 prefix_len,
60c98d1e
JN
578 prefix ? prefix : "",
579 prefix ? "/" : "", path);
2565b43b 580 }
60c98d1e
JN
581 return buf.st_dev;
582}
583
e90fdc39
JS
584/*
585 * We cannot decide in this function whether we are in the work tree or
586 * not, since the config can only be read _after_ this function was called.
587 */
a60645f9 588static const char *setup_git_directory_gently_1(int *nongit_ok)
d288a700 589{
0454dd93 590 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
d288a700 591 static char cwd[PATH_MAX+1];
9951d3b3
NTND
592 const char *gitdirenv, *ret;
593 char *gitfile;
2565b43b 594 int len, offset, offset_parent, ceil_offset;
c7d1d1b1
RH
595 dev_t current_device = 0;
596 int one_filesystem = 1;
d288a700 597
af05d679
SG
598 /*
599 * Let's assume that we are in a git repository.
600 * If it turns out later that we are somewhere else, the value will be
601 * updated accordingly.
602 */
603 if (nongit_ok)
604 *nongit_ok = 0;
605
b3f66fd3
NTND
606 if (!getcwd(cwd, sizeof(cwd)-1))
607 die_errno("Unable to read current working directory");
608 offset = len = strlen(cwd);
609
e90fdc39
JS
610 /*
611 * If GIT_DIR is set explicitly, we're not going
612 * to do any discovery, but we still do repository
613 * validation.
614 */
ad1a382f 615 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
e4e30347 616 if (gitdirenv)
b3f66fd3 617 return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
d288a700 618
0454dd93 619 ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
17d778e7
JH
620 if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
621 ceil_offset = 1;
d288a700 622
892c41b9 623 /*
e90fdc39 624 * Test in the following order (relative to the cwd):
b44ebb19 625 * - .git (file containing "gitdir: <path>")
e90fdc39
JS
626 * - .git/
627 * - ./ (bare)
b44ebb19 628 * - ../.git
e90fdc39
JS
629 * - ../.git/
630 * - ../ (bare)
631 * - ../../.git/
632 * etc.
892c41b9 633 */
cf87463e 634 one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
60c98d1e 635 if (one_filesystem)
2565b43b 636 current_device = get_device_or_die(".", NULL, 0);
e90fdc39 637 for (;;) {
13d6ec91 638 gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
9951d3b3
NTND
639 if (gitfile)
640 gitdirenv = gitfile = xstrdup(gitfile);
641 else {
642 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
643 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
644 }
645
646 if (gitdirenv) {
647 ret = setup_discovered_git_dir(gitdirenv,
648 cwd, offset, len,
649 nongit_ok);
650 free(gitfile);
651 return ret;
652 }
653 free(gitfile);
654
68698da5 655 if (is_git_directory("."))
1cd8031b
NTND
656 return setup_bare_git_dir(cwd, offset, len, nongit_ok);
657
2565b43b
CB
658 offset_parent = offset;
659 while (--offset_parent > ceil_offset && cwd[offset_parent] != '/');
660 if (offset_parent <= ceil_offset)
f161edeb 661 return setup_nongit(cwd, nongit_ok);
8030e442 662 if (one_filesystem) {
2565b43b 663 dev_t parent_device = get_device_or_die("..", cwd, offset);
60c98d1e 664 if (parent_device != current_device) {
8030e442
LD
665 if (nongit_ok) {
666 if (chdir(cwd))
667 die_errno("Cannot come back to cwd");
668 *nongit_ok = 1;
669 return NULL;
670 }
671 cwd[offset] = '\0';
2565b43b 672 die("Not a git repository (or any parent up to mount point %s)\n"
cf87463e 673 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
8030e442
LD
674 }
675 }
502ffe34
LD
676 if (chdir("..")) {
677 cwd[offset] = '\0';
d824cbba 678 die_errno("Cannot change to '%s/..'", cwd);
502ffe34 679 }
2565b43b 680 offset = offset_parent;
892c41b9 681 }
d288a700 682}
5e7bfe25 683
a60645f9
NTND
684const char *setup_git_directory_gently(int *nongit_ok)
685{
686 const char *prefix;
687
688 prefix = setup_git_directory_gently_1(nongit_ok);
1f5d271f
DA
689 if (prefix)
690 setenv("GIT_PREFIX", prefix, 1);
691 else
692 setenv("GIT_PREFIX", "", 1);
693
f07d6a1a 694 if (startup_info) {
a60645f9 695 startup_info->have_repository = !nongit_ok || !*nongit_ok;
f07d6a1a
NTND
696 startup_info->prefix = prefix;
697 }
a60645f9
NTND
698 return prefix;
699}
700
94df2506
JH
701int git_config_perm(const char *var, const char *value)
702{
06cbe855
HO
703 int i;
704 char *endptr;
705
706 if (value == NULL)
707 return PERM_GROUP;
708
709 if (!strcmp(value, "umask"))
710 return PERM_UMASK;
711 if (!strcmp(value, "group"))
712 return PERM_GROUP;
713 if (!strcmp(value, "all") ||
714 !strcmp(value, "world") ||
715 !strcmp(value, "everybody"))
716 return PERM_EVERYBODY;
717
718 /* Parse octal numbers */
719 i = strtol(value, &endptr, 8);
720
721 /* If not an octal number, maybe true/false? */
722 if (*endptr != 0)
723 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
724
725 /*
726 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
5a688fe4 727 * a chmod value to restrict to.
06cbe855
HO
728 */
729 switch (i) {
730 case PERM_UMASK: /* 0 */
731 return PERM_UMASK;
732 case OLD_PERM_GROUP: /* 1 */
733 return PERM_GROUP;
734 case OLD_PERM_EVERYBODY: /* 2 */
735 return PERM_EVERYBODY;
94df2506 736 }
06cbe855
HO
737
738 /* A filemode value was given: 0xxx */
739
740 if ((i & 0600) != 0600)
741 die("Problem with core.sharedRepository filemode value "
742 "(0%.3o).\nThe owner of files must always have "
743 "read and write permissions.", i);
744
745 /*
746 * Mask filemode value. Others can not get write permission.
747 * x flags for directories are handled separately.
748 */
5a688fe4 749 return -(i & 0666);
94df2506
JH
750}
751
ef90d6d4 752int check_repository_format_version(const char *var, const char *value, void *cb)
ab9cb76f 753{
299726d5
JS
754 if (strcmp(var, "core.repositoryformatversion") == 0)
755 repository_format_version = git_config_int(var, value);
457f06d6 756 else if (strcmp(var, "core.sharedrepository") == 0)
94df2506 757 shared_repository = git_config_perm(var, value);
e90fdc39
JS
758 else if (strcmp(var, "core.bare") == 0) {
759 is_bare_repository_cfg = git_config_bool(var, value);
760 if (is_bare_repository_cfg == 1)
761 inside_work_tree = -1;
762 } else if (strcmp(var, "core.worktree") == 0) {
180483c5
JH
763 if (!value)
764 return config_error_nonbool(var);
8e0f7003 765 free(git_work_tree_cfg);
e90fdc39
JS
766 git_work_tree_cfg = xstrdup(value);
767 inside_work_tree = -1;
768 }
299726d5 769 return 0;
ab9cb76f
JH
770}
771
772int check_repository_format(void)
773{
337e51ce 774 return check_repository_format_gently(get_git_dir(), NULL);
ab9cb76f
JH
775}
776
e1e5ec86
CB
777/*
778 * Returns the "prefix", a path to the current working directory
779 * relative to the work tree root, or NULL, if the current working
780 * directory is not a strict subdirectory of the work tree root. The
781 * prefix always ends with a '/' character.
782 */
5e7bfe25
JH
783const char *setup_git_directory(void)
784{
b3f66fd3 785 return setup_git_directory_gently(NULL);
5e7bfe25 786}
abc06822
FG
787
788const char *resolve_gitdir(const char *suspect)
789{
790 if (is_git_directory(suspect))
791 return suspect;
efc5fb6a 792 return read_gitfile(suspect);
abc06822 793}