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