Merge branch 'master' of git://repo.or.cz/alt-git to sync with v1.5.4-rc0
[git/platforms/storm.git] / setup.c
bloba1cfe8e82c1d7896761ed3a61d905c99d7d23aa9
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 const char *prefix_path(const char *prefix, int len, const char *path)
15 const char *orig = path;
16 int do_pfx;
17 for (;;) {
18 char c;
19 if (*path != '.')
20 break;
21 c = path[1];
22 /* "." */
23 if (!c) {
24 path++;
25 break;
27 /* "./" */
28 if (is_dir_sep(c)) {
29 path += 2;
30 continue;
32 if (c != '.')
33 break;
34 c = path[2];
35 if (!c)
36 path += 2;
37 else if (is_dir_sep(c))
38 path += 3;
39 else
40 break;
41 /* ".." and "../" */
42 /* Remove last component of the prefix */
43 do {
44 if (!len)
45 die("'%s' is outside repository", orig);
46 len--;
47 } while (len && !is_dir_sep(prefix[len-1]));
48 continue;
50 do_pfx = len;
51 #ifdef __MINGW32__
52 /* we want to convert '\' in path to '/' (prefix already has '/') */
54 const char *p = path;
55 while (!do_pfx && *p) {
56 do_pfx = *p++ == '\\';
59 #endif
60 if (do_pfx) {
61 int speclen = strlen(path);
62 char *n = xmalloc(speclen + len + 1);
63 char *p;
65 memcpy(n, prefix, len);
66 memcpy(n + len, path, speclen+1);
67 #ifdef __MINGW32__
68 for (p = n + len; *p; p++)
69 if (*p == '\\')
70 *p = '/';
71 #endif
72 path = n;
74 return path;
78 * Unlike prefix_path, this should be used if the named file does
79 * not have to interact with index entry; i.e. name of a random file
80 * on the filesystem.
82 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
84 static char path[PATH_MAX];
85 #ifndef __MINGW32__
86 if (!pfx || !*pfx || is_absolute_path(arg))
87 return arg;
88 memcpy(path, pfx, pfx_len);
89 strcpy(path + pfx_len, arg);
90 #else
91 char *p;
92 /* don't add prefix to absolute paths, but still replace '\' by '/' */
93 if (is_absolute_path(arg))
94 pfx_len = 0;
95 else
96 memcpy(path, pfx, pfx_len);
97 strcpy(path + pfx_len, arg);
98 for (p = path + pfx_len; *p; p++)
99 if (*p == '\\')
100 *p = '/';
101 #endif
102 return path;
106 * Verify a filename that we got as an argument for a pathspec
107 * entry. Note that a filename that begins with "-" never verifies
108 * as true, because even if such a filename were to exist, we want
109 * it to be preceded by the "--" marker (or we want the user to
110 * use a format like "./-filename")
112 void verify_filename(const char *prefix, const char *arg)
114 const char *name;
115 struct stat st;
117 if (*arg == '-')
118 die("bad flag '%s' used after filename", arg);
119 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
120 if (!lstat(name, &st))
121 return;
122 if (errno == ENOENT)
123 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
124 "Use '--' to separate paths from revisions", arg);
125 die("'%s': %s", arg, strerror(errno));
129 * Opposite of the above: the command line did not have -- marker
130 * and we parsed the arg as a refname. It should not be interpretable
131 * as a filename.
133 void verify_non_filename(const char *prefix, const char *arg)
135 const char *name;
136 struct stat st;
138 if (!is_inside_work_tree() || is_inside_git_dir())
139 return;
140 if (*arg == '-')
141 return; /* flag */
142 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
143 if (!lstat(name, &st))
144 die("ambiguous argument '%s': both revision and filename\n"
145 "Use '--' to separate filenames from revisions", arg);
146 if (errno != ENOENT && errno != ENOTDIR)
147 die("'%s': %s", arg, strerror(errno));
150 const char **get_pathspec(const char *prefix, const char **pathspec)
152 const char *entry = *pathspec;
153 const char **p;
154 int prefixlen;
156 if (!prefix && !entry)
157 return NULL;
159 if (!entry) {
160 static const char *spec[2];
161 spec[0] = prefix;
162 spec[1] = NULL;
163 return spec;
166 /* Otherwise we have to re-write the entries.. */
167 p = pathspec;
168 prefixlen = prefix ? strlen(prefix) : 0;
169 do {
170 *p = prefix_path(prefix, prefixlen, entry);
171 } while ((entry = *++p) != NULL);
172 return (const char **) pathspec;
176 * Test if it looks like we're at a git directory.
177 * We want to see:
179 * - either a objects/ directory _or_ the proper
180 * GIT_OBJECT_DIRECTORY environment variable
181 * - a refs/ directory
182 * - either a HEAD symlink or a HEAD file that is formatted as
183 * a proper "ref:", or a regular file HEAD that has a properly
184 * formatted sha1 object name.
186 static int is_git_directory(const char *suspect)
188 char path[PATH_MAX];
189 size_t len = strlen(suspect);
191 strcpy(path, suspect);
192 if (getenv(DB_ENVIRONMENT)) {
193 if (access(getenv(DB_ENVIRONMENT), X_OK))
194 return 0;
196 else {
197 strcpy(path + len, "/objects");
198 if (access(path, X_OK))
199 return 0;
202 strcpy(path + len, "/refs");
203 if (access(path, X_OK))
204 return 0;
206 strcpy(path + len, "/HEAD");
207 if (validate_headref(path))
208 return 0;
210 return 1;
213 int is_inside_git_dir(void)
215 if (inside_git_dir < 0)
216 inside_git_dir = is_inside_dir(get_git_dir());
217 return inside_git_dir;
220 int is_inside_work_tree(void)
222 if (inside_work_tree < 0)
223 inside_work_tree = is_inside_dir(get_git_work_tree());
224 return inside_work_tree;
228 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
229 * The old behaviour (which we retain here) is to set the work tree root
230 * to the cwd, unless overridden by the config, the command line, or
231 * GIT_WORK_TREE.
233 static const char *set_work_tree(const char *dir)
235 char buffer[PATH_MAX + 1];
237 if (!getcwd(buffer, sizeof(buffer)))
238 die ("Could not get the current working directory");
239 git_work_tree_cfg = xstrdup(buffer);
240 inside_work_tree = 1;
242 return NULL;
245 void setup_work_tree(void)
247 const char *work_tree, *git_dir;
248 static int initialized = 0;
250 if (initialized)
251 return;
252 work_tree = get_git_work_tree();
253 git_dir = get_git_dir();
254 if (!is_absolute_path(git_dir))
255 set_git_dir(make_absolute_path(git_dir));
256 if (!work_tree || chdir(work_tree))
257 die("This operation must be run in a work tree");
258 initialized = 1;
261 static int check_repository_format_gently(int *nongit_ok)
263 git_config(check_repository_format_version);
264 if (GIT_REPO_VERSION < repository_format_version) {
265 if (!nongit_ok)
266 die ("Expected git repo version <= %d, found %d",
267 GIT_REPO_VERSION, repository_format_version);
268 warning("Expected git repo version <= %d, found %d",
269 GIT_REPO_VERSION, repository_format_version);
270 warning("Please upgrade Git");
271 *nongit_ok = -1;
272 return -1;
274 return 0;
278 * We cannot decide in this function whether we are in the work tree or
279 * not, since the config can only be read _after_ this function was called.
281 const char *setup_git_directory_gently(int *nongit_ok)
283 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
284 static char cwd[PATH_MAX+1];
285 const char *gitdirenv;
286 int len, offset;
287 int minoffset = 0;
290 * If GIT_DIR is set explicitly, we're not going
291 * to do any discovery, but we still do repository
292 * validation.
294 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
295 if (gitdirenv) {
296 if (PATH_MAX - 40 < strlen(gitdirenv))
297 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
298 if (is_git_directory(gitdirenv)) {
299 static char buffer[1024 + 1];
300 const char *retval;
302 if (!work_tree_env) {
303 retval = set_work_tree(gitdirenv);
304 /* config may override worktree */
305 if (check_repository_format_gently(nongit_ok))
306 return NULL;
307 return retval;
309 if (check_repository_format_gently(nongit_ok))
310 return NULL;
311 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
312 get_git_work_tree());
313 if (!retval || !*retval)
314 return NULL;
315 set_git_dir(make_absolute_path(gitdirenv));
316 if (chdir(work_tree_env) < 0)
317 die ("Could not chdir to %s", work_tree_env);
318 strcat(buffer, "/");
319 return retval;
321 if (nongit_ok) {
322 *nongit_ok = 1;
323 return NULL;
325 die("Not a git repository: '%s'", gitdirenv);
328 if (!getcwd(cwd, sizeof(cwd)-1))
329 die("Unable to read current working directory");
330 #ifdef __MINGW32__
331 if (cwd[1] == ':')
332 minoffset = 2;
333 #endif
336 * Test in the following order (relative to the cwd):
337 * - .git/
338 * - ./ (bare)
339 * - ../.git/
340 * - ../ (bare)
341 * - ../../.git/
342 * etc.
344 offset = len = strlen(cwd);
345 for (;;) {
346 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
347 break;
348 if (is_git_directory(".")) {
349 inside_git_dir = 1;
350 if (!work_tree_env)
351 inside_work_tree = 0;
352 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
353 check_repository_format_gently(nongit_ok);
354 return NULL;
356 chdir("..");
357 do {
358 if (offset <= minoffset) {
359 if (nongit_ok) {
360 if (chdir(cwd))
361 die("Cannot come back to cwd");
362 *nongit_ok = 1;
363 return NULL;
365 die("Not a git repository");
367 } while (offset > minoffset && cwd[--offset] != '/');
370 inside_git_dir = 0;
371 if (!work_tree_env)
372 inside_work_tree = 1;
373 git_work_tree_cfg = xstrndup(cwd, offset);
374 if (check_repository_format_gently(nongit_ok))
375 return NULL;
376 if (offset == len)
377 return NULL;
379 /* Make "offset" point to past the '/', and add a '/' at the end */
380 offset++;
381 cwd[len++] = '/';
382 cwd[len] = 0;
383 return cwd + offset;
386 int git_config_perm(const char *var, const char *value)
388 if (value) {
389 int i;
390 if (!strcmp(value, "umask"))
391 return PERM_UMASK;
392 if (!strcmp(value, "group"))
393 return PERM_GROUP;
394 if (!strcmp(value, "all") ||
395 !strcmp(value, "world") ||
396 !strcmp(value, "everybody"))
397 return PERM_EVERYBODY;
398 i = atoi(value);
399 if (i > 1)
400 return i;
402 return git_config_bool(var, value);
405 int check_repository_format_version(const char *var, const char *value)
407 if (strcmp(var, "core.repositoryformatversion") == 0)
408 repository_format_version = git_config_int(var, value);
409 else if (strcmp(var, "core.sharedrepository") == 0)
410 shared_repository = git_config_perm(var, value);
411 else if (strcmp(var, "core.bare") == 0) {
412 is_bare_repository_cfg = git_config_bool(var, value);
413 if (is_bare_repository_cfg == 1)
414 inside_work_tree = -1;
415 } else if (strcmp(var, "core.worktree") == 0) {
416 if (git_work_tree_cfg)
417 free(git_work_tree_cfg);
418 git_work_tree_cfg = xstrdup(value);
419 inside_work_tree = -1;
421 return 0;
424 int check_repository_format(void)
426 return check_repository_format_gently(NULL);
429 const char *setup_git_directory(void)
431 const char *retval = setup_git_directory_gently(NULL);
433 /* If the work tree is not the default one, recompute prefix */
434 if (inside_work_tree < 0) {
435 static char buffer[PATH_MAX + 1];
436 char *rel;
437 if (retval && chdir(retval))
438 die ("Could not jump back into original cwd");
439 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
440 return rel && *rel ? strcat(rel, "/") : NULL;
443 return retval;