gitk: force focus to main window
[git/platforms.git] / setup.c
blobb2672784ed357f6a3b4b8e0b3fc9e401f69dbd09
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 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
230 * The old behaviour (which we retain here) is to set the work tree root
231 * to the cwd, unless overridden by the config, the command line, or
232 * GIT_WORK_TREE.
234 static const char *set_work_tree(const char *dir)
236 char buffer[PATH_MAX + 1];
238 if (!getcwd(buffer, sizeof(buffer)))
239 die ("Could not get the current working directory");
240 git_work_tree_cfg = xstrdup(buffer);
241 inside_work_tree = 1;
243 return NULL;
247 * We cannot decide in this function whether we are in the work tree or
248 * not, since the config can only be read _after_ this function was called.
250 const char *setup_git_directory_gently(int *nongit_ok)
252 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
253 static char cwd[PATH_MAX+1];
254 const char *gitdirenv;
255 int len, offset, minoffset = 0;
258 * If GIT_DIR is set explicitly, we're not going
259 * to do any discovery, but we still do repository
260 * validation.
262 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
263 if (gitdirenv) {
264 if (PATH_MAX - 40 < strlen(gitdirenv))
265 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
266 if (is_git_directory(gitdirenv)) {
267 if (!work_tree_env)
268 return set_work_tree(gitdirenv);
269 return NULL;
271 if (nongit_ok) {
272 *nongit_ok = 1;
273 return NULL;
275 die("Not a git repository: '%s'", gitdirenv);
278 if (!getcwd(cwd, sizeof(cwd)-1))
279 die("Unable to read current working directory");
281 #ifdef __MINGW32__
282 if (cwd[1] == ':')
283 minoffset = 2;
284 #endif
287 * Test in the following order (relative to the cwd):
288 * - .git/
289 * - ./ (bare)
290 * - ../.git/
291 * - ../ (bare)
292 * - ../../.git/
293 * etc.
295 offset = len = strlen(cwd);
296 for (;;) {
297 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
298 break;
299 if (is_git_directory(".")) {
300 inside_git_dir = 1;
301 if (!work_tree_env)
302 inside_work_tree = 0;
303 set_git_dir(".");
304 return NULL;
306 chdir("..");
307 do {
308 if (offset == minoffset) {
309 if (nongit_ok) {
310 if (chdir(cwd))
311 die("Cannot come back to cwd");
312 *nongit_ok = 1;
313 return NULL;
315 die("Not a git repository");
317 } while (cwd[--offset] != '/');
320 inside_git_dir = 0;
321 if (!work_tree_env)
322 inside_work_tree = 1;
323 git_work_tree_cfg = xstrndup(cwd, offset);
324 if (offset == len)
325 return NULL;
327 /* Make "offset" point to past the '/', and add a '/' at the end */
328 offset++;
329 cwd[len++] = '/';
330 cwd[len] = 0;
331 return cwd + offset;
334 int git_config_perm(const char *var, const char *value)
336 if (value) {
337 int i;
338 if (!strcmp(value, "umask"))
339 return PERM_UMASK;
340 if (!strcmp(value, "group"))
341 return PERM_GROUP;
342 if (!strcmp(value, "all") ||
343 !strcmp(value, "world") ||
344 !strcmp(value, "everybody"))
345 return PERM_EVERYBODY;
346 i = atoi(value);
347 if (i > 1)
348 return i;
350 return git_config_bool(var, value);
353 int check_repository_format_version(const char *var, const char *value)
355 if (strcmp(var, "core.repositoryformatversion") == 0)
356 repository_format_version = git_config_int(var, value);
357 else if (strcmp(var, "core.sharedrepository") == 0)
358 shared_repository = git_config_perm(var, value);
359 else if (strcmp(var, "core.bare") == 0) {
360 is_bare_repository_cfg = git_config_bool(var, value);
361 if (is_bare_repository_cfg == 1)
362 inside_work_tree = -1;
363 } else if (strcmp(var, "core.worktree") == 0) {
364 if (git_work_tree_cfg)
365 free(git_work_tree_cfg);
366 git_work_tree_cfg = xstrdup(value);
367 inside_work_tree = -1;
369 return 0;
372 int check_repository_format(void)
374 git_config(check_repository_format_version);
375 if (GIT_REPO_VERSION < repository_format_version)
376 die ("Expected git repo version <= %d, found %d",
377 GIT_REPO_VERSION, repository_format_version);
378 return 0;
381 const char *setup_git_directory(void)
383 const char *retval = setup_git_directory_gently(NULL);
384 check_repository_format();
386 /* If the work tree is not the default one, recompute prefix */
387 if (inside_work_tree < 0) {
388 static char buffer[PATH_MAX + 1];
389 char *rel;
390 if (retval && chdir(retval))
391 die ("Could not jump back into original cwd");
392 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
393 return rel && *rel ? strcat(rel, "/") : NULL;
396 return retval;