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