]>
Commit | Line | Data |
---|---|---|
d288a700 | 1 | #include "cache.h" |
e90fdc39 JS |
2 | #include "dir.h" |
3 | ||
4 | static int inside_git_dir = -1; | |
5 | static int inside_work_tree = -1; | |
d288a700 | 6 | |
d089ebaa JH |
7 | const char *prefix_path(const char *prefix, int len, const char *path) |
8 | { | |
9 | const char *orig = path; | |
10 | char *sanitized = xmalloc(len + strlen(path) + 1); | |
9a13ba1b | 11 | if (is_absolute_path(orig)) |
d089ebaa JH |
12 | strcpy(sanitized, path); |
13 | else { | |
14 | if (len) | |
15 | memcpy(sanitized, prefix, len); | |
16 | strcpy(sanitized + len, path); | |
f332726e | 17 | } |
f3cad0ad | 18 | if (normalize_path_copy(sanitized, sanitized)) |
d089ebaa | 19 | goto error_out; |
9a13ba1b | 20 | if (is_absolute_path(orig)) { |
72ec8ba6 | 21 | size_t root_len, len, total; |
d089ebaa | 22 | const char *work_tree = get_git_work_tree(); |
b3100fd5 JH |
23 | if (!work_tree) |
24 | goto error_out; | |
25 | len = strlen(work_tree); | |
72ec8ba6 | 26 | root_len = offset_1st_component(work_tree); |
b3100fd5 | 27 | total = strlen(sanitized) + 1; |
d089ebaa | 28 | if (strncmp(sanitized, work_tree, len) || |
72ec8ba6 | 29 | (len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) { |
d089ebaa | 30 | error_out: |
62525ef7 | 31 | die("'%s' is outside repository", orig); |
d089ebaa JH |
32 | } |
33 | if (sanitized[len] == '/') | |
34 | len++; | |
35 | memmove(sanitized, sanitized + len, total - len); | |
36 | } | |
37 | return sanitized; | |
f332726e LT |
38 | } |
39 | ||
a6080a0a | 40 | /* |
4ca06608 JH |
41 | * Unlike prefix_path, this should be used if the named file does |
42 | * not have to interact with index entry; i.e. name of a random file | |
43 | * on the filesystem. | |
44 | */ | |
45 | const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) | |
46 | { | |
47 | static char path[PATH_MAX]; | |
71064e3f | 48 | #ifndef WIN32 |
ecf4831d | 49 | if (!pfx || !*pfx || is_absolute_path(arg)) |
4ca06608 JH |
50 | return arg; |
51 | memcpy(path, pfx, pfx_len); | |
52 | strcpy(path + pfx_len, arg); | |
25fe217b JS |
53 | #else |
54 | char *p; | |
55 | /* don't add prefix to absolute paths, but still replace '\' by '/' */ | |
56 | if (is_absolute_path(arg)) | |
57 | pfx_len = 0; | |
58 | else | |
59 | memcpy(path, pfx, pfx_len); | |
60 | strcpy(path + pfx_len, arg); | |
61 | for (p = path + pfx_len; *p; p++) | |
62 | if (*p == '\\') | |
63 | *p = '/'; | |
64 | #endif | |
4ca06608 JH |
65 | return path; |
66 | } | |
67 | ||
c6e8c800 JH |
68 | int check_filename(const char *prefix, const char *arg) |
69 | { | |
70 | const char *name; | |
71 | struct stat st; | |
72 | ||
73 | name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg; | |
74 | if (!lstat(name, &st)) | |
75 | return 1; /* file exists */ | |
76 | if (errno == ENOENT || errno == ENOTDIR) | |
77 | return 0; /* file does not exist */ | |
78 | die_errno("failed to stat '%s'", arg); | |
79 | } | |
80 | ||
009fee47 MM |
81 | static void NORETURN die_verify_filename(const char *prefix, const char *arg) |
82 | { | |
83 | unsigned char sha1[20]; | |
84 | unsigned mode; | |
85 | /* try a detailed diagnostic ... */ | |
86 | get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix); | |
87 | /* ... or fall back the most general message. */ | |
88 | die("ambiguous argument '%s': unknown revision or path not in the working tree.\n" | |
89 | "Use '--' to separate paths from revisions", arg); | |
90 | ||
91 | } | |
92 | ||
e23d0b4a LT |
93 | /* |
94 | * Verify a filename that we got as an argument for a pathspec | |
95 | * entry. Note that a filename that begins with "-" never verifies | |
96 | * as true, because even if such a filename were to exist, we want | |
97 | * it to be preceded by the "--" marker (or we want the user to | |
98 | * use a format like "./-filename") | |
99 | */ | |
100 | void verify_filename(const char *prefix, const char *arg) | |
101 | { | |
e23d0b4a LT |
102 | if (*arg == '-') |
103 | die("bad flag '%s' used after filename", arg); | |
c6e8c800 | 104 | if (check_filename(prefix, arg)) |
e23d0b4a | 105 | return; |
009fee47 | 106 | die_verify_filename(prefix, arg); |
e23d0b4a LT |
107 | } |
108 | ||
ea92f41f JH |
109 | /* |
110 | * Opposite of the above: the command line did not have -- marker | |
111 | * and we parsed the arg as a refname. It should not be interpretable | |
112 | * as a filename. | |
113 | */ | |
114 | void verify_non_filename(const char *prefix, const char *arg) | |
115 | { | |
7ae3df8c | 116 | if (!is_inside_work_tree() || is_inside_git_dir()) |
68025633 | 117 | return; |
ea92f41f JH |
118 | if (*arg == '-') |
119 | return; /* flag */ | |
c6e8c800 JH |
120 | if (!check_filename(prefix, arg)) |
121 | return; | |
122 | die("ambiguous argument '%s': both revision and filename\n" | |
123 | "Use '--' to separate filenames from revisions", arg); | |
ea92f41f JH |
124 | } |
125 | ||
6b5ee137 | 126 | const char **get_pathspec(const char *prefix, const char **pathspec) |
d288a700 | 127 | { |
6b5ee137 | 128 | const char *entry = *pathspec; |
d089ebaa | 129 | const char **src, **dst; |
d288a700 LT |
130 | int prefixlen; |
131 | ||
f332726e LT |
132 | if (!prefix && !entry) |
133 | return NULL; | |
d288a700 LT |
134 | |
135 | if (!entry) { | |
136 | static const char *spec[2]; | |
137 | spec[0] = prefix; | |
138 | spec[1] = NULL; | |
139 | return spec; | |
140 | } | |
141 | ||
142 | /* Otherwise we have to re-write the entries.. */ | |
d089ebaa JH |
143 | src = pathspec; |
144 | dst = pathspec; | |
f332726e | 145 | prefixlen = prefix ? strlen(prefix) : 0; |
d089ebaa JH |
146 | while (*src) { |
147 | const char *p = prefix_path(prefix, prefixlen, *src); | |
62525ef7 | 148 | *(dst++) = p; |
d089ebaa JH |
149 | src++; |
150 | } | |
151 | *dst = NULL; | |
152 | if (!*pathspec) | |
153 | return NULL; | |
154 | return pathspec; | |
d288a700 LT |
155 | } |
156 | ||
5f5608bc | 157 | /* |
ad1a382f | 158 | * Test if it looks like we're at a git directory. |
5e7bfe25 | 159 | * We want to see: |
5f5608bc | 160 | * |
790296fd | 161 | * - either an objects/ directory _or_ the proper |
5f5608bc | 162 | * GIT_OBJECT_DIRECTORY environment variable |
ad1a382f | 163 | * - a refs/ directory |
8098a178 | 164 | * - either a HEAD symlink or a HEAD file that is formatted as |
c847f537 JH |
165 | * a proper "ref:", or a regular file HEAD that has a properly |
166 | * formatted sha1 object name. | |
5f5608bc | 167 | */ |
ad1a382f | 168 | static int is_git_directory(const char *suspect) |
5f5608bc | 169 | { |
ad1a382f SP |
170 | char path[PATH_MAX]; |
171 | size_t len = strlen(suspect); | |
172 | ||
3c9d0414 GB |
173 | if (PATH_MAX <= len + strlen("/objects")) |
174 | die("Too long path: %.*s", 60, suspect); | |
ad1a382f SP |
175 | strcpy(path, suspect); |
176 | if (getenv(DB_ENVIRONMENT)) { | |
177 | if (access(getenv(DB_ENVIRONMENT), X_OK)) | |
178 | return 0; | |
179 | } | |
180 | else { | |
181 | strcpy(path + len, "/objects"); | |
182 | if (access(path, X_OK)) | |
183 | return 0; | |
184 | } | |
185 | ||
186 | strcpy(path + len, "/refs"); | |
187 | if (access(path, X_OK)) | |
8098a178 | 188 | return 0; |
ad1a382f SP |
189 | |
190 | strcpy(path + len, "/HEAD"); | |
c847f537 | 191 | if (validate_headref(path)) |
ad1a382f SP |
192 | return 0; |
193 | ||
8098a178 | 194 | return 1; |
5f5608bc LT |
195 | } |
196 | ||
68025633 JS |
197 | int is_inside_git_dir(void) |
198 | { | |
e90fdc39 JS |
199 | if (inside_git_dir < 0) |
200 | inside_git_dir = is_inside_dir(get_git_dir()); | |
201 | return inside_git_dir; | |
892c41b9 ML |
202 | } |
203 | ||
892c41b9 ML |
204 | int is_inside_work_tree(void) |
205 | { | |
e90fdc39 JS |
206 | if (inside_work_tree < 0) |
207 | inside_work_tree = is_inside_dir(get_git_work_tree()); | |
208 | return inside_work_tree; | |
892c41b9 ML |
209 | } |
210 | ||
e90fdc39 | 211 | /* |
9517e6b8 | 212 | * set_work_tree() is only ever called if you set GIT_DIR explicitly. |
7efeb8f0 JS |
213 | * The old behaviour (which we retain here) is to set the work tree root |
214 | * to the cwd, unless overridden by the config, the command line, or | |
215 | * GIT_WORK_TREE. | |
e90fdc39 | 216 | */ |
7efeb8f0 | 217 | static const char *set_work_tree(const char *dir) |
892c41b9 | 218 | { |
7efeb8f0 | 219 | char buffer[PATH_MAX + 1]; |
e90fdc39 | 220 | |
7efeb8f0 JS |
221 | if (!getcwd(buffer, sizeof(buffer))) |
222 | die ("Could not get the current working directory"); | |
223 | git_work_tree_cfg = xstrdup(buffer); | |
e90fdc39 JS |
224 | inside_work_tree = 1; |
225 | ||
7efeb8f0 | 226 | return NULL; |
68025633 JS |
227 | } |
228 | ||
f3fa1838 JH |
229 | void setup_work_tree(void) |
230 | { | |
354e6534 JS |
231 | const char *work_tree, *git_dir; |
232 | static int initialized = 0; | |
233 | ||
234 | if (initialized) | |
235 | return; | |
236 | work_tree = get_git_work_tree(); | |
237 | git_dir = get_git_dir(); | |
59f0f2f3 | 238 | if (!is_absolute_path(git_dir)) |
044bbbcb | 239 | git_dir = make_absolute_path(git_dir); |
59f0f2f3 MH |
240 | if (!work_tree || chdir(work_tree)) |
241 | die("This operation must be run in a work tree"); | |
044bbbcb | 242 | set_git_dir(make_relative_path(git_dir, work_tree)); |
354e6534 | 243 | initialized = 1; |
59f0f2f3 MH |
244 | } |
245 | ||
337e51ce | 246 | static int check_repository_format_gently(const char *gitdir, int *nongit_ok) |
9459aa77 | 247 | { |
337e51ce NTND |
248 | char repo_config[PATH_MAX+1]; |
249 | ||
250 | /* | |
251 | * git_config() can't be used here because it calls git_pathdup() | |
252 | * to get $GIT_CONFIG/config. That call will make setup_git_env() | |
253 | * set git_dir to ".git". | |
254 | * | |
255 | * We are in gitdir setup, no git dir has been found useable yet. | |
256 | * Use a gentler version of git_config() to check if this repo | |
257 | * is a good one. | |
258 | */ | |
259 | snprintf(repo_config, PATH_MAX, "%s/config", gitdir); | |
260 | git_config_early(check_repository_format_version, NULL, repo_config); | |
9459aa77 NTND |
261 | if (GIT_REPO_VERSION < repository_format_version) { |
262 | if (!nongit_ok) | |
263 | die ("Expected git repo version <= %d, found %d", | |
264 | GIT_REPO_VERSION, repository_format_version); | |
265 | warning("Expected git repo version <= %d, found %d", | |
266 | GIT_REPO_VERSION, repository_format_version); | |
267 | warning("Please upgrade Git"); | |
268 | *nongit_ok = -1; | |
269 | return -1; | |
270 | } | |
271 | return 0; | |
272 | } | |
273 | ||
b44ebb19 LH |
274 | /* |
275 | * Try to read the location of the git directory from the .git file, | |
276 | * return path to git directory if found. | |
277 | */ | |
278 | const char *read_gitfile_gently(const char *path) | |
279 | { | |
280 | char *buf; | |
40c813e0 BK |
281 | char *dir; |
282 | const char *slash; | |
b44ebb19 LH |
283 | struct stat st; |
284 | int fd; | |
285 | size_t len; | |
286 | ||
287 | if (stat(path, &st)) | |
288 | return NULL; | |
289 | if (!S_ISREG(st.st_mode)) | |
290 | return NULL; | |
291 | fd = open(path, O_RDONLY); | |
292 | if (fd < 0) | |
d824cbba | 293 | die_errno("Error opening '%s'", path); |
b44ebb19 LH |
294 | buf = xmalloc(st.st_size + 1); |
295 | len = read_in_full(fd, buf, st.st_size); | |
296 | close(fd); | |
297 | if (len != st.st_size) | |
298 | die("Error reading %s", path); | |
299 | buf[len] = '\0'; | |
300 | if (prefixcmp(buf, "gitdir: ")) | |
301 | die("Invalid gitfile format: %s", path); | |
302 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') | |
303 | len--; | |
304 | if (len < 9) | |
305 | die("No path in gitfile: %s", path); | |
306 | buf[len] = '\0'; | |
40c813e0 BK |
307 | dir = buf + 8; |
308 | ||
309 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
310 | size_t pathlen = slash+1 - path; | |
311 | size_t dirlen = pathlen + len - 8; | |
312 | dir = xmalloc(dirlen + 1); | |
313 | strncpy(dir, path, pathlen); | |
314 | strncpy(dir + pathlen, buf + 8, len - 8); | |
315 | dir[dirlen] = '\0'; | |
316 | free(buf); | |
317 | buf = dir; | |
318 | } | |
319 | ||
320 | if (!is_git_directory(dir)) | |
321 | die("Not a git repository: %s", dir); | |
322 | path = make_absolute_path(dir); | |
323 | ||
b44ebb19 LH |
324 | free(buf); |
325 | return path; | |
326 | } | |
327 | ||
e4e30347 JN |
328 | static const char *setup_explicit_git_dir(const char *gitdirenv, |
329 | const char *work_tree_env, int *nongit_ok) | |
330 | { | |
331 | static char buffer[1024 + 1]; | |
332 | const char *retval; | |
333 | ||
e6aea2db NTND |
334 | if (startup_info) |
335 | startup_info->setup_explicit = 1; | |
e4e30347 JN |
336 | if (PATH_MAX - 40 < strlen(gitdirenv)) |
337 | die("'$%s' too big", GIT_DIR_ENVIRONMENT); | |
338 | if (!is_git_directory(gitdirenv)) { | |
339 | if (nongit_ok) { | |
340 | *nongit_ok = 1; | |
341 | return NULL; | |
342 | } | |
343 | die("Not a git repository: '%s'", gitdirenv); | |
344 | } | |
345 | if (!work_tree_env) { | |
346 | retval = set_work_tree(gitdirenv); | |
347 | /* config may override worktree */ | |
337e51ce | 348 | if (check_repository_format_gently(gitdirenv, nongit_ok)) |
e4e30347 JN |
349 | return NULL; |
350 | return retval; | |
351 | } | |
337e51ce | 352 | if (check_repository_format_gently(gitdirenv, nongit_ok)) |
e4e30347 JN |
353 | return NULL; |
354 | retval = get_relative_cwd(buffer, sizeof(buffer) - 1, | |
355 | get_git_work_tree()); | |
356 | if (!retval || !*retval) | |
357 | return NULL; | |
358 | set_git_dir(make_absolute_path(gitdirenv)); | |
359 | if (chdir(work_tree_env) < 0) | |
360 | die_errno ("Could not chdir to '%s'", work_tree_env); | |
361 | strcat(buffer, "/"); | |
362 | return retval; | |
363 | } | |
364 | ||
9951d3b3 NTND |
365 | static const char *setup_discovered_git_dir(const char *gitdir, |
366 | char *cwd, int offset, int len, | |
367 | int *nongit_ok) | |
93a00542 | 368 | { |
9951d3b3 NTND |
369 | if (check_repository_format_gently(gitdir, nongit_ok)) |
370 | return NULL; | |
93a00542 | 371 | |
9951d3b3 NTND |
372 | /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ |
373 | if (is_bare_repository_cfg > 0) { | |
374 | set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir)); | |
375 | if (chdir(cwd)) | |
376 | die_errno("Could not come back to cwd"); | |
377 | return NULL; | |
378 | } | |
98937bef | 379 | |
9951d3b3 NTND |
380 | /* #0, #1, #5, #8, #9, #12, #13 */ |
381 | set_git_work_tree("."); | |
382 | if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) | |
383 | set_git_dir(gitdir); | |
98937bef | 384 | inside_git_dir = 0; |
9951d3b3 | 385 | inside_work_tree = 1; |
98937bef NTND |
386 | if (offset == len) |
387 | return NULL; | |
388 | ||
389 | /* Make "offset" point to past the '/', and add a '/' at the end */ | |
390 | offset++; | |
391 | cwd[len++] = '/'; | |
392 | cwd[len] = 0; | |
393 | return cwd + offset; | |
394 | } | |
395 | ||
1cd8031b NTND |
396 | /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */ |
397 | static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok) | |
68698da5 JN |
398 | { |
399 | int root_len; | |
400 | ||
1cd8031b NTND |
401 | if (check_repository_format_gently(".", nongit_ok)) |
402 | return NULL; | |
403 | ||
68698da5 | 404 | inside_git_dir = 1; |
1cd8031b | 405 | inside_work_tree = 0; |
68698da5 | 406 | if (offset != len) { |
8fc0ae80 NTND |
407 | if (chdir(cwd)) |
408 | die_errno("Cannot come back to cwd"); | |
68698da5 JN |
409 | root_len = offset_1st_component(cwd); |
410 | cwd[offset > root_len ? offset : root_len] = '\0'; | |
411 | set_git_dir(cwd); | |
337e51ce | 412 | } |
1cd8031b | 413 | else |
68698da5 | 414 | set_git_dir("."); |
68698da5 JN |
415 | return NULL; |
416 | } | |
417 | ||
f161edeb JN |
418 | static const char *setup_nongit(const char *cwd, int *nongit_ok) |
419 | { | |
420 | if (!nongit_ok) | |
421 | die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); | |
422 | if (chdir(cwd)) | |
423 | die_errno("Cannot come back to cwd"); | |
424 | *nongit_ok = 1; | |
425 | return NULL; | |
426 | } | |
427 | ||
60c98d1e JN |
428 | static dev_t get_device_or_die(const char *path, const char *prefix) |
429 | { | |
430 | struct stat buf; | |
431 | if (stat(path, &buf)) | |
432 | die_errno("failed to stat '%s%s%s'", | |
433 | prefix ? prefix : "", | |
434 | prefix ? "/" : "", path); | |
435 | return buf.st_dev; | |
436 | } | |
437 | ||
e90fdc39 JS |
438 | /* |
439 | * We cannot decide in this function whether we are in the work tree or | |
440 | * not, since the config can only be read _after_ this function was called. | |
441 | */ | |
a60645f9 | 442 | static const char *setup_git_directory_gently_1(int *nongit_ok) |
d288a700 | 443 | { |
e90fdc39 | 444 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
0454dd93 | 445 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
d288a700 | 446 | static char cwd[PATH_MAX+1]; |
9951d3b3 NTND |
447 | const char *gitdirenv, *ret; |
448 | char *gitfile; | |
98937bef | 449 | int len, offset, ceil_offset; |
c7d1d1b1 RH |
450 | dev_t current_device = 0; |
451 | int one_filesystem = 1; | |
d288a700 | 452 | |
af05d679 SG |
453 | /* |
454 | * Let's assume that we are in a git repository. | |
455 | * If it turns out later that we are somewhere else, the value will be | |
456 | * updated accordingly. | |
457 | */ | |
458 | if (nongit_ok) | |
459 | *nongit_ok = 0; | |
460 | ||
e90fdc39 JS |
461 | /* |
462 | * If GIT_DIR is set explicitly, we're not going | |
463 | * to do any discovery, but we still do repository | |
464 | * validation. | |
465 | */ | |
ad1a382f | 466 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
e4e30347 JN |
467 | if (gitdirenv) |
468 | return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok); | |
d288a700 | 469 | |
f66a4d68 | 470 | if (!getcwd(cwd, sizeof(cwd)-1)) |
0721c314 | 471 | die_errno("Unable to read current working directory"); |
d288a700 | 472 | |
0454dd93 | 473 | ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); |
17d778e7 JH |
474 | if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) |
475 | ceil_offset = 1; | |
d288a700 | 476 | |
892c41b9 | 477 | /* |
e90fdc39 | 478 | * Test in the following order (relative to the cwd): |
b44ebb19 | 479 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
480 | * - .git/ |
481 | * - ./ (bare) | |
b44ebb19 | 482 | * - ../.git |
e90fdc39 JS |
483 | * - ../.git/ |
484 | * - ../ (bare) | |
485 | * - ../../.git/ | |
486 | * etc. | |
892c41b9 | 487 | */ |
e90fdc39 | 488 | offset = len = strlen(cwd); |
cf87463e | 489 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
60c98d1e JN |
490 | if (one_filesystem) |
491 | current_device = get_device_or_die(".", NULL); | |
e90fdc39 | 492 | for (;;) { |
9951d3b3 NTND |
493 | gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); |
494 | if (gitfile) | |
495 | gitdirenv = gitfile = xstrdup(gitfile); | |
496 | else { | |
497 | if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) | |
498 | gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; | |
499 | } | |
500 | ||
501 | if (gitdirenv) { | |
502 | ret = setup_discovered_git_dir(gitdirenv, | |
503 | cwd, offset, len, | |
504 | nongit_ok); | |
505 | free(gitfile); | |
506 | return ret; | |
507 | } | |
508 | free(gitfile); | |
509 | ||
68698da5 | 510 | if (is_git_directory(".")) |
1cd8031b NTND |
511 | return setup_bare_git_dir(cwd, offset, len, nongit_ok); |
512 | ||
0454dd93 | 513 | while (--offset > ceil_offset && cwd[offset] != '/'); |
f161edeb JN |
514 | if (offset <= ceil_offset) |
515 | return setup_nongit(cwd, nongit_ok); | |
8030e442 | 516 | if (one_filesystem) { |
60c98d1e JN |
517 | dev_t parent_device = get_device_or_die("..", cwd); |
518 | if (parent_device != current_device) { | |
8030e442 LD |
519 | if (nongit_ok) { |
520 | if (chdir(cwd)) | |
521 | die_errno("Cannot come back to cwd"); | |
522 | *nongit_ok = 1; | |
523 | return NULL; | |
524 | } | |
525 | cwd[offset] = '\0'; | |
526 | die("Not a git repository (or any parent up to mount parent %s)\n" | |
cf87463e | 527 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd); |
8030e442 LD |
528 | } |
529 | } | |
502ffe34 LD |
530 | if (chdir("..")) { |
531 | cwd[offset] = '\0'; | |
d824cbba | 532 | die_errno("Cannot change to '%s/..'", cwd); |
502ffe34 | 533 | } |
892c41b9 | 534 | } |
d288a700 | 535 | } |
5e7bfe25 | 536 | |
a60645f9 NTND |
537 | const char *setup_git_directory_gently(int *nongit_ok) |
538 | { | |
539 | const char *prefix; | |
540 | ||
541 | prefix = setup_git_directory_gently_1(nongit_ok); | |
542 | if (startup_info) | |
543 | startup_info->have_repository = !nongit_ok || !*nongit_ok; | |
544 | return prefix; | |
545 | } | |
546 | ||
94df2506 JH |
547 | int git_config_perm(const char *var, const char *value) |
548 | { | |
06cbe855 HO |
549 | int i; |
550 | char *endptr; | |
551 | ||
552 | if (value == NULL) | |
553 | return PERM_GROUP; | |
554 | ||
555 | if (!strcmp(value, "umask")) | |
556 | return PERM_UMASK; | |
557 | if (!strcmp(value, "group")) | |
558 | return PERM_GROUP; | |
559 | if (!strcmp(value, "all") || | |
560 | !strcmp(value, "world") || | |
561 | !strcmp(value, "everybody")) | |
562 | return PERM_EVERYBODY; | |
563 | ||
564 | /* Parse octal numbers */ | |
565 | i = strtol(value, &endptr, 8); | |
566 | ||
567 | /* If not an octal number, maybe true/false? */ | |
568 | if (*endptr != 0) | |
569 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
570 | ||
571 | /* | |
572 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 573 | * a chmod value to restrict to. |
06cbe855 HO |
574 | */ |
575 | switch (i) { | |
576 | case PERM_UMASK: /* 0 */ | |
577 | return PERM_UMASK; | |
578 | case OLD_PERM_GROUP: /* 1 */ | |
579 | return PERM_GROUP; | |
580 | case OLD_PERM_EVERYBODY: /* 2 */ | |
581 | return PERM_EVERYBODY; | |
94df2506 | 582 | } |
06cbe855 HO |
583 | |
584 | /* A filemode value was given: 0xxx */ | |
585 | ||
586 | if ((i & 0600) != 0600) | |
587 | die("Problem with core.sharedRepository filemode value " | |
588 | "(0%.3o).\nThe owner of files must always have " | |
589 | "read and write permissions.", i); | |
590 | ||
591 | /* | |
592 | * Mask filemode value. Others can not get write permission. | |
593 | * x flags for directories are handled separately. | |
594 | */ | |
5a688fe4 | 595 | return -(i & 0666); |
94df2506 JH |
596 | } |
597 | ||
ef90d6d4 | 598 | int check_repository_format_version(const char *var, const char *value, void *cb) |
ab9cb76f | 599 | { |
299726d5 JS |
600 | if (strcmp(var, "core.repositoryformatversion") == 0) |
601 | repository_format_version = git_config_int(var, value); | |
457f06d6 | 602 | else if (strcmp(var, "core.sharedrepository") == 0) |
94df2506 | 603 | shared_repository = git_config_perm(var, value); |
e90fdc39 JS |
604 | else if (strcmp(var, "core.bare") == 0) { |
605 | is_bare_repository_cfg = git_config_bool(var, value); | |
606 | if (is_bare_repository_cfg == 1) | |
607 | inside_work_tree = -1; | |
608 | } else if (strcmp(var, "core.worktree") == 0) { | |
180483c5 JH |
609 | if (!value) |
610 | return config_error_nonbool(var); | |
8e0f7003 | 611 | free(git_work_tree_cfg); |
e90fdc39 JS |
612 | git_work_tree_cfg = xstrdup(value); |
613 | inside_work_tree = -1; | |
614 | } | |
299726d5 | 615 | return 0; |
ab9cb76f JH |
616 | } |
617 | ||
618 | int check_repository_format(void) | |
619 | { | |
337e51ce | 620 | return check_repository_format_gently(get_git_dir(), NULL); |
ab9cb76f JH |
621 | } |
622 | ||
e1e5ec86 CB |
623 | /* |
624 | * Returns the "prefix", a path to the current working directory | |
625 | * relative to the work tree root, or NULL, if the current working | |
626 | * directory is not a strict subdirectory of the work tree root. The | |
627 | * prefix always ends with a '/' character. | |
628 | */ | |
5e7bfe25 JH |
629 | const char *setup_git_directory(void) |
630 | { | |
4ca06608 | 631 | const char *retval = setup_git_directory_gently(NULL); |
e90fdc39 JS |
632 | |
633 | /* If the work tree is not the default one, recompute prefix */ | |
e6aea2db NTND |
634 | if ((!startup_info || startup_info->setup_explicit) && |
635 | inside_work_tree < 0) { | |
e90fdc39 JS |
636 | static char buffer[PATH_MAX + 1]; |
637 | char *rel; | |
638 | if (retval && chdir(retval)) | |
0721c314 | 639 | die_errno ("Could not jump back into original cwd"); |
e90fdc39 | 640 | rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); |
bb528633 | 641 | if (rel && *rel && chdir(get_git_work_tree())) |
0721c314 | 642 | die_errno ("Could not jump to working directory"); |
e90fdc39 JS |
643 | return rel && *rel ? strcat(rel, "/") : NULL; |
644 | } | |
645 | ||
5e7bfe25 JH |
646 | return retval; |
647 | } |