4 #include "string-list.h"
6 static int inside_git_dir
= -1;
7 static int inside_work_tree
= -1;
8 static int work_tree_config_is_bogus
;
10 static struct startup_info the_startup_info
;
11 struct startup_info
*startup_info
= &the_startup_info
;
14 * The input parameter must contain an absolute path, and it must already be
17 * Find the part of an absolute path that lies inside the work tree by
18 * dereferencing symlinks outside the work tree, for example:
19 * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
20 * /dir/file (work tree is /) -> dir/file
21 * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
22 * /dir/repolink/file (repolink points to /dir/repo) -> file
23 * /dir/repo (exactly equal to work tree) -> (empty string)
25 static int abspath_part_inside_repo(char *path
)
31 const char *work_tree
= get_git_work_tree();
35 wtlen
= strlen(work_tree
);
37 off
= offset_1st_component(path
);
39 /* check if work tree is already the prefix */
40 if (wtlen
<= len
&& !strncmp(path
, work_tree
, wtlen
)) {
41 if (path
[wtlen
] == '/') {
42 memmove(path
, path
+ wtlen
+ 1, len
- wtlen
);
44 } else if (path
[wtlen
- 1] == '/' || path
[wtlen
] == '\0') {
45 /* work tree is the root, or the whole path */
46 memmove(path
, path
+ wtlen
, len
- wtlen
+ 1);
49 /* work tree might match beginning of a symlink to work tree */
55 /* check each '/'-terminated level */
60 if (strcmp(real_path(path0
), work_tree
) == 0) {
61 memmove(path0
, path
+ 1, len
- (path
- path0
));
68 /* check whole path */
69 if (strcmp(real_path(path0
), work_tree
) == 0) {
78 * Normalize "path", prepending the "prefix" for relative paths. If
79 * remaining_prefix is not NULL, return the actual prefix still
80 * remains in the path. For example, prefix = sub1/sub2/ and path is
82 * foo -> sub1/sub2/foo (full prefix)
83 * ../foo -> sub1/foo (remaining prefix is sub1/)
84 * ../../bar -> bar (no remaining prefix)
85 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
86 * `pwd`/../bar -> sub1/bar (no remaining prefix)
88 char *prefix_path_gently(const char *prefix
, int len
,
89 int *remaining_prefix
, const char *path
)
91 const char *orig
= path
;
93 if (is_absolute_path(orig
)) {
94 sanitized
= xmallocz(strlen(path
));
96 *remaining_prefix
= 0;
97 if (normalize_path_copy_len(sanitized
, path
, remaining_prefix
)) {
101 if (abspath_part_inside_repo(sanitized
)) {
106 sanitized
= xstrfmt("%.*s%s", len
, len
? prefix
: "", path
);
107 if (remaining_prefix
)
108 *remaining_prefix
= len
;
109 if (normalize_path_copy_len(sanitized
, sanitized
, remaining_prefix
)) {
117 char *prefix_path(const char *prefix
, int len
, const char *path
)
119 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
121 die("'%s' is outside repository", path
);
125 int path_inside_repo(const char *prefix
, const char *path
)
127 int len
= prefix
? strlen(prefix
) : 0;
128 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
136 int check_filename(const char *prefix
, const char *arg
)
139 char *to_free
= NULL
;
142 if (starts_with(arg
, ":/")) {
143 if (arg
[2] == '\0') /* ":/" is root dir, always exists */
147 name
= to_free
= prefix_filename(prefix
, arg
);
150 if (!lstat(name
, &st
)) {
152 return 1; /* file exists */
154 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
156 return 0; /* file does not exist */
158 die_errno("failed to stat '%s'", arg
);
161 static void NORETURN
die_verify_filename(const char *prefix
,
163 int diagnose_misspelt_rev
)
165 if (!diagnose_misspelt_rev
)
166 die(_("%s: no such path in the working tree.\n"
167 "Use 'git <command> -- <path>...' to specify paths that do not exist locally."),
170 * Saying "'(icase)foo' does not exist in the index" when the
171 * user gave us ":(icase)foo" is just stupid. A magic pathspec
172 * begins with a colon and is followed by a non-alnum; do not
173 * let maybe_die_on_misspelt_object_name() even trigger.
175 if (!(arg
[0] == ':' && !isalnum(arg
[1])))
176 maybe_die_on_misspelt_object_name(arg
, prefix
);
178 /* ... or fall back the most general message. */
179 die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
180 "Use '--' to separate paths from revisions, like this:\n"
181 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
186 * Verify a filename that we got as an argument for a pathspec
187 * entry. Note that a filename that begins with "-" never verifies
188 * as true, because even if such a filename were to exist, we want
189 * it to be preceded by the "--" marker (or we want the user to
190 * use a format like "./-filename")
192 * The "diagnose_misspelt_rev" is used to provide a user-friendly
193 * diagnosis when dying upon finding that "name" is not a pathname.
194 * If set to 1, the diagnosis will try to diagnose "name" as an
195 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
196 * will only complain about an inexisting file.
198 * This function is typically called to check that a "file or rev"
199 * argument is unambiguous. In this case, the caller will want
200 * diagnose_misspelt_rev == 1 when verifying the first non-rev
201 * argument (which could have been a revision), and
202 * diagnose_misspelt_rev == 0 for the next ones (because we already
203 * saw a filename, there's not ambiguity anymore).
205 void verify_filename(const char *prefix
,
207 int diagnose_misspelt_rev
)
210 die("bad flag '%s' used after filename", arg
);
211 if (check_filename(prefix
, arg
) || !no_wildcard(arg
))
213 die_verify_filename(prefix
, arg
, diagnose_misspelt_rev
);
217 * Opposite of the above: the command line did not have -- marker
218 * and we parsed the arg as a refname. It should not be interpretable
221 void verify_non_filename(const char *prefix
, const char *arg
)
223 if (!is_inside_work_tree() || is_inside_git_dir())
227 if (!check_filename(prefix
, arg
))
229 die(_("ambiguous argument '%s': both revision and filename\n"
230 "Use '--' to separate paths from revisions, like this:\n"
231 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
234 int get_common_dir(struct strbuf
*sb
, const char *gitdir
)
236 const char *git_env_common_dir
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
237 if (git_env_common_dir
) {
238 strbuf_addstr(sb
, git_env_common_dir
);
241 return get_common_dir_noenv(sb
, gitdir
);
245 int get_common_dir_noenv(struct strbuf
*sb
, const char *gitdir
)
247 struct strbuf data
= STRBUF_INIT
;
248 struct strbuf path
= STRBUF_INIT
;
251 strbuf_addf(&path
, "%s/commondir", gitdir
);
252 if (file_exists(path
.buf
)) {
253 if (strbuf_read_file(&data
, path
.buf
, 0) <= 0)
254 die_errno(_("failed to read %s"), path
.buf
);
255 while (data
.len
&& (data
.buf
[data
.len
- 1] == '\n' ||
256 data
.buf
[data
.len
- 1] == '\r'))
258 data
.buf
[data
.len
] = '\0';
260 if (!is_absolute_path(data
.buf
))
261 strbuf_addf(&path
, "%s/", gitdir
);
262 strbuf_addbuf(&path
, &data
);
263 strbuf_add_real_path(sb
, path
.buf
);
266 strbuf_addstr(sb
, gitdir
);
269 strbuf_release(&data
);
270 strbuf_release(&path
);
275 * Test if it looks like we're at a git directory.
278 * - either an objects/ directory _or_ the proper
279 * GIT_OBJECT_DIRECTORY environment variable
280 * - a refs/ directory
281 * - either a HEAD symlink or a HEAD file that is formatted as
282 * a proper "ref:", or a regular file HEAD that has a properly
283 * formatted sha1 object name.
285 int is_git_directory(const char *suspect
)
287 struct strbuf path
= STRBUF_INIT
;
291 /* Check worktree-related signatures */
292 strbuf_addf(&path
, "%s/HEAD", suspect
);
293 if (validate_headref(path
.buf
))
297 get_common_dir(&path
, suspect
);
300 /* Check non-worktree-related signatures */
301 if (getenv(DB_ENVIRONMENT
)) {
302 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
306 strbuf_setlen(&path
, len
);
307 strbuf_addstr(&path
, "/objects");
308 if (access(path
.buf
, X_OK
))
312 strbuf_setlen(&path
, len
);
313 strbuf_addstr(&path
, "/refs");
314 if (access(path
.buf
, X_OK
))
319 strbuf_release(&path
);
323 int is_nonbare_repository_dir(struct strbuf
*path
)
327 size_t orig_path_len
= path
->len
;
328 assert(orig_path_len
!= 0);
329 strbuf_complete(path
, '/');
330 strbuf_addstr(path
, ".git");
331 if (read_gitfile_gently(path
->buf
, &gitfile_error
) || is_git_directory(path
->buf
))
333 if (gitfile_error
== READ_GITFILE_ERR_OPEN_FAILED
||
334 gitfile_error
== READ_GITFILE_ERR_READ_FAILED
)
336 strbuf_setlen(path
, orig_path_len
);
340 int is_inside_git_dir(void)
342 if (inside_git_dir
< 0)
343 inside_git_dir
= is_inside_dir(get_git_dir());
344 return inside_git_dir
;
347 int is_inside_work_tree(void)
349 if (inside_work_tree
< 0)
350 inside_work_tree
= is_inside_dir(get_git_work_tree());
351 return inside_work_tree
;
354 void setup_work_tree(void)
356 const char *work_tree
, *git_dir
;
357 static int initialized
= 0;
362 if (work_tree_config_is_bogus
)
363 die("unable to set up work tree using invalid config");
365 work_tree
= get_git_work_tree();
366 git_dir
= get_git_dir();
367 if (!is_absolute_path(git_dir
))
368 git_dir
= real_path(get_git_dir());
369 if (!work_tree
|| chdir(work_tree
))
370 die("This operation must be run in a work tree");
373 * Make sure subsequent git processes find correct worktree
374 * if $GIT_WORK_TREE is set relative
376 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
377 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
379 set_git_dir(remove_leading_path(git_dir
, work_tree
));
383 static int check_repo_format(const char *var
, const char *value
, void *vdata
)
385 struct repository_format
*data
= vdata
;
388 if (strcmp(var
, "core.repositoryformatversion") == 0)
389 data
->version
= git_config_int(var
, value
);
390 else if (skip_prefix(var
, "extensions.", &ext
)) {
392 * record any known extensions here; otherwise,
393 * we fall through to recording it as unknown, and
394 * check_repository_format will complain
396 if (!strcmp(ext
, "noop"))
398 else if (!strcmp(ext
, "preciousobjects"))
399 data
->precious_objects
= git_config_bool(var
, value
);
401 string_list_append(&data
->unknown_extensions
, ext
);
402 } else if (strcmp(var
, "core.bare") == 0) {
403 data
->is_bare
= git_config_bool(var
, value
);
404 } else if (strcmp(var
, "core.worktree") == 0) {
406 return config_error_nonbool(var
);
407 data
->work_tree
= xstrdup(value
);
412 static int check_repository_format_gently(const char *gitdir
, int *nongit_ok
)
414 struct strbuf sb
= STRBUF_INIT
;
415 struct strbuf err
= STRBUF_INIT
;
416 struct repository_format candidate
;
419 has_common
= get_common_dir(&sb
, gitdir
);
420 strbuf_addstr(&sb
, "/config");
421 read_repository_format(&candidate
, sb
.buf
);
425 * For historical use of check_repository_format() in git-init,
426 * we treat a missing config as a silent "ok", even when nongit_ok
429 if (candidate
.version
< 0)
432 if (verify_repository_format(&candidate
, &err
) < 0) {
434 warning("%s", err
.buf
);
435 strbuf_release(&err
);
442 repository_format_precious_objects
= candidate
.precious_objects
;
443 string_list_clear(&candidate
.unknown_extensions
, 0);
445 if (candidate
.is_bare
!= -1) {
446 is_bare_repository_cfg
= candidate
.is_bare
;
447 if (is_bare_repository_cfg
== 1)
448 inside_work_tree
= -1;
450 if (candidate
.work_tree
) {
451 free(git_work_tree_cfg
);
452 git_work_tree_cfg
= candidate
.work_tree
;
453 inside_work_tree
= -1;
456 free(candidate
.work_tree
);
462 int read_repository_format(struct repository_format
*format
, const char *path
)
464 memset(format
, 0, sizeof(*format
));
465 format
->version
= -1;
466 format
->is_bare
= -1;
467 string_list_init(&format
->unknown_extensions
, 1);
468 git_config_from_file(check_repo_format
, path
, format
);
469 return format
->version
;
472 int verify_repository_format(const struct repository_format
*format
,
475 if (GIT_REPO_VERSION_READ
< format
->version
) {
476 strbuf_addf(err
, _("Expected git repo version <= %d, found %d"),
477 GIT_REPO_VERSION_READ
, format
->version
);
481 if (format
->version
>= 1 && format
->unknown_extensions
.nr
) {
484 strbuf_addstr(err
, _("unknown repository extensions found:"));
486 for (i
= 0; i
< format
->unknown_extensions
.nr
; i
++)
487 strbuf_addf(err
, "\n\t%s",
488 format
->unknown_extensions
.items
[i
].string
);
495 void read_gitfile_error_die(int error_code
, const char *path
, const char *dir
)
497 switch (error_code
) {
498 case READ_GITFILE_ERR_STAT_FAILED
:
499 case READ_GITFILE_ERR_NOT_A_FILE
:
500 /* non-fatal; follow return path */
502 case READ_GITFILE_ERR_OPEN_FAILED
:
503 die_errno("Error opening '%s'", path
);
504 case READ_GITFILE_ERR_TOO_LARGE
:
505 die("Too large to be a .git file: '%s'", path
);
506 case READ_GITFILE_ERR_READ_FAILED
:
507 die("Error reading %s", path
);
508 case READ_GITFILE_ERR_INVALID_FORMAT
:
509 die("Invalid gitfile format: %s", path
);
510 case READ_GITFILE_ERR_NO_PATH
:
511 die("No path in gitfile: %s", path
);
512 case READ_GITFILE_ERR_NOT_A_REPO
:
513 die("Not a git repository: %s", dir
);
515 die("BUG: unknown error code");
520 * Try to read the location of the git directory from the .git file,
521 * return path to git directory if found.
523 * On failure, if return_error_code is not NULL, return_error_code
524 * will be set to an error code and NULL will be returned. If
525 * return_error_code is NULL the function will die instead (for most
528 const char *read_gitfile_gently(const char *path
, int *return_error_code
)
530 const int max_file_size
= 1 << 20; /* 1MB */
539 if (stat(path
, &st
)) {
540 /* NEEDSWORK: discern between ENOENT vs other errors */
541 error_code
= READ_GITFILE_ERR_STAT_FAILED
;
544 if (!S_ISREG(st
.st_mode
)) {
545 error_code
= READ_GITFILE_ERR_NOT_A_FILE
;
548 if (st
.st_size
> max_file_size
) {
549 error_code
= READ_GITFILE_ERR_TOO_LARGE
;
552 fd
= open(path
, O_RDONLY
);
554 error_code
= READ_GITFILE_ERR_OPEN_FAILED
;
557 buf
= xmallocz(st
.st_size
);
558 len
= read_in_full(fd
, buf
, st
.st_size
);
560 if (len
!= st
.st_size
) {
561 error_code
= READ_GITFILE_ERR_READ_FAILED
;
564 if (!starts_with(buf
, "gitdir: ")) {
565 error_code
= READ_GITFILE_ERR_INVALID_FORMAT
;
568 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
571 error_code
= READ_GITFILE_ERR_NO_PATH
;
577 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
578 size_t pathlen
= slash
+1 - path
;
579 dir
= xstrfmt("%.*s%.*s", (int)pathlen
, path
,
580 (int)(len
- 8), buf
+ 8);
584 if (!is_git_directory(dir
)) {
585 error_code
= READ_GITFILE_ERR_NOT_A_REPO
;
588 path
= real_path(dir
);
591 if (return_error_code
)
592 *return_error_code
= error_code
;
594 read_gitfile_error_die(error_code
, path
, dir
);
597 return error_code
? NULL
: path
;
600 static const char *setup_explicit_git_dir(const char *gitdirenv
,
604 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
605 const char *worktree
;
609 if (PATH_MAX
- 40 < strlen(gitdirenv
))
610 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
612 gitfile
= (char*)read_gitfile(gitdirenv
);
614 gitfile
= xstrdup(gitfile
);
618 if (!is_git_directory(gitdirenv
)) {
624 die("Not a git repository: '%s'", gitdirenv
);
627 if (check_repository_format_gently(gitdirenv
, nongit_ok
)) {
632 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
634 set_git_work_tree(work_tree_env
);
635 else if (is_bare_repository_cfg
> 0) {
636 if (git_work_tree_cfg
) {
638 warning("core.bare and core.worktree do not make sense");
639 work_tree_config_is_bogus
= 1;
643 set_git_dir(gitdirenv
);
647 else if (git_work_tree_cfg
) { /* #6, #14 */
648 if (is_absolute_path(git_work_tree_cfg
))
649 set_git_work_tree(git_work_tree_cfg
);
652 if (chdir(gitdirenv
))
653 die_errno("Could not chdir to '%s'", gitdirenv
);
654 if (chdir(git_work_tree_cfg
))
655 die_errno("Could not chdir to '%s'", git_work_tree_cfg
);
656 core_worktree
= xgetcwd();
658 die_errno("Could not come back to cwd");
659 set_git_work_tree(core_worktree
);
663 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, 1)) {
665 set_git_dir(gitdirenv
);
670 set_git_work_tree(".");
672 /* set_git_work_tree() must have been called by now */
673 worktree
= get_git_work_tree();
675 /* both get_git_work_tree() and cwd are already normalized */
676 if (!strcmp(cwd
->buf
, worktree
)) { /* cwd == worktree */
677 set_git_dir(gitdirenv
);
682 offset
= dir_inside_of(cwd
->buf
, worktree
);
683 if (offset
>= 0) { /* cwd inside worktree? */
684 set_git_dir(real_path(gitdirenv
));
686 die_errno("Could not chdir to '%s'", worktree
);
687 strbuf_addch(cwd
, '/');
689 return cwd
->buf
+ offset
;
692 /* cwd outside worktree */
693 set_git_dir(gitdirenv
);
698 static const char *setup_discovered_git_dir(const char *gitdir
,
699 struct strbuf
*cwd
, int offset
,
702 if (check_repository_format_gently(gitdir
, nongit_ok
))
705 /* --work-tree is set without --git-dir; use discovered one */
706 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
707 if (offset
!= cwd
->len
&& !is_absolute_path(gitdir
))
708 gitdir
= real_pathdup(gitdir
, 1);
710 die_errno("Could not come back to cwd");
711 return setup_explicit_git_dir(gitdir
, cwd
, nongit_ok
);
714 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
715 if (is_bare_repository_cfg
> 0) {
716 set_git_dir(offset
== cwd
->len
? gitdir
: real_path(gitdir
));
718 die_errno("Could not come back to cwd");
722 /* #0, #1, #5, #8, #9, #12, #13 */
723 set_git_work_tree(".");
724 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
727 inside_work_tree
= 1;
728 if (offset
== cwd
->len
)
731 /* Make "offset" point past the '/' (already the case for root dirs) */
732 if (offset
!= offset_1st_component(cwd
->buf
))
734 /* Add a '/' at the end */
735 strbuf_addch(cwd
, '/');
736 return cwd
->buf
+ offset
;
739 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
740 static const char *setup_bare_git_dir(struct strbuf
*cwd
, int offset
,
745 if (check_repository_format_gently(".", nongit_ok
))
748 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
750 /* --work-tree is set without --git-dir; use discovered one */
751 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
754 gitdir
= offset
== cwd
->len
? "." : xmemdupz(cwd
->buf
, offset
);
756 die_errno("Could not come back to cwd");
757 return setup_explicit_git_dir(gitdir
, cwd
, nongit_ok
);
761 inside_work_tree
= 0;
762 if (offset
!= cwd
->len
) {
764 die_errno("Cannot come back to cwd");
765 root_len
= offset_1st_component(cwd
->buf
);
766 strbuf_setlen(cwd
, offset
> root_len
? offset
: root_len
);
767 set_git_dir(cwd
->buf
);
774 static const char *setup_nongit(const char *cwd
, int *nongit_ok
)
777 die(_("Not a git repository (or any of the parent directories): %s"), DEFAULT_GIT_DIR_ENVIRONMENT
);
779 die_errno(_("Cannot come back to cwd"));
784 static dev_t
get_device_or_die(const char *path
, const char *prefix
, int prefix_len
)
787 if (stat(path
, &buf
)) {
788 die_errno("failed to stat '%*s%s%s'",
790 prefix
? prefix
: "",
791 prefix
? "/" : "", path
);
797 * A "string_list_each_func_t" function that canonicalizes an entry
798 * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or
799 * discards it if unusable. The presence of an empty entry in
800 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
801 * subsequent entries.
803 static int canonicalize_ceiling_entry(struct string_list_item
*item
,
806 int *empty_entry_found
= cb_data
;
807 char *ceil
= item
->string
;
810 *empty_entry_found
= 1;
812 } else if (!is_absolute_path(ceil
)) {
814 } else if (*empty_entry_found
) {
815 /* Keep entry but do not canonicalize it */
818 char *real_path
= real_pathdup(ceil
, 0);
823 item
->string
= real_path
;
828 enum discovery_result
{
833 /* these are errors */
834 GIT_DIR_HIT_CEILING
= -1,
835 GIT_DIR_HIT_MOUNT_POINT
= -2,
836 GIT_DIR_INVALID_GITFILE
= -3
840 * We cannot decide in this function whether we are in the work tree or
841 * not, since the config can only be read _after_ this function was called.
843 * Also, we avoid changing any global state (such as the current working
844 * directory) to allow early callers.
846 * The directory where the search should start needs to be passed in via the
847 * `dir` parameter; upon return, the `dir` buffer will contain the path of
848 * the directory where the search ended, and `gitdir` will contain the path of
849 * the discovered .git/ directory, if any. If `gitdir` is not absolute, it
850 * is relative to `dir` (i.e. *not* necessarily the cwd).
852 static enum discovery_result
setup_git_directory_gently_1(struct strbuf
*dir
,
853 struct strbuf
*gitdir
,
856 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
857 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
858 const char *gitdirenv
;
859 int ceil_offset
= -1, min_offset
= has_dos_drive_prefix(dir
->buf
) ? 3 : 1;
860 dev_t current_device
= 0;
861 int one_filesystem
= 1;
864 * If GIT_DIR is set explicitly, we're not going
865 * to do any discovery, but we still do repository
868 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
870 strbuf_addstr(gitdir
, gitdirenv
);
871 return GIT_DIR_EXPLICIT
;
874 if (env_ceiling_dirs
) {
875 int empty_entry_found
= 0;
877 string_list_split(&ceiling_dirs
, env_ceiling_dirs
, PATH_SEP
, -1);
878 filter_string_list(&ceiling_dirs
, 0,
879 canonicalize_ceiling_entry
, &empty_entry_found
);
880 ceil_offset
= longest_ancestor_length(dir
->buf
, &ceiling_dirs
);
881 string_list_clear(&ceiling_dirs
, 0);
885 ceil_offset
= min_offset
- 2;
888 * Test in the following order (relative to the dir):
889 * - .git (file containing "gitdir: <path>")
898 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
900 current_device
= get_device_or_die(dir
->buf
, NULL
, 0);
902 int offset
= dir
->len
, error_code
= 0;
904 if (offset
> min_offset
)
905 strbuf_addch(dir
, '/');
906 strbuf_addstr(dir
, DEFAULT_GIT_DIR_ENVIRONMENT
);
907 gitdirenv
= read_gitfile_gently(dir
->buf
, die_on_error
?
911 error_code
== READ_GITFILE_ERR_NOT_A_FILE
) {
912 /* NEEDSWORK: fail if .git is not file nor dir */
913 if (is_git_directory(dir
->buf
))
914 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
915 } else if (error_code
!= READ_GITFILE_ERR_STAT_FAILED
)
916 return GIT_DIR_INVALID_GITFILE
;
918 strbuf_setlen(dir
, offset
);
920 strbuf_addstr(gitdir
, gitdirenv
);
921 return GIT_DIR_DISCOVERED
;
924 if (is_git_directory(dir
->buf
)) {
925 strbuf_addstr(gitdir
, ".");
929 if (offset
<= min_offset
)
930 return GIT_DIR_HIT_CEILING
;
932 while (--offset
> ceil_offset
&& !is_dir_sep(dir
->buf
[offset
]))
934 if (offset
<= ceil_offset
)
935 return GIT_DIR_HIT_CEILING
;
937 strbuf_setlen(dir
, offset
> min_offset
? offset
: min_offset
);
938 if (one_filesystem
&&
939 current_device
!= get_device_or_die(dir
->buf
, NULL
, offset
))
940 return GIT_DIR_HIT_MOUNT_POINT
;
944 int discover_git_directory(struct strbuf
*commondir
,
945 struct strbuf
*gitdir
)
947 struct strbuf dir
= STRBUF_INIT
, err
= STRBUF_INIT
;
948 size_t gitdir_offset
= gitdir
->len
, cwd_len
;
949 size_t commondir_offset
= commondir
->len
;
950 struct repository_format candidate
;
952 if (strbuf_getcwd(&dir
))
956 if (setup_git_directory_gently_1(&dir
, gitdir
, 0) <= 0) {
957 strbuf_release(&dir
);
962 * The returned gitdir is relative to dir, and if dir does not reflect
963 * the current working directory, we simply make the gitdir absolute.
965 if (dir
.len
< cwd_len
&& !is_absolute_path(gitdir
->buf
+ gitdir_offset
)) {
966 /* Avoid a trailing "/." */
967 if (!strcmp(".", gitdir
->buf
+ gitdir_offset
))
968 strbuf_setlen(gitdir
, gitdir_offset
);
970 strbuf_addch(&dir
, '/');
971 strbuf_insert(gitdir
, gitdir_offset
, dir
.buf
, dir
.len
);
974 get_common_dir(commondir
, gitdir
->buf
+ gitdir_offset
);
977 strbuf_addf(&dir
, "%s/config", commondir
->buf
+ commondir_offset
);
978 read_repository_format(&candidate
, dir
.buf
);
979 strbuf_release(&dir
);
981 if (verify_repository_format(&candidate
, &err
) < 0) {
982 warning("ignoring git dir '%s': %s",
983 gitdir
->buf
+ gitdir_offset
, err
.buf
);
984 strbuf_release(&err
);
985 strbuf_setlen(commondir
, commondir_offset
);
986 strbuf_setlen(gitdir
, gitdir_offset
);
993 const char *setup_git_directory_gently(int *nongit_ok
)
995 static struct strbuf cwd
= STRBUF_INIT
;
996 struct strbuf dir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
;
997 const char *prefix
, *env_prefix
;
1000 * We may have read an incomplete configuration before
1001 * setting-up the git directory. If so, clear the cache so
1002 * that the next queries to the configuration reload complete
1003 * configuration (including the per-repo config file that we
1004 * ignored previously).
1009 * Let's assume that we are in a git repository.
1010 * If it turns out later that we are somewhere else, the value will be
1011 * updated accordingly.
1016 if (strbuf_getcwd(&cwd
))
1017 die_errno(_("Unable to read current working directory"));
1018 strbuf_addbuf(&dir
, &cwd
);
1020 switch (setup_git_directory_gently_1(&dir
, &gitdir
, 1)) {
1024 case GIT_DIR_EXPLICIT
:
1025 prefix
= setup_explicit_git_dir(gitdir
.buf
, &cwd
, nongit_ok
);
1027 case GIT_DIR_DISCOVERED
:
1028 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1029 die(_("Cannot change to '%s'"), dir
.buf
);
1030 prefix
= setup_discovered_git_dir(gitdir
.buf
, &cwd
, dir
.len
,
1034 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1035 die(_("Cannot change to '%s'"), dir
.buf
);
1036 prefix
= setup_bare_git_dir(&cwd
, dir
.len
, nongit_ok
);
1038 case GIT_DIR_HIT_CEILING
:
1039 prefix
= setup_nongit(cwd
.buf
, nongit_ok
);
1041 case GIT_DIR_HIT_MOUNT_POINT
:
1044 strbuf_release(&cwd
);
1045 strbuf_release(&dir
);
1048 die(_("Not a git repository (or any parent up to mount point %s)\n"
1049 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
1052 die("BUG: unhandled setup_git_directory_1() result");
1055 env_prefix
= getenv(GIT_TOPLEVEL_PREFIX_ENVIRONMENT
);
1057 prefix
= env_prefix
;
1060 setenv(GIT_PREFIX_ENVIRONMENT
, prefix
, 1);
1062 setenv(GIT_PREFIX_ENVIRONMENT
, "", 1);
1064 startup_info
->have_repository
= !nongit_ok
|| !*nongit_ok
;
1065 startup_info
->prefix
= prefix
;
1067 strbuf_release(&dir
);
1068 strbuf_release(&gitdir
);
1073 int git_config_perm(const char *var
, const char *value
)
1081 if (!strcmp(value
, "umask"))
1083 if (!strcmp(value
, "group"))
1085 if (!strcmp(value
, "all") ||
1086 !strcmp(value
, "world") ||
1087 !strcmp(value
, "everybody"))
1088 return PERM_EVERYBODY
;
1090 /* Parse octal numbers */
1091 i
= strtol(value
, &endptr
, 8);
1093 /* If not an octal number, maybe true/false? */
1095 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
1098 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
1099 * a chmod value to restrict to.
1102 case PERM_UMASK
: /* 0 */
1104 case OLD_PERM_GROUP
: /* 1 */
1106 case OLD_PERM_EVERYBODY
: /* 2 */
1107 return PERM_EVERYBODY
;
1110 /* A filemode value was given: 0xxx */
1112 if ((i
& 0600) != 0600)
1113 die(_("Problem with core.sharedRepository filemode value "
1114 "(0%.3o).\nThe owner of files must always have "
1115 "read and write permissions."), i
);
1118 * Mask filemode value. Others can not get write permission.
1119 * x flags for directories are handled separately.
1124 void check_repository_format(void)
1126 check_repository_format_gently(get_git_dir(), NULL
);
1127 startup_info
->have_repository
= 1;
1131 * Returns the "prefix", a path to the current working directory
1132 * relative to the work tree root, or NULL, if the current working
1133 * directory is not a strict subdirectory of the work tree root. The
1134 * prefix always ends with a '/' character.
1136 const char *setup_git_directory(void)
1138 return setup_git_directory_gently(NULL
);
1141 const char *resolve_gitdir_gently(const char *suspect
, int *return_error_code
)
1143 if (is_git_directory(suspect
))
1145 return read_gitfile_gently(suspect
, return_error_code
);
1148 /* if any standard file descriptor is missing open it to /dev/null */
1149 void sanitize_stdfds(void)
1151 int fd
= open("/dev/null", O_RDWR
, 0);
1152 while (fd
!= -1 && fd
< 2)
1155 die_errno("open /dev/null or dup failed");
1162 #ifdef NO_POSIX_GOODIES
1170 die_errno("fork failed");
1175 die_errno("setsid failed");