]>
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 | ||
173 | strcpy(path, suspect); | |
174 | if (getenv(DB_ENVIRONMENT)) { | |
175 | if (access(getenv(DB_ENVIRONMENT), X_OK)) | |
176 | return 0; | |
177 | } | |
178 | else { | |
179 | strcpy(path + len, "/objects"); | |
180 | if (access(path, X_OK)) | |
181 | return 0; | |
182 | } | |
183 | ||
184 | strcpy(path + len, "/refs"); | |
185 | if (access(path, X_OK)) | |
8098a178 | 186 | return 0; |
ad1a382f SP |
187 | |
188 | strcpy(path + len, "/HEAD"); | |
c847f537 | 189 | if (validate_headref(path)) |
ad1a382f SP |
190 | return 0; |
191 | ||
8098a178 | 192 | return 1; |
5f5608bc LT |
193 | } |
194 | ||
68025633 JS |
195 | int is_inside_git_dir(void) |
196 | { | |
e90fdc39 JS |
197 | if (inside_git_dir < 0) |
198 | inside_git_dir = is_inside_dir(get_git_dir()); | |
199 | return inside_git_dir; | |
892c41b9 ML |
200 | } |
201 | ||
892c41b9 ML |
202 | int is_inside_work_tree(void) |
203 | { | |
e90fdc39 JS |
204 | if (inside_work_tree < 0) |
205 | inside_work_tree = is_inside_dir(get_git_work_tree()); | |
206 | return inside_work_tree; | |
892c41b9 ML |
207 | } |
208 | ||
e90fdc39 | 209 | /* |
9517e6b8 | 210 | * set_work_tree() is only ever called if you set GIT_DIR explicitly. |
7efeb8f0 JS |
211 | * The old behaviour (which we retain here) is to set the work tree root |
212 | * to the cwd, unless overridden by the config, the command line, or | |
213 | * GIT_WORK_TREE. | |
e90fdc39 | 214 | */ |
7efeb8f0 | 215 | static const char *set_work_tree(const char *dir) |
892c41b9 | 216 | { |
7efeb8f0 | 217 | char buffer[PATH_MAX + 1]; |
e90fdc39 | 218 | |
7efeb8f0 JS |
219 | if (!getcwd(buffer, sizeof(buffer))) |
220 | die ("Could not get the current working directory"); | |
221 | git_work_tree_cfg = xstrdup(buffer); | |
e90fdc39 JS |
222 | inside_work_tree = 1; |
223 | ||
7efeb8f0 | 224 | return NULL; |
68025633 JS |
225 | } |
226 | ||
f3fa1838 JH |
227 | void setup_work_tree(void) |
228 | { | |
354e6534 JS |
229 | const char *work_tree, *git_dir; |
230 | static int initialized = 0; | |
231 | ||
232 | if (initialized) | |
233 | return; | |
234 | work_tree = get_git_work_tree(); | |
235 | git_dir = get_git_dir(); | |
59f0f2f3 | 236 | if (!is_absolute_path(git_dir)) |
044bbbcb | 237 | git_dir = make_absolute_path(git_dir); |
59f0f2f3 MH |
238 | if (!work_tree || chdir(work_tree)) |
239 | die("This operation must be run in a work tree"); | |
044bbbcb | 240 | set_git_dir(make_relative_path(git_dir, work_tree)); |
354e6534 | 241 | initialized = 1; |
59f0f2f3 MH |
242 | } |
243 | ||
9459aa77 NTND |
244 | static int check_repository_format_gently(int *nongit_ok) |
245 | { | |
ef90d6d4 | 246 | git_config(check_repository_format_version, NULL); |
9459aa77 NTND |
247 | if (GIT_REPO_VERSION < repository_format_version) { |
248 | if (!nongit_ok) | |
249 | die ("Expected git repo version <= %d, found %d", | |
250 | GIT_REPO_VERSION, repository_format_version); | |
251 | warning("Expected git repo version <= %d, found %d", | |
252 | GIT_REPO_VERSION, repository_format_version); | |
253 | warning("Please upgrade Git"); | |
254 | *nongit_ok = -1; | |
255 | return -1; | |
256 | } | |
257 | return 0; | |
258 | } | |
259 | ||
b44ebb19 LH |
260 | /* |
261 | * Try to read the location of the git directory from the .git file, | |
262 | * return path to git directory if found. | |
263 | */ | |
264 | const char *read_gitfile_gently(const char *path) | |
265 | { | |
266 | char *buf; | |
40c813e0 BK |
267 | char *dir; |
268 | const char *slash; | |
b44ebb19 LH |
269 | struct stat st; |
270 | int fd; | |
271 | size_t len; | |
272 | ||
273 | if (stat(path, &st)) | |
274 | return NULL; | |
275 | if (!S_ISREG(st.st_mode)) | |
276 | return NULL; | |
277 | fd = open(path, O_RDONLY); | |
278 | if (fd < 0) | |
d824cbba | 279 | die_errno("Error opening '%s'", path); |
b44ebb19 LH |
280 | buf = xmalloc(st.st_size + 1); |
281 | len = read_in_full(fd, buf, st.st_size); | |
282 | close(fd); | |
283 | if (len != st.st_size) | |
284 | die("Error reading %s", path); | |
285 | buf[len] = '\0'; | |
286 | if (prefixcmp(buf, "gitdir: ")) | |
287 | die("Invalid gitfile format: %s", path); | |
288 | while (buf[len - 1] == '\n' || buf[len - 1] == '\r') | |
289 | len--; | |
290 | if (len < 9) | |
291 | die("No path in gitfile: %s", path); | |
292 | buf[len] = '\0'; | |
40c813e0 BK |
293 | dir = buf + 8; |
294 | ||
295 | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
296 | size_t pathlen = slash+1 - path; | |
297 | size_t dirlen = pathlen + len - 8; | |
298 | dir = xmalloc(dirlen + 1); | |
299 | strncpy(dir, path, pathlen); | |
300 | strncpy(dir + pathlen, buf + 8, len - 8); | |
301 | dir[dirlen] = '\0'; | |
302 | free(buf); | |
303 | buf = dir; | |
304 | } | |
305 | ||
306 | if (!is_git_directory(dir)) | |
307 | die("Not a git repository: %s", dir); | |
308 | path = make_absolute_path(dir); | |
309 | ||
b44ebb19 LH |
310 | free(buf); |
311 | return path; | |
312 | } | |
313 | ||
e90fdc39 JS |
314 | /* |
315 | * We cannot decide in this function whether we are in the work tree or | |
316 | * not, since the config can only be read _after_ this function was called. | |
317 | */ | |
4ca06608 | 318 | const char *setup_git_directory_gently(int *nongit_ok) |
d288a700 | 319 | { |
e90fdc39 | 320 | const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); |
0454dd93 | 321 | const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); |
d288a700 | 322 | static char cwd[PATH_MAX+1]; |
e90fdc39 | 323 | const char *gitdirenv; |
b44ebb19 | 324 | const char *gitfile_dir; |
72ec8ba6 | 325 | int len, offset, ceil_offset, root_len; |
e6405517 | 326 | int current_device = 0, one_filesystem = 1; |
8030e442 | 327 | struct stat buf; |
d288a700 | 328 | |
af05d679 SG |
329 | /* |
330 | * Let's assume that we are in a git repository. | |
331 | * If it turns out later that we are somewhere else, the value will be | |
332 | * updated accordingly. | |
333 | */ | |
334 | if (nongit_ok) | |
335 | *nongit_ok = 0; | |
336 | ||
e90fdc39 JS |
337 | /* |
338 | * If GIT_DIR is set explicitly, we're not going | |
339 | * to do any discovery, but we still do repository | |
340 | * validation. | |
341 | */ | |
ad1a382f | 342 | gitdirenv = getenv(GIT_DIR_ENVIRONMENT); |
e90fdc39 JS |
343 | if (gitdirenv) { |
344 | if (PATH_MAX - 40 < strlen(gitdirenv)) | |
345 | die("'$%s' too big", GIT_DIR_ENVIRONMENT); | |
346 | if (is_git_directory(gitdirenv)) { | |
dd5c8af1 JS |
347 | static char buffer[1024 + 1]; |
348 | const char *retval; | |
349 | ||
138dd1e9 NTND |
350 | if (!work_tree_env) { |
351 | retval = set_work_tree(gitdirenv); | |
9459aa77 NTND |
352 | /* config may override worktree */ |
353 | if (check_repository_format_gently(nongit_ok)) | |
354 | return NULL; | |
138dd1e9 NTND |
355 | return retval; |
356 | } | |
9459aa77 NTND |
357 | if (check_repository_format_gently(nongit_ok)) |
358 | return NULL; | |
dd5c8af1 JS |
359 | retval = get_relative_cwd(buffer, sizeof(buffer) - 1, |
360 | get_git_work_tree()); | |
361 | if (!retval || !*retval) | |
362 | return NULL; | |
363 | set_git_dir(make_absolute_path(gitdirenv)); | |
364 | if (chdir(work_tree_env) < 0) | |
0721c314 | 365 | die_errno ("Could not chdir to '%s'", work_tree_env); |
dd5c8af1 JS |
366 | strcat(buffer, "/"); |
367 | return retval; | |
892c41b9 | 368 | } |
3a3c3fc4 | 369 | if (nongit_ok) { |
41e95f69 ML |
370 | *nongit_ok = 1; |
371 | return NULL; | |
372 | } | |
ad1a382f | 373 | die("Not a git repository: '%s'", gitdirenv); |
5e7bfe25 | 374 | } |
d288a700 | 375 | |
f66a4d68 | 376 | if (!getcwd(cwd, sizeof(cwd)-1)) |
0721c314 | 377 | die_errno("Unable to read current working directory"); |
d288a700 | 378 | |
0454dd93 | 379 | ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs); |
17d778e7 JH |
380 | if (ceil_offset < 0 && has_dos_drive_prefix(cwd)) |
381 | ceil_offset = 1; | |
d288a700 | 382 | |
892c41b9 | 383 | /* |
e90fdc39 | 384 | * Test in the following order (relative to the cwd): |
b44ebb19 | 385 | * - .git (file containing "gitdir: <path>") |
e90fdc39 JS |
386 | * - .git/ |
387 | * - ./ (bare) | |
b44ebb19 | 388 | * - ../.git |
e90fdc39 JS |
389 | * - ../.git/ |
390 | * - ../ (bare) | |
391 | * - ../../.git/ | |
392 | * etc. | |
892c41b9 | 393 | */ |
e90fdc39 | 394 | offset = len = strlen(cwd); |
cf87463e | 395 | one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0); |
8030e442 LD |
396 | if (one_filesystem) { |
397 | if (stat(".", &buf)) | |
398 | die_errno("failed to stat '.'"); | |
399 | current_device = buf.st_dev; | |
400 | } | |
e90fdc39 | 401 | for (;;) { |
b44ebb19 LH |
402 | gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); |
403 | if (gitfile_dir) { | |
404 | if (set_git_dir(gitfile_dir)) | |
405 | die("Repository setup failed"); | |
406 | break; | |
407 | } | |
e90fdc39 JS |
408 | if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) |
409 | break; | |
410 | if (is_git_directory(".")) { | |
411 | inside_git_dir = 1; | |
412 | if (!work_tree_env) | |
413 | inside_work_tree = 0; | |
72183cb2 | 414 | if (offset != len) { |
72ec8ba6 NTND |
415 | root_len = offset_1st_component(cwd); |
416 | cwd[offset > root_len ? offset : root_len] = '\0'; | |
717c3972 | 417 | set_git_dir(cwd); |
72183cb2 | 418 | } else |
717c3972 | 419 | set_git_dir("."); |
9459aa77 | 420 | check_repository_format_gently(nongit_ok); |
892c41b9 ML |
421 | return NULL; |
422 | } | |
0454dd93 DR |
423 | while (--offset > ceil_offset && cwd[offset] != '/'); |
424 | if (offset <= ceil_offset) { | |
425 | if (nongit_ok) { | |
426 | if (chdir(cwd)) | |
0721c314 | 427 | die_errno("Cannot come back to cwd"); |
0454dd93 DR |
428 | *nongit_ok = 1; |
429 | return NULL; | |
e90fdc39 | 430 | } |
f66bc5f9 | 431 | die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT); |
0454dd93 | 432 | } |
8030e442 LD |
433 | if (one_filesystem) { |
434 | if (stat("..", &buf)) { | |
435 | cwd[offset] = '\0'; | |
436 | die_errno("failed to stat '%s/..'", cwd); | |
437 | } | |
438 | if (buf.st_dev != current_device) { | |
439 | if (nongit_ok) { | |
440 | if (chdir(cwd)) | |
441 | die_errno("Cannot come back to cwd"); | |
442 | *nongit_ok = 1; | |
443 | return NULL; | |
444 | } | |
445 | cwd[offset] = '\0'; | |
446 | die("Not a git repository (or any parent up to mount parent %s)\n" | |
cf87463e | 447 | "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).", cwd); |
8030e442 LD |
448 | } |
449 | } | |
502ffe34 LD |
450 | if (chdir("..")) { |
451 | cwd[offset] = '\0'; | |
d824cbba | 452 | die_errno("Cannot change to '%s/..'", cwd); |
502ffe34 | 453 | } |
892c41b9 ML |
454 | } |
455 | ||
e90fdc39 JS |
456 | inside_git_dir = 0; |
457 | if (!work_tree_env) | |
458 | inside_work_tree = 1; | |
72ec8ba6 NTND |
459 | root_len = offset_1st_component(cwd); |
460 | git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len); | |
9459aa77 NTND |
461 | if (check_repository_format_gently(nongit_ok)) |
462 | return NULL; | |
e90fdc39 | 463 | if (offset == len) |
d288a700 LT |
464 | return NULL; |
465 | ||
e90fdc39 JS |
466 | /* Make "offset" point to past the '/', and add a '/' at the end */ |
467 | offset++; | |
468 | cwd[len++] = '/'; | |
469 | cwd[len] = 0; | |
470 | return cwd + offset; | |
d288a700 | 471 | } |
5e7bfe25 | 472 | |
94df2506 JH |
473 | int git_config_perm(const char *var, const char *value) |
474 | { | |
06cbe855 HO |
475 | int i; |
476 | char *endptr; | |
477 | ||
478 | if (value == NULL) | |
479 | return PERM_GROUP; | |
480 | ||
481 | if (!strcmp(value, "umask")) | |
482 | return PERM_UMASK; | |
483 | if (!strcmp(value, "group")) | |
484 | return PERM_GROUP; | |
485 | if (!strcmp(value, "all") || | |
486 | !strcmp(value, "world") || | |
487 | !strcmp(value, "everybody")) | |
488 | return PERM_EVERYBODY; | |
489 | ||
490 | /* Parse octal numbers */ | |
491 | i = strtol(value, &endptr, 8); | |
492 | ||
493 | /* If not an octal number, maybe true/false? */ | |
494 | if (*endptr != 0) | |
495 | return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK; | |
496 | ||
497 | /* | |
498 | * Treat values 0, 1 and 2 as compatibility cases, otherwise it is | |
5a688fe4 | 499 | * a chmod value to restrict to. |
06cbe855 HO |
500 | */ |
501 | switch (i) { | |
502 | case PERM_UMASK: /* 0 */ | |
503 | return PERM_UMASK; | |
504 | case OLD_PERM_GROUP: /* 1 */ | |
505 | return PERM_GROUP; | |
506 | case OLD_PERM_EVERYBODY: /* 2 */ | |
507 | return PERM_EVERYBODY; | |
94df2506 | 508 | } |
06cbe855 HO |
509 | |
510 | /* A filemode value was given: 0xxx */ | |
511 | ||
512 | if ((i & 0600) != 0600) | |
513 | die("Problem with core.sharedRepository filemode value " | |
514 | "(0%.3o).\nThe owner of files must always have " | |
515 | "read and write permissions.", i); | |
516 | ||
517 | /* | |
518 | * Mask filemode value. Others can not get write permission. | |
519 | * x flags for directories are handled separately. | |
520 | */ | |
5a688fe4 | 521 | return -(i & 0666); |
94df2506 JH |
522 | } |
523 | ||
ef90d6d4 | 524 | int check_repository_format_version(const char *var, const char *value, void *cb) |
ab9cb76f | 525 | { |
299726d5 JS |
526 | if (strcmp(var, "core.repositoryformatversion") == 0) |
527 | repository_format_version = git_config_int(var, value); | |
457f06d6 | 528 | else if (strcmp(var, "core.sharedrepository") == 0) |
94df2506 | 529 | shared_repository = git_config_perm(var, value); |
e90fdc39 JS |
530 | else if (strcmp(var, "core.bare") == 0) { |
531 | is_bare_repository_cfg = git_config_bool(var, value); | |
532 | if (is_bare_repository_cfg == 1) | |
533 | inside_work_tree = -1; | |
534 | } else if (strcmp(var, "core.worktree") == 0) { | |
180483c5 JH |
535 | if (!value) |
536 | return config_error_nonbool(var); | |
8e0f7003 | 537 | free(git_work_tree_cfg); |
e90fdc39 JS |
538 | git_work_tree_cfg = xstrdup(value); |
539 | inside_work_tree = -1; | |
540 | } | |
299726d5 | 541 | return 0; |
ab9cb76f JH |
542 | } |
543 | ||
544 | int check_repository_format(void) | |
545 | { | |
9459aa77 | 546 | return check_repository_format_gently(NULL); |
ab9cb76f JH |
547 | } |
548 | ||
5e7bfe25 JH |
549 | const char *setup_git_directory(void) |
550 | { | |
4ca06608 | 551 | const char *retval = setup_git_directory_gently(NULL); |
e90fdc39 JS |
552 | |
553 | /* If the work tree is not the default one, recompute prefix */ | |
554 | if (inside_work_tree < 0) { | |
555 | static char buffer[PATH_MAX + 1]; | |
556 | char *rel; | |
557 | if (retval && chdir(retval)) | |
0721c314 | 558 | die_errno ("Could not jump back into original cwd"); |
e90fdc39 | 559 | rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree()); |
bb528633 | 560 | if (rel && *rel && chdir(get_git_work_tree())) |
0721c314 | 561 | die_errno ("Could not jump to working directory"); |
e90fdc39 JS |
562 | return rel && *rel ? strcat(rel, "/") : NULL; |
563 | } | |
564 | ||
5e7bfe25 JH |
565 | return retval; |
566 | } |