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