]>
Commit | Line | Data |
---|---|---|
e93fc5d7 | 1 | #include "git-compat-util.h" |
0b027f6c | 2 | #include "abspath.h" |
e8cf8ef5 | 3 | #include "copy.h" |
32a8f510 | 4 | #include "environment.h" |
e8cf8ef5 | 5 | #include "exec-cmd.h" |
f394e093 | 6 | #include "gettext.h" |
dabab1d6 | 7 | #include "object-name.h" |
e8cf8ef5 | 8 | #include "refs.h" |
c14c234f | 9 | #include "repository.h" |
b2141fc1 | 10 | #include "config.h" |
e90fdc39 | 11 | #include "dir.h" |
e38da487 | 12 | #include "setup.h" |
31171d9e | 13 | #include "string-list.h" |
8500e0de | 14 | #include "chdir-notify.h" |
c339932b | 15 | #include "path.h" |
60b7a92d | 16 | #include "promisor-remote.h" |
8959555c | 17 | #include "quote.h" |
74ea5c95 | 18 | #include "trace2.h" |
e8cf8ef5 | 19 | #include "worktree.h" |
e90fdc39 JS |
20 | |
21 | static int inside_git_dir = -1; | |
22 | static int inside_work_tree = -1; | |
fada7674 | 23 | static int work_tree_config_is_bogus; |
8d1a7448 GC |
24 | enum allowed_bare_repo { |
25 | ALLOWED_BARE_REPO_EXPLICIT = 0, | |
26 | ALLOWED_BARE_REPO_ALL, | |
27 | }; | |
d288a700 | 28 | |
46c3cd44 JK |
29 | static struct startup_info the_startup_info; |
30 | struct startup_info *startup_info = &the_startup_info; | |
e6f8861b | 31 | const char *tmp_original_cwd; |
46c3cd44 | 32 | |
ddc2a628 MEW |
33 | /* |
34 | * The input parameter must contain an absolute path, and it must already be | |
35 | * normalized. | |
36 | * | |
37 | * Find the part of an absolute path that lies inside the work tree by | |
38 | * dereferencing symlinks outside the work tree, for example: | |
39 | * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file | |
40 | * /dir/file (work tree is /) -> dir/file | |
41 | * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2 | |
42 | * /dir/repolink/file (repolink points to /dir/repo) -> file | |
43 | * /dir/repo (exactly equal to work tree) -> (empty string) | |
44 | */ | |
45 | static int abspath_part_inside_repo(char *path) | |
46 | { | |
47 | size_t len; | |
48 | size_t wtlen; | |
49 | char *path0; | |
50 | int off; | |
51 | const char *work_tree = get_git_work_tree(); | |
3d7747e3 | 52 | struct strbuf realpath = STRBUF_INIT; |
ddc2a628 MEW |
53 | |
54 | if (!work_tree) | |
55 | return -1; | |
56 | wtlen = strlen(work_tree); | |
57 | len = strlen(path); | |
6127ff63 | 58 | off = offset_1st_component(path); |
ddc2a628 MEW |
59 | |
60 | /* check if work tree is already the prefix */ | |
d8727b36 | 61 | if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) { |
ddc2a628 MEW |
62 | if (path[wtlen] == '/') { |
63 | memmove(path, path + wtlen + 1, len - wtlen); | |
64 | return 0; | |
65 | } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') { | |
66 | /* work tree is the root, or the whole path */ | |
67 | memmove(path, path + wtlen, len - wtlen + 1); | |
68 | return 0; | |
69 | } | |
70 | /* work tree might match beginning of a symlink to work tree */ | |
71 | off = wtlen; | |
72 | } | |
73 | path0 = path; | |
6127ff63 | 74 | path += off; |
ddc2a628 MEW |
75 | |
76 | /* check each '/'-terminated level */ | |
77 | while (*path) { | |
78 | path++; | |
79 | if (*path == '/') { | |
80 | *path = '\0'; | |
3d7747e3 AM |
81 | strbuf_realpath(&realpath, path0, 1); |
82 | if (fspathcmp(realpath.buf, work_tree) == 0) { | |
ddc2a628 | 83 | memmove(path0, path + 1, len - (path - path0)); |
3d7747e3 | 84 | strbuf_release(&realpath); |
ddc2a628 MEW |
85 | return 0; |
86 | } | |
87 | *path = '/'; | |
88 | } | |
89 | } | |
90 | ||
91 | /* check whole path */ | |
3d7747e3 AM |
92 | strbuf_realpath(&realpath, path0, 1); |
93 | if (fspathcmp(realpath.buf, work_tree) == 0) { | |
ddc2a628 | 94 | *path0 = '\0'; |
3d7747e3 | 95 | strbuf_release(&realpath); |
ddc2a628 MEW |
96 | return 0; |
97 | } | |
98 | ||
3d7747e3 | 99 | strbuf_release(&realpath); |
ddc2a628 MEW |
100 | return -1; |
101 | } | |
102 | ||
645a29c4 NTND |
103 | /* |
104 | * Normalize "path", prepending the "prefix" for relative paths. If | |
105 | * remaining_prefix is not NULL, return the actual prefix still | |
106 | * remains in the path. For example, prefix = sub1/sub2/ and path is | |
107 | * | |
108 | * foo -> sub1/sub2/foo (full prefix) | |
109 | * ../foo -> sub1/foo (remaining prefix is sub1/) | |
110 | * ../../bar -> bar (no remaining prefix) | |
111 | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) | |
112 | * `pwd`/../bar -> sub1/bar (no remaining prefix) | |
113 | */ | |
114 | char *prefix_path_gently(const char *prefix, int len, | |
115 | int *remaining_prefix, const char *path) | |
d089ebaa JH |
116 | { |
117 | const char *orig = path; | |
18e051a3 CMAB |
118 | char *sanitized; |
119 | if (is_absolute_path(orig)) { | |
3733e694 | 120 | sanitized = xmallocz(strlen(path)); |
645a29c4 NTND |
121 | if (remaining_prefix) |
122 | *remaining_prefix = 0; | |
655ee9ea MEW |
123 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
124 | free(sanitized); | |
125 | return NULL; | |
126 | } | |
127 | if (abspath_part_inside_repo(sanitized)) { | |
128 | free(sanitized); | |
129 | return NULL; | |
130 | } | |
18e051a3 | 131 | } else { |
24041d6b | 132 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
645a29c4 NTND |
133 | if (remaining_prefix) |
134 | *remaining_prefix = len; | |
655ee9ea | 135 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
546e0fd9 JK |
136 | free(sanitized); |
137 | return NULL; | |
d089ebaa | 138 | } |
d089ebaa JH |
139 | } |
140 | return sanitized; | |
f332726e LT |
141 | } |
142 | ||
546e0fd9 JK |
143 | char *prefix_path(const char *prefix, int len, const char *path) |
144 | { | |
645a29c4 | 145 | char *r = prefix_path_gently(prefix, len, NULL, path); |
5c203986 ES |
146 | if (!r) { |
147 | const char *hint_path = get_git_work_tree(); | |
148 | if (!hint_path) | |
149 | hint_path = get_git_dir(); | |
e0020b2f | 150 | die(_("'%s' is outside repository at '%s'"), path, |
5c203986 ES |
151 | absolute_path(hint_path)); |
152 | } | |
546e0fd9 JK |
153 | return r; |
154 | } | |
155 | ||
156 | int path_inside_repo(const char *prefix, const char *path) | |
157 | { | |
158 | int len = prefix ? strlen(prefix) : 0; | |
645a29c4 | 159 | char *r = prefix_path_gently(prefix, len, NULL, path); |
546e0fd9 JK |
160 | if (r) { |
161 | free(r); | |
162 | return 1; | |
163 | } | |
164 | return 0; | |
165 | } | |
166 | ||
c6e8c800 JH |
167 | int check_filename(const char *prefix, const char *arg) |
168 | { | |
e4da43b1 | 169 | char *to_free = NULL; |
c6e8c800 JH |
170 | struct stat st; |
171 | ||
d51c6ee0 JK |
172 | if (skip_prefix(arg, ":/", &arg)) { |
173 | if (!*arg) /* ":/" is root dir, always exists */ | |
4db86e8b | 174 | return 1; |
a08cbcda | 175 | prefix = NULL; |
42471bce JK |
176 | } else if (skip_prefix(arg, ":!", &arg) || |
177 | skip_prefix(arg, ":^", &arg)) { | |
178 | if (!*arg) /* excluding everything is silly, but allowed */ | |
179 | return 1; | |
a08cbcda JK |
180 | } |
181 | ||
182 | if (prefix) | |
183 | arg = to_free = prefix_filename(prefix, arg); | |
184 | ||
185 | if (!lstat(arg, &st)) { | |
e4da43b1 | 186 | free(to_free); |
c6e8c800 | 187 | return 1; /* file exists */ |
e4da43b1 | 188 | } |
93dd544f | 189 | if (is_missing_file_error(errno)) { |
e4da43b1 | 190 | free(to_free); |
c6e8c800 | 191 | return 0; /* file does not exist */ |
e4da43b1 | 192 | } |
fc045fe7 | 193 | die_errno(_("failed to stat '%s'"), arg); |
c6e8c800 JH |
194 | } |
195 | ||
e270f42c NTND |
196 | static void NORETURN die_verify_filename(struct repository *r, |
197 | const char *prefix, | |
023e37c3 MM |
198 | const char *arg, |
199 | int diagnose_misspelt_rev) | |
009fee47 | 200 | { |
023e37c3 | 201 | if (!diagnose_misspelt_rev) |
ab33a76e VA |
202 | die(_("%s: no such path in the working tree.\n" |
203 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally."), | |
023e37c3 | 204 | arg); |
0e539dca JH |
205 | /* |
206 | * Saying "'(icase)foo' does not exist in the index" when the | |
207 | * user gave us ":(icase)foo" is just stupid. A magic pathspec | |
208 | * begins with a colon and is followed by a non-alnum; do not | |
8c135ea2 | 209 | * let maybe_die_on_misspelt_object_name() even trigger. |
0e539dca JH |
210 | */ |
211 | if (!(arg[0] == ':' && !isalnum(arg[1]))) | |
e270f42c | 212 | maybe_die_on_misspelt_object_name(r, arg, prefix); |
0e539dca | 213 | |
009fee47 | 214 | /* ... or fall back the most general message. */ |
ab33a76e VA |
215 | die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
216 | "Use '--' to separate paths from revisions, like this:\n" | |
217 | "'git <command> [<revision>...] -- [<file>...]'"), arg); | |
009fee47 MM |
218 | |
219 | } | |
220 | ||
c99eddd8 JK |
221 | /* |
222 | * Check for arguments that don't resolve as actual files, | |
223 | * but which look sufficiently like pathspecs that we'll consider | |
224 | * them such for the purposes of rev/pathspec DWIM parsing. | |
225 | */ | |
226 | static int looks_like_pathspec(const char *arg) | |
227 | { | |
39e21c6e JK |
228 | const char *p; |
229 | int escaped = 0; | |
230 | ||
231 | /* | |
232 | * Wildcard characters imply the user is looking to match pathspecs | |
233 | * that aren't in the filesystem. Note that this doesn't include | |
234 | * backslash even though it's a glob special; by itself it doesn't | |
235 | * cause any increase in the match. Likewise ignore backslash-escaped | |
236 | * wildcard characters. | |
237 | */ | |
238 | for (p = arg; *p; p++) { | |
239 | if (escaped) { | |
240 | escaped = 0; | |
241 | } else if (is_glob_special(*p)) { | |
242 | if (*p == '\\') | |
243 | escaped = 1; | |
244 | else | |
245 | return 1; | |
246 | } | |
247 | } | |
c99eddd8 JK |
248 | |
249 | /* long-form pathspec magic */ | |
250 | if (starts_with(arg, ":(")) | |
251 | return 1; | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
e23d0b4a LT |
256 | /* |
257 | * Verify a filename that we got as an argument for a pathspec | |
258 | * entry. Note that a filename that begins with "-" never verifies | |
259 | * as true, because even if such a filename were to exist, we want | |
260 | * it to be preceded by the "--" marker (or we want the user to | |
261 | * use a format like "./-filename") | |
023e37c3 MM |
262 | * |
263 | * The "diagnose_misspelt_rev" is used to provide a user-friendly | |
264 | * diagnosis when dying upon finding that "name" is not a pathname. | |
265 | * If set to 1, the diagnosis will try to diagnose "name" as an | |
266 | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis | |
267 | * will only complain about an inexisting file. | |
268 | * | |
269 | * This function is typically called to check that a "file or rev" | |
270 | * argument is unambiguous. In this case, the caller will want | |
271 | * diagnose_misspelt_rev == 1 when verifying the first non-rev | |
272 | * argument (which could have been a revision), and | |
273 | * diagnose_misspelt_rev == 0 for the next ones (because we already | |
274 | * saw a filename, there's not ambiguity anymore). | |
e23d0b4a | 275 | */ |
023e37c3 MM |
276 | void verify_filename(const char *prefix, |
277 | const char *arg, | |
278 | int diagnose_misspelt_rev) | |
e23d0b4a | 279 | { |
e23d0b4a | 280 | if (*arg == '-') |
fc045fe7 | 281 | die(_("option '%s' must come before non-option arguments"), arg); |
2cb47ab6 | 282 | if (looks_like_pathspec(arg) || check_filename(prefix, arg)) |
e23d0b4a | 283 | return; |
e270f42c | 284 | die_verify_filename(the_repository, prefix, arg, diagnose_misspelt_rev); |
e23d0b4a LT |
285 | } |
286 | ||
ea92f41f JH |
287 | /* |
288 | * Opposite of the above: the command line did not have -- marker | |
289 | * and we parsed the arg as a refname. It should not be interpretable | |
290 | * as a filename. | |
291 | */ | |
292 | void verify_non_filename(const char *prefix, const char *arg) | |
293 | { | |
7ae3df8c | 294 | if (!is_inside_work_tree() || is_inside_git_dir()) |
68025633 | 295 | return; |
ea92f41f JH |
296 | if (*arg == '-') |
297 | return; /* flag */ | |
c6e8c800 JH |
298 | if (!check_filename(prefix, arg)) |
299 | return; | |
ab33a76e VA |
300 | die(_("ambiguous argument '%s': both revision and filename\n" |
301 | "Use '--' to separate paths from revisions, like this:\n" | |
302 | "'git <command> [<revision>...] -- [<file>...]'"), arg); | |
ea92f41f JH |
303 | } |
304 | ||
31e26ebc | 305 | int get_common_dir(struct strbuf *sb, const char *gitdir) |
11f9dd71 MK |
306 | { |
307 | const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT); | |
308 | if (git_env_common_dir) { | |
309 | strbuf_addstr(sb, git_env_common_dir); | |
310 | return 1; | |
311 | } else { | |
312 | return get_common_dir_noenv(sb, gitdir); | |
313 | } | |
314 | } | |
315 | ||
316 | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) | |
4dc4e145 NTND |
317 | { |
318 | struct strbuf data = STRBUF_INIT; | |
319 | struct strbuf path = STRBUF_INIT; | |
31e26ebc | 320 | int ret = 0; |
11f9dd71 | 321 | |
4dc4e145 NTND |
322 | strbuf_addf(&path, "%s/commondir", gitdir); |
323 | if (file_exists(path.buf)) { | |
324 | if (strbuf_read_file(&data, path.buf, 0) <= 0) | |
325 | die_errno(_("failed to read %s"), path.buf); | |
326 | while (data.len && (data.buf[data.len - 1] == '\n' || | |
327 | data.buf[data.len - 1] == '\r')) | |
328 | data.len--; | |
329 | data.buf[data.len] = '\0'; | |
330 | strbuf_reset(&path); | |
331 | if (!is_absolute_path(data.buf)) | |
332 | strbuf_addf(&path, "%s/", gitdir); | |
333 | strbuf_addbuf(&path, &data); | |
33ad9ddd | 334 | strbuf_add_real_path(sb, path.buf); |
31e26ebc | 335 | ret = 1; |
4ac9006f | 336 | } else { |
4dc4e145 | 337 | strbuf_addstr(sb, gitdir); |
4ac9006f BW |
338 | } |
339 | ||
4dc4e145 NTND |
340 | strbuf_release(&data); |
341 | strbuf_release(&path); | |
31e26ebc | 342 | return ret; |
4dc4e145 | 343 | } |
d288a700 | 344 | |
5f5608bc | 345 | /* |
ad1a382f | 346 | * Test if it looks like we're at a git directory. |
5e7bfe25 | 347 | * We want to see: |
5f5608bc | 348 | * |
790296fd | 349 | * - either an objects/ directory _or_ the proper |
5f5608bc | 350 | * GIT_OBJECT_DIRECTORY environment variable |
ad1a382f | 351 | * - a refs/ directory |
8098a178 | 352 | * - either a HEAD symlink or a HEAD file that is formatted as |
c847f537 JH |
353 | * a proper "ref:", or a regular file HEAD that has a properly |
354 | * formatted sha1 object name. | |
5f5608bc | 355 | */ |
b3256eb8 | 356 | int is_git_directory(const char *suspect) |
5f5608bc | 357 | { |
1d186b6f NTND |
358 | struct strbuf path = STRBUF_INIT; |
359 | int ret = 0; | |
360 | size_t len; | |
ad1a382f | 361 | |
4dc4e145 | 362 | /* Check worktree-related signatures */ |
fa4d8c78 JK |
363 | strbuf_addstr(&path, suspect); |
364 | strbuf_complete(&path, '/'); | |
365 | strbuf_addstr(&path, "HEAD"); | |
4dc4e145 NTND |
366 | if (validate_headref(path.buf)) |
367 | goto done; | |
368 | ||
369 | strbuf_reset(&path); | |
370 | get_common_dir(&path, suspect); | |
1d186b6f | 371 | len = path.len; |
4dc4e145 NTND |
372 | |
373 | /* Check non-worktree-related signatures */ | |
ad1a382f SP |
374 | if (getenv(DB_ENVIRONMENT)) { |
375 | if (access(getenv(DB_ENVIRONMENT), X_OK)) | |
1d186b6f | 376 | goto done; |
ad1a382f SP |
377 | } |
378 | else { | |
4dc4e145 | 379 | strbuf_setlen(&path, len); |
1d186b6f NTND |
380 | strbuf_addstr(&path, "/objects"); |
381 | if (access(path.buf, X_OK)) | |
382 | goto done; | |
ad1a382f SP |
383 | } |
384 | ||
1d186b6f NTND |
385 | strbuf_setlen(&path, len); |
386 | strbuf_addstr(&path, "/refs"); | |
387 | if (access(path.buf, X_OK)) | |
388 | goto done; | |
ad1a382f | 389 | |
1d186b6f NTND |
390 | ret = 1; |
391 | done: | |
392 | strbuf_release(&path); | |
393 | return ret; | |
5f5608bc LT |
394 | } |
395 | ||
ffd036b1 JK |
396 | int is_nonbare_repository_dir(struct strbuf *path) |
397 | { | |
398 | int ret = 0; | |
399 | int gitfile_error; | |
400 | size_t orig_path_len = path->len; | |
401 | assert(orig_path_len != 0); | |
402 | strbuf_complete(path, '/'); | |
403 | strbuf_addstr(path, ".git"); | |
404 | if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) | |
405 | ret = 1; | |
406 | if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || | |
407 | gitfile_error == READ_GITFILE_ERR_READ_FAILED) | |
408 | ret = 1; | |
409 | strbuf_setlen(path, orig_path_len); | |
410 | return ret; | |
411 | } | |
412 | ||
68025633 JS |
413 | int is_inside_git_dir(void) |
414 | { | |
e90fdc39 JS |
415 | if (inside_git_dir < 0) |
416 | inside_git_dir = is_inside_dir(get_git_dir()); | |
417 | return inside_git_dir; | |
892c41b9 ML |
418 | } |
419 | ||
892c41b9 ML |
420 | int is_inside_work_tree(void) |
421 | { | |
e90fdc39 JS |
422 | if (inside_work_tree < 0) |
423 | inside_work_tree = is_inside_dir(get_git_work_tree()); | |
424 | return inside_work_tree; | |
892c41b9 ML |
425 | } |
426 | ||
f3fa1838 JH |
427 | void setup_work_tree(void) |
428 | { | |
8500e0de | 429 | const char *work_tree; |
354e6534 JS |
430 | static int initialized = 0; |
431 | ||
432 | if (initialized) | |
433 | return; | |
fada7674 JK |
434 | |
435 | if (work_tree_config_is_bogus) | |
fc045fe7 | 436 | die(_("unable to set up work tree using invalid config")); |
fada7674 | 437 | |
354e6534 | 438 | work_tree = get_git_work_tree(); |
8500e0de | 439 | if (!work_tree || chdir_notify(work_tree)) |
fc045fe7 | 440 | die(_("this operation must be run in a work tree")); |
0ed74813 NTND |
441 | |
442 | /* | |
443 | * Make sure subsequent git processes find correct worktree | |
444 | * if $GIT_WORK_TREE is set relative | |
445 | */ | |
446 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) | |
447 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); | |
448 | ||
354e6534 | 449 | initialized = 1; |
59f0f2f3 MH |
450 | } |
451 | ||
e6f8861b EN |
452 | static void setup_original_cwd(void) |
453 | { | |
454 | struct strbuf tmp = STRBUF_INIT; | |
455 | const char *worktree = NULL; | |
456 | int offset = -1; | |
457 | ||
458 | if (!tmp_original_cwd) | |
459 | return; | |
460 | ||
461 | /* | |
462 | * startup_info->original_cwd points to the current working | |
463 | * directory we inherited from our parent process, which is a | |
464 | * directory we want to avoid removing. | |
465 | * | |
466 | * For convience, we would like to have the path relative to the | |
467 | * worktree instead of an absolute path. | |
468 | * | |
469 | * Yes, startup_info->original_cwd is usually the same as 'prefix', | |
470 | * but differs in two ways: | |
471 | * - prefix has a trailing '/' | |
472 | * - if the user passes '-C' to git, that modifies the prefix but | |
473 | * not startup_info->original_cwd. | |
474 | */ | |
475 | ||
476 | /* Normalize the directory */ | |
c37c6dc6 KL |
477 | if (!strbuf_realpath(&tmp, tmp_original_cwd, 0)) { |
478 | trace2_data_string("setup", the_repository, | |
479 | "realpath-path", tmp_original_cwd); | |
480 | trace2_data_string("setup", the_repository, | |
481 | "realpath-failure", strerror(errno)); | |
482 | free((char*)tmp_original_cwd); | |
483 | tmp_original_cwd = NULL; | |
484 | return; | |
485 | } | |
486 | ||
e6f8861b EN |
487 | free((char*)tmp_original_cwd); |
488 | tmp_original_cwd = NULL; | |
489 | startup_info->original_cwd = strbuf_detach(&tmp, NULL); | |
490 | ||
491 | /* | |
492 | * Get our worktree; we only protect the current working directory | |
493 | * if it's in the worktree. | |
494 | */ | |
495 | worktree = get_git_work_tree(); | |
496 | if (!worktree) | |
497 | goto no_prevention_needed; | |
498 | ||
499 | offset = dir_inside_of(startup_info->original_cwd, worktree); | |
500 | if (offset >= 0) { | |
501 | /* | |
502 | * If startup_info->original_cwd == worktree, that is already | |
503 | * protected and we don't need original_cwd as a secondary | |
504 | * protection measure. | |
505 | */ | |
506 | if (!*(startup_info->original_cwd + offset)) | |
507 | goto no_prevention_needed; | |
508 | ||
509 | /* | |
510 | * original_cwd was inside worktree; precompose it just as | |
511 | * we do prefix so that built up paths will match | |
512 | */ | |
513 | startup_info->original_cwd = \ | |
514 | precompose_string_if_needed(startup_info->original_cwd | |
515 | + offset); | |
516 | return; | |
517 | } | |
518 | ||
519 | no_prevention_needed: | |
520 | free((char*)startup_info->original_cwd); | |
521 | startup_info->original_cwd = NULL; | |
522 | } | |
523 | ||
a4e7e317 GC |
524 | static int read_worktree_config(const char *var, const char *value, |
525 | const struct config_context *ctx UNUSED, | |
526 | void *vdata) | |
58b284a2 NTND |
527 | { |
528 | struct repository_format *data = vdata; | |
529 | ||
530 | if (strcmp(var, "core.bare") == 0) { | |
531 | data->is_bare = git_config_bool(var, value); | |
532 | } else if (strcmp(var, "core.worktree") == 0) { | |
533 | if (!value) | |
534 | return config_error_nonbool(var); | |
13019979 | 535 | free(data->work_tree); |
58b284a2 NTND |
536 | data->work_tree = xstrdup(value); |
537 | } | |
538 | return 0; | |
539 | } | |
540 | ||
ec91ffca JK |
541 | enum extension_result { |
542 | EXTENSION_ERROR = -1, /* compatible with error(), etc */ | |
543 | EXTENSION_UNKNOWN = 0, | |
544 | EXTENSION_OK = 1 | |
545 | }; | |
546 | ||
547 | /* | |
548 | * Do not add new extensions to this function. It handles extensions which are | |
549 | * respected even in v0-format repositories for historical compatibility. | |
550 | */ | |
551 | static enum extension_result handle_extension_v0(const char *var, | |
552 | const char *value, | |
553 | const char *ext, | |
554 | struct repository_format *data) | |
555 | { | |
556 | if (!strcmp(ext, "noop")) { | |
557 | return EXTENSION_OK; | |
558 | } else if (!strcmp(ext, "preciousobjects")) { | |
559 | data->precious_objects = git_config_bool(var, value); | |
560 | return EXTENSION_OK; | |
561 | } else if (!strcmp(ext, "partialclone")) { | |
ec91ffca JK |
562 | data->partial_clone = xstrdup(value); |
563 | return EXTENSION_OK; | |
564 | } else if (!strcmp(ext, "worktreeconfig")) { | |
565 | data->worktree_config = git_config_bool(var, value); | |
566 | return EXTENSION_OK; | |
567 | } | |
568 | ||
569 | return EXTENSION_UNKNOWN; | |
570 | } | |
571 | ||
572 | /* | |
573 | * Record any new extensions in this function. | |
574 | */ | |
575 | static enum extension_result handle_extension(const char *var, | |
576 | const char *value, | |
577 | const char *ext, | |
578 | struct repository_format *data) | |
579 | { | |
580 | if (!strcmp(ext, "noop-v1")) { | |
581 | return EXTENSION_OK; | |
e0ad9574 JH |
582 | } else if (!strcmp(ext, "objectformat")) { |
583 | int format; | |
ec91ffca | 584 | |
e0ad9574 JH |
585 | if (!value) |
586 | return config_error_nonbool(var); | |
587 | format = hash_algo_by_name(value); | |
588 | if (format == GIT_HASH_UNKNOWN) | |
1a8aea85 JNA |
589 | return error(_("invalid value for '%s': '%s'"), |
590 | "extensions.objectformat", value); | |
e0ad9574 JH |
591 | data->hash_algo = format; |
592 | return EXTENSION_OK; | |
593 | } | |
ec91ffca JK |
594 | return EXTENSION_UNKNOWN; |
595 | } | |
596 | ||
a4e7e317 GC |
597 | static int check_repo_format(const char *var, const char *value, |
598 | const struct config_context *ctx, void *vdata) | |
31e26ebc | 599 | { |
2cc7c2c7 | 600 | struct repository_format *data = vdata; |
00a09d57 JK |
601 | const char *ext; |
602 | ||
31e26ebc | 603 | if (strcmp(var, "core.repositoryformatversion") == 0) |
8868b1eb | 604 | data->version = git_config_int(var, value, ctx->kvi); |
00a09d57 | 605 | else if (skip_prefix(var, "extensions.", &ext)) { |
ec91ffca JK |
606 | switch (handle_extension_v0(var, value, ext, data)) { |
607 | case EXTENSION_ERROR: | |
608 | return -1; | |
609 | case EXTENSION_OK: | |
610 | return 0; | |
611 | case EXTENSION_UNKNOWN: | |
612 | break; | |
613 | } | |
614 | ||
615 | switch (handle_extension(var, value, ext, data)) { | |
616 | case EXTENSION_ERROR: | |
617 | return -1; | |
618 | case EXTENSION_OK: | |
619 | string_list_append(&data->v1_only_extensions, ext); | |
620 | return 0; | |
621 | case EXTENSION_UNKNOWN: | |
2cc7c2c7 | 622 | string_list_append(&data->unknown_extensions, ext); |
ec91ffca JK |
623 | return 0; |
624 | } | |
00a09d57 | 625 | } |
58b284a2 | 626 | |
a4e7e317 | 627 | return read_worktree_config(var, value, ctx, vdata); |
31e26ebc NTND |
628 | } |
629 | ||
abade65b | 630 | static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok) |
9459aa77 | 631 | { |
7d0fb0da | 632 | struct strbuf sb = STRBUF_INIT; |
2cc7c2c7 | 633 | struct strbuf err = STRBUF_INIT; |
652f18ee | 634 | int has_common; |
00a09d57 | 635 | |
652f18ee | 636 | has_common = get_common_dir(&sb, gitdir); |
e61a509a | 637 | strbuf_addstr(&sb, "/config"); |
abade65b | 638 | read_repository_format(candidate, sb.buf); |
2cc7c2c7 | 639 | strbuf_release(&sb); |
e61a509a | 640 | |
337e51ce | 641 | /* |
2cc7c2c7 JK |
642 | * For historical use of check_repository_format() in git-init, |
643 | * we treat a missing config as a silent "ok", even when nongit_ok | |
644 | * is unset. | |
337e51ce | 645 | */ |
abade65b | 646 | if (candidate->version < 0) |
2cc7c2c7 JK |
647 | return 0; |
648 | ||
abade65b | 649 | if (verify_repository_format(candidate, &err) < 0) { |
2cc7c2c7 JK |
650 | if (nongit_ok) { |
651 | warning("%s", err.buf); | |
652 | strbuf_release(&err); | |
653 | *nongit_ok = -1; | |
654 | return -1; | |
655 | } | |
656 | die("%s", err.buf); | |
657 | } | |
658 | ||
11664196 | 659 | repository_format_precious_objects = candidate->precious_objects; |
abade65b | 660 | string_list_clear(&candidate->unknown_extensions, 0); |
ec91ffca | 661 | string_list_clear(&candidate->v1_only_extensions, 0); |
58b284a2 | 662 | |
3867f6d6 | 663 | if (candidate->worktree_config) { |
58b284a2 NTND |
664 | /* |
665 | * pick up core.bare and core.worktree from per-worktree | |
666 | * config if present | |
667 | */ | |
668 | strbuf_addf(&sb, "%s/config.worktree", gitdir); | |
669 | git_config_from_file(read_worktree_config, sb.buf, candidate); | |
670 | strbuf_release(&sb); | |
671 | has_common = 0; | |
672 | } | |
673 | ||
652f18ee | 674 | if (!has_common) { |
abade65b | 675 | if (candidate->is_bare != -1) { |
676 | is_bare_repository_cfg = candidate->is_bare; | |
652f18ee JK |
677 | if (is_bare_repository_cfg == 1) |
678 | inside_work_tree = -1; | |
679 | } | |
abade65b | 680 | if (candidate->work_tree) { |
652f18ee | 681 | free(git_work_tree_cfg); |
e8805af1 | 682 | git_work_tree_cfg = xstrdup(candidate->work_tree); |
2cc7c2c7 | 683 | inside_work_tree = -1; |
652f18ee | 684 | } |
2cc7c2c7 JK |
685 | } |
686 | ||
687 | return 0; | |
688 | } | |
689 | ||
16af5f1a XL |
690 | int upgrade_repository_format(int target_version) |
691 | { | |
692 | struct strbuf sb = STRBUF_INIT; | |
693 | struct strbuf err = STRBUF_INIT; | |
694 | struct strbuf repo_version = STRBUF_INIT; | |
695 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; | |
4ce14e13 | 696 | int ret; |
16af5f1a XL |
697 | |
698 | strbuf_git_common_path(&sb, the_repository, "config"); | |
699 | read_repository_format(&repo_fmt, sb.buf); | |
700 | strbuf_release(&sb); | |
701 | ||
4ce14e13 PS |
702 | if (repo_fmt.version >= target_version) { |
703 | ret = 0; | |
704 | goto out; | |
705 | } | |
16af5f1a | 706 | |
62f2eca6 | 707 | if (verify_repository_format(&repo_fmt, &err) < 0) { |
4ce14e13 PS |
708 | ret = error("cannot upgrade repository format from %d to %d: %s", |
709 | repo_fmt.version, target_version, err.buf); | |
710 | goto out; | |
711 | } | |
712 | if (!repo_fmt.version && repo_fmt.unknown_extensions.nr) { | |
713 | ret = error("cannot upgrade repository format: " | |
714 | "unknown extension %s", | |
715 | repo_fmt.unknown_extensions.items[0].string); | |
716 | goto out; | |
16af5f1a XL |
717 | } |
718 | ||
719 | strbuf_addf(&repo_version, "%d", target_version); | |
720 | git_config_set("core.repositoryformatversion", repo_version.buf); | |
4ce14e13 PS |
721 | |
722 | ret = 1; | |
723 | ||
724 | out: | |
9972cd60 | 725 | clear_repository_format(&repo_fmt); |
16af5f1a | 726 | strbuf_release(&repo_version); |
4ce14e13 PS |
727 | strbuf_release(&err); |
728 | return ret; | |
16af5f1a XL |
729 | } |
730 | ||
e8805af1 MÅ |
731 | static void init_repository_format(struct repository_format *format) |
732 | { | |
733 | const struct repository_format fresh = REPOSITORY_FORMAT_INIT; | |
734 | ||
735 | memcpy(format, &fresh, sizeof(fresh)); | |
736 | } | |
737 | ||
652f18ee | 738 | int read_repository_format(struct repository_format *format, const char *path) |
2cc7c2c7 | 739 | { |
e8805af1 | 740 | clear_repository_format(format); |
652f18ee | 741 | git_config_from_file(check_repo_format, path, format); |
e8805af1 MÅ |
742 | if (format->version == -1) |
743 | clear_repository_format(format); | |
2cc7c2c7 JK |
744 | return format->version; |
745 | } | |
746 | ||
e8805af1 MÅ |
747 | void clear_repository_format(struct repository_format *format) |
748 | { | |
749 | string_list_clear(&format->unknown_extensions, 0); | |
ec91ffca | 750 | string_list_clear(&format->v1_only_extensions, 0); |
e8805af1 MÅ |
751 | free(format->work_tree); |
752 | free(format->partial_clone); | |
753 | init_repository_format(format); | |
754 | } | |
755 | ||
2cc7c2c7 JK |
756 | int verify_repository_format(const struct repository_format *format, |
757 | struct strbuf *err) | |
758 | { | |
759 | if (GIT_REPO_VERSION_READ < format->version) { | |
274db840 | 760 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
2cc7c2c7 JK |
761 | GIT_REPO_VERSION_READ, format->version); |
762 | return -1; | |
763 | } | |
764 | ||
765 | if (format->version >= 1 && format->unknown_extensions.nr) { | |
00a09d57 JK |
766 | int i; |
767 | ||
8013d7d9 AH |
768 | strbuf_addstr(err, Q_("unknown repository extension found:", |
769 | "unknown repository extensions found:", | |
770 | format->unknown_extensions.nr)); | |
00a09d57 | 771 | |
2cc7c2c7 JK |
772 | for (i = 0; i < format->unknown_extensions.nr; i++) |
773 | strbuf_addf(err, "\n\t%s", | |
774 | format->unknown_extensions.items[i].string); | |
775 | return -1; | |
00a09d57 JK |
776 | } |
777 | ||
ec91ffca JK |
778 | if (format->version == 0 && format->v1_only_extensions.nr) { |
779 | int i; | |
780 | ||
781 | strbuf_addstr(err, | |
8013d7d9 AH |
782 | Q_("repo version is 0, but v1-only extension found:", |
783 | "repo version is 0, but v1-only extensions found:", | |
784 | format->v1_only_extensions.nr)); | |
ec91ffca JK |
785 | |
786 | for (i = 0; i < format->v1_only_extensions.nr; i++) | |
787 | strbuf_addf(err, "\n\t%s", | |
788 | format->v1_only_extensions.items[i].string); | |
789 | return -1; | |
790 | } | |
791 | ||
2cc7c2c7 | 792 | return 0; |
9459aa77 NTND |
793 | } |
794 | ||
5f29433f SB |
795 | void read_gitfile_error_die(int error_code, const char *path, const char *dir) |
796 | { | |
797 | switch (error_code) { | |
798 | case READ_GITFILE_ERR_STAT_FAILED: | |
799 | case READ_GITFILE_ERR_NOT_A_FILE: | |
800 | /* non-fatal; follow return path */ | |
801 | break; | |
802 | case READ_GITFILE_ERR_OPEN_FAILED: | |
fc045fe7 | 803 | die_errno(_("error opening '%s'"), path); |
5f29433f | 804 | case READ_GITFILE_ERR_TOO_LARGE: |
fc045fe7 | 805 | die(_("too large to be a .git file: '%s'"), path); |
5f29433f | 806 | case READ_GITFILE_ERR_READ_FAILED: |
fc045fe7 | 807 | die(_("error reading %s"), path); |
5f29433f | 808 | case READ_GITFILE_ERR_INVALID_FORMAT: |
fc045fe7 | 809 | die(_("invalid gitfile format: %s"), path); |
5f29433f | 810 | case READ_GITFILE_ERR_NO_PATH: |
fc045fe7 | 811 | die(_("no path in gitfile: %s"), path); |
5f29433f | 812 | case READ_GITFILE_ERR_NOT_A_REPO: |
fc045fe7 | 813 | die(_("not a git repository: %s"), dir); |
5f29433f | 814 | default: |
033abf97 | 815 | BUG("unknown error code"); |
5f29433f SB |
816 | } |
817 | } | |
818 | ||
b44ebb19 LH |
819 | /* |
820 | * Try to read the location of the git directory from the .git file, | |
ea1d8756 HWN |
821 | * return path to git directory if found. The return value comes from |
822 | * a shared buffer. | |
a93bedad EE |
823 | * |
824 | * On failure, if return_error_code is not NULL, return_error_code | |
825 | * will be set to an error code and NULL will be returned. If | |
826 | * return_error_code is NULL the function will die instead (for most | |
827 | * cases). | |
b44ebb19 | 828 | */ |
a93bedad | 829 | const char *read_gitfile_gently(const char *path, int *return_error_code) |
b44ebb19 | 830 | { |
921bdd96 | 831 | const int max_file_size = 1 << 20; /* 1MB */ |
a93bedad EE |
832 | int error_code = 0; |
833 | char *buf = NULL; | |
834 | char *dir = NULL; | |
40c813e0 | 835 | const char *slash; |
b44ebb19 LH |
836 | struct stat st; |
837 | int fd; | |
b1905aea | 838 | ssize_t len; |
3d7747e3 | 839 | static struct strbuf realpath = STRBUF_INIT; |
b44ebb19 | 840 | |
a93bedad | 841 | if (stat(path, &st)) { |
5c4003ca | 842 | /* NEEDSWORK: discern between ENOENT vs other errors */ |
a93bedad EE |
843 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
844 | goto cleanup_return; | |
845 | } | |
846 | if (!S_ISREG(st.st_mode)) { | |
847 | error_code = READ_GITFILE_ERR_NOT_A_FILE; | |
848 | goto cleanup_return; | |
849 | } | |
921bdd96 EE |
850 | if (st.st_size > max_file_size) { |
851 | error_code = READ_GITFILE_ERR_TOO_LARGE; | |
852 | goto cleanup_return; | |
853 | } | |
b44ebb19 | 854 | fd = open(path, O_RDONLY); |
a93bedad EE |
855 | if (fd < 0) { |
856 | error_code = READ_GITFILE_ERR_OPEN_FAILED; | |
857 | goto cleanup_return; | |
858 | } | |
3733e694 | 859 | buf = xmallocz(st.st_size); |
b44ebb19 LH |
860 | len = read_in_full(fd, buf, st.st_size); |
861 | close(fd); | |
a93bedad EE |
862 | if (len != st.st_size) { |
863 | error_code = READ_GITFILE_ERR_READ_FAILED; | |
864 | goto cleanup_return; | |
865 | } | |
a93bedad EE |
866 | if (!starts_with(buf, "gitdir: ")) { |
867 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; | |
868 | goto cleanup_return; | |
869 | } | |
b44ebb19 LH |
870 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
871 | len--; | |
a93bedad EE |
872 | if (len < 9) { |
873 | error_code = READ_GITFILE_ERR_NO_PATH; | |
874 | goto cleanup_return; | |
875 | } | |
b44ebb19 | 876 | buf[len] = '\0'; |
40c813e0 BK |
877 | dir = buf + 8; |
878 | ||
879 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
880 | size_t pathlen = slash+1 - path; | |
75faa45a JK |
881 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
882 | (int)(len - 8), buf + 8); | |
40c813e0 BK |
883 | free(buf); |
884 | buf = dir; | |
885 | } | |
a93bedad EE |
886 | if (!is_git_directory(dir)) { |
887 | error_code = READ_GITFILE_ERR_NOT_A_REPO; | |
888 | goto cleanup_return; | |
889 | } | |
3d7747e3 AM |
890 | |
891 | strbuf_realpath(&realpath, dir, 1); | |
892 | path = realpath.buf; | |
40c813e0 | 893 | |
a93bedad | 894 | cleanup_return: |
a93bedad EE |
895 | if (return_error_code) |
896 | *return_error_code = error_code; | |
5f29433f SB |
897 | else if (error_code) |
898 | read_gitfile_error_die(error_code, path, dir); | |
a93bedad | 899 | |
b44ebb19 | 900 | free(buf); |
38ae8784 | 901 | return error_code ? NULL : path; |
b44ebb19 LH |
902 | } |
903 | ||
e4e30347 | 904 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
7333ed17 | 905 | struct strbuf *cwd, |
abade65b | 906 | struct repository_format *repo_fmt, |
b3f66fd3 | 907 | int *nongit_ok) |
e4e30347 | 908 | { |
b3f66fd3 NTND |
909 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
910 | const char *worktree; | |
911 | char *gitfile; | |
9b125da4 | 912 | int offset; |
e4e30347 JN |
913 | |
914 | if (PATH_MAX - 40 < strlen(gitdirenv)) | |
fc045fe7 | 915 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
b3f66fd3 | 916 | |
13d6ec91 | 917 | gitfile = (char*)read_gitfile(gitdirenv); |
b3f66fd3 NTND |
918 | if (gitfile) { |
919 | gitfile = xstrdup(gitfile); | |
920 | gitdirenv = gitfile; | |
921 | } | |
922 | ||
e4e30347 JN |
923 | if (!is_git_directory(gitdirenv)) { |
924 | if (nongit_ok) { | |
925 | *nongit_ok = 1; | |
b3f66fd3 | 926 | free(gitfile); |
e4e30347 JN |
927 | return NULL; |
928 | } | |
fc045fe7 | 929 | die(_("not a git repository: '%s'"), gitdirenv); |
e4e30347 | 930 | } |
b3f66fd3 | 931 | |
abade65b | 932 | if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) { |
b3f66fd3 NTND |
933 | free(gitfile); |
934 | return NULL; | |
e4e30347 | 935 | } |
b3f66fd3 NTND |
936 | |
937 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ | |
938 | if (work_tree_env) | |
939 | set_git_work_tree(work_tree_env); | |
940 | else if (is_bare_repository_cfg > 0) { | |
fada7674 JK |
941 | if (git_work_tree_cfg) { |
942 | /* #22.2, #30 */ | |
943 | warning("core.bare and core.worktree do not make sense"); | |
944 | work_tree_config_is_bogus = 1; | |
945 | } | |
b3f66fd3 NTND |
946 | |
947 | /* #18, #26 */ | |
0915a5b4 | 948 | set_git_dir(gitdirenv, 0); |
b3f66fd3 | 949 | free(gitfile); |
e4e30347 | 950 | return NULL; |
b3f66fd3 NTND |
951 | } |
952 | else if (git_work_tree_cfg) { /* #6, #14 */ | |
953 | if (is_absolute_path(git_work_tree_cfg)) | |
954 | set_git_work_tree(git_work_tree_cfg); | |
955 | else { | |
56b9f6e7 | 956 | char *core_worktree; |
b3f66fd3 | 957 | if (chdir(gitdirenv)) |
fc045fe7 | 958 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
b3f66fd3 | 959 | if (chdir(git_work_tree_cfg)) |
fc045fe7 | 960 | die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg); |
56b9f6e7 | 961 | core_worktree = xgetcwd(); |
7333ed17 | 962 | if (chdir(cwd->buf)) |
fc045fe7 | 963 | die_errno(_("cannot come back to cwd")); |
b3f66fd3 | 964 | set_git_work_tree(core_worktree); |
56b9f6e7 | 965 | free(core_worktree); |
b3f66fd3 NTND |
966 | } |
967 | } | |
2cd83d10 JK |
968 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
969 | /* #16d */ | |
0915a5b4 | 970 | set_git_dir(gitdirenv, 0); |
2cd83d10 JK |
971 | free(gitfile); |
972 | return NULL; | |
973 | } | |
b3f66fd3 NTND |
974 | else /* #2, #10 */ |
975 | set_git_work_tree("."); | |
976 | ||
977 | /* set_git_work_tree() must have been called by now */ | |
978 | worktree = get_git_work_tree(); | |
979 | ||
980 | /* both get_git_work_tree() and cwd are already normalized */ | |
7333ed17 | 981 | if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ |
0915a5b4 | 982 | set_git_dir(gitdirenv, 0); |
b3f66fd3 | 983 | free(gitfile); |
e4e30347 | 984 | return NULL; |
b3f66fd3 | 985 | } |
e4e30347 | 986 | |
7333ed17 | 987 | offset = dir_inside_of(cwd->buf, worktree); |
9b125da4 | 988 | if (offset >= 0) { /* cwd inside worktree? */ |
0915a5b4 | 989 | set_git_dir(gitdirenv, 1); |
b3f66fd3 | 990 | if (chdir(worktree)) |
fc045fe7 | 991 | die_errno(_("cannot chdir to '%s'"), worktree); |
7333ed17 | 992 | strbuf_addch(cwd, '/'); |
b3f66fd3 | 993 | free(gitfile); |
7333ed17 | 994 | return cwd->buf + offset; |
93a00542 | 995 | } |
b3f66fd3 NTND |
996 | |
997 | /* cwd outside worktree */ | |
0915a5b4 | 998 | set_git_dir(gitdirenv, 0); |
b3f66fd3 NTND |
999 | free(gitfile); |
1000 | return NULL; | |
93a00542 JN |
1001 | } |
1002 | ||
9951d3b3 | 1003 | static const char *setup_discovered_git_dir(const char *gitdir, |
7333ed17 | 1004 | struct strbuf *cwd, int offset, |
abade65b | 1005 | struct repository_format *repo_fmt, |
9951d3b3 | 1006 | int *nongit_ok) |
98937bef | 1007 | { |
abade65b | 1008 | if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok)) |
9951d3b3 | 1009 | return NULL; |
98937bef | 1010 | |
4868b2ea JN |
1011 | /* --work-tree is set without --git-dir; use discovered one */ |
1012 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
2d4dcf21 JS |
1013 | char *to_free = NULL; |
1014 | const char *ret; | |
1015 | ||
7333ed17 | 1016 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
2d4dcf21 | 1017 | gitdir = to_free = real_pathdup(gitdir, 1); |
7333ed17 | 1018 | if (chdir(cwd->buf)) |
fc045fe7 | 1019 | die_errno(_("cannot come back to cwd")); |
abade65b | 1020 | ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
2d4dcf21 JS |
1021 | free(to_free); |
1022 | return ret; | |
4868b2ea JN |
1023 | } |
1024 | ||
9951d3b3 NTND |
1025 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
1026 | if (is_bare_repository_cfg > 0) { | |
0915a5b4 | 1027 | set_git_dir(gitdir, (offset != cwd->len)); |
7333ed17 | 1028 | if (chdir(cwd->buf)) |
fc045fe7 | 1029 | die_errno(_("cannot come back to cwd")); |
98937bef | 1030 | return NULL; |
9951d3b3 | 1031 | } |
98937bef | 1032 | |
9951d3b3 NTND |
1033 | /* #0, #1, #5, #8, #9, #12, #13 */ |
1034 | set_git_work_tree("."); | |
1035 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) | |
0915a5b4 | 1036 | set_git_dir(gitdir, 0); |
98937bef | 1037 | inside_git_dir = 0; |
9951d3b3 | 1038 | inside_work_tree = 1; |
5cf7b3b1 | 1039 | if (offset >= cwd->len) |
98937bef NTND |
1040 | return NULL; |
1041 | ||
df380d58 JS |
1042 | /* Make "offset" point past the '/' (already the case for root dirs) */ |
1043 | if (offset != offset_1st_component(cwd->buf)) | |
1044 | offset++; | |
1045 | /* Add a '/' at the end */ | |
7333ed17 RS |
1046 | strbuf_addch(cwd, '/'); |
1047 | return cwd->buf + offset; | |
98937bef NTND |
1048 | } |
1049 | ||
1cd8031b | 1050 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
7333ed17 | 1051 | static const char *setup_bare_git_dir(struct strbuf *cwd, int offset, |
abade65b | 1052 | struct repository_format *repo_fmt, |
7333ed17 | 1053 | int *nongit_ok) |
68698da5 JN |
1054 | { |
1055 | int root_len; | |
1056 | ||
abade65b | 1057 | if (check_repository_format_gently(".", repo_fmt, nongit_ok)) |
1cd8031b NTND |
1058 | return NULL; |
1059 | ||
2cd83d10 JK |
1060 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
1061 | ||
4868b2ea JN |
1062 | /* --work-tree is set without --git-dir; use discovered one */ |
1063 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
da6f8475 | 1064 | static const char *gitdir; |
4868b2ea | 1065 | |
7333ed17 RS |
1066 | gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset); |
1067 | if (chdir(cwd->buf)) | |
fc045fe7 | 1068 | die_errno(_("cannot come back to cwd")); |
abade65b | 1069 | return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
4868b2ea JN |
1070 | } |
1071 | ||
68698da5 | 1072 | inside_git_dir = 1; |
1cd8031b | 1073 | inside_work_tree = 0; |
7333ed17 RS |
1074 | if (offset != cwd->len) { |
1075 | if (chdir(cwd->buf)) | |
fc045fe7 | 1076 | die_errno(_("cannot come back to cwd")); |
7333ed17 RS |
1077 | root_len = offset_1st_component(cwd->buf); |
1078 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); | |
0915a5b4 | 1079 | set_git_dir(cwd->buf, 0); |
337e51ce | 1080 | } |
1cd8031b | 1081 | else |
0915a5b4 | 1082 | set_git_dir(".", 0); |
68698da5 JN |
1083 | return NULL; |
1084 | } | |
1085 | ||
2565b43b | 1086 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
60c98d1e JN |
1087 | { |
1088 | struct stat buf; | |
2565b43b | 1089 | if (stat(path, &buf)) { |
fc045fe7 | 1090 | die_errno(_("failed to stat '%*s%s%s'"), |
2565b43b | 1091 | prefix_len, |
60c98d1e JN |
1092 | prefix ? prefix : "", |
1093 | prefix ? "/" : "", path); | |
2565b43b | 1094 | } |
60c98d1e JN |
1095 | return buf.st_dev; |
1096 | } | |
1097 | ||
9e2326c7 | 1098 | /* |
1b77d83c | 1099 | * A "string_list_each_func_t" function that canonicalizes an entry |
4530a85b | 1100 | * from GIT_CEILING_DIRECTORIES using real_pathdup(), or |
7ec30aaa MH |
1101 | * discards it if unusable. The presence of an empty entry in |
1102 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all | |
1103 | * subsequent entries. | |
9e2326c7 | 1104 | */ |
1b77d83c | 1105 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
7ec30aaa | 1106 | void *cb_data) |
9e2326c7 | 1107 | { |
7ec30aaa | 1108 | int *empty_entry_found = cb_data; |
1b77d83c | 1109 | char *ceil = item->string; |
9e2326c7 | 1110 | |
7ec30aaa MH |
1111 | if (!*ceil) { |
1112 | *empty_entry_found = 1; | |
9e2326c7 | 1113 | return 0; |
7ec30aaa | 1114 | } else if (!is_absolute_path(ceil)) { |
9e2326c7 | 1115 | return 0; |
7ec30aaa MH |
1116 | } else if (*empty_entry_found) { |
1117 | /* Keep entry but do not canonicalize it */ | |
1118 | return 1; | |
1119 | } else { | |
ce83eadd | 1120 | char *real_path = real_pathdup(ceil, 0); |
4ac9006f | 1121 | if (!real_path) { |
7ec30aaa | 1122 | return 0; |
4ac9006f | 1123 | } |
7ec30aaa | 1124 | free(item->string); |
4ac9006f | 1125 | item->string = real_path; |
7ec30aaa MH |
1126 | return 1; |
1127 | } | |
9e2326c7 MH |
1128 | } |
1129 | ||
8959555c JS |
1130 | struct safe_directory_data { |
1131 | const char *path; | |
1132 | int is_safe; | |
1133 | }; | |
1134 | ||
a4e7e317 GC |
1135 | static int safe_directory_cb(const char *key, const char *value, |
1136 | const struct config_context *ctx UNUSED, void *d) | |
8959555c JS |
1137 | { |
1138 | struct safe_directory_data *data = d; | |
1139 | ||
bb50ec3c MV |
1140 | if (strcmp(key, "safe.directory")) |
1141 | return 0; | |
1142 | ||
0f85c4a3 | 1143 | if (!value || !*value) { |
8959555c | 1144 | data->is_safe = 0; |
0f85c4a3 DS |
1145 | } else if (!strcmp(value, "*")) { |
1146 | data->is_safe = 1; | |
1147 | } else { | |
8959555c JS |
1148 | const char *interpolated = NULL; |
1149 | ||
1150 | if (!git_config_pathname(&interpolated, key, value) && | |
1151 | !fspathcmp(data->path, interpolated ? interpolated : value)) | |
1152 | data->is_safe = 1; | |
1153 | ||
1154 | free((char *)interpolated); | |
1155 | } | |
1156 | ||
1157 | return 0; | |
1158 | } | |
1159 | ||
3b0bf270 CMAB |
1160 | /* |
1161 | * Check if a repository is safe, by verifying the ownership of the | |
1162 | * worktree (if any), the git directory, and the gitfile (if any). | |
1163 | * | |
1164 | * Exemptions for known-safe repositories can be added via `safe.directory` | |
1165 | * config settings; for non-bare repositories, their worktree needs to be | |
1166 | * added, for bare ones their git directory. | |
1167 | */ | |
1168 | static int ensure_valid_ownership(const char *gitfile, | |
17d3883f JS |
1169 | const char *worktree, const char *gitdir, |
1170 | struct strbuf *report) | |
8959555c | 1171 | { |
3b0bf270 CMAB |
1172 | struct safe_directory_data data = { |
1173 | .path = worktree ? worktree : gitdir | |
1174 | }; | |
8959555c | 1175 | |
e47363e5 | 1176 | if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) && |
17d3883f JS |
1177 | (!gitfile || is_path_owned_by_current_user(gitfile, report)) && |
1178 | (!worktree || is_path_owned_by_current_user(worktree, report)) && | |
1179 | (!gitdir || is_path_owned_by_current_user(gitdir, report))) | |
8959555c JS |
1180 | return 1; |
1181 | ||
3b0bf270 CMAB |
1182 | /* |
1183 | * data.path is the "path" that identifies the repository and it is | |
1184 | * constant regardless of what failed above. data.is_safe should be | |
1185 | * initialized to false, and might be changed by the callback. | |
1186 | */ | |
6061601d | 1187 | git_protected_config(safe_directory_cb, &data); |
8959555c JS |
1188 | |
1189 | return data.is_safe; | |
1190 | } | |
1191 | ||
a4e7e317 GC |
1192 | static int allowed_bare_repo_cb(const char *key, const char *value, |
1193 | const struct config_context *ctx UNUSED, | |
1194 | void *d) | |
8d1a7448 GC |
1195 | { |
1196 | enum allowed_bare_repo *allowed_bare_repo = d; | |
1197 | ||
1198 | if (strcasecmp(key, "safe.bareRepository")) | |
1199 | return 0; | |
1200 | ||
1201 | if (!strcmp(value, "explicit")) { | |
1202 | *allowed_bare_repo = ALLOWED_BARE_REPO_EXPLICIT; | |
1203 | return 0; | |
1204 | } | |
1205 | if (!strcmp(value, "all")) { | |
1206 | *allowed_bare_repo = ALLOWED_BARE_REPO_ALL; | |
1207 | return 0; | |
1208 | } | |
1209 | return -1; | |
1210 | } | |
1211 | ||
1212 | static enum allowed_bare_repo get_allowed_bare_repo(void) | |
1213 | { | |
1214 | enum allowed_bare_repo result = ALLOWED_BARE_REPO_ALL; | |
1215 | git_protected_config(allowed_bare_repo_cb, &result); | |
1216 | return result; | |
1217 | } | |
1218 | ||
1219 | static const char *allowed_bare_repo_to_string( | |
1220 | enum allowed_bare_repo allowed_bare_repo) | |
1221 | { | |
1222 | switch (allowed_bare_repo) { | |
1223 | case ALLOWED_BARE_REPO_EXPLICIT: | |
1224 | return "explicit"; | |
1225 | case ALLOWED_BARE_REPO_ALL: | |
1226 | return "all"; | |
1227 | default: | |
1228 | BUG("invalid allowed_bare_repo %d", | |
1229 | allowed_bare_repo); | |
1230 | } | |
1231 | return NULL; | |
1232 | } | |
1233 | ||
e90fdc39 JS |
1234 | /* |
1235 | * We cannot decide in this function whether we are in the work tree or | |
1236 | * not, since the config can only be read _after_ this function was called. | |
ce9b8aab JS |
1237 | * |
1238 | * Also, we avoid changing any global state (such as the current working | |
1239 | * directory) to allow early callers. | |
1240 | * | |
1241 | * The directory where the search should start needs to be passed in via the | |
1242 | * `dir` parameter; upon return, the `dir` buffer will contain the path of | |
1243 | * the directory where the search ended, and `gitdir` will contain the path of | |
1244 | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it | |
1245 | * is relative to `dir` (i.e. *not* necessarily the cwd). | |
e90fdc39 | 1246 | */ |
ce9b8aab | 1247 | static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, |
01017dce | 1248 | struct strbuf *gitdir, |
17d3883f | 1249 | struct strbuf *report, |
01017dce | 1250 | int die_on_error) |
d288a700 | 1251 | { |
0454dd93 | 1252 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
31171d9e | 1253 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
ce9b8aab | 1254 | const char *gitdirenv; |
d17f2124 | 1255 | int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); |
c7d1d1b1 RH |
1256 | dev_t current_device = 0; |
1257 | int one_filesystem = 1; | |
d288a700 | 1258 | |
e90fdc39 JS |
1259 | /* |
1260 | * If GIT_DIR is set explicitly, we're not going | |
1261 | * to do any discovery, but we still do repository | |
1262 | * validation. | |
1263 | */ | |
ad1a382f | 1264 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
ce9b8aab JS |
1265 | if (gitdirenv) { |
1266 | strbuf_addstr(gitdir, gitdirenv); | |
1267 | return GIT_DIR_EXPLICIT; | |
1268 | } | |
d288a700 | 1269 | |
31171d9e | 1270 | if (env_ceiling_dirs) { |
7ec30aaa MH |
1271 | int empty_entry_found = 0; |
1272 | ||
31171d9e | 1273 | string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); |
1b77d83c | 1274 | filter_string_list(&ceiling_dirs, 0, |
7ec30aaa | 1275 | canonicalize_ceiling_entry, &empty_entry_found); |
ce9b8aab | 1276 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
31171d9e MH |
1277 | string_list_clear(&ceiling_dirs, 0); |
1278 | } | |
1279 | ||
ce9b8aab JS |
1280 | if (ceil_offset < 0) |
1281 | ceil_offset = min_offset - 2; | |
d288a700 | 1282 | |
e2683d51 JS |
1283 | if (min_offset && min_offset == dir->len && |
1284 | !is_dir_sep(dir->buf[min_offset - 1])) { | |
1285 | strbuf_addch(dir, '/'); | |
1286 | min_offset++; | |
1287 | } | |
1288 | ||
892c41b9 | 1289 | /* |
ce9b8aab | 1290 | * Test in the following order (relative to the dir): |
b44ebb19 | 1291 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
1292 | * - .git/ |
1293 | * - ./ (bare) | |
b44ebb19 | 1294 | * - ../.git |
e90fdc39 JS |
1295 | * - ../.git/ |
1296 | * - ../ (bare) | |
176b2d32 | 1297 | * - ../../.git |
e90fdc39 | 1298 | * etc. |
892c41b9 | 1299 | */ |
cf87463e | 1300 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
60c98d1e | 1301 | if (one_filesystem) |
ce9b8aab | 1302 | current_device = get_device_or_die(dir->buf, NULL, 0); |
e90fdc39 | 1303 | for (;;) { |
01017dce | 1304 | int offset = dir->len, error_code = 0; |
3b0bf270 CMAB |
1305 | char *gitdir_path = NULL; |
1306 | char *gitfile = NULL; | |
ce9b8aab JS |
1307 | |
1308 | if (offset > min_offset) | |
1309 | strbuf_addch(dir, '/'); | |
1310 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); | |
01017dce JS |
1311 | gitdirenv = read_gitfile_gently(dir->buf, die_on_error ? |
1312 | NULL : &error_code); | |
1313 | if (!gitdirenv) { | |
1314 | if (die_on_error || | |
1315 | error_code == READ_GITFILE_ERR_NOT_A_FILE) { | |
5c4003ca | 1316 | /* NEEDSWORK: fail if .git is not file nor dir */ |
3b0bf270 | 1317 | if (is_git_directory(dir->buf)) { |
01017dce | 1318 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
3b0bf270 CMAB |
1319 | gitdir_path = xstrdup(dir->buf); |
1320 | } | |
01017dce JS |
1321 | } else if (error_code != READ_GITFILE_ERR_STAT_FAILED) |
1322 | return GIT_DIR_INVALID_GITFILE; | |
3b0bf270 CMAB |
1323 | } else |
1324 | gitfile = xstrdup(dir->buf); | |
1325 | /* | |
1326 | * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT | |
1327 | * to check that directory for a repository. | |
1328 | * Now trim that tentative addition away, because we want to | |
1329 | * focus on the real directory we are in. | |
1330 | */ | |
ce9b8aab | 1331 | strbuf_setlen(dir, offset); |
9951d3b3 | 1332 | if (gitdirenv) { |
3b0bf270 | 1333 | enum discovery_result ret; |
d51e1dff JS |
1334 | const char *gitdir_candidate = |
1335 | gitdir_path ? gitdir_path : gitdirenv; | |
3b0bf270 | 1336 | |
d51e1dff | 1337 | if (ensure_valid_ownership(gitfile, dir->buf, |
17d3883f | 1338 | gitdir_candidate, report)) { |
3b0bf270 CMAB |
1339 | strbuf_addstr(gitdir, gitdirenv); |
1340 | ret = GIT_DIR_DISCOVERED; | |
1341 | } else | |
1342 | ret = GIT_DIR_INVALID_OWNERSHIP; | |
1343 | ||
1344 | /* | |
1345 | * Earlier, during discovery, we might have allocated | |
1346 | * string copies for gitdir_path or gitfile so make | |
1347 | * sure we don't leak by freeing them now, before | |
1348 | * leaving the loop and function. | |
1349 | * | |
1350 | * Note: gitdirenv will be non-NULL whenever these are | |
1351 | * allocated, therefore we need not take care of releasing | |
1352 | * them outside of this conditional block. | |
1353 | */ | |
1354 | free(gitdir_path); | |
1355 | free(gitfile); | |
1356 | ||
1357 | return ret; | |
9951d3b3 | 1358 | } |
9951d3b3 | 1359 | |
ce9b8aab | 1360 | if (is_git_directory(dir->buf)) { |
e35f202b | 1361 | trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf); |
8d1a7448 GC |
1362 | if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT) |
1363 | return GIT_DIR_DISALLOWED_BARE; | |
17d3883f | 1364 | if (!ensure_valid_ownership(NULL, NULL, dir->buf, report)) |
8959555c | 1365 | return GIT_DIR_INVALID_OWNERSHIP; |
ce9b8aab JS |
1366 | strbuf_addstr(gitdir, "."); |
1367 | return GIT_DIR_BARE; | |
502ffe34 | 1368 | } |
9951d3b3 | 1369 | |
ce9b8aab JS |
1370 | if (offset <= min_offset) |
1371 | return GIT_DIR_HIT_CEILING; | |
1cd8031b | 1372 | |
ce9b8aab | 1373 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
6c1e6544 | 1374 | ; /* continue */ |
ce9b8aab JS |
1375 | if (offset <= ceil_offset) |
1376 | return GIT_DIR_HIT_CEILING; | |
1377 | ||
1378 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); | |
1379 | if (one_filesystem && | |
1380 | current_device != get_device_or_die(dir->buf, NULL, offset)) | |
1381 | return GIT_DIR_HIT_MOUNT_POINT; | |
892c41b9 | 1382 | } |
d288a700 | 1383 | } |
5e7bfe25 | 1384 | |
26ae8da6 DS |
1385 | enum discovery_result discover_git_directory_reason(struct strbuf *commondir, |
1386 | struct strbuf *gitdir) | |
16ac8b8d JS |
1387 | { |
1388 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; | |
1389 | size_t gitdir_offset = gitdir->len, cwd_len; | |
d3fb71b3 | 1390 | size_t commondir_offset = commondir->len; |
e8805af1 | 1391 | struct repository_format candidate = REPOSITORY_FORMAT_INIT; |
26ae8da6 | 1392 | enum discovery_result result; |
16ac8b8d JS |
1393 | |
1394 | if (strbuf_getcwd(&dir)) | |
26ae8da6 | 1395 | return GIT_DIR_CWD_FAILURE; |
16ac8b8d JS |
1396 | |
1397 | cwd_len = dir.len; | |
26ae8da6 DS |
1398 | result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0); |
1399 | if (result <= 0) { | |
16ac8b8d | 1400 | strbuf_release(&dir); |
26ae8da6 | 1401 | return result; |
16ac8b8d JS |
1402 | } |
1403 | ||
1404 | /* | |
1405 | * The returned gitdir is relative to dir, and if dir does not reflect | |
1406 | * the current working directory, we simply make the gitdir absolute. | |
1407 | */ | |
1408 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { | |
1409 | /* Avoid a trailing "/." */ | |
1410 | if (!strcmp(".", gitdir->buf + gitdir_offset)) | |
1411 | strbuf_setlen(gitdir, gitdir_offset); | |
1412 | else | |
1413 | strbuf_addch(&dir, '/'); | |
1414 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); | |
1415 | } | |
1416 | ||
d3fb71b3 BW |
1417 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
1418 | ||
16ac8b8d | 1419 | strbuf_reset(&dir); |
d3fb71b3 | 1420 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
16ac8b8d JS |
1421 | read_repository_format(&candidate, dir.buf); |
1422 | strbuf_release(&dir); | |
1423 | ||
1424 | if (verify_repository_format(&candidate, &err) < 0) { | |
1425 | warning("ignoring git dir '%s': %s", | |
1426 | gitdir->buf + gitdir_offset, err.buf); | |
1427 | strbuf_release(&err); | |
d3fb71b3 | 1428 | strbuf_setlen(commondir, commondir_offset); |
69743f9b | 1429 | strbuf_setlen(gitdir, gitdir_offset); |
e8805af1 | 1430 | clear_repository_format(&candidate); |
26ae8da6 | 1431 | return GIT_DIR_INVALID_FORMAT; |
16ac8b8d JS |
1432 | } |
1433 | ||
e8805af1 | 1434 | clear_repository_format(&candidate); |
26ae8da6 | 1435 | return result; |
16ac8b8d JS |
1436 | } |
1437 | ||
a60645f9 NTND |
1438 | const char *setup_git_directory_gently(int *nongit_ok) |
1439 | { | |
ce9b8aab | 1440 | static struct strbuf cwd = STRBUF_INIT; |
17d3883f | 1441 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT; |
07098b81 | 1442 | const char *prefix = NULL; |
e8805af1 | 1443 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
a60645f9 | 1444 | |
ce9b8aab JS |
1445 | /* |
1446 | * We may have read an incomplete configuration before | |
1447 | * setting-up the git directory. If so, clear the cache so | |
1448 | * that the next queries to the configuration reload complete | |
1449 | * configuration (including the per-repo config file that we | |
1450 | * ignored previously). | |
1451 | */ | |
1452 | git_config_clear(); | |
1453 | ||
1454 | /* | |
1455 | * Let's assume that we are in a git repository. | |
1456 | * If it turns out later that we are somewhere else, the value will be | |
1457 | * updated accordingly. | |
1458 | */ | |
1459 | if (nongit_ok) | |
1460 | *nongit_ok = 0; | |
1461 | ||
1462 | if (strbuf_getcwd(&cwd)) | |
1463 | die_errno(_("Unable to read current working directory")); | |
1464 | strbuf_addbuf(&dir, &cwd); | |
1465 | ||
17d3883f | 1466 | switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) { |
ce9b8aab | 1467 | case GIT_DIR_EXPLICIT: |
abade65b | 1468 | prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1469 | break; |
1470 | case GIT_DIR_DISCOVERED: | |
1471 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1472 | die(_("cannot change to '%s'"), dir.buf); |
ce9b8aab | 1473 | prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len, |
abade65b | 1474 | &repo_fmt, nongit_ok); |
ce9b8aab JS |
1475 | break; |
1476 | case GIT_DIR_BARE: | |
1477 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1478 | die(_("cannot change to '%s'"), dir.buf); |
abade65b | 1479 | prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1480 | break; |
1481 | case GIT_DIR_HIT_CEILING: | |
07098b81 ED |
1482 | if (!nongit_ok) |
1483 | die(_("not a git repository (or any of the parent directories): %s"), | |
1484 | DEFAULT_GIT_DIR_ENVIRONMENT); | |
1485 | *nongit_ok = 1; | |
ce9b8aab JS |
1486 | break; |
1487 | case GIT_DIR_HIT_MOUNT_POINT: | |
07098b81 ED |
1488 | if (!nongit_ok) |
1489 | die(_("not a git repository (or any parent up to mount point %s)\n" | |
1490 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), | |
1491 | dir.buf); | |
1492 | *nongit_ok = 1; | |
1493 | break; | |
8959555c JS |
1494 | case GIT_DIR_INVALID_OWNERSHIP: |
1495 | if (!nongit_ok) { | |
1496 | struct strbuf quoted = STRBUF_INIT; | |
1497 | ||
17d3883f | 1498 | strbuf_complete(&report, '\n'); |
8959555c | 1499 | sq_quote_buf_pretty("ed, dir.buf); |
3b0bf270 | 1500 | die(_("detected dubious ownership in repository at '%s'\n" |
17d3883f | 1501 | "%s" |
8959555c JS |
1502 | "To add an exception for this directory, call:\n" |
1503 | "\n" | |
1504 | "\tgit config --global --add safe.directory %s"), | |
17d3883f | 1505 | dir.buf, report.buf, quoted.buf); |
8959555c JS |
1506 | } |
1507 | *nongit_ok = 1; | |
1508 | break; | |
8d1a7448 GC |
1509 | case GIT_DIR_DISALLOWED_BARE: |
1510 | if (!nongit_ok) { | |
1511 | die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"), | |
1512 | dir.buf, | |
1513 | allowed_bare_repo_to_string(get_allowed_bare_repo())); | |
1514 | } | |
1515 | *nongit_ok = 1; | |
1516 | break; | |
26ae8da6 DS |
1517 | case GIT_DIR_CWD_FAILURE: |
1518 | case GIT_DIR_INVALID_FORMAT: | |
07098b81 ED |
1519 | /* |
1520 | * As a safeguard against setup_git_directory_gently_1 returning | |
26ae8da6 | 1521 | * these values, fallthrough to BUG. Otherwise it is possible to |
07098b81 ED |
1522 | * set startup_info->have_repository to 1 when we did nothing to |
1523 | * find a repository. | |
1524 | */ | |
ce9b8aab | 1525 | default: |
a3ba4fa7 | 1526 | BUG("unhandled setup_git_directory_gently_1() result"); |
ce9b8aab JS |
1527 | } |
1528 | ||
07098b81 ED |
1529 | /* |
1530 | * At this point, nongit_ok is stable. If it is non-NULL and points | |
1531 | * to a non-zero value, then this means that we haven't found a | |
1532 | * repository and that the caller expects startup_info to reflect | |
1533 | * this. | |
1534 | * | |
1535 | * Regardless of the state of nongit_ok, startup_info->prefix and | |
1536 | * the GIT_PREFIX environment variable must always match. For details | |
1537 | * see Documentation/config/alias.txt. | |
1538 | */ | |
c7d0e610 | 1539 | if (nongit_ok && *nongit_ok) |
07098b81 | 1540 | startup_info->have_repository = 0; |
c7d0e610 | 1541 | else |
07098b81 | 1542 | startup_info->have_repository = 1; |
46c3cd44 | 1543 | |
73f192c9 BW |
1544 | /* |
1545 | * Not all paths through the setup code will call 'set_git_dir()' (which | |
1546 | * directly sets up the environment) so in order to guarantee that the | |
1547 | * environment is in a consistent state after setup, explicitly setup | |
1548 | * the environment if we have a repository. | |
1549 | * | |
1550 | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some | |
1551 | * code paths so we also need to explicitly setup the environment if | |
1552 | * the user has set GIT_DIR. It may be beneficial to disallow bogus | |
1553 | * GIT_DIR values at some point in the future. | |
1554 | */ | |
07098b81 ED |
1555 | if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */ |
1556 | startup_info->have_repository || | |
1557 | /* GIT_DIR_EXPLICIT */ | |
1558 | getenv(GIT_DIR_ENVIRONMENT)) { | |
c14c234f BW |
1559 | if (!the_repository->gitdir) { |
1560 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); | |
1561 | if (!gitdir) | |
1562 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; | |
357a03eb | 1563 | setup_git_env(gitdir); |
c14c234f | 1564 | } |
ebaf3bcf | 1565 | if (startup_info->have_repository) { |
78a67668 | 1566 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); |
3867f6d6 VD |
1567 | the_repository->repository_format_worktree_config = |
1568 | repo_fmt.worktree_config; | |
ebaf3bcf JT |
1569 | /* take ownership of repo_fmt.partial_clone */ |
1570 | the_repository->repository_format_partial_clone = | |
1571 | repo_fmt.partial_clone; | |
1572 | repo_fmt.partial_clone = NULL; | |
1573 | } | |
c14c234f | 1574 | } |
c7d0e610 TB |
1575 | /* |
1576 | * Since precompose_string_if_needed() needs to look at | |
1577 | * the core.precomposeunicode configuration, this | |
1578 | * has to happen after the above block that finds | |
1579 | * out where the repository is, i.e. a preparation | |
1580 | * for calling git_config_get_bool(). | |
1581 | */ | |
1582 | if (prefix) { | |
1583 | prefix = precompose_string_if_needed(prefix); | |
1584 | startup_info->prefix = prefix; | |
1585 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); | |
1586 | } else { | |
1587 | startup_info->prefix = NULL; | |
1588 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); | |
1589 | } | |
1590 | ||
e6f8861b | 1591 | setup_original_cwd(); |
73f192c9 | 1592 | |
ce9b8aab JS |
1593 | strbuf_release(&dir); |
1594 | strbuf_release(&gitdir); | |
17d3883f | 1595 | strbuf_release(&report); |
e8805af1 | 1596 | clear_repository_format(&repo_fmt); |
ce9b8aab | 1597 | |
a60645f9 NTND |
1598 | return prefix; |
1599 | } | |
1600 | ||
94df2506 JH |
1601 | int git_config_perm(const char *var, const char *value) |
1602 | { | |
06cbe855 HO |
1603 | int i; |
1604 | char *endptr; | |
1605 | ||
afe8a907 | 1606 | if (!value) |
06cbe855 HO |
1607 | return PERM_GROUP; |
1608 | ||
1609 | if (!strcmp(value, "umask")) | |
1610 | return PERM_UMASK; | |
1611 | if (!strcmp(value, "group")) | |
1612 | return PERM_GROUP; | |
1613 | if (!strcmp(value, "all") || | |
1614 | !strcmp(value, "world") || | |
1615 | !strcmp(value, "everybody")) | |
1616 | return PERM_EVERYBODY; | |
1617 | ||
1618 | /* Parse octal numbers */ | |
1619 | i = strtol(value, &endptr, 8); | |
1620 | ||
1621 | /* If not an octal number, maybe true/false? */ | |
1622 | if (*endptr != 0) | |
1623 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
1624 | ||
1625 | /* | |
1626 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 1627 | * a chmod value to restrict to. |
06cbe855 HO |
1628 | */ |
1629 | switch (i) { | |
1630 | case PERM_UMASK: /* 0 */ | |
1631 | return PERM_UMASK; | |
1632 | case OLD_PERM_GROUP: /* 1 */ | |
1633 | return PERM_GROUP; | |
1634 | case OLD_PERM_EVERYBODY: /* 2 */ | |
1635 | return PERM_EVERYBODY; | |
94df2506 | 1636 | } |
06cbe855 HO |
1637 | |
1638 | /* A filemode value was given: 0xxx */ | |
1639 | ||
1640 | if ((i & 0600) != 0600) | |
fc045fe7 | 1641 | die(_("problem with core.sharedRepository filemode value " |
06cbe855 | 1642 | "(0%.3o).\nThe owner of files must always have " |
2ff30e67 | 1643 | "read and write permissions."), i); |
06cbe855 HO |
1644 | |
1645 | /* | |
1646 | * Mask filemode value. Others can not get write permission. | |
1647 | * x flags for directories are handled separately. | |
1648 | */ | |
5a688fe4 | 1649 | return -(i & 0666); |
94df2506 JH |
1650 | } |
1651 | ||
cfe3917c | 1652 | void check_repository_format(struct repository_format *fmt) |
ab9cb76f | 1653 | { |
e8805af1 | 1654 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
cfe3917c | 1655 | if (!fmt) |
1656 | fmt = &repo_fmt; | |
1657 | check_repository_format_gently(get_git_dir(), fmt, NULL); | |
f1c126bd | 1658 | startup_info->have_repository = 1; |
d553aceb | 1659 | repo_set_hash_algo(the_repository, fmt->hash_algo); |
3867f6d6 VD |
1660 | the_repository->repository_format_worktree_config = |
1661 | fmt->worktree_config; | |
ebaf3bcf JT |
1662 | the_repository->repository_format_partial_clone = |
1663 | xstrdup_or_null(fmt->partial_clone); | |
e8805af1 | 1664 | clear_repository_format(&repo_fmt); |
ab9cb76f JH |
1665 | } |
1666 | ||
e1e5ec86 CB |
1667 | /* |
1668 | * Returns the "prefix", a path to the current working directory | |
1669 | * relative to the work tree root, or NULL, if the current working | |
1670 | * directory is not a strict subdirectory of the work tree root. The | |
1671 | * prefix always ends with a '/' character. | |
1672 | */ | |
5e7bfe25 JH |
1673 | const char *setup_git_directory(void) |
1674 | { | |
b3f66fd3 | 1675 | return setup_git_directory_gently(NULL); |
5e7bfe25 | 1676 | } |
abc06822 | 1677 | |
40d96325 | 1678 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
abc06822 FG |
1679 | { |
1680 | if (is_git_directory(suspect)) | |
1681 | return suspect; | |
40d96325 | 1682 | return read_gitfile_gently(suspect, return_error_code); |
abc06822 | 1683 | } |
1d999ddd TR |
1684 | |
1685 | /* if any standard file descriptor is missing open it to /dev/null */ | |
1686 | void sanitize_stdfds(void) | |
1687 | { | |
d9a65b6c RS |
1688 | int fd = xopen("/dev/null", O_RDWR); |
1689 | while (fd < 2) | |
1690 | fd = xdup(fd); | |
1d999ddd TR |
1691 | if (fd > 2) |
1692 | close(fd); | |
1693 | } | |
de0957ce NTND |
1694 | |
1695 | int daemonize(void) | |
1696 | { | |
1697 | #ifdef NO_POSIX_GOODIES | |
1698 | errno = ENOSYS; | |
1699 | return -1; | |
1700 | #else | |
1701 | switch (fork()) { | |
1702 | case 0: | |
1703 | break; | |
1704 | case -1: | |
fc045fe7 | 1705 | die_errno(_("fork failed")); |
de0957ce NTND |
1706 | default: |
1707 | exit(0); | |
1708 | } | |
1709 | if (setsid() == -1) | |
fc045fe7 | 1710 | die_errno(_("setsid failed")); |
de0957ce NTND |
1711 | close(0); |
1712 | close(1); | |
1713 | close(2); | |
1714 | sanitize_stdfds(); | |
1715 | return 0; | |
1716 | #endif | |
1717 | } | |
e8cf8ef5 EN |
1718 | |
1719 | #ifdef NO_TRUSTABLE_FILEMODE | |
1720 | #define TEST_FILEMODE 0 | |
1721 | #else | |
1722 | #define TEST_FILEMODE 1 | |
1723 | #endif | |
1724 | ||
1725 | #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH" | |
1726 | ||
1727 | static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, | |
1728 | DIR *dir) | |
1729 | { | |
1730 | size_t path_baselen = path->len; | |
1731 | size_t template_baselen = template_path->len; | |
1732 | struct dirent *de; | |
1733 | ||
1734 | /* Note: if ".git/hooks" file exists in the repository being | |
1735 | * re-initialized, /etc/core-git/templates/hooks/update would | |
1736 | * cause "git init" to fail here. I think this is sane but | |
1737 | * it means that the set of templates we ship by default, along | |
1738 | * with the way the namespace under .git/ is organized, should | |
1739 | * be really carefully chosen. | |
1740 | */ | |
1741 | safe_create_dir(path->buf, 1); | |
1742 | while ((de = readdir(dir)) != NULL) { | |
1743 | struct stat st_git, st_template; | |
1744 | int exists = 0; | |
1745 | ||
1746 | strbuf_setlen(path, path_baselen); | |
1747 | strbuf_setlen(template_path, template_baselen); | |
1748 | ||
1749 | if (de->d_name[0] == '.') | |
1750 | continue; | |
1751 | strbuf_addstr(path, de->d_name); | |
1752 | strbuf_addstr(template_path, de->d_name); | |
1753 | if (lstat(path->buf, &st_git)) { | |
1754 | if (errno != ENOENT) | |
1755 | die_errno(_("cannot stat '%s'"), path->buf); | |
1756 | } | |
1757 | else | |
1758 | exists = 1; | |
1759 | ||
1760 | if (lstat(template_path->buf, &st_template)) | |
1761 | die_errno(_("cannot stat template '%s'"), template_path->buf); | |
1762 | ||
1763 | if (S_ISDIR(st_template.st_mode)) { | |
1764 | DIR *subdir = opendir(template_path->buf); | |
1765 | if (!subdir) | |
1766 | die_errno(_("cannot opendir '%s'"), template_path->buf); | |
1767 | strbuf_addch(path, '/'); | |
1768 | strbuf_addch(template_path, '/'); | |
1769 | copy_templates_1(path, template_path, subdir); | |
1770 | closedir(subdir); | |
1771 | } | |
1772 | else if (exists) | |
1773 | continue; | |
1774 | else if (S_ISLNK(st_template.st_mode)) { | |
1775 | struct strbuf lnk = STRBUF_INIT; | |
1776 | if (strbuf_readlink(&lnk, template_path->buf, | |
1777 | st_template.st_size) < 0) | |
1778 | die_errno(_("cannot readlink '%s'"), template_path->buf); | |
1779 | if (symlink(lnk.buf, path->buf)) | |
1780 | die_errno(_("cannot symlink '%s' '%s'"), | |
1781 | lnk.buf, path->buf); | |
1782 | strbuf_release(&lnk); | |
1783 | } | |
1784 | else if (S_ISREG(st_template.st_mode)) { | |
1785 | if (copy_file(path->buf, template_path->buf, st_template.st_mode)) | |
1786 | die_errno(_("cannot copy '%s' to '%s'"), | |
1787 | template_path->buf, path->buf); | |
1788 | } | |
1789 | else | |
1790 | error(_("ignoring template %s"), template_path->buf); | |
1791 | } | |
1792 | } | |
1793 | ||
1794 | static void copy_templates(const char *template_dir, const char *init_template_dir) | |
1795 | { | |
1796 | struct strbuf path = STRBUF_INIT; | |
1797 | struct strbuf template_path = STRBUF_INIT; | |
1798 | size_t template_len; | |
1799 | struct repository_format template_format = REPOSITORY_FORMAT_INIT; | |
1800 | struct strbuf err = STRBUF_INIT; | |
1801 | DIR *dir; | |
1802 | char *to_free = NULL; | |
1803 | ||
1804 | if (!template_dir) | |
1805 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); | |
1806 | if (!template_dir) | |
1807 | template_dir = init_template_dir; | |
1808 | if (!template_dir) | |
1809 | template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR); | |
1810 | if (!template_dir[0]) { | |
1811 | free(to_free); | |
1812 | return; | |
1813 | } | |
1814 | ||
1815 | strbuf_addstr(&template_path, template_dir); | |
1816 | strbuf_complete(&template_path, '/'); | |
1817 | template_len = template_path.len; | |
1818 | ||
1819 | dir = opendir(template_path.buf); | |
1820 | if (!dir) { | |
1821 | warning(_("templates not found in %s"), template_dir); | |
1822 | goto free_return; | |
1823 | } | |
1824 | ||
1825 | /* Make sure that template is from the correct vintage */ | |
1826 | strbuf_addstr(&template_path, "config"); | |
1827 | read_repository_format(&template_format, template_path.buf); | |
1828 | strbuf_setlen(&template_path, template_len); | |
1829 | ||
1830 | /* | |
1831 | * No mention of version at all is OK, but anything else should be | |
1832 | * verified. | |
1833 | */ | |
1834 | if (template_format.version >= 0 && | |
1835 | verify_repository_format(&template_format, &err) < 0) { | |
1836 | warning(_("not copying templates from '%s': %s"), | |
1837 | template_dir, err.buf); | |
1838 | strbuf_release(&err); | |
1839 | goto close_free_return; | |
1840 | } | |
1841 | ||
1842 | strbuf_addstr(&path, get_git_common_dir()); | |
1843 | strbuf_complete(&path, '/'); | |
1844 | copy_templates_1(&path, &template_path, dir); | |
1845 | close_free_return: | |
1846 | closedir(dir); | |
1847 | free_return: | |
1848 | free(to_free); | |
1849 | strbuf_release(&path); | |
1850 | strbuf_release(&template_path); | |
1851 | clear_repository_format(&template_format); | |
1852 | } | |
1853 | ||
1854 | /* | |
1855 | * If the git_dir is not directly inside the working tree, then git will not | |
1856 | * find it by default, and we need to set the worktree explicitly. | |
1857 | */ | |
1858 | static int needs_work_tree_config(const char *git_dir, const char *work_tree) | |
1859 | { | |
1860 | if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git")) | |
1861 | return 0; | |
1862 | if (skip_prefix(git_dir, work_tree, &git_dir) && | |
1863 | !strcmp(git_dir, "/.git")) | |
1864 | return 0; | |
1865 | return 1; | |
1866 | } | |
1867 | ||
1868 | void initialize_repository_version(int hash_algo, int reinit) | |
1869 | { | |
1870 | char repo_version_string[10]; | |
1871 | int repo_version = GIT_REPO_VERSION; | |
1872 | ||
1873 | if (hash_algo != GIT_HASH_SHA1) | |
1874 | repo_version = GIT_REPO_VERSION_READ; | |
1875 | ||
1876 | /* This forces creation of new config file */ | |
1877 | xsnprintf(repo_version_string, sizeof(repo_version_string), | |
1878 | "%d", repo_version); | |
1879 | git_config_set("core.repositoryformatversion", repo_version_string); | |
1880 | ||
1881 | if (hash_algo != GIT_HASH_SHA1) | |
1882 | git_config_set("extensions.objectformat", | |
1883 | hash_algos[hash_algo].name); | |
1884 | else if (reinit) | |
1885 | git_config_set_gently("extensions.objectformat", NULL); | |
1886 | } | |
1887 | ||
1888 | static int create_default_files(const char *template_path, | |
1889 | const char *original_git_dir, | |
1890 | const char *initial_branch, | |
1891 | const struct repository_format *fmt, | |
1892 | int prev_bare_repository, | |
1893 | int init_shared_repository, | |
1894 | int quiet) | |
1895 | { | |
1896 | struct stat st1; | |
1897 | struct strbuf buf = STRBUF_INIT; | |
1898 | char *path; | |
1899 | char junk[2]; | |
1900 | int reinit; | |
1901 | int filemode; | |
1902 | struct strbuf err = STRBUF_INIT; | |
1903 | const char *init_template_dir = NULL; | |
1904 | const char *work_tree = get_git_work_tree(); | |
1905 | ||
1906 | /* | |
1907 | * First copy the templates -- we might have the default | |
1908 | * config file there, in which case we would want to read | |
1909 | * from it after installing. | |
1910 | * | |
1911 | * Before reading that config, we also need to clear out any cached | |
1912 | * values (since we've just potentially changed what's available on | |
1913 | * disk). | |
1914 | */ | |
1915 | git_config_get_pathname("init.templatedir", &init_template_dir); | |
1916 | copy_templates(template_path, init_template_dir); | |
1917 | free((char *)init_template_dir); | |
1918 | git_config_clear(); | |
1919 | reset_shared_repository(); | |
1920 | git_config(git_default_config, NULL); | |
1921 | ||
1922 | /* | |
1923 | * We must make sure command-line options continue to override any | |
1924 | * values we might have just re-read from the config. | |
1925 | */ | |
1926 | if (init_shared_repository != -1) | |
1927 | set_shared_repository(init_shared_repository); | |
1928 | /* | |
1929 | * TODO: heed core.bare from config file in templates if no | |
1930 | * command-line override given | |
1931 | */ | |
1932 | is_bare_repository_cfg = prev_bare_repository || !work_tree; | |
1933 | /* TODO (continued): | |
1934 | * | |
1935 | * Unfortunately, the line above is equivalent to | |
1936 | * is_bare_repository_cfg = !work_tree; | |
1937 | * which ignores the config entirely even if no `--[no-]bare` | |
1938 | * command line option was present. | |
1939 | * | |
1940 | * To see why, note that before this function, there was this call: | |
1941 | * prev_bare_repository = is_bare_repository() | |
1942 | * expanding the right hand side: | |
1943 | * = is_bare_repository_cfg && !get_git_work_tree() | |
1944 | * = is_bare_repository_cfg && !work_tree | |
1945 | * note that the last simplification above is valid because nothing | |
1946 | * calls repo_init() or set_git_work_tree() between any of the | |
1947 | * relevant calls in the code, and thus the !get_git_work_tree() | |
1948 | * calls will return the same result each time. So, what we are | |
1949 | * interested in computing is the right hand side of the line of | |
1950 | * code just above this comment: | |
1951 | * prev_bare_repository || !work_tree | |
1952 | * = is_bare_repository_cfg && !work_tree || !work_tree | |
1953 | * = !work_tree | |
1954 | * because "A && !B || !B == !B" for all boolean values of A & B. | |
1955 | */ | |
1956 | ||
1957 | /* | |
1958 | * We would have created the above under user's umask -- under | |
1959 | * shared-repository settings, we would need to fix them up. | |
1960 | */ | |
1961 | if (get_shared_repository()) { | |
1962 | adjust_shared_perm(get_git_dir()); | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * We need to create a "refs" dir in any case so that older | |
1967 | * versions of git can tell that this is a repository. | |
1968 | */ | |
1969 | safe_create_dir(git_path("refs"), 1); | |
1970 | adjust_shared_perm(git_path("refs")); | |
1971 | ||
1972 | if (refs_init_db(&err)) | |
1973 | die("failed to set up refs db: %s", err.buf); | |
1974 | ||
1975 | /* | |
1976 | * Point the HEAD symref to the initial branch with if HEAD does | |
1977 | * not yet exist. | |
1978 | */ | |
1979 | path = git_path_buf(&buf, "HEAD"); | |
1980 | reinit = (!access(path, R_OK) | |
1981 | || readlink(path, junk, sizeof(junk)-1) != -1); | |
1982 | if (!reinit) { | |
1983 | char *ref; | |
1984 | ||
1985 | if (!initial_branch) | |
1986 | initial_branch = git_default_branch_name(quiet); | |
1987 | ||
1988 | ref = xstrfmt("refs/heads/%s", initial_branch); | |
1989 | if (check_refname_format(ref, 0) < 0) | |
1990 | die(_("invalid initial branch name: '%s'"), | |
1991 | initial_branch); | |
1992 | ||
1993 | if (create_symref("HEAD", ref, NULL) < 0) | |
1994 | exit(1); | |
1995 | free(ref); | |
1996 | } | |
1997 | ||
1998 | initialize_repository_version(fmt->hash_algo, 0); | |
1999 | ||
2000 | /* Check filemode trustability */ | |
2001 | path = git_path_buf(&buf, "config"); | |
2002 | filemode = TEST_FILEMODE; | |
2003 | if (TEST_FILEMODE && !lstat(path, &st1)) { | |
2004 | struct stat st2; | |
2005 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && | |
2006 | !lstat(path, &st2) && | |
2007 | st1.st_mode != st2.st_mode && | |
2008 | !chmod(path, st1.st_mode)); | |
2009 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) | |
2010 | filemode = 0; | |
2011 | } | |
2012 | git_config_set("core.filemode", filemode ? "true" : "false"); | |
2013 | ||
2014 | if (is_bare_repository()) | |
2015 | git_config_set("core.bare", "true"); | |
2016 | else { | |
2017 | git_config_set("core.bare", "false"); | |
2018 | /* allow template config file to override the default */ | |
2019 | if (log_all_ref_updates == LOG_REFS_UNSET) | |
2020 | git_config_set("core.logallrefupdates", "true"); | |
2021 | if (needs_work_tree_config(original_git_dir, work_tree)) | |
2022 | git_config_set("core.worktree", work_tree); | |
2023 | } | |
2024 | ||
2025 | if (!reinit) { | |
2026 | /* Check if symlink is supported in the work tree */ | |
2027 | path = git_path_buf(&buf, "tXXXXXX"); | |
2028 | if (!close(xmkstemp(path)) && | |
2029 | !unlink(path) && | |
2030 | !symlink("testing", path) && | |
2031 | !lstat(path, &st1) && | |
2032 | S_ISLNK(st1.st_mode)) | |
2033 | unlink(path); /* good */ | |
2034 | else | |
2035 | git_config_set("core.symlinks", "false"); | |
2036 | ||
2037 | /* Check if the filesystem is case-insensitive */ | |
2038 | path = git_path_buf(&buf, "CoNfIg"); | |
2039 | if (!access(path, F_OK)) | |
2040 | git_config_set("core.ignorecase", "true"); | |
2041 | probe_utf8_pathname_composition(); | |
2042 | } | |
2043 | ||
2044 | strbuf_release(&buf); | |
2045 | return reinit; | |
2046 | } | |
2047 | ||
2048 | static void create_object_directory(void) | |
2049 | { | |
2050 | struct strbuf path = STRBUF_INIT; | |
2051 | size_t baselen; | |
2052 | ||
2053 | strbuf_addstr(&path, get_object_directory()); | |
2054 | baselen = path.len; | |
2055 | ||
2056 | safe_create_dir(path.buf, 1); | |
2057 | ||
2058 | strbuf_setlen(&path, baselen); | |
2059 | strbuf_addstr(&path, "/pack"); | |
2060 | safe_create_dir(path.buf, 1); | |
2061 | ||
2062 | strbuf_setlen(&path, baselen); | |
2063 | strbuf_addstr(&path, "/info"); | |
2064 | safe_create_dir(path.buf, 1); | |
2065 | ||
2066 | strbuf_release(&path); | |
2067 | } | |
2068 | ||
2069 | static void separate_git_dir(const char *git_dir, const char *git_link) | |
2070 | { | |
2071 | struct stat st; | |
2072 | ||
2073 | if (!stat(git_link, &st)) { | |
2074 | const char *src; | |
2075 | ||
2076 | if (S_ISREG(st.st_mode)) | |
2077 | src = read_gitfile(git_link); | |
2078 | else if (S_ISDIR(st.st_mode)) | |
2079 | src = git_link; | |
2080 | else | |
2081 | die(_("unable to handle file type %d"), (int)st.st_mode); | |
2082 | ||
2083 | if (rename(src, git_dir)) | |
2084 | die_errno(_("unable to move %s to %s"), src, git_dir); | |
2085 | repair_worktrees(NULL, NULL); | |
2086 | } | |
2087 | ||
2088 | write_file(git_link, "gitdir: %s", git_dir); | |
2089 | } | |
2090 | ||
2091 | static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash) | |
2092 | { | |
2093 | const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT); | |
2094 | /* | |
2095 | * If we already have an initialized repo, don't allow the user to | |
2096 | * specify a different algorithm, as that could cause corruption. | |
2097 | * Otherwise, if the user has specified one on the command line, use it. | |
2098 | */ | |
2099 | if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo) | |
2100 | die(_("attempt to reinitialize repository with different hash")); | |
2101 | else if (hash != GIT_HASH_UNKNOWN) | |
2102 | repo_fmt->hash_algo = hash; | |
2103 | else if (env) { | |
2104 | int env_algo = hash_algo_by_name(env); | |
2105 | if (env_algo == GIT_HASH_UNKNOWN) | |
2106 | die(_("unknown hash algorithm '%s'"), env); | |
2107 | repo_fmt->hash_algo = env_algo; | |
2108 | } | |
2109 | } | |
2110 | ||
2111 | int init_db(const char *git_dir, const char *real_git_dir, | |
2112 | const char *template_dir, int hash, const char *initial_branch, | |
2113 | int init_shared_repository, unsigned int flags) | |
2114 | { | |
2115 | int reinit; | |
2116 | int exist_ok = flags & INIT_DB_EXIST_OK; | |
2117 | char *original_git_dir = real_pathdup(git_dir, 1); | |
2118 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; | |
2119 | int prev_bare_repository; | |
2120 | ||
2121 | if (real_git_dir) { | |
2122 | struct stat st; | |
2123 | ||
2124 | if (!exist_ok && !stat(git_dir, &st)) | |
2125 | die(_("%s already exists"), git_dir); | |
2126 | ||
2127 | if (!exist_ok && !stat(real_git_dir, &st)) | |
2128 | die(_("%s already exists"), real_git_dir); | |
2129 | ||
2130 | set_git_dir(real_git_dir, 1); | |
2131 | git_dir = get_git_dir(); | |
2132 | separate_git_dir(git_dir, original_git_dir); | |
2133 | } | |
2134 | else { | |
2135 | set_git_dir(git_dir, 1); | |
2136 | git_dir = get_git_dir(); | |
2137 | } | |
2138 | startup_info->have_repository = 1; | |
2139 | ||
2140 | /* Ensure `core.hidedotfiles` is processed */ | |
2141 | git_config(platform_core_config, NULL); | |
2142 | ||
2143 | safe_create_dir(git_dir, 0); | |
2144 | ||
2145 | prev_bare_repository = is_bare_repository(); | |
2146 | ||
2147 | /* Check to see if the repository version is right. | |
2148 | * Note that a newly created repository does not have | |
2149 | * config file, so this will not fail. What we are catching | |
2150 | * is an attempt to reinitialize new repository with an old tool. | |
2151 | */ | |
2152 | check_repository_format(&repo_fmt); | |
2153 | ||
2154 | validate_hash_algorithm(&repo_fmt, hash); | |
2155 | ||
2156 | reinit = create_default_files(template_dir, original_git_dir, | |
2157 | initial_branch, &repo_fmt, | |
2158 | prev_bare_repository, | |
2159 | init_shared_repository, | |
2160 | flags & INIT_DB_QUIET); | |
2161 | if (reinit && initial_branch) | |
2162 | warning(_("re-init: ignored --initial-branch=%s"), | |
2163 | initial_branch); | |
2164 | ||
2165 | create_object_directory(); | |
2166 | ||
2167 | if (get_shared_repository()) { | |
2168 | char buf[10]; | |
2169 | /* We do not spell "group" and such, so that | |
2170 | * the configuration can be read by older version | |
2171 | * of git. Note, we use octal numbers for new share modes, | |
2172 | * and compatibility values for PERM_GROUP and | |
2173 | * PERM_EVERYBODY. | |
2174 | */ | |
2175 | if (get_shared_repository() < 0) | |
2176 | /* force to the mode value */ | |
2177 | xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository()); | |
2178 | else if (get_shared_repository() == PERM_GROUP) | |
2179 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); | |
2180 | else if (get_shared_repository() == PERM_EVERYBODY) | |
2181 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); | |
2182 | else | |
2183 | BUG("invalid value for shared_repository"); | |
2184 | git_config_set("core.sharedrepository", buf); | |
2185 | git_config_set("receive.denyNonFastforwards", "true"); | |
2186 | } | |
2187 | ||
2188 | if (!(flags & INIT_DB_QUIET)) { | |
2189 | int len = strlen(git_dir); | |
2190 | ||
2191 | if (reinit) | |
2192 | printf(get_shared_repository() | |
2193 | ? _("Reinitialized existing shared Git repository in %s%s\n") | |
2194 | : _("Reinitialized existing Git repository in %s%s\n"), | |
2195 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); | |
2196 | else | |
2197 | printf(get_shared_repository() | |
2198 | ? _("Initialized empty shared Git repository in %s%s\n") | |
2199 | : _("Initialized empty Git repository in %s%s\n"), | |
2200 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); | |
2201 | } | |
2202 | ||
9972cd60 | 2203 | clear_repository_format(&repo_fmt); |
e8cf8ef5 EN |
2204 | free(original_git_dir); |
2205 | return 0; | |
2206 | } |