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