tweak manpage formatting
[git/git.git] / setup.c
CommitLineData
d288a700 1#include "cache.h"
e90fdc39
JS
2#include "dir.h"
3
4static int inside_git_dir = -1;
5static int inside_work_tree = -1;
d288a700 6
6b5ee137 7const char *prefix_path(const char *prefix, int len, const char *path)
f332726e 8{
6b5ee137 9 const char *orig = path;
f332726e
LT
10 for (;;) {
11 char c;
12 if (*path != '.')
13 break;
14 c = path[1];
15 /* "." */
16 if (!c) {
17 path++;
18 break;
19 }
20 /* "./" */
21 if (c == '/') {
22 path += 2;
23 continue;
24 }
25 if (c != '.')
26 break;
27 c = path[2];
28 if (!c)
29 path += 2;
30 else if (c == '/')
31 path += 3;
32 else
33 break;
34 /* ".." and "../" */
35 /* Remove last component of the prefix */
36 do {
37 if (!len)
38 die("'%s' is outside repository", orig);
39 len--;
40 } while (len && prefix[len-1] != '/');
41 continue;
42 }
43 if (len) {
44 int speclen = strlen(path);
45 char *n = xmalloc(speclen + len + 1);
a6080a0a 46
f332726e
LT
47 memcpy(n, prefix, len);
48 memcpy(n + len, path, speclen+1);
49 path = n;
50 }
51 return path;
52}
53
a6080a0a 54/*
4ca06608
JH
55 * Unlike prefix_path, this should be used if the named file does
56 * not have to interact with index entry; i.e. name of a random file
57 * on the filesystem.
58 */
59const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
60{
61 static char path[PATH_MAX];
62 if (!pfx || !*pfx || arg[0] == '/')
63 return arg;
64 memcpy(path, pfx, pfx_len);
65 strcpy(path + pfx_len, arg);
66 return path;
67}
68
e23d0b4a
LT
69/*
70 * Verify a filename that we got as an argument for a pathspec
71 * entry. Note that a filename that begins with "-" never verifies
72 * as true, because even if such a filename were to exist, we want
73 * it to be preceded by the "--" marker (or we want the user to
74 * use a format like "./-filename")
75 */
76void verify_filename(const char *prefix, const char *arg)
77{
78 const char *name;
79 struct stat st;
80
81 if (*arg == '-')
82 die("bad flag '%s' used after filename", arg);
83 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
84 if (!lstat(name, &st))
85 return;
86 if (errno == ENOENT)
ea92f41f
JH
87 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
88 "Use '--' to separate paths from revisions", arg);
e23d0b4a
LT
89 die("'%s': %s", arg, strerror(errno));
90}
91
ea92f41f
JH
92/*
93 * Opposite of the above: the command line did not have -- marker
94 * and we parsed the arg as a refname. It should not be interpretable
95 * as a filename.
96 */
97void verify_non_filename(const char *prefix, const char *arg)
98{
99 const char *name;
100 struct stat st;
101
7ae3df8c 102 if (!is_inside_work_tree() || is_inside_git_dir())
68025633 103 return;
ea92f41f
JH
104 if (*arg == '-')
105 return; /* flag */
106 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
107 if (!lstat(name, &st))
108 die("ambiguous argument '%s': both revision and filename\n"
109 "Use '--' to separate filenames from revisions", arg);
33a798c8 110 if (errno != ENOENT && errno != ENOTDIR)
ea92f41f
JH
111 die("'%s': %s", arg, strerror(errno));
112}
113
6b5ee137 114const char **get_pathspec(const char *prefix, const char **pathspec)
d288a700 115{
6b5ee137
JH
116 const char *entry = *pathspec;
117 const char **p;
d288a700
LT
118 int prefixlen;
119
f332726e
LT
120 if (!prefix && !entry)
121 return NULL;
d288a700
LT
122
123 if (!entry) {
124 static const char *spec[2];
125 spec[0] = prefix;
126 spec[1] = NULL;
127 return spec;
128 }
129
130 /* Otherwise we have to re-write the entries.. */
d288a700 131 p = pathspec;
f332726e 132 prefixlen = prefix ? strlen(prefix) : 0;
d288a700 133 do {
f332726e 134 *p = prefix_path(prefix, prefixlen, entry);
d288a700
LT
135 } while ((entry = *++p) != NULL);
136 return (const char **) pathspec;
137}
138
5f5608bc 139/*
ad1a382f 140 * Test if it looks like we're at a git directory.
5e7bfe25 141 * We want to see:
5f5608bc 142 *
ad1a382f 143 * - either a objects/ directory _or_ the proper
5f5608bc 144 * GIT_OBJECT_DIRECTORY environment variable
ad1a382f 145 * - a refs/ directory
8098a178 146 * - either a HEAD symlink or a HEAD file that is formatted as
c847f537
JH
147 * a proper "ref:", or a regular file HEAD that has a properly
148 * formatted sha1 object name.
5f5608bc 149 */
ad1a382f 150static int is_git_directory(const char *suspect)
5f5608bc 151{
ad1a382f
SP
152 char path[PATH_MAX];
153 size_t len = strlen(suspect);
154
155 strcpy(path, suspect);
156 if (getenv(DB_ENVIRONMENT)) {
157 if (access(getenv(DB_ENVIRONMENT), X_OK))
158 return 0;
159 }
160 else {
161 strcpy(path + len, "/objects");
162 if (access(path, X_OK))
163 return 0;
164 }
165
166 strcpy(path + len, "/refs");
167 if (access(path, X_OK))
8098a178 168 return 0;
ad1a382f
SP
169
170 strcpy(path + len, "/HEAD");
c847f537 171 if (validate_headref(path))
ad1a382f
SP
172 return 0;
173
8098a178 174 return 1;
5f5608bc
LT
175}
176
68025633
JS
177int is_inside_git_dir(void)
178{
e90fdc39
JS
179 if (inside_git_dir < 0)
180 inside_git_dir = is_inside_dir(get_git_dir());
181 return inside_git_dir;
892c41b9
ML
182}
183
892c41b9
ML
184int is_inside_work_tree(void)
185{
e90fdc39
JS
186 if (inside_work_tree < 0)
187 inside_work_tree = is_inside_dir(get_git_work_tree());
188 return inside_work_tree;
892c41b9
ML
189}
190
e90fdc39
JS
191/*
192 * If no worktree was given, and we are outside of a default work tree,
193 * now is the time to set it.
194 *
195 * In other words, if the user calls git with something like
196 *
197 * git --git-dir=/some/where/else/.git bla
198 *
199 * default to /some/where/else as working directory; if the specified
200 * git-dir does not end in "/.git", the cwd is used as working directory.
201 */
202const char *set_work_tree(const char *dir)
892c41b9 203{
6490a338
JH
204 char dir_buffer[PATH_MAX], *rel = NULL;
205 static char buffer[PATH_MAX + 1];
206 int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1;
e90fdc39
JS
207
208 /* strip the variable 'dir' of the postfix "/.git" if it has it */
209 len = strlen(dir);
6490a338
JH
210 if (len > suffix_len &&
211 !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
212 if ((len - suffix_len) >= sizeof(dir_buffer))
213 die("directory name too long");
214 memcpy(dir_buffer, dir, len - suffix_len);
215 dir_buffer[len - suffix_len] = '\0';
e90fdc39
JS
216
217 /* are we inside the default work tree? */
218 rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer);
219 }
6490a338 220
e90fdc39
JS
221 /* if rel is set, the cwd is _not_ the current working tree */
222 if (rel && *rel) {
223 if (!is_absolute_path(dir))
224 set_git_dir(make_absolute_path(dir));
225 dir = dir_buffer;
6490a338
JH
226 if (chdir(dir))
227 die("cannot chdir to %s: %s", dir, strerror(errno));
228 else
229 strcat(rel, "/");
e90fdc39
JS
230 inside_git_dir = 0;
231 } else {
232 rel = NULL;
233 dir = getcwd(buffer, sizeof(buffer));
68025633 234 }
e90fdc39
JS
235 git_work_tree_cfg = xstrdup(dir);
236 inside_work_tree = 1;
237
238 return rel;
68025633
JS
239}
240
e90fdc39
JS
241/*
242 * We cannot decide in this function whether we are in the work tree or
243 * not, since the config can only be read _after_ this function was called.
244 */
4ca06608 245const char *setup_git_directory_gently(int *nongit_ok)
d288a700 246{
e90fdc39 247 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
d288a700 248 static char cwd[PATH_MAX+1];
e90fdc39
JS
249 const char *gitdirenv;
250 int len, offset;
d288a700 251
e90fdc39
JS
252 /*
253 * If GIT_DIR is set explicitly, we're not going
254 * to do any discovery, but we still do repository
255 * validation.
256 */
ad1a382f 257 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
e90fdc39
JS
258 if (gitdirenv) {
259 if (PATH_MAX - 40 < strlen(gitdirenv))
260 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
261 if (is_git_directory(gitdirenv)) {
262 if (!work_tree_env)
263 return set_work_tree(gitdirenv);
ad1a382f 264 return NULL;
892c41b9 265 }
3a3c3fc4 266 if (nongit_ok) {
41e95f69
ML
267 *nongit_ok = 1;
268 return NULL;
269 }
ad1a382f 270 die("Not a git repository: '%s'", gitdirenv);
5e7bfe25 271 }
d288a700 272
f66a4d68 273 if (!getcwd(cwd, sizeof(cwd)-1))
d288a700
LT
274 die("Unable to read current working directory");
275
892c41b9 276 /*
e90fdc39
JS
277 * Test in the following order (relative to the cwd):
278 * - .git/
279 * - ./ (bare)
280 * - ../.git/
281 * - ../ (bare)
282 * - ../../.git/
283 * etc.
892c41b9 284 */
e90fdc39
JS
285 offset = len = strlen(cwd);
286 for (;;) {
287 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
288 break;
289 if (is_git_directory(".")) {
290 inside_git_dir = 1;
291 if (!work_tree_env)
292 inside_work_tree = 0;
293 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
892c41b9
ML
294 return NULL;
295 }
e90fdc39
JS
296 chdir("..");
297 do {
298 if (!offset) {
299 if (nongit_ok) {
300 if (chdir(cwd))
301 die("Cannot come back to cwd");
302 *nongit_ok = 1;
303 return NULL;
304 }
305 die("Not a git repository");
306 }
307 } while (cwd[--offset] != '/');
892c41b9
ML
308 }
309
e90fdc39
JS
310 inside_git_dir = 0;
311 if (!work_tree_env)
312 inside_work_tree = 1;
313 git_work_tree_cfg = xstrndup(cwd, offset);
314 if (offset == len)
d288a700
LT
315 return NULL;
316
e90fdc39
JS
317 /* Make "offset" point to past the '/', and add a '/' at the end */
318 offset++;
319 cwd[len++] = '/';
320 cwd[len] = 0;
321 return cwd + offset;
d288a700 322}
5e7bfe25 323
94df2506
JH
324int git_config_perm(const char *var, const char *value)
325{
326 if (value) {
83525227 327 int i;
94df2506
JH
328 if (!strcmp(value, "umask"))
329 return PERM_UMASK;
330 if (!strcmp(value, "group"))
331 return PERM_GROUP;
332 if (!strcmp(value, "all") ||
333 !strcmp(value, "world") ||
334 !strcmp(value, "everybody"))
335 return PERM_EVERYBODY;
83525227
JS
336 i = atoi(value);
337 if (i > 1)
338 return i;
94df2506
JH
339 }
340 return git_config_bool(var, value);
341}
342
ab9cb76f
JH
343int check_repository_format_version(const char *var, const char *value)
344{
299726d5
JS
345 if (strcmp(var, "core.repositoryformatversion") == 0)
346 repository_format_version = git_config_int(var, value);
457f06d6 347 else if (strcmp(var, "core.sharedrepository") == 0)
94df2506 348 shared_repository = git_config_perm(var, value);
e90fdc39
JS
349 else if (strcmp(var, "core.bare") == 0) {
350 is_bare_repository_cfg = git_config_bool(var, value);
351 if (is_bare_repository_cfg == 1)
352 inside_work_tree = -1;
353 } else if (strcmp(var, "core.worktree") == 0) {
354 if (git_work_tree_cfg)
355 free(git_work_tree_cfg);
356 git_work_tree_cfg = xstrdup(value);
357 inside_work_tree = -1;
358 }
299726d5 359 return 0;
ab9cb76f
JH
360}
361
362int check_repository_format(void)
363{
364 git_config(check_repository_format_version);
365 if (GIT_REPO_VERSION < repository_format_version)
366 die ("Expected git repo version <= %d, found %d",
367 GIT_REPO_VERSION, repository_format_version);
368 return 0;
369}
370
5e7bfe25
JH
371const char *setup_git_directory(void)
372{
4ca06608 373 const char *retval = setup_git_directory_gently(NULL);
22752e4c 374 check_repository_format();
e90fdc39
JS
375
376 /* If the work tree is not the default one, recompute prefix */
377 if (inside_work_tree < 0) {
378 static char buffer[PATH_MAX + 1];
379 char *rel;
380 if (retval && chdir(retval))
381 die ("Could not jump back into original cwd");
382 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
383 return rel && *rel ? strcat(rel, "/") : NULL;
384 }
385
5e7bfe25
JH
386 return retval;
387}