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