fix overslow :/no-such-string-ever-existed diagnostics
[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
a6080a0a 43/*
4ca06608
JH
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 */
48const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
49{
50 static char path[PATH_MAX];
71064e3f 51#ifndef WIN32
9e5f5d4c 52 if (!pfx_len || is_absolute_path(arg))
4ca06608
JH
53 return arg;
54 memcpy(path, pfx, pfx_len);
55 strcpy(path + pfx_len, arg);
25fe217b
JS
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;
9e5f5d4c 61 else if (pfx_len)
25fe217b
JS
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
4ca06608
JH
68 return path;
69}
70
c6e8c800
JH
71int check_filename(const char *prefix, const char *arg)
72{
73 const char *name;
74 struct stat st;
75
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}
83
009fee47
MM
84static 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 ... */
2e83b66c 89 get_sha1_with_mode_1(arg, sha1, &mode, 1, prefix);
009fee47
MM
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);
93
94}
95
e23d0b4a
LT
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 */
103void verify_filename(const char *prefix, const char *arg)
104{
e23d0b4a
LT
105 if (*arg == '-')
106 die("bad flag '%s' used after filename", arg);
c6e8c800 107 if (check_filename(prefix, arg))
e23d0b4a 108 return;
009fee47 109 die_verify_filename(prefix, arg);
e23d0b4a
LT
110}
111
ea92f41f
JH
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 */
117void verify_non_filename(const char *prefix, const char *arg)
118{
7ae3df8c 119 if (!is_inside_work_tree() || is_inside_git_dir())
68025633 120 return;
ea92f41f
JH
121 if (*arg == '-')
122 return; /* flag */
c6e8c800
JH
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);
ea92f41f
JH
127}
128
8a42c985
JH
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" },
6d942927 139 * { PATHSPEC_ICASE, '\0', "icase" },
8a42c985
JH
140 * { PATHSPEC_RECURSIVE, '*', "recursive" },
141 * { PATHSPEC_REGEXP, '\0', "regexp" },
142 *
143 */
144#define PATHSPEC_FROMTOP (1<<0)
145
146struct pathspec_magic {
147 unsigned bit;
148 char mnemonic; /* this cannot be ':'! */
149 const char *name;
150} pathspec_magic[] = {
151 { PATHSPEC_FROMTOP, '/', "top" },
152};
153
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 */
167const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)
168{
169 unsigned magic = 0;
170 const char *copyfrom = elt;
6d942927 171 int i;
8a42c985
JH
172
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;
2f6c9760
JH
206
207 if (!is_pathspec_magic(ch))
208 break;
8a42c985
JH
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)
2f6c9760
JH
215 die("Unimplemented pathspec magic '%c' in '%s'",
216 ch, elt);
8a42c985
JH
217 }
218 if (*copyfrom == ':')
219 copyfrom++;
220 }
221
222 if (magic & PATHSPEC_FROMTOP)
6d942927 223 return xstrdup(copyfrom);
8a42c985 224 else
6d942927 225 return prefix_path(prefix, prefixlen, copyfrom);
8a42c985
JH
226}
227
6b5ee137 228const char **get_pathspec(const char *prefix, const char **pathspec)
d288a700 229{
6b5ee137 230 const char *entry = *pathspec;
d089ebaa 231 const char **src, **dst;
d288a700
LT
232 int prefixlen;
233
f332726e
LT
234 if (!prefix && !entry)
235 return NULL;
d288a700
LT
236
237 if (!entry) {
238 static const char *spec[2];
239 spec[0] = prefix;
240 spec[1] = NULL;
241 return spec;
242 }
243
244 /* Otherwise we have to re-write the entries.. */
d089ebaa
JH
245 src = pathspec;
246 dst = pathspec;
f332726e 247 prefixlen = prefix ? strlen(prefix) : 0;
d089ebaa 248 while (*src) {
8a42c985 249 *(dst++) = prefix_pathspec(prefix, prefixlen, *src);
d089ebaa
JH
250 src++;
251 }
252 *dst = NULL;
253 if (!*pathspec)
254 return NULL;
255 return pathspec;
d288a700
LT
256}
257
5f5608bc 258/*
ad1a382f 259 * Test if it looks like we're at a git directory.
5e7bfe25 260 * We want to see:
5f5608bc 261 *
790296fd 262 * - either an objects/ directory _or_ the proper
5f5608bc 263 * GIT_OBJECT_DIRECTORY environment variable
ad1a382f 264 * - a refs/ directory
8098a178 265 * - either a HEAD symlink or a HEAD file that is formatted as
c847f537
JH
266 * a proper "ref:", or a regular file HEAD that has a properly
267 * formatted sha1 object name.
5f5608bc 268 */
ad1a382f 269static int is_git_directory(const char *suspect)
5f5608bc 270{
ad1a382f
SP
271 char path[PATH_MAX];
272 size_t len = strlen(suspect);
273
3c9d0414
GB
274 if (PATH_MAX <= len + strlen("/objects"))
275 die("Too long path: %.*s", 60, suspect);
ad1a382f
SP
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 }
286
287 strcpy(path + len, "/refs");
288 if (access(path, X_OK))
8098a178 289 return 0;
ad1a382f
SP
290
291 strcpy(path + len, "/HEAD");
c847f537 292 if (validate_headref(path))
ad1a382f
SP
293 return 0;
294
8098a178 295 return 1;
5f5608bc
LT
296}
297
68025633
JS
298int is_inside_git_dir(void)
299{
e90fdc39
JS
300 if (inside_git_dir < 0)
301 inside_git_dir = is_inside_dir(get_git_dir());
302 return inside_git_dir;
892c41b9
ML
303}
304
892c41b9
ML
305int is_inside_work_tree(void)
306{
e90fdc39
JS
307 if (inside_work_tree < 0)
308 inside_work_tree = is_inside_dir(get_git_work_tree());
309 return inside_work_tree;
892c41b9
ML
310}
311
f3fa1838
JH
312void setup_work_tree(void)
313{
354e6534
JS
314 const char *work_tree, *git_dir;
315 static int initialized = 0;
316
317 if (initialized)
318 return;
319 work_tree = get_git_work_tree();
320 git_dir = get_git_dir();
59f0f2f3 321 if (!is_absolute_path(git_dir))
e2a57aac 322 git_dir = real_path(get_git_dir());
59f0f2f3
MH
323 if (!work_tree || chdir(work_tree))
324 die("This operation must be run in a work tree");
0ed74813
NTND
325
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);
332
e2a57aac 333 set_git_dir(relative_path(git_dir, work_tree));
354e6534 334 initialized = 1;
59f0f2f3
MH
335}
336
337e51ce 337static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
9459aa77 338{
337e51ce
NTND
339 char repo_config[PATH_MAX+1];
340
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);
9459aa77
NTND
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;
363}
364
b44ebb19
LH
365/*
366 * Try to read the location of the git directory from the .git file,
367 * return path to git directory if found.
368 */
369const char *read_gitfile_gently(const char *path)
370{
371 char *buf;
40c813e0
BK
372 char *dir;
373 const char *slash;
b44ebb19
LH
374 struct stat st;
375 int fd;
376 size_t len;
377
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)
d824cbba 384 die_errno("Error opening '%s'", path);
b44ebb19
LH
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';
40c813e0
BK
398 dir = buf + 8;
399
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 }
410
411 if (!is_git_directory(dir))
412 die("Not a git repository: %s", dir);
e2a57aac 413 path = real_path(dir);
40c813e0 414
b44ebb19
LH
415 free(buf);
416 return path;
417}
418
e4e30347 419static const char *setup_explicit_git_dir(const char *gitdirenv,
b3f66fd3
NTND
420 char *cwd, int len,
421 int *nongit_ok)
e4e30347 422{
b3f66fd3
NTND
423 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
424 const char *worktree;
425 char *gitfile;
e4e30347
JN
426
427 if (PATH_MAX - 40 < strlen(gitdirenv))
428 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
b3f66fd3
NTND
429
430 gitfile = (char*)read_gitfile_gently(gitdirenv);
431 if (gitfile) {
432 gitfile = xstrdup(gitfile);
433 gitdirenv = gitfile;
434 }
435
e4e30347
JN
436 if (!is_git_directory(gitdirenv)) {
437 if (nongit_ok) {
438 *nongit_ok = 1;
b3f66fd3 439 free(gitfile);
e4e30347
JN
440 return NULL;
441 }
442 die("Not a git repository: '%s'", gitdirenv);
443 }
b3f66fd3
NTND
444
445 if (check_repository_format_gently(gitdirenv, nongit_ok)) {
446 free(gitfile);
447 return NULL;
e4e30347 448 }
b3f66fd3
NTND
449
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");
456
457 /* #18, #26 */
458 set_git_dir(gitdirenv);
459 free(gitfile);
e4e30347 460 return NULL;
b3f66fd3
NTND
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(".");
480
481 /* set_git_work_tree() must have been called by now */
482 worktree = get_git_work_tree();
483
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);
e4e30347 488 return NULL;
b3f66fd3 489 }
e4e30347 490
b3f66fd3
NTND
491 if (!prefixcmp(cwd, worktree) &&
492 cwd[strlen(worktree)] == '/') { /* cwd inside worktree */
e2a57aac 493 set_git_dir(real_path(gitdirenv));
b3f66fd3
NTND
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;
93a00542 500 }
b3f66fd3
NTND
501
502 /* cwd outside worktree */
503 set_git_dir(gitdirenv);
504 free(gitfile);
505 return NULL;
93a00542
JN
506}
507
9951d3b3
NTND
508static const char *setup_discovered_git_dir(const char *gitdir,
509 char *cwd, int offset, int len,
510 int *nongit_ok)
98937bef 511{
9951d3b3
NTND
512 if (check_repository_format_gently(gitdir, nongit_ok))
513 return NULL;
98937bef 514
4868b2ea
JN
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))
e2a57aac 518 gitdir = xstrdup(real_path(gitdir));
4868b2ea
JN
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 }
523
9951d3b3
NTND
524 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
525 if (is_bare_repository_cfg > 0) {
e2a57aac 526 set_git_dir(offset == len ? gitdir : real_path(gitdir));
9951d3b3
NTND
527 if (chdir(cwd))
528 die_errno("Could not come back to cwd");
98937bef 529 return NULL;
9951d3b3 530 }
98937bef 531
9951d3b3
NTND
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);
98937bef 536 inside_git_dir = 0;
9951d3b3 537 inside_work_tree = 1;
98937bef
NTND
538 if (offset == len)
539 return NULL;
540
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;
546}
547
1cd8031b
NTND
548/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
549static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
68698da5
JN
550{
551 int root_len;
552
1cd8031b
NTND
553 if (check_repository_format_gently(".", nongit_ok))
554 return NULL;
555
4868b2ea
JN
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;
559
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 }
565
68698da5 566 inside_git_dir = 1;
1cd8031b 567 inside_work_tree = 0;
68698da5 568 if (offset != len) {
8fc0ae80
NTND
569 if (chdir(cwd))
570 die_errno("Cannot come back to cwd");
68698da5
JN
571 root_len = offset_1st_component(cwd);
572 cwd[offset > root_len ? offset : root_len] = '\0';
573 set_git_dir(cwd);
337e51ce 574 }
1cd8031b 575 else
68698da5 576 set_git_dir(".");
68698da5
JN
577 return NULL;
578}
579
f161edeb
JN
580static const char *setup_nongit(const char *cwd, int *nongit_ok)
581{
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;
588}
589
60c98d1e
JN
590static dev_t get_device_or_die(const char *path, const char *prefix)
591{
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;
598}
599
e90fdc39
JS
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 */
a60645f9 604static const char *setup_git_directory_gently_1(int *nongit_ok)
d288a700 605{
0454dd93 606 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
d288a700 607 static char cwd[PATH_MAX+1];
9951d3b3
NTND
608 const char *gitdirenv, *ret;
609 char *gitfile;
98937bef 610 int len, offset, ceil_offset;
c7d1d1b1
RH
611 dev_t current_device = 0;
612 int one_filesystem = 1;
d288a700 613
af05d679
SG
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;
621
b3f66fd3
NTND
622 if (!getcwd(cwd, sizeof(cwd)-1))
623 die_errno("Unable to read current working directory");
624 offset = len = strlen(cwd);
625
e90fdc39
JS
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 */
ad1a382f 631 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
e4e30347 632 if (gitdirenv)
b3f66fd3 633 return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
d288a700 634
0454dd93 635 ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
17d778e7
JH
636 if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
637 ceil_offset = 1;
d288a700 638
892c41b9 639 /*
e90fdc39 640 * Test in the following order (relative to the cwd):
b44ebb19 641 * - .git (file containing "gitdir: <path>")
e90fdc39
JS
642 * - .git/
643 * - ./ (bare)
b44ebb19 644 * - ../.git
e90fdc39
JS
645 * - ../.git/
646 * - ../ (bare)
647 * - ../../.git/
648 * etc.
892c41b9 649 */
cf87463e 650 one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
60c98d1e
JN
651 if (one_filesystem)
652 current_device = get_device_or_die(".", NULL);
e90fdc39 653 for (;;) {
9951d3b3
NTND
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 }
661
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);
670
68698da5 671 if (is_git_directory("."))
1cd8031b
NTND
672 return setup_bare_git_dir(cwd, offset, len, nongit_ok);
673
0454dd93 674 while (--offset > ceil_offset && cwd[offset] != '/');
f161edeb
JN
675 if (offset <= ceil_offset)
676 return setup_nongit(cwd, nongit_ok);
8030e442 677 if (one_filesystem) {
60c98d1e
JN
678 dev_t parent_device = get_device_or_die("..", cwd);
679 if (parent_device != current_device) {
8030e442
LD
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"
cf87463e 688 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd);
8030e442
LD
689 }
690 }
502ffe34
LD
691 if (chdir("..")) {
692 cwd[offset] = '\0';
d824cbba 693 die_errno("Cannot change to '%s/..'", cwd);
502ffe34 694 }
892c41b9 695 }
d288a700 696}
5e7bfe25 697
a60645f9
NTND
698const char *setup_git_directory_gently(int *nongit_ok)
699{
700 const char *prefix;
701
702 prefix = setup_git_directory_gently_1(nongit_ok);
f07d6a1a 703 if (startup_info) {
a60645f9 704 startup_info->have_repository = !nongit_ok || !*nongit_ok;
f07d6a1a
NTND
705 startup_info->prefix = prefix;
706 }
a60645f9
NTND
707 return prefix;
708}
709
94df2506
JH
710int git_config_perm(const char *var, const char *value)
711{
06cbe855
HO
712 int i;
713 char *endptr;
714
715 if (value == NULL)
716 return PERM_GROUP;
717
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;
726
727 /* Parse octal numbers */
728 i = strtol(value, &endptr, 8);
729
730 /* If not an octal number, maybe true/false? */
731 if (*endptr != 0)
732 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
733
734 /*
735 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
5a688fe4 736 * a chmod value to restrict to.
06cbe855
HO
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;
94df2506 745 }
06cbe855
HO
746
747 /* A filemode value was given: 0xxx */
748
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);
753
754 /*
755 * Mask filemode value. Others can not get write permission.
756 * x flags for directories are handled separately.
757 */
5a688fe4 758 return -(i & 0666);
94df2506
JH
759}
760
ef90d6d4 761int check_repository_format_version(const char *var, const char *value, void *cb)
ab9cb76f 762{
299726d5
JS
763 if (strcmp(var, "core.repositoryformatversion") == 0)
764 repository_format_version = git_config_int(var, value);
457f06d6 765 else if (strcmp(var, "core.sharedrepository") == 0)
94df2506 766 shared_repository = git_config_perm(var, value);
e90fdc39
JS
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) {
180483c5
JH
772 if (!value)
773 return config_error_nonbool(var);
8e0f7003 774 free(git_work_tree_cfg);
e90fdc39
JS
775 git_work_tree_cfg = xstrdup(value);
776 inside_work_tree = -1;
777 }
299726d5 778 return 0;
ab9cb76f
JH
779}
780
781int check_repository_format(void)
782{
337e51ce 783 return check_repository_format_gently(get_git_dir(), NULL);
ab9cb76f
JH
784}
785
e1e5ec86
CB
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 */
5e7bfe25
JH
792const char *setup_git_directory(void)
793{
b3f66fd3 794 return setup_git_directory_gently(NULL);
5e7bfe25 795}