Merge commit 'junio/master'
[git/platforms.git] / setup.c
blob5a8e7b6b5255d650fbd0594d6b13a4f63d4f4699
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 const int is_absolute =
92 is_dir_sep(arg[0]) ||
93 (arg[0] && arg[1] == ':' && is_dir_sep(arg[2]));
94 if (is_absolute)
95 pfx_len = 0;
96 else
97 #endif
98 memcpy(path, pfx, pfx_len);
99 strcpy(path + pfx_len, arg);
100 for (p = path + pfx_len; *p; p++)
101 if (*p == '\\')
102 *p = '/';
103 return path;
107 * Verify a filename that we got as an argument for a pathspec
108 * entry. Note that a filename that begins with "-" never verifies
109 * as true, because even if such a filename were to exist, we want
110 * it to be preceded by the "--" marker (or we want the user to
111 * use a format like "./-filename")
113 void verify_filename(const char *prefix, const char *arg)
115 const char *name;
116 struct stat st;
118 if (*arg == '-')
119 die("bad flag '%s' used after filename", arg);
120 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
121 if (!lstat(name, &st))
122 return;
123 if (errno == ENOENT)
124 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
125 "Use '--' to separate paths from revisions", arg);
126 die("'%s': %s", arg, strerror(errno));
130 * Opposite of the above: the command line did not have -- marker
131 * and we parsed the arg as a refname. It should not be interpretable
132 * as a filename.
134 void verify_non_filename(const char *prefix, const char *arg)
136 const char *name;
137 struct stat st;
139 if (!is_inside_work_tree() || is_inside_git_dir())
140 return;
141 if (*arg == '-')
142 return; /* flag */
143 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
144 if (!lstat(name, &st))
145 die("ambiguous argument '%s': both revision and filename\n"
146 "Use '--' to separate filenames from revisions", arg);
147 if (errno != ENOENT && errno != ENOTDIR)
148 die("'%s': %s", arg, strerror(errno));
151 const char **get_pathspec(const char *prefix, const char **pathspec)
153 const char *entry = *pathspec;
154 const char **p;
155 int prefixlen;
157 if (!prefix && !entry)
158 return NULL;
160 if (!entry) {
161 static const char *spec[2];
162 spec[0] = prefix;
163 spec[1] = NULL;
164 return spec;
167 /* Otherwise we have to re-write the entries.. */
168 p = pathspec;
169 prefixlen = prefix ? strlen(prefix) : 0;
170 do {
171 *p = prefix_path(prefix, prefixlen, entry);
172 } while ((entry = *++p) != NULL);
173 return (const char **) pathspec;
177 * Test if it looks like we're at a git directory.
178 * We want to see:
180 * - either a objects/ directory _or_ the proper
181 * GIT_OBJECT_DIRECTORY environment variable
182 * - a refs/ directory
183 * - either a HEAD symlink or a HEAD file that is formatted as
184 * a proper "ref:", or a regular file HEAD that has a properly
185 * formatted sha1 object name.
187 static int is_git_directory(const char *suspect)
189 char path[PATH_MAX];
190 size_t len = strlen(suspect);
192 strcpy(path, suspect);
193 if (getenv(DB_ENVIRONMENT)) {
194 if (access(getenv(DB_ENVIRONMENT), X_OK))
195 return 0;
197 else {
198 strcpy(path + len, "/objects");
199 if (access(path, X_OK))
200 return 0;
203 strcpy(path + len, "/refs");
204 if (access(path, X_OK))
205 return 0;
207 strcpy(path + len, "/HEAD");
208 if (validate_headref(path))
209 return 0;
211 return 1;
214 int is_inside_git_dir(void)
216 if (inside_git_dir < 0)
217 inside_git_dir = is_inside_dir(get_git_dir());
218 return inside_git_dir;
221 int is_inside_work_tree(void)
223 if (inside_work_tree < 0)
224 inside_work_tree = is_inside_dir(get_git_work_tree());
225 return inside_work_tree;
229 * If no worktree was given, and we are outside of a default work tree,
230 * now is the time to set it.
232 * In other words, if the user calls git with something like
234 * git --git-dir=/some/where/else/.git bla
236 * default to /some/where/else as working directory; if the specified
237 * git-dir does not end in "/.git", the cwd is used as working directory.
239 const char *set_work_tree(const char *dir)
241 char dir_buffer[PATH_MAX], *rel = NULL;
242 static char buffer[PATH_MAX + 1];
243 int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1;
245 /* strip the variable 'dir' of the postfix "/.git" if it has it */
246 len = strlen(dir);
247 if (len > suffix_len &&
248 !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
249 if ((len - suffix_len) >= sizeof(dir_buffer))
250 die("directory name too long");
251 memcpy(dir_buffer, dir, len - suffix_len);
252 dir_buffer[len - suffix_len] = '\0';
254 /* are we inside the default work tree? */
255 rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer);
258 /* if rel is set, the cwd is _not_ the current working tree */
259 if (rel && *rel) {
260 if (!is_absolute_path(dir))
261 set_git_dir(make_absolute_path(dir));
262 dir = dir_buffer;
263 if (chdir(dir))
264 die("cannot chdir to %s: %s", dir, strerror(errno));
265 else
266 strcat(rel, "/");
267 inside_git_dir = 0;
268 } else {
269 rel = NULL;
270 dir = getcwd(buffer, sizeof(buffer));
272 git_work_tree_cfg = xstrdup(dir);
273 inside_work_tree = 1;
275 return rel;
279 * We cannot decide in this function whether we are in the work tree or
280 * not, since the config can only be read _after_ this function was called.
282 const char *setup_git_directory_gently(int *nongit_ok)
284 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
285 static char cwd[PATH_MAX+1];
286 const char *gitdirenv;
287 int len, offset, 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 if (!work_tree_env)
300 return set_work_tree(gitdirenv);
301 return NULL;
303 if (nongit_ok) {
304 *nongit_ok = 1;
305 return NULL;
307 die("Not a git repository: '%s'", gitdirenv);
310 if (!getcwd(cwd, sizeof(cwd)-1))
311 die("Unable to read current working directory");
313 #ifdef __MINGW32__
314 if (cwd[1] == ':')
315 minoffset = 2;
316 #endif
319 * Test in the following order (relative to the cwd):
320 * - .git/
321 * - ./ (bare)
322 * - ../.git/
323 * - ../ (bare)
324 * - ../../.git/
325 * etc.
327 offset = len = strlen(cwd);
328 for (;;) {
329 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
330 break;
331 if (is_git_directory(".")) {
332 inside_git_dir = 1;
333 if (!work_tree_env)
334 inside_work_tree = 0;
335 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
336 return NULL;
338 chdir("..");
339 do {
340 if (offset == minoffset) {
341 if (nongit_ok) {
342 if (chdir(cwd))
343 die("Cannot come back to cwd");
344 *nongit_ok = 1;
345 return NULL;
347 die("Not a git repository");
349 } while (cwd[--offset] != '/');
352 inside_git_dir = 0;
353 if (!work_tree_env)
354 inside_work_tree = 1;
355 git_work_tree_cfg = xstrndup(cwd, offset);
356 if (offset == len)
357 return NULL;
359 /* Make "offset" point to past the '/', and add a '/' at the end */
360 offset++;
361 cwd[len++] = '/';
362 cwd[len] = 0;
363 return cwd + offset;
366 int git_config_perm(const char *var, const char *value)
368 if (value) {
369 int i;
370 if (!strcmp(value, "umask"))
371 return PERM_UMASK;
372 if (!strcmp(value, "group"))
373 return PERM_GROUP;
374 if (!strcmp(value, "all") ||
375 !strcmp(value, "world") ||
376 !strcmp(value, "everybody"))
377 return PERM_EVERYBODY;
378 i = atoi(value);
379 if (i > 1)
380 return i;
382 return git_config_bool(var, value);
385 int check_repository_format_version(const char *var, const char *value)
387 if (strcmp(var, "core.repositoryformatversion") == 0)
388 repository_format_version = git_config_int(var, value);
389 else if (strcmp(var, "core.sharedrepository") == 0)
390 shared_repository = git_config_perm(var, value);
391 else if (strcmp(var, "core.bare") == 0) {
392 is_bare_repository_cfg = git_config_bool(var, value);
393 if (is_bare_repository_cfg == 1)
394 inside_work_tree = -1;
395 } else if (strcmp(var, "core.worktree") == 0) {
396 if (git_work_tree_cfg)
397 free(git_work_tree_cfg);
398 git_work_tree_cfg = xstrdup(value);
399 inside_work_tree = -1;
401 return 0;
404 int check_repository_format(void)
406 git_config(check_repository_format_version);
407 if (GIT_REPO_VERSION < repository_format_version)
408 die ("Expected git repo version <= %d, found %d",
409 GIT_REPO_VERSION, repository_format_version);
410 return 0;
413 const char *setup_git_directory(void)
415 const char *retval = setup_git_directory_gently(NULL);
416 check_repository_format();
418 /* If the work tree is not the default one, recompute prefix */
419 if (inside_work_tree < 0) {
420 static char buffer[PATH_MAX + 1];
421 char *rel;
422 if (retval && chdir(retval))
423 die ("Could not jump back into original cwd");
424 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
425 return rel && *rel ? strcat(rel, "/") : NULL;
428 return retval;