3 #include "environment.h"
5 #include "repository.h"
9 #include "string-list.h"
10 #include "chdir-notify.h"
11 #include "promisor-remote.h"
15 static int inside_git_dir
= -1;
16 static int inside_work_tree
= -1;
17 static int work_tree_config_is_bogus
;
18 enum allowed_bare_repo
{
19 ALLOWED_BARE_REPO_EXPLICIT
= 0,
20 ALLOWED_BARE_REPO_ALL
,
23 static struct startup_info the_startup_info
;
24 struct startup_info
*startup_info
= &the_startup_info
;
25 const char *tmp_original_cwd
;
28 * The input parameter must contain an absolute path, and it must already be
31 * Find the part of an absolute path that lies inside the work tree by
32 * dereferencing symlinks outside the work tree, for example:
33 * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
34 * /dir/file (work tree is /) -> dir/file
35 * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
36 * /dir/repolink/file (repolink points to /dir/repo) -> file
37 * /dir/repo (exactly equal to work tree) -> (empty string)
39 static int abspath_part_inside_repo(char *path
)
45 const char *work_tree
= get_git_work_tree();
46 struct strbuf realpath
= STRBUF_INIT
;
50 wtlen
= strlen(work_tree
);
52 off
= offset_1st_component(path
);
54 /* check if work tree is already the prefix */
55 if (wtlen
<= len
&& !fspathncmp(path
, work_tree
, wtlen
)) {
56 if (path
[wtlen
] == '/') {
57 memmove(path
, path
+ wtlen
+ 1, len
- wtlen
);
59 } else if (path
[wtlen
- 1] == '/' || path
[wtlen
] == '\0') {
60 /* work tree is the root, or the whole path */
61 memmove(path
, path
+ wtlen
, len
- wtlen
+ 1);
64 /* work tree might match beginning of a symlink to work tree */
70 /* check each '/'-terminated level */
75 strbuf_realpath(&realpath
, path0
, 1);
76 if (fspathcmp(realpath
.buf
, work_tree
) == 0) {
77 memmove(path0
, path
+ 1, len
- (path
- path0
));
78 strbuf_release(&realpath
);
85 /* check whole path */
86 strbuf_realpath(&realpath
, path0
, 1);
87 if (fspathcmp(realpath
.buf
, work_tree
) == 0) {
89 strbuf_release(&realpath
);
93 strbuf_release(&realpath
);
98 * Normalize "path", prepending the "prefix" for relative paths. If
99 * remaining_prefix is not NULL, return the actual prefix still
100 * remains in the path. For example, prefix = sub1/sub2/ and path is
102 * foo -> sub1/sub2/foo (full prefix)
103 * ../foo -> sub1/foo (remaining prefix is sub1/)
104 * ../../bar -> bar (no remaining prefix)
105 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
106 * `pwd`/../bar -> sub1/bar (no remaining prefix)
108 char *prefix_path_gently(const char *prefix
, int len
,
109 int *remaining_prefix
, const char *path
)
111 const char *orig
= path
;
113 if (is_absolute_path(orig
)) {
114 sanitized
= xmallocz(strlen(path
));
115 if (remaining_prefix
)
116 *remaining_prefix
= 0;
117 if (normalize_path_copy_len(sanitized
, path
, remaining_prefix
)) {
121 if (abspath_part_inside_repo(sanitized
)) {
126 sanitized
= xstrfmt("%.*s%s", len
, len
? prefix
: "", path
);
127 if (remaining_prefix
)
128 *remaining_prefix
= len
;
129 if (normalize_path_copy_len(sanitized
, sanitized
, remaining_prefix
)) {
137 char *prefix_path(const char *prefix
, int len
, const char *path
)
139 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
141 const char *hint_path
= get_git_work_tree();
143 hint_path
= get_git_dir();
144 die(_("'%s' is outside repository at '%s'"), path
,
145 absolute_path(hint_path
));
150 int path_inside_repo(const char *prefix
, const char *path
)
152 int len
= prefix
? strlen(prefix
) : 0;
153 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
161 int check_filename(const char *prefix
, const char *arg
)
163 char *to_free
= NULL
;
166 if (skip_prefix(arg
, ":/", &arg
)) {
167 if (!*arg
) /* ":/" is root dir, always exists */
170 } else if (skip_prefix(arg
, ":!", &arg
) ||
171 skip_prefix(arg
, ":^", &arg
)) {
172 if (!*arg
) /* excluding everything is silly, but allowed */
177 arg
= to_free
= prefix_filename(prefix
, arg
);
179 if (!lstat(arg
, &st
)) {
181 return 1; /* file exists */
183 if (is_missing_file_error(errno
)) {
185 return 0; /* file does not exist */
187 die_errno(_("failed to stat '%s'"), arg
);
190 static void NORETURN
die_verify_filename(struct repository
*r
,
193 int diagnose_misspelt_rev
)
195 if (!diagnose_misspelt_rev
)
196 die(_("%s: no such path in the working tree.\n"
197 "Use 'git <command> -- <path>...' to specify paths that do not exist locally."),
200 * Saying "'(icase)foo' does not exist in the index" when the
201 * user gave us ":(icase)foo" is just stupid. A magic pathspec
202 * begins with a colon and is followed by a non-alnum; do not
203 * let maybe_die_on_misspelt_object_name() even trigger.
205 if (!(arg
[0] == ':' && !isalnum(arg
[1])))
206 maybe_die_on_misspelt_object_name(r
, arg
, prefix
);
208 /* ... or fall back the most general message. */
209 die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
210 "Use '--' to separate paths from revisions, like this:\n"
211 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
216 * Check for arguments that don't resolve as actual files,
217 * but which look sufficiently like pathspecs that we'll consider
218 * them such for the purposes of rev/pathspec DWIM parsing.
220 static int looks_like_pathspec(const char *arg
)
226 * Wildcard characters imply the user is looking to match pathspecs
227 * that aren't in the filesystem. Note that this doesn't include
228 * backslash even though it's a glob special; by itself it doesn't
229 * cause any increase in the match. Likewise ignore backslash-escaped
230 * wildcard characters.
232 for (p
= arg
; *p
; p
++) {
235 } else if (is_glob_special(*p
)) {
243 /* long-form pathspec magic */
244 if (starts_with(arg
, ":("))
251 * Verify a filename that we got as an argument for a pathspec
252 * entry. Note that a filename that begins with "-" never verifies
253 * as true, because even if such a filename were to exist, we want
254 * it to be preceded by the "--" marker (or we want the user to
255 * use a format like "./-filename")
257 * The "diagnose_misspelt_rev" is used to provide a user-friendly
258 * diagnosis when dying upon finding that "name" is not a pathname.
259 * If set to 1, the diagnosis will try to diagnose "name" as an
260 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
261 * will only complain about an inexisting file.
263 * This function is typically called to check that a "file or rev"
264 * argument is unambiguous. In this case, the caller will want
265 * diagnose_misspelt_rev == 1 when verifying the first non-rev
266 * argument (which could have been a revision), and
267 * diagnose_misspelt_rev == 0 for the next ones (because we already
268 * saw a filename, there's not ambiguity anymore).
270 void verify_filename(const char *prefix
,
272 int diagnose_misspelt_rev
)
275 die(_("option '%s' must come before non-option arguments"), arg
);
276 if (looks_like_pathspec(arg
) || check_filename(prefix
, arg
))
278 die_verify_filename(the_repository
, prefix
, arg
, diagnose_misspelt_rev
);
282 * Opposite of the above: the command line did not have -- marker
283 * and we parsed the arg as a refname. It should not be interpretable
286 void verify_non_filename(const char *prefix
, const char *arg
)
288 if (!is_inside_work_tree() || is_inside_git_dir())
292 if (!check_filename(prefix
, arg
))
294 die(_("ambiguous argument '%s': both revision and filename\n"
295 "Use '--' to separate paths from revisions, like this:\n"
296 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
299 int get_common_dir(struct strbuf
*sb
, const char *gitdir
)
301 const char *git_env_common_dir
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
302 if (git_env_common_dir
) {
303 strbuf_addstr(sb
, git_env_common_dir
);
306 return get_common_dir_noenv(sb
, gitdir
);
310 int get_common_dir_noenv(struct strbuf
*sb
, const char *gitdir
)
312 struct strbuf data
= STRBUF_INIT
;
313 struct strbuf path
= STRBUF_INIT
;
316 strbuf_addf(&path
, "%s/commondir", gitdir
);
317 if (file_exists(path
.buf
)) {
318 if (strbuf_read_file(&data
, path
.buf
, 0) <= 0)
319 die_errno(_("failed to read %s"), path
.buf
);
320 while (data
.len
&& (data
.buf
[data
.len
- 1] == '\n' ||
321 data
.buf
[data
.len
- 1] == '\r'))
323 data
.buf
[data
.len
] = '\0';
325 if (!is_absolute_path(data
.buf
))
326 strbuf_addf(&path
, "%s/", gitdir
);
327 strbuf_addbuf(&path
, &data
);
328 strbuf_add_real_path(sb
, path
.buf
);
331 strbuf_addstr(sb
, gitdir
);
334 strbuf_release(&data
);
335 strbuf_release(&path
);
340 * Test if it looks like we're at a git directory.
343 * - either an objects/ directory _or_ the proper
344 * GIT_OBJECT_DIRECTORY environment variable
345 * - a refs/ directory
346 * - either a HEAD symlink or a HEAD file that is formatted as
347 * a proper "ref:", or a regular file HEAD that has a properly
348 * formatted sha1 object name.
350 int is_git_directory(const char *suspect
)
352 struct strbuf path
= STRBUF_INIT
;
356 /* Check worktree-related signatures */
357 strbuf_addstr(&path
, suspect
);
358 strbuf_complete(&path
, '/');
359 strbuf_addstr(&path
, "HEAD");
360 if (validate_headref(path
.buf
))
364 get_common_dir(&path
, suspect
);
367 /* Check non-worktree-related signatures */
368 if (getenv(DB_ENVIRONMENT
)) {
369 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
373 strbuf_setlen(&path
, len
);
374 strbuf_addstr(&path
, "/objects");
375 if (access(path
.buf
, X_OK
))
379 strbuf_setlen(&path
, len
);
380 strbuf_addstr(&path
, "/refs");
381 if (access(path
.buf
, X_OK
))
386 strbuf_release(&path
);
390 int is_nonbare_repository_dir(struct strbuf
*path
)
394 size_t orig_path_len
= path
->len
;
395 assert(orig_path_len
!= 0);
396 strbuf_complete(path
, '/');
397 strbuf_addstr(path
, ".git");
398 if (read_gitfile_gently(path
->buf
, &gitfile_error
) || is_git_directory(path
->buf
))
400 if (gitfile_error
== READ_GITFILE_ERR_OPEN_FAILED
||
401 gitfile_error
== READ_GITFILE_ERR_READ_FAILED
)
403 strbuf_setlen(path
, orig_path_len
);
407 int is_inside_git_dir(void)
409 if (inside_git_dir
< 0)
410 inside_git_dir
= is_inside_dir(get_git_dir());
411 return inside_git_dir
;
414 int is_inside_work_tree(void)
416 if (inside_work_tree
< 0)
417 inside_work_tree
= is_inside_dir(get_git_work_tree());
418 return inside_work_tree
;
421 void setup_work_tree(void)
423 const char *work_tree
;
424 static int initialized
= 0;
429 if (work_tree_config_is_bogus
)
430 die(_("unable to set up work tree using invalid config"));
432 work_tree
= get_git_work_tree();
433 if (!work_tree
|| chdir_notify(work_tree
))
434 die(_("this operation must be run in a work tree"));
437 * Make sure subsequent git processes find correct worktree
438 * if $GIT_WORK_TREE is set relative
440 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
441 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
446 static void setup_original_cwd(void)
448 struct strbuf tmp
= STRBUF_INIT
;
449 const char *worktree
= NULL
;
452 if (!tmp_original_cwd
)
456 * startup_info->original_cwd points to the current working
457 * directory we inherited from our parent process, which is a
458 * directory we want to avoid removing.
460 * For convience, we would like to have the path relative to the
461 * worktree instead of an absolute path.
463 * Yes, startup_info->original_cwd is usually the same as 'prefix',
464 * but differs in two ways:
465 * - prefix has a trailing '/'
466 * - if the user passes '-C' to git, that modifies the prefix but
467 * not startup_info->original_cwd.
470 /* Normalize the directory */
471 if (!strbuf_realpath(&tmp
, tmp_original_cwd
, 0)) {
472 trace2_data_string("setup", the_repository
,
473 "realpath-path", tmp_original_cwd
);
474 trace2_data_string("setup", the_repository
,
475 "realpath-failure", strerror(errno
));
476 free((char*)tmp_original_cwd
);
477 tmp_original_cwd
= NULL
;
481 free((char*)tmp_original_cwd
);
482 tmp_original_cwd
= NULL
;
483 startup_info
->original_cwd
= strbuf_detach(&tmp
, NULL
);
486 * Get our worktree; we only protect the current working directory
487 * if it's in the worktree.
489 worktree
= get_git_work_tree();
491 goto no_prevention_needed
;
493 offset
= dir_inside_of(startup_info
->original_cwd
, worktree
);
496 * If startup_info->original_cwd == worktree, that is already
497 * protected and we don't need original_cwd as a secondary
498 * protection measure.
500 if (!*(startup_info
->original_cwd
+ offset
))
501 goto no_prevention_needed
;
504 * original_cwd was inside worktree; precompose it just as
505 * we do prefix so that built up paths will match
507 startup_info
->original_cwd
= \
508 precompose_string_if_needed(startup_info
->original_cwd
513 no_prevention_needed
:
514 free((char*)startup_info
->original_cwd
);
515 startup_info
->original_cwd
= NULL
;
518 static int read_worktree_config(const char *var
, const char *value
, void *vdata
)
520 struct repository_format
*data
= vdata
;
522 if (strcmp(var
, "core.bare") == 0) {
523 data
->is_bare
= git_config_bool(var
, value
);
524 } else if (strcmp(var
, "core.worktree") == 0) {
526 return config_error_nonbool(var
);
527 free(data
->work_tree
);
528 data
->work_tree
= xstrdup(value
);
533 enum extension_result
{
534 EXTENSION_ERROR
= -1, /* compatible with error(), etc */
535 EXTENSION_UNKNOWN
= 0,
540 * Do not add new extensions to this function. It handles extensions which are
541 * respected even in v0-format repositories for historical compatibility.
543 static enum extension_result
handle_extension_v0(const char *var
,
546 struct repository_format
*data
)
548 if (!strcmp(ext
, "noop")) {
550 } else if (!strcmp(ext
, "preciousobjects")) {
551 data
->precious_objects
= git_config_bool(var
, value
);
553 } else if (!strcmp(ext
, "partialclone")) {
554 data
->partial_clone
= xstrdup(value
);
556 } else if (!strcmp(ext
, "worktreeconfig")) {
557 data
->worktree_config
= git_config_bool(var
, value
);
561 return EXTENSION_UNKNOWN
;
565 * Record any new extensions in this function.
567 static enum extension_result
handle_extension(const char *var
,
570 struct repository_format
*data
)
572 if (!strcmp(ext
, "noop-v1")) {
574 } else if (!strcmp(ext
, "objectformat")) {
578 return config_error_nonbool(var
);
579 format
= hash_algo_by_name(value
);
580 if (format
== GIT_HASH_UNKNOWN
)
581 return error(_("invalid value for '%s': '%s'"),
582 "extensions.objectformat", value
);
583 data
->hash_algo
= format
;
586 return EXTENSION_UNKNOWN
;
589 static int check_repo_format(const char *var
, const char *value
, void *vdata
)
591 struct repository_format
*data
= vdata
;
594 if (strcmp(var
, "core.repositoryformatversion") == 0)
595 data
->version
= git_config_int(var
, value
);
596 else if (skip_prefix(var
, "extensions.", &ext
)) {
597 switch (handle_extension_v0(var
, value
, ext
, data
)) {
598 case EXTENSION_ERROR
:
602 case EXTENSION_UNKNOWN
:
606 switch (handle_extension(var
, value
, ext
, data
)) {
607 case EXTENSION_ERROR
:
610 string_list_append(&data
->v1_only_extensions
, ext
);
612 case EXTENSION_UNKNOWN
:
613 string_list_append(&data
->unknown_extensions
, ext
);
618 return read_worktree_config(var
, value
, vdata
);
621 static int check_repository_format_gently(const char *gitdir
, struct repository_format
*candidate
, int *nongit_ok
)
623 struct strbuf sb
= STRBUF_INIT
;
624 struct strbuf err
= STRBUF_INIT
;
627 has_common
= get_common_dir(&sb
, gitdir
);
628 strbuf_addstr(&sb
, "/config");
629 read_repository_format(candidate
, sb
.buf
);
633 * For historical use of check_repository_format() in git-init,
634 * we treat a missing config as a silent "ok", even when nongit_ok
637 if (candidate
->version
< 0)
640 if (verify_repository_format(candidate
, &err
) < 0) {
642 warning("%s", err
.buf
);
643 strbuf_release(&err
);
650 repository_format_precious_objects
= candidate
->precious_objects
;
651 repository_format_worktree_config
= candidate
->worktree_config
;
652 string_list_clear(&candidate
->unknown_extensions
, 0);
653 string_list_clear(&candidate
->v1_only_extensions
, 0);
655 if (repository_format_worktree_config
) {
657 * pick up core.bare and core.worktree from per-worktree
660 strbuf_addf(&sb
, "%s/config.worktree", gitdir
);
661 git_config_from_file(read_worktree_config
, sb
.buf
, candidate
);
667 if (candidate
->is_bare
!= -1) {
668 is_bare_repository_cfg
= candidate
->is_bare
;
669 if (is_bare_repository_cfg
== 1)
670 inside_work_tree
= -1;
672 if (candidate
->work_tree
) {
673 free(git_work_tree_cfg
);
674 git_work_tree_cfg
= xstrdup(candidate
->work_tree
);
675 inside_work_tree
= -1;
682 int upgrade_repository_format(int target_version
)
684 struct strbuf sb
= STRBUF_INIT
;
685 struct strbuf err
= STRBUF_INIT
;
686 struct strbuf repo_version
= STRBUF_INIT
;
687 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
689 strbuf_git_common_path(&sb
, the_repository
, "config");
690 read_repository_format(&repo_fmt
, sb
.buf
);
693 if (repo_fmt
.version
>= target_version
)
696 if (verify_repository_format(&repo_fmt
, &err
) < 0) {
697 error("cannot upgrade repository format from %d to %d: %s",
698 repo_fmt
.version
, target_version
, err
.buf
);
699 strbuf_release(&err
);
702 if (!repo_fmt
.version
&& repo_fmt
.unknown_extensions
.nr
)
703 return error("cannot upgrade repository format: "
704 "unknown extension %s",
705 repo_fmt
.unknown_extensions
.items
[0].string
);
707 strbuf_addf(&repo_version
, "%d", target_version
);
708 git_config_set("core.repositoryformatversion", repo_version
.buf
);
709 strbuf_release(&repo_version
);
713 static void init_repository_format(struct repository_format
*format
)
715 const struct repository_format fresh
= REPOSITORY_FORMAT_INIT
;
717 memcpy(format
, &fresh
, sizeof(fresh
));
720 int read_repository_format(struct repository_format
*format
, const char *path
)
722 clear_repository_format(format
);
723 git_config_from_file(check_repo_format
, path
, format
);
724 if (format
->version
== -1)
725 clear_repository_format(format
);
726 return format
->version
;
729 void clear_repository_format(struct repository_format
*format
)
731 string_list_clear(&format
->unknown_extensions
, 0);
732 string_list_clear(&format
->v1_only_extensions
, 0);
733 free(format
->work_tree
);
734 free(format
->partial_clone
);
735 init_repository_format(format
);
738 int verify_repository_format(const struct repository_format
*format
,
741 if (GIT_REPO_VERSION_READ
< format
->version
) {
742 strbuf_addf(err
, _("Expected git repo version <= %d, found %d"),
743 GIT_REPO_VERSION_READ
, format
->version
);
747 if (format
->version
>= 1 && format
->unknown_extensions
.nr
) {
750 strbuf_addstr(err
, Q_("unknown repository extension found:",
751 "unknown repository extensions found:",
752 format
->unknown_extensions
.nr
));
754 for (i
= 0; i
< format
->unknown_extensions
.nr
; i
++)
755 strbuf_addf(err
, "\n\t%s",
756 format
->unknown_extensions
.items
[i
].string
);
760 if (format
->version
== 0 && format
->v1_only_extensions
.nr
) {
764 Q_("repo version is 0, but v1-only extension found:",
765 "repo version is 0, but v1-only extensions found:",
766 format
->v1_only_extensions
.nr
));
768 for (i
= 0; i
< format
->v1_only_extensions
.nr
; i
++)
769 strbuf_addf(err
, "\n\t%s",
770 format
->v1_only_extensions
.items
[i
].string
);
777 void read_gitfile_error_die(int error_code
, const char *path
, const char *dir
)
779 switch (error_code
) {
780 case READ_GITFILE_ERR_STAT_FAILED
:
781 case READ_GITFILE_ERR_NOT_A_FILE
:
782 /* non-fatal; follow return path */
784 case READ_GITFILE_ERR_OPEN_FAILED
:
785 die_errno(_("error opening '%s'"), path
);
786 case READ_GITFILE_ERR_TOO_LARGE
:
787 die(_("too large to be a .git file: '%s'"), path
);
788 case READ_GITFILE_ERR_READ_FAILED
:
789 die(_("error reading %s"), path
);
790 case READ_GITFILE_ERR_INVALID_FORMAT
:
791 die(_("invalid gitfile format: %s"), path
);
792 case READ_GITFILE_ERR_NO_PATH
:
793 die(_("no path in gitfile: %s"), path
);
794 case READ_GITFILE_ERR_NOT_A_REPO
:
795 die(_("not a git repository: %s"), dir
);
797 BUG("unknown error code");
802 * Try to read the location of the git directory from the .git file,
803 * return path to git directory if found. The return value comes from
806 * On failure, if return_error_code is not NULL, return_error_code
807 * will be set to an error code and NULL will be returned. If
808 * return_error_code is NULL the function will die instead (for most
811 const char *read_gitfile_gently(const char *path
, int *return_error_code
)
813 const int max_file_size
= 1 << 20; /* 1MB */
821 static struct strbuf realpath
= STRBUF_INIT
;
823 if (stat(path
, &st
)) {
824 /* NEEDSWORK: discern between ENOENT vs other errors */
825 error_code
= READ_GITFILE_ERR_STAT_FAILED
;
828 if (!S_ISREG(st
.st_mode
)) {
829 error_code
= READ_GITFILE_ERR_NOT_A_FILE
;
832 if (st
.st_size
> max_file_size
) {
833 error_code
= READ_GITFILE_ERR_TOO_LARGE
;
836 fd
= open(path
, O_RDONLY
);
838 error_code
= READ_GITFILE_ERR_OPEN_FAILED
;
841 buf
= xmallocz(st
.st_size
);
842 len
= read_in_full(fd
, buf
, st
.st_size
);
844 if (len
!= st
.st_size
) {
845 error_code
= READ_GITFILE_ERR_READ_FAILED
;
848 if (!starts_with(buf
, "gitdir: ")) {
849 error_code
= READ_GITFILE_ERR_INVALID_FORMAT
;
852 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
855 error_code
= READ_GITFILE_ERR_NO_PATH
;
861 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
862 size_t pathlen
= slash
+1 - path
;
863 dir
= xstrfmt("%.*s%.*s", (int)pathlen
, path
,
864 (int)(len
- 8), buf
+ 8);
868 if (!is_git_directory(dir
)) {
869 error_code
= READ_GITFILE_ERR_NOT_A_REPO
;
873 strbuf_realpath(&realpath
, dir
, 1);
877 if (return_error_code
)
878 *return_error_code
= error_code
;
880 read_gitfile_error_die(error_code
, path
, dir
);
883 return error_code
? NULL
: path
;
886 static const char *setup_explicit_git_dir(const char *gitdirenv
,
888 struct repository_format
*repo_fmt
,
891 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
892 const char *worktree
;
896 if (PATH_MAX
- 40 < strlen(gitdirenv
))
897 die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT
);
899 gitfile
= (char*)read_gitfile(gitdirenv
);
901 gitfile
= xstrdup(gitfile
);
905 if (!is_git_directory(gitdirenv
)) {
911 die(_("not a git repository: '%s'"), gitdirenv
);
914 if (check_repository_format_gently(gitdirenv
, repo_fmt
, nongit_ok
)) {
919 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
921 set_git_work_tree(work_tree_env
);
922 else if (is_bare_repository_cfg
> 0) {
923 if (git_work_tree_cfg
) {
925 warning("core.bare and core.worktree do not make sense");
926 work_tree_config_is_bogus
= 1;
930 set_git_dir(gitdirenv
, 0);
934 else if (git_work_tree_cfg
) { /* #6, #14 */
935 if (is_absolute_path(git_work_tree_cfg
))
936 set_git_work_tree(git_work_tree_cfg
);
939 if (chdir(gitdirenv
))
940 die_errno(_("cannot chdir to '%s'"), gitdirenv
);
941 if (chdir(git_work_tree_cfg
))
942 die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg
);
943 core_worktree
= xgetcwd();
945 die_errno(_("cannot come back to cwd"));
946 set_git_work_tree(core_worktree
);
950 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, 1)) {
952 set_git_dir(gitdirenv
, 0);
957 set_git_work_tree(".");
959 /* set_git_work_tree() must have been called by now */
960 worktree
= get_git_work_tree();
962 /* both get_git_work_tree() and cwd are already normalized */
963 if (!strcmp(cwd
->buf
, worktree
)) { /* cwd == worktree */
964 set_git_dir(gitdirenv
, 0);
969 offset
= dir_inside_of(cwd
->buf
, worktree
);
970 if (offset
>= 0) { /* cwd inside worktree? */
971 set_git_dir(gitdirenv
, 1);
973 die_errno(_("cannot chdir to '%s'"), worktree
);
974 strbuf_addch(cwd
, '/');
976 return cwd
->buf
+ offset
;
979 /* cwd outside worktree */
980 set_git_dir(gitdirenv
, 0);
985 static const char *setup_discovered_git_dir(const char *gitdir
,
986 struct strbuf
*cwd
, int offset
,
987 struct repository_format
*repo_fmt
,
990 if (check_repository_format_gently(gitdir
, repo_fmt
, nongit_ok
))
993 /* --work-tree is set without --git-dir; use discovered one */
994 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
995 char *to_free
= NULL
;
998 if (offset
!= cwd
->len
&& !is_absolute_path(gitdir
))
999 gitdir
= to_free
= real_pathdup(gitdir
, 1);
1000 if (chdir(cwd
->buf
))
1001 die_errno(_("cannot come back to cwd"));
1002 ret
= setup_explicit_git_dir(gitdir
, cwd
, repo_fmt
, nongit_ok
);
1007 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
1008 if (is_bare_repository_cfg
> 0) {
1009 set_git_dir(gitdir
, (offset
!= cwd
->len
));
1010 if (chdir(cwd
->buf
))
1011 die_errno(_("cannot come back to cwd"));
1015 /* #0, #1, #5, #8, #9, #12, #13 */
1016 set_git_work_tree(".");
1017 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
1018 set_git_dir(gitdir
, 0);
1020 inside_work_tree
= 1;
1021 if (offset
>= cwd
->len
)
1024 /* Make "offset" point past the '/' (already the case for root dirs) */
1025 if (offset
!= offset_1st_component(cwd
->buf
))
1027 /* Add a '/' at the end */
1028 strbuf_addch(cwd
, '/');
1029 return cwd
->buf
+ offset
;
1032 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
1033 static const char *setup_bare_git_dir(struct strbuf
*cwd
, int offset
,
1034 struct repository_format
*repo_fmt
,
1039 if (check_repository_format_gently(".", repo_fmt
, nongit_ok
))
1042 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
1044 /* --work-tree is set without --git-dir; use discovered one */
1045 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
1046 static const char *gitdir
;
1048 gitdir
= offset
== cwd
->len
? "." : xmemdupz(cwd
->buf
, offset
);
1049 if (chdir(cwd
->buf
))
1050 die_errno(_("cannot come back to cwd"));
1051 return setup_explicit_git_dir(gitdir
, cwd
, repo_fmt
, nongit_ok
);
1055 inside_work_tree
= 0;
1056 if (offset
!= cwd
->len
) {
1057 if (chdir(cwd
->buf
))
1058 die_errno(_("cannot come back to cwd"));
1059 root_len
= offset_1st_component(cwd
->buf
);
1060 strbuf_setlen(cwd
, offset
> root_len
? offset
: root_len
);
1061 set_git_dir(cwd
->buf
, 0);
1064 set_git_dir(".", 0);
1068 static dev_t
get_device_or_die(const char *path
, const char *prefix
, int prefix_len
)
1071 if (stat(path
, &buf
)) {
1072 die_errno(_("failed to stat '%*s%s%s'"),
1074 prefix
? prefix
: "",
1075 prefix
? "/" : "", path
);
1081 * A "string_list_each_func_t" function that canonicalizes an entry
1082 * from GIT_CEILING_DIRECTORIES using real_pathdup(), or
1083 * discards it if unusable. The presence of an empty entry in
1084 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
1085 * subsequent entries.
1087 static int canonicalize_ceiling_entry(struct string_list_item
*item
,
1090 int *empty_entry_found
= cb_data
;
1091 char *ceil
= item
->string
;
1094 *empty_entry_found
= 1;
1096 } else if (!is_absolute_path(ceil
)) {
1098 } else if (*empty_entry_found
) {
1099 /* Keep entry but do not canonicalize it */
1102 char *real_path
= real_pathdup(ceil
, 0);
1107 item
->string
= real_path
;
1112 struct safe_directory_data
{
1117 static int safe_directory_cb(const char *key
, const char *value
, void *d
)
1119 struct safe_directory_data
*data
= d
;
1121 if (strcmp(key
, "safe.directory"))
1124 if (!value
|| !*value
) {
1126 } else if (!strcmp(value
, "*")) {
1129 const char *interpolated
= NULL
;
1131 if (!git_config_pathname(&interpolated
, key
, value
) &&
1132 !fspathcmp(data
->path
, interpolated
? interpolated
: value
))
1135 free((char *)interpolated
);
1142 * Check if a repository is safe, by verifying the ownership of the
1143 * worktree (if any), the git directory, and the gitfile (if any).
1145 * Exemptions for known-safe repositories can be added via `safe.directory`
1146 * config settings; for non-bare repositories, their worktree needs to be
1147 * added, for bare ones their git directory.
1149 static int ensure_valid_ownership(const char *gitfile
,
1150 const char *worktree
, const char *gitdir
,
1151 struct strbuf
*report
)
1153 struct safe_directory_data data
= {
1154 .path
= worktree
? worktree
: gitdir
1157 if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
1158 (!gitfile
|| is_path_owned_by_current_user(gitfile
, report
)) &&
1159 (!worktree
|| is_path_owned_by_current_user(worktree
, report
)) &&
1160 (!gitdir
|| is_path_owned_by_current_user(gitdir
, report
)))
1164 * data.path is the "path" that identifies the repository and it is
1165 * constant regardless of what failed above. data.is_safe should be
1166 * initialized to false, and might be changed by the callback.
1168 git_protected_config(safe_directory_cb
, &data
);
1170 return data
.is_safe
;
1173 static int allowed_bare_repo_cb(const char *key
, const char *value
, void *d
)
1175 enum allowed_bare_repo
*allowed_bare_repo
= d
;
1177 if (strcasecmp(key
, "safe.bareRepository"))
1180 if (!strcmp(value
, "explicit")) {
1181 *allowed_bare_repo
= ALLOWED_BARE_REPO_EXPLICIT
;
1184 if (!strcmp(value
, "all")) {
1185 *allowed_bare_repo
= ALLOWED_BARE_REPO_ALL
;
1191 static enum allowed_bare_repo
get_allowed_bare_repo(void)
1193 enum allowed_bare_repo result
= ALLOWED_BARE_REPO_ALL
;
1194 git_protected_config(allowed_bare_repo_cb
, &result
);
1198 static const char *allowed_bare_repo_to_string(
1199 enum allowed_bare_repo allowed_bare_repo
)
1201 switch (allowed_bare_repo
) {
1202 case ALLOWED_BARE_REPO_EXPLICIT
:
1204 case ALLOWED_BARE_REPO_ALL
:
1207 BUG("invalid allowed_bare_repo %d",
1213 enum discovery_result
{
1218 /* these are errors */
1219 GIT_DIR_HIT_CEILING
= -1,
1220 GIT_DIR_HIT_MOUNT_POINT
= -2,
1221 GIT_DIR_INVALID_GITFILE
= -3,
1222 GIT_DIR_INVALID_OWNERSHIP
= -4,
1223 GIT_DIR_DISALLOWED_BARE
= -5,
1227 * We cannot decide in this function whether we are in the work tree or
1228 * not, since the config can only be read _after_ this function was called.
1230 * Also, we avoid changing any global state (such as the current working
1231 * directory) to allow early callers.
1233 * The directory where the search should start needs to be passed in via the
1234 * `dir` parameter; upon return, the `dir` buffer will contain the path of
1235 * the directory where the search ended, and `gitdir` will contain the path of
1236 * the discovered .git/ directory, if any. If `gitdir` is not absolute, it
1237 * is relative to `dir` (i.e. *not* necessarily the cwd).
1239 static enum discovery_result
setup_git_directory_gently_1(struct strbuf
*dir
,
1240 struct strbuf
*gitdir
,
1241 struct strbuf
*report
,
1244 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
1245 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
1246 const char *gitdirenv
;
1247 int ceil_offset
= -1, min_offset
= offset_1st_component(dir
->buf
);
1248 dev_t current_device
= 0;
1249 int one_filesystem
= 1;
1252 * If GIT_DIR is set explicitly, we're not going
1253 * to do any discovery, but we still do repository
1256 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
1258 strbuf_addstr(gitdir
, gitdirenv
);
1259 return GIT_DIR_EXPLICIT
;
1262 if (env_ceiling_dirs
) {
1263 int empty_entry_found
= 0;
1265 string_list_split(&ceiling_dirs
, env_ceiling_dirs
, PATH_SEP
, -1);
1266 filter_string_list(&ceiling_dirs
, 0,
1267 canonicalize_ceiling_entry
, &empty_entry_found
);
1268 ceil_offset
= longest_ancestor_length(dir
->buf
, &ceiling_dirs
);
1269 string_list_clear(&ceiling_dirs
, 0);
1272 if (ceil_offset
< 0)
1273 ceil_offset
= min_offset
- 2;
1275 if (min_offset
&& min_offset
== dir
->len
&&
1276 !is_dir_sep(dir
->buf
[min_offset
- 1])) {
1277 strbuf_addch(dir
, '/');
1282 * Test in the following order (relative to the dir):
1283 * - .git (file containing "gitdir: <path>")
1292 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
1294 current_device
= get_device_or_die(dir
->buf
, NULL
, 0);
1296 int offset
= dir
->len
, error_code
= 0;
1297 char *gitdir_path
= NULL
;
1298 char *gitfile
= NULL
;
1300 if (offset
> min_offset
)
1301 strbuf_addch(dir
, '/');
1302 strbuf_addstr(dir
, DEFAULT_GIT_DIR_ENVIRONMENT
);
1303 gitdirenv
= read_gitfile_gently(dir
->buf
, die_on_error
?
1304 NULL
: &error_code
);
1307 error_code
== READ_GITFILE_ERR_NOT_A_FILE
) {
1308 /* NEEDSWORK: fail if .git is not file nor dir */
1309 if (is_git_directory(dir
->buf
)) {
1310 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
1311 gitdir_path
= xstrdup(dir
->buf
);
1313 } else if (error_code
!= READ_GITFILE_ERR_STAT_FAILED
)
1314 return GIT_DIR_INVALID_GITFILE
;
1316 gitfile
= xstrdup(dir
->buf
);
1318 * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
1319 * to check that directory for a repository.
1320 * Now trim that tentative addition away, because we want to
1321 * focus on the real directory we are in.
1323 strbuf_setlen(dir
, offset
);
1325 enum discovery_result ret
;
1326 const char *gitdir_candidate
=
1327 gitdir_path
? gitdir_path
: gitdirenv
;
1329 if (ensure_valid_ownership(gitfile
, dir
->buf
,
1330 gitdir_candidate
, report
)) {
1331 strbuf_addstr(gitdir
, gitdirenv
);
1332 ret
= GIT_DIR_DISCOVERED
;
1334 ret
= GIT_DIR_INVALID_OWNERSHIP
;
1337 * Earlier, during discovery, we might have allocated
1338 * string copies for gitdir_path or gitfile so make
1339 * sure we don't leak by freeing them now, before
1340 * leaving the loop and function.
1342 * Note: gitdirenv will be non-NULL whenever these are
1343 * allocated, therefore we need not take care of releasing
1344 * them outside of this conditional block.
1352 if (is_git_directory(dir
->buf
)) {
1353 if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT
)
1354 return GIT_DIR_DISALLOWED_BARE
;
1355 if (!ensure_valid_ownership(NULL
, NULL
, dir
->buf
, report
))
1356 return GIT_DIR_INVALID_OWNERSHIP
;
1357 strbuf_addstr(gitdir
, ".");
1358 return GIT_DIR_BARE
;
1361 if (offset
<= min_offset
)
1362 return GIT_DIR_HIT_CEILING
;
1364 while (--offset
> ceil_offset
&& !is_dir_sep(dir
->buf
[offset
]))
1366 if (offset
<= ceil_offset
)
1367 return GIT_DIR_HIT_CEILING
;
1369 strbuf_setlen(dir
, offset
> min_offset
? offset
: min_offset
);
1370 if (one_filesystem
&&
1371 current_device
!= get_device_or_die(dir
->buf
, NULL
, offset
))
1372 return GIT_DIR_HIT_MOUNT_POINT
;
1376 int discover_git_directory(struct strbuf
*commondir
,
1377 struct strbuf
*gitdir
)
1379 struct strbuf dir
= STRBUF_INIT
, err
= STRBUF_INIT
;
1380 size_t gitdir_offset
= gitdir
->len
, cwd_len
;
1381 size_t commondir_offset
= commondir
->len
;
1382 struct repository_format candidate
= REPOSITORY_FORMAT_INIT
;
1384 if (strbuf_getcwd(&dir
))
1388 if (setup_git_directory_gently_1(&dir
, gitdir
, NULL
, 0) <= 0) {
1389 strbuf_release(&dir
);
1394 * The returned gitdir is relative to dir, and if dir does not reflect
1395 * the current working directory, we simply make the gitdir absolute.
1397 if (dir
.len
< cwd_len
&& !is_absolute_path(gitdir
->buf
+ gitdir_offset
)) {
1398 /* Avoid a trailing "/." */
1399 if (!strcmp(".", gitdir
->buf
+ gitdir_offset
))
1400 strbuf_setlen(gitdir
, gitdir_offset
);
1402 strbuf_addch(&dir
, '/');
1403 strbuf_insert(gitdir
, gitdir_offset
, dir
.buf
, dir
.len
);
1406 get_common_dir(commondir
, gitdir
->buf
+ gitdir_offset
);
1409 strbuf_addf(&dir
, "%s/config", commondir
->buf
+ commondir_offset
);
1410 read_repository_format(&candidate
, dir
.buf
);
1411 strbuf_release(&dir
);
1413 if (verify_repository_format(&candidate
, &err
) < 0) {
1414 warning("ignoring git dir '%s': %s",
1415 gitdir
->buf
+ gitdir_offset
, err
.buf
);
1416 strbuf_release(&err
);
1417 strbuf_setlen(commondir
, commondir_offset
);
1418 strbuf_setlen(gitdir
, gitdir_offset
);
1419 clear_repository_format(&candidate
);
1423 /* take ownership of candidate.partial_clone */
1424 the_repository
->repository_format_partial_clone
=
1425 candidate
.partial_clone
;
1426 candidate
.partial_clone
= NULL
;
1428 clear_repository_format(&candidate
);
1432 const char *setup_git_directory_gently(int *nongit_ok
)
1434 static struct strbuf cwd
= STRBUF_INIT
;
1435 struct strbuf dir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
, report
= STRBUF_INIT
;
1436 const char *prefix
= NULL
;
1437 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
1440 * We may have read an incomplete configuration before
1441 * setting-up the git directory. If so, clear the cache so
1442 * that the next queries to the configuration reload complete
1443 * configuration (including the per-repo config file that we
1444 * ignored previously).
1449 * Let's assume that we are in a git repository.
1450 * If it turns out later that we are somewhere else, the value will be
1451 * updated accordingly.
1456 if (strbuf_getcwd(&cwd
))
1457 die_errno(_("Unable to read current working directory"));
1458 strbuf_addbuf(&dir
, &cwd
);
1460 switch (setup_git_directory_gently_1(&dir
, &gitdir
, &report
, 1)) {
1461 case GIT_DIR_EXPLICIT
:
1462 prefix
= setup_explicit_git_dir(gitdir
.buf
, &cwd
, &repo_fmt
, nongit_ok
);
1464 case GIT_DIR_DISCOVERED
:
1465 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1466 die(_("cannot change to '%s'"), dir
.buf
);
1467 prefix
= setup_discovered_git_dir(gitdir
.buf
, &cwd
, dir
.len
,
1468 &repo_fmt
, nongit_ok
);
1471 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1472 die(_("cannot change to '%s'"), dir
.buf
);
1473 prefix
= setup_bare_git_dir(&cwd
, dir
.len
, &repo_fmt
, nongit_ok
);
1475 case GIT_DIR_HIT_CEILING
:
1477 die(_("not a git repository (or any of the parent directories): %s"),
1478 DEFAULT_GIT_DIR_ENVIRONMENT
);
1481 case GIT_DIR_HIT_MOUNT_POINT
:
1483 die(_("not a git repository (or any parent up to mount point %s)\n"
1484 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
1488 case GIT_DIR_INVALID_OWNERSHIP
:
1490 struct strbuf quoted
= STRBUF_INIT
;
1492 strbuf_complete(&report
, '\n');
1493 sq_quote_buf_pretty("ed
, dir
.buf
);
1494 die(_("detected dubious ownership in repository at '%s'\n"
1496 "To add an exception for this directory, call:\n"
1498 "\tgit config --global --add safe.directory %s"),
1499 dir
.buf
, report
.buf
, quoted
.buf
);
1503 case GIT_DIR_DISALLOWED_BARE
:
1505 die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"),
1507 allowed_bare_repo_to_string(get_allowed_bare_repo()));
1513 * As a safeguard against setup_git_directory_gently_1 returning
1514 * this value, fallthrough to BUG. Otherwise it is possible to
1515 * set startup_info->have_repository to 1 when we did nothing to
1516 * find a repository.
1519 BUG("unhandled setup_git_directory_gently_1() result");
1523 * At this point, nongit_ok is stable. If it is non-NULL and points
1524 * to a non-zero value, then this means that we haven't found a
1525 * repository and that the caller expects startup_info to reflect
1528 * Regardless of the state of nongit_ok, startup_info->prefix and
1529 * the GIT_PREFIX environment variable must always match. For details
1530 * see Documentation/config/alias.txt.
1532 if (nongit_ok
&& *nongit_ok
)
1533 startup_info
->have_repository
= 0;
1535 startup_info
->have_repository
= 1;
1538 * Not all paths through the setup code will call 'set_git_dir()' (which
1539 * directly sets up the environment) so in order to guarantee that the
1540 * environment is in a consistent state after setup, explicitly setup
1541 * the environment if we have a repository.
1543 * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
1544 * code paths so we also need to explicitly setup the environment if
1545 * the user has set GIT_DIR. It may be beneficial to disallow bogus
1546 * GIT_DIR values at some point in the future.
1548 if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */
1549 startup_info
->have_repository
||
1550 /* GIT_DIR_EXPLICIT */
1551 getenv(GIT_DIR_ENVIRONMENT
)) {
1552 if (!the_repository
->gitdir
) {
1553 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
1555 gitdir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
1556 setup_git_env(gitdir
);
1558 if (startup_info
->have_repository
) {
1559 repo_set_hash_algo(the_repository
, repo_fmt
.hash_algo
);
1560 /* take ownership of repo_fmt.partial_clone */
1561 the_repository
->repository_format_partial_clone
=
1562 repo_fmt
.partial_clone
;
1563 repo_fmt
.partial_clone
= NULL
;
1567 * Since precompose_string_if_needed() needs to look at
1568 * the core.precomposeunicode configuration, this
1569 * has to happen after the above block that finds
1570 * out where the repository is, i.e. a preparation
1571 * for calling git_config_get_bool().
1574 prefix
= precompose_string_if_needed(prefix
);
1575 startup_info
->prefix
= prefix
;
1576 setenv(GIT_PREFIX_ENVIRONMENT
, prefix
, 1);
1578 startup_info
->prefix
= NULL
;
1579 setenv(GIT_PREFIX_ENVIRONMENT
, "", 1);
1582 setup_original_cwd();
1584 strbuf_release(&dir
);
1585 strbuf_release(&gitdir
);
1586 strbuf_release(&report
);
1587 clear_repository_format(&repo_fmt
);
1592 int git_config_perm(const char *var
, const char *value
)
1600 if (!strcmp(value
, "umask"))
1602 if (!strcmp(value
, "group"))
1604 if (!strcmp(value
, "all") ||
1605 !strcmp(value
, "world") ||
1606 !strcmp(value
, "everybody"))
1607 return PERM_EVERYBODY
;
1609 /* Parse octal numbers */
1610 i
= strtol(value
, &endptr
, 8);
1612 /* If not an octal number, maybe true/false? */
1614 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
1617 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
1618 * a chmod value to restrict to.
1621 case PERM_UMASK
: /* 0 */
1623 case OLD_PERM_GROUP
: /* 1 */
1625 case OLD_PERM_EVERYBODY
: /* 2 */
1626 return PERM_EVERYBODY
;
1629 /* A filemode value was given: 0xxx */
1631 if ((i
& 0600) != 0600)
1632 die(_("problem with core.sharedRepository filemode value "
1633 "(0%.3o).\nThe owner of files must always have "
1634 "read and write permissions."), i
);
1637 * Mask filemode value. Others can not get write permission.
1638 * x flags for directories are handled separately.
1643 void check_repository_format(struct repository_format
*fmt
)
1645 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
1648 check_repository_format_gently(get_git_dir(), fmt
, NULL
);
1649 startup_info
->have_repository
= 1;
1650 repo_set_hash_algo(the_repository
, fmt
->hash_algo
);
1651 the_repository
->repository_format_partial_clone
=
1652 xstrdup_or_null(fmt
->partial_clone
);
1653 clear_repository_format(&repo_fmt
);
1657 * Returns the "prefix", a path to the current working directory
1658 * relative to the work tree root, or NULL, if the current working
1659 * directory is not a strict subdirectory of the work tree root. The
1660 * prefix always ends with a '/' character.
1662 const char *setup_git_directory(void)
1664 return setup_git_directory_gently(NULL
);
1667 const char *resolve_gitdir_gently(const char *suspect
, int *return_error_code
)
1669 if (is_git_directory(suspect
))
1671 return read_gitfile_gently(suspect
, return_error_code
);
1674 /* if any standard file descriptor is missing open it to /dev/null */
1675 void sanitize_stdfds(void)
1677 int fd
= xopen("/dev/null", O_RDWR
);
1686 #ifdef NO_POSIX_GOODIES
1694 die_errno(_("fork failed"));
1699 die_errno(_("setsid failed"));