Merge commit 'spearce/master'
[git/mingw.git] / setup.c
blob8715d19b8ecca9972ae831c1d0618cf0eb6eb312
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 char *p;
86 #ifndef __MINGW32__
87 if (!pfx || !*pfx || arg[0] == '/')
88 return arg;
89 #else
90 /* don't add prefix to absolute paths */
91 if (is_absolute_path(arg))
92 pfx_len = 0;
93 else
94 #endif
95 memcpy(path, pfx, pfx_len);
96 strcpy(path + pfx_len, arg);
97 for (p = path + pfx_len; *p; p++)
98 if (*p == '\\')
99 *p = '/';
100 return path;
104 * Verify a filename that we got as an argument for a pathspec
105 * entry. Note that a filename that begins with "-" never verifies
106 * as true, because even if such a filename were to exist, we want
107 * it to be preceded by the "--" marker (or we want the user to
108 * use a format like "./-filename")
110 void verify_filename(const char *prefix, const char *arg)
112 const char *name;
113 struct stat st;
115 if (*arg == '-')
116 die("bad flag '%s' used after filename", arg);
117 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
118 if (!lstat(name, &st))
119 return;
120 if (errno == ENOENT)
121 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
122 "Use '--' to separate paths from revisions", arg);
123 die("'%s': %s", arg, strerror(errno));
127 * Opposite of the above: the command line did not have -- marker
128 * and we parsed the arg as a refname. It should not be interpretable
129 * as a filename.
131 void verify_non_filename(const char *prefix, const char *arg)
133 const char *name;
134 struct stat st;
136 if (!is_inside_work_tree() || is_inside_git_dir())
137 return;
138 if (*arg == '-')
139 return; /* flag */
140 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
141 if (!lstat(name, &st))
142 die("ambiguous argument '%s': both revision and filename\n"
143 "Use '--' to separate filenames from revisions", arg);
144 if (errno != ENOENT && errno != ENOTDIR)
145 die("'%s': %s", arg, strerror(errno));
148 const char **get_pathspec(const char *prefix, const char **pathspec)
150 const char *entry = *pathspec;
151 const char **p;
152 int prefixlen;
154 if (!prefix && !entry)
155 return NULL;
157 if (!entry) {
158 static const char *spec[2];
159 spec[0] = prefix;
160 spec[1] = NULL;
161 return spec;
164 /* Otherwise we have to re-write the entries.. */
165 p = pathspec;
166 prefixlen = prefix ? strlen(prefix) : 0;
167 do {
168 *p = prefix_path(prefix, prefixlen, entry);
169 } while ((entry = *++p) != NULL);
170 return (const char **) pathspec;
174 * Test if it looks like we're at a git directory.
175 * We want to see:
177 * - either a objects/ directory _or_ the proper
178 * GIT_OBJECT_DIRECTORY environment variable
179 * - a refs/ directory
180 * - either a HEAD symlink or a HEAD file that is formatted as
181 * a proper "ref:", or a regular file HEAD that has a properly
182 * formatted sha1 object name.
184 static int is_git_directory(const char *suspect)
186 char path[PATH_MAX];
187 size_t len = strlen(suspect);
189 strcpy(path, suspect);
190 if (getenv(DB_ENVIRONMENT)) {
191 if (access(getenv(DB_ENVIRONMENT), X_OK))
192 return 0;
194 else {
195 strcpy(path + len, "/objects");
196 if (access(path, X_OK))
197 return 0;
200 strcpy(path + len, "/refs");
201 if (access(path, X_OK))
202 return 0;
204 strcpy(path + len, "/HEAD");
205 if (validate_headref(path))
206 return 0;
208 return 1;
211 int is_inside_git_dir(void)
213 if (inside_git_dir < 0)
214 inside_git_dir = is_inside_dir(get_git_dir());
215 return inside_git_dir;
218 int is_inside_work_tree(void)
220 if (inside_work_tree < 0)
221 inside_work_tree = is_inside_dir(get_git_work_tree());
222 return inside_work_tree;
226 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
227 * The old behaviour (which we retain here) is to set the work tree root
228 * to the cwd, unless overridden by the config, the command line, or
229 * GIT_WORK_TREE.
231 static const char *set_work_tree(const char *dir)
233 char buffer[PATH_MAX + 1];
235 if (!getcwd(buffer, sizeof(buffer)))
236 die ("Could not get the current working directory");
237 git_work_tree_cfg = xstrdup(buffer);
238 inside_work_tree = 1;
240 return NULL;
244 * We cannot decide in this function whether we are in the work tree or
245 * not, since the config can only be read _after_ this function was called.
247 const char *setup_git_directory_gently(int *nongit_ok)
249 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
250 static char cwd[PATH_MAX+1];
251 const char *gitdirenv;
252 int len, offset;
253 int minoffset = 0;
256 * If GIT_DIR is set explicitly, we're not going
257 * to do any discovery, but we still do repository
258 * validation.
260 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
261 if (gitdirenv) {
262 if (PATH_MAX - 40 < strlen(gitdirenv))
263 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
264 if (is_git_directory(gitdirenv)) {
265 static char buffer[1024 + 1];
266 const char *retval;
268 if (!work_tree_env)
269 return set_work_tree(gitdirenv);
270 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
271 get_git_work_tree());
272 if (!retval || !*retval)
273 return NULL;
274 set_git_dir(make_absolute_path(gitdirenv));
275 if (chdir(work_tree_env) < 0)
276 die ("Could not chdir to %s", work_tree_env);
277 strcat(buffer, "/");
278 return retval;
280 if (nongit_ok) {
281 *nongit_ok = 1;
282 return NULL;
284 die("Not a git repository: '%s'", gitdirenv);
287 if (!getcwd(cwd, sizeof(cwd)-1))
288 die("Unable to read current working directory");
289 #ifdef __MINGW32__
290 if (cwd[1] == ':')
291 minoffset = 2;
292 #endif
295 * Test in the following order (relative to the cwd):
296 * - .git/
297 * - ./ (bare)
298 * - ../.git/
299 * - ../ (bare)
300 * - ../../.git/
301 * etc.
303 offset = len = strlen(cwd);
304 for (;;) {
305 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
306 break;
307 if (is_git_directory(".")) {
308 inside_git_dir = 1;
309 if (!work_tree_env)
310 inside_work_tree = 0;
311 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
312 return NULL;
314 chdir("..");
315 do {
316 if (offset <= minoffset) {
317 if (nongit_ok) {
318 if (chdir(cwd))
319 die("Cannot come back to cwd");
320 *nongit_ok = 1;
321 return NULL;
323 die("Not a git repository");
325 } while (offset > minoffset && cwd[--offset] != '/');
328 inside_git_dir = 0;
329 if (!work_tree_env)
330 inside_work_tree = 1;
331 git_work_tree_cfg = xstrndup(cwd, offset);
332 if (offset == len)
333 return NULL;
335 /* Make "offset" point to past the '/', and add a '/' at the end */
336 offset++;
337 cwd[len++] = '/';
338 cwd[len] = 0;
339 return cwd + offset;
342 int git_config_perm(const char *var, const char *value)
344 if (value) {
345 int i;
346 if (!strcmp(value, "umask"))
347 return PERM_UMASK;
348 if (!strcmp(value, "group"))
349 return PERM_GROUP;
350 if (!strcmp(value, "all") ||
351 !strcmp(value, "world") ||
352 !strcmp(value, "everybody"))
353 return PERM_EVERYBODY;
354 i = atoi(value);
355 if (i > 1)
356 return i;
358 return git_config_bool(var, value);
361 int check_repository_format_version(const char *var, const char *value)
363 if (strcmp(var, "core.repositoryformatversion") == 0)
364 repository_format_version = git_config_int(var, value);
365 else if (strcmp(var, "core.sharedrepository") == 0)
366 shared_repository = git_config_perm(var, value);
367 else if (strcmp(var, "core.bare") == 0) {
368 is_bare_repository_cfg = git_config_bool(var, value);
369 if (is_bare_repository_cfg == 1)
370 inside_work_tree = -1;
371 } else if (strcmp(var, "core.worktree") == 0) {
372 if (git_work_tree_cfg)
373 free(git_work_tree_cfg);
374 git_work_tree_cfg = xstrdup(value);
375 inside_work_tree = -1;
377 return 0;
380 int check_repository_format(void)
382 git_config(check_repository_format_version);
383 if (GIT_REPO_VERSION < repository_format_version)
384 die ("Expected git repo version <= %d, found %d",
385 GIT_REPO_VERSION, repository_format_version);
386 return 0;
389 const char *setup_git_directory(void)
391 const char *retval = setup_git_directory_gently(NULL);
392 check_repository_format();
394 /* If the work tree is not the default one, recompute prefix */
395 if (inside_work_tree < 0) {
396 static char buffer[PATH_MAX + 1];
397 char *rel;
398 if (retval && chdir(retval))
399 die ("Could not jump back into original cwd");
400 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
401 return rel && *rel ? strcat(rel, "/") : NULL;
404 return retval;