Merge branch 'master' of git://repo.or.cz/alt-git
[git/platforms/storm.git] / setup.c
blobd184d4d5e5fc0160210165e04e3e97c1bc905624
1 #include "cache.h"
2 #include "dir.h"
4 static int inside_git_dir = -1;
5 static int inside_work_tree = -1;
7 #ifdef __MINGW32__
8 static inline int is_dir_sep(char c) { return c == '/' || c == '\\'; }
9 #else
10 static inline int is_dir_sep(char c) { return c == '/'; }
11 #endif
13 static int sanitary_path_copy(char *dst, const char *src)
15 char *dst0 = dst;
17 #ifdef __MINGW32__
18 if (isalpha(*src) && src[1] == ':') {
19 *dst++ = *src++;
20 *dst++ = *src++;
21 dst0 = dst;
23 #endif
24 if (is_dir_sep(*src)) {
25 *dst++ = '/';
26 while (is_dir_sep(*src))
27 src++;
30 for (;;) {
31 char c = *src;
34 * A path component that begins with . could be
35 * special:
36 * (1) "." and ends -- ignore and terminate.
37 * (2) "./" -- ignore them, eat slash and continue.
38 * (3) ".." and ends -- strip one and terminate.
39 * (4) "../" -- strip one, eat slash and continue.
41 if (c == '.') {
42 switch (src[1]) {
43 case '\0':
44 /* (1) */
45 src++;
46 break;
47 case '/':
48 #ifdef __MINGW32__
49 case '\\':
50 #endif
51 /* (2) */
52 src += 2;
53 while (is_dir_sep(*src))
54 src++;
55 continue;
56 case '.':
57 switch (src[2]) {
58 case '\0':
59 /* (3) */
60 src += 2;
61 goto up_one;
62 case '/':
63 #ifdef __MINGW32__
64 case '\\':
65 #endif
66 /* (4) */
67 src += 3;
68 while (is_dir_sep(*src))
69 src++;
70 goto up_one;
75 /* copy up to the next '/', and eat all '/' */
76 while ((c = *src++) != '\0' && !is_dir_sep(c))
77 *dst++ = c;
78 if (is_dir_sep(c)) {
79 *dst++ = '/';
80 while (is_dir_sep(c))
81 c = *src++;
82 src--;
83 } else if (!c)
84 break;
85 continue;
87 up_one:
89 * dst0..dst is prefix portion, and dst[-1] is '/';
90 * go up one level.
92 dst -= 2; /* go past trailing '/' if any */
93 if (dst < dst0)
94 return -1;
95 while (1) {
96 if (dst <= dst0)
97 break;
98 c = *dst--;
99 if (c == '/') { /* MinGW: cannot be '\\' anymore */
100 dst += 2;
101 break;
105 *dst = '\0';
106 return 0;
109 const char *prefix_path(const char *prefix, int len, const char *path)
111 const char *orig = path;
112 char *sanitized = xmalloc(len + strlen(path) + 1);
113 if (is_absolute_path(orig))
114 strcpy(sanitized, path);
115 else {
116 if (len)
117 memcpy(sanitized, prefix, len);
118 strcpy(sanitized + len, path);
120 if (sanitary_path_copy(sanitized, sanitized))
121 goto error_out;
122 if (is_absolute_path(orig)) {
123 const char *work_tree = get_git_work_tree();
124 size_t len = strlen(work_tree);
125 size_t total = strlen(sanitized) + 1;
126 if (strncmp(sanitized, work_tree, len) ||
127 (sanitized[len] != '\0' && sanitized[len] != '/')) {
128 error_out:
129 error("'%s' is outside repository", orig);
130 free(sanitized);
131 return NULL;
133 if (sanitized[len] == '/')
134 len++;
135 memmove(sanitized, sanitized + len, total - len);
137 return sanitized;
141 * Unlike prefix_path, this should be used if the named file does
142 * not have to interact with index entry; i.e. name of a random file
143 * on the filesystem.
145 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
147 static char path[PATH_MAX];
148 #ifndef __MINGW32__
149 if (!pfx || !*pfx || is_absolute_path(arg))
150 return arg;
151 memcpy(path, pfx, pfx_len);
152 strcpy(path + pfx_len, arg);
153 #else
154 char *p;
155 /* don't add prefix to absolute paths, but still replace '\' by '/' */
156 if (is_absolute_path(arg))
157 pfx_len = 0;
158 else
159 memcpy(path, pfx, pfx_len);
160 strcpy(path + pfx_len, arg);
161 for (p = path + pfx_len; *p; p++)
162 if (*p == '\\')
163 *p = '/';
164 #endif
165 return path;
169 * Verify a filename that we got as an argument for a pathspec
170 * entry. Note that a filename that begins with "-" never verifies
171 * as true, because even if such a filename were to exist, we want
172 * it to be preceded by the "--" marker (or we want the user to
173 * use a format like "./-filename")
175 void verify_filename(const char *prefix, const char *arg)
177 const char *name;
178 struct stat st;
180 if (*arg == '-')
181 die("bad flag '%s' used after filename", arg);
182 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
183 if (!lstat(name, &st))
184 return;
185 if (errno == ENOENT)
186 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
187 "Use '--' to separate paths from revisions", arg);
188 die("'%s': %s", arg, strerror(errno));
192 * Opposite of the above: the command line did not have -- marker
193 * and we parsed the arg as a refname. It should not be interpretable
194 * as a filename.
196 void verify_non_filename(const char *prefix, const char *arg)
198 const char *name;
199 struct stat st;
201 if (!is_inside_work_tree() || is_inside_git_dir())
202 return;
203 if (*arg == '-')
204 return; /* flag */
205 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
206 if (!lstat(name, &st))
207 die("ambiguous argument '%s': both revision and filename\n"
208 "Use '--' to separate filenames from revisions", arg);
209 if (errno != ENOENT && errno != ENOTDIR)
210 die("'%s': %s", arg, strerror(errno));
213 const char **get_pathspec(const char *prefix, const char **pathspec)
215 const char *entry = *pathspec;
216 const char **src, **dst;
217 int prefixlen;
219 if (!prefix && !entry)
220 return NULL;
222 if (!entry) {
223 static const char *spec[2];
224 spec[0] = prefix;
225 spec[1] = NULL;
226 return spec;
229 /* Otherwise we have to re-write the entries.. */
230 src = pathspec;
231 dst = pathspec;
232 prefixlen = prefix ? strlen(prefix) : 0;
233 while (*src) {
234 const char *p = prefix_path(prefix, prefixlen, *src);
235 if (p)
236 *(dst++) = p;
237 src++;
239 *dst = NULL;
240 if (!*pathspec)
241 return NULL;
242 return pathspec;
246 * Test if it looks like we're at a git directory.
247 * We want to see:
249 * - either an objects/ directory _or_ the proper
250 * GIT_OBJECT_DIRECTORY environment variable
251 * - a refs/ directory
252 * - either a HEAD symlink or a HEAD file that is formatted as
253 * a proper "ref:", or a regular file HEAD that has a properly
254 * formatted sha1 object name.
256 static int is_git_directory(const char *suspect)
258 char path[PATH_MAX];
259 size_t len = strlen(suspect);
261 strcpy(path, suspect);
262 if (getenv(DB_ENVIRONMENT)) {
263 if (access(getenv(DB_ENVIRONMENT), X_OK))
264 return 0;
266 else {
267 strcpy(path + len, "/objects");
268 if (access(path, X_OK))
269 return 0;
272 strcpy(path + len, "/refs");
273 if (access(path, X_OK))
274 return 0;
276 strcpy(path + len, "/HEAD");
277 if (validate_headref(path))
278 return 0;
280 return 1;
283 int is_inside_git_dir(void)
285 if (inside_git_dir < 0)
286 inside_git_dir = is_inside_dir(get_git_dir());
287 return inside_git_dir;
290 int is_inside_work_tree(void)
292 if (inside_work_tree < 0)
293 inside_work_tree = is_inside_dir(get_git_work_tree());
294 return inside_work_tree;
298 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
299 * The old behaviour (which we retain here) is to set the work tree root
300 * to the cwd, unless overridden by the config, the command line, or
301 * GIT_WORK_TREE.
303 static const char *set_work_tree(const char *dir)
305 char buffer[PATH_MAX + 1];
307 if (!getcwd(buffer, sizeof(buffer)))
308 die ("Could not get the current working directory");
309 git_work_tree_cfg = xstrdup(buffer);
310 inside_work_tree = 1;
312 return NULL;
315 void setup_work_tree(void)
317 const char *work_tree, *git_dir;
318 static int initialized = 0;
320 if (initialized)
321 return;
322 work_tree = get_git_work_tree();
323 git_dir = get_git_dir();
324 if (!is_absolute_path(git_dir))
325 set_git_dir(make_absolute_path(git_dir));
326 if (!work_tree || chdir(work_tree))
327 die("This operation must be run in a work tree");
328 initialized = 1;
331 static int check_repository_format_gently(int *nongit_ok)
333 git_config(check_repository_format_version);
334 if (GIT_REPO_VERSION < repository_format_version) {
335 if (!nongit_ok)
336 die ("Expected git repo version <= %d, found %d",
337 GIT_REPO_VERSION, repository_format_version);
338 warning("Expected git repo version <= %d, found %d",
339 GIT_REPO_VERSION, repository_format_version);
340 warning("Please upgrade Git");
341 *nongit_ok = -1;
342 return -1;
344 return 0;
348 * We cannot decide in this function whether we are in the work tree or
349 * not, since the config can only be read _after_ this function was called.
351 const char *setup_git_directory_gently(int *nongit_ok)
353 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
354 static char cwd[PATH_MAX+1];
355 const char *gitdirenv;
356 int len, offset;
357 int minoffset = 0;
360 * If GIT_DIR is set explicitly, we're not going
361 * to do any discovery, but we still do repository
362 * validation.
364 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
365 if (gitdirenv) {
366 if (PATH_MAX - 40 < strlen(gitdirenv))
367 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
368 if (is_git_directory(gitdirenv)) {
369 static char buffer[1024 + 1];
370 const char *retval;
372 if (!work_tree_env) {
373 retval = set_work_tree(gitdirenv);
374 /* config may override worktree */
375 if (check_repository_format_gently(nongit_ok))
376 return NULL;
377 return retval;
379 if (check_repository_format_gently(nongit_ok))
380 return NULL;
381 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
382 get_git_work_tree());
383 if (!retval || !*retval)
384 return NULL;
385 set_git_dir(make_absolute_path(gitdirenv));
386 if (chdir(work_tree_env) < 0)
387 die ("Could not chdir to %s", work_tree_env);
388 strcat(buffer, "/");
389 return retval;
391 if (nongit_ok) {
392 *nongit_ok = 1;
393 return NULL;
395 die("Not a git repository: '%s'", gitdirenv);
398 if (!getcwd(cwd, sizeof(cwd)-1))
399 die("Unable to read current working directory");
400 if (has_dos_drive_prefix(cwd))
401 minoffset = 2;
404 * Test in the following order (relative to the cwd):
405 * - .git/
406 * - ./ (bare)
407 * - ../.git/
408 * - ../ (bare)
409 * - ../../.git/
410 * etc.
412 offset = len = strlen(cwd);
413 for (;;) {
414 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
415 break;
416 if (is_git_directory(".")) {
417 inside_git_dir = 1;
418 if (!work_tree_env)
419 inside_work_tree = 0;
420 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
421 check_repository_format_gently(nongit_ok);
422 return NULL;
424 chdir("..");
425 do {
426 if (offset <= minoffset) {
427 if (nongit_ok) {
428 if (chdir(cwd))
429 die("Cannot come back to cwd");
430 *nongit_ok = 1;
431 return NULL;
433 die("Not a git repository");
435 } while (offset > minoffset && cwd[--offset] != '/');
438 inside_git_dir = 0;
439 if (!work_tree_env)
440 inside_work_tree = 1;
441 git_work_tree_cfg = xstrndup(cwd, offset);
442 if (check_repository_format_gently(nongit_ok))
443 return NULL;
444 if (offset == len)
445 return NULL;
447 /* Make "offset" point to past the '/', and add a '/' at the end */
448 offset++;
449 cwd[len++] = '/';
450 cwd[len] = 0;
451 return cwd + offset;
454 int git_config_perm(const char *var, const char *value)
456 if (value) {
457 int i;
458 if (!strcmp(value, "umask"))
459 return PERM_UMASK;
460 if (!strcmp(value, "group"))
461 return PERM_GROUP;
462 if (!strcmp(value, "all") ||
463 !strcmp(value, "world") ||
464 !strcmp(value, "everybody"))
465 return PERM_EVERYBODY;
466 i = atoi(value);
467 if (i > 1)
468 return i;
470 return git_config_bool(var, value);
473 int check_repository_format_version(const char *var, const char *value)
475 if (strcmp(var, "core.repositoryformatversion") == 0)
476 repository_format_version = git_config_int(var, value);
477 else if (strcmp(var, "core.sharedrepository") == 0)
478 shared_repository = git_config_perm(var, value);
479 else if (strcmp(var, "core.bare") == 0) {
480 is_bare_repository_cfg = git_config_bool(var, value);
481 if (is_bare_repository_cfg == 1)
482 inside_work_tree = -1;
483 } else if (strcmp(var, "core.worktree") == 0) {
484 if (!value)
485 return config_error_nonbool(var);
486 free(git_work_tree_cfg);
487 git_work_tree_cfg = xstrdup(value);
488 inside_work_tree = -1;
490 return 0;
493 int check_repository_format(void)
495 return check_repository_format_gently(NULL);
498 const char *setup_git_directory(void)
500 const char *retval = setup_git_directory_gently(NULL);
502 /* If the work tree is not the default one, recompute prefix */
503 if (inside_work_tree < 0) {
504 static char buffer[PATH_MAX + 1];
505 char *rel;
506 if (retval && chdir(retval))
507 die ("Could not jump back into original cwd");
508 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
509 return rel && *rel ? strcat(rel, "/") : NULL;
512 return retval;