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