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