]>
Commit | Line | Data |
---|---|---|
d288a700 | 1 | #include "cache.h" |
c14c234f | 2 | #include "repository.h" |
b2141fc1 | 3 | #include "config.h" |
e90fdc39 | 4 | #include "dir.h" |
31171d9e | 5 | #include "string-list.h" |
8500e0de | 6 | #include "chdir-notify.h" |
60b7a92d | 7 | #include "promisor-remote.h" |
8959555c | 8 | #include "quote.h" |
e90fdc39 JS |
9 | |
10 | static int inside_git_dir = -1; | |
11 | static int inside_work_tree = -1; | |
fada7674 | 12 | static int work_tree_config_is_bogus; |
8d1a7448 GC |
13 | enum allowed_bare_repo { |
14 | ALLOWED_BARE_REPO_EXPLICIT = 0, | |
15 | ALLOWED_BARE_REPO_ALL, | |
16 | }; | |
d288a700 | 17 | |
46c3cd44 JK |
18 | static struct startup_info the_startup_info; |
19 | struct startup_info *startup_info = &the_startup_info; | |
e6f8861b | 20 | const char *tmp_original_cwd; |
46c3cd44 | 21 | |
ddc2a628 MEW |
22 | /* |
23 | * The input parameter must contain an absolute path, and it must already be | |
24 | * normalized. | |
25 | * | |
26 | * Find the part of an absolute path that lies inside the work tree by | |
27 | * dereferencing symlinks outside the work tree, for example: | |
28 | * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file | |
29 | * /dir/file (work tree is /) -> dir/file | |
30 | * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2 | |
31 | * /dir/repolink/file (repolink points to /dir/repo) -> file | |
32 | * /dir/repo (exactly equal to work tree) -> (empty string) | |
33 | */ | |
34 | static int abspath_part_inside_repo(char *path) | |
35 | { | |
36 | size_t len; | |
37 | size_t wtlen; | |
38 | char *path0; | |
39 | int off; | |
40 | const char *work_tree = get_git_work_tree(); | |
3d7747e3 | 41 | struct strbuf realpath = STRBUF_INIT; |
ddc2a628 MEW |
42 | |
43 | if (!work_tree) | |
44 | return -1; | |
45 | wtlen = strlen(work_tree); | |
46 | len = strlen(path); | |
6127ff63 | 47 | off = offset_1st_component(path); |
ddc2a628 MEW |
48 | |
49 | /* check if work tree is already the prefix */ | |
d8727b36 | 50 | if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) { |
ddc2a628 MEW |
51 | if (path[wtlen] == '/') { |
52 | memmove(path, path + wtlen + 1, len - wtlen); | |
53 | return 0; | |
54 | } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') { | |
55 | /* work tree is the root, or the whole path */ | |
56 | memmove(path, path + wtlen, len - wtlen + 1); | |
57 | return 0; | |
58 | } | |
59 | /* work tree might match beginning of a symlink to work tree */ | |
60 | off = wtlen; | |
61 | } | |
62 | path0 = path; | |
6127ff63 | 63 | path += off; |
ddc2a628 MEW |
64 | |
65 | /* check each '/'-terminated level */ | |
66 | while (*path) { | |
67 | path++; | |
68 | if (*path == '/') { | |
69 | *path = '\0'; | |
3d7747e3 AM |
70 | strbuf_realpath(&realpath, path0, 1); |
71 | if (fspathcmp(realpath.buf, work_tree) == 0) { | |
ddc2a628 | 72 | memmove(path0, path + 1, len - (path - path0)); |
3d7747e3 | 73 | strbuf_release(&realpath); |
ddc2a628 MEW |
74 | return 0; |
75 | } | |
76 | *path = '/'; | |
77 | } | |
78 | } | |
79 | ||
80 | /* check whole path */ | |
3d7747e3 AM |
81 | strbuf_realpath(&realpath, path0, 1); |
82 | if (fspathcmp(realpath.buf, work_tree) == 0) { | |
ddc2a628 | 83 | *path0 = '\0'; |
3d7747e3 | 84 | strbuf_release(&realpath); |
ddc2a628 MEW |
85 | return 0; |
86 | } | |
87 | ||
3d7747e3 | 88 | strbuf_release(&realpath); |
ddc2a628 MEW |
89 | return -1; |
90 | } | |
91 | ||
645a29c4 NTND |
92 | /* |
93 | * Normalize "path", prepending the "prefix" for relative paths. If | |
94 | * remaining_prefix is not NULL, return the actual prefix still | |
95 | * remains in the path. For example, prefix = sub1/sub2/ and path is | |
96 | * | |
97 | * foo -> sub1/sub2/foo (full prefix) | |
98 | * ../foo -> sub1/foo (remaining prefix is sub1/) | |
99 | * ../../bar -> bar (no remaining prefix) | |
100 | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) | |
101 | * `pwd`/../bar -> sub1/bar (no remaining prefix) | |
102 | */ | |
103 | char *prefix_path_gently(const char *prefix, int len, | |
104 | int *remaining_prefix, const char *path) | |
d089ebaa JH |
105 | { |
106 | const char *orig = path; | |
18e051a3 CMAB |
107 | char *sanitized; |
108 | if (is_absolute_path(orig)) { | |
3733e694 | 109 | sanitized = xmallocz(strlen(path)); |
645a29c4 NTND |
110 | if (remaining_prefix) |
111 | *remaining_prefix = 0; | |
655ee9ea MEW |
112 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
113 | free(sanitized); | |
114 | return NULL; | |
115 | } | |
116 | if (abspath_part_inside_repo(sanitized)) { | |
117 | free(sanitized); | |
118 | return NULL; | |
119 | } | |
18e051a3 | 120 | } else { |
24041d6b | 121 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
645a29c4 NTND |
122 | if (remaining_prefix) |
123 | *remaining_prefix = len; | |
655ee9ea | 124 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
546e0fd9 JK |
125 | free(sanitized); |
126 | return NULL; | |
d089ebaa | 127 | } |
d089ebaa JH |
128 | } |
129 | return sanitized; | |
f332726e LT |
130 | } |
131 | ||
546e0fd9 JK |
132 | char *prefix_path(const char *prefix, int len, const char *path) |
133 | { | |
645a29c4 | 134 | char *r = prefix_path_gently(prefix, len, NULL, path); |
5c203986 ES |
135 | if (!r) { |
136 | const char *hint_path = get_git_work_tree(); | |
137 | if (!hint_path) | |
138 | hint_path = get_git_dir(); | |
e0020b2f | 139 | die(_("'%s' is outside repository at '%s'"), path, |
5c203986 ES |
140 | absolute_path(hint_path)); |
141 | } | |
546e0fd9 JK |
142 | return r; |
143 | } | |
144 | ||
145 | int path_inside_repo(const char *prefix, const char *path) | |
146 | { | |
147 | int len = prefix ? strlen(prefix) : 0; | |
645a29c4 | 148 | char *r = prefix_path_gently(prefix, len, NULL, path); |
546e0fd9 JK |
149 | if (r) { |
150 | free(r); | |
151 | return 1; | |
152 | } | |
153 | return 0; | |
154 | } | |
155 | ||
c6e8c800 JH |
156 | int check_filename(const char *prefix, const char *arg) |
157 | { | |
e4da43b1 | 158 | char *to_free = NULL; |
c6e8c800 JH |
159 | struct stat st; |
160 | ||
d51c6ee0 JK |
161 | if (skip_prefix(arg, ":/", &arg)) { |
162 | if (!*arg) /* ":/" is root dir, always exists */ | |
4db86e8b | 163 | return 1; |
a08cbcda | 164 | prefix = NULL; |
42471bce JK |
165 | } else if (skip_prefix(arg, ":!", &arg) || |
166 | skip_prefix(arg, ":^", &arg)) { | |
167 | if (!*arg) /* excluding everything is silly, but allowed */ | |
168 | return 1; | |
a08cbcda JK |
169 | } |
170 | ||
171 | if (prefix) | |
172 | arg = to_free = prefix_filename(prefix, arg); | |
173 | ||
174 | if (!lstat(arg, &st)) { | |
e4da43b1 | 175 | free(to_free); |
c6e8c800 | 176 | return 1; /* file exists */ |
e4da43b1 | 177 | } |
93dd544f | 178 | if (is_missing_file_error(errno)) { |
e4da43b1 | 179 | free(to_free); |
c6e8c800 | 180 | return 0; /* file does not exist */ |
e4da43b1 | 181 | } |
fc045fe7 | 182 | die_errno(_("failed to stat '%s'"), arg); |
c6e8c800 JH |
183 | } |
184 | ||
e270f42c NTND |
185 | static void NORETURN die_verify_filename(struct repository *r, |
186 | const char *prefix, | |
023e37c3 MM |
187 | const char *arg, |
188 | int diagnose_misspelt_rev) | |
009fee47 | 189 | { |
023e37c3 | 190 | if (!diagnose_misspelt_rev) |
ab33a76e VA |
191 | die(_("%s: no such path in the working tree.\n" |
192 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally."), | |
023e37c3 | 193 | arg); |
0e539dca JH |
194 | /* |
195 | * Saying "'(icase)foo' does not exist in the index" when the | |
196 | * user gave us ":(icase)foo" is just stupid. A magic pathspec | |
197 | * begins with a colon and is followed by a non-alnum; do not | |
8c135ea2 | 198 | * let maybe_die_on_misspelt_object_name() even trigger. |
0e539dca JH |
199 | */ |
200 | if (!(arg[0] == ':' && !isalnum(arg[1]))) | |
e270f42c | 201 | maybe_die_on_misspelt_object_name(r, arg, prefix); |
0e539dca | 202 | |
009fee47 | 203 | /* ... or fall back the most general message. */ |
ab33a76e VA |
204 | die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
205 | "Use '--' to separate paths from revisions, like this:\n" | |
206 | "'git <command> [<revision>...] -- [<file>...]'"), arg); | |
009fee47 MM |
207 | |
208 | } | |
209 | ||
c99eddd8 JK |
210 | /* |
211 | * Check for arguments that don't resolve as actual files, | |
212 | * but which look sufficiently like pathspecs that we'll consider | |
213 | * them such for the purposes of rev/pathspec DWIM parsing. | |
214 | */ | |
215 | static int looks_like_pathspec(const char *arg) | |
216 | { | |
39e21c6e JK |
217 | const char *p; |
218 | int escaped = 0; | |
219 | ||
220 | /* | |
221 | * Wildcard characters imply the user is looking to match pathspecs | |
222 | * that aren't in the filesystem. Note that this doesn't include | |
223 | * backslash even though it's a glob special; by itself it doesn't | |
224 | * cause any increase in the match. Likewise ignore backslash-escaped | |
225 | * wildcard characters. | |
226 | */ | |
227 | for (p = arg; *p; p++) { | |
228 | if (escaped) { | |
229 | escaped = 0; | |
230 | } else if (is_glob_special(*p)) { | |
231 | if (*p == '\\') | |
232 | escaped = 1; | |
233 | else | |
234 | return 1; | |
235 | } | |
236 | } | |
c99eddd8 JK |
237 | |
238 | /* long-form pathspec magic */ | |
239 | if (starts_with(arg, ":(")) | |
240 | return 1; | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
e23d0b4a LT |
245 | /* |
246 | * Verify a filename that we got as an argument for a pathspec | |
247 | * entry. Note that a filename that begins with "-" never verifies | |
248 | * as true, because even if such a filename were to exist, we want | |
249 | * it to be preceded by the "--" marker (or we want the user to | |
250 | * use a format like "./-filename") | |
023e37c3 MM |
251 | * |
252 | * The "diagnose_misspelt_rev" is used to provide a user-friendly | |
253 | * diagnosis when dying upon finding that "name" is not a pathname. | |
254 | * If set to 1, the diagnosis will try to diagnose "name" as an | |
255 | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis | |
256 | * will only complain about an inexisting file. | |
257 | * | |
258 | * This function is typically called to check that a "file or rev" | |
259 | * argument is unambiguous. In this case, the caller will want | |
260 | * diagnose_misspelt_rev == 1 when verifying the first non-rev | |
261 | * argument (which could have been a revision), and | |
262 | * diagnose_misspelt_rev == 0 for the next ones (because we already | |
263 | * saw a filename, there's not ambiguity anymore). | |
e23d0b4a | 264 | */ |
023e37c3 MM |
265 | void verify_filename(const char *prefix, |
266 | const char *arg, | |
267 | int diagnose_misspelt_rev) | |
e23d0b4a | 268 | { |
e23d0b4a | 269 | if (*arg == '-') |
fc045fe7 | 270 | die(_("option '%s' must come before non-option arguments"), arg); |
2cb47ab6 | 271 | if (looks_like_pathspec(arg) || check_filename(prefix, arg)) |
e23d0b4a | 272 | return; |
e270f42c | 273 | die_verify_filename(the_repository, prefix, arg, diagnose_misspelt_rev); |
e23d0b4a LT |
274 | } |
275 | ||
ea92f41f JH |
276 | /* |
277 | * Opposite of the above: the command line did not have -- marker | |
278 | * and we parsed the arg as a refname. It should not be interpretable | |
279 | * as a filename. | |
280 | */ | |
281 | void verify_non_filename(const char *prefix, const char *arg) | |
282 | { | |
7ae3df8c | 283 | if (!is_inside_work_tree() || is_inside_git_dir()) |
68025633 | 284 | return; |
ea92f41f JH |
285 | if (*arg == '-') |
286 | return; /* flag */ | |
c6e8c800 JH |
287 | if (!check_filename(prefix, arg)) |
288 | return; | |
ab33a76e VA |
289 | die(_("ambiguous argument '%s': both revision and filename\n" |
290 | "Use '--' to separate paths from revisions, like this:\n" | |
291 | "'git <command> [<revision>...] -- [<file>...]'"), arg); | |
ea92f41f JH |
292 | } |
293 | ||
31e26ebc | 294 | int get_common_dir(struct strbuf *sb, const char *gitdir) |
11f9dd71 MK |
295 | { |
296 | const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT); | |
297 | if (git_env_common_dir) { | |
298 | strbuf_addstr(sb, git_env_common_dir); | |
299 | return 1; | |
300 | } else { | |
301 | return get_common_dir_noenv(sb, gitdir); | |
302 | } | |
303 | } | |
304 | ||
305 | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) | |
4dc4e145 NTND |
306 | { |
307 | struct strbuf data = STRBUF_INIT; | |
308 | struct strbuf path = STRBUF_INIT; | |
31e26ebc | 309 | int ret = 0; |
11f9dd71 | 310 | |
4dc4e145 NTND |
311 | strbuf_addf(&path, "%s/commondir", gitdir); |
312 | if (file_exists(path.buf)) { | |
313 | if (strbuf_read_file(&data, path.buf, 0) <= 0) | |
314 | die_errno(_("failed to read %s"), path.buf); | |
315 | while (data.len && (data.buf[data.len - 1] == '\n' || | |
316 | data.buf[data.len - 1] == '\r')) | |
317 | data.len--; | |
318 | data.buf[data.len] = '\0'; | |
319 | strbuf_reset(&path); | |
320 | if (!is_absolute_path(data.buf)) | |
321 | strbuf_addf(&path, "%s/", gitdir); | |
322 | strbuf_addbuf(&path, &data); | |
33ad9ddd | 323 | strbuf_add_real_path(sb, path.buf); |
31e26ebc | 324 | ret = 1; |
4ac9006f | 325 | } else { |
4dc4e145 | 326 | strbuf_addstr(sb, gitdir); |
4ac9006f BW |
327 | } |
328 | ||
4dc4e145 NTND |
329 | strbuf_release(&data); |
330 | strbuf_release(&path); | |
31e26ebc | 331 | return ret; |
4dc4e145 | 332 | } |
d288a700 | 333 | |
5f5608bc | 334 | /* |
ad1a382f | 335 | * Test if it looks like we're at a git directory. |
5e7bfe25 | 336 | * We want to see: |
5f5608bc | 337 | * |
790296fd | 338 | * - either an objects/ directory _or_ the proper |
5f5608bc | 339 | * GIT_OBJECT_DIRECTORY environment variable |
ad1a382f | 340 | * - a refs/ directory |
8098a178 | 341 | * - either a HEAD symlink or a HEAD file that is formatted as |
c847f537 JH |
342 | * a proper "ref:", or a regular file HEAD that has a properly |
343 | * formatted sha1 object name. | |
5f5608bc | 344 | */ |
b3256eb8 | 345 | int is_git_directory(const char *suspect) |
5f5608bc | 346 | { |
1d186b6f NTND |
347 | struct strbuf path = STRBUF_INIT; |
348 | int ret = 0; | |
349 | size_t len; | |
ad1a382f | 350 | |
4dc4e145 | 351 | /* Check worktree-related signatures */ |
fa4d8c78 JK |
352 | strbuf_addstr(&path, suspect); |
353 | strbuf_complete(&path, '/'); | |
354 | strbuf_addstr(&path, "HEAD"); | |
4dc4e145 NTND |
355 | if (validate_headref(path.buf)) |
356 | goto done; | |
357 | ||
358 | strbuf_reset(&path); | |
359 | get_common_dir(&path, suspect); | |
1d186b6f | 360 | len = path.len; |
4dc4e145 NTND |
361 | |
362 | /* Check non-worktree-related signatures */ | |
ad1a382f SP |
363 | if (getenv(DB_ENVIRONMENT)) { |
364 | if (access(getenv(DB_ENVIRONMENT), X_OK)) | |
1d186b6f | 365 | goto done; |
ad1a382f SP |
366 | } |
367 | else { | |
4dc4e145 | 368 | strbuf_setlen(&path, len); |
1d186b6f NTND |
369 | strbuf_addstr(&path, "/objects"); |
370 | if (access(path.buf, X_OK)) | |
371 | goto done; | |
ad1a382f SP |
372 | } |
373 | ||
1d186b6f NTND |
374 | strbuf_setlen(&path, len); |
375 | strbuf_addstr(&path, "/refs"); | |
376 | if (access(path.buf, X_OK)) | |
377 | goto done; | |
ad1a382f | 378 | |
1d186b6f NTND |
379 | ret = 1; |
380 | done: | |
381 | strbuf_release(&path); | |
382 | return ret; | |
5f5608bc LT |
383 | } |
384 | ||
ffd036b1 JK |
385 | int is_nonbare_repository_dir(struct strbuf *path) |
386 | { | |
387 | int ret = 0; | |
388 | int gitfile_error; | |
389 | size_t orig_path_len = path->len; | |
390 | assert(orig_path_len != 0); | |
391 | strbuf_complete(path, '/'); | |
392 | strbuf_addstr(path, ".git"); | |
393 | if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) | |
394 | ret = 1; | |
395 | if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || | |
396 | gitfile_error == READ_GITFILE_ERR_READ_FAILED) | |
397 | ret = 1; | |
398 | strbuf_setlen(path, orig_path_len); | |
399 | return ret; | |
400 | } | |
401 | ||
68025633 JS |
402 | int is_inside_git_dir(void) |
403 | { | |
e90fdc39 JS |
404 | if (inside_git_dir < 0) |
405 | inside_git_dir = is_inside_dir(get_git_dir()); | |
406 | return inside_git_dir; | |
892c41b9 ML |
407 | } |
408 | ||
892c41b9 ML |
409 | int is_inside_work_tree(void) |
410 | { | |
e90fdc39 JS |
411 | if (inside_work_tree < 0) |
412 | inside_work_tree = is_inside_dir(get_git_work_tree()); | |
413 | return inside_work_tree; | |
892c41b9 ML |
414 | } |
415 | ||
f3fa1838 JH |
416 | void setup_work_tree(void) |
417 | { | |
8500e0de | 418 | const char *work_tree; |
354e6534 JS |
419 | static int initialized = 0; |
420 | ||
421 | if (initialized) | |
422 | return; | |
fada7674 JK |
423 | |
424 | if (work_tree_config_is_bogus) | |
fc045fe7 | 425 | die(_("unable to set up work tree using invalid config")); |
fada7674 | 426 | |
354e6534 | 427 | work_tree = get_git_work_tree(); |
8500e0de | 428 | if (!work_tree || chdir_notify(work_tree)) |
fc045fe7 | 429 | die(_("this operation must be run in a work tree")); |
0ed74813 NTND |
430 | |
431 | /* | |
432 | * Make sure subsequent git processes find correct worktree | |
433 | * if $GIT_WORK_TREE is set relative | |
434 | */ | |
435 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) | |
436 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); | |
437 | ||
354e6534 | 438 | initialized = 1; |
59f0f2f3 MH |
439 | } |
440 | ||
e6f8861b EN |
441 | static void setup_original_cwd(void) |
442 | { | |
443 | struct strbuf tmp = STRBUF_INIT; | |
444 | const char *worktree = NULL; | |
445 | int offset = -1; | |
446 | ||
447 | if (!tmp_original_cwd) | |
448 | return; | |
449 | ||
450 | /* | |
451 | * startup_info->original_cwd points to the current working | |
452 | * directory we inherited from our parent process, which is a | |
453 | * directory we want to avoid removing. | |
454 | * | |
455 | * For convience, we would like to have the path relative to the | |
456 | * worktree instead of an absolute path. | |
457 | * | |
458 | * Yes, startup_info->original_cwd is usually the same as 'prefix', | |
459 | * but differs in two ways: | |
460 | * - prefix has a trailing '/' | |
461 | * - if the user passes '-C' to git, that modifies the prefix but | |
462 | * not startup_info->original_cwd. | |
463 | */ | |
464 | ||
465 | /* Normalize the directory */ | |
c37c6dc6 KL |
466 | if (!strbuf_realpath(&tmp, tmp_original_cwd, 0)) { |
467 | trace2_data_string("setup", the_repository, | |
468 | "realpath-path", tmp_original_cwd); | |
469 | trace2_data_string("setup", the_repository, | |
470 | "realpath-failure", strerror(errno)); | |
471 | free((char*)tmp_original_cwd); | |
472 | tmp_original_cwd = NULL; | |
473 | return; | |
474 | } | |
475 | ||
e6f8861b EN |
476 | free((char*)tmp_original_cwd); |
477 | tmp_original_cwd = NULL; | |
478 | startup_info->original_cwd = strbuf_detach(&tmp, NULL); | |
479 | ||
480 | /* | |
481 | * Get our worktree; we only protect the current working directory | |
482 | * if it's in the worktree. | |
483 | */ | |
484 | worktree = get_git_work_tree(); | |
485 | if (!worktree) | |
486 | goto no_prevention_needed; | |
487 | ||
488 | offset = dir_inside_of(startup_info->original_cwd, worktree); | |
489 | if (offset >= 0) { | |
490 | /* | |
491 | * If startup_info->original_cwd == worktree, that is already | |
492 | * protected and we don't need original_cwd as a secondary | |
493 | * protection measure. | |
494 | */ | |
495 | if (!*(startup_info->original_cwd + offset)) | |
496 | goto no_prevention_needed; | |
497 | ||
498 | /* | |
499 | * original_cwd was inside worktree; precompose it just as | |
500 | * we do prefix so that built up paths will match | |
501 | */ | |
502 | startup_info->original_cwd = \ | |
503 | precompose_string_if_needed(startup_info->original_cwd | |
504 | + offset); | |
505 | return; | |
506 | } | |
507 | ||
508 | no_prevention_needed: | |
509 | free((char*)startup_info->original_cwd); | |
510 | startup_info->original_cwd = NULL; | |
511 | } | |
512 | ||
58b284a2 NTND |
513 | static int read_worktree_config(const char *var, const char *value, void *vdata) |
514 | { | |
515 | struct repository_format *data = vdata; | |
516 | ||
517 | if (strcmp(var, "core.bare") == 0) { | |
518 | data->is_bare = git_config_bool(var, value); | |
519 | } else if (strcmp(var, "core.worktree") == 0) { | |
520 | if (!value) | |
521 | return config_error_nonbool(var); | |
13019979 | 522 | free(data->work_tree); |
58b284a2 NTND |
523 | data->work_tree = xstrdup(value); |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
ec91ffca JK |
528 | enum extension_result { |
529 | EXTENSION_ERROR = -1, /* compatible with error(), etc */ | |
530 | EXTENSION_UNKNOWN = 0, | |
531 | EXTENSION_OK = 1 | |
532 | }; | |
533 | ||
534 | /* | |
535 | * Do not add new extensions to this function. It handles extensions which are | |
536 | * respected even in v0-format repositories for historical compatibility. | |
537 | */ | |
538 | static enum extension_result handle_extension_v0(const char *var, | |
539 | const char *value, | |
540 | const char *ext, | |
541 | struct repository_format *data) | |
542 | { | |
543 | if (!strcmp(ext, "noop")) { | |
544 | return EXTENSION_OK; | |
545 | } else if (!strcmp(ext, "preciousobjects")) { | |
546 | data->precious_objects = git_config_bool(var, value); | |
547 | return EXTENSION_OK; | |
548 | } else if (!strcmp(ext, "partialclone")) { | |
ec91ffca JK |
549 | data->partial_clone = xstrdup(value); |
550 | return EXTENSION_OK; | |
551 | } else if (!strcmp(ext, "worktreeconfig")) { | |
552 | data->worktree_config = git_config_bool(var, value); | |
553 | return EXTENSION_OK; | |
554 | } | |
555 | ||
556 | return EXTENSION_UNKNOWN; | |
557 | } | |
558 | ||
559 | /* | |
560 | * Record any new extensions in this function. | |
561 | */ | |
562 | static enum extension_result handle_extension(const char *var, | |
563 | const char *value, | |
564 | const char *ext, | |
565 | struct repository_format *data) | |
566 | { | |
567 | if (!strcmp(ext, "noop-v1")) { | |
568 | return EXTENSION_OK; | |
e0ad9574 JH |
569 | } else if (!strcmp(ext, "objectformat")) { |
570 | int format; | |
ec91ffca | 571 | |
e0ad9574 JH |
572 | if (!value) |
573 | return config_error_nonbool(var); | |
574 | format = hash_algo_by_name(value); | |
575 | if (format == GIT_HASH_UNKNOWN) | |
1a8aea85 JNA |
576 | return error(_("invalid value for '%s': '%s'"), |
577 | "extensions.objectformat", value); | |
e0ad9574 JH |
578 | data->hash_algo = format; |
579 | return EXTENSION_OK; | |
580 | } | |
ec91ffca JK |
581 | return EXTENSION_UNKNOWN; |
582 | } | |
583 | ||
2cc7c2c7 | 584 | static int check_repo_format(const char *var, const char *value, void *vdata) |
31e26ebc | 585 | { |
2cc7c2c7 | 586 | struct repository_format *data = vdata; |
00a09d57 JK |
587 | const char *ext; |
588 | ||
31e26ebc | 589 | if (strcmp(var, "core.repositoryformatversion") == 0) |
2cc7c2c7 | 590 | data->version = git_config_int(var, value); |
00a09d57 | 591 | else if (skip_prefix(var, "extensions.", &ext)) { |
ec91ffca JK |
592 | switch (handle_extension_v0(var, value, ext, data)) { |
593 | case EXTENSION_ERROR: | |
594 | return -1; | |
595 | case EXTENSION_OK: | |
596 | return 0; | |
597 | case EXTENSION_UNKNOWN: | |
598 | break; | |
599 | } | |
600 | ||
601 | switch (handle_extension(var, value, ext, data)) { | |
602 | case EXTENSION_ERROR: | |
603 | return -1; | |
604 | case EXTENSION_OK: | |
605 | string_list_append(&data->v1_only_extensions, ext); | |
606 | return 0; | |
607 | case EXTENSION_UNKNOWN: | |
2cc7c2c7 | 608 | string_list_append(&data->unknown_extensions, ext); |
ec91ffca JK |
609 | return 0; |
610 | } | |
00a09d57 | 611 | } |
58b284a2 NTND |
612 | |
613 | return read_worktree_config(var, value, vdata); | |
31e26ebc NTND |
614 | } |
615 | ||
abade65b | 616 | static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok) |
9459aa77 | 617 | { |
7d0fb0da | 618 | struct strbuf sb = STRBUF_INIT; |
2cc7c2c7 | 619 | struct strbuf err = STRBUF_INIT; |
652f18ee | 620 | int has_common; |
00a09d57 | 621 | |
652f18ee | 622 | has_common = get_common_dir(&sb, gitdir); |
e61a509a | 623 | strbuf_addstr(&sb, "/config"); |
abade65b | 624 | read_repository_format(candidate, sb.buf); |
2cc7c2c7 | 625 | strbuf_release(&sb); |
e61a509a | 626 | |
337e51ce | 627 | /* |
2cc7c2c7 JK |
628 | * For historical use of check_repository_format() in git-init, |
629 | * we treat a missing config as a silent "ok", even when nongit_ok | |
630 | * is unset. | |
337e51ce | 631 | */ |
abade65b | 632 | if (candidate->version < 0) |
2cc7c2c7 JK |
633 | return 0; |
634 | ||
abade65b | 635 | if (verify_repository_format(candidate, &err) < 0) { |
2cc7c2c7 JK |
636 | if (nongit_ok) { |
637 | warning("%s", err.buf); | |
638 | strbuf_release(&err); | |
639 | *nongit_ok = -1; | |
640 | return -1; | |
641 | } | |
642 | die("%s", err.buf); | |
643 | } | |
644 | ||
11664196 | 645 | repository_format_precious_objects = candidate->precious_objects; |
11664196 | 646 | repository_format_worktree_config = candidate->worktree_config; |
abade65b | 647 | string_list_clear(&candidate->unknown_extensions, 0); |
ec91ffca | 648 | string_list_clear(&candidate->v1_only_extensions, 0); |
58b284a2 NTND |
649 | |
650 | if (repository_format_worktree_config) { | |
651 | /* | |
652 | * pick up core.bare and core.worktree from per-worktree | |
653 | * config if present | |
654 | */ | |
655 | strbuf_addf(&sb, "%s/config.worktree", gitdir); | |
656 | git_config_from_file(read_worktree_config, sb.buf, candidate); | |
657 | strbuf_release(&sb); | |
658 | has_common = 0; | |
659 | } | |
660 | ||
652f18ee | 661 | if (!has_common) { |
abade65b | 662 | if (candidate->is_bare != -1) { |
663 | is_bare_repository_cfg = candidate->is_bare; | |
652f18ee JK |
664 | if (is_bare_repository_cfg == 1) |
665 | inside_work_tree = -1; | |
666 | } | |
abade65b | 667 | if (candidate->work_tree) { |
652f18ee | 668 | free(git_work_tree_cfg); |
e8805af1 | 669 | git_work_tree_cfg = xstrdup(candidate->work_tree); |
2cc7c2c7 | 670 | inside_work_tree = -1; |
652f18ee | 671 | } |
2cc7c2c7 JK |
672 | } |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
16af5f1a XL |
677 | int upgrade_repository_format(int target_version) |
678 | { | |
679 | struct strbuf sb = STRBUF_INIT; | |
680 | struct strbuf err = STRBUF_INIT; | |
681 | struct strbuf repo_version = STRBUF_INIT; | |
682 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; | |
683 | ||
684 | strbuf_git_common_path(&sb, the_repository, "config"); | |
685 | read_repository_format(&repo_fmt, sb.buf); | |
686 | strbuf_release(&sb); | |
687 | ||
688 | if (repo_fmt.version >= target_version) | |
689 | return 0; | |
690 | ||
62f2eca6 JN |
691 | if (verify_repository_format(&repo_fmt, &err) < 0) { |
692 | error("cannot upgrade repository format from %d to %d: %s", | |
693 | repo_fmt.version, target_version, err.buf); | |
16af5f1a XL |
694 | strbuf_release(&err); |
695 | return -1; | |
696 | } | |
62f2eca6 JN |
697 | if (!repo_fmt.version && repo_fmt.unknown_extensions.nr) |
698 | return error("cannot upgrade repository format: " | |
699 | "unknown extension %s", | |
700 | repo_fmt.unknown_extensions.items[0].string); | |
16af5f1a XL |
701 | |
702 | strbuf_addf(&repo_version, "%d", target_version); | |
703 | git_config_set("core.repositoryformatversion", repo_version.buf); | |
704 | strbuf_release(&repo_version); | |
705 | return 1; | |
706 | } | |
707 | ||
e8805af1 MÅ |
708 | static void init_repository_format(struct repository_format *format) |
709 | { | |
710 | const struct repository_format fresh = REPOSITORY_FORMAT_INIT; | |
711 | ||
712 | memcpy(format, &fresh, sizeof(fresh)); | |
713 | } | |
714 | ||
652f18ee | 715 | int read_repository_format(struct repository_format *format, const char *path) |
2cc7c2c7 | 716 | { |
e8805af1 | 717 | clear_repository_format(format); |
652f18ee | 718 | git_config_from_file(check_repo_format, path, format); |
e8805af1 MÅ |
719 | if (format->version == -1) |
720 | clear_repository_format(format); | |
2cc7c2c7 JK |
721 | return format->version; |
722 | } | |
723 | ||
e8805af1 MÅ |
724 | void clear_repository_format(struct repository_format *format) |
725 | { | |
726 | string_list_clear(&format->unknown_extensions, 0); | |
ec91ffca | 727 | string_list_clear(&format->v1_only_extensions, 0); |
e8805af1 MÅ |
728 | free(format->work_tree); |
729 | free(format->partial_clone); | |
730 | init_repository_format(format); | |
731 | } | |
732 | ||
2cc7c2c7 JK |
733 | int verify_repository_format(const struct repository_format *format, |
734 | struct strbuf *err) | |
735 | { | |
736 | if (GIT_REPO_VERSION_READ < format->version) { | |
274db840 | 737 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
2cc7c2c7 JK |
738 | GIT_REPO_VERSION_READ, format->version); |
739 | return -1; | |
740 | } | |
741 | ||
742 | if (format->version >= 1 && format->unknown_extensions.nr) { | |
00a09d57 JK |
743 | int i; |
744 | ||
8013d7d9 AH |
745 | strbuf_addstr(err, Q_("unknown repository extension found:", |
746 | "unknown repository extensions found:", | |
747 | format->unknown_extensions.nr)); | |
00a09d57 | 748 | |
2cc7c2c7 JK |
749 | for (i = 0; i < format->unknown_extensions.nr; i++) |
750 | strbuf_addf(err, "\n\t%s", | |
751 | format->unknown_extensions.items[i].string); | |
752 | return -1; | |
00a09d57 JK |
753 | } |
754 | ||
ec91ffca JK |
755 | if (format->version == 0 && format->v1_only_extensions.nr) { |
756 | int i; | |
757 | ||
758 | strbuf_addstr(err, | |
8013d7d9 AH |
759 | Q_("repo version is 0, but v1-only extension found:", |
760 | "repo version is 0, but v1-only extensions found:", | |
761 | format->v1_only_extensions.nr)); | |
ec91ffca JK |
762 | |
763 | for (i = 0; i < format->v1_only_extensions.nr; i++) | |
764 | strbuf_addf(err, "\n\t%s", | |
765 | format->v1_only_extensions.items[i].string); | |
766 | return -1; | |
767 | } | |
768 | ||
2cc7c2c7 | 769 | return 0; |
9459aa77 NTND |
770 | } |
771 | ||
5f29433f SB |
772 | void read_gitfile_error_die(int error_code, const char *path, const char *dir) |
773 | { | |
774 | switch (error_code) { | |
775 | case READ_GITFILE_ERR_STAT_FAILED: | |
776 | case READ_GITFILE_ERR_NOT_A_FILE: | |
777 | /* non-fatal; follow return path */ | |
778 | break; | |
779 | case READ_GITFILE_ERR_OPEN_FAILED: | |
fc045fe7 | 780 | die_errno(_("error opening '%s'"), path); |
5f29433f | 781 | case READ_GITFILE_ERR_TOO_LARGE: |
fc045fe7 | 782 | die(_("too large to be a .git file: '%s'"), path); |
5f29433f | 783 | case READ_GITFILE_ERR_READ_FAILED: |
fc045fe7 | 784 | die(_("error reading %s"), path); |
5f29433f | 785 | case READ_GITFILE_ERR_INVALID_FORMAT: |
fc045fe7 | 786 | die(_("invalid gitfile format: %s"), path); |
5f29433f | 787 | case READ_GITFILE_ERR_NO_PATH: |
fc045fe7 | 788 | die(_("no path in gitfile: %s"), path); |
5f29433f | 789 | case READ_GITFILE_ERR_NOT_A_REPO: |
fc045fe7 | 790 | die(_("not a git repository: %s"), dir); |
5f29433f | 791 | default: |
033abf97 | 792 | BUG("unknown error code"); |
5f29433f SB |
793 | } |
794 | } | |
795 | ||
b44ebb19 LH |
796 | /* |
797 | * Try to read the location of the git directory from the .git file, | |
ea1d8756 HWN |
798 | * return path to git directory if found. The return value comes from |
799 | * a shared buffer. | |
a93bedad EE |
800 | * |
801 | * On failure, if return_error_code is not NULL, return_error_code | |
802 | * will be set to an error code and NULL will be returned. If | |
803 | * return_error_code is NULL the function will die instead (for most | |
804 | * cases). | |
b44ebb19 | 805 | */ |
a93bedad | 806 | const char *read_gitfile_gently(const char *path, int *return_error_code) |
b44ebb19 | 807 | { |
921bdd96 | 808 | const int max_file_size = 1 << 20; /* 1MB */ |
a93bedad EE |
809 | int error_code = 0; |
810 | char *buf = NULL; | |
811 | char *dir = NULL; | |
40c813e0 | 812 | const char *slash; |
b44ebb19 LH |
813 | struct stat st; |
814 | int fd; | |
b1905aea | 815 | ssize_t len; |
3d7747e3 | 816 | static struct strbuf realpath = STRBUF_INIT; |
b44ebb19 | 817 | |
a93bedad | 818 | if (stat(path, &st)) { |
5c4003ca | 819 | /* NEEDSWORK: discern between ENOENT vs other errors */ |
a93bedad EE |
820 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
821 | goto cleanup_return; | |
822 | } | |
823 | if (!S_ISREG(st.st_mode)) { | |
824 | error_code = READ_GITFILE_ERR_NOT_A_FILE; | |
825 | goto cleanup_return; | |
826 | } | |
921bdd96 EE |
827 | if (st.st_size > max_file_size) { |
828 | error_code = READ_GITFILE_ERR_TOO_LARGE; | |
829 | goto cleanup_return; | |
830 | } | |
b44ebb19 | 831 | fd = open(path, O_RDONLY); |
a93bedad EE |
832 | if (fd < 0) { |
833 | error_code = READ_GITFILE_ERR_OPEN_FAILED; | |
834 | goto cleanup_return; | |
835 | } | |
3733e694 | 836 | buf = xmallocz(st.st_size); |
b44ebb19 LH |
837 | len = read_in_full(fd, buf, st.st_size); |
838 | close(fd); | |
a93bedad EE |
839 | if (len != st.st_size) { |
840 | error_code = READ_GITFILE_ERR_READ_FAILED; | |
841 | goto cleanup_return; | |
842 | } | |
a93bedad EE |
843 | if (!starts_with(buf, "gitdir: ")) { |
844 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; | |
845 | goto cleanup_return; | |
846 | } | |
b44ebb19 LH |
847 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
848 | len--; | |
a93bedad EE |
849 | if (len < 9) { |
850 | error_code = READ_GITFILE_ERR_NO_PATH; | |
851 | goto cleanup_return; | |
852 | } | |
b44ebb19 | 853 | buf[len] = '\0'; |
40c813e0 BK |
854 | dir = buf + 8; |
855 | ||
856 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
857 | size_t pathlen = slash+1 - path; | |
75faa45a JK |
858 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
859 | (int)(len - 8), buf + 8); | |
40c813e0 BK |
860 | free(buf); |
861 | buf = dir; | |
862 | } | |
a93bedad EE |
863 | if (!is_git_directory(dir)) { |
864 | error_code = READ_GITFILE_ERR_NOT_A_REPO; | |
865 | goto cleanup_return; | |
866 | } | |
3d7747e3 AM |
867 | |
868 | strbuf_realpath(&realpath, dir, 1); | |
869 | path = realpath.buf; | |
40c813e0 | 870 | |
a93bedad | 871 | cleanup_return: |
a93bedad EE |
872 | if (return_error_code) |
873 | *return_error_code = error_code; | |
5f29433f SB |
874 | else if (error_code) |
875 | read_gitfile_error_die(error_code, path, dir); | |
a93bedad | 876 | |
b44ebb19 | 877 | free(buf); |
38ae8784 | 878 | return error_code ? NULL : path; |
b44ebb19 LH |
879 | } |
880 | ||
e4e30347 | 881 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
7333ed17 | 882 | struct strbuf *cwd, |
abade65b | 883 | struct repository_format *repo_fmt, |
b3f66fd3 | 884 | int *nongit_ok) |
e4e30347 | 885 | { |
b3f66fd3 NTND |
886 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
887 | const char *worktree; | |
888 | char *gitfile; | |
9b125da4 | 889 | int offset; |
e4e30347 JN |
890 | |
891 | if (PATH_MAX - 40 < strlen(gitdirenv)) | |
fc045fe7 | 892 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
b3f66fd3 | 893 | |
13d6ec91 | 894 | gitfile = (char*)read_gitfile(gitdirenv); |
b3f66fd3 NTND |
895 | if (gitfile) { |
896 | gitfile = xstrdup(gitfile); | |
897 | gitdirenv = gitfile; | |
898 | } | |
899 | ||
e4e30347 JN |
900 | if (!is_git_directory(gitdirenv)) { |
901 | if (nongit_ok) { | |
902 | *nongit_ok = 1; | |
b3f66fd3 | 903 | free(gitfile); |
e4e30347 JN |
904 | return NULL; |
905 | } | |
fc045fe7 | 906 | die(_("not a git repository: '%s'"), gitdirenv); |
e4e30347 | 907 | } |
b3f66fd3 | 908 | |
abade65b | 909 | if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) { |
b3f66fd3 NTND |
910 | free(gitfile); |
911 | return NULL; | |
e4e30347 | 912 | } |
b3f66fd3 NTND |
913 | |
914 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ | |
915 | if (work_tree_env) | |
916 | set_git_work_tree(work_tree_env); | |
917 | else if (is_bare_repository_cfg > 0) { | |
fada7674 JK |
918 | if (git_work_tree_cfg) { |
919 | /* #22.2, #30 */ | |
920 | warning("core.bare and core.worktree do not make sense"); | |
921 | work_tree_config_is_bogus = 1; | |
922 | } | |
b3f66fd3 NTND |
923 | |
924 | /* #18, #26 */ | |
0915a5b4 | 925 | set_git_dir(gitdirenv, 0); |
b3f66fd3 | 926 | free(gitfile); |
e4e30347 | 927 | return NULL; |
b3f66fd3 NTND |
928 | } |
929 | else if (git_work_tree_cfg) { /* #6, #14 */ | |
930 | if (is_absolute_path(git_work_tree_cfg)) | |
931 | set_git_work_tree(git_work_tree_cfg); | |
932 | else { | |
56b9f6e7 | 933 | char *core_worktree; |
b3f66fd3 | 934 | if (chdir(gitdirenv)) |
fc045fe7 | 935 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
b3f66fd3 | 936 | if (chdir(git_work_tree_cfg)) |
fc045fe7 | 937 | die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg); |
56b9f6e7 | 938 | core_worktree = xgetcwd(); |
7333ed17 | 939 | if (chdir(cwd->buf)) |
fc045fe7 | 940 | die_errno(_("cannot come back to cwd")); |
b3f66fd3 | 941 | set_git_work_tree(core_worktree); |
56b9f6e7 | 942 | free(core_worktree); |
b3f66fd3 NTND |
943 | } |
944 | } | |
2cd83d10 JK |
945 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
946 | /* #16d */ | |
0915a5b4 | 947 | set_git_dir(gitdirenv, 0); |
2cd83d10 JK |
948 | free(gitfile); |
949 | return NULL; | |
950 | } | |
b3f66fd3 NTND |
951 | else /* #2, #10 */ |
952 | set_git_work_tree("."); | |
953 | ||
954 | /* set_git_work_tree() must have been called by now */ | |
955 | worktree = get_git_work_tree(); | |
956 | ||
957 | /* both get_git_work_tree() and cwd are already normalized */ | |
7333ed17 | 958 | if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ |
0915a5b4 | 959 | set_git_dir(gitdirenv, 0); |
b3f66fd3 | 960 | free(gitfile); |
e4e30347 | 961 | return NULL; |
b3f66fd3 | 962 | } |
e4e30347 | 963 | |
7333ed17 | 964 | offset = dir_inside_of(cwd->buf, worktree); |
9b125da4 | 965 | if (offset >= 0) { /* cwd inside worktree? */ |
0915a5b4 | 966 | set_git_dir(gitdirenv, 1); |
b3f66fd3 | 967 | if (chdir(worktree)) |
fc045fe7 | 968 | die_errno(_("cannot chdir to '%s'"), worktree); |
7333ed17 | 969 | strbuf_addch(cwd, '/'); |
b3f66fd3 | 970 | free(gitfile); |
7333ed17 | 971 | return cwd->buf + offset; |
93a00542 | 972 | } |
b3f66fd3 NTND |
973 | |
974 | /* cwd outside worktree */ | |
0915a5b4 | 975 | set_git_dir(gitdirenv, 0); |
b3f66fd3 NTND |
976 | free(gitfile); |
977 | return NULL; | |
93a00542 JN |
978 | } |
979 | ||
9951d3b3 | 980 | static const char *setup_discovered_git_dir(const char *gitdir, |
7333ed17 | 981 | struct strbuf *cwd, int offset, |
abade65b | 982 | struct repository_format *repo_fmt, |
9951d3b3 | 983 | int *nongit_ok) |
98937bef | 984 | { |
abade65b | 985 | if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok)) |
9951d3b3 | 986 | return NULL; |
98937bef | 987 | |
4868b2ea JN |
988 | /* --work-tree is set without --git-dir; use discovered one */ |
989 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
2d4dcf21 JS |
990 | char *to_free = NULL; |
991 | const char *ret; | |
992 | ||
7333ed17 | 993 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
2d4dcf21 | 994 | gitdir = to_free = real_pathdup(gitdir, 1); |
7333ed17 | 995 | if (chdir(cwd->buf)) |
fc045fe7 | 996 | die_errno(_("cannot come back to cwd")); |
abade65b | 997 | ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
2d4dcf21 JS |
998 | free(to_free); |
999 | return ret; | |
4868b2ea JN |
1000 | } |
1001 | ||
9951d3b3 NTND |
1002 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
1003 | if (is_bare_repository_cfg > 0) { | |
0915a5b4 | 1004 | set_git_dir(gitdir, (offset != cwd->len)); |
7333ed17 | 1005 | if (chdir(cwd->buf)) |
fc045fe7 | 1006 | die_errno(_("cannot come back to cwd")); |
98937bef | 1007 | return NULL; |
9951d3b3 | 1008 | } |
98937bef | 1009 | |
9951d3b3 NTND |
1010 | /* #0, #1, #5, #8, #9, #12, #13 */ |
1011 | set_git_work_tree("."); | |
1012 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) | |
0915a5b4 | 1013 | set_git_dir(gitdir, 0); |
98937bef | 1014 | inside_git_dir = 0; |
9951d3b3 | 1015 | inside_work_tree = 1; |
5cf7b3b1 | 1016 | if (offset >= cwd->len) |
98937bef NTND |
1017 | return NULL; |
1018 | ||
df380d58 JS |
1019 | /* Make "offset" point past the '/' (already the case for root dirs) */ |
1020 | if (offset != offset_1st_component(cwd->buf)) | |
1021 | offset++; | |
1022 | /* Add a '/' at the end */ | |
7333ed17 RS |
1023 | strbuf_addch(cwd, '/'); |
1024 | return cwd->buf + offset; | |
98937bef NTND |
1025 | } |
1026 | ||
1cd8031b | 1027 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
7333ed17 | 1028 | static const char *setup_bare_git_dir(struct strbuf *cwd, int offset, |
abade65b | 1029 | struct repository_format *repo_fmt, |
7333ed17 | 1030 | int *nongit_ok) |
68698da5 JN |
1031 | { |
1032 | int root_len; | |
1033 | ||
abade65b | 1034 | if (check_repository_format_gently(".", repo_fmt, nongit_ok)) |
1cd8031b NTND |
1035 | return NULL; |
1036 | ||
2cd83d10 JK |
1037 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
1038 | ||
4868b2ea JN |
1039 | /* --work-tree is set without --git-dir; use discovered one */ |
1040 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
da6f8475 | 1041 | static const char *gitdir; |
4868b2ea | 1042 | |
7333ed17 RS |
1043 | gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset); |
1044 | if (chdir(cwd->buf)) | |
fc045fe7 | 1045 | die_errno(_("cannot come back to cwd")); |
abade65b | 1046 | return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
4868b2ea JN |
1047 | } |
1048 | ||
68698da5 | 1049 | inside_git_dir = 1; |
1cd8031b | 1050 | inside_work_tree = 0; |
7333ed17 RS |
1051 | if (offset != cwd->len) { |
1052 | if (chdir(cwd->buf)) | |
fc045fe7 | 1053 | die_errno(_("cannot come back to cwd")); |
7333ed17 RS |
1054 | root_len = offset_1st_component(cwd->buf); |
1055 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); | |
0915a5b4 | 1056 | set_git_dir(cwd->buf, 0); |
337e51ce | 1057 | } |
1cd8031b | 1058 | else |
0915a5b4 | 1059 | set_git_dir(".", 0); |
68698da5 JN |
1060 | return NULL; |
1061 | } | |
1062 | ||
2565b43b | 1063 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
60c98d1e JN |
1064 | { |
1065 | struct stat buf; | |
2565b43b | 1066 | if (stat(path, &buf)) { |
fc045fe7 | 1067 | die_errno(_("failed to stat '%*s%s%s'"), |
2565b43b | 1068 | prefix_len, |
60c98d1e JN |
1069 | prefix ? prefix : "", |
1070 | prefix ? "/" : "", path); | |
2565b43b | 1071 | } |
60c98d1e JN |
1072 | return buf.st_dev; |
1073 | } | |
1074 | ||
9e2326c7 | 1075 | /* |
1b77d83c | 1076 | * A "string_list_each_func_t" function that canonicalizes an entry |
4530a85b | 1077 | * from GIT_CEILING_DIRECTORIES using real_pathdup(), or |
7ec30aaa MH |
1078 | * discards it if unusable. The presence of an empty entry in |
1079 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all | |
1080 | * subsequent entries. | |
9e2326c7 | 1081 | */ |
1b77d83c | 1082 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
7ec30aaa | 1083 | void *cb_data) |
9e2326c7 | 1084 | { |
7ec30aaa | 1085 | int *empty_entry_found = cb_data; |
1b77d83c | 1086 | char *ceil = item->string; |
9e2326c7 | 1087 | |
7ec30aaa MH |
1088 | if (!*ceil) { |
1089 | *empty_entry_found = 1; | |
9e2326c7 | 1090 | return 0; |
7ec30aaa | 1091 | } else if (!is_absolute_path(ceil)) { |
9e2326c7 | 1092 | return 0; |
7ec30aaa MH |
1093 | } else if (*empty_entry_found) { |
1094 | /* Keep entry but do not canonicalize it */ | |
1095 | return 1; | |
1096 | } else { | |
ce83eadd | 1097 | char *real_path = real_pathdup(ceil, 0); |
4ac9006f | 1098 | if (!real_path) { |
7ec30aaa | 1099 | return 0; |
4ac9006f | 1100 | } |
7ec30aaa | 1101 | free(item->string); |
4ac9006f | 1102 | item->string = real_path; |
7ec30aaa MH |
1103 | return 1; |
1104 | } | |
9e2326c7 MH |
1105 | } |
1106 | ||
8959555c JS |
1107 | struct safe_directory_data { |
1108 | const char *path; | |
1109 | int is_safe; | |
1110 | }; | |
1111 | ||
1112 | static int safe_directory_cb(const char *key, const char *value, void *d) | |
1113 | { | |
1114 | struct safe_directory_data *data = d; | |
1115 | ||
bb50ec3c MV |
1116 | if (strcmp(key, "safe.directory")) |
1117 | return 0; | |
1118 | ||
0f85c4a3 | 1119 | if (!value || !*value) { |
8959555c | 1120 | data->is_safe = 0; |
0f85c4a3 DS |
1121 | } else if (!strcmp(value, "*")) { |
1122 | data->is_safe = 1; | |
1123 | } else { | |
8959555c JS |
1124 | const char *interpolated = NULL; |
1125 | ||
1126 | if (!git_config_pathname(&interpolated, key, value) && | |
1127 | !fspathcmp(data->path, interpolated ? interpolated : value)) | |
1128 | data->is_safe = 1; | |
1129 | ||
1130 | free((char *)interpolated); | |
1131 | } | |
1132 | ||
1133 | return 0; | |
1134 | } | |
1135 | ||
3b0bf270 CMAB |
1136 | /* |
1137 | * Check if a repository is safe, by verifying the ownership of the | |
1138 | * worktree (if any), the git directory, and the gitfile (if any). | |
1139 | * | |
1140 | * Exemptions for known-safe repositories can be added via `safe.directory` | |
1141 | * config settings; for non-bare repositories, their worktree needs to be | |
1142 | * added, for bare ones their git directory. | |
1143 | */ | |
1144 | static int ensure_valid_ownership(const char *gitfile, | |
17d3883f JS |
1145 | const char *worktree, const char *gitdir, |
1146 | struct strbuf *report) | |
8959555c | 1147 | { |
3b0bf270 CMAB |
1148 | struct safe_directory_data data = { |
1149 | .path = worktree ? worktree : gitdir | |
1150 | }; | |
8959555c | 1151 | |
e47363e5 | 1152 | if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) && |
17d3883f JS |
1153 | (!gitfile || is_path_owned_by_current_user(gitfile, report)) && |
1154 | (!worktree || is_path_owned_by_current_user(worktree, report)) && | |
1155 | (!gitdir || is_path_owned_by_current_user(gitdir, report))) | |
8959555c JS |
1156 | return 1; |
1157 | ||
3b0bf270 CMAB |
1158 | /* |
1159 | * data.path is the "path" that identifies the repository and it is | |
1160 | * constant regardless of what failed above. data.is_safe should be | |
1161 | * initialized to false, and might be changed by the callback. | |
1162 | */ | |
6061601d | 1163 | git_protected_config(safe_directory_cb, &data); |
8959555c JS |
1164 | |
1165 | return data.is_safe; | |
1166 | } | |
1167 | ||
8d1a7448 GC |
1168 | static int allowed_bare_repo_cb(const char *key, const char *value, void *d) |
1169 | { | |
1170 | enum allowed_bare_repo *allowed_bare_repo = d; | |
1171 | ||
1172 | if (strcasecmp(key, "safe.bareRepository")) | |
1173 | return 0; | |
1174 | ||
1175 | if (!strcmp(value, "explicit")) { | |
1176 | *allowed_bare_repo = ALLOWED_BARE_REPO_EXPLICIT; | |
1177 | return 0; | |
1178 | } | |
1179 | if (!strcmp(value, "all")) { | |
1180 | *allowed_bare_repo = ALLOWED_BARE_REPO_ALL; | |
1181 | return 0; | |
1182 | } | |
1183 | return -1; | |
1184 | } | |
1185 | ||
1186 | static enum allowed_bare_repo get_allowed_bare_repo(void) | |
1187 | { | |
1188 | enum allowed_bare_repo result = ALLOWED_BARE_REPO_ALL; | |
1189 | git_protected_config(allowed_bare_repo_cb, &result); | |
1190 | return result; | |
1191 | } | |
1192 | ||
1193 | static const char *allowed_bare_repo_to_string( | |
1194 | enum allowed_bare_repo allowed_bare_repo) | |
1195 | { | |
1196 | switch (allowed_bare_repo) { | |
1197 | case ALLOWED_BARE_REPO_EXPLICIT: | |
1198 | return "explicit"; | |
1199 | case ALLOWED_BARE_REPO_ALL: | |
1200 | return "all"; | |
1201 | default: | |
1202 | BUG("invalid allowed_bare_repo %d", | |
1203 | allowed_bare_repo); | |
1204 | } | |
1205 | return NULL; | |
1206 | } | |
1207 | ||
ce9b8aab JS |
1208 | enum discovery_result { |
1209 | GIT_DIR_NONE = 0, | |
1210 | GIT_DIR_EXPLICIT, | |
1211 | GIT_DIR_DISCOVERED, | |
1212 | GIT_DIR_BARE, | |
1213 | /* these are errors */ | |
1214 | GIT_DIR_HIT_CEILING = -1, | |
01017dce | 1215 | GIT_DIR_HIT_MOUNT_POINT = -2, |
8959555c | 1216 | GIT_DIR_INVALID_GITFILE = -3, |
8d1a7448 GC |
1217 | GIT_DIR_INVALID_OWNERSHIP = -4, |
1218 | GIT_DIR_DISALLOWED_BARE = -5, | |
ce9b8aab JS |
1219 | }; |
1220 | ||
e90fdc39 JS |
1221 | /* |
1222 | * We cannot decide in this function whether we are in the work tree or | |
1223 | * not, since the config can only be read _after_ this function was called. | |
ce9b8aab JS |
1224 | * |
1225 | * Also, we avoid changing any global state (such as the current working | |
1226 | * directory) to allow early callers. | |
1227 | * | |
1228 | * The directory where the search should start needs to be passed in via the | |
1229 | * `dir` parameter; upon return, the `dir` buffer will contain the path of | |
1230 | * the directory where the search ended, and `gitdir` will contain the path of | |
1231 | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it | |
1232 | * is relative to `dir` (i.e. *not* necessarily the cwd). | |
e90fdc39 | 1233 | */ |
ce9b8aab | 1234 | static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, |
01017dce | 1235 | struct strbuf *gitdir, |
17d3883f | 1236 | struct strbuf *report, |
01017dce | 1237 | int die_on_error) |
d288a700 | 1238 | { |
0454dd93 | 1239 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
31171d9e | 1240 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
ce9b8aab | 1241 | const char *gitdirenv; |
d17f2124 | 1242 | int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); |
c7d1d1b1 RH |
1243 | dev_t current_device = 0; |
1244 | int one_filesystem = 1; | |
d288a700 | 1245 | |
e90fdc39 JS |
1246 | /* |
1247 | * If GIT_DIR is set explicitly, we're not going | |
1248 | * to do any discovery, but we still do repository | |
1249 | * validation. | |
1250 | */ | |
ad1a382f | 1251 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
ce9b8aab JS |
1252 | if (gitdirenv) { |
1253 | strbuf_addstr(gitdir, gitdirenv); | |
1254 | return GIT_DIR_EXPLICIT; | |
1255 | } | |
d288a700 | 1256 | |
31171d9e | 1257 | if (env_ceiling_dirs) { |
7ec30aaa MH |
1258 | int empty_entry_found = 0; |
1259 | ||
31171d9e | 1260 | string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); |
1b77d83c | 1261 | filter_string_list(&ceiling_dirs, 0, |
7ec30aaa | 1262 | canonicalize_ceiling_entry, &empty_entry_found); |
ce9b8aab | 1263 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
31171d9e MH |
1264 | string_list_clear(&ceiling_dirs, 0); |
1265 | } | |
1266 | ||
ce9b8aab JS |
1267 | if (ceil_offset < 0) |
1268 | ceil_offset = min_offset - 2; | |
d288a700 | 1269 | |
e2683d51 JS |
1270 | if (min_offset && min_offset == dir->len && |
1271 | !is_dir_sep(dir->buf[min_offset - 1])) { | |
1272 | strbuf_addch(dir, '/'); | |
1273 | min_offset++; | |
1274 | } | |
1275 | ||
892c41b9 | 1276 | /* |
ce9b8aab | 1277 | * Test in the following order (relative to the dir): |
b44ebb19 | 1278 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
1279 | * - .git/ |
1280 | * - ./ (bare) | |
b44ebb19 | 1281 | * - ../.git |
e90fdc39 JS |
1282 | * - ../.git/ |
1283 | * - ../ (bare) | |
176b2d32 | 1284 | * - ../../.git |
e90fdc39 | 1285 | * etc. |
892c41b9 | 1286 | */ |
cf87463e | 1287 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
60c98d1e | 1288 | if (one_filesystem) |
ce9b8aab | 1289 | current_device = get_device_or_die(dir->buf, NULL, 0); |
e90fdc39 | 1290 | for (;;) { |
01017dce | 1291 | int offset = dir->len, error_code = 0; |
3b0bf270 CMAB |
1292 | char *gitdir_path = NULL; |
1293 | char *gitfile = NULL; | |
ce9b8aab JS |
1294 | |
1295 | if (offset > min_offset) | |
1296 | strbuf_addch(dir, '/'); | |
1297 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); | |
01017dce JS |
1298 | gitdirenv = read_gitfile_gently(dir->buf, die_on_error ? |
1299 | NULL : &error_code); | |
1300 | if (!gitdirenv) { | |
1301 | if (die_on_error || | |
1302 | error_code == READ_GITFILE_ERR_NOT_A_FILE) { | |
5c4003ca | 1303 | /* NEEDSWORK: fail if .git is not file nor dir */ |
3b0bf270 | 1304 | if (is_git_directory(dir->buf)) { |
01017dce | 1305 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
3b0bf270 CMAB |
1306 | gitdir_path = xstrdup(dir->buf); |
1307 | } | |
01017dce JS |
1308 | } else if (error_code != READ_GITFILE_ERR_STAT_FAILED) |
1309 | return GIT_DIR_INVALID_GITFILE; | |
3b0bf270 CMAB |
1310 | } else |
1311 | gitfile = xstrdup(dir->buf); | |
1312 | /* | |
1313 | * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT | |
1314 | * to check that directory for a repository. | |
1315 | * Now trim that tentative addition away, because we want to | |
1316 | * focus on the real directory we are in. | |
1317 | */ | |
ce9b8aab | 1318 | strbuf_setlen(dir, offset); |
9951d3b3 | 1319 | if (gitdirenv) { |
3b0bf270 | 1320 | enum discovery_result ret; |
d51e1dff JS |
1321 | const char *gitdir_candidate = |
1322 | gitdir_path ? gitdir_path : gitdirenv; | |
3b0bf270 | 1323 | |
d51e1dff | 1324 | if (ensure_valid_ownership(gitfile, dir->buf, |
17d3883f | 1325 | gitdir_candidate, report)) { |
3b0bf270 CMAB |
1326 | strbuf_addstr(gitdir, gitdirenv); |
1327 | ret = GIT_DIR_DISCOVERED; | |
1328 | } else | |
1329 | ret = GIT_DIR_INVALID_OWNERSHIP; | |
1330 | ||
1331 | /* | |
1332 | * Earlier, during discovery, we might have allocated | |
1333 | * string copies for gitdir_path or gitfile so make | |
1334 | * sure we don't leak by freeing them now, before | |
1335 | * leaving the loop and function. | |
1336 | * | |
1337 | * Note: gitdirenv will be non-NULL whenever these are | |
1338 | * allocated, therefore we need not take care of releasing | |
1339 | * them outside of this conditional block. | |
1340 | */ | |
1341 | free(gitdir_path); | |
1342 | free(gitfile); | |
1343 | ||
1344 | return ret; | |
9951d3b3 | 1345 | } |
9951d3b3 | 1346 | |
ce9b8aab | 1347 | if (is_git_directory(dir->buf)) { |
8d1a7448 GC |
1348 | if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT) |
1349 | return GIT_DIR_DISALLOWED_BARE; | |
17d3883f | 1350 | if (!ensure_valid_ownership(NULL, NULL, dir->buf, report)) |
8959555c | 1351 | return GIT_DIR_INVALID_OWNERSHIP; |
ce9b8aab JS |
1352 | strbuf_addstr(gitdir, "."); |
1353 | return GIT_DIR_BARE; | |
502ffe34 | 1354 | } |
9951d3b3 | 1355 | |
ce9b8aab JS |
1356 | if (offset <= min_offset) |
1357 | return GIT_DIR_HIT_CEILING; | |
1cd8031b | 1358 | |
ce9b8aab | 1359 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
6c1e6544 | 1360 | ; /* continue */ |
ce9b8aab JS |
1361 | if (offset <= ceil_offset) |
1362 | return GIT_DIR_HIT_CEILING; | |
1363 | ||
1364 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); | |
1365 | if (one_filesystem && | |
1366 | current_device != get_device_or_die(dir->buf, NULL, offset)) | |
1367 | return GIT_DIR_HIT_MOUNT_POINT; | |
892c41b9 | 1368 | } |
d288a700 | 1369 | } |
5e7bfe25 | 1370 | |
d3fb71b3 BW |
1371 | int discover_git_directory(struct strbuf *commondir, |
1372 | struct strbuf *gitdir) | |
16ac8b8d JS |
1373 | { |
1374 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; | |
1375 | size_t gitdir_offset = gitdir->len, cwd_len; | |
d3fb71b3 | 1376 | size_t commondir_offset = commondir->len; |
e8805af1 | 1377 | struct repository_format candidate = REPOSITORY_FORMAT_INIT; |
16ac8b8d JS |
1378 | |
1379 | if (strbuf_getcwd(&dir)) | |
d3fb71b3 | 1380 | return -1; |
16ac8b8d JS |
1381 | |
1382 | cwd_len = dir.len; | |
17d3883f | 1383 | if (setup_git_directory_gently_1(&dir, gitdir, NULL, 0) <= 0) { |
16ac8b8d | 1384 | strbuf_release(&dir); |
d3fb71b3 | 1385 | return -1; |
16ac8b8d JS |
1386 | } |
1387 | ||
1388 | /* | |
1389 | * The returned gitdir is relative to dir, and if dir does not reflect | |
1390 | * the current working directory, we simply make the gitdir absolute. | |
1391 | */ | |
1392 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { | |
1393 | /* Avoid a trailing "/." */ | |
1394 | if (!strcmp(".", gitdir->buf + gitdir_offset)) | |
1395 | strbuf_setlen(gitdir, gitdir_offset); | |
1396 | else | |
1397 | strbuf_addch(&dir, '/'); | |
1398 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); | |
1399 | } | |
1400 | ||
d3fb71b3 BW |
1401 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
1402 | ||
16ac8b8d | 1403 | strbuf_reset(&dir); |
d3fb71b3 | 1404 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
16ac8b8d JS |
1405 | read_repository_format(&candidate, dir.buf); |
1406 | strbuf_release(&dir); | |
1407 | ||
1408 | if (verify_repository_format(&candidate, &err) < 0) { | |
1409 | warning("ignoring git dir '%s': %s", | |
1410 | gitdir->buf + gitdir_offset, err.buf); | |
1411 | strbuf_release(&err); | |
d3fb71b3 | 1412 | strbuf_setlen(commondir, commondir_offset); |
69743f9b | 1413 | strbuf_setlen(gitdir, gitdir_offset); |
e8805af1 | 1414 | clear_repository_format(&candidate); |
d3fb71b3 | 1415 | return -1; |
16ac8b8d JS |
1416 | } |
1417 | ||
ebaf3bcf JT |
1418 | /* take ownership of candidate.partial_clone */ |
1419 | the_repository->repository_format_partial_clone = | |
1420 | candidate.partial_clone; | |
1421 | candidate.partial_clone = NULL; | |
1422 | ||
e8805af1 | 1423 | clear_repository_format(&candidate); |
d3fb71b3 | 1424 | return 0; |
16ac8b8d JS |
1425 | } |
1426 | ||
a60645f9 NTND |
1427 | const char *setup_git_directory_gently(int *nongit_ok) |
1428 | { | |
ce9b8aab | 1429 | static struct strbuf cwd = STRBUF_INIT; |
17d3883f | 1430 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT; |
07098b81 | 1431 | const char *prefix = NULL; |
e8805af1 | 1432 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
a60645f9 | 1433 | |
ce9b8aab JS |
1434 | /* |
1435 | * We may have read an incomplete configuration before | |
1436 | * setting-up the git directory. If so, clear the cache so | |
1437 | * that the next queries to the configuration reload complete | |
1438 | * configuration (including the per-repo config file that we | |
1439 | * ignored previously). | |
1440 | */ | |
1441 | git_config_clear(); | |
1442 | ||
1443 | /* | |
1444 | * Let's assume that we are in a git repository. | |
1445 | * If it turns out later that we are somewhere else, the value will be | |
1446 | * updated accordingly. | |
1447 | */ | |
1448 | if (nongit_ok) | |
1449 | *nongit_ok = 0; | |
1450 | ||
1451 | if (strbuf_getcwd(&cwd)) | |
1452 | die_errno(_("Unable to read current working directory")); | |
1453 | strbuf_addbuf(&dir, &cwd); | |
1454 | ||
17d3883f | 1455 | switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) { |
ce9b8aab | 1456 | case GIT_DIR_EXPLICIT: |
abade65b | 1457 | prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1458 | break; |
1459 | case GIT_DIR_DISCOVERED: | |
1460 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1461 | die(_("cannot change to '%s'"), dir.buf); |
ce9b8aab | 1462 | prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len, |
abade65b | 1463 | &repo_fmt, nongit_ok); |
ce9b8aab JS |
1464 | break; |
1465 | case GIT_DIR_BARE: | |
1466 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1467 | die(_("cannot change to '%s'"), dir.buf); |
abade65b | 1468 | prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1469 | break; |
1470 | case GIT_DIR_HIT_CEILING: | |
07098b81 ED |
1471 | if (!nongit_ok) |
1472 | die(_("not a git repository (or any of the parent directories): %s"), | |
1473 | DEFAULT_GIT_DIR_ENVIRONMENT); | |
1474 | *nongit_ok = 1; | |
ce9b8aab JS |
1475 | break; |
1476 | case GIT_DIR_HIT_MOUNT_POINT: | |
07098b81 ED |
1477 | if (!nongit_ok) |
1478 | die(_("not a git repository (or any parent up to mount point %s)\n" | |
1479 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), | |
1480 | dir.buf); | |
1481 | *nongit_ok = 1; | |
1482 | break; | |
8959555c JS |
1483 | case GIT_DIR_INVALID_OWNERSHIP: |
1484 | if (!nongit_ok) { | |
1485 | struct strbuf quoted = STRBUF_INIT; | |
1486 | ||
17d3883f | 1487 | strbuf_complete(&report, '\n'); |
8959555c | 1488 | sq_quote_buf_pretty("ed, dir.buf); |
3b0bf270 | 1489 | die(_("detected dubious ownership in repository at '%s'\n" |
17d3883f | 1490 | "%s" |
8959555c JS |
1491 | "To add an exception for this directory, call:\n" |
1492 | "\n" | |
1493 | "\tgit config --global --add safe.directory %s"), | |
17d3883f | 1494 | dir.buf, report.buf, quoted.buf); |
8959555c JS |
1495 | } |
1496 | *nongit_ok = 1; | |
1497 | break; | |
8d1a7448 GC |
1498 | case GIT_DIR_DISALLOWED_BARE: |
1499 | if (!nongit_ok) { | |
1500 | die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"), | |
1501 | dir.buf, | |
1502 | allowed_bare_repo_to_string(get_allowed_bare_repo())); | |
1503 | } | |
1504 | *nongit_ok = 1; | |
1505 | break; | |
07098b81 ED |
1506 | case GIT_DIR_NONE: |
1507 | /* | |
1508 | * As a safeguard against setup_git_directory_gently_1 returning | |
1509 | * this value, fallthrough to BUG. Otherwise it is possible to | |
1510 | * set startup_info->have_repository to 1 when we did nothing to | |
1511 | * find a repository. | |
1512 | */ | |
ce9b8aab | 1513 | default: |
a3ba4fa7 | 1514 | BUG("unhandled setup_git_directory_gently_1() result"); |
ce9b8aab JS |
1515 | } |
1516 | ||
07098b81 ED |
1517 | /* |
1518 | * At this point, nongit_ok is stable. If it is non-NULL and points | |
1519 | * to a non-zero value, then this means that we haven't found a | |
1520 | * repository and that the caller expects startup_info to reflect | |
1521 | * this. | |
1522 | * | |
1523 | * Regardless of the state of nongit_ok, startup_info->prefix and | |
1524 | * the GIT_PREFIX environment variable must always match. For details | |
1525 | * see Documentation/config/alias.txt. | |
1526 | */ | |
c7d0e610 | 1527 | if (nongit_ok && *nongit_ok) |
07098b81 | 1528 | startup_info->have_repository = 0; |
c7d0e610 | 1529 | else |
07098b81 | 1530 | startup_info->have_repository = 1; |
46c3cd44 | 1531 | |
73f192c9 BW |
1532 | /* |
1533 | * Not all paths through the setup code will call 'set_git_dir()' (which | |
1534 | * directly sets up the environment) so in order to guarantee that the | |
1535 | * environment is in a consistent state after setup, explicitly setup | |
1536 | * the environment if we have a repository. | |
1537 | * | |
1538 | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some | |
1539 | * code paths so we also need to explicitly setup the environment if | |
1540 | * the user has set GIT_DIR. It may be beneficial to disallow bogus | |
1541 | * GIT_DIR values at some point in the future. | |
1542 | */ | |
07098b81 ED |
1543 | if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */ |
1544 | startup_info->have_repository || | |
1545 | /* GIT_DIR_EXPLICIT */ | |
1546 | getenv(GIT_DIR_ENVIRONMENT)) { | |
c14c234f BW |
1547 | if (!the_repository->gitdir) { |
1548 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); | |
1549 | if (!gitdir) | |
1550 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; | |
357a03eb | 1551 | setup_git_env(gitdir); |
c14c234f | 1552 | } |
ebaf3bcf | 1553 | if (startup_info->have_repository) { |
78a67668 | 1554 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); |
ebaf3bcf JT |
1555 | /* take ownership of repo_fmt.partial_clone */ |
1556 | the_repository->repository_format_partial_clone = | |
1557 | repo_fmt.partial_clone; | |
1558 | repo_fmt.partial_clone = NULL; | |
1559 | } | |
c14c234f | 1560 | } |
c7d0e610 TB |
1561 | /* |
1562 | * Since precompose_string_if_needed() needs to look at | |
1563 | * the core.precomposeunicode configuration, this | |
1564 | * has to happen after the above block that finds | |
1565 | * out where the repository is, i.e. a preparation | |
1566 | * for calling git_config_get_bool(). | |
1567 | */ | |
1568 | if (prefix) { | |
1569 | prefix = precompose_string_if_needed(prefix); | |
1570 | startup_info->prefix = prefix; | |
1571 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); | |
1572 | } else { | |
1573 | startup_info->prefix = NULL; | |
1574 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); | |
1575 | } | |
1576 | ||
e6f8861b | 1577 | setup_original_cwd(); |
73f192c9 | 1578 | |
ce9b8aab JS |
1579 | strbuf_release(&dir); |
1580 | strbuf_release(&gitdir); | |
17d3883f | 1581 | strbuf_release(&report); |
e8805af1 | 1582 | clear_repository_format(&repo_fmt); |
ce9b8aab | 1583 | |
a60645f9 NTND |
1584 | return prefix; |
1585 | } | |
1586 | ||
94df2506 JH |
1587 | int git_config_perm(const char *var, const char *value) |
1588 | { | |
06cbe855 HO |
1589 | int i; |
1590 | char *endptr; | |
1591 | ||
afe8a907 | 1592 | if (!value) |
06cbe855 HO |
1593 | return PERM_GROUP; |
1594 | ||
1595 | if (!strcmp(value, "umask")) | |
1596 | return PERM_UMASK; | |
1597 | if (!strcmp(value, "group")) | |
1598 | return PERM_GROUP; | |
1599 | if (!strcmp(value, "all") || | |
1600 | !strcmp(value, "world") || | |
1601 | !strcmp(value, "everybody")) | |
1602 | return PERM_EVERYBODY; | |
1603 | ||
1604 | /* Parse octal numbers */ | |
1605 | i = strtol(value, &endptr, 8); | |
1606 | ||
1607 | /* If not an octal number, maybe true/false? */ | |
1608 | if (*endptr != 0) | |
1609 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
1610 | ||
1611 | /* | |
1612 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 1613 | * a chmod value to restrict to. |
06cbe855 HO |
1614 | */ |
1615 | switch (i) { | |
1616 | case PERM_UMASK: /* 0 */ | |
1617 | return PERM_UMASK; | |
1618 | case OLD_PERM_GROUP: /* 1 */ | |
1619 | return PERM_GROUP; | |
1620 | case OLD_PERM_EVERYBODY: /* 2 */ | |
1621 | return PERM_EVERYBODY; | |
94df2506 | 1622 | } |
06cbe855 HO |
1623 | |
1624 | /* A filemode value was given: 0xxx */ | |
1625 | ||
1626 | if ((i & 0600) != 0600) | |
fc045fe7 | 1627 | die(_("problem with core.sharedRepository filemode value " |
06cbe855 | 1628 | "(0%.3o).\nThe owner of files must always have " |
2ff30e67 | 1629 | "read and write permissions."), i); |
06cbe855 HO |
1630 | |
1631 | /* | |
1632 | * Mask filemode value. Others can not get write permission. | |
1633 | * x flags for directories are handled separately. | |
1634 | */ | |
5a688fe4 | 1635 | return -(i & 0666); |
94df2506 JH |
1636 | } |
1637 | ||
cfe3917c | 1638 | void check_repository_format(struct repository_format *fmt) |
ab9cb76f | 1639 | { |
e8805af1 | 1640 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
cfe3917c | 1641 | if (!fmt) |
1642 | fmt = &repo_fmt; | |
1643 | check_repository_format_gently(get_git_dir(), fmt, NULL); | |
f1c126bd | 1644 | startup_info->have_repository = 1; |
d553aceb | 1645 | repo_set_hash_algo(the_repository, fmt->hash_algo); |
ebaf3bcf JT |
1646 | the_repository->repository_format_partial_clone = |
1647 | xstrdup_or_null(fmt->partial_clone); | |
e8805af1 | 1648 | clear_repository_format(&repo_fmt); |
ab9cb76f JH |
1649 | } |
1650 | ||
e1e5ec86 CB |
1651 | /* |
1652 | * Returns the "prefix", a path to the current working directory | |
1653 | * relative to the work tree root, or NULL, if the current working | |
1654 | * directory is not a strict subdirectory of the work tree root. The | |
1655 | * prefix always ends with a '/' character. | |
1656 | */ | |
5e7bfe25 JH |
1657 | const char *setup_git_directory(void) |
1658 | { | |
b3f66fd3 | 1659 | return setup_git_directory_gently(NULL); |
5e7bfe25 | 1660 | } |
abc06822 | 1661 | |
40d96325 | 1662 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
abc06822 FG |
1663 | { |
1664 | if (is_git_directory(suspect)) | |
1665 | return suspect; | |
40d96325 | 1666 | return read_gitfile_gently(suspect, return_error_code); |
abc06822 | 1667 | } |
1d999ddd TR |
1668 | |
1669 | /* if any standard file descriptor is missing open it to /dev/null */ | |
1670 | void sanitize_stdfds(void) | |
1671 | { | |
d9a65b6c RS |
1672 | int fd = xopen("/dev/null", O_RDWR); |
1673 | while (fd < 2) | |
1674 | fd = xdup(fd); | |
1d999ddd TR |
1675 | if (fd > 2) |
1676 | close(fd); | |
1677 | } | |
de0957ce NTND |
1678 | |
1679 | int daemonize(void) | |
1680 | { | |
1681 | #ifdef NO_POSIX_GOODIES | |
1682 | errno = ENOSYS; | |
1683 | return -1; | |
1684 | #else | |
1685 | switch (fork()) { | |
1686 | case 0: | |
1687 | break; | |
1688 | case -1: | |
fc045fe7 | 1689 | die_errno(_("fork failed")); |
de0957ce NTND |
1690 | default: |
1691 | exit(0); | |
1692 | } | |
1693 | if (setsid() == -1) | |
fc045fe7 | 1694 | die_errno(_("setsid failed")); |
de0957ce NTND |
1695 | close(0); |
1696 | close(1); | |
1697 | close(2); | |
1698 | sanitize_stdfds(); | |
1699 | return 0; | |
1700 | #endif | |
1701 | } |