3 #include "environment.h"
5 #include "repository.h"
8 #include "string-list.h"
9 #include "chdir-notify.h"
10 #include "promisor-remote.h"
13 static int inside_git_dir
= -1;
14 static int inside_work_tree
= -1;
15 static int work_tree_config_is_bogus
;
16 enum allowed_bare_repo
{
17 ALLOWED_BARE_REPO_EXPLICIT
= 0,
18 ALLOWED_BARE_REPO_ALL
,
21 static struct startup_info the_startup_info
;
22 struct startup_info
*startup_info
= &the_startup_info
;
23 const char *tmp_original_cwd
;
26 * The input parameter must contain an absolute path, and it must already be
29 * Find the part of an absolute path that lies inside the work tree by
30 * dereferencing symlinks outside the work tree, for example:
31 * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
32 * /dir/file (work tree is /) -> dir/file
33 * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
34 * /dir/repolink/file (repolink points to /dir/repo) -> file
35 * /dir/repo (exactly equal to work tree) -> (empty string)
37 static int abspath_part_inside_repo(char *path
)
43 const char *work_tree
= get_git_work_tree();
44 struct strbuf realpath
= STRBUF_INIT
;
48 wtlen
= strlen(work_tree
);
50 off
= offset_1st_component(path
);
52 /* check if work tree is already the prefix */
53 if (wtlen
<= len
&& !fspathncmp(path
, work_tree
, wtlen
)) {
54 if (path
[wtlen
] == '/') {
55 memmove(path
, path
+ wtlen
+ 1, len
- wtlen
);
57 } else if (path
[wtlen
- 1] == '/' || path
[wtlen
] == '\0') {
58 /* work tree is the root, or the whole path */
59 memmove(path
, path
+ wtlen
, len
- wtlen
+ 1);
62 /* work tree might match beginning of a symlink to work tree */
68 /* check each '/'-terminated level */
73 strbuf_realpath(&realpath
, path0
, 1);
74 if (fspathcmp(realpath
.buf
, work_tree
) == 0) {
75 memmove(path0
, path
+ 1, len
- (path
- path0
));
76 strbuf_release(&realpath
);
83 /* check whole path */
84 strbuf_realpath(&realpath
, path0
, 1);
85 if (fspathcmp(realpath
.buf
, work_tree
) == 0) {
87 strbuf_release(&realpath
);
91 strbuf_release(&realpath
);
96 * Normalize "path", prepending the "prefix" for relative paths. If
97 * remaining_prefix is not NULL, return the actual prefix still
98 * remains in the path. For example, prefix = sub1/sub2/ and path is
100 * foo -> sub1/sub2/foo (full prefix)
101 * ../foo -> sub1/foo (remaining prefix is sub1/)
102 * ../../bar -> bar (no remaining prefix)
103 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
104 * `pwd`/../bar -> sub1/bar (no remaining prefix)
106 char *prefix_path_gently(const char *prefix
, int len
,
107 int *remaining_prefix
, const char *path
)
109 const char *orig
= path
;
111 if (is_absolute_path(orig
)) {
112 sanitized
= xmallocz(strlen(path
));
113 if (remaining_prefix
)
114 *remaining_prefix
= 0;
115 if (normalize_path_copy_len(sanitized
, path
, remaining_prefix
)) {
119 if (abspath_part_inside_repo(sanitized
)) {
124 sanitized
= xstrfmt("%.*s%s", len
, len
? prefix
: "", path
);
125 if (remaining_prefix
)
126 *remaining_prefix
= len
;
127 if (normalize_path_copy_len(sanitized
, sanitized
, remaining_prefix
)) {
135 char *prefix_path(const char *prefix
, int len
, const char *path
)
137 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
139 const char *hint_path
= get_git_work_tree();
141 hint_path
= get_git_dir();
142 die(_("'%s' is outside repository at '%s'"), path
,
143 absolute_path(hint_path
));
148 int path_inside_repo(const char *prefix
, const char *path
)
150 int len
= prefix
? strlen(prefix
) : 0;
151 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
159 int check_filename(const char *prefix
, const char *arg
)
161 char *to_free
= NULL
;
164 if (skip_prefix(arg
, ":/", &arg
)) {
165 if (!*arg
) /* ":/" is root dir, always exists */
168 } else if (skip_prefix(arg
, ":!", &arg
) ||
169 skip_prefix(arg
, ":^", &arg
)) {
170 if (!*arg
) /* excluding everything is silly, but allowed */
175 arg
= to_free
= prefix_filename(prefix
, arg
);
177 if (!lstat(arg
, &st
)) {
179 return 1; /* file exists */
181 if (is_missing_file_error(errno
)) {
183 return 0; /* file does not exist */
185 die_errno(_("failed to stat '%s'"), arg
);
188 static void NORETURN
die_verify_filename(struct repository
*r
,
191 int diagnose_misspelt_rev
)
193 if (!diagnose_misspelt_rev
)
194 die(_("%s: no such path in the working tree.\n"
195 "Use 'git <command> -- <path>...' to specify paths that do not exist locally."),
198 * Saying "'(icase)foo' does not exist in the index" when the
199 * user gave us ":(icase)foo" is just stupid. A magic pathspec
200 * begins with a colon and is followed by a non-alnum; do not
201 * let maybe_die_on_misspelt_object_name() even trigger.
203 if (!(arg
[0] == ':' && !isalnum(arg
[1])))
204 maybe_die_on_misspelt_object_name(r
, arg
, prefix
);
206 /* ... or fall back the most general message. */
207 die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
208 "Use '--' to separate paths from revisions, like this:\n"
209 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
214 * Check for arguments that don't resolve as actual files,
215 * but which look sufficiently like pathspecs that we'll consider
216 * them such for the purposes of rev/pathspec DWIM parsing.
218 static int looks_like_pathspec(const char *arg
)
224 * Wildcard characters imply the user is looking to match pathspecs
225 * that aren't in the filesystem. Note that this doesn't include
226 * backslash even though it's a glob special; by itself it doesn't
227 * cause any increase in the match. Likewise ignore backslash-escaped
228 * wildcard characters.
230 for (p
= arg
; *p
; p
++) {
233 } else if (is_glob_special(*p
)) {
241 /* long-form pathspec magic */
242 if (starts_with(arg
, ":("))
249 * Verify a filename that we got as an argument for a pathspec
250 * entry. Note that a filename that begins with "-" never verifies
251 * as true, because even if such a filename were to exist, we want
252 * it to be preceded by the "--" marker (or we want the user to
253 * use a format like "./-filename")
255 * The "diagnose_misspelt_rev" is used to provide a user-friendly
256 * diagnosis when dying upon finding that "name" is not a pathname.
257 * If set to 1, the diagnosis will try to diagnose "name" as an
258 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
259 * will only complain about an inexisting file.
261 * This function is typically called to check that a "file or rev"
262 * argument is unambiguous. In this case, the caller will want
263 * diagnose_misspelt_rev == 1 when verifying the first non-rev
264 * argument (which could have been a revision), and
265 * diagnose_misspelt_rev == 0 for the next ones (because we already
266 * saw a filename, there's not ambiguity anymore).
268 void verify_filename(const char *prefix
,
270 int diagnose_misspelt_rev
)
273 die(_("option '%s' must come before non-option arguments"), arg
);
274 if (looks_like_pathspec(arg
) || check_filename(prefix
, arg
))
276 die_verify_filename(the_repository
, prefix
, arg
, diagnose_misspelt_rev
);
280 * Opposite of the above: the command line did not have -- marker
281 * and we parsed the arg as a refname. It should not be interpretable
284 void verify_non_filename(const char *prefix
, const char *arg
)
286 if (!is_inside_work_tree() || is_inside_git_dir())
290 if (!check_filename(prefix
, arg
))
292 die(_("ambiguous argument '%s': both revision and filename\n"
293 "Use '--' to separate paths from revisions, like this:\n"
294 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
297 int get_common_dir(struct strbuf
*sb
, const char *gitdir
)
299 const char *git_env_common_dir
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
300 if (git_env_common_dir
) {
301 strbuf_addstr(sb
, git_env_common_dir
);
304 return get_common_dir_noenv(sb
, gitdir
);
308 int get_common_dir_noenv(struct strbuf
*sb
, const char *gitdir
)
310 struct strbuf data
= STRBUF_INIT
;
311 struct strbuf path
= STRBUF_INIT
;
314 strbuf_addf(&path
, "%s/commondir", gitdir
);
315 if (file_exists(path
.buf
)) {
316 if (strbuf_read_file(&data
, path
.buf
, 0) <= 0)
317 die_errno(_("failed to read %s"), path
.buf
);
318 while (data
.len
&& (data
.buf
[data
.len
- 1] == '\n' ||
319 data
.buf
[data
.len
- 1] == '\r'))
321 data
.buf
[data
.len
] = '\0';
323 if (!is_absolute_path(data
.buf
))
324 strbuf_addf(&path
, "%s/", gitdir
);
325 strbuf_addbuf(&path
, &data
);
326 strbuf_add_real_path(sb
, path
.buf
);
329 strbuf_addstr(sb
, gitdir
);
332 strbuf_release(&data
);
333 strbuf_release(&path
);
338 * Test if it looks like we're at a git directory.
341 * - either an objects/ directory _or_ the proper
342 * GIT_OBJECT_DIRECTORY environment variable
343 * - a refs/ directory
344 * - either a HEAD symlink or a HEAD file that is formatted as
345 * a proper "ref:", or a regular file HEAD that has a properly
346 * formatted sha1 object name.
348 int is_git_directory(const char *suspect
)
350 struct strbuf path
= STRBUF_INIT
;
354 /* Check worktree-related signatures */
355 strbuf_addstr(&path
, suspect
);
356 strbuf_complete(&path
, '/');
357 strbuf_addstr(&path
, "HEAD");
358 if (validate_headref(path
.buf
))
362 get_common_dir(&path
, suspect
);
365 /* Check non-worktree-related signatures */
366 if (getenv(DB_ENVIRONMENT
)) {
367 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
371 strbuf_setlen(&path
, len
);
372 strbuf_addstr(&path
, "/objects");
373 if (access(path
.buf
, X_OK
))
377 strbuf_setlen(&path
, len
);
378 strbuf_addstr(&path
, "/refs");
379 if (access(path
.buf
, X_OK
))
384 strbuf_release(&path
);
388 int is_nonbare_repository_dir(struct strbuf
*path
)
392 size_t orig_path_len
= path
->len
;
393 assert(orig_path_len
!= 0);
394 strbuf_complete(path
, '/');
395 strbuf_addstr(path
, ".git");
396 if (read_gitfile_gently(path
->buf
, &gitfile_error
) || is_git_directory(path
->buf
))
398 if (gitfile_error
== READ_GITFILE_ERR_OPEN_FAILED
||
399 gitfile_error
== READ_GITFILE_ERR_READ_FAILED
)
401 strbuf_setlen(path
, orig_path_len
);
405 int is_inside_git_dir(void)
407 if (inside_git_dir
< 0)
408 inside_git_dir
= is_inside_dir(get_git_dir());
409 return inside_git_dir
;
412 int is_inside_work_tree(void)
414 if (inside_work_tree
< 0)
415 inside_work_tree
= is_inside_dir(get_git_work_tree());
416 return inside_work_tree
;
419 void setup_work_tree(void)
421 const char *work_tree
;
422 static int initialized
= 0;
427 if (work_tree_config_is_bogus
)
428 die(_("unable to set up work tree using invalid config"));
430 work_tree
= get_git_work_tree();
431 if (!work_tree
|| chdir_notify(work_tree
))
432 die(_("this operation must be run in a work tree"));
435 * Make sure subsequent git processes find correct worktree
436 * if $GIT_WORK_TREE is set relative
438 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
439 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
444 static void setup_original_cwd(void)
446 struct strbuf tmp
= STRBUF_INIT
;
447 const char *worktree
= NULL
;
450 if (!tmp_original_cwd
)
454 * startup_info->original_cwd points to the current working
455 * directory we inherited from our parent process, which is a
456 * directory we want to avoid removing.
458 * For convience, we would like to have the path relative to the
459 * worktree instead of an absolute path.
461 * Yes, startup_info->original_cwd is usually the same as 'prefix',
462 * but differs in two ways:
463 * - prefix has a trailing '/'
464 * - if the user passes '-C' to git, that modifies the prefix but
465 * not startup_info->original_cwd.
468 /* Normalize the directory */
469 if (!strbuf_realpath(&tmp
, tmp_original_cwd
, 0)) {
470 trace2_data_string("setup", the_repository
,
471 "realpath-path", tmp_original_cwd
);
472 trace2_data_string("setup", the_repository
,
473 "realpath-failure", strerror(errno
));
474 free((char*)tmp_original_cwd
);
475 tmp_original_cwd
= NULL
;
479 free((char*)tmp_original_cwd
);
480 tmp_original_cwd
= NULL
;
481 startup_info
->original_cwd
= strbuf_detach(&tmp
, NULL
);
484 * Get our worktree; we only protect the current working directory
485 * if it's in the worktree.
487 worktree
= get_git_work_tree();
489 goto no_prevention_needed
;
491 offset
= dir_inside_of(startup_info
->original_cwd
, worktree
);
494 * If startup_info->original_cwd == worktree, that is already
495 * protected and we don't need original_cwd as a secondary
496 * protection measure.
498 if (!*(startup_info
->original_cwd
+ offset
))
499 goto no_prevention_needed
;
502 * original_cwd was inside worktree; precompose it just as
503 * we do prefix so that built up paths will match
505 startup_info
->original_cwd
= \
506 precompose_string_if_needed(startup_info
->original_cwd
511 no_prevention_needed
:
512 free((char*)startup_info
->original_cwd
);
513 startup_info
->original_cwd
= NULL
;
516 static int read_worktree_config(const char *var
, const char *value
, void *vdata
)
518 struct repository_format
*data
= vdata
;
520 if (strcmp(var
, "core.bare") == 0) {
521 data
->is_bare
= git_config_bool(var
, value
);
522 } else if (strcmp(var
, "core.worktree") == 0) {
524 return config_error_nonbool(var
);
525 free(data
->work_tree
);
526 data
->work_tree
= xstrdup(value
);
531 enum extension_result
{
532 EXTENSION_ERROR
= -1, /* compatible with error(), etc */
533 EXTENSION_UNKNOWN
= 0,
538 * Do not add new extensions to this function. It handles extensions which are
539 * respected even in v0-format repositories for historical compatibility.
541 static enum extension_result
handle_extension_v0(const char *var
,
544 struct repository_format
*data
)
546 if (!strcmp(ext
, "noop")) {
548 } else if (!strcmp(ext
, "preciousobjects")) {
549 data
->precious_objects
= git_config_bool(var
, value
);
551 } else if (!strcmp(ext
, "partialclone")) {
552 data
->partial_clone
= xstrdup(value
);
554 } else if (!strcmp(ext
, "worktreeconfig")) {
555 data
->worktree_config
= git_config_bool(var
, value
);
559 return EXTENSION_UNKNOWN
;
563 * Record any new extensions in this function.
565 static enum extension_result
handle_extension(const char *var
,
568 struct repository_format
*data
)
570 if (!strcmp(ext
, "noop-v1")) {
572 } else if (!strcmp(ext
, "objectformat")) {
576 return config_error_nonbool(var
);
577 format
= hash_algo_by_name(value
);
578 if (format
== GIT_HASH_UNKNOWN
)
579 return error(_("invalid value for '%s': '%s'"),
580 "extensions.objectformat", value
);
581 data
->hash_algo
= format
;
584 return EXTENSION_UNKNOWN
;
587 static int check_repo_format(const char *var
, const char *value
, void *vdata
)
589 struct repository_format
*data
= vdata
;
592 if (strcmp(var
, "core.repositoryformatversion") == 0)
593 data
->version
= git_config_int(var
, value
);
594 else if (skip_prefix(var
, "extensions.", &ext
)) {
595 switch (handle_extension_v0(var
, value
, ext
, data
)) {
596 case EXTENSION_ERROR
:
600 case EXTENSION_UNKNOWN
:
604 switch (handle_extension(var
, value
, ext
, data
)) {
605 case EXTENSION_ERROR
:
608 string_list_append(&data
->v1_only_extensions
, ext
);
610 case EXTENSION_UNKNOWN
:
611 string_list_append(&data
->unknown_extensions
, ext
);
616 return read_worktree_config(var
, value
, vdata
);
619 static int check_repository_format_gently(const char *gitdir
, struct repository_format
*candidate
, int *nongit_ok
)
621 struct strbuf sb
= STRBUF_INIT
;
622 struct strbuf err
= STRBUF_INIT
;
625 has_common
= get_common_dir(&sb
, gitdir
);
626 strbuf_addstr(&sb
, "/config");
627 read_repository_format(candidate
, sb
.buf
);
631 * For historical use of check_repository_format() in git-init,
632 * we treat a missing config as a silent "ok", even when nongit_ok
635 if (candidate
->version
< 0)
638 if (verify_repository_format(candidate
, &err
) < 0) {
640 warning("%s", err
.buf
);
641 strbuf_release(&err
);
648 repository_format_precious_objects
= candidate
->precious_objects
;
649 repository_format_worktree_config
= candidate
->worktree_config
;
650 string_list_clear(&candidate
->unknown_extensions
, 0);
651 string_list_clear(&candidate
->v1_only_extensions
, 0);
653 if (repository_format_worktree_config
) {
655 * pick up core.bare and core.worktree from per-worktree
658 strbuf_addf(&sb
, "%s/config.worktree", gitdir
);
659 git_config_from_file(read_worktree_config
, sb
.buf
, candidate
);
665 if (candidate
->is_bare
!= -1) {
666 is_bare_repository_cfg
= candidate
->is_bare
;
667 if (is_bare_repository_cfg
== 1)
668 inside_work_tree
= -1;
670 if (candidate
->work_tree
) {
671 free(git_work_tree_cfg
);
672 git_work_tree_cfg
= xstrdup(candidate
->work_tree
);
673 inside_work_tree
= -1;
680 int upgrade_repository_format(int target_version
)
682 struct strbuf sb
= STRBUF_INIT
;
683 struct strbuf err
= STRBUF_INIT
;
684 struct strbuf repo_version
= STRBUF_INIT
;
685 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
687 strbuf_git_common_path(&sb
, the_repository
, "config");
688 read_repository_format(&repo_fmt
, sb
.buf
);
691 if (repo_fmt
.version
>= target_version
)
694 if (verify_repository_format(&repo_fmt
, &err
) < 0) {
695 error("cannot upgrade repository format from %d to %d: %s",
696 repo_fmt
.version
, target_version
, err
.buf
);
697 strbuf_release(&err
);
700 if (!repo_fmt
.version
&& repo_fmt
.unknown_extensions
.nr
)
701 return error("cannot upgrade repository format: "
702 "unknown extension %s",
703 repo_fmt
.unknown_extensions
.items
[0].string
);
705 strbuf_addf(&repo_version
, "%d", target_version
);
706 git_config_set("core.repositoryformatversion", repo_version
.buf
);
707 strbuf_release(&repo_version
);
711 static void init_repository_format(struct repository_format
*format
)
713 const struct repository_format fresh
= REPOSITORY_FORMAT_INIT
;
715 memcpy(format
, &fresh
, sizeof(fresh
));
718 int read_repository_format(struct repository_format
*format
, const char *path
)
720 clear_repository_format(format
);
721 git_config_from_file(check_repo_format
, path
, format
);
722 if (format
->version
== -1)
723 clear_repository_format(format
);
724 return format
->version
;
727 void clear_repository_format(struct repository_format
*format
)
729 string_list_clear(&format
->unknown_extensions
, 0);
730 string_list_clear(&format
->v1_only_extensions
, 0);
731 free(format
->work_tree
);
732 free(format
->partial_clone
);
733 init_repository_format(format
);
736 int verify_repository_format(const struct repository_format
*format
,
739 if (GIT_REPO_VERSION_READ
< format
->version
) {
740 strbuf_addf(err
, _("Expected git repo version <= %d, found %d"),
741 GIT_REPO_VERSION_READ
, format
->version
);
745 if (format
->version
>= 1 && format
->unknown_extensions
.nr
) {
748 strbuf_addstr(err
, Q_("unknown repository extension found:",
749 "unknown repository extensions found:",
750 format
->unknown_extensions
.nr
));
752 for (i
= 0; i
< format
->unknown_extensions
.nr
; i
++)
753 strbuf_addf(err
, "\n\t%s",
754 format
->unknown_extensions
.items
[i
].string
);
758 if (format
->version
== 0 && format
->v1_only_extensions
.nr
) {
762 Q_("repo version is 0, but v1-only extension found:",
763 "repo version is 0, but v1-only extensions found:",
764 format
->v1_only_extensions
.nr
));
766 for (i
= 0; i
< format
->v1_only_extensions
.nr
; i
++)
767 strbuf_addf(err
, "\n\t%s",
768 format
->v1_only_extensions
.items
[i
].string
);
775 void read_gitfile_error_die(int error_code
, const char *path
, const char *dir
)
777 switch (error_code
) {
778 case READ_GITFILE_ERR_STAT_FAILED
:
779 case READ_GITFILE_ERR_NOT_A_FILE
:
780 /* non-fatal; follow return path */
782 case READ_GITFILE_ERR_OPEN_FAILED
:
783 die_errno(_("error opening '%s'"), path
);
784 case READ_GITFILE_ERR_TOO_LARGE
:
785 die(_("too large to be a .git file: '%s'"), path
);
786 case READ_GITFILE_ERR_READ_FAILED
:
787 die(_("error reading %s"), path
);
788 case READ_GITFILE_ERR_INVALID_FORMAT
:
789 die(_("invalid gitfile format: %s"), path
);
790 case READ_GITFILE_ERR_NO_PATH
:
791 die(_("no path in gitfile: %s"), path
);
792 case READ_GITFILE_ERR_NOT_A_REPO
:
793 die(_("not a git repository: %s"), dir
);
795 BUG("unknown error code");
800 * Try to read the location of the git directory from the .git file,
801 * return path to git directory if found. The return value comes from
804 * On failure, if return_error_code is not NULL, return_error_code
805 * will be set to an error code and NULL will be returned. If
806 * return_error_code is NULL the function will die instead (for most
809 const char *read_gitfile_gently(const char *path
, int *return_error_code
)
811 const int max_file_size
= 1 << 20; /* 1MB */
819 static struct strbuf realpath
= STRBUF_INIT
;
821 if (stat(path
, &st
)) {
822 /* NEEDSWORK: discern between ENOENT vs other errors */
823 error_code
= READ_GITFILE_ERR_STAT_FAILED
;
826 if (!S_ISREG(st
.st_mode
)) {
827 error_code
= READ_GITFILE_ERR_NOT_A_FILE
;
830 if (st
.st_size
> max_file_size
) {
831 error_code
= READ_GITFILE_ERR_TOO_LARGE
;
834 fd
= open(path
, O_RDONLY
);
836 error_code
= READ_GITFILE_ERR_OPEN_FAILED
;
839 buf
= xmallocz(st
.st_size
);
840 len
= read_in_full(fd
, buf
, st
.st_size
);
842 if (len
!= st
.st_size
) {
843 error_code
= READ_GITFILE_ERR_READ_FAILED
;
846 if (!starts_with(buf
, "gitdir: ")) {
847 error_code
= READ_GITFILE_ERR_INVALID_FORMAT
;
850 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
853 error_code
= READ_GITFILE_ERR_NO_PATH
;
859 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
860 size_t pathlen
= slash
+1 - path
;
861 dir
= xstrfmt("%.*s%.*s", (int)pathlen
, path
,
862 (int)(len
- 8), buf
+ 8);
866 if (!is_git_directory(dir
)) {
867 error_code
= READ_GITFILE_ERR_NOT_A_REPO
;
871 strbuf_realpath(&realpath
, dir
, 1);
875 if (return_error_code
)
876 *return_error_code
= error_code
;
878 read_gitfile_error_die(error_code
, path
, dir
);
881 return error_code
? NULL
: path
;
884 static const char *setup_explicit_git_dir(const char *gitdirenv
,
886 struct repository_format
*repo_fmt
,
889 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
890 const char *worktree
;
894 if (PATH_MAX
- 40 < strlen(gitdirenv
))
895 die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT
);
897 gitfile
= (char*)read_gitfile(gitdirenv
);
899 gitfile
= xstrdup(gitfile
);
903 if (!is_git_directory(gitdirenv
)) {
909 die(_("not a git repository: '%s'"), gitdirenv
);
912 if (check_repository_format_gently(gitdirenv
, repo_fmt
, nongit_ok
)) {
917 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
919 set_git_work_tree(work_tree_env
);
920 else if (is_bare_repository_cfg
> 0) {
921 if (git_work_tree_cfg
) {
923 warning("core.bare and core.worktree do not make sense");
924 work_tree_config_is_bogus
= 1;
928 set_git_dir(gitdirenv
, 0);
932 else if (git_work_tree_cfg
) { /* #6, #14 */
933 if (is_absolute_path(git_work_tree_cfg
))
934 set_git_work_tree(git_work_tree_cfg
);
937 if (chdir(gitdirenv
))
938 die_errno(_("cannot chdir to '%s'"), gitdirenv
);
939 if (chdir(git_work_tree_cfg
))
940 die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg
);
941 core_worktree
= xgetcwd();
943 die_errno(_("cannot come back to cwd"));
944 set_git_work_tree(core_worktree
);
948 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, 1)) {
950 set_git_dir(gitdirenv
, 0);
955 set_git_work_tree(".");
957 /* set_git_work_tree() must have been called by now */
958 worktree
= get_git_work_tree();
960 /* both get_git_work_tree() and cwd are already normalized */
961 if (!strcmp(cwd
->buf
, worktree
)) { /* cwd == worktree */
962 set_git_dir(gitdirenv
, 0);
967 offset
= dir_inside_of(cwd
->buf
, worktree
);
968 if (offset
>= 0) { /* cwd inside worktree? */
969 set_git_dir(gitdirenv
, 1);
971 die_errno(_("cannot chdir to '%s'"), worktree
);
972 strbuf_addch(cwd
, '/');
974 return cwd
->buf
+ offset
;
977 /* cwd outside worktree */
978 set_git_dir(gitdirenv
, 0);
983 static const char *setup_discovered_git_dir(const char *gitdir
,
984 struct strbuf
*cwd
, int offset
,
985 struct repository_format
*repo_fmt
,
988 if (check_repository_format_gently(gitdir
, repo_fmt
, nongit_ok
))
991 /* --work-tree is set without --git-dir; use discovered one */
992 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
993 char *to_free
= NULL
;
996 if (offset
!= cwd
->len
&& !is_absolute_path(gitdir
))
997 gitdir
= to_free
= real_pathdup(gitdir
, 1);
999 die_errno(_("cannot come back to cwd"));
1000 ret
= setup_explicit_git_dir(gitdir
, cwd
, repo_fmt
, nongit_ok
);
1005 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
1006 if (is_bare_repository_cfg
> 0) {
1007 set_git_dir(gitdir
, (offset
!= cwd
->len
));
1008 if (chdir(cwd
->buf
))
1009 die_errno(_("cannot come back to cwd"));
1013 /* #0, #1, #5, #8, #9, #12, #13 */
1014 set_git_work_tree(".");
1015 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
1016 set_git_dir(gitdir
, 0);
1018 inside_work_tree
= 1;
1019 if (offset
>= cwd
->len
)
1022 /* Make "offset" point past the '/' (already the case for root dirs) */
1023 if (offset
!= offset_1st_component(cwd
->buf
))
1025 /* Add a '/' at the end */
1026 strbuf_addch(cwd
, '/');
1027 return cwd
->buf
+ offset
;
1030 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
1031 static const char *setup_bare_git_dir(struct strbuf
*cwd
, int offset
,
1032 struct repository_format
*repo_fmt
,
1037 if (check_repository_format_gently(".", repo_fmt
, nongit_ok
))
1040 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
1042 /* --work-tree is set without --git-dir; use discovered one */
1043 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
1044 static const char *gitdir
;
1046 gitdir
= offset
== cwd
->len
? "." : xmemdupz(cwd
->buf
, offset
);
1047 if (chdir(cwd
->buf
))
1048 die_errno(_("cannot come back to cwd"));
1049 return setup_explicit_git_dir(gitdir
, cwd
, repo_fmt
, nongit_ok
);
1053 inside_work_tree
= 0;
1054 if (offset
!= cwd
->len
) {
1055 if (chdir(cwd
->buf
))
1056 die_errno(_("cannot come back to cwd"));
1057 root_len
= offset_1st_component(cwd
->buf
);
1058 strbuf_setlen(cwd
, offset
> root_len
? offset
: root_len
);
1059 set_git_dir(cwd
->buf
, 0);
1062 set_git_dir(".", 0);
1066 static dev_t
get_device_or_die(const char *path
, const char *prefix
, int prefix_len
)
1069 if (stat(path
, &buf
)) {
1070 die_errno(_("failed to stat '%*s%s%s'"),
1072 prefix
? prefix
: "",
1073 prefix
? "/" : "", path
);
1079 * A "string_list_each_func_t" function that canonicalizes an entry
1080 * from GIT_CEILING_DIRECTORIES using real_pathdup(), or
1081 * discards it if unusable. The presence of an empty entry in
1082 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
1083 * subsequent entries.
1085 static int canonicalize_ceiling_entry(struct string_list_item
*item
,
1088 int *empty_entry_found
= cb_data
;
1089 char *ceil
= item
->string
;
1092 *empty_entry_found
= 1;
1094 } else if (!is_absolute_path(ceil
)) {
1096 } else if (*empty_entry_found
) {
1097 /* Keep entry but do not canonicalize it */
1100 char *real_path
= real_pathdup(ceil
, 0);
1105 item
->string
= real_path
;
1110 struct safe_directory_data
{
1115 static int safe_directory_cb(const char *key
, const char *value
, void *d
)
1117 struct safe_directory_data
*data
= d
;
1119 if (strcmp(key
, "safe.directory"))
1122 if (!value
|| !*value
) {
1124 } else if (!strcmp(value
, "*")) {
1127 const char *interpolated
= NULL
;
1129 if (!git_config_pathname(&interpolated
, key
, value
) &&
1130 !fspathcmp(data
->path
, interpolated
? interpolated
: value
))
1133 free((char *)interpolated
);
1140 * Check if a repository is safe, by verifying the ownership of the
1141 * worktree (if any), the git directory, and the gitfile (if any).
1143 * Exemptions for known-safe repositories can be added via `safe.directory`
1144 * config settings; for non-bare repositories, their worktree needs to be
1145 * added, for bare ones their git directory.
1147 static int ensure_valid_ownership(const char *gitfile
,
1148 const char *worktree
, const char *gitdir
,
1149 struct strbuf
*report
)
1151 struct safe_directory_data data
= {
1152 .path
= worktree
? worktree
: gitdir
1155 if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
1156 (!gitfile
|| is_path_owned_by_current_user(gitfile
, report
)) &&
1157 (!worktree
|| is_path_owned_by_current_user(worktree
, report
)) &&
1158 (!gitdir
|| is_path_owned_by_current_user(gitdir
, report
)))
1162 * data.path is the "path" that identifies the repository and it is
1163 * constant regardless of what failed above. data.is_safe should be
1164 * initialized to false, and might be changed by the callback.
1166 git_protected_config(safe_directory_cb
, &data
);
1168 return data
.is_safe
;
1171 static int allowed_bare_repo_cb(const char *key
, const char *value
, void *d
)
1173 enum allowed_bare_repo
*allowed_bare_repo
= d
;
1175 if (strcasecmp(key
, "safe.bareRepository"))
1178 if (!strcmp(value
, "explicit")) {
1179 *allowed_bare_repo
= ALLOWED_BARE_REPO_EXPLICIT
;
1182 if (!strcmp(value
, "all")) {
1183 *allowed_bare_repo
= ALLOWED_BARE_REPO_ALL
;
1189 static enum allowed_bare_repo
get_allowed_bare_repo(void)
1191 enum allowed_bare_repo result
= ALLOWED_BARE_REPO_ALL
;
1192 git_protected_config(allowed_bare_repo_cb
, &result
);
1196 static const char *allowed_bare_repo_to_string(
1197 enum allowed_bare_repo allowed_bare_repo
)
1199 switch (allowed_bare_repo
) {
1200 case ALLOWED_BARE_REPO_EXPLICIT
:
1202 case ALLOWED_BARE_REPO_ALL
:
1205 BUG("invalid allowed_bare_repo %d",
1211 enum discovery_result
{
1216 /* these are errors */
1217 GIT_DIR_HIT_CEILING
= -1,
1218 GIT_DIR_HIT_MOUNT_POINT
= -2,
1219 GIT_DIR_INVALID_GITFILE
= -3,
1220 GIT_DIR_INVALID_OWNERSHIP
= -4,
1221 GIT_DIR_DISALLOWED_BARE
= -5,
1225 * We cannot decide in this function whether we are in the work tree or
1226 * not, since the config can only be read _after_ this function was called.
1228 * Also, we avoid changing any global state (such as the current working
1229 * directory) to allow early callers.
1231 * The directory where the search should start needs to be passed in via the
1232 * `dir` parameter; upon return, the `dir` buffer will contain the path of
1233 * the directory where the search ended, and `gitdir` will contain the path of
1234 * the discovered .git/ directory, if any. If `gitdir` is not absolute, it
1235 * is relative to `dir` (i.e. *not* necessarily the cwd).
1237 static enum discovery_result
setup_git_directory_gently_1(struct strbuf
*dir
,
1238 struct strbuf
*gitdir
,
1239 struct strbuf
*report
,
1242 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
1243 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
1244 const char *gitdirenv
;
1245 int ceil_offset
= -1, min_offset
= offset_1st_component(dir
->buf
);
1246 dev_t current_device
= 0;
1247 int one_filesystem
= 1;
1250 * If GIT_DIR is set explicitly, we're not going
1251 * to do any discovery, but we still do repository
1254 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
1256 strbuf_addstr(gitdir
, gitdirenv
);
1257 return GIT_DIR_EXPLICIT
;
1260 if (env_ceiling_dirs
) {
1261 int empty_entry_found
= 0;
1263 string_list_split(&ceiling_dirs
, env_ceiling_dirs
, PATH_SEP
, -1);
1264 filter_string_list(&ceiling_dirs
, 0,
1265 canonicalize_ceiling_entry
, &empty_entry_found
);
1266 ceil_offset
= longest_ancestor_length(dir
->buf
, &ceiling_dirs
);
1267 string_list_clear(&ceiling_dirs
, 0);
1270 if (ceil_offset
< 0)
1271 ceil_offset
= min_offset
- 2;
1273 if (min_offset
&& min_offset
== dir
->len
&&
1274 !is_dir_sep(dir
->buf
[min_offset
- 1])) {
1275 strbuf_addch(dir
, '/');
1280 * Test in the following order (relative to the dir):
1281 * - .git (file containing "gitdir: <path>")
1290 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
1292 current_device
= get_device_or_die(dir
->buf
, NULL
, 0);
1294 int offset
= dir
->len
, error_code
= 0;
1295 char *gitdir_path
= NULL
;
1296 char *gitfile
= NULL
;
1298 if (offset
> min_offset
)
1299 strbuf_addch(dir
, '/');
1300 strbuf_addstr(dir
, DEFAULT_GIT_DIR_ENVIRONMENT
);
1301 gitdirenv
= read_gitfile_gently(dir
->buf
, die_on_error
?
1302 NULL
: &error_code
);
1305 error_code
== READ_GITFILE_ERR_NOT_A_FILE
) {
1306 /* NEEDSWORK: fail if .git is not file nor dir */
1307 if (is_git_directory(dir
->buf
)) {
1308 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
1309 gitdir_path
= xstrdup(dir
->buf
);
1311 } else if (error_code
!= READ_GITFILE_ERR_STAT_FAILED
)
1312 return GIT_DIR_INVALID_GITFILE
;
1314 gitfile
= xstrdup(dir
->buf
);
1316 * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
1317 * to check that directory for a repository.
1318 * Now trim that tentative addition away, because we want to
1319 * focus on the real directory we are in.
1321 strbuf_setlen(dir
, offset
);
1323 enum discovery_result ret
;
1324 const char *gitdir_candidate
=
1325 gitdir_path
? gitdir_path
: gitdirenv
;
1327 if (ensure_valid_ownership(gitfile
, dir
->buf
,
1328 gitdir_candidate
, report
)) {
1329 strbuf_addstr(gitdir
, gitdirenv
);
1330 ret
= GIT_DIR_DISCOVERED
;
1332 ret
= GIT_DIR_INVALID_OWNERSHIP
;
1335 * Earlier, during discovery, we might have allocated
1336 * string copies for gitdir_path or gitfile so make
1337 * sure we don't leak by freeing them now, before
1338 * leaving the loop and function.
1340 * Note: gitdirenv will be non-NULL whenever these are
1341 * allocated, therefore we need not take care of releasing
1342 * them outside of this conditional block.
1350 if (is_git_directory(dir
->buf
)) {
1351 if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT
)
1352 return GIT_DIR_DISALLOWED_BARE
;
1353 if (!ensure_valid_ownership(NULL
, NULL
, dir
->buf
, report
))
1354 return GIT_DIR_INVALID_OWNERSHIP
;
1355 strbuf_addstr(gitdir
, ".");
1356 return GIT_DIR_BARE
;
1359 if (offset
<= min_offset
)
1360 return GIT_DIR_HIT_CEILING
;
1362 while (--offset
> ceil_offset
&& !is_dir_sep(dir
->buf
[offset
]))
1364 if (offset
<= ceil_offset
)
1365 return GIT_DIR_HIT_CEILING
;
1367 strbuf_setlen(dir
, offset
> min_offset
? offset
: min_offset
);
1368 if (one_filesystem
&&
1369 current_device
!= get_device_or_die(dir
->buf
, NULL
, offset
))
1370 return GIT_DIR_HIT_MOUNT_POINT
;
1374 int discover_git_directory(struct strbuf
*commondir
,
1375 struct strbuf
*gitdir
)
1377 struct strbuf dir
= STRBUF_INIT
, err
= STRBUF_INIT
;
1378 size_t gitdir_offset
= gitdir
->len
, cwd_len
;
1379 size_t commondir_offset
= commondir
->len
;
1380 struct repository_format candidate
= REPOSITORY_FORMAT_INIT
;
1382 if (strbuf_getcwd(&dir
))
1386 if (setup_git_directory_gently_1(&dir
, gitdir
, NULL
, 0) <= 0) {
1387 strbuf_release(&dir
);
1392 * The returned gitdir is relative to dir, and if dir does not reflect
1393 * the current working directory, we simply make the gitdir absolute.
1395 if (dir
.len
< cwd_len
&& !is_absolute_path(gitdir
->buf
+ gitdir_offset
)) {
1396 /* Avoid a trailing "/." */
1397 if (!strcmp(".", gitdir
->buf
+ gitdir_offset
))
1398 strbuf_setlen(gitdir
, gitdir_offset
);
1400 strbuf_addch(&dir
, '/');
1401 strbuf_insert(gitdir
, gitdir_offset
, dir
.buf
, dir
.len
);
1404 get_common_dir(commondir
, gitdir
->buf
+ gitdir_offset
);
1407 strbuf_addf(&dir
, "%s/config", commondir
->buf
+ commondir_offset
);
1408 read_repository_format(&candidate
, dir
.buf
);
1409 strbuf_release(&dir
);
1411 if (verify_repository_format(&candidate
, &err
) < 0) {
1412 warning("ignoring git dir '%s': %s",
1413 gitdir
->buf
+ gitdir_offset
, err
.buf
);
1414 strbuf_release(&err
);
1415 strbuf_setlen(commondir
, commondir_offset
);
1416 strbuf_setlen(gitdir
, gitdir_offset
);
1417 clear_repository_format(&candidate
);
1421 /* take ownership of candidate.partial_clone */
1422 the_repository
->repository_format_partial_clone
=
1423 candidate
.partial_clone
;
1424 candidate
.partial_clone
= NULL
;
1426 clear_repository_format(&candidate
);
1430 const char *setup_git_directory_gently(int *nongit_ok
)
1432 static struct strbuf cwd
= STRBUF_INIT
;
1433 struct strbuf dir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
, report
= STRBUF_INIT
;
1434 const char *prefix
= NULL
;
1435 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
1438 * We may have read an incomplete configuration before
1439 * setting-up the git directory. If so, clear the cache so
1440 * that the next queries to the configuration reload complete
1441 * configuration (including the per-repo config file that we
1442 * ignored previously).
1447 * Let's assume that we are in a git repository.
1448 * If it turns out later that we are somewhere else, the value will be
1449 * updated accordingly.
1454 if (strbuf_getcwd(&cwd
))
1455 die_errno(_("Unable to read current working directory"));
1456 strbuf_addbuf(&dir
, &cwd
);
1458 switch (setup_git_directory_gently_1(&dir
, &gitdir
, &report
, 1)) {
1459 case GIT_DIR_EXPLICIT
:
1460 prefix
= setup_explicit_git_dir(gitdir
.buf
, &cwd
, &repo_fmt
, nongit_ok
);
1462 case GIT_DIR_DISCOVERED
:
1463 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1464 die(_("cannot change to '%s'"), dir
.buf
);
1465 prefix
= setup_discovered_git_dir(gitdir
.buf
, &cwd
, dir
.len
,
1466 &repo_fmt
, nongit_ok
);
1469 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1470 die(_("cannot change to '%s'"), dir
.buf
);
1471 prefix
= setup_bare_git_dir(&cwd
, dir
.len
, &repo_fmt
, nongit_ok
);
1473 case GIT_DIR_HIT_CEILING
:
1475 die(_("not a git repository (or any of the parent directories): %s"),
1476 DEFAULT_GIT_DIR_ENVIRONMENT
);
1479 case GIT_DIR_HIT_MOUNT_POINT
:
1481 die(_("not a git repository (or any parent up to mount point %s)\n"
1482 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
1486 case GIT_DIR_INVALID_OWNERSHIP
:
1488 struct strbuf quoted
= STRBUF_INIT
;
1490 strbuf_complete(&report
, '\n');
1491 sq_quote_buf_pretty("ed
, dir
.buf
);
1492 die(_("detected dubious ownership in repository at '%s'\n"
1494 "To add an exception for this directory, call:\n"
1496 "\tgit config --global --add safe.directory %s"),
1497 dir
.buf
, report
.buf
, quoted
.buf
);
1501 case GIT_DIR_DISALLOWED_BARE
:
1503 die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"),
1505 allowed_bare_repo_to_string(get_allowed_bare_repo()));
1511 * As a safeguard against setup_git_directory_gently_1 returning
1512 * this value, fallthrough to BUG. Otherwise it is possible to
1513 * set startup_info->have_repository to 1 when we did nothing to
1514 * find a repository.
1517 BUG("unhandled setup_git_directory_gently_1() result");
1521 * At this point, nongit_ok is stable. If it is non-NULL and points
1522 * to a non-zero value, then this means that we haven't found a
1523 * repository and that the caller expects startup_info to reflect
1526 * Regardless of the state of nongit_ok, startup_info->prefix and
1527 * the GIT_PREFIX environment variable must always match. For details
1528 * see Documentation/config/alias.txt.
1530 if (nongit_ok
&& *nongit_ok
)
1531 startup_info
->have_repository
= 0;
1533 startup_info
->have_repository
= 1;
1536 * Not all paths through the setup code will call 'set_git_dir()' (which
1537 * directly sets up the environment) so in order to guarantee that the
1538 * environment is in a consistent state after setup, explicitly setup
1539 * the environment if we have a repository.
1541 * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
1542 * code paths so we also need to explicitly setup the environment if
1543 * the user has set GIT_DIR. It may be beneficial to disallow bogus
1544 * GIT_DIR values at some point in the future.
1546 if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */
1547 startup_info
->have_repository
||
1548 /* GIT_DIR_EXPLICIT */
1549 getenv(GIT_DIR_ENVIRONMENT
)) {
1550 if (!the_repository
->gitdir
) {
1551 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
1553 gitdir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
1554 setup_git_env(gitdir
);
1556 if (startup_info
->have_repository
) {
1557 repo_set_hash_algo(the_repository
, repo_fmt
.hash_algo
);
1558 /* take ownership of repo_fmt.partial_clone */
1559 the_repository
->repository_format_partial_clone
=
1560 repo_fmt
.partial_clone
;
1561 repo_fmt
.partial_clone
= NULL
;
1565 * Since precompose_string_if_needed() needs to look at
1566 * the core.precomposeunicode configuration, this
1567 * has to happen after the above block that finds
1568 * out where the repository is, i.e. a preparation
1569 * for calling git_config_get_bool().
1572 prefix
= precompose_string_if_needed(prefix
);
1573 startup_info
->prefix
= prefix
;
1574 setenv(GIT_PREFIX_ENVIRONMENT
, prefix
, 1);
1576 startup_info
->prefix
= NULL
;
1577 setenv(GIT_PREFIX_ENVIRONMENT
, "", 1);
1580 setup_original_cwd();
1582 strbuf_release(&dir
);
1583 strbuf_release(&gitdir
);
1584 strbuf_release(&report
);
1585 clear_repository_format(&repo_fmt
);
1590 int git_config_perm(const char *var
, const char *value
)
1598 if (!strcmp(value
, "umask"))
1600 if (!strcmp(value
, "group"))
1602 if (!strcmp(value
, "all") ||
1603 !strcmp(value
, "world") ||
1604 !strcmp(value
, "everybody"))
1605 return PERM_EVERYBODY
;
1607 /* Parse octal numbers */
1608 i
= strtol(value
, &endptr
, 8);
1610 /* If not an octal number, maybe true/false? */
1612 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
1615 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
1616 * a chmod value to restrict to.
1619 case PERM_UMASK
: /* 0 */
1621 case OLD_PERM_GROUP
: /* 1 */
1623 case OLD_PERM_EVERYBODY
: /* 2 */
1624 return PERM_EVERYBODY
;
1627 /* A filemode value was given: 0xxx */
1629 if ((i
& 0600) != 0600)
1630 die(_("problem with core.sharedRepository filemode value "
1631 "(0%.3o).\nThe owner of files must always have "
1632 "read and write permissions."), i
);
1635 * Mask filemode value. Others can not get write permission.
1636 * x flags for directories are handled separately.
1641 void check_repository_format(struct repository_format
*fmt
)
1643 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
1646 check_repository_format_gently(get_git_dir(), fmt
, NULL
);
1647 startup_info
->have_repository
= 1;
1648 repo_set_hash_algo(the_repository
, fmt
->hash_algo
);
1649 the_repository
->repository_format_partial_clone
=
1650 xstrdup_or_null(fmt
->partial_clone
);
1651 clear_repository_format(&repo_fmt
);
1655 * Returns the "prefix", a path to the current working directory
1656 * relative to the work tree root, or NULL, if the current working
1657 * directory is not a strict subdirectory of the work tree root. The
1658 * prefix always ends with a '/' character.
1660 const char *setup_git_directory(void)
1662 return setup_git_directory_gently(NULL
);
1665 const char *resolve_gitdir_gently(const char *suspect
, int *return_error_code
)
1667 if (is_git_directory(suspect
))
1669 return read_gitfile_gently(suspect
, return_error_code
);
1672 /* if any standard file descriptor is missing open it to /dev/null */
1673 void sanitize_stdfds(void)
1675 int fd
= xopen("/dev/null", O_RDWR
);
1684 #ifdef NO_POSIX_GOODIES
1692 die_errno(_("fork failed"));
1697 die_errno(_("setsid failed"));