]>
Commit | Line | Data |
---|---|---|
d288a700 | 1 | #include "cache.h" |
e90fdc39 | 2 | #include "dir.h" |
31171d9e | 3 | #include "string-list.h" |
e90fdc39 JS |
4 | |
5 | static int inside_git_dir = -1; | |
6 | static int inside_work_tree = -1; | |
d288a700 | 7 | |
645a29c4 NTND |
8 | /* |
9 | * Normalize "path", prepending the "prefix" for relative paths. If | |
10 | * remaining_prefix is not NULL, return the actual prefix still | |
11 | * remains in the path. For example, prefix = sub1/sub2/ and path is | |
12 | * | |
13 | * foo -> sub1/sub2/foo (full prefix) | |
14 | * ../foo -> sub1/foo (remaining prefix is sub1/) | |
15 | * ../../bar -> bar (no remaining prefix) | |
16 | * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix) | |
17 | * `pwd`/../bar -> sub1/bar (no remaining prefix) | |
18 | */ | |
19 | char *prefix_path_gently(const char *prefix, int len, | |
20 | int *remaining_prefix, const char *path) | |
d089ebaa JH |
21 | { |
22 | const char *orig = path; | |
18e051a3 CMAB |
23 | char *sanitized; |
24 | if (is_absolute_path(orig)) { | |
e2a57aac | 25 | const char *temp = real_path(path); |
18e051a3 CMAB |
26 | sanitized = xmalloc(len + strlen(temp) + 1); |
27 | strcpy(sanitized, temp); | |
645a29c4 NTND |
28 | if (remaining_prefix) |
29 | *remaining_prefix = 0; | |
18e051a3 CMAB |
30 | } else { |
31 | sanitized = xmalloc(len + strlen(path) + 1); | |
d089ebaa JH |
32 | if (len) |
33 | memcpy(sanitized, prefix, len); | |
34 | strcpy(sanitized + len, path); | |
645a29c4 NTND |
35 | if (remaining_prefix) |
36 | *remaining_prefix = len; | |
f332726e | 37 | } |
645a29c4 | 38 | if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) |
d089ebaa | 39 | goto error_out; |
9a13ba1b | 40 | if (is_absolute_path(orig)) { |
72ec8ba6 | 41 | size_t root_len, len, total; |
d089ebaa | 42 | const char *work_tree = get_git_work_tree(); |
b3100fd5 JH |
43 | if (!work_tree) |
44 | goto error_out; | |
45 | len = strlen(work_tree); | |
72ec8ba6 | 46 | root_len = offset_1st_component(work_tree); |
b3100fd5 | 47 | total = strlen(sanitized) + 1; |
d089ebaa | 48 | if (strncmp(sanitized, work_tree, len) || |
72ec8ba6 | 49 | (len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) { |
d089ebaa | 50 | error_out: |
546e0fd9 JK |
51 | free(sanitized); |
52 | return NULL; | |
d089ebaa JH |
53 | } |
54 | if (sanitized[len] == '/') | |
55 | len++; | |
56 | memmove(sanitized, sanitized + len, total - len); | |
57 | } | |
58 | return sanitized; | |
f332726e LT |
59 | } |
60 | ||
546e0fd9 JK |
61 | char *prefix_path(const char *prefix, int len, const char *path) |
62 | { | |
645a29c4 | 63 | char *r = prefix_path_gently(prefix, len, NULL, path); |
546e0fd9 JK |
64 | if (!r) |
65 | die("'%s' is outside repository", path); | |
66 | return r; | |
67 | } | |
68 | ||
69 | int path_inside_repo(const char *prefix, const char *path) | |
70 | { | |
71 | int len = prefix ? strlen(prefix) : 0; | |
645a29c4 | 72 | char *r = prefix_path_gently(prefix, len, NULL, path); |
546e0fd9 JK |
73 | if (r) { |
74 | free(r); | |
75 | return 1; | |
76 | } | |
77 | return 0; | |
78 | } | |
79 | ||
c6e8c800 JH |
80 | int check_filename(const char *prefix, const char *arg) |
81 | { | |
82 | const char *name; | |
83 | struct stat st; | |
84 | ||
4db86e8b NTND |
85 | if (!prefixcmp(arg, ":/")) { |
86 | if (arg[2] == '\0') /* ":/" is root dir, always exists */ | |
87 | return 1; | |
88 | name = arg + 2; | |
89 | } else if (prefix) | |
90 | name = prefix_filename(prefix, strlen(prefix), arg); | |
91 | else | |
92 | name = arg; | |
c6e8c800 JH |
93 | if (!lstat(name, &st)) |
94 | return 1; /* file exists */ | |
95 | if (errno == ENOENT || errno == ENOTDIR) | |
96 | return 0; /* file does not exist */ | |
97 | die_errno("failed to stat '%s'", arg); | |
98 | } | |
99 | ||
023e37c3 MM |
100 | static void NORETURN die_verify_filename(const char *prefix, |
101 | const char *arg, | |
102 | int diagnose_misspelt_rev) | |
009fee47 | 103 | { |
023e37c3 MM |
104 | if (!diagnose_misspelt_rev) |
105 | die("%s: no such path in the working tree.\n" | |
4d4b5739 | 106 | "Use 'git <command> -- <path>...' to specify paths that do not exist locally.", |
023e37c3 | 107 | arg); |
0e539dca JH |
108 | /* |
109 | * Saying "'(icase)foo' does not exist in the index" when the | |
110 | * user gave us ":(icase)foo" is just stupid. A magic pathspec | |
111 | * begins with a colon and is followed by a non-alnum; do not | |
8c135ea2 | 112 | * let maybe_die_on_misspelt_object_name() even trigger. |
0e539dca JH |
113 | */ |
114 | if (!(arg[0] == ':' && !isalnum(arg[1]))) | |
8c135ea2 | 115 | maybe_die_on_misspelt_object_name(arg, prefix); |
0e539dca | 116 | |
009fee47 MM |
117 | /* ... or fall back the most general message. */ |
118 | die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" | |
4d4b5739 MM |
119 | "Use '--' to separate paths from revisions, like this:\n" |
120 | "'git <command> [<revision>...] -- [<file>...]'", arg); | |
009fee47 MM |
121 | |
122 | } | |
123 | ||
e23d0b4a LT |
124 | /* |
125 | * Verify a filename that we got as an argument for a pathspec | |
126 | * entry. Note that a filename that begins with "-" never verifies | |
127 | * as true, because even if such a filename were to exist, we want | |
128 | * it to be preceded by the "--" marker (or we want the user to | |
129 | * use a format like "./-filename") | |
023e37c3 MM |
130 | * |
131 | * The "diagnose_misspelt_rev" is used to provide a user-friendly | |
132 | * diagnosis when dying upon finding that "name" is not a pathname. | |
133 | * If set to 1, the diagnosis will try to diagnose "name" as an | |
134 | * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis | |
135 | * will only complain about an inexisting file. | |
136 | * | |
137 | * This function is typically called to check that a "file or rev" | |
138 | * argument is unambiguous. In this case, the caller will want | |
139 | * diagnose_misspelt_rev == 1 when verifying the first non-rev | |
140 | * argument (which could have been a revision), and | |
141 | * diagnose_misspelt_rev == 0 for the next ones (because we already | |
142 | * saw a filename, there's not ambiguity anymore). | |
e23d0b4a | 143 | */ |
023e37c3 MM |
144 | void verify_filename(const char *prefix, |
145 | const char *arg, | |
146 | int diagnose_misspelt_rev) | |
e23d0b4a | 147 | { |
e23d0b4a LT |
148 | if (*arg == '-') |
149 | die("bad flag '%s' used after filename", arg); | |
c6e8c800 | 150 | if (check_filename(prefix, arg)) |
e23d0b4a | 151 | return; |
023e37c3 | 152 | die_verify_filename(prefix, arg, diagnose_misspelt_rev); |
e23d0b4a LT |
153 | } |
154 | ||
ea92f41f JH |
155 | /* |
156 | * Opposite of the above: the command line did not have -- marker | |
157 | * and we parsed the arg as a refname. It should not be interpretable | |
158 | * as a filename. | |
159 | */ | |
160 | void verify_non_filename(const char *prefix, const char *arg) | |
161 | { | |
7ae3df8c | 162 | if (!is_inside_work_tree() || is_inside_git_dir()) |
68025633 | 163 | return; |
ea92f41f JH |
164 | if (*arg == '-') |
165 | return; /* flag */ | |
c6e8c800 JH |
166 | if (!check_filename(prefix, arg)) |
167 | return; | |
168 | die("ambiguous argument '%s': both revision and filename\n" | |
4d4b5739 MM |
169 | "Use '--' to separate paths from revisions, like this:\n" |
170 | "'git <command> [<revision>...] -- [<file>...]'", arg); | |
ea92f41f JH |
171 | } |
172 | ||
d288a700 | 173 | |
5f5608bc | 174 | /* |
ad1a382f | 175 | * Test if it looks like we're at a git directory. |
5e7bfe25 | 176 | * We want to see: |
5f5608bc | 177 | * |
790296fd | 178 | * - either an objects/ directory _or_ the proper |
5f5608bc | 179 | * GIT_OBJECT_DIRECTORY environment variable |
ad1a382f | 180 | * - a refs/ directory |
8098a178 | 181 | * - either a HEAD symlink or a HEAD file that is formatted as |
c847f537 JH |
182 | * a proper "ref:", or a regular file HEAD that has a properly |
183 | * formatted sha1 object name. | |
5f5608bc | 184 | */ |
b3256eb8 | 185 | int is_git_directory(const char *suspect) |
5f5608bc | 186 | { |
ad1a382f SP |
187 | char path[PATH_MAX]; |
188 | size_t len = strlen(suspect); | |
189 | ||
3c9d0414 GB |
190 | if (PATH_MAX <= len + strlen("/objects")) |
191 | die("Too long path: %.*s", 60, suspect); | |
ad1a382f SP |
192 | strcpy(path, suspect); |
193 | if (getenv(DB_ENVIRONMENT)) { | |
194 | if (access(getenv(DB_ENVIRONMENT), X_OK)) | |
195 | return 0; | |
196 | } | |
197 | else { | |
198 | strcpy(path + len, "/objects"); | |
199 | if (access(path, X_OK)) | |
200 | return 0; | |
201 | } | |
202 | ||
203 | strcpy(path + len, "/refs"); | |
204 | if (access(path, X_OK)) | |
8098a178 | 205 | return 0; |
ad1a382f SP |
206 | |
207 | strcpy(path + len, "/HEAD"); | |
c847f537 | 208 | if (validate_headref(path)) |
ad1a382f SP |
209 | return 0; |
210 | ||
8098a178 | 211 | return 1; |
5f5608bc LT |
212 | } |
213 | ||
68025633 JS |
214 | int is_inside_git_dir(void) |
215 | { | |
e90fdc39 JS |
216 | if (inside_git_dir < 0) |
217 | inside_git_dir = is_inside_dir(get_git_dir()); | |
218 | return inside_git_dir; | |
892c41b9 ML |
219 | } |
220 | ||
892c41b9 ML |
221 | int is_inside_work_tree(void) |
222 | { | |
e90fdc39 JS |
223 | if (inside_work_tree < 0) |
224 | inside_work_tree = is_inside_dir(get_git_work_tree()); | |
225 | return inside_work_tree; | |
892c41b9 ML |
226 | } |
227 | ||
f3fa1838 JH |
228 | void setup_work_tree(void) |
229 | { | |
354e6534 JS |
230 | const char *work_tree, *git_dir; |
231 | static int initialized = 0; | |
232 | ||
233 | if (initialized) | |
234 | return; | |
235 | work_tree = get_git_work_tree(); | |
236 | git_dir = get_git_dir(); | |
59f0f2f3 | 237 | if (!is_absolute_path(git_dir)) |
e2a57aac | 238 | git_dir = real_path(get_git_dir()); |
59f0f2f3 MH |
239 | if (!work_tree || chdir(work_tree)) |
240 | die("This operation must be run in a work tree"); | |
0ed74813 NTND |
241 | |
242 | /* | |
243 | * Make sure subsequent git processes find correct worktree | |
244 | * if $GIT_WORK_TREE is set relative | |
245 | */ | |
246 | if (getenv(GIT_WORK_TREE_ENVIRONMENT)) | |
247 | setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1); | |
248 | ||
41894ae3 | 249 | set_git_dir(remove_leading_path(git_dir, work_tree)); |
354e6534 | 250 | initialized = 1; |
59f0f2f3 MH |
251 | } |
252 | ||
337e51ce | 253 | static int check_repository_format_gently(const char *gitdir, int *nongit_ok) |
9459aa77 | 254 | { |
337e51ce NTND |
255 | char repo_config[PATH_MAX+1]; |
256 | ||
257 | /* | |
258 | * git_config() can't be used here because it calls git_pathdup() | |
259 | * to get $GIT_CONFIG/config. That call will make setup_git_env() | |
260 | * set git_dir to ".git". | |
261 | * | |
262 | * We are in gitdir setup, no git dir has been found useable yet. | |
263 | * Use a gentler version of git_config() to check if this repo | |
264 | * is a good one. | |
265 | */ | |
266 | snprintf(repo_config, PATH_MAX, "%s/config", gitdir); | |
267 | git_config_early(check_repository_format_version, NULL, repo_config); | |
9459aa77 NTND |
268 | if (GIT_REPO_VERSION < repository_format_version) { |
269 | if (!nongit_ok) | |
270 | die ("Expected git repo version <= %d, found %d", | |
271 | GIT_REPO_VERSION, repository_format_version); | |
272 | warning("Expected git repo version <= %d, found %d", | |
273 | GIT_REPO_VERSION, repository_format_version); | |
274 | warning("Please upgrade Git"); | |
275 | *nongit_ok = -1; | |
276 | return -1; | |
277 | } | |
278 | return 0; | |
279 | } | |
280 | ||
b44ebb19 LH |
281 | /* |
282 | * Try to read the location of the git directory from the .git file, | |
283 | * return path to git directory if found. | |
284 | */ | |
13d6ec91 | 285 | const char *read_gitfile(const char *path) |
b44ebb19 LH |
286 | { |
287 | char *buf; | |
40c813e0 BK |
288 | char *dir; |
289 | const char *slash; | |
b44ebb19 LH |
290 | struct stat st; |
291 | int fd; | |
b1905aea | 292 | ssize_t len; |
b44ebb19 LH |
293 | |
294 | if (stat(path, &st)) | |
295 | return NULL; | |
296 | if (!S_ISREG(st.st_mode)) | |
297 | return NULL; | |
298 | fd = open(path, O_RDONLY); | |
299 | if (fd < 0) | |
d824cbba | 300 | die_errno("Error opening '%s'", path); |
b44ebb19 LH |
301 | buf = xmalloc(st.st_size + 1); |
302 | len = read_in_full(fd, buf, st.st_size); | |
303 | close(fd); | |
304 | if (len != st.st_size) | |
305 | die("Error reading %s", path); | |
306 | buf[len] = '\0'; | |
307 | if (prefixcmp(buf, "gitdir: ")) | |
308 | die("Invalid gitfile format: %s", path); | |
309 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') | |
310 | len--; | |
311 | if (len < 9) | |
312 | die("No path in gitfile: %s", path); | |
313 | buf[len] = '\0'; | |
40c813e0 BK |
314 | dir = buf + 8; |
315 | ||
316 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
317 | size_t pathlen = slash+1 - path; | |
318 | size_t dirlen = pathlen + len - 8; | |
319 | dir = xmalloc(dirlen + 1); | |
320 | strncpy(dir, path, pathlen); | |
321 | strncpy(dir + pathlen, buf + 8, len - 8); | |
322 | dir[dirlen] = '\0'; | |
323 | free(buf); | |
324 | buf = dir; | |
325 | } | |
326 | ||
327 | if (!is_git_directory(dir)) | |
328 | die("Not a git repository: %s", dir); | |
e2a57aac | 329 | path = real_path(dir); |
40c813e0 | 330 | |
b44ebb19 LH |
331 | free(buf); |
332 | return path; | |
333 | } | |
334 | ||
e4e30347 | 335 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
b3f66fd3 NTND |
336 | char *cwd, int len, |
337 | int *nongit_ok) | |
e4e30347 | 338 | { |
b3f66fd3 NTND |
339 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
340 | const char *worktree; | |
341 | char *gitfile; | |
9b125da4 | 342 | int offset; |
e4e30347 JN |
343 | |
344 | if (PATH_MAX - 40 < strlen(gitdirenv)) | |
345 | die("'$%s' too big", GIT_DIR_ENVIRONMENT); | |
b3f66fd3 | 346 | |
13d6ec91 | 347 | gitfile = (char*)read_gitfile(gitdirenv); |
b3f66fd3 NTND |
348 | if (gitfile) { |
349 | gitfile = xstrdup(gitfile); | |
350 | gitdirenv = gitfile; | |
351 | } | |
352 | ||
e4e30347 JN |
353 | if (!is_git_directory(gitdirenv)) { |
354 | if (nongit_ok) { | |
355 | *nongit_ok = 1; | |
b3f66fd3 | 356 | free(gitfile); |
e4e30347 JN |
357 | return NULL; |
358 | } | |
359 | die("Not a git repository: '%s'", gitdirenv); | |
360 | } | |
b3f66fd3 NTND |
361 | |
362 | if (check_repository_format_gently(gitdirenv, nongit_ok)) { | |
363 | free(gitfile); | |
364 | return NULL; | |
e4e30347 | 365 | } |
b3f66fd3 NTND |
366 | |
367 | /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */ | |
368 | if (work_tree_env) | |
369 | set_git_work_tree(work_tree_env); | |
370 | else if (is_bare_repository_cfg > 0) { | |
371 | if (git_work_tree_cfg) /* #22.2, #30 */ | |
372 | die("core.bare and core.worktree do not make sense"); | |
373 | ||
374 | /* #18, #26 */ | |
375 | set_git_dir(gitdirenv); | |
376 | free(gitfile); | |
e4e30347 | 377 | return NULL; |
b3f66fd3 NTND |
378 | } |
379 | else if (git_work_tree_cfg) { /* #6, #14 */ | |
380 | if (is_absolute_path(git_work_tree_cfg)) | |
381 | set_git_work_tree(git_work_tree_cfg); | |
382 | else { | |
383 | char core_worktree[PATH_MAX]; | |
384 | if (chdir(gitdirenv)) | |
385 | die_errno("Could not chdir to '%s'", gitdirenv); | |
386 | if (chdir(git_work_tree_cfg)) | |
387 | die_errno("Could not chdir to '%s'", git_work_tree_cfg); | |
388 | if (!getcwd(core_worktree, PATH_MAX)) | |
389 | die_errno("Could not get directory '%s'", git_work_tree_cfg); | |
390 | if (chdir(cwd)) | |
391 | die_errno("Could not come back to cwd"); | |
392 | set_git_work_tree(core_worktree); | |
393 | } | |
394 | } | |
2cd83d10 JK |
395 | else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) { |
396 | /* #16d */ | |
397 | set_git_dir(gitdirenv); | |
398 | free(gitfile); | |
399 | return NULL; | |
400 | } | |
b3f66fd3 NTND |
401 | else /* #2, #10 */ |
402 | set_git_work_tree("."); | |
403 | ||
404 | /* set_git_work_tree() must have been called by now */ | |
405 | worktree = get_git_work_tree(); | |
406 | ||
407 | /* both get_git_work_tree() and cwd are already normalized */ | |
408 | if (!strcmp(cwd, worktree)) { /* cwd == worktree */ | |
409 | set_git_dir(gitdirenv); | |
410 | free(gitfile); | |
e4e30347 | 411 | return NULL; |
b3f66fd3 | 412 | } |
e4e30347 | 413 | |
9b125da4 NTND |
414 | offset = dir_inside_of(cwd, worktree); |
415 | if (offset >= 0) { /* cwd inside worktree? */ | |
e2a57aac | 416 | set_git_dir(real_path(gitdirenv)); |
b3f66fd3 NTND |
417 | if (chdir(worktree)) |
418 | die_errno("Could not chdir to '%s'", worktree); | |
419 | cwd[len++] = '/'; | |
420 | cwd[len] = '\0'; | |
421 | free(gitfile); | |
9b125da4 | 422 | return cwd + offset; |
93a00542 | 423 | } |
b3f66fd3 NTND |
424 | |
425 | /* cwd outside worktree */ | |
426 | set_git_dir(gitdirenv); | |
427 | free(gitfile); | |
428 | return NULL; | |
93a00542 JN |
429 | } |
430 | ||
9951d3b3 NTND |
431 | static const char *setup_discovered_git_dir(const char *gitdir, |
432 | char *cwd, int offset, int len, | |
433 | int *nongit_ok) | |
98937bef | 434 | { |
9951d3b3 NTND |
435 | if (check_repository_format_gently(gitdir, nongit_ok)) |
436 | return NULL; | |
98937bef | 437 | |
4868b2ea JN |
438 | /* --work-tree is set without --git-dir; use discovered one */ |
439 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
440 | if (offset != len && !is_absolute_path(gitdir)) | |
e2a57aac | 441 | gitdir = xstrdup(real_path(gitdir)); |
4868b2ea JN |
442 | if (chdir(cwd)) |
443 | die_errno("Could not come back to cwd"); | |
444 | return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok); | |
445 | } | |
446 | ||
9951d3b3 NTND |
447 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
448 | if (is_bare_repository_cfg > 0) { | |
e2a57aac | 449 | set_git_dir(offset == len ? gitdir : real_path(gitdir)); |
9951d3b3 NTND |
450 | if (chdir(cwd)) |
451 | die_errno("Could not come back to cwd"); | |
98937bef | 452 | return NULL; |
9951d3b3 | 453 | } |
98937bef | 454 | |
9951d3b3 NTND |
455 | /* #0, #1, #5, #8, #9, #12, #13 */ |
456 | set_git_work_tree("."); | |
457 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) | |
458 | set_git_dir(gitdir); | |
98937bef | 459 | inside_git_dir = 0; |
9951d3b3 | 460 | inside_work_tree = 1; |
98937bef NTND |
461 | if (offset == len) |
462 | return NULL; | |
463 | ||
464 | /* Make "offset" point to past the '/', and add a '/' at the end */ | |
465 | offset++; | |
466 | cwd[len++] = '/'; | |
467 | cwd[len] = 0; | |
468 | return cwd + offset; | |
469 | } | |
470 | ||
1cd8031b NTND |
471 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
472 | static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok) | |
68698da5 JN |
473 | { |
474 | int root_len; | |
475 | ||
1cd8031b NTND |
476 | if (check_repository_format_gently(".", nongit_ok)) |
477 | return NULL; | |
478 | ||
2cd83d10 JK |
479 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
480 | ||
4868b2ea JN |
481 | /* --work-tree is set without --git-dir; use discovered one */ |
482 | if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) { | |
483 | const char *gitdir; | |
484 | ||
485 | gitdir = offset == len ? "." : xmemdupz(cwd, offset); | |
486 | if (chdir(cwd)) | |
487 | die_errno("Could not come back to cwd"); | |
488 | return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok); | |
489 | } | |
490 | ||
68698da5 | 491 | inside_git_dir = 1; |
1cd8031b | 492 | inside_work_tree = 0; |
68698da5 | 493 | if (offset != len) { |
8fc0ae80 NTND |
494 | if (chdir(cwd)) |
495 | die_errno("Cannot come back to cwd"); | |
68698da5 JN |
496 | root_len = offset_1st_component(cwd); |
497 | cwd[offset > root_len ? offset : root_len] = '\0'; | |
498 | set_git_dir(cwd); | |
337e51ce | 499 | } |
1cd8031b | 500 | else |
68698da5 | 501 | set_git_dir("."); |
68698da5 JN |
502 | return NULL; |
503 | } | |
504 | ||
f161edeb JN |
505 | static const char *setup_nongit(const char *cwd, int *nongit_ok) |
506 | { | |
507 | if (!nongit_ok) | |
508 | die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); | |
509 | if (chdir(cwd)) | |
510 | die_errno("Cannot come back to cwd"); | |
511 | *nongit_ok = 1; | |
512 | return NULL; | |
513 | } | |
514 | ||
2565b43b | 515 | static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len) |
60c98d1e JN |
516 | { |
517 | struct stat buf; | |
2565b43b CB |
518 | if (stat(path, &buf)) { |
519 | die_errno("failed to stat '%*s%s%s'", | |
520 | prefix_len, | |
60c98d1e JN |
521 | prefix ? prefix : "", |
522 | prefix ? "/" : "", path); | |
2565b43b | 523 | } |
60c98d1e JN |
524 | return buf.st_dev; |
525 | } | |
526 | ||
9e2326c7 | 527 | /* |
1b77d83c MH |
528 | * A "string_list_each_func_t" function that canonicalizes an entry |
529 | * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or | |
7ec30aaa MH |
530 | * discards it if unusable. The presence of an empty entry in |
531 | * GIT_CEILING_DIRECTORIES turns off canonicalization for all | |
532 | * subsequent entries. | |
9e2326c7 | 533 | */ |
1b77d83c | 534 | static int canonicalize_ceiling_entry(struct string_list_item *item, |
7ec30aaa | 535 | void *cb_data) |
9e2326c7 | 536 | { |
7ec30aaa | 537 | int *empty_entry_found = cb_data; |
1b77d83c | 538 | char *ceil = item->string; |
9e2326c7 | 539 | |
7ec30aaa MH |
540 | if (!*ceil) { |
541 | *empty_entry_found = 1; | |
9e2326c7 | 542 | return 0; |
7ec30aaa | 543 | } else if (!is_absolute_path(ceil)) { |
9e2326c7 | 544 | return 0; |
7ec30aaa MH |
545 | } else if (*empty_entry_found) { |
546 | /* Keep entry but do not canonicalize it */ | |
547 | return 1; | |
548 | } else { | |
549 | const char *real_path = real_path_if_valid(ceil); | |
550 | if (!real_path) | |
551 | return 0; | |
552 | free(item->string); | |
553 | item->string = xstrdup(real_path); | |
554 | return 1; | |
555 | } | |
9e2326c7 MH |
556 | } |
557 | ||
e90fdc39 JS |
558 | /* |
559 | * We cannot decide in this function whether we are in the work tree or | |
560 | * not, since the config can only be read _after_ this function was called. | |
561 | */ | |
a60645f9 | 562 | static const char *setup_git_directory_gently_1(int *nongit_ok) |
d288a700 | 563 | { |
0454dd93 | 564 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
31171d9e | 565 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
abf03eeb | 566 | static char cwd[PATH_MAX + 1]; |
9951d3b3 NTND |
567 | const char *gitdirenv, *ret; |
568 | char *gitfile; | |
31171d9e | 569 | int len, offset, offset_parent, ceil_offset = -1; |
c7d1d1b1 RH |
570 | dev_t current_device = 0; |
571 | int one_filesystem = 1; | |
d288a700 | 572 | |
af05d679 SG |
573 | /* |
574 | * Let's assume that we are in a git repository. | |
575 | * If it turns out later that we are somewhere else, the value will be | |
576 | * updated accordingly. | |
577 | */ | |
578 | if (nongit_ok) | |
579 | *nongit_ok = 0; | |
580 | ||
abf03eeb | 581 | if (!getcwd(cwd, sizeof(cwd) - 1)) |
b3f66fd3 NTND |
582 | die_errno("Unable to read current working directory"); |
583 | offset = len = strlen(cwd); | |
584 | ||
e90fdc39 JS |
585 | /* |
586 | * If GIT_DIR is set explicitly, we're not going | |
587 | * to do any discovery, but we still do repository | |
588 | * validation. | |
589 | */ | |
ad1a382f | 590 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
e4e30347 | 591 | if (gitdirenv) |
b3f66fd3 | 592 | return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok); |
d288a700 | 593 | |
31171d9e | 594 | if (env_ceiling_dirs) { |
7ec30aaa MH |
595 | int empty_entry_found = 0; |
596 | ||
31171d9e | 597 | string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1); |
1b77d83c | 598 | filter_string_list(&ceiling_dirs, 0, |
7ec30aaa | 599 | canonicalize_ceiling_entry, &empty_entry_found); |
31171d9e MH |
600 | ceil_offset = longest_ancestor_length(cwd, &ceiling_dirs); |
601 | string_list_clear(&ceiling_dirs, 0); | |
602 | } | |
603 | ||
17d778e7 JH |
604 | if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) |
605 | ceil_offset = 1; | |
d288a700 | 606 | |
892c41b9 | 607 | /* |
e90fdc39 | 608 | * Test in the following order (relative to the cwd): |
b44ebb19 | 609 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
610 | * - .git/ |
611 | * - ./ (bare) | |
b44ebb19 | 612 | * - ../.git |
e90fdc39 JS |
613 | * - ../.git/ |
614 | * - ../ (bare) | |
615 | * - ../../.git/ | |
616 | * etc. | |
892c41b9 | 617 | */ |
cf87463e | 618 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
60c98d1e | 619 | if (one_filesystem) |
2565b43b | 620 | current_device = get_device_or_die(".", NULL, 0); |
e90fdc39 | 621 | for (;;) { |
13d6ec91 | 622 | gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT); |
9951d3b3 NTND |
623 | if (gitfile) |
624 | gitdirenv = gitfile = xstrdup(gitfile); | |
625 | else { | |
626 | if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) | |
627 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; | |
628 | } | |
629 | ||
630 | if (gitdirenv) { | |
631 | ret = setup_discovered_git_dir(gitdirenv, | |
632 | cwd, offset, len, | |
633 | nongit_ok); | |
634 | free(gitfile); | |
635 | return ret; | |
636 | } | |
637 | free(gitfile); | |
638 | ||
68698da5 | 639 | if (is_git_directory(".")) |
1cd8031b NTND |
640 | return setup_bare_git_dir(cwd, offset, len, nongit_ok); |
641 | ||
2565b43b CB |
642 | offset_parent = offset; |
643 | while (--offset_parent > ceil_offset && cwd[offset_parent] != '/'); | |
644 | if (offset_parent <= ceil_offset) | |
f161edeb | 645 | return setup_nongit(cwd, nongit_ok); |
8030e442 | 646 | if (one_filesystem) { |
2565b43b | 647 | dev_t parent_device = get_device_or_die("..", cwd, offset); |
60c98d1e | 648 | if (parent_device != current_device) { |
8030e442 LD |
649 | if (nongit_ok) { |
650 | if (chdir(cwd)) | |
651 | die_errno("Cannot come back to cwd"); | |
652 | *nongit_ok = 1; | |
653 | return NULL; | |
654 | } | |
655 | cwd[offset] = '\0'; | |
2565b43b | 656 | die("Not a git repository (or any parent up to mount point %s)\n" |
cf87463e | 657 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd); |
8030e442 LD |
658 | } |
659 | } | |
502ffe34 LD |
660 | if (chdir("..")) { |
661 | cwd[offset] = '\0'; | |
d824cbba | 662 | die_errno("Cannot change to '%s/..'", cwd); |
502ffe34 | 663 | } |
2565b43b | 664 | offset = offset_parent; |
892c41b9 | 665 | } |
d288a700 | 666 | } |
5e7bfe25 | 667 | |
a60645f9 NTND |
668 | const char *setup_git_directory_gently(int *nongit_ok) |
669 | { | |
670 | const char *prefix; | |
671 | ||
672 | prefix = setup_git_directory_gently_1(nongit_ok); | |
1f5d271f | 673 | if (prefix) |
a6f7f9a3 | 674 | setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1); |
1f5d271f | 675 | else |
a6f7f9a3 | 676 | setenv(GIT_PREFIX_ENVIRONMENT, "", 1); |
1f5d271f | 677 | |
f07d6a1a | 678 | if (startup_info) { |
a60645f9 | 679 | startup_info->have_repository = !nongit_ok || !*nongit_ok; |
f07d6a1a NTND |
680 | startup_info->prefix = prefix; |
681 | } | |
a60645f9 NTND |
682 | return prefix; |
683 | } | |
684 | ||
94df2506 JH |
685 | int git_config_perm(const char *var, const char *value) |
686 | { | |
06cbe855 HO |
687 | int i; |
688 | char *endptr; | |
689 | ||
690 | if (value == NULL) | |
691 | return PERM_GROUP; | |
692 | ||
693 | if (!strcmp(value, "umask")) | |
694 | return PERM_UMASK; | |
695 | if (!strcmp(value, "group")) | |
696 | return PERM_GROUP; | |
697 | if (!strcmp(value, "all") || | |
698 | !strcmp(value, "world") || | |
699 | !strcmp(value, "everybody")) | |
700 | return PERM_EVERYBODY; | |
701 | ||
702 | /* Parse octal numbers */ | |
703 | i = strtol(value, &endptr, 8); | |
704 | ||
705 | /* If not an octal number, maybe true/false? */ | |
706 | if (*endptr != 0) | |
707 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
708 | ||
709 | /* | |
710 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 711 | * a chmod value to restrict to. |
06cbe855 HO |
712 | */ |
713 | switch (i) { | |
714 | case PERM_UMASK: /* 0 */ | |
715 | return PERM_UMASK; | |
716 | case OLD_PERM_GROUP: /* 1 */ | |
717 | return PERM_GROUP; | |
718 | case OLD_PERM_EVERYBODY: /* 2 */ | |
719 | return PERM_EVERYBODY; | |
94df2506 | 720 | } |
06cbe855 HO |
721 | |
722 | /* A filemode value was given: 0xxx */ | |
723 | ||
724 | if ((i & 0600) != 0600) | |
725 | die("Problem with core.sharedRepository filemode value " | |
726 | "(0%.3o).\nThe owner of files must always have " | |
727 | "read and write permissions.", i); | |
728 | ||
729 | /* | |
730 | * Mask filemode value. Others can not get write permission. | |
731 | * x flags for directories are handled separately. | |
732 | */ | |
5a688fe4 | 733 | return -(i & 0666); |
94df2506 JH |
734 | } |
735 | ||
ef90d6d4 | 736 | int check_repository_format_version(const char *var, const char *value, void *cb) |
ab9cb76f | 737 | { |
299726d5 JS |
738 | if (strcmp(var, "core.repositoryformatversion") == 0) |
739 | repository_format_version = git_config_int(var, value); | |
457f06d6 | 740 | else if (strcmp(var, "core.sharedrepository") == 0) |
94df2506 | 741 | shared_repository = git_config_perm(var, value); |
e90fdc39 JS |
742 | else if (strcmp(var, "core.bare") == 0) { |
743 | is_bare_repository_cfg = git_config_bool(var, value); | |
744 | if (is_bare_repository_cfg == 1) | |
745 | inside_work_tree = -1; | |
746 | } else if (strcmp(var, "core.worktree") == 0) { | |
180483c5 JH |
747 | if (!value) |
748 | return config_error_nonbool(var); | |
8e0f7003 | 749 | free(git_work_tree_cfg); |
e90fdc39 JS |
750 | git_work_tree_cfg = xstrdup(value); |
751 | inside_work_tree = -1; | |
752 | } | |
299726d5 | 753 | return 0; |
ab9cb76f JH |
754 | } |
755 | ||
756 | int check_repository_format(void) | |
757 | { | |
337e51ce | 758 | return check_repository_format_gently(get_git_dir(), NULL); |
ab9cb76f JH |
759 | } |
760 | ||
e1e5ec86 CB |
761 | /* |
762 | * Returns the "prefix", a path to the current working directory | |
763 | * relative to the work tree root, or NULL, if the current working | |
764 | * directory is not a strict subdirectory of the work tree root. The | |
765 | * prefix always ends with a '/' character. | |
766 | */ | |
5e7bfe25 JH |
767 | const char *setup_git_directory(void) |
768 | { | |
b3f66fd3 | 769 | return setup_git_directory_gently(NULL); |
5e7bfe25 | 770 | } |
abc06822 FG |
771 | |
772 | const char *resolve_gitdir(const char *suspect) | |
773 | { | |
774 | if (is_git_directory(suspect)) | |
775 | return suspect; | |
efc5fb6a | 776 | return read_gitfile(suspect); |
abc06822 | 777 | } |
1d999ddd TR |
778 | |
779 | /* if any standard file descriptor is missing open it to /dev/null */ | |
780 | void sanitize_stdfds(void) | |
781 | { | |
782 | int fd = open("/dev/null", O_RDWR, 0); | |
783 | while (fd != -1 && fd < 2) | |
784 | fd = dup(fd); | |
785 | if (fd == -1) | |
786 | die_errno("open /dev/null or dup failed"); | |
787 | if (fd > 2) | |
788 | close(fd); | |
789 | } |