Line | Count | Source |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
3 | | |
4 | | #include "git-compat-util.h" |
5 | | #include "abspath.h" |
6 | | #include "copy.h" |
7 | | #include "environment.h" |
8 | | #include "exec-cmd.h" |
9 | | #include "gettext.h" |
10 | | #include "hex.h" |
11 | | #include "object-file.h" |
12 | | #include "object-name.h" |
13 | | #include "refs.h" |
14 | | #include "replace-object.h" |
15 | | #include "repository.h" |
16 | | #include "config.h" |
17 | | #include "dir.h" |
18 | | #include "setup.h" |
19 | | #include "shallow.h" |
20 | | #include "string-list.h" |
21 | | #include "strvec.h" |
22 | | #include "chdir-notify.h" |
23 | | #include "path.h" |
24 | | #include "quote.h" |
25 | | #include "trace.h" |
26 | | #include "trace2.h" |
27 | | #include "worktree.h" |
28 | | #include "exec-cmd.h" |
29 | | |
30 | | static int inside_git_dir = -1; |
31 | | static int inside_work_tree = -1; |
32 | | static int work_tree_config_is_bogus; |
33 | | enum allowed_bare_repo { |
34 | | ALLOWED_BARE_REPO_EXPLICIT = 0, |
35 | | ALLOWED_BARE_REPO_ALL, |
36 | | }; |
37 | | |
38 | | static struct startup_info the_startup_info; |
39 | | struct startup_info *startup_info = &the_startup_info; |
40 | | const char *tmp_original_cwd; |
41 | | |
42 | | /* |
43 | | * The input parameter must contain an absolute path, and it must already be |
44 | | * normalized. |
45 | | * |
46 | | * Find the part of an absolute path that lies inside the work tree by |
47 | | * dereferencing symlinks outside the work tree, for example: |
48 | | * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file |
49 | | * /dir/file (work tree is /) -> dir/file |
50 | | * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2 |
51 | | * /dir/repolink/file (repolink points to /dir/repo) -> file |
52 | | * /dir/repo (exactly equal to work tree) -> (empty string) |
53 | | */ |
54 | | static int abspath_part_inside_repo(char *path) |
55 | 0 | { |
56 | 0 | size_t len; |
57 | 0 | size_t wtlen; |
58 | 0 | char *path0; |
59 | 0 | int off; |
60 | 0 | const char *work_tree = precompose_string_if_needed(repo_get_work_tree(the_repository)); |
61 | 0 | struct strbuf realpath = STRBUF_INIT; |
62 | |
|
63 | 0 | if (!work_tree) |
64 | 0 | return -1; |
65 | 0 | wtlen = strlen(work_tree); |
66 | 0 | len = strlen(path); |
67 | 0 | off = offset_1st_component(path); |
68 | | |
69 | | /* check if work tree is already the prefix */ |
70 | 0 | if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) { |
71 | 0 | if (path[wtlen] == '/') { |
72 | 0 | memmove(path, path + wtlen + 1, len - wtlen); |
73 | 0 | return 0; |
74 | 0 | } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') { |
75 | | /* work tree is the root, or the whole path */ |
76 | 0 | memmove(path, path + wtlen, len - wtlen + 1); |
77 | 0 | return 0; |
78 | 0 | } |
79 | | /* work tree might match beginning of a symlink to work tree */ |
80 | 0 | off = wtlen; |
81 | 0 | } |
82 | 0 | path0 = path; |
83 | 0 | path += off; |
84 | | |
85 | | /* check each '/'-terminated level */ |
86 | 0 | while (*path) { |
87 | 0 | path++; |
88 | 0 | if (*path == '/') { |
89 | 0 | *path = '\0'; |
90 | 0 | strbuf_realpath(&realpath, path0, 1); |
91 | 0 | if (fspathcmp(realpath.buf, work_tree) == 0) { |
92 | 0 | memmove(path0, path + 1, len - (path - path0)); |
93 | 0 | strbuf_release(&realpath); |
94 | 0 | return 0; |
95 | 0 | } |
96 | 0 | *path = '/'; |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | /* check whole path */ |
101 | 0 | strbuf_realpath(&realpath, path0, 1); |
102 | 0 | if (fspathcmp(realpath.buf, work_tree) == 0) { |
103 | 0 | *path0 = '\0'; |
104 | 0 | strbuf_release(&realpath); |
105 | 0 | return 0; |
106 | 0 | } |
107 | | |
108 | 0 | strbuf_release(&realpath); |
109 | 0 | return -1; |
110 | 0 | } |
111 | | |
112 | | /* |
113 | | * Normalize "path", prepending the "prefix" for relative paths. If |
114 | | * remaining_prefix is not NULL, return the actual prefix still |
115 | | * remains in the path. For example, prefix = sub1/sub2/ and path is |
116 | | * |
117 | | * foo -> sub1/sub2/foo (full prefix) |
118 | | * ../foo -> sub1/foo (remaining prefix is sub1/) |
119 | | * ../../bar -> bar (no remaining prefix) |
120 | | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) |
121 | | * `pwd`/../bar -> sub1/bar (no remaining prefix) |
122 | | */ |
123 | | char *prefix_path_gently(const char *prefix, int len, |
124 | | int *remaining_prefix, const char *path) |
125 | 0 | { |
126 | 0 | const char *orig = path; |
127 | 0 | char *sanitized; |
128 | 0 | if (is_absolute_path(orig)) { |
129 | 0 | sanitized = xmallocz(strlen(path)); |
130 | 0 | if (remaining_prefix) |
131 | 0 | *remaining_prefix = 0; |
132 | 0 | if (normalize_path_copy_len(sanitized, path, remaining_prefix)) { |
133 | 0 | free(sanitized); |
134 | 0 | return NULL; |
135 | 0 | } |
136 | 0 | if (abspath_part_inside_repo(sanitized)) { |
137 | 0 | free(sanitized); |
138 | 0 | return NULL; |
139 | 0 | } |
140 | 0 | } else { |
141 | 0 | sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); |
142 | 0 | if (remaining_prefix) |
143 | 0 | *remaining_prefix = len; |
144 | 0 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { |
145 | 0 | free(sanitized); |
146 | 0 | return NULL; |
147 | 0 | } |
148 | 0 | } |
149 | 0 | return sanitized; |
150 | 0 | } |
151 | | |
152 | | char *prefix_path(const char *prefix, int len, const char *path) |
153 | 0 | { |
154 | 0 | char *r = prefix_path_gently(prefix, len, NULL, path); |
155 | 0 | if (!r) { |
156 | 0 | const char *hint_path = repo_get_work_tree(the_repository); |
157 | 0 | if (!hint_path) |
158 | 0 | hint_path = repo_get_git_dir(the_repository); |
159 | 0 | die(_("'%s' is outside repository at '%s'"), path, |
160 | 0 | absolute_path(hint_path)); |
161 | 0 | } |
162 | 0 | return r; |
163 | 0 | } |
164 | | |
165 | | int path_inside_repo(const char *prefix, const char *path) |
166 | 0 | { |
167 | 0 | int len = prefix ? strlen(prefix) : 0; |
168 | 0 | char *r = prefix_path_gently(prefix, len, NULL, path); |
169 | 0 | if (r) { |
170 | 0 | free(r); |
171 | 0 | return 1; |
172 | 0 | } |
173 | 0 | return 0; |
174 | 0 | } |
175 | | |
176 | | int check_filename(const char *prefix, const char *arg) |
177 | 0 | { |
178 | 0 | char *to_free = NULL; |
179 | 0 | struct stat st; |
180 | |
|
181 | 0 | if (skip_prefix(arg, ":/", &arg)) { |
182 | 0 | if (!*arg) /* ":/" is root dir, always exists */ |
183 | 0 | return 1; |
184 | 0 | prefix = NULL; |
185 | 0 | } else if (skip_prefix(arg, ":!", &arg) || |
186 | 0 | skip_prefix(arg, ":^", &arg)) { |
187 | 0 | if (!*arg) /* excluding everything is silly, but allowed */ |
188 | 0 | return 1; |
189 | 0 | } |
190 | | |
191 | 0 | if (prefix) |
192 | 0 | arg = to_free = prefix_filename(prefix, arg); |
193 | |
|
194 | 0 | if (!lstat(arg, &st)) { |
195 | 0 | free(to_free); |
196 | 0 | return 1; /* file exists */ |
197 | 0 | } |
198 | 0 | if (is_missing_file_error(errno)) { |
199 | 0 | free(to_free); |
200 | 0 | return 0; /* file does not exist */ |
201 | 0 | } |
202 | 0 | die_errno(_("failed to stat '%s'"), arg); |
203 | 0 | } |
204 | | |
205 | | static void NORETURN die_verify_filename(struct repository *r, |
206 | | const char *prefix, |
207 | | const char *arg, |
208 | | int diagnose_misspelt_rev) |
209 | 0 | { |
210 | 0 | if (!diagnose_misspelt_rev) |
211 | 0 | die(_("%s: no such path in the working tree.\n" |
212 | 0 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally."), |
213 | 0 | arg); |
214 | | /* |
215 | | * Saying "'(icase)foo' does not exist in the index" when the |
216 | | * user gave us ":(icase)foo" is just stupid. A magic pathspec |
217 | | * begins with a colon and is followed by a non-alnum; do not |
218 | | * let maybe_die_on_misspelt_object_name() even trigger. |
219 | | */ |
220 | 0 | if (!(arg[0] == ':' && !isalnum(arg[1]))) |
221 | 0 | maybe_die_on_misspelt_object_name(r, arg, prefix); |
222 | | |
223 | | /* ... or fall back the most general message. */ |
224 | 0 | die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n" |
225 | 0 | "Use '--' to separate paths from revisions, like this:\n" |
226 | 0 | "'git <command> [<revision>...] -- [<file>...]'"), arg); |
227 | |
|
228 | 0 | } |
229 | | |
230 | | /* |
231 | | * Check for arguments that don't resolve as actual files, |
232 | | * but which look sufficiently like pathspecs that we'll consider |
233 | | * them such for the purposes of rev/pathspec DWIM parsing. |
234 | | */ |
235 | | static int looks_like_pathspec(const char *arg) |
236 | 0 | { |
237 | 0 | const char *p; |
238 | 0 | int escaped = 0; |
239 | | |
240 | | /* |
241 | | * Wildcard characters imply the user is looking to match pathspecs |
242 | | * that aren't in the filesystem. Note that this doesn't include |
243 | | * backslash even though it's a glob special; by itself it doesn't |
244 | | * cause any increase in the match. Likewise ignore backslash-escaped |
245 | | * wildcard characters. |
246 | | */ |
247 | 0 | for (p = arg; *p; p++) { |
248 | 0 | if (escaped) { |
249 | 0 | escaped = 0; |
250 | 0 | } else if (is_glob_special(*p)) { |
251 | 0 | if (*p == '\\') |
252 | 0 | escaped = 1; |
253 | 0 | else |
254 | 0 | return 1; |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | | /* long-form pathspec magic */ |
259 | 0 | if (starts_with(arg, ":(")) |
260 | 0 | return 1; |
261 | | |
262 | 0 | return 0; |
263 | 0 | } |
264 | | |
265 | | /* |
266 | | * Verify a filename that we got as an argument for a pathspec |
267 | | * entry. Note that a filename that begins with "-" never verifies |
268 | | * as true, because even if such a filename were to exist, we want |
269 | | * it to be preceded by the "--" marker (or we want the user to |
270 | | * use a format like "./-filename") |
271 | | * |
272 | | * The "diagnose_misspelt_rev" is used to provide a user-friendly |
273 | | * diagnosis when dying upon finding that "name" is not a pathname. |
274 | | * If set to 1, the diagnosis will try to diagnose "name" as an |
275 | | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis |
276 | | * will only complain about an inexisting file. |
277 | | * |
278 | | * This function is typically called to check that a "file or rev" |
279 | | * argument is unambiguous. In this case, the caller will want |
280 | | * diagnose_misspelt_rev == 1 when verifying the first non-rev |
281 | | * argument (which could have been a revision), and |
282 | | * diagnose_misspelt_rev == 0 for the next ones (because we already |
283 | | * saw a filename, there's not ambiguity anymore). |
284 | | */ |
285 | | void verify_filename(const char *prefix, |
286 | | const char *arg, |
287 | | int diagnose_misspelt_rev) |
288 | 0 | { |
289 | 0 | if (*arg == '-') |
290 | 0 | die(_("option '%s' must come before non-option arguments"), arg); |
291 | 0 | if (looks_like_pathspec(arg) || check_filename(prefix, arg)) |
292 | 0 | return; |
293 | 0 | die_verify_filename(the_repository, prefix, arg, diagnose_misspelt_rev); |
294 | 0 | } |
295 | | |
296 | | /* |
297 | | * Opposite of the above: the command line did not have -- marker |
298 | | * and we parsed the arg as a refname. It should not be interpretable |
299 | | * as a filename. |
300 | | */ |
301 | | void verify_non_filename(const char *prefix, const char *arg) |
302 | 0 | { |
303 | 0 | if (!is_inside_work_tree() || is_inside_git_dir()) |
304 | 0 | return; |
305 | 0 | if (*arg == '-') |
306 | 0 | return; /* flag */ |
307 | 0 | if (!check_filename(prefix, arg)) |
308 | 0 | return; |
309 | 0 | die(_("ambiguous argument '%s': both revision and filename\n" |
310 | 0 | "Use '--' to separate paths from revisions, like this:\n" |
311 | 0 | "'git <command> [<revision>...] -- [<file>...]'"), arg); |
312 | 0 | } |
313 | | |
314 | | int get_common_dir(struct strbuf *sb, const char *gitdir) |
315 | 0 | { |
316 | 0 | const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT); |
317 | 0 | if (git_env_common_dir) { |
318 | 0 | strbuf_addstr(sb, git_env_common_dir); |
319 | 0 | return 1; |
320 | 0 | } else { |
321 | 0 | return get_common_dir_noenv(sb, gitdir); |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | | int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) |
326 | 0 | { |
327 | 0 | struct strbuf data = STRBUF_INIT; |
328 | 0 | struct strbuf path = STRBUF_INIT; |
329 | 0 | int ret = 0; |
330 | |
|
331 | 0 | strbuf_addf(&path, "%s/commondir", gitdir); |
332 | 0 | if (file_exists(path.buf)) { |
333 | 0 | if (strbuf_read_file(&data, path.buf, 0) <= 0) |
334 | 0 | die_errno(_("failed to read %s"), path.buf); |
335 | 0 | while (data.len && (data.buf[data.len - 1] == '\n' || |
336 | 0 | data.buf[data.len - 1] == '\r')) |
337 | 0 | data.len--; |
338 | 0 | data.buf[data.len] = '\0'; |
339 | 0 | strbuf_reset(&path); |
340 | 0 | if (!is_absolute_path(data.buf)) |
341 | 0 | strbuf_addf(&path, "%s/", gitdir); |
342 | 0 | strbuf_addbuf(&path, &data); |
343 | 0 | strbuf_add_real_path(sb, path.buf); |
344 | 0 | ret = 1; |
345 | 0 | } else { |
346 | 0 | strbuf_addstr(sb, gitdir); |
347 | 0 | } |
348 | | |
349 | 0 | strbuf_release(&data); |
350 | 0 | strbuf_release(&path); |
351 | 0 | return ret; |
352 | 0 | } |
353 | | |
354 | | static int validate_headref(const char *path) |
355 | 0 | { |
356 | 0 | struct stat st; |
357 | 0 | char buffer[256]; |
358 | 0 | const char *refname; |
359 | 0 | struct object_id oid; |
360 | 0 | int fd; |
361 | 0 | ssize_t len; |
362 | |
|
363 | 0 | if (lstat(path, &st) < 0) |
364 | 0 | return -1; |
365 | | |
366 | | /* Make sure it is a "refs/.." symlink */ |
367 | 0 | if (S_ISLNK(st.st_mode)) { |
368 | 0 | len = readlink(path, buffer, sizeof(buffer)-1); |
369 | 0 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
370 | 0 | return 0; |
371 | 0 | return -1; |
372 | 0 | } |
373 | | |
374 | | /* |
375 | | * Anything else, just open it and try to see if it is a symbolic ref. |
376 | | */ |
377 | 0 | fd = open(path, O_RDONLY); |
378 | 0 | if (fd < 0) |
379 | 0 | return -1; |
380 | 0 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
381 | 0 | close(fd); |
382 | |
|
383 | 0 | if (len < 0) |
384 | 0 | return -1; |
385 | 0 | buffer[len] = '\0'; |
386 | | |
387 | | /* |
388 | | * Is it a symbolic ref? |
389 | | */ |
390 | 0 | if (skip_prefix(buffer, "ref:", &refname)) { |
391 | 0 | while (isspace(*refname)) |
392 | 0 | refname++; |
393 | 0 | if (starts_with(refname, "refs/")) |
394 | 0 | return 0; |
395 | 0 | } |
396 | | |
397 | | /* |
398 | | * Is this a detached HEAD? |
399 | | */ |
400 | 0 | if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN) |
401 | 0 | return 0; |
402 | | |
403 | 0 | return -1; |
404 | 0 | } |
405 | | |
406 | | /* |
407 | | * Test if it looks like we're at a git directory. |
408 | | * We want to see: |
409 | | * |
410 | | * - either an objects/ directory _or_ the proper |
411 | | * GIT_OBJECT_DIRECTORY environment variable |
412 | | * - a refs/ directory |
413 | | * - either a HEAD symlink or a HEAD file that is formatted as |
414 | | * a proper "ref:", or a regular file HEAD that has a properly |
415 | | * formatted sha1 object name. |
416 | | */ |
417 | | int is_git_directory(const char *suspect) |
418 | 0 | { |
419 | 0 | struct strbuf path = STRBUF_INIT; |
420 | 0 | int ret = 0; |
421 | 0 | size_t len; |
422 | | |
423 | | /* Check worktree-related signatures */ |
424 | 0 | strbuf_addstr(&path, suspect); |
425 | 0 | strbuf_complete(&path, '/'); |
426 | 0 | strbuf_addstr(&path, "HEAD"); |
427 | 0 | if (validate_headref(path.buf)) |
428 | 0 | goto done; |
429 | | |
430 | 0 | strbuf_reset(&path); |
431 | 0 | get_common_dir(&path, suspect); |
432 | 0 | len = path.len; |
433 | | |
434 | | /* Check non-worktree-related signatures */ |
435 | 0 | if (getenv(DB_ENVIRONMENT)) { |
436 | 0 | if (access(getenv(DB_ENVIRONMENT), X_OK)) |
437 | 0 | goto done; |
438 | 0 | } |
439 | 0 | else { |
440 | 0 | strbuf_setlen(&path, len); |
441 | 0 | strbuf_addstr(&path, "/objects"); |
442 | 0 | if (access(path.buf, X_OK)) |
443 | 0 | goto done; |
444 | 0 | } |
445 | | |
446 | 0 | strbuf_setlen(&path, len); |
447 | 0 | strbuf_addstr(&path, "/refs"); |
448 | 0 | if (access(path.buf, X_OK)) |
449 | 0 | goto done; |
450 | | |
451 | 0 | ret = 1; |
452 | 0 | done: |
453 | 0 | strbuf_release(&path); |
454 | 0 | return ret; |
455 | 0 | } |
456 | | |
457 | | int is_nonbare_repository_dir(struct strbuf *path) |
458 | 0 | { |
459 | 0 | int ret = 0; |
460 | 0 | int gitfile_error; |
461 | 0 | size_t orig_path_len = path->len; |
462 | 0 | assert(orig_path_len != 0); |
463 | 0 | strbuf_complete(path, '/'); |
464 | 0 | strbuf_addstr(path, ".git"); |
465 | 0 | if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf)) |
466 | 0 | ret = 1; |
467 | 0 | if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED || |
468 | 0 | gitfile_error == READ_GITFILE_ERR_READ_FAILED) |
469 | 0 | ret = 1; |
470 | 0 | strbuf_setlen(path, orig_path_len); |
471 | 0 | return ret; |
472 | 0 | } |
473 | | |
474 | | int is_inside_git_dir(void) |
475 | 0 | { |
476 | 0 | if (inside_git_dir < 0) |
477 | 0 | inside_git_dir = is_inside_dir(repo_get_git_dir(the_repository)); |
478 | 0 | return inside_git_dir; |
479 | 0 | } |
480 | | |
481 | | int is_inside_work_tree(void) |
482 | 0 | { |
483 | 0 | if (inside_work_tree < 0) |
484 | 0 | inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository)); |
485 | 0 | return inside_work_tree; |
486 | 0 | } |
487 | | |
488 | | void setup_work_tree(void) |
489 | 0 | { |
490 | 0 | const char *work_tree; |
491 | 0 | static int initialized = 0; |
492 | |
|
493 | 0 | if (initialized) |
494 | 0 | return; |
495 | | |
496 | 0 | if (work_tree_config_is_bogus) |
497 | 0 | die(_("unable to set up work tree using invalid config")); |
498 | | |
499 | 0 | work_tree = repo_get_work_tree(the_repository); |
500 | 0 | if (!work_tree || chdir_notify(work_tree)) |
501 | 0 | die(_("this operation must be run in a work tree")); |
502 | | |
503 | | /* |
504 | | * Make sure subsequent git processes find correct worktree |
505 | | * if $GIT_WORK_TREE is set relative |
506 | | */ |
507 | 0 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) |
508 | 0 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); |
509 | |
|
510 | 0 | initialized = 1; |
511 | 0 | } |
512 | | |
513 | | static void setup_original_cwd(void) |
514 | 0 | { |
515 | 0 | struct strbuf tmp = STRBUF_INIT; |
516 | 0 | const char *worktree = NULL; |
517 | 0 | int offset = -1; |
518 | |
|
519 | 0 | if (!tmp_original_cwd) |
520 | 0 | return; |
521 | | |
522 | | /* |
523 | | * startup_info->original_cwd points to the current working |
524 | | * directory we inherited from our parent process, which is a |
525 | | * directory we want to avoid removing. |
526 | | * |
527 | | * For convenience, we would like to have the path relative to the |
528 | | * worktree instead of an absolute path. |
529 | | * |
530 | | * Yes, startup_info->original_cwd is usually the same as 'prefix', |
531 | | * but differs in two ways: |
532 | | * - prefix has a trailing '/' |
533 | | * - if the user passes '-C' to git, that modifies the prefix but |
534 | | * not startup_info->original_cwd. |
535 | | */ |
536 | | |
537 | | /* Normalize the directory */ |
538 | 0 | if (!strbuf_realpath(&tmp, tmp_original_cwd, 0)) { |
539 | 0 | trace2_data_string("setup", the_repository, |
540 | 0 | "realpath-path", tmp_original_cwd); |
541 | 0 | trace2_data_string("setup", the_repository, |
542 | 0 | "realpath-failure", strerror(errno)); |
543 | 0 | free((char*)tmp_original_cwd); |
544 | 0 | tmp_original_cwd = NULL; |
545 | 0 | return; |
546 | 0 | } |
547 | | |
548 | 0 | free((char*)tmp_original_cwd); |
549 | 0 | tmp_original_cwd = NULL; |
550 | 0 | startup_info->original_cwd = strbuf_detach(&tmp, NULL); |
551 | | |
552 | | /* |
553 | | * Get our worktree; we only protect the current working directory |
554 | | * if it's in the worktree. |
555 | | */ |
556 | 0 | worktree = repo_get_work_tree(the_repository); |
557 | 0 | if (!worktree) |
558 | 0 | goto no_prevention_needed; |
559 | | |
560 | 0 | offset = dir_inside_of(startup_info->original_cwd, worktree); |
561 | 0 | if (offset >= 0) { |
562 | | /* |
563 | | * If startup_info->original_cwd == worktree, that is already |
564 | | * protected and we don't need original_cwd as a secondary |
565 | | * protection measure. |
566 | | */ |
567 | 0 | if (!*(startup_info->original_cwd + offset)) |
568 | 0 | goto no_prevention_needed; |
569 | | |
570 | | /* |
571 | | * original_cwd was inside worktree; precompose it just as |
572 | | * we do prefix so that built up paths will match |
573 | | */ |
574 | 0 | startup_info->original_cwd = \ |
575 | 0 | precompose_string_if_needed(startup_info->original_cwd |
576 | 0 | + offset); |
577 | 0 | return; |
578 | 0 | } |
579 | | |
580 | 0 | no_prevention_needed: |
581 | 0 | free((char*)startup_info->original_cwd); |
582 | 0 | startup_info->original_cwd = NULL; |
583 | 0 | } |
584 | | |
585 | | static int read_worktree_config(const char *var, const char *value, |
586 | | const struct config_context *ctx UNUSED, |
587 | | void *vdata) |
588 | 0 | { |
589 | 0 | struct repository_format *data = vdata; |
590 | |
|
591 | 0 | if (strcmp(var, "core.bare") == 0) { |
592 | 0 | data->is_bare = git_config_bool(var, value); |
593 | 0 | } else if (strcmp(var, "core.worktree") == 0) { |
594 | 0 | if (!value) |
595 | 0 | return config_error_nonbool(var); |
596 | 0 | free(data->work_tree); |
597 | 0 | data->work_tree = xstrdup(value); |
598 | 0 | } |
599 | 0 | return 0; |
600 | 0 | } |
601 | | |
602 | | enum extension_result { |
603 | | EXTENSION_ERROR = -1, /* compatible with error(), etc */ |
604 | | EXTENSION_UNKNOWN = 0, |
605 | | EXTENSION_OK = 1 |
606 | | }; |
607 | | |
608 | | /* |
609 | | * Do not add new extensions to this function. It handles extensions which are |
610 | | * respected even in v0-format repositories for historical compatibility. |
611 | | */ |
612 | | static enum extension_result handle_extension_v0(const char *var, |
613 | | const char *value, |
614 | | const char *ext, |
615 | | struct repository_format *data) |
616 | 0 | { |
617 | 0 | if (!strcmp(ext, "noop")) { |
618 | 0 | return EXTENSION_OK; |
619 | 0 | } else if (!strcmp(ext, "preciousobjects")) { |
620 | 0 | data->precious_objects = git_config_bool(var, value); |
621 | 0 | return EXTENSION_OK; |
622 | 0 | } else if (!strcmp(ext, "partialclone")) { |
623 | 0 | if (!value) |
624 | 0 | return config_error_nonbool(var); |
625 | 0 | data->partial_clone = xstrdup(value); |
626 | 0 | return EXTENSION_OK; |
627 | 0 | } else if (!strcmp(ext, "worktreeconfig")) { |
628 | 0 | data->worktree_config = git_config_bool(var, value); |
629 | 0 | return EXTENSION_OK; |
630 | 0 | } |
631 | | |
632 | 0 | return EXTENSION_UNKNOWN; |
633 | 0 | } |
634 | | |
635 | | /* |
636 | | * Record any new extensions in this function. |
637 | | */ |
638 | | static enum extension_result handle_extension(const char *var, |
639 | | const char *value, |
640 | | const char *ext, |
641 | | struct repository_format *data) |
642 | 0 | { |
643 | 0 | if (!strcmp(ext, "noop-v1")) { |
644 | 0 | return EXTENSION_OK; |
645 | 0 | } else if (!strcmp(ext, "objectformat")) { |
646 | 0 | int format; |
647 | |
|
648 | 0 | if (!value) |
649 | 0 | return config_error_nonbool(var); |
650 | 0 | format = hash_algo_by_name(value); |
651 | 0 | if (format == GIT_HASH_UNKNOWN) |
652 | 0 | return error(_("invalid value for '%s': '%s'"), |
653 | 0 | "extensions.objectformat", value); |
654 | 0 | data->hash_algo = format; |
655 | 0 | return EXTENSION_OK; |
656 | 0 | } else if (!strcmp(ext, "compatobjectformat")) { |
657 | 0 | struct string_list_item *item; |
658 | 0 | int format; |
659 | |
|
660 | 0 | if (!value) |
661 | 0 | return config_error_nonbool(var); |
662 | 0 | format = hash_algo_by_name(value); |
663 | 0 | if (format == GIT_HASH_UNKNOWN) |
664 | 0 | return error(_("invalid value for '%s': '%s'"), |
665 | 0 | "extensions.compatobjectformat", value); |
666 | | /* For now only support compatObjectFormat being specified once. */ |
667 | 0 | for_each_string_list_item(item, &data->v1_only_extensions) { |
668 | 0 | if (!strcmp(item->string, "compatobjectformat")) |
669 | 0 | return error(_("'%s' already specified as '%s'"), |
670 | 0 | "extensions.compatobjectformat", |
671 | 0 | hash_algos[data->compat_hash_algo].name); |
672 | 0 | } |
673 | 0 | data->compat_hash_algo = format; |
674 | 0 | return EXTENSION_OK; |
675 | 0 | } else if (!strcmp(ext, "refstorage")) { |
676 | 0 | unsigned int format; |
677 | |
|
678 | 0 | if (!value) |
679 | 0 | return config_error_nonbool(var); |
680 | 0 | format = ref_storage_format_by_name(value); |
681 | 0 | if (format == REF_STORAGE_FORMAT_UNKNOWN) |
682 | 0 | return error(_("invalid value for '%s': '%s'"), |
683 | 0 | "extensions.refstorage", value); |
684 | 0 | data->ref_storage_format = format; |
685 | 0 | return EXTENSION_OK; |
686 | 0 | } else if (!strcmp(ext, "relativeworktrees")) { |
687 | 0 | data->relative_worktrees = git_config_bool(var, value); |
688 | 0 | return EXTENSION_OK; |
689 | 0 | } |
690 | 0 | return EXTENSION_UNKNOWN; |
691 | 0 | } |
692 | | |
693 | | static int check_repo_format(const char *var, const char *value, |
694 | | const struct config_context *ctx, void *vdata) |
695 | 0 | { |
696 | 0 | struct repository_format *data = vdata; |
697 | 0 | const char *ext; |
698 | |
|
699 | 0 | if (strcmp(var, "core.repositoryformatversion") == 0) |
700 | 0 | data->version = git_config_int(var, value, ctx->kvi); |
701 | 0 | else if (skip_prefix(var, "extensions.", &ext)) { |
702 | 0 | switch (handle_extension_v0(var, value, ext, data)) { |
703 | 0 | case EXTENSION_ERROR: |
704 | 0 | return -1; |
705 | 0 | case EXTENSION_OK: |
706 | 0 | return 0; |
707 | 0 | case EXTENSION_UNKNOWN: |
708 | 0 | break; |
709 | 0 | } |
710 | | |
711 | 0 | switch (handle_extension(var, value, ext, data)) { |
712 | 0 | case EXTENSION_ERROR: |
713 | 0 | return -1; |
714 | 0 | case EXTENSION_OK: |
715 | 0 | string_list_append(&data->v1_only_extensions, ext); |
716 | 0 | return 0; |
717 | 0 | case EXTENSION_UNKNOWN: |
718 | 0 | string_list_append(&data->unknown_extensions, ext); |
719 | 0 | return 0; |
720 | 0 | } |
721 | 0 | } |
722 | | |
723 | 0 | return read_worktree_config(var, value, ctx, vdata); |
724 | 0 | } |
725 | | |
726 | | static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok) |
727 | 0 | { |
728 | 0 | struct strbuf sb = STRBUF_INIT; |
729 | 0 | struct strbuf err = STRBUF_INIT; |
730 | 0 | int has_common; |
731 | |
|
732 | 0 | has_common = get_common_dir(&sb, gitdir); |
733 | 0 | strbuf_addstr(&sb, "/config"); |
734 | 0 | read_repository_format(candidate, sb.buf); |
735 | 0 | strbuf_release(&sb); |
736 | | |
737 | | /* |
738 | | * For historical use of check_repository_format() in git-init, |
739 | | * we treat a missing config as a silent "ok", even when nongit_ok |
740 | | * is unset. |
741 | | */ |
742 | 0 | if (candidate->version < 0) |
743 | 0 | return 0; |
744 | | |
745 | 0 | if (verify_repository_format(candidate, &err) < 0) { |
746 | 0 | if (nongit_ok) { |
747 | 0 | warning("%s", err.buf); |
748 | 0 | strbuf_release(&err); |
749 | 0 | *nongit_ok = -1; |
750 | 0 | return -1; |
751 | 0 | } |
752 | 0 | die("%s", err.buf); |
753 | 0 | } |
754 | | |
755 | 0 | the_repository->repository_format_precious_objects = candidate->precious_objects; |
756 | |
|
757 | 0 | string_list_clear(&candidate->unknown_extensions, 0); |
758 | 0 | string_list_clear(&candidate->v1_only_extensions, 0); |
759 | |
|
760 | 0 | if (candidate->worktree_config) { |
761 | | /* |
762 | | * pick up core.bare and core.worktree from per-worktree |
763 | | * config if present |
764 | | */ |
765 | 0 | strbuf_addf(&sb, "%s/config.worktree", gitdir); |
766 | 0 | git_config_from_file(read_worktree_config, sb.buf, candidate); |
767 | 0 | strbuf_release(&sb); |
768 | 0 | has_common = 0; |
769 | 0 | } |
770 | |
|
771 | 0 | if (!has_common) { |
772 | 0 | if (candidate->is_bare != -1) { |
773 | 0 | is_bare_repository_cfg = candidate->is_bare; |
774 | 0 | if (is_bare_repository_cfg == 1) |
775 | 0 | inside_work_tree = -1; |
776 | 0 | } |
777 | 0 | if (candidate->work_tree) { |
778 | 0 | free(git_work_tree_cfg); |
779 | 0 | git_work_tree_cfg = xstrdup(candidate->work_tree); |
780 | 0 | inside_work_tree = -1; |
781 | 0 | } |
782 | 0 | } |
783 | |
|
784 | 0 | return 0; |
785 | 0 | } |
786 | | |
787 | | int upgrade_repository_format(int target_version) |
788 | 0 | { |
789 | 0 | struct strbuf sb = STRBUF_INIT; |
790 | 0 | struct strbuf err = STRBUF_INIT; |
791 | 0 | struct strbuf repo_version = STRBUF_INIT; |
792 | 0 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
793 | 0 | int ret; |
794 | |
|
795 | 0 | repo_common_path_append(the_repository, &sb, "config"); |
796 | 0 | read_repository_format(&repo_fmt, sb.buf); |
797 | 0 | strbuf_release(&sb); |
798 | |
|
799 | 0 | if (repo_fmt.version >= target_version) { |
800 | 0 | ret = 0; |
801 | 0 | goto out; |
802 | 0 | } |
803 | | |
804 | 0 | if (verify_repository_format(&repo_fmt, &err) < 0) { |
805 | 0 | ret = error("cannot upgrade repository format from %d to %d: %s", |
806 | 0 | repo_fmt.version, target_version, err.buf); |
807 | 0 | goto out; |
808 | 0 | } |
809 | 0 | if (!repo_fmt.version && repo_fmt.unknown_extensions.nr) { |
810 | 0 | ret = error("cannot upgrade repository format: " |
811 | 0 | "unknown extension %s", |
812 | 0 | repo_fmt.unknown_extensions.items[0].string); |
813 | 0 | goto out; |
814 | 0 | } |
815 | | |
816 | 0 | strbuf_addf(&repo_version, "%d", target_version); |
817 | 0 | repo_config_set(the_repository, "core.repositoryformatversion", repo_version.buf); |
818 | |
|
819 | 0 | ret = 1; |
820 | |
|
821 | 0 | out: |
822 | 0 | clear_repository_format(&repo_fmt); |
823 | 0 | strbuf_release(&repo_version); |
824 | 0 | strbuf_release(&err); |
825 | 0 | return ret; |
826 | 0 | } |
827 | | |
828 | | static void init_repository_format(struct repository_format *format) |
829 | 0 | { |
830 | 0 | const struct repository_format fresh = REPOSITORY_FORMAT_INIT; |
831 | |
|
832 | 0 | memcpy(format, &fresh, sizeof(fresh)); |
833 | 0 | } |
834 | | |
835 | | int read_repository_format(struct repository_format *format, const char *path) |
836 | 0 | { |
837 | 0 | clear_repository_format(format); |
838 | 0 | format->hash_algo = GIT_HASH_SHA1_LEGACY; |
839 | 0 | git_config_from_file(check_repo_format, path, format); |
840 | 0 | if (format->version == -1) { |
841 | 0 | clear_repository_format(format); |
842 | 0 | format->hash_algo = GIT_HASH_SHA1_LEGACY; |
843 | 0 | } |
844 | 0 | return format->version; |
845 | 0 | } |
846 | | |
847 | | void clear_repository_format(struct repository_format *format) |
848 | 0 | { |
849 | 0 | string_list_clear(&format->unknown_extensions, 0); |
850 | 0 | string_list_clear(&format->v1_only_extensions, 0); |
851 | 0 | free(format->work_tree); |
852 | 0 | free(format->partial_clone); |
853 | 0 | init_repository_format(format); |
854 | 0 | } |
855 | | |
856 | | int verify_repository_format(const struct repository_format *format, |
857 | | struct strbuf *err) |
858 | 0 | { |
859 | 0 | if (GIT_REPO_VERSION_READ < format->version) { |
860 | 0 | strbuf_addf(err, _("Expected git repo version <= %d, found %d"), |
861 | 0 | GIT_REPO_VERSION_READ, format->version); |
862 | 0 | return -1; |
863 | 0 | } |
864 | | |
865 | 0 | if (format->version >= 1 && format->unknown_extensions.nr) { |
866 | 0 | int i; |
867 | |
|
868 | 0 | strbuf_addstr(err, Q_("unknown repository extension found:", |
869 | 0 | "unknown repository extensions found:", |
870 | 0 | format->unknown_extensions.nr)); |
871 | |
|
872 | 0 | for (i = 0; i < format->unknown_extensions.nr; i++) |
873 | 0 | strbuf_addf(err, "\n\t%s", |
874 | 0 | format->unknown_extensions.items[i].string); |
875 | 0 | return -1; |
876 | 0 | } |
877 | | |
878 | 0 | if (format->version == 0 && format->v1_only_extensions.nr) { |
879 | 0 | int i; |
880 | |
|
881 | 0 | strbuf_addstr(err, |
882 | 0 | Q_("repo version is 0, but v1-only extension found:", |
883 | 0 | "repo version is 0, but v1-only extensions found:", |
884 | 0 | format->v1_only_extensions.nr)); |
885 | |
|
886 | 0 | for (i = 0; i < format->v1_only_extensions.nr; i++) |
887 | 0 | strbuf_addf(err, "\n\t%s", |
888 | 0 | format->v1_only_extensions.items[i].string); |
889 | 0 | return -1; |
890 | 0 | } |
891 | | |
892 | 0 | return 0; |
893 | 0 | } |
894 | | |
895 | | void read_gitfile_error_die(int error_code, const char *path, const char *dir) |
896 | 0 | { |
897 | 0 | switch (error_code) { |
898 | 0 | case READ_GITFILE_ERR_STAT_FAILED: |
899 | 0 | case READ_GITFILE_ERR_NOT_A_FILE: |
900 | | /* non-fatal; follow return path */ |
901 | 0 | break; |
902 | 0 | case READ_GITFILE_ERR_OPEN_FAILED: |
903 | 0 | die_errno(_("error opening '%s'"), path); |
904 | 0 | case READ_GITFILE_ERR_TOO_LARGE: |
905 | 0 | die(_("too large to be a .git file: '%s'"), path); |
906 | 0 | case READ_GITFILE_ERR_READ_FAILED: |
907 | 0 | die(_("error reading %s"), path); |
908 | 0 | case READ_GITFILE_ERR_INVALID_FORMAT: |
909 | 0 | die(_("invalid gitfile format: %s"), path); |
910 | 0 | case READ_GITFILE_ERR_NO_PATH: |
911 | 0 | die(_("no path in gitfile: %s"), path); |
912 | 0 | case READ_GITFILE_ERR_NOT_A_REPO: |
913 | 0 | die(_("not a git repository: %s"), dir); |
914 | 0 | default: |
915 | 0 | BUG("unknown error code"); |
916 | 0 | } |
917 | 0 | } |
918 | | |
919 | | /* |
920 | | * Try to read the location of the git directory from the .git file, |
921 | | * return path to git directory if found. The return value comes from |
922 | | * a shared buffer. |
923 | | * |
924 | | * On failure, if return_error_code is not NULL, return_error_code |
925 | | * will be set to an error code and NULL will be returned. If |
926 | | * return_error_code is NULL the function will die instead (for most |
927 | | * cases). |
928 | | */ |
929 | | const char *read_gitfile_gently(const char *path, int *return_error_code) |
930 | 0 | { |
931 | 0 | const int max_file_size = 1 << 20; /* 1MB */ |
932 | 0 | int error_code = 0; |
933 | 0 | char *buf = NULL; |
934 | 0 | char *dir = NULL; |
935 | 0 | const char *slash; |
936 | 0 | struct stat st; |
937 | 0 | int fd; |
938 | 0 | ssize_t len; |
939 | 0 | static struct strbuf realpath = STRBUF_INIT; |
940 | |
|
941 | 0 | if (stat(path, &st)) { |
942 | | /* NEEDSWORK: discern between ENOENT vs other errors */ |
943 | 0 | error_code = READ_GITFILE_ERR_STAT_FAILED; |
944 | 0 | goto cleanup_return; |
945 | 0 | } |
946 | 0 | if (!S_ISREG(st.st_mode)) { |
947 | 0 | error_code = READ_GITFILE_ERR_NOT_A_FILE; |
948 | 0 | goto cleanup_return; |
949 | 0 | } |
950 | 0 | if (st.st_size > max_file_size) { |
951 | 0 | error_code = READ_GITFILE_ERR_TOO_LARGE; |
952 | 0 | goto cleanup_return; |
953 | 0 | } |
954 | 0 | fd = open(path, O_RDONLY); |
955 | 0 | if (fd < 0) { |
956 | 0 | error_code = READ_GITFILE_ERR_OPEN_FAILED; |
957 | 0 | goto cleanup_return; |
958 | 0 | } |
959 | 0 | buf = xmallocz(st.st_size); |
960 | 0 | len = read_in_full(fd, buf, st.st_size); |
961 | 0 | close(fd); |
962 | 0 | if (len != st.st_size) { |
963 | 0 | error_code = READ_GITFILE_ERR_READ_FAILED; |
964 | 0 | goto cleanup_return; |
965 | 0 | } |
966 | 0 | if (!starts_with(buf, "gitdir: ")) { |
967 | 0 | error_code = READ_GITFILE_ERR_INVALID_FORMAT; |
968 | 0 | goto cleanup_return; |
969 | 0 | } |
970 | 0 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') |
971 | 0 | len--; |
972 | 0 | if (len < 9) { |
973 | 0 | error_code = READ_GITFILE_ERR_NO_PATH; |
974 | 0 | goto cleanup_return; |
975 | 0 | } |
976 | 0 | buf[len] = '\0'; |
977 | 0 | dir = buf + 8; |
978 | |
|
979 | 0 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { |
980 | 0 | size_t pathlen = slash+1 - path; |
981 | 0 | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, |
982 | 0 | (int)(len - 8), buf + 8); |
983 | 0 | free(buf); |
984 | 0 | buf = dir; |
985 | 0 | } |
986 | 0 | if (!is_git_directory(dir)) { |
987 | 0 | error_code = READ_GITFILE_ERR_NOT_A_REPO; |
988 | 0 | goto cleanup_return; |
989 | 0 | } |
990 | | |
991 | 0 | strbuf_realpath(&realpath, dir, 1); |
992 | 0 | path = realpath.buf; |
993 | |
|
994 | 0 | cleanup_return: |
995 | 0 | if (return_error_code) |
996 | 0 | *return_error_code = error_code; |
997 | 0 | else if (error_code) |
998 | 0 | read_gitfile_error_die(error_code, path, dir); |
999 | |
|
1000 | 0 | free(buf); |
1001 | 0 | return error_code ? NULL : path; |
1002 | 0 | } |
1003 | | |
1004 | | static void setup_git_env_internal(const char *git_dir, |
1005 | | bool skip_initializing_odb) |
1006 | 0 | { |
1007 | 0 | char *git_replace_ref_base; |
1008 | 0 | const char *shallow_file; |
1009 | 0 | const char *replace_ref_base; |
1010 | 0 | struct set_gitdir_args args = { NULL }; |
1011 | 0 | struct strvec to_free = STRVEC_INIT; |
1012 | |
|
1013 | 0 | args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT); |
1014 | 0 | args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT); |
1015 | 0 | args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT); |
1016 | 0 | args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT); |
1017 | 0 | args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT); |
1018 | 0 | if (getenv(GIT_QUARANTINE_ENVIRONMENT)) |
1019 | 0 | args.disable_ref_updates = true; |
1020 | 0 | args.skip_initializing_odb = skip_initializing_odb; |
1021 | |
|
1022 | 0 | repo_set_gitdir(the_repository, git_dir, &args); |
1023 | 0 | strvec_clear(&to_free); |
1024 | |
|
1025 | 0 | if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT)) |
1026 | 0 | disable_replace_refs(); |
1027 | 0 | replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT); |
1028 | 0 | git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base |
1029 | 0 | : "refs/replace/"); |
1030 | 0 | update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base); |
1031 | |
|
1032 | 0 | shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT); |
1033 | 0 | if (shallow_file) |
1034 | 0 | set_alternate_shallow_file(the_repository, shallow_file, 0); |
1035 | |
|
1036 | 0 | if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0)) |
1037 | 0 | fetch_if_missing = 0; |
1038 | 0 | } |
1039 | | |
1040 | | void setup_git_env(const char *git_dir) |
1041 | 0 | { |
1042 | 0 | setup_git_env_internal(git_dir, false); |
1043 | 0 | } |
1044 | | |
1045 | | static void set_git_dir_1(const char *path, bool skip_initializing_odb) |
1046 | 0 | { |
1047 | 0 | xsetenv(GIT_DIR_ENVIRONMENT, path, 1); |
1048 | 0 | setup_git_env_internal(path, skip_initializing_odb); |
1049 | 0 | } |
1050 | | |
1051 | | static void update_relative_gitdir(const char *name UNUSED, |
1052 | | const char *old_cwd, |
1053 | | const char *new_cwd, |
1054 | | void *data UNUSED) |
1055 | 0 | { |
1056 | 0 | char *path = reparent_relative_path(old_cwd, new_cwd, |
1057 | 0 | repo_get_git_dir(the_repository)); |
1058 | 0 | trace_printf_key(&trace_setup_key, |
1059 | 0 | "setup: move $GIT_DIR to '%s'", |
1060 | 0 | path); |
1061 | 0 | set_git_dir_1(path, true); |
1062 | 0 | free(path); |
1063 | 0 | } |
1064 | | |
1065 | | static void set_git_dir(const char *path, int make_realpath) |
1066 | 0 | { |
1067 | 0 | struct strbuf realpath = STRBUF_INIT; |
1068 | |
|
1069 | 0 | if (make_realpath) { |
1070 | 0 | strbuf_realpath(&realpath, path, 1); |
1071 | 0 | path = realpath.buf; |
1072 | 0 | } |
1073 | |
|
1074 | 0 | set_git_dir_1(path, false); |
1075 | 0 | if (!is_absolute_path(path)) |
1076 | 0 | chdir_notify_register(NULL, update_relative_gitdir, NULL); |
1077 | |
|
1078 | 0 | strbuf_release(&realpath); |
1079 | 0 | } |
1080 | | |
1081 | | static const char *setup_explicit_git_dir(const char *gitdirenv, |
1082 | | struct strbuf *cwd, |
1083 | | struct repository_format *repo_fmt, |
1084 | | int *nongit_ok) |
1085 | 0 | { |
1086 | 0 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
1087 | 0 | const char *worktree; |
1088 | 0 | char *gitfile; |
1089 | 0 | int offset; |
1090 | |
|
1091 | 0 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
1092 | 0 | die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT); |
1093 | | |
1094 | 0 | gitfile = (char*)read_gitfile(gitdirenv); |
1095 | 0 | if (gitfile) { |
1096 | 0 | gitfile = xstrdup(gitfile); |
1097 | 0 | gitdirenv = gitfile; |
1098 | 0 | } |
1099 | |
|
1100 | 0 | if (!is_git_directory(gitdirenv)) { |
1101 | 0 | if (nongit_ok) { |
1102 | 0 | *nongit_ok = 1; |
1103 | 0 | free(gitfile); |
1104 | 0 | return NULL; |
1105 | 0 | } |
1106 | 0 | die(_("not a git repository: '%s'"), gitdirenv); |
1107 | 0 | } |
1108 | | |
1109 | 0 | if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) { |
1110 | 0 | free(gitfile); |
1111 | 0 | return NULL; |
1112 | 0 | } |
1113 | | |
1114 | | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ |
1115 | 0 | if (work_tree_env) |
1116 | 0 | set_git_work_tree(work_tree_env); |
1117 | 0 | else if (is_bare_repository_cfg > 0) { |
1118 | 0 | if (git_work_tree_cfg) { |
1119 | | /* #22.2, #30 */ |
1120 | 0 | warning("core.bare and core.worktree do not make sense"); |
1121 | 0 | work_tree_config_is_bogus = 1; |
1122 | 0 | } |
1123 | | |
1124 | | /* #18, #26 */ |
1125 | 0 | set_git_dir(gitdirenv, 0); |
1126 | 0 | free(gitfile); |
1127 | 0 | return NULL; |
1128 | 0 | } |
1129 | 0 | else if (git_work_tree_cfg) { /* #6, #14 */ |
1130 | 0 | if (is_absolute_path(git_work_tree_cfg)) |
1131 | 0 | set_git_work_tree(git_work_tree_cfg); |
1132 | 0 | else { |
1133 | 0 | char *core_worktree; |
1134 | 0 | if (chdir(gitdirenv)) |
1135 | 0 | die_errno(_("cannot chdir to '%s'"), gitdirenv); |
1136 | 0 | if (chdir(git_work_tree_cfg)) |
1137 | 0 | die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg); |
1138 | 0 | core_worktree = xgetcwd(); |
1139 | 0 | if (chdir(cwd->buf)) |
1140 | 0 | die_errno(_("cannot come back to cwd")); |
1141 | 0 | set_git_work_tree(core_worktree); |
1142 | 0 | free(core_worktree); |
1143 | 0 | } |
1144 | 0 | } |
1145 | 0 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
1146 | | /* #16d */ |
1147 | 0 | set_git_dir(gitdirenv, 0); |
1148 | 0 | free(gitfile); |
1149 | 0 | return NULL; |
1150 | 0 | } |
1151 | 0 | else /* #2, #10 */ |
1152 | 0 | set_git_work_tree("."); |
1153 | | |
1154 | | /* set_git_work_tree() must have been called by now */ |
1155 | 0 | worktree = repo_get_work_tree(the_repository); |
1156 | | |
1157 | | /* both repo_get_work_tree() and cwd are already normalized */ |
1158 | 0 | if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ |
1159 | 0 | set_git_dir(gitdirenv, 0); |
1160 | 0 | free(gitfile); |
1161 | 0 | return NULL; |
1162 | 0 | } |
1163 | | |
1164 | 0 | offset = dir_inside_of(cwd->buf, worktree); |
1165 | 0 | if (offset >= 0) { /* cwd inside worktree? */ |
1166 | 0 | set_git_dir(gitdirenv, 1); |
1167 | 0 | if (chdir(worktree)) |
1168 | 0 | die_errno(_("cannot chdir to '%s'"), worktree); |
1169 | 0 | strbuf_addch(cwd, '/'); |
1170 | 0 | free(gitfile); |
1171 | 0 | return cwd->buf + offset; |
1172 | 0 | } |
1173 | | |
1174 | | /* cwd outside worktree */ |
1175 | 0 | set_git_dir(gitdirenv, 0); |
1176 | 0 | free(gitfile); |
1177 | 0 | return NULL; |
1178 | 0 | } |
1179 | | |
1180 | | static const char *setup_discovered_git_dir(const char *gitdir, |
1181 | | struct strbuf *cwd, int offset, |
1182 | | struct repository_format *repo_fmt, |
1183 | | int *nongit_ok) |
1184 | 0 | { |
1185 | 0 | if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok)) |
1186 | 0 | return NULL; |
1187 | | |
1188 | | /* --work-tree is set without --git-dir; use discovered one */ |
1189 | 0 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { |
1190 | 0 | char *to_free = NULL; |
1191 | 0 | const char *ret; |
1192 | |
|
1193 | 0 | if (offset != cwd->len && !is_absolute_path(gitdir)) |
1194 | 0 | gitdir = to_free = real_pathdup(gitdir, 1); |
1195 | 0 | if (chdir(cwd->buf)) |
1196 | 0 | die_errno(_("cannot come back to cwd")); |
1197 | 0 | ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
1198 | 0 | free(to_free); |
1199 | 0 | return ret; |
1200 | 0 | } |
1201 | | |
1202 | | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
1203 | 0 | if (is_bare_repository_cfg > 0) { |
1204 | 0 | set_git_dir(gitdir, (offset != cwd->len)); |
1205 | 0 | if (chdir(cwd->buf)) |
1206 | 0 | die_errno(_("cannot come back to cwd")); |
1207 | 0 | return NULL; |
1208 | 0 | } |
1209 | | |
1210 | | /* #0, #1, #5, #8, #9, #12, #13 */ |
1211 | 0 | set_git_work_tree("."); |
1212 | 0 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) |
1213 | 0 | set_git_dir(gitdir, 0); |
1214 | 0 | inside_git_dir = 0; |
1215 | 0 | inside_work_tree = 1; |
1216 | 0 | if (offset >= cwd->len) |
1217 | 0 | return NULL; |
1218 | | |
1219 | | /* Make "offset" point past the '/' (already the case for root dirs) */ |
1220 | 0 | if (offset != offset_1st_component(cwd->buf)) |
1221 | 0 | offset++; |
1222 | | /* Add a '/' at the end */ |
1223 | 0 | strbuf_addch(cwd, '/'); |
1224 | 0 | return cwd->buf + offset; |
1225 | 0 | } |
1226 | | |
1227 | | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
1228 | | static const char *setup_bare_git_dir(struct strbuf *cwd, int offset, |
1229 | | struct repository_format *repo_fmt, |
1230 | | int *nongit_ok) |
1231 | 0 | { |
1232 | 0 | int root_len; |
1233 | |
|
1234 | 0 | if (check_repository_format_gently(".", repo_fmt, nongit_ok)) |
1235 | 0 | return NULL; |
1236 | | |
1237 | 0 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
1238 | | |
1239 | | /* --work-tree is set without --git-dir; use discovered one */ |
1240 | 0 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { |
1241 | 0 | static const char *gitdir; |
1242 | |
|
1243 | 0 | gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset); |
1244 | 0 | if (chdir(cwd->buf)) |
1245 | 0 | die_errno(_("cannot come back to cwd")); |
1246 | 0 | return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok); |
1247 | 0 | } |
1248 | | |
1249 | 0 | inside_git_dir = 1; |
1250 | 0 | inside_work_tree = 0; |
1251 | 0 | if (offset != cwd->len) { |
1252 | 0 | if (chdir(cwd->buf)) |
1253 | 0 | die_errno(_("cannot come back to cwd")); |
1254 | 0 | root_len = offset_1st_component(cwd->buf); |
1255 | 0 | strbuf_setlen(cwd, offset > root_len ? offset : root_len); |
1256 | 0 | set_git_dir(cwd->buf, 0); |
1257 | 0 | } |
1258 | 0 | else |
1259 | 0 | set_git_dir(".", 0); |
1260 | 0 | return NULL; |
1261 | 0 | } |
1262 | | |
1263 | | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
1264 | 0 | { |
1265 | 0 | struct stat buf; |
1266 | 0 | if (stat(path, &buf)) { |
1267 | 0 | die_errno(_("failed to stat '%*s%s%s'"), |
1268 | 0 | prefix_len, |
1269 | 0 | prefix ? prefix : "", |
1270 | 0 | prefix ? "/" : "", path); |
1271 | 0 | } |
1272 | 0 | return buf.st_dev; |
1273 | 0 | } |
1274 | | |
1275 | | /* |
1276 | | * A "string_list_each_func_t" function that canonicalizes an entry |
1277 | | * from GIT_CEILING_DIRECTORIES using real_pathdup(), or |
1278 | | * discards it if unusable. The presence of an empty entry in |
1279 | | * GIT_CEILING_DIRECTORIES turns off canonicalization for all |
1280 | | * subsequent entries. |
1281 | | */ |
1282 | | static int canonicalize_ceiling_entry(struct string_list_item *item, |
1283 | | void *cb_data) |
1284 | 0 | { |
1285 | 0 | int *empty_entry_found = cb_data; |
1286 | 0 | char *ceil = item->string; |
1287 | |
|
1288 | 0 | if (!*ceil) { |
1289 | 0 | *empty_entry_found = 1; |
1290 | 0 | return 0; |
1291 | 0 | } else if (!is_absolute_path(ceil)) { |
1292 | 0 | return 0; |
1293 | 0 | } else if (*empty_entry_found) { |
1294 | | /* Keep entry but do not canonicalize it */ |
1295 | 0 | return 1; |
1296 | 0 | } else { |
1297 | 0 | char *real_path = real_pathdup(ceil, 0); |
1298 | 0 | if (!real_path) { |
1299 | 0 | return 0; |
1300 | 0 | } |
1301 | 0 | free(item->string); |
1302 | 0 | item->string = real_path; |
1303 | 0 | return 1; |
1304 | 0 | } |
1305 | 0 | } |
1306 | | |
1307 | | struct safe_directory_data { |
1308 | | char *path; |
1309 | | int is_safe; |
1310 | | }; |
1311 | | |
1312 | | static int safe_directory_cb(const char *key, const char *value, |
1313 | | const struct config_context *ctx UNUSED, void *d) |
1314 | 0 | { |
1315 | 0 | struct safe_directory_data *data = d; |
1316 | |
|
1317 | 0 | if (strcmp(key, "safe.directory")) |
1318 | 0 | return 0; |
1319 | | |
1320 | 0 | if (!value || !*value) { |
1321 | 0 | data->is_safe = 0; |
1322 | 0 | } else if (!strcmp(value, "*")) { |
1323 | 0 | data->is_safe = 1; |
1324 | 0 | } else { |
1325 | 0 | char *allowed = NULL; |
1326 | |
|
1327 | 0 | if (!git_config_pathname(&allowed, key, value) && allowed) { |
1328 | 0 | char *normalized = NULL; |
1329 | | |
1330 | | /* |
1331 | | * Setting safe.directory to a non-absolute path |
1332 | | * makes little sense---it won't be relative to |
1333 | | * the configuration file the item is defined in. |
1334 | | * Except for ".", which means "if we are at the top |
1335 | | * level of a repository, then it is OK", which is |
1336 | | * slightly tighter than "*" that allows discovery. |
1337 | | */ |
1338 | 0 | if (!is_absolute_path(allowed) && strcmp(allowed, ".")) { |
1339 | 0 | warning(_("safe.directory '%s' not absolute"), |
1340 | 0 | allowed); |
1341 | 0 | goto next; |
1342 | 0 | } |
1343 | | |
1344 | | /* |
1345 | | * A .gitconfig in $HOME may be shared across |
1346 | | * different machines and safe.directory entries |
1347 | | * may or may not exist as paths on all of these |
1348 | | * machines. In other words, it is not a warning |
1349 | | * worthy event when there is no such path on this |
1350 | | * machine---the entry may be useful elsewhere. |
1351 | | */ |
1352 | 0 | normalized = real_pathdup(allowed, 0); |
1353 | 0 | if (!normalized) |
1354 | 0 | goto next; |
1355 | | |
1356 | 0 | if (ends_with(normalized, "/*")) { |
1357 | 0 | size_t len = strlen(normalized); |
1358 | 0 | if (!fspathncmp(normalized, data->path, len - 1)) |
1359 | 0 | data->is_safe = 1; |
1360 | 0 | } else if (!fspathcmp(data->path, normalized)) { |
1361 | 0 | data->is_safe = 1; |
1362 | 0 | } |
1363 | 0 | next: |
1364 | 0 | free(normalized); |
1365 | 0 | free(allowed); |
1366 | 0 | } |
1367 | 0 | } |
1368 | | |
1369 | 0 | return 0; |
1370 | 0 | } |
1371 | | |
1372 | | /* |
1373 | | * Check if a repository is safe, by verifying the ownership of the |
1374 | | * worktree (if any), the git directory, and the gitfile (if any). |
1375 | | * |
1376 | | * Exemptions for known-safe repositories can be added via `safe.directory` |
1377 | | * config settings; for non-bare repositories, their worktree needs to be |
1378 | | * added, for bare ones their git directory. |
1379 | | */ |
1380 | | static int ensure_valid_ownership(const char *gitfile, |
1381 | | const char *worktree, const char *gitdir, |
1382 | | struct strbuf *report) |
1383 | 0 | { |
1384 | 0 | struct safe_directory_data data = { 0 }; |
1385 | |
|
1386 | 0 | if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) && |
1387 | 0 | (!gitfile || is_path_owned_by_current_user(gitfile, report)) && |
1388 | 0 | (!worktree || is_path_owned_by_current_user(worktree, report)) && |
1389 | 0 | (!gitdir || is_path_owned_by_current_user(gitdir, report))) |
1390 | 0 | return 1; |
1391 | | |
1392 | | /* |
1393 | | * normalize the data.path for comparison with normalized paths |
1394 | | * that come from the configuration file. The path is unsafe |
1395 | | * if it cannot be normalized. |
1396 | | */ |
1397 | 0 | data.path = real_pathdup(worktree ? worktree : gitdir, 0); |
1398 | 0 | if (!data.path) |
1399 | 0 | return 0; |
1400 | | |
1401 | | /* |
1402 | | * data.path is the "path" that identifies the repository and it is |
1403 | | * constant regardless of what failed above. data.is_safe should be |
1404 | | * initialized to false, and might be changed by the callback. |
1405 | | */ |
1406 | 0 | git_protected_config(safe_directory_cb, &data); |
1407 | |
|
1408 | 0 | free(data.path); |
1409 | 0 | return data.is_safe; |
1410 | 0 | } |
1411 | | |
1412 | | void die_upon_dubious_ownership(const char *gitfile, const char *worktree, |
1413 | | const char *gitdir) |
1414 | 0 | { |
1415 | 0 | struct strbuf report = STRBUF_INIT, quoted = STRBUF_INIT; |
1416 | 0 | const char *path; |
1417 | |
|
1418 | 0 | if (ensure_valid_ownership(gitfile, worktree, gitdir, &report)) |
1419 | 0 | return; |
1420 | | |
1421 | 0 | strbuf_complete(&report, '\n'); |
1422 | 0 | path = gitfile ? gitfile : gitdir; |
1423 | 0 | sq_quote_buf_pretty("ed, path); |
1424 | |
|
1425 | 0 | die(_("detected dubious ownership in repository at '%s'\n" |
1426 | 0 | "%s" |
1427 | 0 | "To add an exception for this directory, call:\n" |
1428 | 0 | "\n" |
1429 | 0 | "\tgit config --global --add safe.directory %s"), |
1430 | 0 | path, report.buf, quoted.buf); |
1431 | 0 | } |
1432 | | |
1433 | | static int allowed_bare_repo_cb(const char *key, const char *value, |
1434 | | const struct config_context *ctx UNUSED, |
1435 | | void *d) |
1436 | 0 | { |
1437 | 0 | enum allowed_bare_repo *allowed_bare_repo = d; |
1438 | |
|
1439 | 0 | if (strcasecmp(key, "safe.bareRepository")) |
1440 | 0 | return 0; |
1441 | | |
1442 | 0 | if (!strcmp(value, "explicit")) { |
1443 | 0 | *allowed_bare_repo = ALLOWED_BARE_REPO_EXPLICIT; |
1444 | 0 | return 0; |
1445 | 0 | } |
1446 | 0 | if (!strcmp(value, "all")) { |
1447 | 0 | *allowed_bare_repo = ALLOWED_BARE_REPO_ALL; |
1448 | 0 | return 0; |
1449 | 0 | } |
1450 | 0 | return -1; |
1451 | 0 | } |
1452 | | |
1453 | | static enum allowed_bare_repo get_allowed_bare_repo(void) |
1454 | 0 | { |
1455 | 0 | enum allowed_bare_repo result = ALLOWED_BARE_REPO_ALL; |
1456 | 0 | git_protected_config(allowed_bare_repo_cb, &result); |
1457 | 0 | return result; |
1458 | 0 | } |
1459 | | |
1460 | | static const char *allowed_bare_repo_to_string( |
1461 | | enum allowed_bare_repo allowed_bare_repo) |
1462 | 0 | { |
1463 | 0 | switch (allowed_bare_repo) { |
1464 | 0 | case ALLOWED_BARE_REPO_EXPLICIT: |
1465 | 0 | return "explicit"; |
1466 | 0 | case ALLOWED_BARE_REPO_ALL: |
1467 | 0 | return "all"; |
1468 | 0 | default: |
1469 | 0 | BUG("invalid allowed_bare_repo %d", |
1470 | 0 | allowed_bare_repo); |
1471 | 0 | } |
1472 | 0 | return NULL; |
1473 | 0 | } |
1474 | | |
1475 | | static int is_implicit_bare_repo(const char *path) |
1476 | 0 | { |
1477 | | /* |
1478 | | * what we found is a ".git" directory at the root of |
1479 | | * the working tree. |
1480 | | */ |
1481 | 0 | if (ends_with_path_components(path, ".git")) |
1482 | 0 | return 1; |
1483 | | |
1484 | | /* |
1485 | | * we are inside $GIT_DIR of a secondary worktree of a |
1486 | | * non-bare repository. |
1487 | | */ |
1488 | 0 | if (strstr(path, "/.git/worktrees/")) |
1489 | 0 | return 1; |
1490 | | |
1491 | | /* |
1492 | | * we are inside $GIT_DIR of a worktree of a non-embedded |
1493 | | * submodule, whose superproject is not a bare repository. |
1494 | | */ |
1495 | 0 | if (strstr(path, "/.git/modules/")) |
1496 | 0 | return 1; |
1497 | | |
1498 | 0 | return 0; |
1499 | 0 | } |
1500 | | |
1501 | | /* |
1502 | | * We cannot decide in this function whether we are in the work tree or |
1503 | | * not, since the config can only be read _after_ this function was called. |
1504 | | * |
1505 | | * Also, we avoid changing any global state (such as the current working |
1506 | | * directory) to allow early callers. |
1507 | | * |
1508 | | * The directory where the search should start needs to be passed in via the |
1509 | | * `dir` parameter; upon return, the `dir` buffer will contain the path of |
1510 | | * the directory where the search ended, and `gitdir` will contain the path of |
1511 | | * the discovered .git/ directory, if any. If `gitdir` is not absolute, it |
1512 | | * is relative to `dir` (i.e. *not* necessarily the cwd). |
1513 | | */ |
1514 | | static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir, |
1515 | | struct strbuf *gitdir, |
1516 | | struct strbuf *report, |
1517 | | int die_on_error) |
1518 | 0 | { |
1519 | 0 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
1520 | 0 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
1521 | 0 | const char *gitdirenv; |
1522 | 0 | int ceil_offset = -1, min_offset = offset_1st_component(dir->buf); |
1523 | 0 | dev_t current_device = 0; |
1524 | 0 | int one_filesystem = 1; |
1525 | | |
1526 | | /* |
1527 | | * If GIT_DIR is set explicitly, we're not going |
1528 | | * to do any discovery, but we still do repository |
1529 | | * validation. |
1530 | | */ |
1531 | 0 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
1532 | 0 | if (gitdirenv) { |
1533 | 0 | strbuf_addstr(gitdir, gitdirenv); |
1534 | 0 | return GIT_DIR_EXPLICIT; |
1535 | 0 | } |
1536 | | |
1537 | 0 | if (env_ceiling_dirs) { |
1538 | 0 | int empty_entry_found = 0; |
1539 | 0 | static const char path_sep[] = { PATH_SEP, '\0' }; |
1540 | |
|
1541 | 0 | string_list_split(&ceiling_dirs, env_ceiling_dirs, path_sep, -1); |
1542 | 0 | filter_string_list(&ceiling_dirs, 0, |
1543 | 0 | canonicalize_ceiling_entry, &empty_entry_found); |
1544 | 0 | ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs); |
1545 | 0 | string_list_clear(&ceiling_dirs, 0); |
1546 | 0 | } |
1547 | |
|
1548 | 0 | if (ceil_offset < 0) |
1549 | 0 | ceil_offset = min_offset - 2; |
1550 | |
|
1551 | 0 | if (min_offset && min_offset == dir->len && |
1552 | 0 | !is_dir_sep(dir->buf[min_offset - 1])) { |
1553 | 0 | strbuf_addch(dir, '/'); |
1554 | 0 | min_offset++; |
1555 | 0 | } |
1556 | | |
1557 | | /* |
1558 | | * Test in the following order (relative to the dir): |
1559 | | * - .git (file containing "gitdir: <path>") |
1560 | | * - .git/ |
1561 | | * - ./ (bare) |
1562 | | * - ../.git |
1563 | | * - ../.git/ |
1564 | | * - ../ (bare) |
1565 | | * - ../../.git |
1566 | | * etc. |
1567 | | */ |
1568 | 0 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
1569 | 0 | if (one_filesystem) |
1570 | 0 | current_device = get_device_or_die(dir->buf, NULL, 0); |
1571 | 0 | for (;;) { |
1572 | 0 | int offset = dir->len, error_code = 0; |
1573 | 0 | char *gitdir_path = NULL; |
1574 | 0 | char *gitfile = NULL; |
1575 | |
|
1576 | 0 | if (offset > min_offset) |
1577 | 0 | strbuf_addch(dir, '/'); |
1578 | 0 | strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT); |
1579 | 0 | gitdirenv = read_gitfile_gently(dir->buf, die_on_error ? |
1580 | 0 | NULL : &error_code); |
1581 | 0 | if (!gitdirenv) { |
1582 | 0 | if (die_on_error || |
1583 | 0 | error_code == READ_GITFILE_ERR_NOT_A_FILE) { |
1584 | | /* NEEDSWORK: fail if .git is not file nor dir */ |
1585 | 0 | if (is_git_directory(dir->buf)) { |
1586 | 0 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; |
1587 | 0 | gitdir_path = xstrdup(dir->buf); |
1588 | 0 | } |
1589 | 0 | } else if (error_code != READ_GITFILE_ERR_STAT_FAILED) |
1590 | 0 | return GIT_DIR_INVALID_GITFILE; |
1591 | 0 | } else |
1592 | 0 | gitfile = xstrdup(dir->buf); |
1593 | | /* |
1594 | | * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT |
1595 | | * to check that directory for a repository. |
1596 | | * Now trim that tentative addition away, because we want to |
1597 | | * focus on the real directory we are in. |
1598 | | */ |
1599 | 0 | strbuf_setlen(dir, offset); |
1600 | 0 | if (gitdirenv) { |
1601 | 0 | enum discovery_result ret; |
1602 | 0 | const char *gitdir_candidate = |
1603 | 0 | gitdir_path ? gitdir_path : gitdirenv; |
1604 | |
|
1605 | 0 | if (ensure_valid_ownership(gitfile, dir->buf, |
1606 | 0 | gitdir_candidate, report)) { |
1607 | 0 | strbuf_addstr(gitdir, gitdirenv); |
1608 | 0 | ret = GIT_DIR_DISCOVERED; |
1609 | 0 | } else |
1610 | 0 | ret = GIT_DIR_INVALID_OWNERSHIP; |
1611 | | |
1612 | | /* |
1613 | | * Earlier, during discovery, we might have allocated |
1614 | | * string copies for gitdir_path or gitfile so make |
1615 | | * sure we don't leak by freeing them now, before |
1616 | | * leaving the loop and function. |
1617 | | * |
1618 | | * Note: gitdirenv will be non-NULL whenever these are |
1619 | | * allocated, therefore we need not take care of releasing |
1620 | | * them outside of this conditional block. |
1621 | | */ |
1622 | 0 | free(gitdir_path); |
1623 | 0 | free(gitfile); |
1624 | |
|
1625 | 0 | return ret; |
1626 | 0 | } |
1627 | | |
1628 | 0 | if (is_git_directory(dir->buf)) { |
1629 | 0 | trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf); |
1630 | 0 | if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT && |
1631 | 0 | !is_implicit_bare_repo(dir->buf)) |
1632 | 0 | return GIT_DIR_DISALLOWED_BARE; |
1633 | 0 | if (!ensure_valid_ownership(NULL, NULL, dir->buf, report)) |
1634 | 0 | return GIT_DIR_INVALID_OWNERSHIP; |
1635 | 0 | strbuf_addstr(gitdir, "."); |
1636 | 0 | return GIT_DIR_BARE; |
1637 | 0 | } |
1638 | | |
1639 | 0 | if (offset <= min_offset) |
1640 | 0 | return GIT_DIR_HIT_CEILING; |
1641 | | |
1642 | 0 | while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset])) |
1643 | 0 | ; /* continue */ |
1644 | 0 | if (offset <= ceil_offset) |
1645 | 0 | return GIT_DIR_HIT_CEILING; |
1646 | | |
1647 | 0 | strbuf_setlen(dir, offset > min_offset ? offset : min_offset); |
1648 | 0 | if (one_filesystem && |
1649 | 0 | current_device != get_device_or_die(dir->buf, NULL, offset)) |
1650 | 0 | return GIT_DIR_HIT_MOUNT_POINT; |
1651 | 0 | } |
1652 | 0 | } |
1653 | | |
1654 | | enum discovery_result discover_git_directory_reason(struct strbuf *commondir, |
1655 | | struct strbuf *gitdir) |
1656 | 0 | { |
1657 | 0 | struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT; |
1658 | 0 | size_t gitdir_offset = gitdir->len, cwd_len; |
1659 | 0 | size_t commondir_offset = commondir->len; |
1660 | 0 | struct repository_format candidate = REPOSITORY_FORMAT_INIT; |
1661 | 0 | enum discovery_result result; |
1662 | |
|
1663 | 0 | if (strbuf_getcwd(&dir)) |
1664 | 0 | return GIT_DIR_CWD_FAILURE; |
1665 | | |
1666 | 0 | cwd_len = dir.len; |
1667 | 0 | result = setup_git_directory_gently_1(&dir, gitdir, NULL, 0); |
1668 | 0 | if (result <= 0) { |
1669 | 0 | strbuf_release(&dir); |
1670 | 0 | return result; |
1671 | 0 | } |
1672 | | |
1673 | | /* |
1674 | | * The returned gitdir is relative to dir, and if dir does not reflect |
1675 | | * the current working directory, we simply make the gitdir absolute. |
1676 | | */ |
1677 | 0 | if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) { |
1678 | | /* Avoid a trailing "/." */ |
1679 | 0 | if (!strcmp(".", gitdir->buf + gitdir_offset)) |
1680 | 0 | strbuf_setlen(gitdir, gitdir_offset); |
1681 | 0 | else |
1682 | 0 | strbuf_addch(&dir, '/'); |
1683 | 0 | strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len); |
1684 | 0 | } |
1685 | |
|
1686 | 0 | get_common_dir(commondir, gitdir->buf + gitdir_offset); |
1687 | |
|
1688 | 0 | strbuf_reset(&dir); |
1689 | 0 | strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset); |
1690 | 0 | read_repository_format(&candidate, dir.buf); |
1691 | 0 | strbuf_release(&dir); |
1692 | |
|
1693 | 0 | if (verify_repository_format(&candidate, &err) < 0) { |
1694 | 0 | warning("ignoring git dir '%s': %s", |
1695 | 0 | gitdir->buf + gitdir_offset, err.buf); |
1696 | 0 | strbuf_release(&err); |
1697 | 0 | strbuf_setlen(commondir, commondir_offset); |
1698 | 0 | strbuf_setlen(gitdir, gitdir_offset); |
1699 | 0 | clear_repository_format(&candidate); |
1700 | 0 | return GIT_DIR_INVALID_FORMAT; |
1701 | 0 | } |
1702 | | |
1703 | 0 | clear_repository_format(&candidate); |
1704 | 0 | return result; |
1705 | 0 | } |
1706 | | |
1707 | | const char *enter_repo(const char *path, unsigned flags) |
1708 | 0 | { |
1709 | 0 | static struct strbuf validated_path = STRBUF_INIT; |
1710 | 0 | static struct strbuf used_path = STRBUF_INIT; |
1711 | |
|
1712 | 0 | if (!path) |
1713 | 0 | return NULL; |
1714 | | |
1715 | 0 | if (!(flags & ENTER_REPO_STRICT)) { |
1716 | 0 | static const char *suffix[] = { |
1717 | 0 | "/.git", "", ".git/.git", ".git", NULL, |
1718 | 0 | }; |
1719 | 0 | const char *gitfile; |
1720 | 0 | int len = strlen(path); |
1721 | 0 | int i; |
1722 | 0 | while ((1 < len) && (path[len-1] == '/')) |
1723 | 0 | len--; |
1724 | | |
1725 | | /* |
1726 | | * We can handle arbitrary-sized buffers, but this remains as a |
1727 | | * sanity check on untrusted input. |
1728 | | */ |
1729 | 0 | if (PATH_MAX <= len) |
1730 | 0 | return NULL; |
1731 | | |
1732 | 0 | strbuf_reset(&used_path); |
1733 | 0 | strbuf_reset(&validated_path); |
1734 | 0 | strbuf_add(&used_path, path, len); |
1735 | 0 | strbuf_add(&validated_path, path, len); |
1736 | |
|
1737 | 0 | if (used_path.buf[0] == '~') { |
1738 | 0 | char *newpath = interpolate_path(used_path.buf, 0); |
1739 | 0 | if (!newpath) |
1740 | 0 | return NULL; |
1741 | 0 | strbuf_attach(&used_path, newpath, strlen(newpath), |
1742 | 0 | strlen(newpath)); |
1743 | 0 | } |
1744 | 0 | for (i = 0; suffix[i]; i++) { |
1745 | 0 | struct stat st; |
1746 | 0 | size_t baselen = used_path.len; |
1747 | 0 | strbuf_addstr(&used_path, suffix[i]); |
1748 | 0 | if (!stat(used_path.buf, &st) && |
1749 | 0 | (S_ISREG(st.st_mode) || |
1750 | 0 | (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) { |
1751 | 0 | strbuf_addstr(&validated_path, suffix[i]); |
1752 | 0 | break; |
1753 | 0 | } |
1754 | 0 | strbuf_setlen(&used_path, baselen); |
1755 | 0 | } |
1756 | 0 | if (!suffix[i]) |
1757 | 0 | return NULL; |
1758 | 0 | gitfile = read_gitfile(used_path.buf); |
1759 | 0 | if (!(flags & ENTER_REPO_ANY_OWNER_OK)) |
1760 | 0 | die_upon_dubious_ownership(gitfile, NULL, used_path.buf); |
1761 | 0 | if (gitfile) { |
1762 | 0 | strbuf_reset(&used_path); |
1763 | 0 | strbuf_addstr(&used_path, gitfile); |
1764 | 0 | } |
1765 | 0 | if (chdir(used_path.buf)) |
1766 | 0 | return NULL; |
1767 | 0 | path = validated_path.buf; |
1768 | 0 | } |
1769 | 0 | else { |
1770 | 0 | const char *gitfile = read_gitfile(path); |
1771 | 0 | if (!(flags & ENTER_REPO_ANY_OWNER_OK)) |
1772 | 0 | die_upon_dubious_ownership(gitfile, NULL, path); |
1773 | 0 | if (gitfile) |
1774 | 0 | path = gitfile; |
1775 | 0 | if (chdir(path)) |
1776 | 0 | return NULL; |
1777 | 0 | } |
1778 | | |
1779 | 0 | if (is_git_directory(".")) { |
1780 | 0 | set_git_dir(".", 0); |
1781 | 0 | check_repository_format(NULL); |
1782 | 0 | return path; |
1783 | 0 | } |
1784 | | |
1785 | 0 | return NULL; |
1786 | 0 | } |
1787 | | |
1788 | | static int git_work_tree_initialized; |
1789 | | |
1790 | | /* |
1791 | | * Note. This works only before you used a work tree. This was added |
1792 | | * primarily to support git-clone to work in a new repository it just |
1793 | | * created, and is not meant to flip between different work trees. |
1794 | | */ |
1795 | | void set_git_work_tree(const char *new_work_tree) |
1796 | 0 | { |
1797 | 0 | if (git_work_tree_initialized) { |
1798 | 0 | struct strbuf realpath = STRBUF_INIT; |
1799 | |
|
1800 | 0 | strbuf_realpath(&realpath, new_work_tree, 1); |
1801 | 0 | new_work_tree = realpath.buf; |
1802 | 0 | if (strcmp(new_work_tree, the_repository->worktree)) |
1803 | 0 | die("internal error: work tree has already been set\n" |
1804 | 0 | "Current worktree: %s\nNew worktree: %s", |
1805 | 0 | the_repository->worktree, new_work_tree); |
1806 | 0 | strbuf_release(&realpath); |
1807 | 0 | return; |
1808 | 0 | } |
1809 | 0 | git_work_tree_initialized = 1; |
1810 | 0 | repo_set_worktree(the_repository, new_work_tree); |
1811 | 0 | } |
1812 | | |
1813 | | const char *setup_git_directory_gently(int *nongit_ok) |
1814 | 0 | { |
1815 | 0 | static struct strbuf cwd = STRBUF_INIT; |
1816 | 0 | struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT; |
1817 | 0 | const char *prefix = NULL; |
1818 | 0 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
1819 | | |
1820 | | /* |
1821 | | * We may have read an incomplete configuration before |
1822 | | * setting-up the git directory. If so, clear the cache so |
1823 | | * that the next queries to the configuration reload complete |
1824 | | * configuration (including the per-repo config file that we |
1825 | | * ignored previously). |
1826 | | */ |
1827 | 0 | repo_config_clear(the_repository); |
1828 | | |
1829 | | /* |
1830 | | * Let's assume that we are in a git repository. |
1831 | | * If it turns out later that we are somewhere else, the value will be |
1832 | | * updated accordingly. |
1833 | | */ |
1834 | 0 | if (nongit_ok) |
1835 | 0 | *nongit_ok = 0; |
1836 | |
|
1837 | 0 | if (strbuf_getcwd(&cwd)) |
1838 | 0 | die_errno(_("Unable to read current working directory")); |
1839 | 0 | strbuf_addbuf(&dir, &cwd); |
1840 | |
|
1841 | 0 | switch (setup_git_directory_gently_1(&dir, &gitdir, &report, 1)) { |
1842 | 0 | case GIT_DIR_EXPLICIT: |
1843 | 0 | prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok); |
1844 | 0 | break; |
1845 | 0 | case GIT_DIR_DISCOVERED: |
1846 | 0 | if (dir.len < cwd.len && chdir(dir.buf)) |
1847 | 0 | die(_("cannot change to '%s'"), dir.buf); |
1848 | 0 | prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len, |
1849 | 0 | &repo_fmt, nongit_ok); |
1850 | 0 | break; |
1851 | 0 | case GIT_DIR_BARE: |
1852 | 0 | if (dir.len < cwd.len && chdir(dir.buf)) |
1853 | 0 | die(_("cannot change to '%s'"), dir.buf); |
1854 | 0 | prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok); |
1855 | 0 | break; |
1856 | 0 | case GIT_DIR_HIT_CEILING: |
1857 | 0 | if (!nongit_ok) |
1858 | 0 | die(_("not a git repository (or any of the parent directories): %s"), |
1859 | 0 | DEFAULT_GIT_DIR_ENVIRONMENT); |
1860 | 0 | *nongit_ok = 1; |
1861 | 0 | break; |
1862 | 0 | case GIT_DIR_HIT_MOUNT_POINT: |
1863 | 0 | if (!nongit_ok) |
1864 | 0 | die(_("not a git repository (or any parent up to mount point %s)\n" |
1865 | 0 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), |
1866 | 0 | dir.buf); |
1867 | 0 | *nongit_ok = 1; |
1868 | 0 | break; |
1869 | 0 | case GIT_DIR_INVALID_OWNERSHIP: |
1870 | 0 | if (!nongit_ok) { |
1871 | 0 | struct strbuf quoted = STRBUF_INIT; |
1872 | |
|
1873 | 0 | strbuf_complete(&report, '\n'); |
1874 | 0 | sq_quote_buf_pretty("ed, dir.buf); |
1875 | 0 | die(_("detected dubious ownership in repository at '%s'\n" |
1876 | 0 | "%s" |
1877 | 0 | "To add an exception for this directory, call:\n" |
1878 | 0 | "\n" |
1879 | 0 | "\tgit config --global --add safe.directory %s"), |
1880 | 0 | dir.buf, report.buf, quoted.buf); |
1881 | 0 | } |
1882 | 0 | *nongit_ok = 1; |
1883 | 0 | break; |
1884 | 0 | case GIT_DIR_DISALLOWED_BARE: |
1885 | 0 | if (!nongit_ok) { |
1886 | 0 | die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"), |
1887 | 0 | dir.buf, |
1888 | 0 | allowed_bare_repo_to_string(get_allowed_bare_repo())); |
1889 | 0 | } |
1890 | 0 | *nongit_ok = 1; |
1891 | 0 | break; |
1892 | 0 | case GIT_DIR_CWD_FAILURE: |
1893 | 0 | case GIT_DIR_INVALID_FORMAT: |
1894 | | /* |
1895 | | * As a safeguard against setup_git_directory_gently_1 returning |
1896 | | * these values, fallthrough to BUG. Otherwise it is possible to |
1897 | | * set startup_info->have_repository to 1 when we did nothing to |
1898 | | * find a repository. |
1899 | | */ |
1900 | 0 | default: |
1901 | 0 | BUG("unhandled setup_git_directory_gently_1() result"); |
1902 | 0 | } |
1903 | | |
1904 | | /* |
1905 | | * At this point, nongit_ok is stable. If it is non-NULL and points |
1906 | | * to a non-zero value, then this means that we haven't found a |
1907 | | * repository and that the caller expects startup_info to reflect |
1908 | | * this. |
1909 | | * |
1910 | | * Regardless of the state of nongit_ok, startup_info->prefix and |
1911 | | * the GIT_PREFIX environment variable must always match. For details |
1912 | | * see Documentation/config/alias.adoc. |
1913 | | */ |
1914 | 0 | if (nongit_ok && *nongit_ok) |
1915 | 0 | startup_info->have_repository = 0; |
1916 | 0 | else |
1917 | 0 | startup_info->have_repository = 1; |
1918 | | |
1919 | | /* |
1920 | | * Not all paths through the setup code will call 'set_git_dir()' (which |
1921 | | * directly sets up the environment) so in order to guarantee that the |
1922 | | * environment is in a consistent state after setup, explicitly setup |
1923 | | * the environment if we have a repository. |
1924 | | * |
1925 | | * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some |
1926 | | * code paths so we also need to explicitly setup the environment if |
1927 | | * the user has set GIT_DIR. It may be beneficial to disallow bogus |
1928 | | * GIT_DIR values at some point in the future. |
1929 | | */ |
1930 | 0 | if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */ |
1931 | 0 | startup_info->have_repository || |
1932 | | /* GIT_DIR_EXPLICIT */ |
1933 | 0 | getenv(GIT_DIR_ENVIRONMENT)) { |
1934 | 0 | if (!the_repository->gitdir) { |
1935 | 0 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
1936 | 0 | if (!gitdir) |
1937 | 0 | gitdir = DEFAULT_GIT_DIR_ENVIRONMENT; |
1938 | 0 | setup_git_env(gitdir); |
1939 | 0 | } |
1940 | 0 | if (startup_info->have_repository) { |
1941 | 0 | repo_set_hash_algo(the_repository, repo_fmt.hash_algo); |
1942 | 0 | repo_set_compat_hash_algo(the_repository, |
1943 | 0 | repo_fmt.compat_hash_algo); |
1944 | 0 | repo_set_ref_storage_format(the_repository, |
1945 | 0 | repo_fmt.ref_storage_format); |
1946 | 0 | the_repository->repository_format_worktree_config = |
1947 | 0 | repo_fmt.worktree_config; |
1948 | 0 | the_repository->repository_format_relative_worktrees = |
1949 | 0 | repo_fmt.relative_worktrees; |
1950 | | /* take ownership of repo_fmt.partial_clone */ |
1951 | 0 | the_repository->repository_format_partial_clone = |
1952 | 0 | repo_fmt.partial_clone; |
1953 | 0 | repo_fmt.partial_clone = NULL; |
1954 | 0 | the_repository->repository_format_precious_objects = |
1955 | 0 | repo_fmt.precious_objects; |
1956 | 0 | } |
1957 | 0 | } |
1958 | | /* |
1959 | | * Since precompose_string_if_needed() needs to look at |
1960 | | * the core.precomposeunicode configuration, this |
1961 | | * has to happen after the above block that finds |
1962 | | * out where the repository is, i.e. a preparation |
1963 | | * for calling repo_config_get_bool(). |
1964 | | */ |
1965 | 0 | if (prefix) { |
1966 | 0 | prefix = precompose_string_if_needed(prefix); |
1967 | 0 | startup_info->prefix = prefix; |
1968 | 0 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); |
1969 | 0 | } else { |
1970 | 0 | startup_info->prefix = NULL; |
1971 | 0 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); |
1972 | 0 | } |
1973 | |
|
1974 | 0 | setup_original_cwd(); |
1975 | |
|
1976 | 0 | strbuf_release(&dir); |
1977 | 0 | strbuf_release(&gitdir); |
1978 | 0 | strbuf_release(&report); |
1979 | 0 | clear_repository_format(&repo_fmt); |
1980 | |
|
1981 | 0 | return prefix; |
1982 | 0 | } |
1983 | | |
1984 | | int git_config_perm(const char *var, const char *value) |
1985 | 0 | { |
1986 | 0 | int i; |
1987 | 0 | char *endptr; |
1988 | |
|
1989 | 0 | if (!value) |
1990 | 0 | return PERM_GROUP; |
1991 | | |
1992 | 0 | if (!strcmp(value, "umask")) |
1993 | 0 | return PERM_UMASK; |
1994 | 0 | if (!strcmp(value, "group")) |
1995 | 0 | return PERM_GROUP; |
1996 | 0 | if (!strcmp(value, "all") || |
1997 | 0 | !strcmp(value, "world") || |
1998 | 0 | !strcmp(value, "everybody")) |
1999 | 0 | return PERM_EVERYBODY; |
2000 | | |
2001 | | /* Parse octal numbers */ |
2002 | 0 | i = strtol(value, &endptr, 8); |
2003 | | |
2004 | | /* If not an octal number, maybe true/false? */ |
2005 | 0 | if (*endptr != 0) |
2006 | 0 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; |
2007 | | |
2008 | | /* |
2009 | | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is |
2010 | | * a chmod value to restrict to. |
2011 | | */ |
2012 | 0 | switch (i) { |
2013 | 0 | case PERM_UMASK: /* 0 */ |
2014 | 0 | return PERM_UMASK; |
2015 | 0 | case OLD_PERM_GROUP: /* 1 */ |
2016 | 0 | return PERM_GROUP; |
2017 | 0 | case OLD_PERM_EVERYBODY: /* 2 */ |
2018 | 0 | return PERM_EVERYBODY; |
2019 | 0 | } |
2020 | | |
2021 | | /* A filemode value was given: 0xxx */ |
2022 | | |
2023 | 0 | if ((i & 0600) != 0600) |
2024 | 0 | die(_("problem with core.sharedRepository filemode value " |
2025 | 0 | "(0%.3o).\nThe owner of files must always have " |
2026 | 0 | "read and write permissions."), i); |
2027 | | |
2028 | | /* |
2029 | | * Mask filemode value. Others can not get write permission. |
2030 | | * x flags for directories are handled separately. |
2031 | | */ |
2032 | 0 | return -(i & 0666); |
2033 | 0 | } |
2034 | | |
2035 | | void check_repository_format(struct repository_format *fmt) |
2036 | 0 | { |
2037 | 0 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
2038 | 0 | if (!fmt) |
2039 | 0 | fmt = &repo_fmt; |
2040 | 0 | check_repository_format_gently(repo_get_git_dir(the_repository), fmt, NULL); |
2041 | 0 | startup_info->have_repository = 1; |
2042 | 0 | repo_set_hash_algo(the_repository, fmt->hash_algo); |
2043 | 0 | repo_set_compat_hash_algo(the_repository, fmt->compat_hash_algo); |
2044 | 0 | repo_set_ref_storage_format(the_repository, |
2045 | 0 | fmt->ref_storage_format); |
2046 | 0 | the_repository->repository_format_worktree_config = |
2047 | 0 | fmt->worktree_config; |
2048 | 0 | the_repository->repository_format_relative_worktrees = |
2049 | 0 | fmt->relative_worktrees; |
2050 | 0 | the_repository->repository_format_partial_clone = |
2051 | 0 | xstrdup_or_null(fmt->partial_clone); |
2052 | 0 | clear_repository_format(&repo_fmt); |
2053 | 0 | } |
2054 | | |
2055 | | /* |
2056 | | * Returns the "prefix", a path to the current working directory |
2057 | | * relative to the work tree root, or NULL, if the current working |
2058 | | * directory is not a strict subdirectory of the work tree root. The |
2059 | | * prefix always ends with a '/' character. |
2060 | | */ |
2061 | | const char *setup_git_directory(void) |
2062 | 0 | { |
2063 | 0 | return setup_git_directory_gently(NULL); |
2064 | 0 | } |
2065 | | |
2066 | | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code) |
2067 | 0 | { |
2068 | 0 | if (is_git_directory(suspect)) |
2069 | 0 | return suspect; |
2070 | 0 | return read_gitfile_gently(suspect, return_error_code); |
2071 | 0 | } |
2072 | | |
2073 | | /* if any standard file descriptor is missing open it to /dev/null */ |
2074 | | void sanitize_stdfds(void) |
2075 | 0 | { |
2076 | 0 | int fd = xopen("/dev/null", O_RDWR); |
2077 | 0 | while (fd < 2) |
2078 | 0 | fd = xdup(fd); |
2079 | 0 | if (fd > 2) |
2080 | 0 | close(fd); |
2081 | 0 | } |
2082 | | |
2083 | | int daemonize(void) |
2084 | 0 | { |
2085 | | #ifdef NO_POSIX_GOODIES |
2086 | | errno = ENOSYS; |
2087 | | return -1; |
2088 | | #else |
2089 | 0 | switch (fork()) { |
2090 | 0 | case 0: |
2091 | 0 | break; |
2092 | 0 | case -1: |
2093 | 0 | die_errno(_("fork failed")); |
2094 | 0 | default: |
2095 | 0 | exit(0); |
2096 | 0 | } |
2097 | 0 | if (setsid() == -1) |
2098 | 0 | die_errno(_("setsid failed")); |
2099 | 0 | close(0); |
2100 | 0 | close(1); |
2101 | 0 | close(2); |
2102 | 0 | sanitize_stdfds(); |
2103 | 0 | return 0; |
2104 | 0 | #endif |
2105 | 0 | } |
2106 | | |
2107 | | struct template_dir_cb_data { |
2108 | | char *path; |
2109 | | int initialized; |
2110 | | }; |
2111 | | |
2112 | | static int template_dir_cb(const char *key, const char *value, |
2113 | | const struct config_context *ctx UNUSED, void *d) |
2114 | 0 | { |
2115 | 0 | struct template_dir_cb_data *data = d; |
2116 | |
|
2117 | 0 | if (strcmp(key, "init.templatedir")) |
2118 | 0 | return 0; |
2119 | | |
2120 | 0 | if (!value) { |
2121 | 0 | data->path = NULL; |
2122 | 0 | } else { |
2123 | 0 | char *path = NULL; |
2124 | |
|
2125 | 0 | FREE_AND_NULL(data->path); |
2126 | 0 | if (!git_config_pathname(&path, key, value)) |
2127 | 0 | data->path = path ? path : xstrdup(value); |
2128 | 0 | } |
2129 | |
|
2130 | 0 | return 0; |
2131 | 0 | } |
2132 | | |
2133 | | const char *get_template_dir(const char *option_template) |
2134 | 0 | { |
2135 | 0 | const char *template_dir = option_template; |
2136 | |
|
2137 | 0 | if (!template_dir) |
2138 | 0 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
2139 | 0 | if (!template_dir) { |
2140 | 0 | static struct template_dir_cb_data data; |
2141 | |
|
2142 | 0 | if (!data.initialized) { |
2143 | 0 | git_protected_config(template_dir_cb, &data); |
2144 | 0 | data.initialized = 1; |
2145 | 0 | } |
2146 | 0 | template_dir = data.path; |
2147 | 0 | } |
2148 | 0 | if (!template_dir) { |
2149 | 0 | static char *dir; |
2150 | |
|
2151 | 0 | if (!dir) |
2152 | 0 | dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); |
2153 | 0 | template_dir = dir; |
2154 | 0 | } |
2155 | 0 | return template_dir; |
2156 | 0 | } |
2157 | | |
2158 | | #ifdef NO_TRUSTABLE_FILEMODE |
2159 | | #define TEST_FILEMODE 0 |
2160 | | #else |
2161 | 0 | #define TEST_FILEMODE 1 |
2162 | | #endif |
2163 | | |
2164 | 0 | #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH" |
2165 | | |
2166 | | static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, |
2167 | | DIR *dir) |
2168 | 0 | { |
2169 | 0 | size_t path_baselen = path->len; |
2170 | 0 | size_t template_baselen = template_path->len; |
2171 | 0 | struct dirent *de; |
2172 | | |
2173 | | /* Note: if ".git/hooks" file exists in the repository being |
2174 | | * re-initialized, /etc/core-git/templates/hooks/update would |
2175 | | * cause "git init" to fail here. I think this is sane but |
2176 | | * it means that the set of templates we ship by default, along |
2177 | | * with the way the namespace under .git/ is organized, should |
2178 | | * be really carefully chosen. |
2179 | | */ |
2180 | 0 | safe_create_dir(the_repository, path->buf, 1); |
2181 | 0 | while ((de = readdir(dir)) != NULL) { |
2182 | 0 | struct stat st_git, st_template; |
2183 | 0 | int exists = 0; |
2184 | |
|
2185 | 0 | strbuf_setlen(path, path_baselen); |
2186 | 0 | strbuf_setlen(template_path, template_baselen); |
2187 | |
|
2188 | 0 | if (de->d_name[0] == '.') |
2189 | 0 | continue; |
2190 | 0 | strbuf_addstr(path, de->d_name); |
2191 | 0 | strbuf_addstr(template_path, de->d_name); |
2192 | 0 | if (lstat(path->buf, &st_git)) { |
2193 | 0 | if (errno != ENOENT) |
2194 | 0 | die_errno(_("cannot stat '%s'"), path->buf); |
2195 | 0 | } |
2196 | 0 | else |
2197 | 0 | exists = 1; |
2198 | | |
2199 | 0 | if (lstat(template_path->buf, &st_template)) |
2200 | 0 | die_errno(_("cannot stat template '%s'"), template_path->buf); |
2201 | | |
2202 | 0 | if (S_ISDIR(st_template.st_mode)) { |
2203 | 0 | DIR *subdir = opendir(template_path->buf); |
2204 | 0 | if (!subdir) |
2205 | 0 | die_errno(_("cannot opendir '%s'"), template_path->buf); |
2206 | 0 | strbuf_addch(path, '/'); |
2207 | 0 | strbuf_addch(template_path, '/'); |
2208 | 0 | copy_templates_1(path, template_path, subdir); |
2209 | 0 | closedir(subdir); |
2210 | 0 | } |
2211 | 0 | else if (exists) |
2212 | 0 | continue; |
2213 | 0 | else if (S_ISLNK(st_template.st_mode)) { |
2214 | 0 | struct strbuf lnk = STRBUF_INIT; |
2215 | 0 | if (strbuf_readlink(&lnk, template_path->buf, |
2216 | 0 | st_template.st_size) < 0) |
2217 | 0 | die_errno(_("cannot readlink '%s'"), template_path->buf); |
2218 | 0 | if (symlink(lnk.buf, path->buf)) |
2219 | 0 | die_errno(_("cannot symlink '%s' '%s'"), |
2220 | 0 | lnk.buf, path->buf); |
2221 | 0 | strbuf_release(&lnk); |
2222 | 0 | } |
2223 | 0 | else if (S_ISREG(st_template.st_mode)) { |
2224 | 0 | if (copy_file(path->buf, template_path->buf, st_template.st_mode)) |
2225 | 0 | die_errno(_("cannot copy '%s' to '%s'"), |
2226 | 0 | template_path->buf, path->buf); |
2227 | 0 | } |
2228 | 0 | else |
2229 | 0 | error(_("ignoring template %s"), template_path->buf); |
2230 | 0 | } |
2231 | 0 | } |
2232 | | |
2233 | | static void copy_templates(const char *option_template) |
2234 | 0 | { |
2235 | 0 | const char *template_dir = get_template_dir(option_template); |
2236 | 0 | struct strbuf path = STRBUF_INIT; |
2237 | 0 | struct strbuf template_path = STRBUF_INIT; |
2238 | 0 | size_t template_len; |
2239 | 0 | struct repository_format template_format = REPOSITORY_FORMAT_INIT; |
2240 | 0 | struct strbuf err = STRBUF_INIT; |
2241 | 0 | DIR *dir; |
2242 | 0 | char *to_free = NULL; |
2243 | |
|
2244 | 0 | if (!template_dir || !*template_dir) |
2245 | 0 | return; |
2246 | | |
2247 | 0 | strbuf_addstr(&template_path, template_dir); |
2248 | 0 | strbuf_complete(&template_path, '/'); |
2249 | 0 | template_len = template_path.len; |
2250 | |
|
2251 | 0 | dir = opendir(template_path.buf); |
2252 | 0 | if (!dir) { |
2253 | 0 | warning(_("templates not found in %s"), template_dir); |
2254 | 0 | goto free_return; |
2255 | 0 | } |
2256 | | |
2257 | | /* Make sure that template is from the correct vintage */ |
2258 | 0 | strbuf_addstr(&template_path, "config"); |
2259 | 0 | read_repository_format(&template_format, template_path.buf); |
2260 | 0 | strbuf_setlen(&template_path, template_len); |
2261 | | |
2262 | | /* |
2263 | | * No mention of version at all is OK, but anything else should be |
2264 | | * verified. |
2265 | | */ |
2266 | 0 | if (template_format.version >= 0 && |
2267 | 0 | verify_repository_format(&template_format, &err) < 0) { |
2268 | 0 | warning(_("not copying templates from '%s': %s"), |
2269 | 0 | template_dir, err.buf); |
2270 | 0 | strbuf_release(&err); |
2271 | 0 | goto close_free_return; |
2272 | 0 | } |
2273 | | |
2274 | 0 | strbuf_addstr(&path, repo_get_common_dir(the_repository)); |
2275 | 0 | strbuf_complete(&path, '/'); |
2276 | 0 | copy_templates_1(&path, &template_path, dir); |
2277 | 0 | close_free_return: |
2278 | 0 | closedir(dir); |
2279 | 0 | free_return: |
2280 | 0 | free(to_free); |
2281 | 0 | strbuf_release(&path); |
2282 | 0 | strbuf_release(&template_path); |
2283 | 0 | clear_repository_format(&template_format); |
2284 | 0 | } |
2285 | | |
2286 | | /* |
2287 | | * If the git_dir is not directly inside the working tree, then git will not |
2288 | | * find it by default, and we need to set the worktree explicitly. |
2289 | | */ |
2290 | | static int needs_work_tree_config(const char *git_dir, const char *work_tree) |
2291 | 0 | { |
2292 | 0 | if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git")) |
2293 | 0 | return 0; |
2294 | 0 | if (skip_prefix(git_dir, work_tree, &git_dir) && |
2295 | 0 | !strcmp(git_dir, "/.git")) |
2296 | 0 | return 0; |
2297 | 0 | return 1; |
2298 | 0 | } |
2299 | | |
2300 | | void initialize_repository_version(int hash_algo, |
2301 | | enum ref_storage_format ref_storage_format, |
2302 | | int reinit) |
2303 | 0 | { |
2304 | 0 | struct strbuf repo_version = STRBUF_INIT; |
2305 | 0 | int target_version = GIT_REPO_VERSION; |
2306 | | |
2307 | | /* |
2308 | | * Note that we initialize the repository version to 1 when the ref |
2309 | | * storage format is unknown. This is on purpose so that we can add the |
2310 | | * correct object format to the config during git-clone(1). The format |
2311 | | * version will get adjusted by git-clone(1) once it has learned about |
2312 | | * the remote repository's format. |
2313 | | */ |
2314 | 0 | if (hash_algo != GIT_HASH_SHA1_LEGACY || |
2315 | 0 | ref_storage_format != REF_STORAGE_FORMAT_FILES) |
2316 | 0 | target_version = GIT_REPO_VERSION_READ; |
2317 | |
|
2318 | 0 | if (hash_algo != GIT_HASH_SHA1_LEGACY && hash_algo != GIT_HASH_UNKNOWN) |
2319 | 0 | repo_config_set(the_repository, "extensions.objectformat", |
2320 | 0 | hash_algos[hash_algo].name); |
2321 | 0 | else if (reinit) |
2322 | 0 | repo_config_set_gently(the_repository, "extensions.objectformat", NULL); |
2323 | |
|
2324 | 0 | if (ref_storage_format != REF_STORAGE_FORMAT_FILES) |
2325 | 0 | repo_config_set(the_repository, "extensions.refstorage", |
2326 | 0 | ref_storage_format_to_name(ref_storage_format)); |
2327 | 0 | else if (reinit) |
2328 | 0 | repo_config_set_gently(the_repository, "extensions.refstorage", NULL); |
2329 | |
|
2330 | 0 | if (reinit) { |
2331 | 0 | struct strbuf config = STRBUF_INIT; |
2332 | 0 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
2333 | |
|
2334 | 0 | repo_common_path_append(the_repository, &config, "config"); |
2335 | 0 | read_repository_format(&repo_fmt, config.buf); |
2336 | |
|
2337 | 0 | if (repo_fmt.v1_only_extensions.nr) |
2338 | 0 | target_version = GIT_REPO_VERSION_READ; |
2339 | |
|
2340 | 0 | strbuf_release(&config); |
2341 | 0 | clear_repository_format(&repo_fmt); |
2342 | 0 | } |
2343 | |
|
2344 | 0 | strbuf_addf(&repo_version, "%d", target_version); |
2345 | 0 | repo_config_set(the_repository, "core.repositoryformatversion", repo_version.buf); |
2346 | |
|
2347 | 0 | strbuf_release(&repo_version); |
2348 | 0 | } |
2349 | | |
2350 | | static int is_reinit(void) |
2351 | 0 | { |
2352 | 0 | struct strbuf buf = STRBUF_INIT; |
2353 | 0 | char junk[2]; |
2354 | 0 | int ret; |
2355 | |
|
2356 | 0 | repo_git_path_replace(the_repository, &buf, "HEAD"); |
2357 | 0 | ret = !access(buf.buf, R_OK) || readlink(buf.buf, junk, sizeof(junk) - 1) != -1; |
2358 | 0 | strbuf_release(&buf); |
2359 | 0 | return ret; |
2360 | 0 | } |
2361 | | |
2362 | | void create_reference_database(enum ref_storage_format ref_storage_format, |
2363 | | const char *initial_branch, int quiet) |
2364 | 0 | { |
2365 | 0 | struct strbuf err = STRBUF_INIT; |
2366 | 0 | char *to_free = NULL; |
2367 | 0 | int reinit = is_reinit(); |
2368 | |
|
2369 | 0 | repo_set_ref_storage_format(the_repository, ref_storage_format); |
2370 | 0 | if (ref_store_create_on_disk(get_main_ref_store(the_repository), 0, &err)) |
2371 | 0 | die("failed to set up refs db: %s", err.buf); |
2372 | | |
2373 | | /* |
2374 | | * Point the HEAD symref to the initial branch with if HEAD does |
2375 | | * not yet exist. |
2376 | | */ |
2377 | 0 | if (!reinit) { |
2378 | 0 | char *ref; |
2379 | |
|
2380 | 0 | if (!initial_branch) |
2381 | 0 | initial_branch = to_free = |
2382 | 0 | repo_default_branch_name(the_repository, quiet); |
2383 | |
|
2384 | 0 | ref = xstrfmt("refs/heads/%s", initial_branch); |
2385 | 0 | if (check_refname_format(ref, 0) < 0) |
2386 | 0 | die(_("invalid initial branch name: '%s'"), |
2387 | 0 | initial_branch); |
2388 | | |
2389 | 0 | if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", ref, NULL) < 0) |
2390 | 0 | exit(1); |
2391 | 0 | free(ref); |
2392 | 0 | } |
2393 | | |
2394 | 0 | if (reinit && initial_branch) |
2395 | 0 | warning(_("re-init: ignored --initial-branch=%s"), |
2396 | 0 | initial_branch); |
2397 | |
|
2398 | 0 | strbuf_release(&err); |
2399 | 0 | free(to_free); |
2400 | 0 | } |
2401 | | |
2402 | | static int create_default_files(const char *template_path, |
2403 | | const char *original_git_dir, |
2404 | | const struct repository_format *fmt, |
2405 | | int init_shared_repository) |
2406 | 0 | { |
2407 | 0 | struct stat st1; |
2408 | 0 | struct strbuf path = STRBUF_INIT; |
2409 | 0 | int reinit; |
2410 | 0 | int filemode; |
2411 | 0 | const char *work_tree = repo_get_work_tree(the_repository); |
2412 | | |
2413 | | /* |
2414 | | * First copy the templates -- we might have the default |
2415 | | * config file there, in which case we would want to read |
2416 | | * from it after installing. |
2417 | | * |
2418 | | * Before reading that config, we also need to clear out any cached |
2419 | | * values (since we've just potentially changed what's available on |
2420 | | * disk). |
2421 | | */ |
2422 | 0 | copy_templates(template_path); |
2423 | 0 | repo_config_clear(the_repository); |
2424 | 0 | repo_settings_reset_shared_repository(the_repository); |
2425 | 0 | repo_config(the_repository, git_default_config, NULL); |
2426 | |
|
2427 | 0 | reinit = is_reinit(); |
2428 | | |
2429 | | /* |
2430 | | * We must make sure command-line options continue to override any |
2431 | | * values we might have just re-read from the config. |
2432 | | */ |
2433 | 0 | if (init_shared_repository != -1) |
2434 | 0 | repo_settings_set_shared_repository(the_repository, |
2435 | 0 | init_shared_repository); |
2436 | |
|
2437 | 0 | is_bare_repository_cfg = !work_tree; |
2438 | | |
2439 | | /* |
2440 | | * We would have created the above under user's umask -- under |
2441 | | * shared-repository settings, we would need to fix them up. |
2442 | | */ |
2443 | 0 | if (repo_settings_get_shared_repository(the_repository)) { |
2444 | 0 | adjust_shared_perm(the_repository, repo_get_git_dir(the_repository)); |
2445 | 0 | } |
2446 | |
|
2447 | 0 | initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit); |
2448 | | |
2449 | | /* Check filemode trustability */ |
2450 | 0 | repo_git_path_replace(the_repository, &path, "config"); |
2451 | 0 | filemode = TEST_FILEMODE; |
2452 | 0 | if (TEST_FILEMODE && !lstat(path.buf, &st1)) { |
2453 | 0 | struct stat st2; |
2454 | 0 | filemode = (!chmod(path.buf, st1.st_mode ^ S_IXUSR) && |
2455 | 0 | !lstat(path.buf, &st2) && |
2456 | 0 | st1.st_mode != st2.st_mode && |
2457 | 0 | !chmod(path.buf, st1.st_mode)); |
2458 | 0 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) |
2459 | 0 | filemode = 0; |
2460 | 0 | } |
2461 | 0 | repo_config_set(the_repository, "core.filemode", filemode ? "true" : "false"); |
2462 | |
|
2463 | 0 | if (is_bare_repository()) |
2464 | 0 | repo_config_set(the_repository, "core.bare", "true"); |
2465 | 0 | else { |
2466 | 0 | repo_config_set(the_repository, "core.bare", "false"); |
2467 | | /* allow template config file to override the default */ |
2468 | 0 | if (repo_settings_get_log_all_ref_updates(the_repository) == LOG_REFS_UNSET) |
2469 | 0 | repo_config_set(the_repository, "core.logallrefupdates", "true"); |
2470 | 0 | if (needs_work_tree_config(original_git_dir, work_tree)) |
2471 | 0 | repo_config_set(the_repository, "core.worktree", work_tree); |
2472 | 0 | } |
2473 | |
|
2474 | 0 | if (!reinit) { |
2475 | | /* Check if symlink is supported in the work tree */ |
2476 | 0 | repo_git_path_replace(the_repository, &path, "tXXXXXX"); |
2477 | 0 | if (!close(xmkstemp(path.buf)) && |
2478 | 0 | !unlink(path.buf) && |
2479 | 0 | !symlink("testing", path.buf) && |
2480 | 0 | !lstat(path.buf, &st1) && |
2481 | 0 | S_ISLNK(st1.st_mode)) |
2482 | 0 | unlink(path.buf); /* good */ |
2483 | 0 | else |
2484 | 0 | repo_config_set(the_repository, "core.symlinks", "false"); |
2485 | | |
2486 | | /* Check if the filesystem is case-insensitive */ |
2487 | 0 | repo_git_path_replace(the_repository, &path, "CoNfIg"); |
2488 | 0 | if (!access(path.buf, F_OK)) |
2489 | 0 | repo_config_set(the_repository, "core.ignorecase", "true"); |
2490 | 0 | probe_utf8_pathname_composition(); |
2491 | 0 | } |
2492 | |
|
2493 | 0 | strbuf_release(&path); |
2494 | 0 | return reinit; |
2495 | 0 | } |
2496 | | |
2497 | | static void create_object_directory(void) |
2498 | 0 | { |
2499 | 0 | struct strbuf path = STRBUF_INIT; |
2500 | 0 | size_t baselen; |
2501 | |
|
2502 | 0 | strbuf_addstr(&path, repo_get_object_directory(the_repository)); |
2503 | 0 | baselen = path.len; |
2504 | |
|
2505 | 0 | safe_create_dir(the_repository, path.buf, 1); |
2506 | |
|
2507 | 0 | strbuf_setlen(&path, baselen); |
2508 | 0 | strbuf_addstr(&path, "/pack"); |
2509 | 0 | safe_create_dir(the_repository, path.buf, 1); |
2510 | |
|
2511 | 0 | strbuf_setlen(&path, baselen); |
2512 | 0 | strbuf_addstr(&path, "/info"); |
2513 | 0 | safe_create_dir(the_repository, path.buf, 1); |
2514 | |
|
2515 | 0 | strbuf_release(&path); |
2516 | 0 | } |
2517 | | |
2518 | | static void separate_git_dir(const char *git_dir, const char *git_link) |
2519 | 0 | { |
2520 | 0 | struct stat st; |
2521 | |
|
2522 | 0 | if (!stat(git_link, &st)) { |
2523 | 0 | const char *src; |
2524 | |
|
2525 | 0 | if (S_ISREG(st.st_mode)) |
2526 | 0 | src = read_gitfile(git_link); |
2527 | 0 | else if (S_ISDIR(st.st_mode)) |
2528 | 0 | src = git_link; |
2529 | 0 | else |
2530 | 0 | die(_("unable to handle file type %d"), (int)st.st_mode); |
2531 | | |
2532 | 0 | if (rename(src, git_dir)) |
2533 | 0 | die_errno(_("unable to move %s to %s"), src, git_dir); |
2534 | 0 | repair_worktrees_after_gitdir_move(src); |
2535 | 0 | } |
2536 | | |
2537 | 0 | write_file(git_link, "gitdir: %s", git_dir); |
2538 | 0 | } |
2539 | | |
2540 | | struct default_format_config { |
2541 | | int hash; |
2542 | | enum ref_storage_format ref_format; |
2543 | | }; |
2544 | | |
2545 | | static int read_default_format_config(const char *key, const char *value, |
2546 | | const struct config_context *ctx UNUSED, |
2547 | | void *payload) |
2548 | 0 | { |
2549 | 0 | struct default_format_config *cfg = payload; |
2550 | 0 | char *str = NULL; |
2551 | 0 | int ret; |
2552 | |
|
2553 | 0 | if (!strcmp(key, "init.defaultobjectformat")) { |
2554 | 0 | ret = git_config_string(&str, key, value); |
2555 | 0 | if (ret) |
2556 | 0 | goto out; |
2557 | 0 | cfg->hash = hash_algo_by_name(str); |
2558 | 0 | if (cfg->hash == GIT_HASH_UNKNOWN) |
2559 | 0 | warning(_("unknown hash algorithm '%s'"), str); |
2560 | 0 | goto out; |
2561 | 0 | } |
2562 | | |
2563 | 0 | if (!strcmp(key, "init.defaultrefformat")) { |
2564 | 0 | ret = git_config_string(&str, key, value); |
2565 | 0 | if (ret) |
2566 | 0 | goto out; |
2567 | 0 | cfg->ref_format = ref_storage_format_by_name(str); |
2568 | 0 | if (cfg->ref_format == REF_STORAGE_FORMAT_UNKNOWN) |
2569 | 0 | warning(_("unknown ref storage format '%s'"), str); |
2570 | 0 | goto out; |
2571 | 0 | } |
2572 | | |
2573 | | /* |
2574 | | * Enable the reftable format when "features.experimental" is enabled. |
2575 | | * "init.defaultRefFormat" takes precedence over this setting. |
2576 | | */ |
2577 | 0 | if (!strcmp(key, "feature.experimental") && |
2578 | 0 | cfg->ref_format == REF_STORAGE_FORMAT_UNKNOWN && |
2579 | 0 | git_config_bool(key, value)) { |
2580 | 0 | cfg->ref_format = REF_STORAGE_FORMAT_REFTABLE; |
2581 | 0 | ret = 0; |
2582 | 0 | goto out; |
2583 | 0 | } |
2584 | | |
2585 | 0 | ret = 0; |
2586 | 0 | out: |
2587 | 0 | free(str); |
2588 | 0 | return ret; |
2589 | 0 | } |
2590 | | |
2591 | | static void repository_format_configure(struct repository_format *repo_fmt, |
2592 | | int hash, enum ref_storage_format ref_format) |
2593 | 0 | { |
2594 | 0 | struct default_format_config cfg = { |
2595 | 0 | .hash = GIT_HASH_UNKNOWN, |
2596 | 0 | .ref_format = REF_STORAGE_FORMAT_UNKNOWN, |
2597 | 0 | }; |
2598 | 0 | struct config_options opts = { |
2599 | 0 | .respect_includes = 1, |
2600 | 0 | .ignore_repo = 1, |
2601 | 0 | .ignore_worktree = 1, |
2602 | 0 | }; |
2603 | 0 | const char *env; |
2604 | |
|
2605 | 0 | config_with_options(read_default_format_config, &cfg, NULL, NULL, &opts); |
2606 | | |
2607 | | /* |
2608 | | * If we already have an initialized repo, don't allow the user to |
2609 | | * specify a different algorithm, as that could cause corruption. |
2610 | | * Otherwise, if the user has specified one on the command line, use it. |
2611 | | */ |
2612 | 0 | env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT); |
2613 | 0 | if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo) |
2614 | 0 | die(_("attempt to reinitialize repository with different hash")); |
2615 | 0 | else if (hash != GIT_HASH_UNKNOWN) |
2616 | 0 | repo_fmt->hash_algo = hash; |
2617 | 0 | else if (env) { |
2618 | 0 | int env_algo = hash_algo_by_name(env); |
2619 | 0 | if (env_algo == GIT_HASH_UNKNOWN) |
2620 | 0 | die(_("unknown hash algorithm '%s'"), env); |
2621 | 0 | if (repo_fmt->version < 0 || |
2622 | 0 | repo_fmt->hash_algo == GIT_HASH_UNKNOWN) |
2623 | 0 | repo_fmt->hash_algo = env_algo; |
2624 | 0 | } else if (cfg.hash != GIT_HASH_UNKNOWN) { |
2625 | 0 | repo_fmt->hash_algo = cfg.hash; |
2626 | 0 | } |
2627 | 0 | repo_set_hash_algo(the_repository, repo_fmt->hash_algo); |
2628 | |
|
2629 | 0 | env = getenv("GIT_DEFAULT_REF_FORMAT"); |
2630 | 0 | if (repo_fmt->version >= 0 && |
2631 | 0 | ref_format != REF_STORAGE_FORMAT_UNKNOWN && |
2632 | 0 | ref_format != repo_fmt->ref_storage_format) { |
2633 | 0 | die(_("attempt to reinitialize repository with different reference storage format")); |
2634 | 0 | } else if (ref_format != REF_STORAGE_FORMAT_UNKNOWN) { |
2635 | 0 | repo_fmt->ref_storage_format = ref_format; |
2636 | 0 | } else if (env) { |
2637 | 0 | ref_format = ref_storage_format_by_name(env); |
2638 | 0 | if (ref_format == REF_STORAGE_FORMAT_UNKNOWN) |
2639 | 0 | die(_("unknown ref storage format '%s'"), env); |
2640 | 0 | if (repo_fmt->version < 0 || |
2641 | 0 | repo_fmt->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN) |
2642 | 0 | repo_fmt->ref_storage_format = ref_format; |
2643 | 0 | } else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) { |
2644 | 0 | repo_fmt->ref_storage_format = cfg.ref_format; |
2645 | 0 | } else { |
2646 | 0 | repo_fmt->ref_storage_format = REF_STORAGE_FORMAT_DEFAULT; |
2647 | 0 | } |
2648 | 0 | repo_set_ref_storage_format(the_repository, repo_fmt->ref_storage_format); |
2649 | 0 | } |
2650 | | |
2651 | | int init_db(const char *git_dir, const char *real_git_dir, |
2652 | | const char *template_dir, int hash, |
2653 | | enum ref_storage_format ref_storage_format, |
2654 | | const char *initial_branch, |
2655 | | int init_shared_repository, unsigned int flags) |
2656 | 0 | { |
2657 | 0 | int reinit; |
2658 | 0 | int exist_ok = flags & INIT_DB_EXIST_OK; |
2659 | 0 | char *original_git_dir = real_pathdup(git_dir, 1); |
2660 | 0 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
2661 | |
|
2662 | 0 | if (real_git_dir) { |
2663 | 0 | struct stat st; |
2664 | |
|
2665 | 0 | if (!exist_ok && !stat(git_dir, &st)) |
2666 | 0 | die(_("%s already exists"), git_dir); |
2667 | | |
2668 | 0 | if (!exist_ok && !stat(real_git_dir, &st)) |
2669 | 0 | die(_("%s already exists"), real_git_dir); |
2670 | | |
2671 | 0 | set_git_dir(real_git_dir, 1); |
2672 | 0 | git_dir = repo_get_git_dir(the_repository); |
2673 | 0 | separate_git_dir(git_dir, original_git_dir); |
2674 | 0 | } |
2675 | 0 | else { |
2676 | 0 | set_git_dir(git_dir, 1); |
2677 | 0 | git_dir = repo_get_git_dir(the_repository); |
2678 | 0 | } |
2679 | 0 | startup_info->have_repository = 1; |
2680 | | |
2681 | | /* |
2682 | | * Check to see if the repository version is right. |
2683 | | * Note that a newly created repository does not have |
2684 | | * config file, so this will not fail. What we are catching |
2685 | | * is an attempt to reinitialize new repository with an old tool. |
2686 | | */ |
2687 | 0 | check_repository_format(&repo_fmt); |
2688 | |
|
2689 | 0 | repository_format_configure(&repo_fmt, hash, ref_storage_format); |
2690 | | |
2691 | | /* |
2692 | | * Ensure `core.hidedotfiles` is processed. This must happen after we |
2693 | | * have set up the repository format such that we can evaluate |
2694 | | * includeIf conditions correctly in the case of re-initialization. |
2695 | | */ |
2696 | 0 | repo_config(the_repository, platform_core_config, NULL); |
2697 | |
|
2698 | 0 | safe_create_dir(the_repository, git_dir, 0); |
2699 | |
|
2700 | 0 | reinit = create_default_files(template_dir, original_git_dir, |
2701 | 0 | &repo_fmt, init_shared_repository); |
2702 | |
|
2703 | 0 | if (!(flags & INIT_DB_SKIP_REFDB)) |
2704 | 0 | create_reference_database(repo_fmt.ref_storage_format, |
2705 | 0 | initial_branch, flags & INIT_DB_QUIET); |
2706 | 0 | create_object_directory(); |
2707 | |
|
2708 | 0 | if (repo_settings_get_shared_repository(the_repository)) { |
2709 | 0 | char buf[10]; |
2710 | | /* We do not spell "group" and such, so that |
2711 | | * the configuration can be read by older version |
2712 | | * of git. Note, we use octal numbers for new share modes, |
2713 | | * and compatibility values for PERM_GROUP and |
2714 | | * PERM_EVERYBODY. |
2715 | | */ |
2716 | 0 | if (repo_settings_get_shared_repository(the_repository) < 0) |
2717 | | /* force to the mode value */ |
2718 | 0 | xsnprintf(buf, sizeof(buf), "0%o", -repo_settings_get_shared_repository(the_repository)); |
2719 | 0 | else if (repo_settings_get_shared_repository(the_repository) == PERM_GROUP) |
2720 | 0 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); |
2721 | 0 | else if (repo_settings_get_shared_repository(the_repository) == PERM_EVERYBODY) |
2722 | 0 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); |
2723 | 0 | else |
2724 | 0 | BUG("invalid value for shared_repository"); |
2725 | 0 | repo_config_set(the_repository, "core.sharedrepository", buf); |
2726 | 0 | repo_config_set(the_repository, "receive.denyNonFastforwards", "true"); |
2727 | 0 | } |
2728 | | |
2729 | 0 | if (!(flags & INIT_DB_QUIET)) { |
2730 | 0 | int len = strlen(git_dir); |
2731 | |
|
2732 | 0 | if (reinit) |
2733 | 0 | printf(repo_settings_get_shared_repository(the_repository) |
2734 | 0 | ? _("Reinitialized existing shared Git repository in %s%s\n") |
2735 | 0 | : _("Reinitialized existing Git repository in %s%s\n"), |
2736 | 0 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
2737 | 0 | else |
2738 | 0 | printf(repo_settings_get_shared_repository(the_repository) |
2739 | 0 | ? _("Initialized empty shared Git repository in %s%s\n") |
2740 | 0 | : _("Initialized empty Git repository in %s%s\n"), |
2741 | 0 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
2742 | 0 | } |
2743 | |
|
2744 | 0 | clear_repository_format(&repo_fmt); |
2745 | 0 | free(original_git_dir); |
2746 | 0 | return 0; |
2747 | 0 | } |