3 #include "string-list.h"
5 static int inside_git_dir
= -1;
6 static int inside_work_tree
= -1;
7 static int work_tree_config_is_bogus
;
10 * The input parameter must contain an absolute path, and it must already be
13 * Find the part of an absolute path that lies inside the work tree by
14 * dereferencing symlinks outside the work tree, for example:
15 * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
16 * /dir/file (work tree is /) -> dir/file
17 * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
18 * /dir/repolink/file (repolink points to /dir/repo) -> file
19 * /dir/repo (exactly equal to work tree) -> (empty string)
21 static int abspath_part_inside_repo(char *path
)
27 const char *work_tree
= get_git_work_tree();
31 wtlen
= strlen(work_tree
);
33 off
= offset_1st_component(path
);
35 /* check if work tree is already the prefix */
36 if (wtlen
<= len
&& !strncmp(path
, work_tree
, wtlen
)) {
37 if (path
[wtlen
] == '/') {
38 memmove(path
, path
+ wtlen
+ 1, len
- wtlen
);
40 } else if (path
[wtlen
- 1] == '/' || path
[wtlen
] == '\0') {
41 /* work tree is the root, or the whole path */
42 memmove(path
, path
+ wtlen
, len
- wtlen
+ 1);
45 /* work tree might match beginning of a symlink to work tree */
51 /* check each '/'-terminated level */
56 if (strcmp(real_path(path0
), work_tree
) == 0) {
57 memmove(path0
, path
+ 1, len
- (path
- path0
));
64 /* check whole path */
65 if (strcmp(real_path(path0
), work_tree
) == 0) {
74 * Normalize "path", prepending the "prefix" for relative paths. If
75 * remaining_prefix is not NULL, return the actual prefix still
76 * remains in the path. For example, prefix = sub1/sub2/ and path is
78 * foo -> sub1/sub2/foo (full prefix)
79 * ../foo -> sub1/foo (remaining prefix is sub1/)
80 * ../../bar -> bar (no remaining prefix)
81 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
82 * `pwd`/../bar -> sub1/bar (no remaining prefix)
84 char *prefix_path_gently(const char *prefix
, int len
,
85 int *remaining_prefix
, const char *path
)
87 const char *orig
= path
;
89 if (is_absolute_path(orig
)) {
90 sanitized
= xmallocz(strlen(path
));
92 *remaining_prefix
= 0;
93 if (normalize_path_copy_len(sanitized
, path
, remaining_prefix
)) {
97 if (abspath_part_inside_repo(sanitized
)) {
102 sanitized
= xstrfmt("%.*s%s", len
, prefix
, path
);
103 if (remaining_prefix
)
104 *remaining_prefix
= len
;
105 if (normalize_path_copy_len(sanitized
, sanitized
, remaining_prefix
)) {
113 char *prefix_path(const char *prefix
, int len
, const char *path
)
115 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
117 die("'%s' is outside repository", path
);
121 int path_inside_repo(const char *prefix
, const char *path
)
123 int len
= prefix
? strlen(prefix
) : 0;
124 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
132 int check_filename(const char *prefix
, const char *arg
)
137 if (starts_with(arg
, ":/")) {
138 if (arg
[2] == '\0') /* ":/" is root dir, always exists */
142 name
= prefix_filename(prefix
, strlen(prefix
), arg
);
145 if (!lstat(name
, &st
))
146 return 1; /* file exists */
147 if (errno
== ENOENT
|| errno
== ENOTDIR
)
148 return 0; /* file does not exist */
149 die_errno("failed to stat '%s'", arg
);
152 static void NORETURN
die_verify_filename(const char *prefix
,
154 int diagnose_misspelt_rev
)
156 if (!diagnose_misspelt_rev
)
157 die("%s: no such path in the working tree.\n"
158 "Use 'git <command> -- <path>...' to specify paths that do not exist locally.",
161 * Saying "'(icase)foo' does not exist in the index" when the
162 * user gave us ":(icase)foo" is just stupid. A magic pathspec
163 * begins with a colon and is followed by a non-alnum; do not
164 * let maybe_die_on_misspelt_object_name() even trigger.
166 if (!(arg
[0] == ':' && !isalnum(arg
[1])))
167 maybe_die_on_misspelt_object_name(arg
, prefix
);
169 /* ... or fall back the most general message. */
170 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
171 "Use '--' to separate paths from revisions, like this:\n"
172 "'git <command> [<revision>...] -- [<file>...]'", arg
);
177 * Verify a filename that we got as an argument for a pathspec
178 * entry. Note that a filename that begins with "-" never verifies
179 * as true, because even if such a filename were to exist, we want
180 * it to be preceded by the "--" marker (or we want the user to
181 * use a format like "./-filename")
183 * The "diagnose_misspelt_rev" is used to provide a user-friendly
184 * diagnosis when dying upon finding that "name" is not a pathname.
185 * If set to 1, the diagnosis will try to diagnose "name" as an
186 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
187 * will only complain about an inexisting file.
189 * This function is typically called to check that a "file or rev"
190 * argument is unambiguous. In this case, the caller will want
191 * diagnose_misspelt_rev == 1 when verifying the first non-rev
192 * argument (which could have been a revision), and
193 * diagnose_misspelt_rev == 0 for the next ones (because we already
194 * saw a filename, there's not ambiguity anymore).
196 void verify_filename(const char *prefix
,
198 int diagnose_misspelt_rev
)
201 die("bad flag '%s' used after filename", arg
);
202 if (check_filename(prefix
, arg
) || !no_wildcard(arg
))
204 die_verify_filename(prefix
, arg
, diagnose_misspelt_rev
);
208 * Opposite of the above: the command line did not have -- marker
209 * and we parsed the arg as a refname. It should not be interpretable
212 void verify_non_filename(const char *prefix
, const char *arg
)
214 if (!is_inside_work_tree() || is_inside_git_dir())
218 if (!check_filename(prefix
, arg
))
220 die("ambiguous argument '%s': both revision and filename\n"
221 "Use '--' to separate paths from revisions, like this:\n"
222 "'git <command> [<revision>...] -- [<file>...]'", arg
);
225 int get_common_dir(struct strbuf
*sb
, const char *gitdir
)
227 const char *git_env_common_dir
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
228 if (git_env_common_dir
) {
229 strbuf_addstr(sb
, git_env_common_dir
);
232 return get_common_dir_noenv(sb
, gitdir
);
236 int get_common_dir_noenv(struct strbuf
*sb
, const char *gitdir
)
238 struct strbuf data
= STRBUF_INIT
;
239 struct strbuf path
= STRBUF_INIT
;
242 strbuf_addf(&path
, "%s/commondir", gitdir
);
243 if (file_exists(path
.buf
)) {
244 if (strbuf_read_file(&data
, path
.buf
, 0) <= 0)
245 die_errno(_("failed to read %s"), path
.buf
);
246 while (data
.len
&& (data
.buf
[data
.len
- 1] == '\n' ||
247 data
.buf
[data
.len
- 1] == '\r'))
249 data
.buf
[data
.len
] = '\0';
251 if (!is_absolute_path(data
.buf
))
252 strbuf_addf(&path
, "%s/", gitdir
);
253 strbuf_addbuf(&path
, &data
);
254 strbuf_addstr(sb
, real_path(path
.buf
));
257 strbuf_addstr(sb
, gitdir
);
258 strbuf_release(&data
);
259 strbuf_release(&path
);
264 * Test if it looks like we're at a git directory.
267 * - either an objects/ directory _or_ the proper
268 * GIT_OBJECT_DIRECTORY environment variable
269 * - a refs/ directory
270 * - either a HEAD symlink or a HEAD file that is formatted as
271 * a proper "ref:", or a regular file HEAD that has a properly
272 * formatted sha1 object name.
274 int is_git_directory(const char *suspect
)
276 struct strbuf path
= STRBUF_INIT
;
280 /* Check worktree-related signatures */
281 strbuf_addf(&path
, "%s/HEAD", suspect
);
282 if (validate_headref(path
.buf
))
286 get_common_dir(&path
, suspect
);
289 /* Check non-worktree-related signatures */
290 if (getenv(DB_ENVIRONMENT
)) {
291 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
295 strbuf_setlen(&path
, len
);
296 strbuf_addstr(&path
, "/objects");
297 if (access(path
.buf
, X_OK
))
301 strbuf_setlen(&path
, len
);
302 strbuf_addstr(&path
, "/refs");
303 if (access(path
.buf
, X_OK
))
308 strbuf_release(&path
);
312 int is_nonbare_repository_dir(struct strbuf
*path
)
316 size_t orig_path_len
= path
->len
;
317 assert(orig_path_len
!= 0);
318 strbuf_complete(path
, '/');
319 strbuf_addstr(path
, ".git");
320 if (read_gitfile_gently(path
->buf
, &gitfile_error
) || is_git_directory(path
->buf
))
322 if (gitfile_error
== READ_GITFILE_ERR_OPEN_FAILED
||
323 gitfile_error
== READ_GITFILE_ERR_READ_FAILED
)
325 strbuf_setlen(path
, orig_path_len
);
329 int is_inside_git_dir(void)
331 if (inside_git_dir
< 0)
332 inside_git_dir
= is_inside_dir(get_git_dir());
333 return inside_git_dir
;
336 int is_inside_work_tree(void)
338 if (inside_work_tree
< 0)
339 inside_work_tree
= is_inside_dir(get_git_work_tree());
340 return inside_work_tree
;
343 void setup_work_tree(void)
345 const char *work_tree
, *git_dir
;
346 static int initialized
= 0;
351 if (work_tree_config_is_bogus
)
352 die("unable to set up work tree using invalid config");
354 work_tree
= get_git_work_tree();
355 git_dir
= get_git_dir();
356 if (!is_absolute_path(git_dir
))
357 git_dir
= real_path(get_git_dir());
358 if (!work_tree
|| chdir(work_tree
))
359 die("This operation must be run in a work tree");
362 * Make sure subsequent git processes find correct worktree
363 * if $GIT_WORK_TREE is set relative
365 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
366 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
368 set_git_dir(remove_leading_path(git_dir
, work_tree
));
372 static int check_repo_format(const char *var
, const char *value
, void *vdata
)
374 struct repository_format
*data
= vdata
;
377 if (strcmp(var
, "core.repositoryformatversion") == 0)
378 data
->version
= git_config_int(var
, value
);
379 else if (skip_prefix(var
, "extensions.", &ext
)) {
381 * record any known extensions here; otherwise,
382 * we fall through to recording it as unknown, and
383 * check_repository_format will complain
385 if (!strcmp(ext
, "noop"))
387 else if (!strcmp(ext
, "preciousobjects"))
388 data
->precious_objects
= git_config_bool(var
, value
);
390 string_list_append(&data
->unknown_extensions
, ext
);
391 } else if (strcmp(var
, "core.bare") == 0) {
392 data
->is_bare
= git_config_bool(var
, value
);
393 } else if (strcmp(var
, "core.worktree") == 0) {
395 return config_error_nonbool(var
);
396 data
->work_tree
= xstrdup(value
);
401 static int check_repository_format_gently(const char *gitdir
, int *nongit_ok
)
403 struct strbuf sb
= STRBUF_INIT
;
404 struct strbuf err
= STRBUF_INIT
;
405 struct repository_format candidate
;
408 has_common
= get_common_dir(&sb
, gitdir
);
409 strbuf_addstr(&sb
, "/config");
410 read_repository_format(&candidate
, sb
.buf
);
414 * For historical use of check_repository_format() in git-init,
415 * we treat a missing config as a silent "ok", even when nongit_ok
418 if (candidate
.version
< 0)
421 if (verify_repository_format(&candidate
, &err
) < 0) {
423 warning("%s", err
.buf
);
424 strbuf_release(&err
);
431 repository_format_version
= candidate
.version
;
432 repository_format_precious_objects
= candidate
.precious_objects
;
433 string_list_clear(&candidate
.unknown_extensions
, 0);
435 if (candidate
.is_bare
!= -1) {
436 is_bare_repository_cfg
= candidate
.is_bare
;
437 if (is_bare_repository_cfg
== 1)
438 inside_work_tree
= -1;
440 if (candidate
.work_tree
) {
441 free(git_work_tree_cfg
);
442 git_work_tree_cfg
= candidate
.work_tree
;
443 inside_work_tree
= -1;
446 free(candidate
.work_tree
);
452 int read_repository_format(struct repository_format
*format
, const char *path
)
454 memset(format
, 0, sizeof(*format
));
455 format
->version
= -1;
456 format
->is_bare
= -1;
457 string_list_init(&format
->unknown_extensions
, 1);
458 git_config_from_file(check_repo_format
, path
, format
);
459 return format
->version
;
462 int verify_repository_format(const struct repository_format
*format
,
465 if (GIT_REPO_VERSION_READ
< format
->version
) {
466 strbuf_addf(err
, "Expected git repo version <= %d, found %d",
467 GIT_REPO_VERSION_READ
, format
->version
);
471 if (format
->version
>= 1 && format
->unknown_extensions
.nr
) {
474 strbuf_addstr(err
, "unknown repository extensions found:");
476 for (i
= 0; i
< format
->unknown_extensions
.nr
; i
++)
477 strbuf_addf(err
, "\n\t%s",
478 format
->unknown_extensions
.items
[i
].string
);
486 * Try to read the location of the git directory from the .git file,
487 * return path to git directory if found.
489 * On failure, if return_error_code is not NULL, return_error_code
490 * will be set to an error code and NULL will be returned. If
491 * return_error_code is NULL the function will die instead (for most
494 const char *read_gitfile_gently(const char *path
, int *return_error_code
)
496 const int max_file_size
= 1 << 20; /* 1MB */
505 if (stat(path
, &st
)) {
506 error_code
= READ_GITFILE_ERR_STAT_FAILED
;
509 if (!S_ISREG(st
.st_mode
)) {
510 error_code
= READ_GITFILE_ERR_NOT_A_FILE
;
513 if (st
.st_size
> max_file_size
) {
514 error_code
= READ_GITFILE_ERR_TOO_LARGE
;
517 fd
= open(path
, O_RDONLY
);
519 error_code
= READ_GITFILE_ERR_OPEN_FAILED
;
522 buf
= xmallocz(st
.st_size
);
523 len
= read_in_full(fd
, buf
, st
.st_size
);
525 if (len
!= st
.st_size
) {
526 error_code
= READ_GITFILE_ERR_READ_FAILED
;
529 if (!starts_with(buf
, "gitdir: ")) {
530 error_code
= READ_GITFILE_ERR_INVALID_FORMAT
;
533 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
536 error_code
= READ_GITFILE_ERR_NO_PATH
;
542 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
543 size_t pathlen
= slash
+1 - path
;
544 dir
= xstrfmt("%.*s%.*s", (int)pathlen
, path
,
545 (int)(len
- 8), buf
+ 8);
549 if (!is_git_directory(dir
)) {
550 error_code
= READ_GITFILE_ERR_NOT_A_REPO
;
553 path
= real_path(dir
);
556 if (return_error_code
)
557 *return_error_code
= error_code
;
558 else if (error_code
) {
559 switch (error_code
) {
560 case READ_GITFILE_ERR_STAT_FAILED
:
561 case READ_GITFILE_ERR_NOT_A_FILE
:
562 /* non-fatal; follow return path */
564 case READ_GITFILE_ERR_OPEN_FAILED
:
565 die_errno("Error opening '%s'", path
);
566 case READ_GITFILE_ERR_TOO_LARGE
:
567 die("Too large to be a .git file: '%s'", path
);
568 case READ_GITFILE_ERR_READ_FAILED
:
569 die("Error reading %s", path
);
570 case READ_GITFILE_ERR_INVALID_FORMAT
:
571 die("Invalid gitfile format: %s", path
);
572 case READ_GITFILE_ERR_NO_PATH
:
573 die("No path in gitfile: %s", path
);
574 case READ_GITFILE_ERR_NOT_A_REPO
:
575 die("Not a git repository: %s", dir
);
582 return error_code
? NULL
: path
;
585 static const char *setup_explicit_git_dir(const char *gitdirenv
,
589 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
590 const char *worktree
;
594 if (PATH_MAX
- 40 < strlen(gitdirenv
))
595 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
597 gitfile
= (char*)read_gitfile(gitdirenv
);
599 gitfile
= xstrdup(gitfile
);
603 if (!is_git_directory(gitdirenv
)) {
609 die("Not a git repository: '%s'", gitdirenv
);
612 if (check_repository_format_gently(gitdirenv
, nongit_ok
)) {
617 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
619 set_git_work_tree(work_tree_env
);
620 else if (is_bare_repository_cfg
> 0) {
621 if (git_work_tree_cfg
) {
623 warning("core.bare and core.worktree do not make sense");
624 work_tree_config_is_bogus
= 1;
628 set_git_dir(gitdirenv
);
632 else if (git_work_tree_cfg
) { /* #6, #14 */
633 if (is_absolute_path(git_work_tree_cfg
))
634 set_git_work_tree(git_work_tree_cfg
);
637 if (chdir(gitdirenv
))
638 die_errno("Could not chdir to '%s'", gitdirenv
);
639 if (chdir(git_work_tree_cfg
))
640 die_errno("Could not chdir to '%s'", git_work_tree_cfg
);
641 core_worktree
= xgetcwd();
643 die_errno("Could not come back to cwd");
644 set_git_work_tree(core_worktree
);
648 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, 1)) {
650 set_git_dir(gitdirenv
);
655 set_git_work_tree(".");
657 /* set_git_work_tree() must have been called by now */
658 worktree
= get_git_work_tree();
660 /* both get_git_work_tree() and cwd are already normalized */
661 if (!strcmp(cwd
->buf
, worktree
)) { /* cwd == worktree */
662 set_git_dir(gitdirenv
);
667 offset
= dir_inside_of(cwd
->buf
, worktree
);
668 if (offset
>= 0) { /* cwd inside worktree? */
669 set_git_dir(real_path(gitdirenv
));
671 die_errno("Could not chdir to '%s'", worktree
);
672 strbuf_addch(cwd
, '/');
674 return cwd
->buf
+ offset
;
677 /* cwd outside worktree */
678 set_git_dir(gitdirenv
);
683 static const char *setup_discovered_git_dir(const char *gitdir
,
684 struct strbuf
*cwd
, int offset
,
687 if (check_repository_format_gently(gitdir
, nongit_ok
))
690 /* --work-tree is set without --git-dir; use discovered one */
691 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
692 if (offset
!= cwd
->len
&& !is_absolute_path(gitdir
))
693 gitdir
= xstrdup(real_path(gitdir
));
695 die_errno("Could not come back to cwd");
696 return setup_explicit_git_dir(gitdir
, cwd
, nongit_ok
);
699 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
700 if (is_bare_repository_cfg
> 0) {
701 set_git_dir(offset
== cwd
->len
? gitdir
: real_path(gitdir
));
703 die_errno("Could not come back to cwd");
707 /* #0, #1, #5, #8, #9, #12, #13 */
708 set_git_work_tree(".");
709 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
712 inside_work_tree
= 1;
713 if (offset
== cwd
->len
)
716 /* Make "offset" point to past the '/', and add a '/' at the end */
718 strbuf_addch(cwd
, '/');
719 return cwd
->buf
+ offset
;
722 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
723 static const char *setup_bare_git_dir(struct strbuf
*cwd
, int offset
,
728 if (check_repository_format_gently(".", nongit_ok
))
731 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
733 /* --work-tree is set without --git-dir; use discovered one */
734 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
737 gitdir
= offset
== cwd
->len
? "." : xmemdupz(cwd
->buf
, offset
);
739 die_errno("Could not come back to cwd");
740 return setup_explicit_git_dir(gitdir
, cwd
, nongit_ok
);
744 inside_work_tree
= 0;
745 if (offset
!= cwd
->len
) {
747 die_errno("Cannot come back to cwd");
748 root_len
= offset_1st_component(cwd
->buf
);
749 strbuf_setlen(cwd
, offset
> root_len
? offset
: root_len
);
750 set_git_dir(cwd
->buf
);
757 static const char *setup_nongit(const char *cwd
, int *nongit_ok
)
760 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
762 die_errno("Cannot come back to cwd");
767 static dev_t
get_device_or_die(const char *path
, const char *prefix
, int prefix_len
)
770 if (stat(path
, &buf
)) {
771 die_errno("failed to stat '%*s%s%s'",
773 prefix
? prefix
: "",
774 prefix
? "/" : "", path
);
780 * A "string_list_each_func_t" function that canonicalizes an entry
781 * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or
782 * discards it if unusable. The presence of an empty entry in
783 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
784 * subsequent entries.
786 static int canonicalize_ceiling_entry(struct string_list_item
*item
,
789 int *empty_entry_found
= cb_data
;
790 char *ceil
= item
->string
;
793 *empty_entry_found
= 1;
795 } else if (!is_absolute_path(ceil
)) {
797 } else if (*empty_entry_found
) {
798 /* Keep entry but do not canonicalize it */
801 const char *real_path
= real_path_if_valid(ceil
);
805 item
->string
= xstrdup(real_path
);
811 * We cannot decide in this function whether we are in the work tree or
812 * not, since the config can only be read _after_ this function was called.
814 static const char *setup_git_directory_gently_1(int *nongit_ok
)
816 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
817 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
818 static struct strbuf cwd
= STRBUF_INIT
;
819 const char *gitdirenv
, *ret
;
821 int offset
, offset_parent
, ceil_offset
= -1;
822 dev_t current_device
= 0;
823 int one_filesystem
= 1;
826 * We may have read an incomplete configuration before
827 * setting-up the git directory. If so, clear the cache so
828 * that the next queries to the configuration reload complete
829 * configuration (including the per-repo config file that we
830 * ignored previously).
835 * Let's assume that we are in a git repository.
836 * If it turns out later that we are somewhere else, the value will be
837 * updated accordingly.
842 if (strbuf_getcwd(&cwd
))
843 die_errno("Unable to read current working directory");
847 * If GIT_DIR is set explicitly, we're not going
848 * to do any discovery, but we still do repository
851 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
853 return setup_explicit_git_dir(gitdirenv
, &cwd
, nongit_ok
);
855 if (env_ceiling_dirs
) {
856 int empty_entry_found
= 0;
858 string_list_split(&ceiling_dirs
, env_ceiling_dirs
, PATH_SEP
, -1);
859 filter_string_list(&ceiling_dirs
, 0,
860 canonicalize_ceiling_entry
, &empty_entry_found
);
861 ceil_offset
= longest_ancestor_length(cwd
.buf
, &ceiling_dirs
);
862 string_list_clear(&ceiling_dirs
, 0);
865 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
.buf
))
869 * Test in the following order (relative to the cwd):
870 * - .git (file containing "gitdir: <path>")
879 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
881 current_device
= get_device_or_die(".", NULL
, 0);
883 gitfile
= (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT
);
885 gitdirenv
= gitfile
= xstrdup(gitfile
);
887 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
888 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
892 ret
= setup_discovered_git_dir(gitdirenv
,
900 if (is_git_directory("."))
901 return setup_bare_git_dir(&cwd
, offset
, nongit_ok
);
903 offset_parent
= offset
;
904 while (--offset_parent
> ceil_offset
&& cwd
.buf
[offset_parent
] != '/');
905 if (offset_parent
<= ceil_offset
)
906 return setup_nongit(cwd
.buf
, nongit_ok
);
907 if (one_filesystem
) {
908 dev_t parent_device
= get_device_or_die("..", cwd
.buf
,
910 if (parent_device
!= current_device
) {
913 die_errno("Cannot come back to cwd");
917 strbuf_setlen(&cwd
, offset
);
918 die("Not a git repository (or any parent up to mount point %s)\n"
919 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).",
924 strbuf_setlen(&cwd
, offset
);
925 die_errno("Cannot change to '%s/..'", cwd
.buf
);
927 offset
= offset_parent
;
931 const char *setup_git_directory_gently(int *nongit_ok
)
935 prefix
= setup_git_directory_gently_1(nongit_ok
);
937 setenv(GIT_PREFIX_ENVIRONMENT
, prefix
, 1);
939 setenv(GIT_PREFIX_ENVIRONMENT
, "", 1);
942 startup_info
->have_repository
= !nongit_ok
|| !*nongit_ok
;
943 startup_info
->prefix
= prefix
;
948 int git_config_perm(const char *var
, const char *value
)
956 if (!strcmp(value
, "umask"))
958 if (!strcmp(value
, "group"))
960 if (!strcmp(value
, "all") ||
961 !strcmp(value
, "world") ||
962 !strcmp(value
, "everybody"))
963 return PERM_EVERYBODY
;
965 /* Parse octal numbers */
966 i
= strtol(value
, &endptr
, 8);
968 /* If not an octal number, maybe true/false? */
970 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
973 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
974 * a chmod value to restrict to.
977 case PERM_UMASK
: /* 0 */
979 case OLD_PERM_GROUP
: /* 1 */
981 case OLD_PERM_EVERYBODY
: /* 2 */
982 return PERM_EVERYBODY
;
985 /* A filemode value was given: 0xxx */
987 if ((i
& 0600) != 0600)
988 die("Problem with core.sharedRepository filemode value "
989 "(0%.3o).\nThe owner of files must always have "
990 "read and write permissions.", i
);
993 * Mask filemode value. Others can not get write permission.
994 * x flags for directories are handled separately.
999 void check_repository_format(void)
1001 check_repository_format_gently(get_git_dir(), NULL
);
1005 * Returns the "prefix", a path to the current working directory
1006 * relative to the work tree root, or NULL, if the current working
1007 * directory is not a strict subdirectory of the work tree root. The
1008 * prefix always ends with a '/' character.
1010 const char *setup_git_directory(void)
1012 return setup_git_directory_gently(NULL
);
1015 const char *resolve_gitdir(const char *suspect
)
1017 if (is_git_directory(suspect
))
1019 return read_gitfile(suspect
);
1022 /* if any standard file descriptor is missing open it to /dev/null */
1023 void sanitize_stdfds(void)
1025 int fd
= open("/dev/null", O_RDWR
, 0);
1026 while (fd
!= -1 && fd
< 2)
1029 die_errno("open /dev/null or dup failed");
1036 #ifdef NO_POSIX_GOODIES
1044 die_errno("fork failed");
1049 die_errno("setsid failed");