skip t5512 because remote does not yet work
[git/platforms/storm.git] / setup.c
blobfd7d117189371ebf62b156e94c9f376763697b59
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 || is_absolute_path(arg))
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;
243 void setup_work_tree(void)
245 const char *work_tree, *git_dir;
246 static int initialized = 0;
248 if (initialized)
249 return;
250 work_tree = get_git_work_tree();
251 git_dir = get_git_dir();
252 if (!is_absolute_path(git_dir))
253 set_git_dir(make_absolute_path(git_dir));
254 if (!work_tree || chdir(work_tree))
255 die("This operation must be run in a work tree");
256 initialized = 1;
260 * We cannot decide in this function whether we are in the work tree or
261 * not, since the config can only be read _after_ this function was called.
263 const char *setup_git_directory_gently(int *nongit_ok)
265 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
266 static char cwd[PATH_MAX+1];
267 const char *gitdirenv;
268 int len, offset;
269 int minoffset = 0;
272 * If GIT_DIR is set explicitly, we're not going
273 * to do any discovery, but we still do repository
274 * validation.
276 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
277 if (gitdirenv) {
278 if (PATH_MAX - 40 < strlen(gitdirenv))
279 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
280 if (is_git_directory(gitdirenv)) {
281 static char buffer[1024 + 1];
282 const char *retval;
284 if (!work_tree_env)
285 return set_work_tree(gitdirenv);
286 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
287 get_git_work_tree());
288 if (!retval || !*retval)
289 return NULL;
290 set_git_dir(make_absolute_path(gitdirenv));
291 if (chdir(work_tree_env) < 0)
292 die ("Could not chdir to %s", work_tree_env);
293 strcat(buffer, "/");
294 return retval;
296 if (nongit_ok) {
297 *nongit_ok = 1;
298 return NULL;
300 die("Not a git repository: '%s'", gitdirenv);
303 if (!getcwd(cwd, sizeof(cwd)-1))
304 die("Unable to read current working directory");
305 #ifdef __MINGW32__
306 if (cwd[1] == ':')
307 minoffset = 2;
308 #endif
311 * Test in the following order (relative to the cwd):
312 * - .git/
313 * - ./ (bare)
314 * - ../.git/
315 * - ../ (bare)
316 * - ../../.git/
317 * etc.
319 offset = len = strlen(cwd);
320 for (;;) {
321 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
322 break;
323 if (is_git_directory(".")) {
324 inside_git_dir = 1;
325 if (!work_tree_env)
326 inside_work_tree = 0;
327 set_git_dir(".");
328 return NULL;
330 chdir("..");
331 do {
332 if (offset <= minoffset) {
333 if (nongit_ok) {
334 if (chdir(cwd))
335 die("Cannot come back to cwd");
336 *nongit_ok = 1;
337 return NULL;
339 die("Not a git repository");
341 } while (offset > minoffset && cwd[--offset] != '/');
344 inside_git_dir = 0;
345 if (!work_tree_env)
346 inside_work_tree = 1;
347 git_work_tree_cfg = xstrndup(cwd, offset);
348 if (offset == len)
349 return NULL;
351 /* Make "offset" point to past the '/', and add a '/' at the end */
352 offset++;
353 cwd[len++] = '/';
354 cwd[len] = 0;
355 return cwd + offset;
358 int git_config_perm(const char *var, const char *value)
360 if (value) {
361 int i;
362 if (!strcmp(value, "umask"))
363 return PERM_UMASK;
364 if (!strcmp(value, "group"))
365 return PERM_GROUP;
366 if (!strcmp(value, "all") ||
367 !strcmp(value, "world") ||
368 !strcmp(value, "everybody"))
369 return PERM_EVERYBODY;
370 i = atoi(value);
371 if (i > 1)
372 return i;
374 return git_config_bool(var, value);
377 int check_repository_format_version(const char *var, const char *value)
379 if (strcmp(var, "core.repositoryformatversion") == 0)
380 repository_format_version = git_config_int(var, value);
381 else if (strcmp(var, "core.sharedrepository") == 0)
382 shared_repository = git_config_perm(var, value);
383 else if (strcmp(var, "core.bare") == 0) {
384 is_bare_repository_cfg = git_config_bool(var, value);
385 if (is_bare_repository_cfg == 1)
386 inside_work_tree = -1;
387 } else if (strcmp(var, "core.worktree") == 0) {
388 if (git_work_tree_cfg)
389 free(git_work_tree_cfg);
390 git_work_tree_cfg = xstrdup(value);
391 inside_work_tree = -1;
393 return 0;
396 int check_repository_format(void)
398 git_config(check_repository_format_version);
399 if (GIT_REPO_VERSION < repository_format_version)
400 die ("Expected git repo version <= %d, found %d",
401 GIT_REPO_VERSION, repository_format_version);
402 return 0;
405 const char *setup_git_directory(void)
407 const char *retval = setup_git_directory_gently(NULL);
408 check_repository_format();
410 /* If the work tree is not the default one, recompute prefix */
411 if (inside_work_tree < 0) {
412 static char buffer[PATH_MAX + 1];
413 char *rel;
414 if (retval && chdir(retval))
415 die ("Could not jump back into original cwd");
416 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
417 return rel && *rel ? strcat(rel, "/") : NULL;
420 return retval;