]>
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" |
8959555c | 16 | #include "quote.h" |
74ea5c95 | 17 | #include "trace2.h" |
e8cf8ef5 | 18 | #include "worktree.h" |
df93e407 | 19 | #include "exec-cmd.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")) { | |
a6271269 JK |
562 | if (!value) |
563 | return config_error_nonbool(var); | |
ec91ffca JK |
564 | data->partial_clone = xstrdup(value); |
565 | return EXTENSION_OK; | |
566 | } else if (!strcmp(ext, "worktreeconfig")) { | |
567 | data->worktree_config = git_config_bool(var, value); | |
568 | return EXTENSION_OK; | |
569 | } | |
570 | ||
571 | return EXTENSION_UNKNOWN; | |
572 | } | |
573 | ||
574 | /* | |
575 | * Record any new extensions in this function. | |
576 | */ | |
577 | static enum extension_result handle_extension(const char *var, | |
578 | const char *value, | |
579 | const char *ext, | |
580 | struct repository_format *data) | |
581 | { | |
582 | if (!strcmp(ext, "noop-v1")) { | |
583 | return EXTENSION_OK; | |
e0ad9574 JH |
584 | } else if (!strcmp(ext, "objectformat")) { |
585 | int format; | |
ec91ffca | 586 | |
e0ad9574 JH |
587 | if (!value) |
588 | return config_error_nonbool(var); | |
589 | format = hash_algo_by_name(value); | |
590 | if (format == GIT_HASH_UNKNOWN) | |
1a8aea85 JNA |
591 | return error(_("invalid value for '%s': '%s'"), |
592 | "extensions.objectformat", value); | |
e0ad9574 JH |
593 | data->hash_algo = format; |
594 | return EXTENSION_OK; | |
9ae702fa | 595 | } else if (!strcmp(ext, "compatobjectformat")) { |
596 | struct string_list_item *item; | |
597 | int format; | |
598 | ||
599 | if (!value) | |
600 | return config_error_nonbool(var); | |
601 | format = hash_algo_by_name(value); | |
602 | if (format == GIT_HASH_UNKNOWN) | |
603 | return error(_("invalid value for '%s': '%s'"), | |
604 | "extensions.compatobjectformat", value); | |
605 | /* For now only support compatObjectFormat being specified once. */ | |
606 | for_each_string_list_item(item, &data->v1_only_extensions) { | |
607 | if (!strcmp(item->string, "compatobjectformat")) | |
608 | return error(_("'%s' already specified as '%s'"), | |
609 | "extensions.compatobjectformat", | |
610 | hash_algos[data->compat_hash_algo].name); | |
611 | } | |
612 | data->compat_hash_algo = format; | |
613 | return EXTENSION_OK; | |
d7497a42 PS |
614 | } else if (!strcmp(ext, "refstorage")) { |
615 | unsigned int format; | |
616 | ||
617 | if (!value) | |
618 | return config_error_nonbool(var); | |
619 | format = ref_storage_format_by_name(value); | |
620 | if (format == REF_STORAGE_FORMAT_UNKNOWN) | |
621 | return error(_("invalid value for '%s': '%s'"), | |
622 | "extensions.refstorage", value); | |
623 | data->ref_storage_format = format; | |
624 | return EXTENSION_OK; | |
e0ad9574 | 625 | } |
ec91ffca JK |
626 | return EXTENSION_UNKNOWN; |
627 | } | |
628 | ||
a4e7e317 GC |
629 | static int check_repo_format(const char *var, const char *value, |
630 | const struct config_context *ctx, void *vdata) | |
31e26ebc | 631 | { |
2cc7c2c7 | 632 | struct repository_format *data = vdata; |
00a09d57 JK |
633 | const char *ext; |
634 | ||
31e26ebc | 635 | if (strcmp(var, "core.repositoryformatversion") == 0) |
8868b1eb | 636 | data->version = git_config_int(var, value, ctx->kvi); |
00a09d57 | 637 | else if (skip_prefix(var, "extensions.", &ext)) { |
ec91ffca JK |
638 | switch (handle_extension_v0(var, value, ext, data)) { |
639 | case EXTENSION_ERROR: | |
640 | return -1; | |
641 | case EXTENSION_OK: | |
642 | return 0; | |
643 | case EXTENSION_UNKNOWN: | |
644 | break; | |
645 | } | |
646 | ||
647 | switch (handle_extension(var, value, ext, data)) { | |
648 | case EXTENSION_ERROR: | |
649 | return -1; | |
650 | case EXTENSION_OK: | |
651 | string_list_append(&data->v1_only_extensions, ext); | |
652 | return 0; | |
653 | case EXTENSION_UNKNOWN: | |
2cc7c2c7 | 654 | string_list_append(&data->unknown_extensions, ext); |
ec91ffca JK |
655 | return 0; |
656 | } | |
00a09d57 | 657 | } |
58b284a2 | 658 | |
a4e7e317 | 659 | return read_worktree_config(var, value, ctx, vdata); |
31e26ebc NTND |
660 | } |
661 | ||
abade65b | 662 | static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok) |
9459aa77 | 663 | { |
7d0fb0da | 664 | struct strbuf sb = STRBUF_INIT; |
2cc7c2c7 | 665 | struct strbuf err = STRBUF_INIT; |
652f18ee | 666 | int has_common; |
00a09d57 | 667 | |
652f18ee | 668 | has_common = get_common_dir(&sb, gitdir); |
e61a509a | 669 | strbuf_addstr(&sb, "/config"); |
abade65b | 670 | read_repository_format(candidate, sb.buf); |
2cc7c2c7 | 671 | strbuf_release(&sb); |
e61a509a | 672 | |
337e51ce | 673 | /* |
2cc7c2c7 JK |
674 | * For historical use of check_repository_format() in git-init, |
675 | * we treat a missing config as a silent "ok", even when nongit_ok | |
676 | * is unset. | |
337e51ce | 677 | */ |
abade65b | 678 | if (candidate->version < 0) |
2cc7c2c7 JK |
679 | return 0; |
680 | ||
abade65b | 681 | if (verify_repository_format(candidate, &err) < 0) { |
2cc7c2c7 JK |
682 | if (nongit_ok) { |
683 | warning("%s", err.buf); | |
684 | strbuf_release(&err); | |
685 | *nongit_ok = -1; | |
686 | return -1; | |
687 | } | |
688 | die("%s", err.buf); | |
689 | } | |
690 | ||
11664196 | 691 | repository_format_precious_objects = candidate->precious_objects; |
abade65b | 692 | string_list_clear(&candidate->unknown_extensions, 0); |
ec91ffca | 693 | string_list_clear(&candidate->v1_only_extensions, 0); |
58b284a2 | 694 | |
3867f6d6 | 695 | if (candidate->worktree_config) { |
58b284a2 NTND |
696 | /* |
697 | * pick up core.bare and core.worktree from per-worktree | |
698 | * config if present | |
699 | */ | |
700 | strbuf_addf(&sb, "%s/config.worktree", gitdir); | |
701 | git_config_from_file(read_worktree_config, sb.buf, candidate); | |
702 | strbuf_release(&sb); | |
703 | has_common = 0; | |
704 | } | |
705 | ||
652f18ee | 706 | if (!has_common) { |
abade65b | 707 | if (candidate->is_bare != -1) { |
708 | is_bare_repository_cfg = candidate->is_bare; | |
652f18ee JK |
709 | if (is_bare_repository_cfg == 1) |
710 | inside_work_tree = -1; | |
711 | } | |
abade65b | 712 | if (candidate->work_tree) { |
652f18ee | 713 | free(git_work_tree_cfg); |
e8805af1 | 714 | git_work_tree_cfg = xstrdup(candidate->work_tree); |
2cc7c2c7 | 715 | inside_work_tree = -1; |
652f18ee | 716 | } |
2cc7c2c7 JK |
717 | } |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
16af5f1a XL |
722 | int upgrade_repository_format(int target_version) |
723 | { | |
724 | struct strbuf sb = STRBUF_INIT; | |
725 | struct strbuf err = STRBUF_INIT; | |
726 | struct strbuf repo_version = STRBUF_INIT; | |
727 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; | |
4ce14e13 | 728 | int ret; |
16af5f1a XL |
729 | |
730 | strbuf_git_common_path(&sb, the_repository, "config"); | |
731 | read_repository_format(&repo_fmt, sb.buf); | |
732 | strbuf_release(&sb); | |
733 | ||
4ce14e13 PS |
734 | if (repo_fmt.version >= target_version) { |
735 | ret = 0; | |
736 | goto out; | |
737 | } | |
16af5f1a | 738 | |
62f2eca6 | 739 | if (verify_repository_format(&repo_fmt, &err) < 0) { |
4ce14e13 PS |
740 | ret = error("cannot upgrade repository format from %d to %d: %s", |
741 | repo_fmt.version, target_version, err.buf); | |
742 | goto out; | |
743 | } | |
744 | if (!repo_fmt.version && repo_fmt.unknown_extensions.nr) { | |
745 | ret = error("cannot upgrade repository format: " | |
746 | "unknown extension %s", | |
747 | repo_fmt.unknown_extensions.items[0].string); | |
748 | goto out; | |
16af5f1a XL |
749 | } |
750 | ||
751 | strbuf_addf(&repo_version, "%d", target_version); | |
752 | git_config_set("core.repositoryformatversion", repo_version.buf); | |
4ce14e13 PS |
753 | |
754 | ret = 1; | |
755 | ||
756 | out: | |
9972cd60 | 757 | clear_repository_format(&repo_fmt); |
16af5f1a | 758 | strbuf_release(&repo_version); |
4ce14e13 PS |
759 | strbuf_release(&err); |
760 | return ret; | |
16af5f1a XL |
761 | } |
762 | ||
e8805af1 MÅ |
763 | static void init_repository_format(struct repository_format *format) |
764 | { | |
765 | const struct repository_format fresh = REPOSITORY_FORMAT_INIT; | |
766 | ||
767 | memcpy(format, &fresh, sizeof(fresh)); | |
768 | } | |
769 | ||
652f18ee | 770 | int read_repository_format(struct repository_format *format, const char *path) |
2cc7c2c7 | 771 | { |
e8805af1 | 772 | clear_repository_format(format); |
652f18ee | 773 | git_config_from_file(check_repo_format, path, format); |
e8805af1 MÅ |
774 | if (format->version == -1) |
775 | clear_repository_format(format); | |
2cc7c2c7 JK |
776 | return format->version; |
777 | } | |
778 | ||
e8805af1 MÅ |
779 | void clear_repository_format(struct repository_format *format) |
780 | { | |
781 | string_list_clear(&format->unknown_extensions, 0); | |
ec91ffca | 782 | string_list_clear(&format->v1_only_extensions, 0); |
e8805af1 MÅ |
783 | free(format->work_tree); |
784 | free(format->partial_clone); | |
785 | init_repository_format(format); | |
786 | } | |
787 | ||
2cc7c2c7 JK |
788 | int verify_repository_format(const struct repository_format *format, |
789 | struct strbuf *err) | |
790 | { | |
791 | if (GIT_REPO_VERSION_READ < format->version) { | |
274db840 | 792 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
2cc7c2c7 JK |
793 | GIT_REPO_VERSION_READ, format->version); |
794 | return -1; | |
795 | } | |
796 | ||
797 | if (format->version >= 1 && format->unknown_extensions.nr) { | |
00a09d57 JK |
798 | int i; |
799 | ||
8013d7d9 AH |
800 | strbuf_addstr(err, Q_("unknown repository extension found:", |
801 | "unknown repository extensions found:", | |
802 | format->unknown_extensions.nr)); | |
00a09d57 | 803 | |
2cc7c2c7 JK |
804 | for (i = 0; i < format->unknown_extensions.nr; i++) |
805 | strbuf_addf(err, "\n\t%s", | |
806 | format->unknown_extensions.items[i].string); | |
807 | return -1; | |
00a09d57 JK |
808 | } |
809 | ||
ec91ffca JK |
810 | if (format->version == 0 && format->v1_only_extensions.nr) { |
811 | int i; | |
812 | ||
813 | strbuf_addstr(err, | |
8013d7d9 AH |
814 | Q_("repo version is 0, but v1-only extension found:", |
815 | "repo version is 0, but v1-only extensions found:", | |
816 | format->v1_only_extensions.nr)); | |
ec91ffca JK |
817 | |
818 | for (i = 0; i < format->v1_only_extensions.nr; i++) | |
819 | strbuf_addf(err, "\n\t%s", | |
820 | format->v1_only_extensions.items[i].string); | |
821 | return -1; | |
822 | } | |
823 | ||
2cc7c2c7 | 824 | return 0; |
9459aa77 NTND |
825 | } |
826 | ||
5f29433f SB |
827 | void read_gitfile_error_die(int error_code, const char *path, const char *dir) |
828 | { | |
829 | switch (error_code) { | |
830 | case READ_GITFILE_ERR_STAT_FAILED: | |
831 | case READ_GITFILE_ERR_NOT_A_FILE: | |
832 | /* non-fatal; follow return path */ | |
833 | break; | |
834 | case READ_GITFILE_ERR_OPEN_FAILED: | |
fc045fe7 | 835 | die_errno(_("error opening '%s'"), path); |
5f29433f | 836 | case READ_GITFILE_ERR_TOO_LARGE: |
fc045fe7 | 837 | die(_("too large to be a .git file: '%s'"), path); |
5f29433f | 838 | case READ_GITFILE_ERR_READ_FAILED: |
fc045fe7 | 839 | die(_("error reading %s"), path); |
5f29433f | 840 | case READ_GITFILE_ERR_INVALID_FORMAT: |
fc045fe7 | 841 | die(_("invalid gitfile format: %s"), path); |
5f29433f | 842 | case READ_GITFILE_ERR_NO_PATH: |
fc045fe7 | 843 | die(_("no path in gitfile: %s"), path); |
5f29433f | 844 | case READ_GITFILE_ERR_NOT_A_REPO: |
fc045fe7 | 845 | die(_("not a git repository: %s"), dir); |
5f29433f | 846 | default: |
033abf97 | 847 | BUG("unknown error code"); |
5f29433f SB |
848 | } |
849 | } | |
850 | ||
b44ebb19 LH |
851 | /* |
852 | * Try to read the location of the git directory from the .git file, | |
ea1d8756 HWN |
853 | * return path to git directory if found. The return value comes from |
854 | * a shared buffer. | |
a93bedad EE |
855 | * |
856 | * On failure, if return_error_code is not NULL, return_error_code | |
857 | * will be set to an error code and NULL will be returned. If | |
858 | * return_error_code is NULL the function will die instead (for most | |
859 | * cases). | |
b44ebb19 | 860 | */ |
a93bedad | 861 | const char *read_gitfile_gently(const char *path, int *return_error_code) |
b44ebb19 | 862 | { |
921bdd96 | 863 | const int max_file_size = 1 << 20; /* 1MB */ |
a93bedad EE |
864 | int error_code = 0; |
865 | char *buf = NULL; | |
866 | char *dir = NULL; | |
40c813e0 | 867 | const char *slash; |
b44ebb19 LH |
868 | struct stat st; |
869 | int fd; | |
b1905aea | 870 | ssize_t len; |
3d7747e3 | 871 | static struct strbuf realpath = STRBUF_INIT; |
b44ebb19 | 872 | |
a93bedad | 873 | if (stat(path, &st)) { |
5c4003ca | 874 | /* NEEDSWORK: discern between ENOENT vs other errors */ |
a93bedad EE |
875 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
876 | goto cleanup_return; | |
877 | } | |
878 | if (!S_ISREG(st.st_mode)) { | |
879 | error_code = READ_GITFILE_ERR_NOT_A_FILE; | |
880 | goto cleanup_return; | |
881 | } | |
921bdd96 EE |
882 | if (st.st_size > max_file_size) { |
883 | error_code = READ_GITFILE_ERR_TOO_LARGE; | |
884 | goto cleanup_return; | |
885 | } | |
b44ebb19 | 886 | fd = open(path, O_RDONLY); |
a93bedad EE |
887 | if (fd < 0) { |
888 | error_code = READ_GITFILE_ERR_OPEN_FAILED; | |
889 | goto cleanup_return; | |
890 | } | |
3733e694 | 891 | buf = xmallocz(st.st_size); |
b44ebb19 LH |
892 | len = read_in_full(fd, buf, st.st_size); |
893 | close(fd); | |
a93bedad EE |
894 | if (len != st.st_size) { |
895 | error_code = READ_GITFILE_ERR_READ_FAILED; | |
896 | goto cleanup_return; | |
897 | } | |
a93bedad EE |
898 | if (!starts_with(buf, "gitdir: ")) { |
899 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; | |
900 | goto cleanup_return; | |
901 | } | |
b44ebb19 LH |
902 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
903 | len--; | |
a93bedad EE |
904 | if (len < 9) { |
905 | error_code = READ_GITFILE_ERR_NO_PATH; | |
906 | goto cleanup_return; | |
907 | } | |
b44ebb19 | 908 | buf[len] = '\0'; |
40c813e0 BK |
909 | dir = buf + 8; |
910 | ||
911 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
912 | size_t pathlen = slash+1 - path; | |
75faa45a JK |
913 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
914 | (int)(len - 8), buf + 8); | |
40c813e0 BK |
915 | free(buf); |
916 | buf = dir; | |
917 | } | |
a93bedad EE |
918 | if (!is_git_directory(dir)) { |
919 | error_code = READ_GITFILE_ERR_NOT_A_REPO; | |
920 | goto cleanup_return; | |
921 | } | |
3d7747e3 AM |
922 | |
923 | strbuf_realpath(&realpath, dir, 1); | |
924 | path = realpath.buf; | |
40c813e0 | 925 | |
a93bedad | 926 | cleanup_return: |
a93bedad EE |
927 | if (return_error_code) |
928 | *return_error_code = error_code; | |
5f29433f SB |
929 | else if (error_code) |
930 | read_gitfile_error_die(error_code, path, dir); | |
a93bedad | 931 | |
b44ebb19 | 932 | free(buf); |
38ae8784 | 933 | return error_code ? NULL : path; |
b44ebb19 LH |
934 | } |
935 | ||
e4e30347 | 936 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
7333ed17 | 937 | struct strbuf *cwd, |
abade65b | 938 | struct repository_format *repo_fmt, |
b3f66fd3 | 939 | int *nongit_ok) |
e4e30347 | 940 | { |
b3f66fd3 NTND |
941 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
942 | const char *worktree; | |
943 | char *gitfile; | |
9b125da4 | 944 | int offset; |
e4e30347 JN |
945 | |
946 | if (PATH_MAX - 40 < strlen(gitdirenv)) | |
fc045fe7 | 947 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
b3f66fd3 | 948 | |
13d6ec91 | 949 | gitfile = (char*)read_gitfile(gitdirenv); |
b3f66fd3 NTND |
950 | if (gitfile) { |
951 | gitfile = xstrdup(gitfile); | |
952 | gitdirenv = gitfile; | |
953 | } | |
954 | ||
e4e30347 JN |
955 | if (!is_git_directory(gitdirenv)) { |
956 | if (nongit_ok) { | |
957 | *nongit_ok = 1; | |
b3f66fd3 | 958 | free(gitfile); |
e4e30347 JN |
959 | return NULL; |
960 | } | |
fc045fe7 | 961 | die(_("not a git repository: '%s'"), gitdirenv); |
e4e30347 | 962 | } |
b3f66fd3 | 963 | |
abade65b | 964 | if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) { |
b3f66fd3 NTND |
965 | free(gitfile); |
966 | return NULL; | |
e4e30347 | 967 | } |
b3f66fd3 NTND |
968 | |
969 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ | |
970 | if (work_tree_env) | |
971 | set_git_work_tree(work_tree_env); | |
972 | else if (is_bare_repository_cfg > 0) { | |
fada7674 JK |
973 | if (git_work_tree_cfg) { |
974 | /* #22.2, #30 */ | |
975 | warning("core.bare and core.worktree do not make sense"); | |
976 | work_tree_config_is_bogus = 1; | |
977 | } | |
b3f66fd3 NTND |
978 | |
979 | /* #18, #26 */ | |
0915a5b4 | 980 | set_git_dir(gitdirenv, 0); |
b3f66fd3 | 981 | free(gitfile); |
e4e30347 | 982 | return NULL; |
b3f66fd3 NTND |
983 | } |
984 | else if (git_work_tree_cfg) { /* #6, #14 */ | |
985 | if (is_absolute_path(git_work_tree_cfg)) | |
986 | set_git_work_tree(git_work_tree_cfg); | |
987 | else { | |
56b9f6e7 | 988 | char *core_worktree; |
b3f66fd3 | 989 | if (chdir(gitdirenv)) |
fc045fe7 | 990 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
b3f66fd3 | 991 | if (chdir(git_work_tree_cfg)) |
fc045fe7 | 992 | die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg); |
56b9f6e7 | 993 | core_worktree = xgetcwd(); |
7333ed17 | 994 | if (chdir(cwd->buf)) |
fc045fe7 | 995 | die_errno(_("cannot come back to cwd")); |
b3f66fd3 | 996 | set_git_work_tree(core_worktree); |
56b9f6e7 | 997 | free(core_worktree); |
b3f66fd3 NTND |
998 | } |
999 | } | |
2cd83d10 JK |
1000 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
1001 | /* #16d */ | |
0915a5b4 | 1002 | set_git_dir(gitdirenv, 0); |
2cd83d10 JK |
1003 | free(gitfile); |
1004 | return NULL; | |
1005 | } | |
b3f66fd3 NTND |
1006 | else /* #2, #10 */ |
1007 | set_git_work_tree("."); | |
1008 | ||
1009 | /* set_git_work_tree() must have been called by now */ | |
1010 | worktree = get_git_work_tree(); | |
1011 | ||
1012 | /* both get_git_work_tree() and cwd are already normalized */ | |
7333ed17 | 1013 | if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ |
0915a5b4 | 1014 | set_git_dir(gitdirenv, 0); |
b3f66fd3 | 1015 | free(gitfile); |
e4e30347 | 1016 | return NULL; |
b3f66fd3 | 1017 | } |
e4e30347 | 1018 | |
7333ed17 | 1019 | offset = dir_inside_of(cwd->buf, worktree); |
9b125da4 | 1020 | if (offset >= 0) { /* cwd inside worktree? */ |
0915a5b4 | 1021 | set_git_dir(gitdirenv, 1); |
b3f66fd3 | 1022 | if (chdir(worktree)) |
fc045fe7 | 1023 | die_errno(_("cannot chdir to '%s'"), worktree); |
7333ed17 | 1024 | strbuf_addch(cwd, '/'); |
b3f66fd3 | 1025 | free(gitfile); |
7333ed17 | 1026 | return cwd->buf + offset; |
93a00542 | 1027 | } |
b3f66fd3 NTND |
1028 | |
1029 | /* cwd outside worktree */ | |
0915a5b4 | 1030 | set_git_dir(gitdirenv, 0); |
b3f66fd3 NTND |
1031 | free(gitfile); |
1032 | return NULL; | |
93a00542 JN |
1033 | } |
1034 | ||
9951d3b3 | 1035 | static const char *setup_discovered_git_dir(const char *gitdir, |
7333ed17 | 1036 | struct strbuf *cwd, int offset, |
abade65b | 1037 | struct repository_format *repo_fmt, |
9951d3b3 | 1038 | int *nongit_ok) |
98937bef | 1039 | { |
abade65b | 1040 | if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok)) |
9951d3b3 | 1041 | return NULL; |
98937bef | 1042 | |
4868b2ea JN |
1043 | /* --work-tree is set without --git-dir; use discovered one */ |
1044 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
2d4dcf21 JS |
1045 | char *to_free = NULL; |
1046 | const char *ret; | |
1047 | ||
7333ed17 | 1048 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
2d4dcf21 | 1049 | gitdir = to_free = real_pathdup(gitdir, 1); |
7333ed17 | 1050 | if (chdir(cwd->buf)) |
fc045fe7 | 1051 | die_errno(_("cannot come back to cwd")); |
abade65b | 1052 | ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
2d4dcf21 JS |
1053 | free(to_free); |
1054 | return ret; | |
4868b2ea JN |
1055 | } |
1056 | ||
9951d3b3 NTND |
1057 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
1058 | if (is_bare_repository_cfg > 0) { | |
0915a5b4 | 1059 | set_git_dir(gitdir, (offset != cwd->len)); |
7333ed17 | 1060 | if (chdir(cwd->buf)) |
fc045fe7 | 1061 | die_errno(_("cannot come back to cwd")); |
98937bef | 1062 | return NULL; |
9951d3b3 | 1063 | } |
98937bef | 1064 | |
9951d3b3 NTND |
1065 | /* #0, #1, #5, #8, #9, #12, #13 */ |
1066 | set_git_work_tree("."); | |
1067 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) | |
0915a5b4 | 1068 | set_git_dir(gitdir, 0); |
98937bef | 1069 | inside_git_dir = 0; |
9951d3b3 | 1070 | inside_work_tree = 1; |
5cf7b3b1 | 1071 | if (offset >= cwd->len) |
98937bef NTND |
1072 | return NULL; |
1073 | ||
df380d58 JS |
1074 | /* Make "offset" point past the '/' (already the case for root dirs) */ |
1075 | if (offset != offset_1st_component(cwd->buf)) | |
1076 | offset++; | |
1077 | /* Add a '/' at the end */ | |
7333ed17 RS |
1078 | strbuf_addch(cwd, '/'); |
1079 | return cwd->buf + offset; | |
98937bef NTND |
1080 | } |
1081 | ||
1cd8031b | 1082 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
7333ed17 | 1083 | static const char *setup_bare_git_dir(struct strbuf *cwd, int offset, |
abade65b | 1084 | struct repository_format *repo_fmt, |
7333ed17 | 1085 | int *nongit_ok) |
68698da5 JN |
1086 | { |
1087 | int root_len; | |
1088 | ||
abade65b | 1089 | if (check_repository_format_gently(".", repo_fmt, nongit_ok)) |
1cd8031b NTND |
1090 | return NULL; |
1091 | ||
2cd83d10 JK |
1092 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
1093 | ||
4868b2ea JN |
1094 | /* --work-tree is set without --git-dir; use discovered one */ |
1095 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
da6f8475 | 1096 | static const char *gitdir; |
4868b2ea | 1097 | |
7333ed17 RS |
1098 | gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset); |
1099 | if (chdir(cwd->buf)) | |
fc045fe7 | 1100 | die_errno(_("cannot come back to cwd")); |
abade65b | 1101 | return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
4868b2ea JN |
1102 | } |
1103 | ||
68698da5 | 1104 | inside_git_dir = 1; |
1cd8031b | 1105 | inside_work_tree = 0; |
7333ed17 RS |
1106 | if (offset != cwd->len) { |
1107 | if (chdir(cwd->buf)) | |
fc045fe7 | 1108 | die_errno(_("cannot come back to cwd")); |
7333ed17 RS |
1109 | root_len = offset_1st_component(cwd->buf); |
1110 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); | |
0915a5b4 | 1111 | set_git_dir(cwd->buf, 0); |
337e51ce | 1112 | } |
1cd8031b | 1113 | else |
0915a5b4 | 1114 | set_git_dir(".", 0); |
68698da5 JN |
1115 | return NULL; |
1116 | } | |
1117 | ||
2565b43b | 1118 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
60c98d1e JN |
1119 | { |
1120 | struct stat buf; | |
2565b43b | 1121 | if (stat(path, &buf)) { |
fc045fe7 | 1122 | die_errno(_("failed to stat '%*s%s%s'"), |
2565b43b | 1123 | prefix_len, |
60c98d1e JN |
1124 | prefix ? prefix : "", |
1125 | prefix ? "/" : "", path); | |
2565b43b | 1126 | } |
60c98d1e JN |
1127 | return buf.st_dev; |
1128 | } | |
1129 | ||
9e2326c7 | 1130 | /* |
1b77d83c | 1131 | * A "string_list_each_func_t" function that canonicalizes an entry |
4530a85b | 1132 | * from GIT_CEILING_DIRECTORIES using real_pathdup(), or |
7ec30aaa MH |
1133 | * discards it if unusable. The presence of an empty entry in |
1134 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all | |
1135 | * subsequent entries. | |
9e2326c7 | 1136 | */ |
1b77d83c | 1137 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
7ec30aaa | 1138 | void *cb_data) |
9e2326c7 | 1139 | { |
7ec30aaa | 1140 | int *empty_entry_found = cb_data; |
1b77d83c | 1141 | char *ceil = item->string; |
9e2326c7 | 1142 | |
7ec30aaa MH |
1143 | if (!*ceil) { |
1144 | *empty_entry_found = 1; | |
9e2326c7 | 1145 | return 0; |
7ec30aaa | 1146 | } else if (!is_absolute_path(ceil)) { |
9e2326c7 | 1147 | return 0; |
7ec30aaa MH |
1148 | } else if (*empty_entry_found) { |
1149 | /* Keep entry but do not canonicalize it */ | |
1150 | return 1; | |
1151 | } else { | |
ce83eadd | 1152 | char *real_path = real_pathdup(ceil, 0); |
4ac9006f | 1153 | if (!real_path) { |
7ec30aaa | 1154 | return 0; |
4ac9006f | 1155 | } |
7ec30aaa | 1156 | free(item->string); |
4ac9006f | 1157 | item->string = real_path; |
7ec30aaa MH |
1158 | return 1; |
1159 | } | |
9e2326c7 MH |
1160 | } |
1161 | ||
8959555c JS |
1162 | struct safe_directory_data { |
1163 | const char *path; | |
1164 | int is_safe; | |
1165 | }; | |
1166 | ||
a4e7e317 GC |
1167 | static int safe_directory_cb(const char *key, const char *value, |
1168 | const struct config_context *ctx UNUSED, void *d) | |
8959555c JS |
1169 | { |
1170 | struct safe_directory_data *data = d; | |
1171 | ||
bb50ec3c MV |
1172 | if (strcmp(key, "safe.directory")) |
1173 | return 0; | |
1174 | ||
0f85c4a3 | 1175 | if (!value || !*value) { |
8959555c | 1176 | data->is_safe = 0; |
0f85c4a3 DS |
1177 | } else if (!strcmp(value, "*")) { |
1178 | data->is_safe = 1; | |
1179 | } else { | |
8959555c JS |
1180 | const char *interpolated = NULL; |
1181 | ||
1182 | if (!git_config_pathname(&interpolated, key, value) && | |
1183 | !fspathcmp(data->path, interpolated ? interpolated : value)) | |
1184 | data->is_safe = 1; | |
1185 | ||
1186 | free((char *)interpolated); | |
1187 | } | |
1188 | ||
1189 | return 0; | |
1190 | } | |
1191 | ||
3b0bf270 CMAB |
1192 | /* |
1193 | * Check if a repository is safe, by verifying the ownership of the | |
1194 | * worktree (if any), the git directory, and the gitfile (if any). | |
1195 | * | |
1196 | * Exemptions for known-safe repositories can be added via `safe.directory` | |
1197 | * config settings; for non-bare repositories, their worktree needs to be | |
1198 | * added, for bare ones their git directory. | |
1199 | */ | |
1200 | static int ensure_valid_ownership(const char *gitfile, | |
17d3883f JS |
1201 | const char *worktree, const char *gitdir, |
1202 | struct strbuf *report) | |
8959555c | 1203 | { |
3b0bf270 CMAB |
1204 | struct safe_directory_data data = { |
1205 | .path = worktree ? worktree : gitdir | |
1206 | }; | |
8959555c | 1207 | |
e47363e5 | 1208 | if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) && |
17d3883f JS |
1209 | (!gitfile || is_path_owned_by_current_user(gitfile, report)) && |
1210 | (!worktree || is_path_owned_by_current_user(worktree, report)) && | |
1211 | (!gitdir || is_path_owned_by_current_user(gitdir, report))) | |
8959555c JS |
1212 | return 1; |
1213 | ||
3b0bf270 CMAB |
1214 | /* |
1215 | * data.path is the "path" that identifies the repository and it is | |
1216 | * constant regardless of what failed above. data.is_safe should be | |
1217 | * initialized to false, and might be changed by the callback. | |
1218 | */ | |
6061601d | 1219 | git_protected_config(safe_directory_cb, &data); |
8959555c JS |
1220 | |
1221 | return data.is_safe; | |
1222 | } | |
1223 | ||
f4aa8c8b JS |
1224 | void die_upon_dubious_ownership(const char *gitfile, const char *worktree, |
1225 | const char *gitdir) | |
1226 | { | |
1227 | struct strbuf report = STRBUF_INIT, quoted = STRBUF_INIT; | |
1228 | const char *path; | |
1229 | ||
1230 | if (ensure_valid_ownership(gitfile, worktree, gitdir, &report)) | |
1231 | return; | |
1232 | ||
1233 | strbuf_complete(&report, '\n'); | |
1234 | path = gitfile ? gitfile : gitdir; | |
1235 | sq_quote_buf_pretty("ed, path); | |
1236 | ||
1237 | die(_("detected dubious ownership in repository at '%s'\n" | |
1238 | "%s" | |
1239 | "To add an exception for this directory, call:\n" | |
1240 | "\n" | |
1241 | "\tgit config --global --add safe.directory %s"), | |
1242 | path, report.buf, quoted.buf); | |
1243 | } | |
8959555c | 1244 | |
a4e7e317 GC |
1245 | static int allowed_bare_repo_cb(const char *key, const char *value, |
1246 | const struct config_context *ctx UNUSED, | |
1247 | void *d) | |
8d1a7448 GC |
1248 | { |
1249 | enum allowed_bare_repo *allowed_bare_repo = d; | |
1250 | ||
1251 | if (strcasecmp(key, "safe.bareRepository")) | |
1252 | return 0; | |
1253 | ||
1254 | if (!strcmp(value, "explicit")) { | |
1255 | *allowed_bare_repo = ALLOWED_BARE_REPO_EXPLICIT; | |
1256 | return 0; | |
1257 | } | |
1258 | if (!strcmp(value, "all")) { | |
1259 | *allowed_bare_repo = ALLOWED_BARE_REPO_ALL; | |
1260 | return 0; | |
1261 | } | |
1262 | return -1; | |
1263 | } | |
1264 | ||
1265 | static enum allowed_bare_repo get_allowed_bare_repo(void) | |
1266 | { | |
1267 | enum allowed_bare_repo result = ALLOWED_BARE_REPO_ALL; | |
1268 | git_protected_config(allowed_bare_repo_cb, &result); | |
1269 | return result; | |
1270 | } | |
1271 | ||
1272 | static const char *allowed_bare_repo_to_string( | |
1273 | enum allowed_bare_repo allowed_bare_repo) | |
1274 | { | |
1275 | switch (allowed_bare_repo) { | |
1276 | case ALLOWED_BARE_REPO_EXPLICIT: | |
1277 | return "explicit"; | |
1278 | case ALLOWED_BARE_REPO_ALL: | |
1279 | return "all"; | |
1280 | default: | |
1281 | BUG("invalid allowed_bare_repo %d", | |
1282 | allowed_bare_repo); | |
1283 | } | |
1284 | return NULL; | |
1285 | } | |
1286 | ||
30b7c4bd JH |
1287 | static int is_implicit_bare_repo(const char *path) |
1288 | { | |
1289 | /* | |
1290 | * what we found is a ".git" directory at the root of | |
1291 | * the working tree. | |
1292 | */ | |
1293 | if (ends_with_path_components(path, ".git")) | |
1294 | return 1; | |
1295 | ||
1296 | /* | |
1297 | * we are inside $GIT_DIR of a secondary worktree of a | |
1298 | * non-bare repository. | |
1299 | */ | |
1300 | if (strstr(path, "/.git/worktrees/")) | |
1301 | return 1; | |
1302 | ||
1303 | /* | |
1304 | * we are inside $GIT_DIR of a worktree of a non-embedded | |
1305 | * submodule, whose superproject is not a bare repository. | |
1306 | */ | |
1307 | if (strstr(path, "/.git/modules/")) | |
1308 | return 1; | |
1309 | ||
1310 | return 0; | |
1311 | } | |
1312 | ||
e90fdc39 JS |
1313 | /* |
1314 | * We cannot decide in this function whether we are in the work tree or | |
1315 | * not, since the config can only be read _after_ this function was called. | |
ce9b8aab JS |
1316 | * |
1317 | * Also, we avoid changing any global state (such as the current working | |
1318 | * directory) to allow early callers. | |
1319 | * | |
1320 | * The directory where the search should start needs to be passed in via the | |
1321 | * `dir` parameter; upon return, the `dir` buffer will contain the path of | |
1322 | * the directory where the search ended, and `gitdir` will contain the path of | |
1323 | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it | |
1324 | * is relative to `dir` (i.e. *not* necessarily the cwd). | |
e90fdc39 | 1325 | */ |
ce9b8aab | 1326 | static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, |
01017dce | 1327 | struct strbuf *gitdir, |
17d3883f | 1328 | struct strbuf *report, |
01017dce | 1329 | int die_on_error) |
d288a700 | 1330 | { |
0454dd93 | 1331 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
31171d9e | 1332 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
ce9b8aab | 1333 | const char *gitdirenv; |
d17f2124 | 1334 | int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); |
c7d1d1b1 RH |
1335 | dev_t current_device = 0; |
1336 | int one_filesystem = 1; | |
d288a700 | 1337 | |
e90fdc39 JS |
1338 | /* |
1339 | * If GIT_DIR is set explicitly, we're not going | |
1340 | * to do any discovery, but we still do repository | |
1341 | * validation. | |
1342 | */ | |
ad1a382f | 1343 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
ce9b8aab JS |
1344 | if (gitdirenv) { |
1345 | strbuf_addstr(gitdir, gitdirenv); | |
1346 | return GIT_DIR_EXPLICIT; | |
1347 | } | |
d288a700 | 1348 | |
31171d9e | 1349 | if (env_ceiling_dirs) { |
7ec30aaa MH |
1350 | int empty_entry_found = 0; |
1351 | ||
31171d9e | 1352 | string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); |
1b77d83c | 1353 | filter_string_list(&ceiling_dirs, 0, |
7ec30aaa | 1354 | canonicalize_ceiling_entry, &empty_entry_found); |
ce9b8aab | 1355 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
31171d9e MH |
1356 | string_list_clear(&ceiling_dirs, 0); |
1357 | } | |
1358 | ||
ce9b8aab JS |
1359 | if (ceil_offset < 0) |
1360 | ceil_offset = min_offset - 2; | |
d288a700 | 1361 | |
e2683d51 JS |
1362 | if (min_offset && min_offset == dir->len && |
1363 | !is_dir_sep(dir->buf[min_offset - 1])) { | |
1364 | strbuf_addch(dir, '/'); | |
1365 | min_offset++; | |
1366 | } | |
1367 | ||
892c41b9 | 1368 | /* |
ce9b8aab | 1369 | * Test in the following order (relative to the dir): |
b44ebb19 | 1370 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
1371 | * - .git/ |
1372 | * - ./ (bare) | |
b44ebb19 | 1373 | * - ../.git |
e90fdc39 JS |
1374 | * - ../.git/ |
1375 | * - ../ (bare) | |
176b2d32 | 1376 | * - ../../.git |
e90fdc39 | 1377 | * etc. |
892c41b9 | 1378 | */ |
cf87463e | 1379 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
60c98d1e | 1380 | if (one_filesystem) |
ce9b8aab | 1381 | current_device = get_device_or_die(dir->buf, NULL, 0); |
e90fdc39 | 1382 | for (;;) { |
01017dce | 1383 | int offset = dir->len, error_code = 0; |
3b0bf270 CMAB |
1384 | char *gitdir_path = NULL; |
1385 | char *gitfile = NULL; | |
ce9b8aab JS |
1386 | |
1387 | if (offset > min_offset) | |
1388 | strbuf_addch(dir, '/'); | |
1389 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); | |
01017dce JS |
1390 | gitdirenv = read_gitfile_gently(dir->buf, die_on_error ? |
1391 | NULL : &error_code); | |
1392 | if (!gitdirenv) { | |
1393 | if (die_on_error || | |
1394 | error_code == READ_GITFILE_ERR_NOT_A_FILE) { | |
5c4003ca | 1395 | /* NEEDSWORK: fail if .git is not file nor dir */ |
3b0bf270 | 1396 | if (is_git_directory(dir->buf)) { |
01017dce | 1397 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
3b0bf270 CMAB |
1398 | gitdir_path = xstrdup(dir->buf); |
1399 | } | |
01017dce JS |
1400 | } else if (error_code != READ_GITFILE_ERR_STAT_FAILED) |
1401 | return GIT_DIR_INVALID_GITFILE; | |
3b0bf270 CMAB |
1402 | } else |
1403 | gitfile = xstrdup(dir->buf); | |
1404 | /* | |
1405 | * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT | |
1406 | * to check that directory for a repository. | |
1407 | * Now trim that tentative addition away, because we want to | |
1408 | * focus on the real directory we are in. | |
1409 | */ | |
ce9b8aab | 1410 | strbuf_setlen(dir, offset); |
9951d3b3 | 1411 | if (gitdirenv) { |
3b0bf270 | 1412 | enum discovery_result ret; |
d51e1dff JS |
1413 | const char *gitdir_candidate = |
1414 | gitdir_path ? gitdir_path : gitdirenv; | |
3b0bf270 | 1415 | |
d51e1dff | 1416 | if (ensure_valid_ownership(gitfile, dir->buf, |
17d3883f | 1417 | gitdir_candidate, report)) { |
3b0bf270 CMAB |
1418 | strbuf_addstr(gitdir, gitdirenv); |
1419 | ret = GIT_DIR_DISCOVERED; | |
1420 | } else | |
1421 | ret = GIT_DIR_INVALID_OWNERSHIP; | |
1422 | ||
1423 | /* | |
1424 | * Earlier, during discovery, we might have allocated | |
1425 | * string copies for gitdir_path or gitfile so make | |
1426 | * sure we don't leak by freeing them now, before | |
1427 | * leaving the loop and function. | |
1428 | * | |
1429 | * Note: gitdirenv will be non-NULL whenever these are | |
1430 | * allocated, therefore we need not take care of releasing | |
1431 | * them outside of this conditional block. | |
1432 | */ | |
1433 | free(gitdir_path); | |
1434 | free(gitfile); | |
1435 | ||
1436 | return ret; | |
9951d3b3 | 1437 | } |
9951d3b3 | 1438 | |
ce9b8aab | 1439 | if (is_git_directory(dir->buf)) { |
e35f202b | 1440 | trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf); |
45bb9162 | 1441 | if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT && |
30b7c4bd | 1442 | !is_implicit_bare_repo(dir->buf)) |
8d1a7448 | 1443 | return GIT_DIR_DISALLOWED_BARE; |
17d3883f | 1444 | if (!ensure_valid_ownership(NULL, NULL, dir->buf, report)) |
8959555c | 1445 | return GIT_DIR_INVALID_OWNERSHIP; |
ce9b8aab JS |
1446 | strbuf_addstr(gitdir, "."); |
1447 | return GIT_DIR_BARE; | |
502ffe34 | 1448 | } |
9951d3b3 | 1449 | |
ce9b8aab JS |
1450 | if (offset <= min_offset) |
1451 | return GIT_DIR_HIT_CEILING; | |
1cd8031b | 1452 | |
ce9b8aab | 1453 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
6c1e6544 | 1454 | ; /* continue */ |
ce9b8aab JS |
1455 | if (offset <= ceil_offset) |
1456 | return GIT_DIR_HIT_CEILING; | |
1457 | ||
1458 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); | |
1459 | if (one_filesystem && | |
1460 | current_device != get_device_or_die(dir->buf, NULL, offset)) | |
1461 | return GIT_DIR_HIT_MOUNT_POINT; | |
892c41b9 | 1462 | } |
d288a700 | 1463 | } |
5e7bfe25 | 1464 | |
26ae8da6 DS |
1465 | enum discovery_result discover_git_directory_reason(struct strbuf *commondir, |
1466 | struct strbuf *gitdir) | |
16ac8b8d JS |
1467 | { |
1468 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; | |
1469 | size_t gitdir_offset = gitdir->len, cwd_len; | |
d3fb71b3 | 1470 | size_t commondir_offset = commondir->len; |
e8805af1 | 1471 | struct repository_format candidate = REPOSITORY_FORMAT_INIT; |
26ae8da6 | 1472 | enum discovery_result result; |
16ac8b8d JS |
1473 | |
1474 | if (strbuf_getcwd(&dir)) | |
26ae8da6 | 1475 | return GIT_DIR_CWD_FAILURE; |
16ac8b8d JS |
1476 | |
1477 | cwd_len = dir.len; | |
26ae8da6 DS |
1478 | result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0); |
1479 | if (result <= 0) { | |
16ac8b8d | 1480 | strbuf_release(&dir); |
26ae8da6 | 1481 | return result; |
16ac8b8d JS |
1482 | } |
1483 | ||
1484 | /* | |
1485 | * The returned gitdir is relative to dir, and if dir does not reflect | |
1486 | * the current working directory, we simply make the gitdir absolute. | |
1487 | */ | |
1488 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { | |
1489 | /* Avoid a trailing "/." */ | |
1490 | if (!strcmp(".", gitdir->buf + gitdir_offset)) | |
1491 | strbuf_setlen(gitdir, gitdir_offset); | |
1492 | else | |
1493 | strbuf_addch(&dir, '/'); | |
1494 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); | |
1495 | } | |
1496 | ||
d3fb71b3 BW |
1497 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
1498 | ||
16ac8b8d | 1499 | strbuf_reset(&dir); |
d3fb71b3 | 1500 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
16ac8b8d JS |
1501 | read_repository_format(&candidate, dir.buf); |
1502 | strbuf_release(&dir); | |
1503 | ||
1504 | if (verify_repository_format(&candidate, &err) < 0) { | |
1505 | warning("ignoring git dir '%s': %s", | |
1506 | gitdir->buf + gitdir_offset, err.buf); | |
1507 | strbuf_release(&err); | |
d3fb71b3 | 1508 | strbuf_setlen(commondir, commondir_offset); |
69743f9b | 1509 | strbuf_setlen(gitdir, gitdir_offset); |
e8805af1 | 1510 | clear_repository_format(&candidate); |
26ae8da6 | 1511 | return GIT_DIR_INVALID_FORMAT; |
16ac8b8d JS |
1512 | } |
1513 | ||
e8805af1 | 1514 | clear_repository_format(&candidate); |
26ae8da6 | 1515 | return result; |
16ac8b8d JS |
1516 | } |
1517 | ||
a60645f9 NTND |
1518 | const char *setup_git_directory_gently(int *nongit_ok) |
1519 | { | |
ce9b8aab | 1520 | static struct strbuf cwd = STRBUF_INIT; |
17d3883f | 1521 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT; |
07098b81 | 1522 | const char *prefix = NULL; |
e8805af1 | 1523 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
a60645f9 | 1524 | |
ce9b8aab JS |
1525 | /* |
1526 | * We may have read an incomplete configuration before | |
1527 | * setting-up the git directory. If so, clear the cache so | |
1528 | * that the next queries to the configuration reload complete | |
1529 | * configuration (including the per-repo config file that we | |
1530 | * ignored previously). | |
1531 | */ | |
1532 | git_config_clear(); | |
1533 | ||
1534 | /* | |
1535 | * Let's assume that we are in a git repository. | |
1536 | * If it turns out later that we are somewhere else, the value will be | |
1537 | * updated accordingly. | |
1538 | */ | |
1539 | if (nongit_ok) | |
1540 | *nongit_ok = 0; | |
1541 | ||
1542 | if (strbuf_getcwd(&cwd)) | |
1543 | die_errno(_("Unable to read current working directory")); | |
1544 | strbuf_addbuf(&dir, &cwd); | |
1545 | ||
17d3883f | 1546 | switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) { |
ce9b8aab | 1547 | case GIT_DIR_EXPLICIT: |
abade65b | 1548 | prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1549 | break; |
1550 | case GIT_DIR_DISCOVERED: | |
1551 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1552 | die(_("cannot change to '%s'"), dir.buf); |
ce9b8aab | 1553 | prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len, |
abade65b | 1554 | &repo_fmt, nongit_ok); |
ce9b8aab JS |
1555 | break; |
1556 | case GIT_DIR_BARE: | |
1557 | if (dir.len < cwd.len && chdir(dir.buf)) | |
fc045fe7 | 1558 | die(_("cannot change to '%s'"), dir.buf); |
abade65b | 1559 | prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok); |
ce9b8aab JS |
1560 | break; |
1561 | case GIT_DIR_HIT_CEILING: | |
07098b81 ED |
1562 | if (!nongit_ok) |
1563 | die(_("not a git repository (or any of the parent directories): %s"), | |
1564 | DEFAULT_GIT_DIR_ENVIRONMENT); | |
1565 | *nongit_ok = 1; | |
ce9b8aab JS |
1566 | break; |
1567 | case GIT_DIR_HIT_MOUNT_POINT: | |
07098b81 ED |
1568 | if (!nongit_ok) |
1569 | die(_("not a git repository (or any parent up to mount point %s)\n" | |
1570 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), | |
1571 | dir.buf); | |
1572 | *nongit_ok = 1; | |
1573 | break; | |
8959555c JS |
1574 | case GIT_DIR_INVALID_OWNERSHIP: |
1575 | if (!nongit_ok) { | |
1576 | struct strbuf quoted = STRBUF_INIT; | |
1577 | ||
17d3883f | 1578 | strbuf_complete(&report, '\n'); |
8959555c | 1579 | sq_quote_buf_pretty("ed, dir.buf); |
3b0bf270 | 1580 | die(_("detected dubious ownership in repository at '%s'\n" |
17d3883f | 1581 | "%s" |
8959555c JS |
1582 | "To add an exception for this directory, call:\n" |
1583 | "\n" | |
1584 | "\tgit config --global --add safe.directory %s"), | |
17d3883f | 1585 | dir.buf, report.buf, quoted.buf); |
8959555c JS |
1586 | } |
1587 | *nongit_ok = 1; | |
1588 | break; | |
8d1a7448 GC |
1589 | case GIT_DIR_DISALLOWED_BARE: |
1590 | if (!nongit_ok) { | |
1591 | die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"), | |
1592 | dir.buf, | |
1593 | allowed_bare_repo_to_string(get_allowed_bare_repo())); | |
1594 | } | |
1595 | *nongit_ok = 1; | |
1596 | break; | |
26ae8da6 DS |
1597 | case GIT_DIR_CWD_FAILURE: |
1598 | case GIT_DIR_INVALID_FORMAT: | |
07098b81 ED |
1599 | /* |
1600 | * As a safeguard against setup_git_directory_gently_1 returning | |
26ae8da6 | 1601 | * these values, fallthrough to BUG. Otherwise it is possible to |
07098b81 ED |
1602 | * set startup_info->have_repository to 1 when we did nothing to |
1603 | * find a repository. | |
1604 | */ | |
ce9b8aab | 1605 | default: |
a3ba4fa7 | 1606 | BUG("unhandled setup_git_directory_gently_1() result"); |
ce9b8aab JS |
1607 | } |
1608 | ||
07098b81 ED |
1609 | /* |
1610 | * At this point, nongit_ok is stable. If it is non-NULL and points | |
1611 | * to a non-zero value, then this means that we haven't found a | |
1612 | * repository and that the caller expects startup_info to reflect | |
1613 | * this. | |
1614 | * | |
1615 | * Regardless of the state of nongit_ok, startup_info->prefix and | |
1616 | * the GIT_PREFIX environment variable must always match. For details | |
1617 | * see Documentation/config/alias.txt. | |
1618 | */ | |
c7d0e610 | 1619 | if (nongit_ok && *nongit_ok) |
07098b81 | 1620 | startup_info->have_repository = 0; |
c7d0e610 | 1621 | else |
07098b81 | 1622 | startup_info->have_repository = 1; |
46c3cd44 | 1623 | |
73f192c9 BW |
1624 | /* |
1625 | * Not all paths through the setup code will call 'set_git_dir()' (which | |
1626 | * directly sets up the environment) so in order to guarantee that the | |
1627 | * environment is in a consistent state after setup, explicitly setup | |
1628 | * the environment if we have a repository. | |
1629 | * | |
1630 | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some | |
1631 | * code paths so we also need to explicitly setup the environment if | |
1632 | * the user has set GIT_DIR. It may be beneficial to disallow bogus | |
1633 | * GIT_DIR values at some point in the future. | |
1634 | */ | |
07098b81 ED |
1635 | if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */ |
1636 | startup_info->have_repository || | |
1637 | /* GIT_DIR_EXPLICIT */ | |
1638 | getenv(GIT_DIR_ENVIRONMENT)) { | |
c14c234f BW |
1639 | if (!the_repository->gitdir) { |
1640 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); | |
1641 | if (!gitdir) | |
1642 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; | |
357a03eb | 1643 | setup_git_env(gitdir); |
c14c234f | 1644 | } |
ebaf3bcf | 1645 | if (startup_info->have_repository) { |
78a67668 | 1646 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); |
15a1ca1a | 1647 | repo_set_compat_hash_algo(the_repository, |
9ae702fa | 1648 | repo_fmt.compat_hash_algo); |
173761e2 PS |
1649 | repo_set_ref_storage_format(the_repository, |
1650 | repo_fmt.ref_storage_format); | |
3867f6d6 VD |
1651 | the_repository->repository_format_worktree_config = |
1652 | repo_fmt.worktree_config; | |
ebaf3bcf JT |
1653 | /* take ownership of repo_fmt.partial_clone */ |
1654 | the_repository->repository_format_partial_clone = | |
1655 | repo_fmt.partial_clone; | |
1656 | repo_fmt.partial_clone = NULL; | |
1657 | } | |
c14c234f | 1658 | } |
c7d0e610 TB |
1659 | /* |
1660 | * Since precompose_string_if_needed() needs to look at | |
1661 | * the core.precomposeunicode configuration, this | |
1662 | * has to happen after the above block that finds | |
1663 | * out where the repository is, i.e. a preparation | |
1664 | * for calling git_config_get_bool(). | |
1665 | */ | |
1666 | if (prefix) { | |
1667 | prefix = precompose_string_if_needed(prefix); | |
1668 | startup_info->prefix = prefix; | |
1669 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); | |
1670 | } else { | |
1671 | startup_info->prefix = NULL; | |
1672 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); | |
1673 | } | |
1674 | ||
e6f8861b | 1675 | setup_original_cwd(); |
73f192c9 | 1676 | |
ce9b8aab JS |
1677 | strbuf_release(&dir); |
1678 | strbuf_release(&gitdir); | |
17d3883f | 1679 | strbuf_release(&report); |
e8805af1 | 1680 | clear_repository_format(&repo_fmt); |
ce9b8aab | 1681 | |
a60645f9 NTND |
1682 | return prefix; |
1683 | } | |
1684 | ||
94df2506 JH |
1685 | int git_config_perm(const char *var, const char *value) |
1686 | { | |
06cbe855 HO |
1687 | int i; |
1688 | char *endptr; | |
1689 | ||
afe8a907 | 1690 | if (!value) |
06cbe855 HO |
1691 | return PERM_GROUP; |
1692 | ||
1693 | if (!strcmp(value, "umask")) | |
1694 | return PERM_UMASK; | |
1695 | if (!strcmp(value, "group")) | |
1696 | return PERM_GROUP; | |
1697 | if (!strcmp(value, "all") || | |
1698 | !strcmp(value, "world") || | |
1699 | !strcmp(value, "everybody")) | |
1700 | return PERM_EVERYBODY; | |
1701 | ||
1702 | /* Parse octal numbers */ | |
1703 | i = strtol(value, &endptr, 8); | |
1704 | ||
1705 | /* If not an octal number, maybe true/false? */ | |
1706 | if (*endptr != 0) | |
1707 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
1708 | ||
1709 | /* | |
1710 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 1711 | * a chmod value to restrict to. |
06cbe855 HO |
1712 | */ |
1713 | switch (i) { | |
1714 | case PERM_UMASK: /* 0 */ | |
1715 | return PERM_UMASK; | |
1716 | case OLD_PERM_GROUP: /* 1 */ | |
1717 | return PERM_GROUP; | |
1718 | case OLD_PERM_EVERYBODY: /* 2 */ | |
1719 | return PERM_EVERYBODY; | |
94df2506 | 1720 | } |
06cbe855 HO |
1721 | |
1722 | /* A filemode value was given: 0xxx */ | |
1723 | ||
1724 | if ((i & 0600) != 0600) | |
fc045fe7 | 1725 | die(_("problem with core.sharedRepository filemode value " |
06cbe855 | 1726 | "(0%.3o).\nThe owner of files must always have " |
2ff30e67 | 1727 | "read and write permissions."), i); |
06cbe855 HO |
1728 | |
1729 | /* | |
1730 | * Mask filemode value. Others can not get write permission. | |
1731 | * x flags for directories are handled separately. | |
1732 | */ | |
5a688fe4 | 1733 | return -(i & 0666); |
94df2506 JH |
1734 | } |
1735 | ||
cfe3917c | 1736 | void check_repository_format(struct repository_format *fmt) |
ab9cb76f | 1737 | { |
e8805af1 | 1738 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
cfe3917c | 1739 | if (!fmt) |
1740 | fmt = &repo_fmt; | |
1741 | check_repository_format_gently(get_git_dir(), fmt, NULL); | |
f1c126bd | 1742 | startup_info->have_repository = 1; |
d553aceb | 1743 | repo_set_hash_algo(the_repository, fmt->hash_algo); |
9ae702fa | 1744 | repo_set_compat_hash_algo(the_repository, fmt->compat_hash_algo); |
173761e2 PS |
1745 | repo_set_ref_storage_format(the_repository, |
1746 | fmt->ref_storage_format); | |
3867f6d6 VD |
1747 | the_repository->repository_format_worktree_config = |
1748 | fmt->worktree_config; | |
ebaf3bcf JT |
1749 | the_repository->repository_format_partial_clone = |
1750 | xstrdup_or_null(fmt->partial_clone); | |
e8805af1 | 1751 | clear_repository_format(&repo_fmt); |
ab9cb76f JH |
1752 | } |
1753 | ||
e1e5ec86 CB |
1754 | /* |
1755 | * Returns the "prefix", a path to the current working directory | |
1756 | * relative to the work tree root, or NULL, if the current working | |
1757 | * directory is not a strict subdirectory of the work tree root. The | |
1758 | * prefix always ends with a '/' character. | |
1759 | */ | |
5e7bfe25 JH |
1760 | const char *setup_git_directory(void) |
1761 | { | |
b3f66fd3 | 1762 | return setup_git_directory_gently(NULL); |
5e7bfe25 | 1763 | } |
abc06822 | 1764 | |
40d96325 | 1765 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
abc06822 FG |
1766 | { |
1767 | if (is_git_directory(suspect)) | |
1768 | return suspect; | |
40d96325 | 1769 | return read_gitfile_gently(suspect, return_error_code); |
abc06822 | 1770 | } |
1d999ddd TR |
1771 | |
1772 | /* if any standard file descriptor is missing open it to /dev/null */ | |
1773 | void sanitize_stdfds(void) | |
1774 | { | |
d9a65b6c RS |
1775 | int fd = xopen("/dev/null", O_RDWR); |
1776 | while (fd < 2) | |
1777 | fd = xdup(fd); | |
1d999ddd TR |
1778 | if (fd > 2) |
1779 | close(fd); | |
1780 | } | |
de0957ce NTND |
1781 | |
1782 | int daemonize(void) | |
1783 | { | |
1784 | #ifdef NO_POSIX_GOODIES | |
1785 | errno = ENOSYS; | |
1786 | return -1; | |
1787 | #else | |
1788 | switch (fork()) { | |
1789 | case 0: | |
1790 | break; | |
1791 | case -1: | |
fc045fe7 | 1792 | die_errno(_("fork failed")); |
de0957ce NTND |
1793 | default: |
1794 | exit(0); | |
1795 | } | |
1796 | if (setsid() == -1) | |
fc045fe7 | 1797 | die_errno(_("setsid failed")); |
de0957ce NTND |
1798 | close(0); |
1799 | close(1); | |
1800 | close(2); | |
1801 | sanitize_stdfds(); | |
1802 | return 0; | |
1803 | #endif | |
1804 | } | |
e8cf8ef5 | 1805 | |
4412a04f JS |
1806 | struct template_dir_cb_data { |
1807 | char *path; | |
1808 | int initialized; | |
1809 | }; | |
1810 | ||
be348e98 JS |
1811 | static int template_dir_cb(const char *key, const char *value, |
1812 | const struct config_context *ctx, void *d) | |
4412a04f JS |
1813 | { |
1814 | struct template_dir_cb_data *data = d; | |
1815 | ||
1816 | if (strcmp(key, "init.templatedir")) | |
1817 | return 0; | |
1818 | ||
1819 | if (!value) { | |
1820 | data->path = NULL; | |
1821 | } else { | |
1822 | char *path = NULL; | |
1823 | ||
1824 | FREE_AND_NULL(data->path); | |
1825 | if (!git_config_pathname((const char **)&path, key, value)) | |
1826 | data->path = path ? path : xstrdup(value); | |
1827 | } | |
1828 | ||
1829 | return 0; | |
1830 | } | |
1831 | ||
df93e407 JS |
1832 | const char *get_template_dir(const char *option_template) |
1833 | { | |
1834 | const char *template_dir = option_template; | |
1835 | ||
1836 | if (!template_dir) | |
1837 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); | |
1838 | if (!template_dir) { | |
4412a04f | 1839 | static struct template_dir_cb_data data; |
df93e407 | 1840 | |
4412a04f JS |
1841 | if (!data.initialized) { |
1842 | git_protected_config(template_dir_cb, &data); | |
1843 | data.initialized = 1; | |
df93e407 | 1844 | } |
4412a04f | 1845 | template_dir = data.path; |
df93e407 JS |
1846 | } |
1847 | if (!template_dir) { | |
1848 | static char *dir; | |
1849 | ||
1850 | if (!dir) | |
1851 | dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); | |
1852 | template_dir = dir; | |
1853 | } | |
1854 | return template_dir; | |
1855 | } | |
be348e98 | 1856 | |
e8cf8ef5 EN |
1857 | #ifdef NO_TRUSTABLE_FILEMODE |
1858 | #define TEST_FILEMODE 0 | |
1859 | #else | |
1860 | #define TEST_FILEMODE 1 | |
1861 | #endif | |
1862 | ||
1863 | #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH" | |
1864 | ||
1865 | static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, | |
1866 | DIR *dir) | |
1867 | { | |
1868 | size_t path_baselen = path->len; | |
1869 | size_t template_baselen = template_path->len; | |
1870 | struct dirent *de; | |
1871 | ||
1872 | /* Note: if ".git/hooks" file exists in the repository being | |
1873 | * re-initialized, /etc/core-git/templates/hooks/update would | |
1874 | * cause "git init" to fail here. I think this is sane but | |
1875 | * it means that the set of templates we ship by default, along | |
1876 | * with the way the namespace under .git/ is organized, should | |
1877 | * be really carefully chosen. | |
1878 | */ | |
1879 | safe_create_dir(path->buf, 1); | |
1880 | while ((de = readdir(dir)) != NULL) { | |
1881 | struct stat st_git, st_template; | |
1882 | int exists = 0; | |
1883 | ||
1884 | strbuf_setlen(path, path_baselen); | |
1885 | strbuf_setlen(template_path, template_baselen); | |
1886 | ||
1887 | if (de->d_name[0] == '.') | |
1888 | continue; | |
1889 | strbuf_addstr(path, de->d_name); | |
1890 | strbuf_addstr(template_path, de->d_name); | |
1891 | if (lstat(path->buf, &st_git)) { | |
1892 | if (errno != ENOENT) | |
1893 | die_errno(_("cannot stat '%s'"), path->buf); | |
1894 | } | |
1895 | else | |
1896 | exists = 1; | |
1897 | ||
1898 | if (lstat(template_path->buf, &st_template)) | |
1899 | die_errno(_("cannot stat template '%s'"), template_path->buf); | |
1900 | ||
1901 | if (S_ISDIR(st_template.st_mode)) { | |
1902 | DIR *subdir = opendir(template_path->buf); | |
1903 | if (!subdir) | |
1904 | die_errno(_("cannot opendir '%s'"), template_path->buf); | |
1905 | strbuf_addch(path, '/'); | |
1906 | strbuf_addch(template_path, '/'); | |
1907 | copy_templates_1(path, template_path, subdir); | |
1908 | closedir(subdir); | |
1909 | } | |
1910 | else if (exists) | |
1911 | continue; | |
1912 | else if (S_ISLNK(st_template.st_mode)) { | |
1913 | struct strbuf lnk = STRBUF_INIT; | |
1914 | if (strbuf_readlink(&lnk, template_path->buf, | |
1915 | st_template.st_size) < 0) | |
1916 | die_errno(_("cannot readlink '%s'"), template_path->buf); | |
1917 | if (symlink(lnk.buf, path->buf)) | |
1918 | die_errno(_("cannot symlink '%s' '%s'"), | |
1919 | lnk.buf, path->buf); | |
1920 | strbuf_release(&lnk); | |
1921 | } | |
1922 | else if (S_ISREG(st_template.st_mode)) { | |
1923 | if (copy_file(path->buf, template_path->buf, st_template.st_mode)) | |
1924 | die_errno(_("cannot copy '%s' to '%s'"), | |
1925 | template_path->buf, path->buf); | |
1926 | } | |
1927 | else | |
1928 | error(_("ignoring template %s"), template_path->buf); | |
1929 | } | |
1930 | } | |
1931 | ||
be348e98 | 1932 | static void copy_templates(const char *option_template) |
e8cf8ef5 | 1933 | { |
be348e98 | 1934 | const char *template_dir = get_template_dir(option_template); |
e8cf8ef5 EN |
1935 | struct strbuf path = STRBUF_INIT; |
1936 | struct strbuf template_path = STRBUF_INIT; | |
1937 | size_t template_len; | |
1938 | struct repository_format template_format = REPOSITORY_FORMAT_INIT; | |
1939 | struct strbuf err = STRBUF_INIT; | |
1940 | DIR *dir; | |
1941 | char *to_free = NULL; | |
1942 | ||
be348e98 | 1943 | if (!template_dir || !*template_dir) |
e8cf8ef5 | 1944 | return; |
e8cf8ef5 EN |
1945 | |
1946 | strbuf_addstr(&template_path, template_dir); | |
1947 | strbuf_complete(&template_path, '/'); | |
1948 | template_len = template_path.len; | |
1949 | ||
1950 | dir = opendir(template_path.buf); | |
1951 | if (!dir) { | |
1952 | warning(_("templates not found in %s"), template_dir); | |
1953 | goto free_return; | |
1954 | } | |
1955 | ||
1956 | /* Make sure that template is from the correct vintage */ | |
1957 | strbuf_addstr(&template_path, "config"); | |
1958 | read_repository_format(&template_format, template_path.buf); | |
1959 | strbuf_setlen(&template_path, template_len); | |
1960 | ||
1961 | /* | |
1962 | * No mention of version at all is OK, but anything else should be | |
1963 | * verified. | |
1964 | */ | |
1965 | if (template_format.version >= 0 && | |
1966 | verify_repository_format(&template_format, &err) < 0) { | |
1967 | warning(_("not copying templates from '%s': %s"), | |
1968 | template_dir, err.buf); | |
1969 | strbuf_release(&err); | |
1970 | goto close_free_return; | |
1971 | } | |
1972 | ||
1973 | strbuf_addstr(&path, get_git_common_dir()); | |
1974 | strbuf_complete(&path, '/'); | |
1975 | copy_templates_1(&path, &template_path, dir); | |
1976 | close_free_return: | |
1977 | closedir(dir); | |
1978 | free_return: | |
1979 | free(to_free); | |
1980 | strbuf_release(&path); | |
1981 | strbuf_release(&template_path); | |
1982 | clear_repository_format(&template_format); | |
1983 | } | |
1984 | ||
1985 | /* | |
1986 | * If the git_dir is not directly inside the working tree, then git will not | |
1987 | * find it by default, and we need to set the worktree explicitly. | |
1988 | */ | |
1989 | static int needs_work_tree_config(const char *git_dir, const char *work_tree) | |
1990 | { | |
1991 | if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git")) | |
1992 | return 0; | |
1993 | if (skip_prefix(git_dir, work_tree, &git_dir) && | |
1994 | !strcmp(git_dir, "/.git")) | |
1995 | return 0; | |
1996 | return 1; | |
1997 | } | |
1998 | ||
d7497a42 PS |
1999 | void initialize_repository_version(int hash_algo, |
2000 | unsigned int ref_storage_format, | |
2001 | int reinit) | |
e8cf8ef5 EN |
2002 | { |
2003 | char repo_version_string[10]; | |
2004 | int repo_version = GIT_REPO_VERSION; | |
2005 | ||
199f44cb PS |
2006 | /* |
2007 | * Note that we initialize the repository version to 1 when the ref | |
2008 | * storage format is unknown. This is on purpose so that we can add the | |
2009 | * correct object format to the config during git-clone(1). The format | |
2010 | * version will get adjusted by git-clone(1) once it has learned about | |
2011 | * the remote repository's format. | |
2012 | */ | |
d7497a42 PS |
2013 | if (hash_algo != GIT_HASH_SHA1 || |
2014 | ref_storage_format != REF_STORAGE_FORMAT_FILES) | |
e8cf8ef5 EN |
2015 | repo_version = GIT_REPO_VERSION_READ; |
2016 | ||
2017 | /* This forces creation of new config file */ | |
2018 | xsnprintf(repo_version_string, sizeof(repo_version_string), | |
2019 | "%d", repo_version); | |
2020 | git_config_set("core.repositoryformatversion", repo_version_string); | |
2021 | ||
199f44cb | 2022 | if (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN) |
e8cf8ef5 EN |
2023 | git_config_set("extensions.objectformat", |
2024 | hash_algos[hash_algo].name); | |
2025 | else if (reinit) | |
2026 | git_config_set_gently("extensions.objectformat", NULL); | |
d7497a42 PS |
2027 | |
2028 | if (ref_storage_format != REF_STORAGE_FORMAT_FILES) | |
2029 | git_config_set("extensions.refstorage", | |
2030 | ref_storage_format_to_name(ref_storage_format)); | |
e8cf8ef5 EN |
2031 | } |
2032 | ||
79543e76 PS |
2033 | static int is_reinit(void) |
2034 | { | |
2035 | struct strbuf buf = STRBUF_INIT; | |
2036 | char junk[2]; | |
2037 | int ret; | |
2038 | ||
2039 | git_path_buf(&buf, "HEAD"); | |
2040 | ret = !access(buf.buf, R_OK) || readlink(buf.buf, junk, sizeof(junk) - 1) != -1; | |
2041 | strbuf_release(&buf); | |
2042 | return ret; | |
2043 | } | |
2044 | ||
173761e2 PS |
2045 | void create_reference_database(unsigned int ref_storage_format, |
2046 | const char *initial_branch, int quiet) | |
79543e76 PS |
2047 | { |
2048 | struct strbuf err = STRBUF_INIT; | |
2049 | int reinit = is_reinit(); | |
2050 | ||
173761e2 | 2051 | repo_set_ref_storage_format(the_repository, ref_storage_format); |
2e573d61 | 2052 | if (refs_init_db(get_main_ref_store(the_repository), 0, &err)) |
79543e76 PS |
2053 | die("failed to set up refs db: %s", err.buf); |
2054 | ||
2055 | /* | |
2056 | * Point the HEAD symref to the initial branch with if HEAD does | |
2057 | * not yet exist. | |
2058 | */ | |
2059 | if (!reinit) { | |
2060 | char *ref; | |
2061 | ||
2062 | if (!initial_branch) | |
2063 | initial_branch = git_default_branch_name(quiet); | |
2064 | ||
2065 | ref = xstrfmt("refs/heads/%s", initial_branch); | |
2066 | if (check_refname_format(ref, 0) < 0) | |
2067 | die(_("invalid initial branch name: '%s'"), | |
2068 | initial_branch); | |
2069 | ||
2070 | if (create_symref("HEAD", ref, NULL) < 0) | |
2071 | exit(1); | |
2072 | free(ref); | |
2073 | } | |
2074 | ||
2075 | if (reinit && initial_branch) | |
2076 | warning(_("re-init: ignored --initial-branch=%s"), | |
2077 | initial_branch); | |
2078 | ||
2079 | strbuf_release(&err); | |
2080 | } | |
2081 | ||
e8cf8ef5 EN |
2082 | static int create_default_files(const char *template_path, |
2083 | const char *original_git_dir, | |
e8cf8ef5 | 2084 | const struct repository_format *fmt, |
56cd0334 | 2085 | int init_shared_repository) |
e8cf8ef5 EN |
2086 | { |
2087 | struct stat st1; | |
2088 | struct strbuf buf = STRBUF_INIT; | |
2089 | char *path; | |
e8cf8ef5 EN |
2090 | int reinit; |
2091 | int filemode; | |
e8cf8ef5 EN |
2092 | const char *work_tree = get_git_work_tree(); |
2093 | ||
2094 | /* | |
2095 | * First copy the templates -- we might have the default | |
2096 | * config file there, in which case we would want to read | |
2097 | * from it after installing. | |
2098 | * | |
2099 | * Before reading that config, we also need to clear out any cached | |
2100 | * values (since we've just potentially changed what's available on | |
2101 | * disk). | |
2102 | */ | |
be348e98 | 2103 | copy_templates(template_path); |
e8cf8ef5 EN |
2104 | git_config_clear(); |
2105 | reset_shared_repository(); | |
2106 | git_config(git_default_config, NULL); | |
2107 | ||
79543e76 PS |
2108 | reinit = is_reinit(); |
2109 | ||
e8cf8ef5 EN |
2110 | /* |
2111 | * We must make sure command-line options continue to override any | |
2112 | * values we might have just re-read from the config. | |
2113 | */ | |
2114 | if (init_shared_repository != -1) | |
2115 | set_shared_repository(init_shared_repository); | |
8145a8fd GT |
2116 | |
2117 | is_bare_repository_cfg = !work_tree; | |
e8cf8ef5 EN |
2118 | |
2119 | /* | |
2120 | * We would have created the above under user's umask -- under | |
2121 | * shared-repository settings, we would need to fix them up. | |
2122 | */ | |
2123 | if (get_shared_repository()) { | |
2124 | adjust_shared_perm(get_git_dir()); | |
2125 | } | |
2126 | ||
d7497a42 | 2127 | initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0); |
e8cf8ef5 EN |
2128 | |
2129 | /* Check filemode trustability */ | |
2130 | path = git_path_buf(&buf, "config"); | |
2131 | filemode = TEST_FILEMODE; | |
2132 | if (TEST_FILEMODE && !lstat(path, &st1)) { | |
2133 | struct stat st2; | |
2134 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && | |
2135 | !lstat(path, &st2) && | |
2136 | st1.st_mode != st2.st_mode && | |
2137 | !chmod(path, st1.st_mode)); | |
2138 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) | |
2139 | filemode = 0; | |
2140 | } | |
2141 | git_config_set("core.filemode", filemode ? "true" : "false"); | |
2142 | ||
2143 | if (is_bare_repository()) | |
2144 | git_config_set("core.bare", "true"); | |
2145 | else { | |
2146 | git_config_set("core.bare", "false"); | |
2147 | /* allow template config file to override the default */ | |
2148 | if (log_all_ref_updates == LOG_REFS_UNSET) | |
2149 | git_config_set("core.logallrefupdates", "true"); | |
2150 | if (needs_work_tree_config(original_git_dir, work_tree)) | |
2151 | git_config_set("core.worktree", work_tree); | |
2152 | } | |
2153 | ||
2154 | if (!reinit) { | |
2155 | /* Check if symlink is supported in the work tree */ | |
2156 | path = git_path_buf(&buf, "tXXXXXX"); | |
2157 | if (!close(xmkstemp(path)) && | |
2158 | !unlink(path) && | |
2159 | !symlink("testing", path) && | |
2160 | !lstat(path, &st1) && | |
2161 | S_ISLNK(st1.st_mode)) | |
2162 | unlink(path); /* good */ | |
2163 | else | |
2164 | git_config_set("core.symlinks", "false"); | |
2165 | ||
2166 | /* Check if the filesystem is case-insensitive */ | |
2167 | path = git_path_buf(&buf, "CoNfIg"); | |
2168 | if (!access(path, F_OK)) | |
2169 | git_config_set("core.ignorecase", "true"); | |
2170 | probe_utf8_pathname_composition(); | |
2171 | } | |
2172 | ||
2173 | strbuf_release(&buf); | |
2174 | return reinit; | |
2175 | } | |
2176 | ||
2177 | static void create_object_directory(void) | |
2178 | { | |
2179 | struct strbuf path = STRBUF_INIT; | |
2180 | size_t baselen; | |
2181 | ||
2182 | strbuf_addstr(&path, get_object_directory()); | |
2183 | baselen = path.len; | |
2184 | ||
2185 | safe_create_dir(path.buf, 1); | |
2186 | ||
2187 | strbuf_setlen(&path, baselen); | |
2188 | strbuf_addstr(&path, "/pack"); | |
2189 | safe_create_dir(path.buf, 1); | |
2190 | ||
2191 | strbuf_setlen(&path, baselen); | |
2192 | strbuf_addstr(&path, "/info"); | |
2193 | safe_create_dir(path.buf, 1); | |
2194 | ||
2195 | strbuf_release(&path); | |
2196 | } | |
2197 | ||
2198 | static void separate_git_dir(const char *git_dir, const char *git_link) | |
2199 | { | |
2200 | struct stat st; | |
2201 | ||
2202 | if (!stat(git_link, &st)) { | |
2203 | const char *src; | |
2204 | ||
2205 | if (S_ISREG(st.st_mode)) | |
2206 | src = read_gitfile(git_link); | |
2207 | else if (S_ISDIR(st.st_mode)) | |
2208 | src = git_link; | |
2209 | else | |
2210 | die(_("unable to handle file type %d"), (int)st.st_mode); | |
2211 | ||
2212 | if (rename(src, git_dir)) | |
2213 | die_errno(_("unable to move %s to %s"), src, git_dir); | |
2214 | repair_worktrees(NULL, NULL); | |
2215 | } | |
2216 | ||
2217 | write_file(git_link, "gitdir: %s", git_dir); | |
2218 | } | |
2219 | ||
2220 | static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash) | |
2221 | { | |
2222 | const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT); | |
2223 | /* | |
2224 | * If we already have an initialized repo, don't allow the user to | |
2225 | * specify a different algorithm, as that could cause corruption. | |
2226 | * Otherwise, if the user has specified one on the command line, use it. | |
2227 | */ | |
2228 | if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo) | |
2229 | die(_("attempt to reinitialize repository with different hash")); | |
2230 | else if (hash != GIT_HASH_UNKNOWN) | |
2231 | repo_fmt->hash_algo = hash; | |
2232 | else if (env) { | |
2233 | int env_algo = hash_algo_by_name(env); | |
2234 | if (env_algo == GIT_HASH_UNKNOWN) | |
2235 | die(_("unknown hash algorithm '%s'"), env); | |
2236 | repo_fmt->hash_algo = env_algo; | |
2237 | } | |
2238 | } | |
2239 | ||
173761e2 PS |
2240 | static void validate_ref_storage_format(struct repository_format *repo_fmt, |
2241 | unsigned int format) | |
2242 | { | |
aa19619a PS |
2243 | const char *name = getenv("GIT_DEFAULT_REF_FORMAT"); |
2244 | ||
173761e2 PS |
2245 | if (repo_fmt->version >= 0 && |
2246 | format != REF_STORAGE_FORMAT_UNKNOWN && | |
2247 | format != repo_fmt->ref_storage_format) { | |
2248 | die(_("attempt to reinitialize repository with different reference storage format")); | |
2249 | } else if (format != REF_STORAGE_FORMAT_UNKNOWN) { | |
2250 | repo_fmt->ref_storage_format = format; | |
aa19619a PS |
2251 | } else if (name) { |
2252 | format = ref_storage_format_by_name(name); | |
2253 | if (format == REF_STORAGE_FORMAT_UNKNOWN) | |
2254 | die(_("unknown ref storage format '%s'"), name); | |
2255 | repo_fmt->ref_storage_format = format; | |
173761e2 PS |
2256 | } |
2257 | } | |
2258 | ||
e8cf8ef5 | 2259 | int init_db(const char *git_dir, const char *real_git_dir, |
173761e2 PS |
2260 | const char *template_dir, int hash, |
2261 | unsigned int ref_storage_format, | |
2262 | const char *initial_branch, | |
e8cf8ef5 EN |
2263 | int init_shared_repository, unsigned int flags) |
2264 | { | |
2265 | int reinit; | |
2266 | int exist_ok = flags & INIT_DB_EXIST_OK; | |
2267 | char *original_git_dir = real_pathdup(git_dir, 1); | |
2268 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; | |
e8cf8ef5 EN |
2269 | |
2270 | if (real_git_dir) { | |
2271 | struct stat st; | |
2272 | ||
2273 | if (!exist_ok && !stat(git_dir, &st)) | |
2274 | die(_("%s already exists"), git_dir); | |
2275 | ||
2276 | if (!exist_ok && !stat(real_git_dir, &st)) | |
2277 | die(_("%s already exists"), real_git_dir); | |
2278 | ||
2279 | set_git_dir(real_git_dir, 1); | |
2280 | git_dir = get_git_dir(); | |
2281 | separate_git_dir(git_dir, original_git_dir); | |
2282 | } | |
2283 | else { | |
2284 | set_git_dir(git_dir, 1); | |
2285 | git_dir = get_git_dir(); | |
2286 | } | |
2287 | startup_info->have_repository = 1; | |
2288 | ||
2289 | /* Ensure `core.hidedotfiles` is processed */ | |
2290 | git_config(platform_core_config, NULL); | |
2291 | ||
2292 | safe_create_dir(git_dir, 0); | |
2293 | ||
e8cf8ef5 EN |
2294 | |
2295 | /* Check to see if the repository version is right. | |
2296 | * Note that a newly created repository does not have | |
2297 | * config file, so this will not fail. What we are catching | |
2298 | * is an attempt to reinitialize new repository with an old tool. | |
2299 | */ | |
2300 | check_repository_format(&repo_fmt); | |
2301 | ||
2302 | validate_hash_algorithm(&repo_fmt, hash); | |
173761e2 | 2303 | validate_ref_storage_format(&repo_fmt, ref_storage_format); |
e8cf8ef5 EN |
2304 | |
2305 | reinit = create_default_files(template_dir, original_git_dir, | |
8145a8fd | 2306 | &repo_fmt, init_shared_repository); |
e8cf8ef5 | 2307 | |
58be32ff PS |
2308 | /* |
2309 | * Now that we have set up both the hash algorithm and the ref storage | |
2310 | * format we can update the repository's settings accordingly. | |
2311 | */ | |
2312 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); | |
2313 | repo_set_ref_storage_format(the_repository, repo_fmt.ref_storage_format); | |
2314 | ||
56cd0334 | 2315 | if (!(flags & INIT_DB_SKIP_REFDB)) |
173761e2 PS |
2316 | create_reference_database(repo_fmt.ref_storage_format, |
2317 | initial_branch, flags & INIT_DB_QUIET); | |
e8cf8ef5 EN |
2318 | create_object_directory(); |
2319 | ||
2320 | if (get_shared_repository()) { | |
2321 | char buf[10]; | |
2322 | /* We do not spell "group" and such, so that | |
2323 | * the configuration can be read by older version | |
2324 | * of git. Note, we use octal numbers for new share modes, | |
2325 | * and compatibility values for PERM_GROUP and | |
2326 | * PERM_EVERYBODY. | |
2327 | */ | |
2328 | if (get_shared_repository() < 0) | |
2329 | /* force to the mode value */ | |
2330 | xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository()); | |
2331 | else if (get_shared_repository() == PERM_GROUP) | |
2332 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); | |
2333 | else if (get_shared_repository() == PERM_EVERYBODY) | |
2334 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); | |
2335 | else | |
2336 | BUG("invalid value for shared_repository"); | |
2337 | git_config_set("core.sharedrepository", buf); | |
2338 | git_config_set("receive.denyNonFastforwards", "true"); | |
2339 | } | |
2340 | ||
2341 | if (!(flags & INIT_DB_QUIET)) { | |
2342 | int len = strlen(git_dir); | |
2343 | ||
2344 | if (reinit) | |
2345 | printf(get_shared_repository() | |
2346 | ? _("Reinitialized existing shared Git repository in %s%s\n") | |
2347 | : _("Reinitialized existing Git repository in %s%s\n"), | |
2348 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); | |
2349 | else | |
2350 | printf(get_shared_repository() | |
2351 | ? _("Initialized empty shared Git repository in %s%s\n") | |
2352 | : _("Initialized empty Git repository in %s%s\n"), | |
2353 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); | |
2354 | } | |
2355 | ||
9972cd60 | 2356 | clear_repository_format(&repo_fmt); |
e8cf8ef5 EN |
2357 | free(original_git_dir); |
2358 | return 0; | |
2359 | } |