setup.c: Prepare for Windows directory separators.
[git/git.git] / setup.c
CommitLineData
d288a700 1#include "cache.h"
e90fdc39
JS
2#include "dir.h"
3
4static int inside_git_dir = -1;
5static int inside_work_tree = -1;
d288a700 6
d089ebaa 7static int sanitary_path_copy(char *dst, const char *src)
f332726e 8{
d089ebaa
JH
9 char *dst0 = dst;
10
11 if (*src == '/') {
12 *dst++ = '/';
13 while (*src == '/')
14 src++;
15 }
16
f332726e 17 for (;;) {
d089ebaa
JH
18 char c = *src;
19
20 /*
21 * A path component that begins with . could be
22 * special:
23 * (1) "." and ends -- ignore and terminate.
24 * (2) "./" -- ignore them, eat slash and continue.
25 * (3) ".." and ends -- strip one and terminate.
26 * (4) "../" -- strip one, eat slash and continue.
27 */
28 if (c == '.') {
4cd148d8 29 if (!src[1]) {
d089ebaa
JH
30 /* (1) */
31 src++;
4cd148d8 32 } else if (src[1] == '/') {
d089ebaa
JH
33 /* (2) */
34 src += 2;
35 while (*src == '/')
36 src++;
37 continue;
4cd148d8
JS
38 } else if (src[1] == '.') {
39 if (!src[2]) {
d089ebaa
JH
40 /* (3) */
41 src += 2;
42 goto up_one;
4cd148d8 43 } else if (src[2] == '/') {
d089ebaa
JH
44 /* (4) */
45 src += 3;
46 while (*src == '/')
47 src++;
48 goto up_one;
49 }
50 }
f332726e 51 }
d089ebaa
JH
52
53 /* copy up to the next '/', and eat all '/' */
54 while ((c = *src++) != '\0' && c != '/')
55 *dst++ = c;
f332726e 56 if (c == '/') {
d089ebaa
JH
57 *dst++ = c;
58 while (c == '/')
59 c = *src++;
60 src--;
61 } else if (!c)
f332726e 62 break;
f332726e 63 continue;
d089ebaa
JH
64
65 up_one:
66 /*
67 * dst0..dst is prefix portion, and dst[-1] is '/';
68 * go up one level.
69 */
70 dst -= 2; /* go past trailing '/' if any */
71 if (dst < dst0)
72 return -1;
73 while (1) {
74 if (dst <= dst0)
75 break;
76 c = *dst--;
77 if (c == '/') {
78 dst += 2;
79 break;
80 }
81 }
f332726e 82 }
d089ebaa
JH
83 *dst = '\0';
84 return 0;
85}
a6080a0a 86
d089ebaa
JH
87const char *prefix_path(const char *prefix, int len, const char *path)
88{
89 const char *orig = path;
90 char *sanitized = xmalloc(len + strlen(path) + 1);
9a13ba1b 91 if (is_absolute_path(orig))
d089ebaa
JH
92 strcpy(sanitized, path);
93 else {
94 if (len)
95 memcpy(sanitized, prefix, len);
96 strcpy(sanitized + len, path);
f332726e 97 }
d089ebaa
JH
98 if (sanitary_path_copy(sanitized, sanitized))
99 goto error_out;
9a13ba1b 100 if (is_absolute_path(orig)) {
d089ebaa
JH
101 const char *work_tree = get_git_work_tree();
102 size_t len = strlen(work_tree);
103 size_t total = strlen(sanitized) + 1;
104 if (strncmp(sanitized, work_tree, len) ||
105 (sanitized[len] != '\0' && sanitized[len] != '/')) {
106 error_out:
107 error("'%s' is outside repository", orig);
108 free(sanitized);
109 return NULL;
110 }
111 if (sanitized[len] == '/')
112 len++;
113 memmove(sanitized, sanitized + len, total - len);
114 }
115 return sanitized;
f332726e
LT
116}
117
a6080a0a 118/*
4ca06608
JH
119 * Unlike prefix_path, this should be used if the named file does
120 * not have to interact with index entry; i.e. name of a random file
121 * on the filesystem.
122 */
123const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
124{
125 static char path[PATH_MAX];
ecf4831d 126 if (!pfx || !*pfx || is_absolute_path(arg))
4ca06608
JH
127 return arg;
128 memcpy(path, pfx, pfx_len);
129 strcpy(path + pfx_len, arg);
130 return path;
131}
132
e23d0b4a
LT
133/*
134 * Verify a filename that we got as an argument for a pathspec
135 * entry. Note that a filename that begins with "-" never verifies
136 * as true, because even if such a filename were to exist, we want
137 * it to be preceded by the "--" marker (or we want the user to
138 * use a format like "./-filename")
139 */
140void verify_filename(const char *prefix, const char *arg)
141{
142 const char *name;
143 struct stat st;
144
145 if (*arg == '-')
146 die("bad flag '%s' used after filename", arg);
147 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
148 if (!lstat(name, &st))
149 return;
150 if (errno == ENOENT)
ea92f41f
JH
151 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
152 "Use '--' to separate paths from revisions", arg);
e23d0b4a
LT
153 die("'%s': %s", arg, strerror(errno));
154}
155
ea92f41f
JH
156/*
157 * Opposite of the above: the command line did not have -- marker
158 * and we parsed the arg as a refname. It should not be interpretable
159 * as a filename.
160 */
161void verify_non_filename(const char *prefix, const char *arg)
162{
163 const char *name;
164 struct stat st;
165
7ae3df8c 166 if (!is_inside_work_tree() || is_inside_git_dir())
68025633 167 return;
ea92f41f
JH
168 if (*arg == '-')
169 return; /* flag */
170 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
171 if (!lstat(name, &st))
172 die("ambiguous argument '%s': both revision and filename\n"
173 "Use '--' to separate filenames from revisions", arg);
33a798c8 174 if (errno != ENOENT && errno != ENOTDIR)
ea92f41f
JH
175 die("'%s': %s", arg, strerror(errno));
176}
177
6b5ee137 178const char **get_pathspec(const char *prefix, const char **pathspec)
d288a700 179{
6b5ee137 180 const char *entry = *pathspec;
d089ebaa 181 const char **src, **dst;
d288a700
LT
182 int prefixlen;
183
f332726e
LT
184 if (!prefix && !entry)
185 return NULL;
d288a700
LT
186
187 if (!entry) {
188 static const char *spec[2];
189 spec[0] = prefix;
190 spec[1] = NULL;
191 return spec;
192 }
193
194 /* Otherwise we have to re-write the entries.. */
d089ebaa
JH
195 src = pathspec;
196 dst = pathspec;
f332726e 197 prefixlen = prefix ? strlen(prefix) : 0;
d089ebaa
JH
198 while (*src) {
199 const char *p = prefix_path(prefix, prefixlen, *src);
200 if (p)
201 *(dst++) = p;
3296766e
JH
202 else
203 exit(128); /* error message already given */
d089ebaa
JH
204 src++;
205 }
206 *dst = NULL;
207 if (!*pathspec)
208 return NULL;
209 return pathspec;
d288a700
LT
210}
211
5f5608bc 212/*
ad1a382f 213 * Test if it looks like we're at a git directory.
5e7bfe25 214 * We want to see:
5f5608bc 215 *
790296fd 216 * - either an objects/ directory _or_ the proper
5f5608bc 217 * GIT_OBJECT_DIRECTORY environment variable
ad1a382f 218 * - a refs/ directory
8098a178 219 * - either a HEAD symlink or a HEAD file that is formatted as
c847f537
JH
220 * a proper "ref:", or a regular file HEAD that has a properly
221 * formatted sha1 object name.
5f5608bc 222 */
ad1a382f 223static int is_git_directory(const char *suspect)
5f5608bc 224{
ad1a382f
SP
225 char path[PATH_MAX];
226 size_t len = strlen(suspect);
227
228 strcpy(path, suspect);
229 if (getenv(DB_ENVIRONMENT)) {
230 if (access(getenv(DB_ENVIRONMENT), X_OK))
231 return 0;
232 }
233 else {
234 strcpy(path + len, "/objects");
235 if (access(path, X_OK))
236 return 0;
237 }
238
239 strcpy(path + len, "/refs");
240 if (access(path, X_OK))
8098a178 241 return 0;
ad1a382f
SP
242
243 strcpy(path + len, "/HEAD");
c847f537 244 if (validate_headref(path))
ad1a382f
SP
245 return 0;
246
8098a178 247 return 1;
5f5608bc
LT
248}
249
68025633
JS
250int is_inside_git_dir(void)
251{
e90fdc39
JS
252 if (inside_git_dir < 0)
253 inside_git_dir = is_inside_dir(get_git_dir());
254 return inside_git_dir;
892c41b9
ML
255}
256
892c41b9
ML
257int is_inside_work_tree(void)
258{
e90fdc39
JS
259 if (inside_work_tree < 0)
260 inside_work_tree = is_inside_dir(get_git_work_tree());
261 return inside_work_tree;
892c41b9
ML
262}
263
e90fdc39 264/*
7efeb8f0
JS
265 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
266 * The old behaviour (which we retain here) is to set the work tree root
267 * to the cwd, unless overridden by the config, the command line, or
268 * GIT_WORK_TREE.
e90fdc39 269 */
7efeb8f0 270static const char *set_work_tree(const char *dir)
892c41b9 271{
7efeb8f0 272 char buffer[PATH_MAX + 1];
e90fdc39 273
7efeb8f0
JS
274 if (!getcwd(buffer, sizeof(buffer)))
275 die ("Could not get the current working directory");
276 git_work_tree_cfg = xstrdup(buffer);
e90fdc39
JS
277 inside_work_tree = 1;
278
7efeb8f0 279 return NULL;
68025633
JS
280}
281
f3fa1838
JH
282void setup_work_tree(void)
283{
354e6534
JS
284 const char *work_tree, *git_dir;
285 static int initialized = 0;
286
287 if (initialized)
288 return;
289 work_tree = get_git_work_tree();
290 git_dir = get_git_dir();
59f0f2f3
MH
291 if (!is_absolute_path(git_dir))
292 set_git_dir(make_absolute_path(git_dir));
293 if (!work_tree || chdir(work_tree))
294 die("This operation must be run in a work tree");
354e6534 295 initialized = 1;
59f0f2f3
MH
296}
297
9459aa77
NTND
298static int check_repository_format_gently(int *nongit_ok)
299{
ef90d6d4 300 git_config(check_repository_format_version, NULL);
9459aa77
NTND
301 if (GIT_REPO_VERSION < repository_format_version) {
302 if (!nongit_ok)
303 die ("Expected git repo version <= %d, found %d",
304 GIT_REPO_VERSION, repository_format_version);
305 warning("Expected git repo version <= %d, found %d",
306 GIT_REPO_VERSION, repository_format_version);
307 warning("Please upgrade Git");
308 *nongit_ok = -1;
309 return -1;
310 }
311 return 0;
312}
313
b44ebb19
LH
314/*
315 * Try to read the location of the git directory from the .git file,
316 * return path to git directory if found.
317 */
318const char *read_gitfile_gently(const char *path)
319{
320 char *buf;
321 struct stat st;
322 int fd;
323 size_t len;
324
325 if (stat(path, &st))
326 return NULL;
327 if (!S_ISREG(st.st_mode))
328 return NULL;
329 fd = open(path, O_RDONLY);
330 if (fd < 0)
331 die("Error opening %s: %s", path, strerror(errno));
332 buf = xmalloc(st.st_size + 1);
333 len = read_in_full(fd, buf, st.st_size);
334 close(fd);
335 if (len != st.st_size)
336 die("Error reading %s", path);
337 buf[len] = '\0';
338 if (prefixcmp(buf, "gitdir: "))
339 die("Invalid gitfile format: %s", path);
340 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
341 len--;
342 if (len < 9)
343 die("No path in gitfile: %s", path);
344 buf[len] = '\0';
345 if (!is_git_directory(buf + 8))
346 die("Not a git repository: %s", buf + 8);
347 path = make_absolute_path(buf + 8);
348 free(buf);
349 return path;
350}
351
e90fdc39
JS
352/*
353 * We cannot decide in this function whether we are in the work tree or
354 * not, since the config can only be read _after_ this function was called.
355 */
4ca06608 356const char *setup_git_directory_gently(int *nongit_ok)
d288a700 357{
e90fdc39 358 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
d288a700 359 static char cwd[PATH_MAX+1];
e90fdc39 360 const char *gitdirenv;
b44ebb19 361 const char *gitfile_dir;
e90fdc39 362 int len, offset;
d288a700 363
af05d679
SG
364 /*
365 * Let's assume that we are in a git repository.
366 * If it turns out later that we are somewhere else, the value will be
367 * updated accordingly.
368 */
369 if (nongit_ok)
370 *nongit_ok = 0;
371
e90fdc39
JS
372 /*
373 * If GIT_DIR is set explicitly, we're not going
374 * to do any discovery, but we still do repository
375 * validation.
376 */
ad1a382f 377 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
e90fdc39
JS
378 if (gitdirenv) {
379 if (PATH_MAX - 40 < strlen(gitdirenv))
380 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
381 if (is_git_directory(gitdirenv)) {
dd5c8af1
JS
382 static char buffer[1024 + 1];
383 const char *retval;
384
138dd1e9
NTND
385 if (!work_tree_env) {
386 retval = set_work_tree(gitdirenv);
9459aa77
NTND
387 /* config may override worktree */
388 if (check_repository_format_gently(nongit_ok))
389 return NULL;
138dd1e9
NTND
390 return retval;
391 }
9459aa77
NTND
392 if (check_repository_format_gently(nongit_ok))
393 return NULL;
dd5c8af1
JS
394 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
395 get_git_work_tree());
396 if (!retval || !*retval)
397 return NULL;
398 set_git_dir(make_absolute_path(gitdirenv));
399 if (chdir(work_tree_env) < 0)
400 die ("Could not chdir to %s", work_tree_env);
401 strcat(buffer, "/");
402 return retval;
892c41b9 403 }
3a3c3fc4 404 if (nongit_ok) {
41e95f69
ML
405 *nongit_ok = 1;
406 return NULL;
407 }
ad1a382f 408 die("Not a git repository: '%s'", gitdirenv);
5e7bfe25 409 }
d288a700 410
f66a4d68 411 if (!getcwd(cwd, sizeof(cwd)-1))
d288a700
LT
412 die("Unable to read current working directory");
413
892c41b9 414 /*
e90fdc39 415 * Test in the following order (relative to the cwd):
b44ebb19 416 * - .git (file containing "gitdir: <path>")
e90fdc39
JS
417 * - .git/
418 * - ./ (bare)
b44ebb19 419 * - ../.git
e90fdc39
JS
420 * - ../.git/
421 * - ../ (bare)
422 * - ../../.git/
423 * etc.
892c41b9 424 */
e90fdc39
JS
425 offset = len = strlen(cwd);
426 for (;;) {
b44ebb19
LH
427 gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
428 if (gitfile_dir) {
429 if (set_git_dir(gitfile_dir))
430 die("Repository setup failed");
431 break;
432 }
e90fdc39
JS
433 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
434 break;
435 if (is_git_directory(".")) {
436 inside_git_dir = 1;
437 if (!work_tree_env)
438 inside_work_tree = 0;
439 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
9459aa77 440 check_repository_format_gently(nongit_ok);
892c41b9
ML
441 return NULL;
442 }
e90fdc39
JS
443 chdir("..");
444 do {
445 if (!offset) {
446 if (nongit_ok) {
447 if (chdir(cwd))
448 die("Cannot come back to cwd");
449 *nongit_ok = 1;
450 return NULL;
451 }
452 die("Not a git repository");
453 }
454 } while (cwd[--offset] != '/');
892c41b9
ML
455 }
456
e90fdc39
JS
457 inside_git_dir = 0;
458 if (!work_tree_env)
459 inside_work_tree = 1;
460 git_work_tree_cfg = xstrndup(cwd, offset);
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
473int 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
499 * a chmod value.
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 */
521 return i & 0666;
94df2506
JH
522}
523
ef90d6d4 524int 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
544int check_repository_format(void)
545{
9459aa77 546 return check_repository_format_gently(NULL);
ab9cb76f
JH
547}
548
5e7bfe25
JH
549const 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))
558 die ("Could not jump back into original cwd");
559 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
560 return rel && *rel ? strcat(rel, "/") : NULL;
561 }
562
5e7bfe25
JH
563 return retval;
564}