Documentation/rev-list-options.txt: Fix missing line in example history graph
[git/dkf.git] / setup.c
blob5716d90b57574d045114f4aaad1bdf36fd79ed89
1 #include "cache.h"
2 #include "dir.h"
4 static int inside_git_dir = -1;
5 static int inside_work_tree = -1;
7 const char *prefix_path(const char *prefix, int len, const char *path)
9 const char *orig = path;
10 char *sanitized = xmalloc(len + strlen(path) + 1);
11 if (is_absolute_path(orig))
12 strcpy(sanitized, path);
13 else {
14 if (len)
15 memcpy(sanitized, prefix, len);
16 strcpy(sanitized + len, path);
18 if (normalize_path_copy(sanitized, sanitized))
19 goto error_out;
20 if (is_absolute_path(orig)) {
21 size_t root_len, len, total;
22 const char *work_tree = get_git_work_tree();
23 if (!work_tree)
24 goto error_out;
25 len = strlen(work_tree);
26 root_len = offset_1st_component(work_tree);
27 total = strlen(sanitized) + 1;
28 if (strncmp(sanitized, work_tree, len) ||
29 (len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) {
30 error_out:
31 die("'%s' is outside repository", orig);
33 if (sanitized[len] == '/')
34 len++;
35 memmove(sanitized, sanitized + len, total - len);
37 return sanitized;
41 * Unlike prefix_path, this should be used if the named file does
42 * not have to interact with index entry; i.e. name of a random file
43 * on the filesystem.
45 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
47 static char path[PATH_MAX];
48 #ifndef WIN32
49 if (!pfx || !*pfx || is_absolute_path(arg))
50 return arg;
51 memcpy(path, pfx, pfx_len);
52 strcpy(path + pfx_len, arg);
53 #else
54 char *p;
55 /* don't add prefix to absolute paths, but still replace '\' by '/' */
56 if (is_absolute_path(arg))
57 pfx_len = 0;
58 else
59 memcpy(path, pfx, pfx_len);
60 strcpy(path + pfx_len, arg);
61 for (p = path + pfx_len; *p; p++)
62 if (*p == '\\')
63 *p = '/';
64 #endif
65 return path;
68 int check_filename(const char *prefix, const char *arg)
70 const char *name;
71 struct stat st;
73 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
74 if (!lstat(name, &st))
75 return 1; /* file exists */
76 if (errno == ENOENT || errno == ENOTDIR)
77 return 0; /* file does not exist */
78 die_errno("failed to stat '%s'", arg);
81 static void NORETURN die_verify_filename(const char *prefix, const char *arg)
83 unsigned char sha1[20];
84 unsigned mode;
85 /* try a detailed diagnostic ... */
86 get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
87 /* ... or fall back the most general message. */
88 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
89 "Use '--' to separate paths from revisions", arg);
94 * Verify a filename that we got as an argument for a pathspec
95 * entry. Note that a filename that begins with "-" never verifies
96 * as true, because even if such a filename were to exist, we want
97 * it to be preceded by the "--" marker (or we want the user to
98 * use a format like "./-filename")
100 void verify_filename(const char *prefix, const char *arg)
102 if (*arg == '-')
103 die("bad flag '%s' used after filename", arg);
104 if (check_filename(prefix, arg))
105 return;
106 die_verify_filename(prefix, arg);
110 * Opposite of the above: the command line did not have -- marker
111 * and we parsed the arg as a refname. It should not be interpretable
112 * as a filename.
114 void verify_non_filename(const char *prefix, const char *arg)
116 if (!is_inside_work_tree() || is_inside_git_dir())
117 return;
118 if (*arg == '-')
119 return; /* flag */
120 if (!check_filename(prefix, arg))
121 return;
122 die("ambiguous argument '%s': both revision and filename\n"
123 "Use '--' to separate filenames from revisions", arg);
126 const char **get_pathspec(const char *prefix, const char **pathspec)
128 const char *entry = *pathspec;
129 const char **src, **dst;
130 int prefixlen;
132 if (!prefix && !entry)
133 return NULL;
135 if (!entry) {
136 static const char *spec[2];
137 spec[0] = prefix;
138 spec[1] = NULL;
139 return spec;
142 /* Otherwise we have to re-write the entries.. */
143 src = pathspec;
144 dst = pathspec;
145 prefixlen = prefix ? strlen(prefix) : 0;
146 while (*src) {
147 const char *p = prefix_path(prefix, prefixlen, *src);
148 *(dst++) = p;
149 src++;
151 *dst = NULL;
152 if (!*pathspec)
153 return NULL;
154 return pathspec;
158 * Test if it looks like we're at a git directory.
159 * We want to see:
161 * - either an objects/ directory _or_ the proper
162 * GIT_OBJECT_DIRECTORY environment variable
163 * - a refs/ directory
164 * - either a HEAD symlink or a HEAD file that is formatted as
165 * a proper "ref:", or a regular file HEAD that has a properly
166 * formatted sha1 object name.
168 static int is_git_directory(const char *suspect)
170 char path[PATH_MAX];
171 size_t len = strlen(suspect);
173 strcpy(path, suspect);
174 if (getenv(DB_ENVIRONMENT)) {
175 if (access(getenv(DB_ENVIRONMENT), X_OK))
176 return 0;
178 else {
179 strcpy(path + len, "/objects");
180 if (access(path, X_OK))
181 return 0;
184 strcpy(path + len, "/refs");
185 if (access(path, X_OK))
186 return 0;
188 strcpy(path + len, "/HEAD");
189 if (validate_headref(path))
190 return 0;
192 return 1;
195 int is_inside_git_dir(void)
197 if (inside_git_dir < 0)
198 inside_git_dir = is_inside_dir(get_git_dir());
199 return inside_git_dir;
202 int is_inside_work_tree(void)
204 if (inside_work_tree < 0)
205 inside_work_tree = is_inside_dir(get_git_work_tree());
206 return inside_work_tree;
210 * set_work_tree() is only ever called if you set GIT_DIR explicitly.
211 * The old behaviour (which we retain here) is to set the work tree root
212 * to the cwd, unless overridden by the config, the command line, or
213 * GIT_WORK_TREE.
215 static const char *set_work_tree(const char *dir)
217 char buffer[PATH_MAX + 1];
219 if (!getcwd(buffer, sizeof(buffer)))
220 die ("Could not get the current working directory");
221 git_work_tree_cfg = xstrdup(buffer);
222 inside_work_tree = 1;
224 return NULL;
227 void setup_work_tree(void)
229 const char *work_tree, *git_dir;
230 static int initialized = 0;
232 if (initialized)
233 return;
234 work_tree = get_git_work_tree();
235 git_dir = get_git_dir();
236 if (!is_absolute_path(git_dir))
237 git_dir = make_absolute_path(git_dir);
238 if (!work_tree || chdir(work_tree))
239 die("This operation must be run in a work tree");
240 set_git_dir(make_relative_path(git_dir, work_tree));
241 initialized = 1;
244 static int check_repository_format_gently(int *nongit_ok)
246 git_config(check_repository_format_version, NULL);
247 if (GIT_REPO_VERSION < repository_format_version) {
248 if (!nongit_ok)
249 die ("Expected git repo version <= %d, found %d",
250 GIT_REPO_VERSION, repository_format_version);
251 warning("Expected git repo version <= %d, found %d",
252 GIT_REPO_VERSION, repository_format_version);
253 warning("Please upgrade Git");
254 *nongit_ok = -1;
255 return -1;
257 return 0;
261 * Try to read the location of the git directory from the .git file,
262 * return path to git directory if found.
264 const char *read_gitfile_gently(const char *path)
266 char *buf;
267 char *dir;
268 const char *slash;
269 struct stat st;
270 int fd;
271 size_t len;
273 if (stat(path, &st))
274 return NULL;
275 if (!S_ISREG(st.st_mode))
276 return NULL;
277 fd = open(path, O_RDONLY);
278 if (fd < 0)
279 die_errno("Error opening '%s'", path);
280 buf = xmalloc(st.st_size + 1);
281 len = read_in_full(fd, buf, st.st_size);
282 close(fd);
283 if (len != st.st_size)
284 die("Error reading %s", path);
285 buf[len] = '\0';
286 if (prefixcmp(buf, "gitdir: "))
287 die("Invalid gitfile format: %s", path);
288 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
289 len--;
290 if (len < 9)
291 die("No path in gitfile: %s", path);
292 buf[len] = '\0';
293 dir = buf + 8;
295 if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
296 size_t pathlen = slash+1 - path;
297 size_t dirlen = pathlen + len - 8;
298 dir = xmalloc(dirlen + 1);
299 strncpy(dir, path, pathlen);
300 strncpy(dir + pathlen, buf + 8, len - 8);
301 dir[dirlen] = '\0';
302 free(buf);
303 buf = dir;
306 if (!is_git_directory(dir))
307 die("Not a git repository: %s", dir);
308 path = make_absolute_path(dir);
310 free(buf);
311 return path;
315 * We cannot decide in this function whether we are in the work tree or
316 * not, since the config can only be read _after_ this function was called.
318 const char *setup_git_directory_gently(int *nongit_ok)
320 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
321 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
322 static char cwd[PATH_MAX+1];
323 const char *gitdirenv;
324 const char *gitfile_dir;
325 int len, offset, ceil_offset, root_len;
328 * Let's assume that we are in a git repository.
329 * If it turns out later that we are somewhere else, the value will be
330 * updated accordingly.
332 if (nongit_ok)
333 *nongit_ok = 0;
336 * If GIT_DIR is set explicitly, we're not going
337 * to do any discovery, but we still do repository
338 * validation.
340 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
341 if (gitdirenv) {
342 if (PATH_MAX - 40 < strlen(gitdirenv))
343 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
344 if (is_git_directory(gitdirenv)) {
345 static char buffer[1024 + 1];
346 const char *retval;
348 if (!work_tree_env) {
349 retval = set_work_tree(gitdirenv);
350 /* config may override worktree */
351 if (check_repository_format_gently(nongit_ok))
352 return NULL;
353 return retval;
355 if (check_repository_format_gently(nongit_ok))
356 return NULL;
357 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
358 get_git_work_tree());
359 if (!retval || !*retval)
360 return NULL;
361 set_git_dir(make_absolute_path(gitdirenv));
362 if (chdir(work_tree_env) < 0)
363 die_errno ("Could not chdir to '%s'", work_tree_env);
364 strcat(buffer, "/");
365 return retval;
367 if (nongit_ok) {
368 *nongit_ok = 1;
369 return NULL;
371 die("Not a git repository: '%s'", gitdirenv);
374 if (!getcwd(cwd, sizeof(cwd)-1))
375 die_errno("Unable to read current working directory");
377 ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
378 if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
379 ceil_offset = 1;
382 * Test in the following order (relative to the cwd):
383 * - .git (file containing "gitdir: <path>")
384 * - .git/
385 * - ./ (bare)
386 * - ../.git
387 * - ../.git/
388 * - ../ (bare)
389 * - ../../.git/
390 * etc.
392 offset = len = strlen(cwd);
393 for (;;) {
394 gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
395 if (gitfile_dir) {
396 if (set_git_dir(gitfile_dir))
397 die("Repository setup failed");
398 break;
400 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
401 break;
402 if (is_git_directory(".")) {
403 inside_git_dir = 1;
404 if (!work_tree_env)
405 inside_work_tree = 0;
406 if (offset != len) {
407 root_len = offset_1st_component(cwd);
408 cwd[offset > root_len ? offset : root_len] = '\0';
409 set_git_dir(cwd);
410 } else
411 set_git_dir(".");
412 check_repository_format_gently(nongit_ok);
413 return NULL;
415 while (--offset > ceil_offset && cwd[offset] != '/');
416 if (offset <= ceil_offset) {
417 if (nongit_ok) {
418 if (chdir(cwd))
419 die_errno("Cannot come back to cwd");
420 *nongit_ok = 1;
421 return NULL;
423 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
425 if (chdir(".."))
426 die_errno("Cannot change to '%s/..'", cwd);
429 inside_git_dir = 0;
430 if (!work_tree_env)
431 inside_work_tree = 1;
432 root_len = offset_1st_component(cwd);
433 git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
434 if (check_repository_format_gently(nongit_ok))
435 return NULL;
436 if (offset == len)
437 return NULL;
439 /* Make "offset" point to past the '/', and add a '/' at the end */
440 offset++;
441 cwd[len++] = '/';
442 cwd[len] = 0;
443 return cwd + offset;
446 int git_config_perm(const char *var, const char *value)
448 int i;
449 char *endptr;
451 if (value == NULL)
452 return PERM_GROUP;
454 if (!strcmp(value, "umask"))
455 return PERM_UMASK;
456 if (!strcmp(value, "group"))
457 return PERM_GROUP;
458 if (!strcmp(value, "all") ||
459 !strcmp(value, "world") ||
460 !strcmp(value, "everybody"))
461 return PERM_EVERYBODY;
463 /* Parse octal numbers */
464 i = strtol(value, &endptr, 8);
466 /* If not an octal number, maybe true/false? */
467 if (*endptr != 0)
468 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
471 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
472 * a chmod value to restrict to.
474 switch (i) {
475 case PERM_UMASK: /* 0 */
476 return PERM_UMASK;
477 case OLD_PERM_GROUP: /* 1 */
478 return PERM_GROUP;
479 case OLD_PERM_EVERYBODY: /* 2 */
480 return PERM_EVERYBODY;
483 /* A filemode value was given: 0xxx */
485 if ((i & 0600) != 0600)
486 die("Problem with core.sharedRepository filemode value "
487 "(0%.3o).\nThe owner of files must always have "
488 "read and write permissions.", i);
491 * Mask filemode value. Others can not get write permission.
492 * x flags for directories are handled separately.
494 return -(i & 0666);
497 int check_repository_format_version(const char *var, const char *value, void *cb)
499 if (strcmp(var, "core.repositoryformatversion") == 0)
500 repository_format_version = git_config_int(var, value);
501 else if (strcmp(var, "core.sharedrepository") == 0)
502 shared_repository = git_config_perm(var, value);
503 else if (strcmp(var, "core.bare") == 0) {
504 is_bare_repository_cfg = git_config_bool(var, value);
505 if (is_bare_repository_cfg == 1)
506 inside_work_tree = -1;
507 } else if (strcmp(var, "core.worktree") == 0) {
508 if (!value)
509 return config_error_nonbool(var);
510 free(git_work_tree_cfg);
511 git_work_tree_cfg = xstrdup(value);
512 inside_work_tree = -1;
514 return 0;
517 int check_repository_format(void)
519 return check_repository_format_gently(NULL);
522 const char *setup_git_directory(void)
524 const char *retval = setup_git_directory_gently(NULL);
526 /* If the work tree is not the default one, recompute prefix */
527 if (inside_work_tree < 0) {
528 static char buffer[PATH_MAX + 1];
529 char *rel;
530 if (retval && chdir(retval))
531 die_errno ("Could not jump back into original cwd");
532 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
533 if (rel && *rel && chdir(get_git_work_tree()))
534 die_errno ("Could not jump to working directory");
535 return rel && *rel ? strcat(rel, "/") : NULL;
538 return retval;