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