]>
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" |
e90fdc39 JS |
8 | |
9 | static int inside_git_dir = -1; | |
10 | static int inside_work_tree = -1; | |
fada7674 | 11 | static int work_tree_config_is_bogus; |
d288a700 | 12 | |
46c3cd44 JK |
13 | static struct startup_info the_startup_info; |
14 | struct startup_info *startup_info = &the_startup_info; | |
15 | ||
ddc2a628 MEW |
16 | /* |
17 | * The input parameter must contain an absolute path, and it must already be | |
18 | * normalized. | |
19 | * | |
20 | * Find the part of an absolute path that lies inside the work tree by | |
21 | * dereferencing symlinks outside the work tree, for example: | |
22 | * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file | |
23 | * /dir/file (work tree is /) -> dir/file | |
24 | * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2 | |
25 | * /dir/repolink/file (repolink points to /dir/repo) -> file | |
26 | * /dir/repo (exactly equal to work tree) -> (empty string) | |
27 | */ | |
28 | static int abspath_part_inside_repo(char *path) | |
29 | { | |
30 | size_t len; | |
31 | size_t wtlen; | |
32 | char *path0; | |
33 | int off; | |
34 | const char *work_tree = get_git_work_tree(); | |
35 | ||
36 | if (!work_tree) | |
37 | return -1; | |
38 | wtlen = strlen(work_tree); | |
39 | len = strlen(path); | |
6127ff63 | 40 | off = offset_1st_component(path); |
ddc2a628 MEW |
41 | |
42 | /* check if work tree is already the prefix */ | |
d8727b36 | 43 | if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) { |
ddc2a628 MEW |
44 | if (path[wtlen] == '/') { |
45 | memmove(path, path + wtlen + 1, len - wtlen); | |
46 | return 0; | |
47 | } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') { | |
48 | /* work tree is the root, or the whole path */ | |
49 | memmove(path, path + wtlen, len - wtlen + 1); | |
50 | return 0; | |
51 | } | |
52 | /* work tree might match beginning of a symlink to work tree */ | |
53 | off = wtlen; | |
54 | } | |
55 | path0 = path; | |
6127ff63 | 56 | path += off; |
ddc2a628 MEW |
57 | |
58 | /* check each '/'-terminated level */ | |
59 | while (*path) { | |
60 | path++; | |
61 | if (*path == '/') { | |
62 | *path = '\0'; | |
d8727b36 | 63 | if (fspathcmp(real_path(path0), work_tree) == 0) { |
ddc2a628 MEW |
64 | memmove(path0, path + 1, len - (path - path0)); |
65 | return 0; | |
66 | } | |
67 | *path = '/'; | |
68 | } | |
69 | } | |
70 | ||
71 | /* check whole path */ | |
d8727b36 | 72 | if (fspathcmp(real_path(path0), work_tree) == 0) { |
ddc2a628 MEW |
73 | *path0 = '\0'; |
74 | return 0; | |
75 | } | |
76 | ||
77 | return -1; | |
78 | } | |
79 | ||
645a29c4 NTND |
80 | /* |
81 | * Normalize "path", prepending the "prefix" for relative paths. If | |
82 | * remaining_prefix is not NULL, return the actual prefix still | |
83 | * remains in the path. For example, prefix = sub1/sub2/ and path is | |
84 | * | |
85 | * foo -> sub1/sub2/foo (full prefix) | |
86 | * ../foo -> sub1/foo (remaining prefix is sub1/) | |
87 | * ../../bar -> bar (no remaining prefix) | |
88 | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) | |
89 | * `pwd`/../bar -> sub1/bar (no remaining prefix) | |
90 | */ | |
91 | char *prefix_path_gently(const char *prefix, int len, | |
92 | int *remaining_prefix, const char *path) | |
d089ebaa JH |
93 | { |
94 | const char *orig = path; | |
18e051a3 CMAB |
95 | char *sanitized; |
96 | if (is_absolute_path(orig)) { | |
3733e694 | 97 | sanitized = xmallocz(strlen(path)); |
645a29c4 NTND |
98 | if (remaining_prefix) |
99 | *remaining_prefix = 0; | |
655ee9ea MEW |
100 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
101 | free(sanitized); | |
102 | return NULL; | |
103 | } | |
104 | if (abspath_part_inside_repo(sanitized)) { | |
105 | free(sanitized); | |
106 | return NULL; | |
107 | } | |
18e051a3 | 108 | } else { |
24041d6b | 109 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
645a29c4 NTND |
110 | if (remaining_prefix) |
111 | *remaining_prefix = len; | |
655ee9ea | 112 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
546e0fd9 JK |
113 | free(sanitized); |
114 | return NULL; | |
d089ebaa | 115 | } |
d089ebaa JH |
116 | } |
117 | return sanitized; | |
f332726e LT |
118 | } |
119 | ||
546e0fd9 JK |
120 | char *prefix_path(const char *prefix, int len, const char *path) |
121 | { | |
645a29c4 | 122 | char *r = prefix_path_gently(prefix, len, NULL, path); |
5c203986 ES |
123 | if (!r) { |
124 | const char *hint_path = get_git_work_tree(); | |
125 | if (!hint_path) | |
126 | hint_path = get_git_dir(); | |
e0020b2f | 127 | die(_("'%s' is outside repository at '%s'"), path, |
5c203986 ES |
128 | absolute_path(hint_path)); |
129 | } | |
546e0fd9 JK |
130 | return r; |
131 | } | |
132 | ||
133 | int path_inside_repo(const char *prefix, const char *path) | |
134 | { | |
135 | int len = prefix ? strlen(prefix) : 0; | |
645a29c4 | 136 | char *r = prefix_path_gently(prefix, len, NULL, path); |
546e0fd9 JK |
137 | if (r) { |
138 | free(r); | |
139 | return 1; | |
140 | } | |
141 | return 0; | |
142 | } | |
143 | ||
c6e8c800 JH |
144 | int check_filename(const char *prefix, const char *arg) |
145 | { | |
e4da43b1 | 146 | char *to_free = NULL; |
c6e8c800 JH |
147 | struct stat st; |
148 | ||
d51c6ee0 JK |
149 | if (skip_prefix(arg, ":/", &arg)) { |
150 | if (!*arg) /* ":/" is root dir, always exists */ | |
4db86e8b | 151 | return 1; |
a08cbcda | 152 | prefix = NULL; |
42471bce JK |
153 | } else if (skip_prefix(arg, ":!", &arg) || |
154 | skip_prefix(arg, ":^", &arg)) { | |
155 | if (!*arg) /* excluding everything is silly, but allowed */ | |
156 | return 1; | |
a08cbcda JK |
157 | } |
158 | ||
159 | if (prefix) | |
160 | arg = to_free = prefix_filename(prefix, arg); | |
161 | ||
162 | if (!lstat(arg, &st)) { | |
e4da43b1 | 163 | free(to_free); |
c6e8c800 | 164 | return 1; /* file exists */ |
e4da43b1 | 165 | } |
93dd544f | 166 | if (is_missing_file_error(errno)) { |
e4da43b1 | 167 | free(to_free); |
c6e8c800 | 168 | return 0; /* file does not exist */ |
e4da43b1 | 169 | } |
fc045fe7 | 170 | die_errno(_("failed to stat '%s'"), arg); |
c6e8c800 JH |
171 | } |
172 | ||
e270f42c NTND |
173 | static void NORETURN die_verify_filename(struct repository *r, |
174 | const char *prefix, | |
023e37c3 MM |
175 | const char *arg, |
176 | int diagnose_misspelt_rev) | |
009fee47 | 177 | { |
023e37c3 | 178 | if (!diagnose_misspelt_rev) |
ab33a76e VA |
179 | die(_("%s: no such path in the working tree.\n" |
180 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally."), | |
023e37c3 | 181 | arg); |
0e539dca JH |
182 | /* |
183 | * Saying "'(icase)foo' does not exist in the index" when the | |
184 | * user gave us ":(icase)foo" is just stupid. A magic pathspec | |
185 | * begins with a colon and is followed by a non-alnum; do not | |
8c135ea2 | 186 | * let maybe_die_on_misspelt_object_name() even trigger. |
0e539dca JH |
187 | */ |
188 | if (!(arg[0] == ':' && !isalnum(arg[1]))) | |
e270f42c | 189 | maybe_die_on_misspelt_object_name(r, arg, prefix); |
0e539dca | 190 | |
009fee47 | 191 | /* ... or fall back the most general message. */ |
ab33a76e VA |
192 | die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
193 | "Use '--' to separate paths from revisions, like this:\n" | |
194 | "'git <command> [<revision>...] -- [<file>...]'"), arg); | |
009fee47 MM |
195 | |
196 | } | |
197 | ||
c99eddd8 JK |
198 | /* |
199 | * Check for arguments that don't resolve as actual files, | |
200 | * but which look sufficiently like pathspecs that we'll consider | |
201 | * them such for the purposes of rev/pathspec DWIM parsing. | |
202 | */ | |
203 | static int looks_like_pathspec(const char *arg) | |
204 | { | |
205 | /* anything with a wildcard character */ | |
206 | if (!no_wildcard(arg)) | |
207 | return 1; | |
208 | ||
209 | /* long-form pathspec magic */ | |
210 | if (starts_with(arg, ":(")) | |
211 | return 1; | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
e23d0b4a LT |
216 | /* |
217 | * Verify a filename that we got as an argument for a pathspec | |
218 | * entry. Note that a filename that begins with "-" never verifies | |
219 | * as true, because even if such a filename were to exist, we want | |
220 | * it to be preceded by the "--" marker (or we want the user to | |
221 | * use a format like "./-filename") | |
023e37c3 MM |
222 | * |
223 | * The "diagnose_misspelt_rev" is used to provide a user-friendly | |
224 | * diagnosis when dying upon finding that "name" is not a pathname. | |
225 | * If set to 1, the diagnosis will try to diagnose "name" as an | |
226 | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis | |
227 | * will only complain about an inexisting file. | |
228 | * | |
229 | * This function is typically called to check that a "file or rev" | |
230 | * argument is unambiguous. In this case, the caller will want | |
231 | * diagnose_misspelt_rev == 1 when verifying the first non-rev | |
232 | * argument (which could have been a revision), and | |
233 | * diagnose_misspelt_rev == 0 for the next ones (because we already | |
234 | * saw a filename, there's not ambiguity anymore). | |
e23d0b4a | 235 | */ |
023e37c3 MM |
236 | void verify_filename(const char *prefix, |
237 | const char *arg, | |
238 | int diagnose_misspelt_rev) | |
e23d0b4a | 239 | { |
e23d0b4a | 240 | if (*arg == '-') |
fc045fe7 | 241 | die(_("option '%s' must come before non-option arguments"), arg); |
2cb47ab6 | 242 | if (looks_like_pathspec(arg) || check_filename(prefix, arg)) |
e23d0b4a | 243 | return; |
e270f42c | 244 | die_verify_filename(the_repository, prefix, arg, diagnose_misspelt_rev); |
e23d0b4a LT |
245 | } |
246 | ||
ea92f41f JH |
247 | /* |
248 | * Opposite of the above: the command line did not have -- marker | |
249 | * and we parsed the arg as a refname. It should not be interpretable | |
250 | * as a filename. | |
251 | */ | |
252 | void verify_non_filename(const char *prefix, const char *arg) | |
253 | { | |
7ae3df8c | 254 | if (!is_inside_work_tree() || is_inside_git_dir()) |
68025633 | 255 | return; |
ea92f41f JH |
256 | if (*arg == '-') |
257 | return; /* flag */ | |
c6e8c800 JH |
258 | if (!check_filename(prefix, arg)) |
259 | return; | |
ab33a76e VA |
260 | die(_("ambiguous argument '%s': both revision and filename\n" |
261 | "Use '--' to separate paths from revisions, like this:\n" | |
262 | "'git <command> [<revision>...] -- [<file>...]'"), arg); | |
ea92f41f JH |
263 | } |
264 | ||
31e26ebc | 265 | int get_common_dir(struct strbuf *sb, const char *gitdir) |
11f9dd71 MK |
266 | { |
267 | const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT); | |
268 | if (git_env_common_dir) { | |
269 | strbuf_addstr(sb, git_env_common_dir); | |
270 | return 1; | |
271 | } else { | |
272 | return get_common_dir_noenv(sb, gitdir); | |
273 | } | |
274 | } | |
275 | ||
276 | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) | |
4dc4e145 NTND |
277 | { |
278 | struct strbuf data = STRBUF_INIT; | |
279 | struct strbuf path = STRBUF_INIT; | |
31e26ebc | 280 | int ret = 0; |
11f9dd71 | 281 | |
4dc4e145 NTND |
282 | strbuf_addf(&path, "%s/commondir", gitdir); |
283 | if (file_exists(path.buf)) { | |
284 | if (strbuf_read_file(&data, path.buf, 0) <= 0) | |
285 | die_errno(_("failed to read %s"), path.buf); | |
286 | while (data.len && (data.buf[data.len - 1] == '\n' || | |
287 | data.buf[data.len - 1] == '\r')) | |
288 | data.len--; | |
289 | data.buf[data.len] = '\0'; | |
290 | strbuf_reset(&path); | |
291 | if (!is_absolute_path(data.buf)) | |
292 | strbuf_addf(&path, "%s/", gitdir); | |
293 | strbuf_addbuf(&path, &data); | |
33ad9ddd | 294 | strbuf_add_real_path(sb, path.buf); |
31e26ebc | 295 | ret = 1; |
4ac9006f | 296 | } else { |
4dc4e145 | 297 | strbuf_addstr(sb, gitdir); |
4ac9006f BW |
298 | } |
299 | ||
4dc4e145 NTND |
300 | strbuf_release(&data); |
301 | strbuf_release(&path); | |
31e26ebc | 302 | return ret; |
4dc4e145 | 303 | } |
d288a700 | 304 | |
5f5608bc | 305 | /* |
ad1a382f | 306 | * Test if it looks like we're at a git directory. |
5e7bfe25 | 307 | * We want to see: |
5f5608bc | 308 | * |
790296fd | 309 | * - either an objects/ directory _or_ the proper |
5f5608bc | 310 | * GIT_OBJECT_DIRECTORY environment variable |
ad1a382f | 311 | * - a refs/ directory |
8098a178 | 312 | * - either a HEAD symlink or a HEAD file that is formatted as |
c847f537 JH |
313 | * a proper "ref:", or a regular file HEAD that has a properly |
314 | * formatted sha1 object name. | |
5f5608bc | 315 | */ |
b3256eb8 | 316 | int is_git_directory(const char *suspect) |
5f5608bc | 317 | { |
1d186b6f NTND |
318 | struct strbuf path = STRBUF_INIT; |
319 | int ret = 0; | |
320 | size_t len; | |
ad1a382f | 321 | |
4dc4e145 | 322 | /* Check worktree-related signatures */ |
fa4d8c78 JK |
323 | strbuf_addstr(&path, suspect); |
324 | strbuf_complete(&path, '/'); | |
325 | strbuf_addstr(&path, "HEAD"); | |
4dc4e145 NTND |
326 | if (validate_headref(path.buf)) |
327 | goto done; | |
328 | ||
329 | strbuf_reset(&path); | |
330 | get_common_dir(&path, suspect); | |
1d186b6f | 331 | len = path.len; |
4dc4e145 NTND |
332 | |
333 | /* Check non-worktree-related signatures */ | |
ad1a382f SP |
334 | if (getenv(DB_ENVIRONMENT)) { |
335 | if (access(getenv(DB_ENVIRONMENT), X_OK)) | |
1d186b6f | 336 | goto done; |
ad1a382f SP |
337 | } |
338 | else { | |
4dc4e145 | 339 | strbuf_setlen(&path, len); |
1d186b6f NTND |
340 | strbuf_addstr(&path, "/objects"); |
341 | if (access(path.buf, X_OK)) | |
342 | goto done; | |
ad1a382f SP |
343 | } |
344 | ||
1d186b6f NTND |
345 | strbuf_setlen(&path, len); |
346 | strbuf_addstr(&path, "/refs"); | |
347 | if (access(path.buf, X_OK)) | |
348 | goto done; | |
ad1a382f | 349 | |
1d186b6f NTND |
350 | ret = 1; |
351 | done: | |
352 | strbuf_release(&path); | |
353 | return ret; | |
5f5608bc LT |
354 | } |
355 | ||
ffd036b1 JK |
356 | int is_nonbare_repository_dir(struct strbuf *path) |
357 | { | |
358 | int ret = 0; | |
359 | int gitfile_error; | |
360 | size_t orig_path_len = path->len; | |
361 | assert(orig_path_len != 0); | |
362 | strbuf_complete(path, '/'); | |
363 | strbuf_addstr(path, ".git"); | |
364 | if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) | |
365 | ret = 1; | |
366 | if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || | |
367 | gitfile_error == READ_GITFILE_ERR_READ_FAILED) | |
368 | ret = 1; | |
369 | strbuf_setlen(path, orig_path_len); | |
370 | return ret; | |
371 | } | |
372 | ||
68025633 JS |
373 | int is_inside_git_dir(void) |
374 | { | |
e90fdc39 JS |
375 | if (inside_git_dir < 0) |
376 | inside_git_dir = is_inside_dir(get_git_dir()); | |
377 | return inside_git_dir; | |
892c41b9 ML |
378 | } |
379 | ||
892c41b9 ML |
380 | int is_inside_work_tree(void) |
381 | { | |
e90fdc39 JS |
382 | if (inside_work_tree < 0) |
383 | inside_work_tree = is_inside_dir(get_git_work_tree()); | |
384 | return inside_work_tree; | |
892c41b9 ML |
385 | } |
386 | ||
f3fa1838 JH |
387 | void setup_work_tree(void) |
388 | { | |
8500e0de | 389 | const char *work_tree; |
354e6534 JS |
390 | static int initialized = 0; |
391 | ||
392 | if (initialized) | |
393 | return; | |
fada7674 JK |
394 | |
395 | if (work_tree_config_is_bogus) | |
fc045fe7 | 396 | die(_("unable to set up work tree using invalid config")); |
fada7674 | 397 | |
354e6534 | 398 | work_tree = get_git_work_tree(); |
8500e0de | 399 | if (!work_tree || chdir_notify(work_tree)) |
fc045fe7 | 400 | die(_("this operation must be run in a work tree")); |
0ed74813 NTND |
401 | |
402 | /* | |
403 | * Make sure subsequent git processes find correct worktree | |
404 | * if $GIT_WORK_TREE is set relative | |
405 | */ | |
406 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) | |
407 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); | |
408 | ||
354e6534 | 409 | initialized = 1; |
59f0f2f3 MH |
410 | } |
411 | ||
58b284a2 NTND |
412 | static int read_worktree_config(const char *var, const char *value, void *vdata) |
413 | { | |
414 | struct repository_format *data = vdata; | |
415 | ||
416 | if (strcmp(var, "core.bare") == 0) { | |
417 | data->is_bare = git_config_bool(var, value); | |
418 | } else if (strcmp(var, "core.worktree") == 0) { | |
419 | if (!value) | |
420 | return config_error_nonbool(var); | |
13019979 | 421 | free(data->work_tree); |
58b284a2 NTND |
422 | data->work_tree = xstrdup(value); |
423 | } | |
424 | return 0; | |
425 | } | |
426 | ||
2cc7c2c7 | 427 | static int check_repo_format(const char *var, const char *value, void *vdata) |
31e26ebc | 428 | { |
2cc7c2c7 | 429 | struct repository_format *data = vdata; |
00a09d57 JK |
430 | const char *ext; |
431 | ||
31e26ebc | 432 | if (strcmp(var, "core.repositoryformatversion") == 0) |
2cc7c2c7 | 433 | data->version = git_config_int(var, value); |
00a09d57 JK |
434 | else if (skip_prefix(var, "extensions.", &ext)) { |
435 | /* | |
436 | * record any known extensions here; otherwise, | |
437 | * we fall through to recording it as unknown, and | |
438 | * check_repository_format will complain | |
439 | */ | |
440 | if (!strcmp(ext, "noop")) | |
441 | ; | |
067fbd41 | 442 | else if (!strcmp(ext, "preciousobjects")) |
2cc7c2c7 | 443 | data->precious_objects = git_config_bool(var, value); |
75b97fec JT |
444 | else if (!strcmp(ext, "partialclone")) { |
445 | if (!value) | |
446 | return config_error_nonbool(var); | |
447 | data->partial_clone = xstrdup(value); | |
58b284a2 NTND |
448 | } else if (!strcmp(ext, "worktreeconfig")) |
449 | data->worktree_config = git_config_bool(var, value); | |
450 | else | |
2cc7c2c7 | 451 | string_list_append(&data->unknown_extensions, ext); |
00a09d57 | 452 | } |
58b284a2 NTND |
453 | |
454 | return read_worktree_config(var, value, vdata); | |
31e26ebc NTND |
455 | } |
456 | ||
abade65b | 457 | static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok) |
9459aa77 | 458 | { |
7d0fb0da | 459 | struct strbuf sb = STRBUF_INIT; |
2cc7c2c7 | 460 | struct strbuf err = STRBUF_INIT; |
652f18ee | 461 | int has_common; |
00a09d57 | 462 | |
652f18ee | 463 | has_common = get_common_dir(&sb, gitdir); |
e61a509a | 464 | strbuf_addstr(&sb, "/config"); |
abade65b | 465 | read_repository_format(candidate, sb.buf); |
2cc7c2c7 | 466 | strbuf_release(&sb); |
e61a509a | 467 | |
337e51ce | 468 | /* |
2cc7c2c7 JK |
469 | * For historical use of check_repository_format() in git-init, |
470 | * we treat a missing config as a silent "ok", even when nongit_ok | |
471 | * is unset. | |
337e51ce | 472 | */ |
abade65b | 473 | if (candidate->version < 0) |
2cc7c2c7 JK |
474 | return 0; |
475 | ||
abade65b | 476 | if (verify_repository_format(candidate, &err) < 0) { |
2cc7c2c7 JK |
477 | if (nongit_ok) { |
478 | warning("%s", err.buf); | |
479 | strbuf_release(&err); | |
480 | *nongit_ok = -1; | |
481 | return -1; | |
482 | } | |
483 | die("%s", err.buf); | |
484 | } | |
485 | ||
abade65b | 486 | repository_format_precious_objects = candidate->precious_objects; |
60b7a92d | 487 | set_repository_format_partial_clone(candidate->partial_clone); |
58b284a2 | 488 | repository_format_worktree_config = candidate->worktree_config; |
abade65b | 489 | string_list_clear(&candidate->unknown_extensions, 0); |
58b284a2 NTND |
490 | |
491 | if (repository_format_worktree_config) { | |
492 | /* | |
493 | * pick up core.bare and core.worktree from per-worktree | |
494 | * config if present | |
495 | */ | |
496 | strbuf_addf(&sb, "%s/config.worktree", gitdir); | |
497 | git_config_from_file(read_worktree_config, sb.buf, candidate); | |
498 | strbuf_release(&sb); | |
499 | has_common = 0; | |
500 | } | |
501 | ||
652f18ee | 502 | if (!has_common) { |
abade65b | 503 | if (candidate->is_bare != -1) { |
504 | is_bare_repository_cfg = candidate->is_bare; | |
652f18ee JK |
505 | if (is_bare_repository_cfg == 1) |
506 | inside_work_tree = -1; | |
507 | } | |
abade65b | 508 | if (candidate->work_tree) { |
652f18ee | 509 | free(git_work_tree_cfg); |
e8805af1 | 510 | git_work_tree_cfg = xstrdup(candidate->work_tree); |
2cc7c2c7 | 511 | inside_work_tree = -1; |
652f18ee | 512 | } |
2cc7c2c7 JK |
513 | } |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
e8805af1 MÅ |
518 | static void init_repository_format(struct repository_format *format) |
519 | { | |
520 | const struct repository_format fresh = REPOSITORY_FORMAT_INIT; | |
521 | ||
522 | memcpy(format, &fresh, sizeof(fresh)); | |
523 | } | |
524 | ||
652f18ee | 525 | int read_repository_format(struct repository_format *format, const char *path) |
2cc7c2c7 | 526 | { |
e8805af1 | 527 | clear_repository_format(format); |
652f18ee | 528 | git_config_from_file(check_repo_format, path, format); |
e8805af1 MÅ |
529 | if (format->version == -1) |
530 | clear_repository_format(format); | |
2cc7c2c7 JK |
531 | return format->version; |
532 | } | |
533 | ||
e8805af1 MÅ |
534 | void clear_repository_format(struct repository_format *format) |
535 | { | |
536 | string_list_clear(&format->unknown_extensions, 0); | |
537 | free(format->work_tree); | |
538 | free(format->partial_clone); | |
539 | init_repository_format(format); | |
540 | } | |
541 | ||
2cc7c2c7 JK |
542 | int verify_repository_format(const struct repository_format *format, |
543 | struct strbuf *err) | |
544 | { | |
545 | if (GIT_REPO_VERSION_READ < format->version) { | |
274db840 | 546 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
2cc7c2c7 JK |
547 | GIT_REPO_VERSION_READ, format->version); |
548 | return -1; | |
549 | } | |
550 | ||
551 | if (format->version >= 1 && format->unknown_extensions.nr) { | |
00a09d57 JK |
552 | int i; |
553 | ||
274db840 | 554 | strbuf_addstr(err, _("unknown repository extensions found:")); |
00a09d57 | 555 | |
2cc7c2c7 JK |
556 | for (i = 0; i < format->unknown_extensions.nr; i++) |
557 | strbuf_addf(err, "\n\t%s", | |
558 | format->unknown_extensions.items[i].string); | |
559 | return -1; | |
00a09d57 JK |
560 | } |
561 | ||
2cc7c2c7 | 562 | return 0; |
9459aa77 NTND |
563 | } |
564 | ||
5f29433f SB |
565 | void read_gitfile_error_die(int error_code, const char *path, const char *dir) |
566 | { | |
567 | switch (error_code) { | |
568 | case READ_GITFILE_ERR_STAT_FAILED: | |
569 | case READ_GITFILE_ERR_NOT_A_FILE: | |
570 | /* non-fatal; follow return path */ | |
571 | break; | |
572 | case READ_GITFILE_ERR_OPEN_FAILED: | |
fc045fe7 | 573 | die_errno(_("error opening '%s'"), path); |
5f29433f | 574 | case READ_GITFILE_ERR_TOO_LARGE: |
fc045fe7 | 575 | die(_("too large to be a .git file: '%s'"), path); |
5f29433f | 576 | case READ_GITFILE_ERR_READ_FAILED: |
fc045fe7 | 577 | die(_("error reading %s"), path); |
5f29433f | 578 | case READ_GITFILE_ERR_INVALID_FORMAT: |
fc045fe7 | 579 | die(_("invalid gitfile format: %s"), path); |
5f29433f | 580 | case READ_GITFILE_ERR_NO_PATH: |
fc045fe7 | 581 | die(_("no path in gitfile: %s"), path); |
5f29433f | 582 | case READ_GITFILE_ERR_NOT_A_REPO: |
fc045fe7 | 583 | die(_("not a git repository: %s"), dir); |
5f29433f | 584 | default: |
033abf97 | 585 | BUG("unknown error code"); |
5f29433f SB |
586 | } |
587 | } | |
588 | ||
b44ebb19 LH |
589 | /* |
590 | * Try to read the location of the git directory from the .git file, | |
ea1d8756 HWN |
591 | * return path to git directory if found. The return value comes from |
592 | * a shared buffer. | |
a93bedad EE |
593 | * |
594 | * On failure, if return_error_code is not NULL, return_error_code | |
595 | * will be set to an error code and NULL will be returned. If | |
596 | * return_error_code is NULL the function will die instead (for most | |
597 | * cases). | |
b44ebb19 | 598 | */ |
a93bedad | 599 | const char *read_gitfile_gently(const char *path, int *return_error_code) |
b44ebb19 | 600 | { |
921bdd96 | 601 | const int max_file_size = 1 << 20; /* 1MB */ |
a93bedad EE |
602 | int error_code = 0; |
603 | char *buf = NULL; | |
604 | char *dir = NULL; | |
40c813e0 | 605 | const char *slash; |
b44ebb19 LH |
606 | struct stat st; |
607 | int fd; | |
b1905aea | 608 | ssize_t len; |
b44ebb19 | 609 | |
a93bedad | 610 | if (stat(path, &st)) { |
5c4003ca | 611 | /* NEEDSWORK: discern between ENOENT vs other errors */ |
a93bedad EE |
612 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
613 | goto cleanup_return; | |
614 | } | |
615 | if (!S_ISREG(st.st_mode)) { | |
616 | error_code = READ_GITFILE_ERR_NOT_A_FILE; | |
617 | goto cleanup_return; | |
618 | } | |
921bdd96 EE |
619 | if (st.st_size > max_file_size) { |
620 | error_code = READ_GITFILE_ERR_TOO_LARGE; | |
621 | goto cleanup_return; | |
622 | } | |
b44ebb19 | 623 | fd = open(path, O_RDONLY); |
a93bedad EE |
624 | if (fd < 0) { |
625 | error_code = READ_GITFILE_ERR_OPEN_FAILED; | |
626 | goto cleanup_return; | |
627 | } | |
3733e694 | 628 | buf = xmallocz(st.st_size); |
b44ebb19 LH |
629 | len = read_in_full(fd, buf, st.st_size); |
630 | close(fd); | |
a93bedad EE |
631 | if (len != st.st_size) { |
632 | error_code = READ_GITFILE_ERR_READ_FAILED; | |
633 | goto cleanup_return; | |
634 | } | |
a93bedad EE |
635 | if (!starts_with(buf, "gitdir: ")) { |
636 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; | |
637 | goto cleanup_return; | |
638 | } | |
b44ebb19 LH |
639 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
640 | len--; | |
a93bedad EE |
641 | if (len < 9) { |
642 | error_code = READ_GITFILE_ERR_NO_PATH; | |
643 | goto cleanup_return; | |
644 | } | |
b44ebb19 | 645 | buf[len] = '\0'; |
40c813e0 BK |
646 | dir = buf + 8; |
647 | ||
648 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
649 | size_t pathlen = slash+1 - path; | |
75faa45a JK |
650 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
651 | (int)(len - 8), buf + 8); | |
40c813e0 BK |
652 | free(buf); |
653 | buf = dir; | |
654 | } | |
a93bedad EE |
655 | if (!is_git_directory(dir)) { |
656 | error_code = READ_GITFILE_ERR_NOT_A_REPO; | |
657 | goto cleanup_return; | |
658 | } | |
e2a57aac | 659 | path = real_path(dir); |
40c813e0 | 660 | |
a93bedad | 661 | cleanup_return: |
a93bedad EE |
662 | if (return_error_code) |
663 | *return_error_code = error_code; | |
5f29433f SB |
664 | else if (error_code) |
665 | read_gitfile_error_die(error_code, path, dir); | |
a93bedad | 666 | |
b44ebb19 | 667 | free(buf); |
38ae8784 | 668 | return error_code ? NULL : path; |
b44ebb19 LH |
669 | } |
670 | ||
e4e30347 | 671 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
7333ed17 | 672 | struct strbuf *cwd, |
abade65b | 673 | struct repository_format *repo_fmt, |
b3f66fd3 | 674 | int *nongit_ok) |
e4e30347 | 675 | { |
b3f66fd3 NTND |
676 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
677 | const char *worktree; | |
678 | char *gitfile; | |
9b125da4 | 679 | int offset; |
e4e30347 JN |
680 | |
681 | if (PATH_MAX - 40 < strlen(gitdirenv)) | |
fc045fe7 | 682 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
b3f66fd3 | 683 | |
13d6ec91 | 684 | gitfile = (char*)read_gitfile(gitdirenv); |
b3f66fd3 NTND |
685 | if (gitfile) { |
686 | gitfile = xstrdup(gitfile); | |
687 | gitdirenv = gitfile; | |
688 | } | |
689 | ||
e4e30347 JN |
690 | if (!is_git_directory(gitdirenv)) { |
691 | if (nongit_ok) { | |
692 | *nongit_ok = 1; | |
b3f66fd3 | 693 | free(gitfile); |
e4e30347 JN |
694 | return NULL; |
695 | } | |
fc045fe7 | 696 | die(_("not a git repository: '%s'"), gitdirenv); |
e4e30347 | 697 | } |
b3f66fd3 | 698 | |
abade65b | 699 | if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) { |
b3f66fd3 NTND |
700 | free(gitfile); |
701 | return NULL; | |
e4e30347 | 702 | } |
b3f66fd3 NTND |
703 | |
704 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ | |
705 | if (work_tree_env) | |
706 | set_git_work_tree(work_tree_env); | |
707 | else if (is_bare_repository_cfg > 0) { | |
fada7674 JK |
708 | if (git_work_tree_cfg) { |
709 | /* #22.2, #30 */ | |
710 | warning("core.bare and core.worktree do not make sense"); | |
711 | work_tree_config_is_bogus = 1; | |
712 | } | |
b3f66fd3 NTND |
713 | |
714 | /* #18, #26 */ | |
715 | set_git_dir(gitdirenv); | |
716 | free(gitfile); | |
e4e30347 | 717 | return NULL; |
b3f66fd3 NTND |
718 | } |
719 | else if (git_work_tree_cfg) { /* #6, #14 */ | |
720 | if (is_absolute_path(git_work_tree_cfg)) | |
721 | set_git_work_tree(git_work_tree_cfg); | |
722 | else { | |
56b9f6e7 | 723 | char *core_worktree; |
b3f66fd3 | 724 | if (chdir(gitdirenv)) |
fc045fe7 | 725 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
b3f66fd3 | 726 | if (chdir(git_work_tree_cfg)) |
fc045fe7 | 727 | die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg); |
56b9f6e7 | 728 | core_worktree = xgetcwd(); |
7333ed17 | 729 | if (chdir(cwd->buf)) |
fc045fe7 | 730 | die_errno(_("cannot come back to cwd")); |
b3f66fd3 | 731 | set_git_work_tree(core_worktree); |
56b9f6e7 | 732 | free(core_worktree); |
b3f66fd3 NTND |
733 | } |
734 | } | |
2cd83d10 JK |
735 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
736 | /* #16d */ | |
737 | set_git_dir(gitdirenv); | |
738 | free(gitfile); | |
739 | return NULL; | |
740 | } | |
b3f66fd3 NTND |
741 | else /* #2, #10 */ |
742 | set_git_work_tree("."); | |
743 | ||
744 | /* set_git_work_tree() must have been called by now */ | |
745 | worktree = get_git_work_tree(); | |
746 | ||
747 | /* both get_git_work_tree() and cwd are already normalized */ | |
7333ed17 | 748 | if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ |
b3f66fd3 NTND |
749 | set_git_dir(gitdirenv); |
750 | free(gitfile); | |
e4e30347 | 751 | return NULL; |
b3f66fd3 | 752 | } |
e4e30347 | 753 | |
7333ed17 | 754 | offset = dir_inside_of(cwd->buf, worktree); |
9b125da4 | 755 | if (offset >= 0) { /* cwd inside worktree? */ |
e2a57aac | 756 | set_git_dir(real_path(gitdirenv)); |
b3f66fd3 | 757 | if (chdir(worktree)) |
fc045fe7 | 758 | die_errno(_("cannot chdir to '%s'"), worktree); |
7333ed17 | 759 | strbuf_addch(cwd, '/'); |
b3f66fd3 | 760 | free(gitfile); |
7333ed17 | 761 | return cwd->buf + offset; |
93a00542 | 762 | } |
b3f66fd3 NTND |
763 | |
764 | /* cwd outside worktree */ | |
765 | set_git_dir(gitdirenv); | |
766 | free(gitfile); | |
767 | return NULL; | |
93a00542 JN |
768 | } |
769 | ||
9951d3b3 | 770 | static const char *setup_discovered_git_dir(const char *gitdir, |
7333ed17 | 771 | struct strbuf *cwd, int offset, |
abade65b | 772 | struct repository_format *repo_fmt, |
9951d3b3 | 773 | int *nongit_ok) |
98937bef | 774 | { |
abade65b | 775 | if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok)) |
9951d3b3 | 776 | return NULL; |
98937bef | 777 | |
4868b2ea JN |
778 | /* --work-tree is set without --git-dir; use discovered one */ |
779 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
2d4dcf21 JS |
780 | char *to_free = NULL; |
781 | const char *ret; | |
782 | ||
7333ed17 | 783 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
2d4dcf21 | 784 | gitdir = to_free = real_pathdup(gitdir, 1); |
7333ed17 | 785 | if (chdir(cwd->buf)) |
fc045fe7 | 786 | die_errno(_("cannot come back to cwd")); |
abade65b | 787 | ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
2d4dcf21 JS |
788 | free(to_free); |
789 | return ret; | |
4868b2ea JN |
790 | } |
791 | ||
9951d3b3 NTND |
792 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
793 | if (is_bare_repository_cfg > 0) { | |
7333ed17 RS |
794 | set_git_dir(offset == cwd->len ? gitdir : real_path(gitdir)); |
795 | if (chdir(cwd->buf)) | |
fc045fe7 | 796 | die_errno(_("cannot come back to cwd")); |
98937bef | 797 | return NULL; |
9951d3b3 | 798 | } |
98937bef | 799 | |
9951d3b3 NTND |
800 | /* #0, #1, #5, #8, #9, #12, #13 */ |
801 | set_git_work_tree("."); | |
802 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) | |
803 | set_git_dir(gitdir); | |
98937bef | 804 | inside_git_dir = 0; |
9951d3b3 | 805 | inside_work_tree = 1; |
5cf7b3b1 | 806 | if (offset >= cwd->len) |
98937bef NTND |
807 | return NULL; |
808 | ||
df380d58 JS |
809 | /* Make "offset" point past the '/' (already the case for root dirs) */ |
810 | if (offset != offset_1st_component(cwd->buf)) | |
811 | offset++; | |
812 | /* Add a '/' at the end */ | |
7333ed17 RS |
813 | strbuf_addch(cwd, '/'); |
814 | return cwd->buf + offset; | |
98937bef NTND |
815 | } |
816 | ||
1cd8031b | 817 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
7333ed17 | 818 | static const char *setup_bare_git_dir(struct strbuf *cwd, int offset, |
abade65b | 819 | struct repository_format *repo_fmt, |
7333ed17 | 820 | int *nongit_ok) |
68698da5 JN |
821 | { |
822 | int root_len; | |
823 | ||
abade65b | 824 | if (check_repository_format_gently(".", repo_fmt, nongit_ok)) |
1cd8031b NTND |
825 | return NULL; |
826 | ||
2cd83d10 JK |
827 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
828 | ||
4868b2ea JN |
829 | /* --work-tree is set without --git-dir; use discovered one */ |
830 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
da6f8475 | 831 | static const char *gitdir; |
4868b2ea | 832 | |
7333ed17 RS |
833 | gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset); |
834 | if (chdir(cwd->buf)) | |
fc045fe7 | 835 | die_errno(_("cannot come back to cwd")); |
abade65b | 836 | return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
4868b2ea JN |
837 | } |
838 | ||
68698da5 | 839 | inside_git_dir = 1; |
1cd8031b | 840 | inside_work_tree = 0; |
7333ed17 RS |
841 | if (offset != cwd->len) { |
842 | if (chdir(cwd->buf)) | |
fc045fe7 | 843 | die_errno(_("cannot come back to cwd")); |
7333ed17 RS |
844 | root_len = offset_1st_component(cwd->buf); |
845 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); | |
846 | set_git_dir(cwd->buf); | |
337e51ce | 847 | } |
1cd8031b | 848 | else |
68698da5 | 849 | set_git_dir("."); |
68698da5 JN |
850 | return NULL; |
851 | } | |
852 | ||
2565b43b | 853 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
60c98d1e JN |
854 | { |
855 | struct stat buf; | |
2565b43b | 856 | if (stat(path, &buf)) { |
fc045fe7 | 857 | die_errno(_("failed to stat '%*s%s%s'"), |
2565b43b | 858 | prefix_len, |
60c98d1e JN |
859 | prefix ? prefix : "", |
860 | prefix ? "/" : "", path); | |
2565b43b | 861 | } |
60c98d1e JN |
862 | return buf.st_dev; |
863 | } | |
864 | ||
9e2326c7 | 865 | /* |
1b77d83c MH |
866 | * A "string_list_each_func_t" function that canonicalizes an entry |
867 | * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or | |
7ec30aaa MH |
868 | * discards it if unusable. The presence of an empty entry in |
869 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all | |
870 | * subsequent entries. | |
9e2326c7 | 871 | */ |
1b77d83c | 872 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
7ec30aaa | 873 | void *cb_data) |
9e2326c7 | 874 | { |
7ec30aaa | 875 | int *empty_entry_found = cb_data; |
1b77d83c | 876 | char *ceil = item->string; |
9e2326c7 | 877 | |
7ec30aaa MH |
878 | if (!*ceil) { |
879 | *empty_entry_found = 1; | |
9e2326c7 | 880 | return 0; |
7ec30aaa | 881 | } else if (!is_absolute_path(ceil)) { |
9e2326c7 | 882 | return 0; |
7ec30aaa MH |
883 | } else if (*empty_entry_found) { |
884 | /* Keep entry but do not canonicalize it */ | |
885 | return 1; | |
886 | } else { | |
ce83eadd | 887 | char *real_path = real_pathdup(ceil, 0); |
4ac9006f | 888 | if (!real_path) { |
7ec30aaa | 889 | return 0; |
4ac9006f | 890 | } |
7ec30aaa | 891 | free(item->string); |
4ac9006f | 892 | item->string = real_path; |
7ec30aaa MH |
893 | return 1; |
894 | } | |
9e2326c7 MH |
895 | } |
896 | ||
ce9b8aab JS |
897 | enum discovery_result { |
898 | GIT_DIR_NONE = 0, | |
899 | GIT_DIR_EXPLICIT, | |
900 | GIT_DIR_DISCOVERED, | |
901 | GIT_DIR_BARE, | |
902 | /* these are errors */ | |
903 | GIT_DIR_HIT_CEILING = -1, | |
01017dce JS |
904 | GIT_DIR_HIT_MOUNT_POINT = -2, |
905 | GIT_DIR_INVALID_GITFILE = -3 | |
ce9b8aab JS |
906 | }; |
907 | ||
e90fdc39 JS |
908 | /* |
909 | * We cannot decide in this function whether we are in the work tree or | |
910 | * not, since the config can only be read _after_ this function was called. | |
ce9b8aab JS |
911 | * |
912 | * Also, we avoid changing any global state (such as the current working | |
913 | * directory) to allow early callers. | |
914 | * | |
915 | * The directory where the search should start needs to be passed in via the | |
916 | * `dir` parameter; upon return, the `dir` buffer will contain the path of | |
917 | * the directory where the search ended, and `gitdir` will contain the path of | |
918 | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it | |
919 | * is relative to `dir` (i.e. *not* necessarily the cwd). | |
e90fdc39 | 920 | */ |
ce9b8aab | 921 | static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, |
01017dce JS |
922 | struct strbuf *gitdir, |
923 | int die_on_error) | |
d288a700 | 924 | { |
0454dd93 | 925 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
31171d9e | 926 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
ce9b8aab | 927 | const char *gitdirenv; |
d17f2124 | 928 | int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); |
c7d1d1b1 RH |
929 | dev_t current_device = 0; |
930 | int one_filesystem = 1; | |
d288a700 | 931 | |
e90fdc39 JS |
932 | /* |
933 | * If GIT_DIR is set explicitly, we're not going | |
934 | * to do any discovery, but we still do repository | |
935 | * validation. | |
936 | */ | |
ad1a382f | 937 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
ce9b8aab JS |
938 | if (gitdirenv) { |
939 | strbuf_addstr(gitdir, gitdirenv); | |
940 | return GIT_DIR_EXPLICIT; | |
941 | } | |
d288a700 | 942 | |
31171d9e | 943 | if (env_ceiling_dirs) { |
7ec30aaa MH |
944 | int empty_entry_found = 0; |
945 | ||
31171d9e | 946 | string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); |
1b77d83c | 947 | filter_string_list(&ceiling_dirs, 0, |
7ec30aaa | 948 | canonicalize_ceiling_entry, &empty_entry_found); |
ce9b8aab | 949 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
31171d9e MH |
950 | string_list_clear(&ceiling_dirs, 0); |
951 | } | |
952 | ||
ce9b8aab JS |
953 | if (ceil_offset < 0) |
954 | ceil_offset = min_offset - 2; | |
d288a700 | 955 | |
e2683d51 JS |
956 | if (min_offset && min_offset == dir->len && |
957 | !is_dir_sep(dir->buf[min_offset - 1])) { | |
958 | strbuf_addch(dir, '/'); | |
959 | min_offset++; | |
960 | } | |
961 | ||
892c41b9 | 962 | /* |
ce9b8aab | 963 | * Test in the following order (relative to the dir): |
b44ebb19 | 964 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
965 | * - .git/ |
966 | * - ./ (bare) | |
b44ebb19 | 967 | * - ../.git |
e90fdc39 JS |
968 | * - ../.git/ |
969 | * - ../ (bare) | |
176b2d32 | 970 | * - ../../.git |
e90fdc39 | 971 | * etc. |
892c41b9 | 972 | */ |
cf87463e | 973 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
60c98d1e | 974 | if (one_filesystem) |
ce9b8aab | 975 | current_device = get_device_or_die(dir->buf, NULL, 0); |
e90fdc39 | 976 | for (;;) { |
01017dce | 977 | int offset = dir->len, error_code = 0; |
ce9b8aab JS |
978 | |
979 | if (offset > min_offset) | |
980 | strbuf_addch(dir, '/'); | |
981 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); | |
01017dce JS |
982 | gitdirenv = read_gitfile_gently(dir->buf, die_on_error ? |
983 | NULL : &error_code); | |
984 | if (!gitdirenv) { | |
985 | if (die_on_error || | |
986 | error_code == READ_GITFILE_ERR_NOT_A_FILE) { | |
5c4003ca | 987 | /* NEEDSWORK: fail if .git is not file nor dir */ |
01017dce JS |
988 | if (is_git_directory(dir->buf)) |
989 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; | |
990 | } else if (error_code != READ_GITFILE_ERR_STAT_FAILED) | |
991 | return GIT_DIR_INVALID_GITFILE; | |
9951d3b3 | 992 | } |
ce9b8aab | 993 | strbuf_setlen(dir, offset); |
9951d3b3 | 994 | if (gitdirenv) { |
ce9b8aab JS |
995 | strbuf_addstr(gitdir, gitdirenv); |
996 | return GIT_DIR_DISCOVERED; | |
9951d3b3 | 997 | } |
9951d3b3 | 998 | |
ce9b8aab JS |
999 | if (is_git_directory(dir->buf)) { |
1000 | strbuf_addstr(gitdir, "."); | |
1001 | return GIT_DIR_BARE; | |
502ffe34 | 1002 | } |
9951d3b3 | 1003 | |
ce9b8aab JS |
1004 | if (offset <= min_offset) |
1005 | return GIT_DIR_HIT_CEILING; | |
1cd8031b | 1006 | |
ce9b8aab | 1007 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
6c1e6544 | 1008 | ; /* continue */ |
ce9b8aab JS |
1009 | if (offset <= ceil_offset) |
1010 | return GIT_DIR_HIT_CEILING; | |
1011 | ||
1012 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); | |
1013 | if (one_filesystem && | |
1014 | current_device != get_device_or_die(dir->buf, NULL, offset)) | |
1015 | return GIT_DIR_HIT_MOUNT_POINT; | |
892c41b9 | 1016 | } |
d288a700 | 1017 | } |
5e7bfe25 | 1018 | |
d3fb71b3 BW |
1019 | int discover_git_directory(struct strbuf *commondir, |
1020 | struct strbuf *gitdir) | |
16ac8b8d JS |
1021 | { |
1022 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; | |
1023 | size_t gitdir_offset = gitdir->len, cwd_len; | |
d3fb71b3 | 1024 | size_t commondir_offset = commondir->len; |
e8805af1 | 1025 | struct repository_format candidate = REPOSITORY_FORMAT_INIT; |
16ac8b8d JS |
1026 | |
1027 | if (strbuf_getcwd(&dir)) | |
d3fb71b3 | 1028 | return -1; |
16ac8b8d JS |
1029 | |
1030 | cwd_len = dir.len; | |
01017dce | 1031 | if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) { |
16ac8b8d | 1032 | strbuf_release(&dir); |
d3fb71b3 | 1033 | return -1; |
16ac8b8d JS |
1034 | } |
1035 | ||
1036 | /* | |
1037 | * The returned gitdir is relative to dir, and if dir does not reflect | |
1038 | * the current working directory, we simply make the gitdir absolute. | |
1039 | */ | |
1040 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { | |
1041 | /* Avoid a trailing "/." */ | |
1042 | if (!strcmp(".", gitdir->buf + gitdir_offset)) | |
1043 | strbuf_setlen(gitdir, gitdir_offset); | |
1044 | else | |
1045 | strbuf_addch(&dir, '/'); | |
1046 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); | |
1047 | } | |
1048 | ||
d3fb71b3 BW |
1049 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
1050 | ||
16ac8b8d | 1051 | strbuf_reset(&dir); |
d3fb71b3 | 1052 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
16ac8b8d JS |
1053 | read_repository_format(&candidate, dir.buf); |
1054 | strbuf_release(&dir); | |
1055 | ||
1056 | if (verify_repository_format(&candidate, &err) < 0) { | |
1057 | warning("ignoring git dir '%s': %s", | |
1058 | gitdir->buf + gitdir_offset, err.buf); | |
1059 | strbuf_release(&err); | |
d3fb71b3 | 1060 | strbuf_setlen(commondir, commondir_offset); |
69743f9b | 1061 | strbuf_setlen(gitdir, gitdir_offset); |
e8805af1 | 1062 | clear_repository_format(&candidate); |
d3fb71b3 | 1063 | return -1; |
16ac8b8d JS |
1064 | } |
1065 | ||
e8805af1 | 1066 | clear_repository_format(&candidate); |
d3fb71b3 | 1067 | return 0; |
16ac8b8d JS |
1068 | } |
1069 | ||
a60645f9 NTND |
1070 | const char *setup_git_directory_gently(int *nongit_ok) |
1071 | { | |
ce9b8aab JS |
1072 | static struct strbuf cwd = STRBUF_INIT; |
1073 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT; | |
07098b81 | 1074 | const char *prefix = NULL; |
e8805af1 | 1075 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
a60645f9 | 1076 | |
ce9b8aab JS |
1077 | /* |
1078 | * We may have read an incomplete configuration before | |
1079 | * setting-up the git directory. If so, clear the cache so | |
1080 | * that the next queries to the configuration reload complete | |
1081 | * configuration (including the per-repo config file that we | |
1082 | * ignored previously). | |
1083 | */ | |
1084 | git_config_clear(); | |
1085 | ||
1086 | /* | |
1087 | * Let's assume that we are in a git repository. | |
1088 | * If it turns out later that we are somewhere else, the value will be | |
1089 | * updated accordingly. | |
1090 | */ | |
1091 | if (nongit_ok) | |
1092 | *nongit_ok = 0; | |
1093 | ||
1094 | if (strbuf_getcwd(&cwd)) | |
1095 | die_errno(_("Unable to read current working directory")); | |
1096 | strbuf_addbuf(&dir, &cwd); | |
1097 | ||
01017dce | 1098 | switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) { |
ce9b8aab | 1099 | case GIT_DIR_EXPLICIT: |
abade65b | 1100 | prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1101 | break; |
1102 | case GIT_DIR_DISCOVERED: | |
1103 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1104 | die(_("cannot change to '%s'"), dir.buf); |
ce9b8aab | 1105 | prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len, |
abade65b | 1106 | &repo_fmt, nongit_ok); |
ce9b8aab JS |
1107 | break; |
1108 | case GIT_DIR_BARE: | |
1109 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1110 | die(_("cannot change to '%s'"), dir.buf); |
abade65b | 1111 | prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1112 | break; |
1113 | case GIT_DIR_HIT_CEILING: | |
07098b81 ED |
1114 | if (!nongit_ok) |
1115 | die(_("not a git repository (or any of the parent directories): %s"), | |
1116 | DEFAULT_GIT_DIR_ENVIRONMENT); | |
1117 | *nongit_ok = 1; | |
ce9b8aab JS |
1118 | break; |
1119 | case GIT_DIR_HIT_MOUNT_POINT: | |
07098b81 ED |
1120 | if (!nongit_ok) |
1121 | die(_("not a git repository (or any parent up to mount point %s)\n" | |
1122 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), | |
1123 | dir.buf); | |
1124 | *nongit_ok = 1; | |
1125 | break; | |
1126 | case GIT_DIR_NONE: | |
1127 | /* | |
1128 | * As a safeguard against setup_git_directory_gently_1 returning | |
1129 | * this value, fallthrough to BUG. Otherwise it is possible to | |
1130 | * set startup_info->have_repository to 1 when we did nothing to | |
1131 | * find a repository. | |
1132 | */ | |
ce9b8aab | 1133 | default: |
033abf97 | 1134 | BUG("unhandled setup_git_directory_1() result"); |
ce9b8aab JS |
1135 | } |
1136 | ||
07098b81 ED |
1137 | /* |
1138 | * At this point, nongit_ok is stable. If it is non-NULL and points | |
1139 | * to a non-zero value, then this means that we haven't found a | |
1140 | * repository and that the caller expects startup_info to reflect | |
1141 | * this. | |
1142 | * | |
1143 | * Regardless of the state of nongit_ok, startup_info->prefix and | |
1144 | * the GIT_PREFIX environment variable must always match. For details | |
1145 | * see Documentation/config/alias.txt. | |
1146 | */ | |
1147 | if (nongit_ok && *nongit_ok) { | |
1148 | startup_info->have_repository = 0; | |
1149 | startup_info->prefix = NULL; | |
a6f7f9a3 | 1150 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); |
07098b81 ED |
1151 | } else { |
1152 | startup_info->have_repository = 1; | |
1153 | startup_info->prefix = prefix; | |
1154 | if (prefix) | |
1155 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); | |
1156 | else | |
1157 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); | |
1158 | } | |
46c3cd44 | 1159 | |
73f192c9 BW |
1160 | /* |
1161 | * Not all paths through the setup code will call 'set_git_dir()' (which | |
1162 | * directly sets up the environment) so in order to guarantee that the | |
1163 | * environment is in a consistent state after setup, explicitly setup | |
1164 | * the environment if we have a repository. | |
1165 | * | |
1166 | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some | |
1167 | * code paths so we also need to explicitly setup the environment if | |
1168 | * the user has set GIT_DIR. It may be beneficial to disallow bogus | |
1169 | * GIT_DIR values at some point in the future. | |
1170 | */ | |
07098b81 ED |
1171 | if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */ |
1172 | startup_info->have_repository || | |
1173 | /* GIT_DIR_EXPLICIT */ | |
1174 | getenv(GIT_DIR_ENVIRONMENT)) { | |
c14c234f BW |
1175 | if (!the_repository->gitdir) { |
1176 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); | |
1177 | if (!gitdir) | |
1178 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; | |
357a03eb | 1179 | setup_git_env(gitdir); |
c14c234f | 1180 | } |
78a67668 | 1181 | if (startup_info->have_repository) |
1182 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); | |
c14c234f | 1183 | } |
73f192c9 | 1184 | |
ce9b8aab JS |
1185 | strbuf_release(&dir); |
1186 | strbuf_release(&gitdir); | |
e8805af1 | 1187 | clear_repository_format(&repo_fmt); |
ce9b8aab | 1188 | |
a60645f9 NTND |
1189 | return prefix; |
1190 | } | |
1191 | ||
94df2506 JH |
1192 | int git_config_perm(const char *var, const char *value) |
1193 | { | |
06cbe855 HO |
1194 | int i; |
1195 | char *endptr; | |
1196 | ||
1197 | if (value == NULL) | |
1198 | return PERM_GROUP; | |
1199 | ||
1200 | if (!strcmp(value, "umask")) | |
1201 | return PERM_UMASK; | |
1202 | if (!strcmp(value, "group")) | |
1203 | return PERM_GROUP; | |
1204 | if (!strcmp(value, "all") || | |
1205 | !strcmp(value, "world") || | |
1206 | !strcmp(value, "everybody")) | |
1207 | return PERM_EVERYBODY; | |
1208 | ||
1209 | /* Parse octal numbers */ | |
1210 | i = strtol(value, &endptr, 8); | |
1211 | ||
1212 | /* If not an octal number, maybe true/false? */ | |
1213 | if (*endptr != 0) | |
1214 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
1215 | ||
1216 | /* | |
1217 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 1218 | * a chmod value to restrict to. |
06cbe855 HO |
1219 | */ |
1220 | switch (i) { | |
1221 | case PERM_UMASK: /* 0 */ | |
1222 | return PERM_UMASK; | |
1223 | case OLD_PERM_GROUP: /* 1 */ | |
1224 | return PERM_GROUP; | |
1225 | case OLD_PERM_EVERYBODY: /* 2 */ | |
1226 | return PERM_EVERYBODY; | |
94df2506 | 1227 | } |
06cbe855 HO |
1228 | |
1229 | /* A filemode value was given: 0xxx */ | |
1230 | ||
1231 | if ((i & 0600) != 0600) | |
fc045fe7 | 1232 | die(_("problem with core.sharedRepository filemode value " |
06cbe855 | 1233 | "(0%.3o).\nThe owner of files must always have " |
2ff30e67 | 1234 | "read and write permissions."), i); |
06cbe855 HO |
1235 | |
1236 | /* | |
1237 | * Mask filemode value. Others can not get write permission. | |
1238 | * x flags for directories are handled separately. | |
1239 | */ | |
5a688fe4 | 1240 | return -(i & 0666); |
94df2506 JH |
1241 | } |
1242 | ||
4b0d1eeb | 1243 | void check_repository_format(void) |
ab9cb76f | 1244 | { |
e8805af1 | 1245 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
abade65b | 1246 | check_repository_format_gently(get_git_dir(), &repo_fmt, NULL); |
f1c126bd | 1247 | startup_info->have_repository = 1; |
e8805af1 | 1248 | clear_repository_format(&repo_fmt); |
ab9cb76f JH |
1249 | } |
1250 | ||
e1e5ec86 CB |
1251 | /* |
1252 | * Returns the "prefix", a path to the current working directory | |
1253 | * relative to the work tree root, or NULL, if the current working | |
1254 | * directory is not a strict subdirectory of the work tree root. The | |
1255 | * prefix always ends with a '/' character. | |
1256 | */ | |
5e7bfe25 JH |
1257 | const char *setup_git_directory(void) |
1258 | { | |
b3f66fd3 | 1259 | return setup_git_directory_gently(NULL); |
5e7bfe25 | 1260 | } |
abc06822 | 1261 | |
40d96325 | 1262 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
abc06822 FG |
1263 | { |
1264 | if (is_git_directory(suspect)) | |
1265 | return suspect; | |
40d96325 | 1266 | return read_gitfile_gently(suspect, return_error_code); |
abc06822 | 1267 | } |
1d999ddd TR |
1268 | |
1269 | /* if any standard file descriptor is missing open it to /dev/null */ | |
1270 | void sanitize_stdfds(void) | |
1271 | { | |
1272 | int fd = open("/dev/null", O_RDWR, 0); | |
1273 | while (fd != -1 && fd < 2) | |
1274 | fd = dup(fd); | |
1275 | if (fd == -1) | |
fc045fe7 | 1276 | die_errno(_("open /dev/null or dup failed")); |
1d999ddd TR |
1277 | if (fd > 2) |
1278 | close(fd); | |
1279 | } | |
de0957ce NTND |
1280 | |
1281 | int daemonize(void) | |
1282 | { | |
1283 | #ifdef NO_POSIX_GOODIES | |
1284 | errno = ENOSYS; | |
1285 | return -1; | |
1286 | #else | |
1287 | switch (fork()) { | |
1288 | case 0: | |
1289 | break; | |
1290 | case -1: | |
fc045fe7 | 1291 | die_errno(_("fork failed")); |
de0957ce NTND |
1292 | default: |
1293 | exit(0); | |
1294 | } | |
1295 | if (setsid() == -1) | |
fc045fe7 | 1296 | die_errno(_("setsid failed")); |
de0957ce NTND |
1297 | close(0); |
1298 | close(1); | |
1299 | close(2); | |
1300 | sanitize_stdfds(); | |
1301 | return 0; | |
1302 | #endif | |
1303 | } |