1 #include "git-compat-util.h"
3 #include "environment.h"
5 #include "object-name.h"
6 #include "repository.h"
10 #include "string-list.h"
11 #include "chdir-notify.h"
12 #include "promisor-remote.h"
18 static int inside_git_dir
= -1;
19 static int inside_work_tree
= -1;
20 static int work_tree_config_is_bogus
;
21 enum allowed_bare_repo
{
22 ALLOWED_BARE_REPO_EXPLICIT
= 0,
23 ALLOWED_BARE_REPO_ALL
,
26 static struct startup_info the_startup_info
;
27 struct startup_info
*startup_info
= &the_startup_info
;
28 const char *tmp_original_cwd
;
31 * The input parameter must contain an absolute path, and it must already be
34 * Find the part of an absolute path that lies inside the work tree by
35 * dereferencing symlinks outside the work tree, for example:
36 * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
37 * /dir/file (work tree is /) -> dir/file
38 * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
39 * /dir/repolink/file (repolink points to /dir/repo) -> file
40 * /dir/repo (exactly equal to work tree) -> (empty string)
42 static int abspath_part_inside_repo(char *path
)
48 const char *work_tree
= get_git_work_tree();
49 struct strbuf realpath
= STRBUF_INIT
;
53 wtlen
= strlen(work_tree
);
55 off
= offset_1st_component(path
);
57 /* check if work tree is already the prefix */
58 if (wtlen
<= len
&& !fspathncmp(path
, work_tree
, wtlen
)) {
59 if (path
[wtlen
] == '/') {
60 memmove(path
, path
+ wtlen
+ 1, len
- wtlen
);
62 } else if (path
[wtlen
- 1] == '/' || path
[wtlen
] == '\0') {
63 /* work tree is the root, or the whole path */
64 memmove(path
, path
+ wtlen
, len
- wtlen
+ 1);
67 /* work tree might match beginning of a symlink to work tree */
73 /* check each '/'-terminated level */
78 strbuf_realpath(&realpath
, path0
, 1);
79 if (fspathcmp(realpath
.buf
, work_tree
) == 0) {
80 memmove(path0
, path
+ 1, len
- (path
- path0
));
81 strbuf_release(&realpath
);
88 /* check whole path */
89 strbuf_realpath(&realpath
, path0
, 1);
90 if (fspathcmp(realpath
.buf
, work_tree
) == 0) {
92 strbuf_release(&realpath
);
96 strbuf_release(&realpath
);
101 * Normalize "path", prepending the "prefix" for relative paths. If
102 * remaining_prefix is not NULL, return the actual prefix still
103 * remains in the path. For example, prefix = sub1/sub2/ and path is
105 * foo -> sub1/sub2/foo (full prefix)
106 * ../foo -> sub1/foo (remaining prefix is sub1/)
107 * ../../bar -> bar (no remaining prefix)
108 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
109 * `pwd`/../bar -> sub1/bar (no remaining prefix)
111 char *prefix_path_gently(const char *prefix
, int len
,
112 int *remaining_prefix
, const char *path
)
114 const char *orig
= path
;
116 if (is_absolute_path(orig
)) {
117 sanitized
= xmallocz(strlen(path
));
118 if (remaining_prefix
)
119 *remaining_prefix
= 0;
120 if (normalize_path_copy_len(sanitized
, path
, remaining_prefix
)) {
124 if (abspath_part_inside_repo(sanitized
)) {
129 sanitized
= xstrfmt("%.*s%s", len
, len
? prefix
: "", path
);
130 if (remaining_prefix
)
131 *remaining_prefix
= len
;
132 if (normalize_path_copy_len(sanitized
, sanitized
, remaining_prefix
)) {
140 char *prefix_path(const char *prefix
, int len
, const char *path
)
142 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
144 const char *hint_path
= get_git_work_tree();
146 hint_path
= get_git_dir();
147 die(_("'%s' is outside repository at '%s'"), path
,
148 absolute_path(hint_path
));
153 int path_inside_repo(const char *prefix
, const char *path
)
155 int len
= prefix
? strlen(prefix
) : 0;
156 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
164 int check_filename(const char *prefix
, const char *arg
)
166 char *to_free
= NULL
;
169 if (skip_prefix(arg
, ":/", &arg
)) {
170 if (!*arg
) /* ":/" is root dir, always exists */
173 } else if (skip_prefix(arg
, ":!", &arg
) ||
174 skip_prefix(arg
, ":^", &arg
)) {
175 if (!*arg
) /* excluding everything is silly, but allowed */
180 arg
= to_free
= prefix_filename(prefix
, arg
);
182 if (!lstat(arg
, &st
)) {
184 return 1; /* file exists */
186 if (is_missing_file_error(errno
)) {
188 return 0; /* file does not exist */
190 die_errno(_("failed to stat '%s'"), arg
);
193 static void NORETURN
die_verify_filename(struct repository
*r
,
196 int diagnose_misspelt_rev
)
198 if (!diagnose_misspelt_rev
)
199 die(_("%s: no such path in the working tree.\n"
200 "Use 'git <command> -- <path>...' to specify paths that do not exist locally."),
203 * Saying "'(icase)foo' does not exist in the index" when the
204 * user gave us ":(icase)foo" is just stupid. A magic pathspec
205 * begins with a colon and is followed by a non-alnum; do not
206 * let maybe_die_on_misspelt_object_name() even trigger.
208 if (!(arg
[0] == ':' && !isalnum(arg
[1])))
209 maybe_die_on_misspelt_object_name(r
, arg
, prefix
);
211 /* ... or fall back the most general message. */
212 die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
213 "Use '--' to separate paths from revisions, like this:\n"
214 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
219 * Check for arguments that don't resolve as actual files,
220 * but which look sufficiently like pathspecs that we'll consider
221 * them such for the purposes of rev/pathspec DWIM parsing.
223 static int looks_like_pathspec(const char *arg
)
229 * Wildcard characters imply the user is looking to match pathspecs
230 * that aren't in the filesystem. Note that this doesn't include
231 * backslash even though it's a glob special; by itself it doesn't
232 * cause any increase in the match. Likewise ignore backslash-escaped
233 * wildcard characters.
235 for (p
= arg
; *p
; p
++) {
238 } else if (is_glob_special(*p
)) {
246 /* long-form pathspec magic */
247 if (starts_with(arg
, ":("))
254 * Verify a filename that we got as an argument for a pathspec
255 * entry. Note that a filename that begins with "-" never verifies
256 * as true, because even if such a filename were to exist, we want
257 * it to be preceded by the "--" marker (or we want the user to
258 * use a format like "./-filename")
260 * The "diagnose_misspelt_rev" is used to provide a user-friendly
261 * diagnosis when dying upon finding that "name" is not a pathname.
262 * If set to 1, the diagnosis will try to diagnose "name" as an
263 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
264 * will only complain about an inexisting file.
266 * This function is typically called to check that a "file or rev"
267 * argument is unambiguous. In this case, the caller will want
268 * diagnose_misspelt_rev == 1 when verifying the first non-rev
269 * argument (which could have been a revision), and
270 * diagnose_misspelt_rev == 0 for the next ones (because we already
271 * saw a filename, there's not ambiguity anymore).
273 void verify_filename(const char *prefix
,
275 int diagnose_misspelt_rev
)
278 die(_("option '%s' must come before non-option arguments"), arg
);
279 if (looks_like_pathspec(arg
) || check_filename(prefix
, arg
))
281 die_verify_filename(the_repository
, prefix
, arg
, diagnose_misspelt_rev
);
285 * Opposite of the above: the command line did not have -- marker
286 * and we parsed the arg as a refname. It should not be interpretable
289 void verify_non_filename(const char *prefix
, const char *arg
)
291 if (!is_inside_work_tree() || is_inside_git_dir())
295 if (!check_filename(prefix
, arg
))
297 die(_("ambiguous argument '%s': both revision and filename\n"
298 "Use '--' to separate paths from revisions, like this:\n"
299 "'git <command> [<revision>...] -- [<file>...]'"), arg
);
302 int get_common_dir(struct strbuf
*sb
, const char *gitdir
)
304 const char *git_env_common_dir
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
305 if (git_env_common_dir
) {
306 strbuf_addstr(sb
, git_env_common_dir
);
309 return get_common_dir_noenv(sb
, gitdir
);
313 int get_common_dir_noenv(struct strbuf
*sb
, const char *gitdir
)
315 struct strbuf data
= STRBUF_INIT
;
316 struct strbuf path
= STRBUF_INIT
;
319 strbuf_addf(&path
, "%s/commondir", gitdir
);
320 if (file_exists(path
.buf
)) {
321 if (strbuf_read_file(&data
, path
.buf
, 0) <= 0)
322 die_errno(_("failed to read %s"), path
.buf
);
323 while (data
.len
&& (data
.buf
[data
.len
- 1] == '\n' ||
324 data
.buf
[data
.len
- 1] == '\r'))
326 data
.buf
[data
.len
] = '\0';
328 if (!is_absolute_path(data
.buf
))
329 strbuf_addf(&path
, "%s/", gitdir
);
330 strbuf_addbuf(&path
, &data
);
331 strbuf_add_real_path(sb
, path
.buf
);
334 strbuf_addstr(sb
, gitdir
);
337 strbuf_release(&data
);
338 strbuf_release(&path
);
343 * Test if it looks like we're at a git directory.
346 * - either an objects/ directory _or_ the proper
347 * GIT_OBJECT_DIRECTORY environment variable
348 * - a refs/ directory
349 * - either a HEAD symlink or a HEAD file that is formatted as
350 * a proper "ref:", or a regular file HEAD that has a properly
351 * formatted sha1 object name.
353 int is_git_directory(const char *suspect
)
355 struct strbuf path
= STRBUF_INIT
;
359 /* Check worktree-related signatures */
360 strbuf_addstr(&path
, suspect
);
361 strbuf_complete(&path
, '/');
362 strbuf_addstr(&path
, "HEAD");
363 if (validate_headref(path
.buf
))
367 get_common_dir(&path
, suspect
);
370 /* Check non-worktree-related signatures */
371 if (getenv(DB_ENVIRONMENT
)) {
372 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
376 strbuf_setlen(&path
, len
);
377 strbuf_addstr(&path
, "/objects");
378 if (access(path
.buf
, X_OK
))
382 strbuf_setlen(&path
, len
);
383 strbuf_addstr(&path
, "/refs");
384 if (access(path
.buf
, X_OK
))
389 strbuf_release(&path
);
393 int is_nonbare_repository_dir(struct strbuf
*path
)
397 size_t orig_path_len
= path
->len
;
398 assert(orig_path_len
!= 0);
399 strbuf_complete(path
, '/');
400 strbuf_addstr(path
, ".git");
401 if (read_gitfile_gently(path
->buf
, &gitfile_error
) || is_git_directory(path
->buf
))
403 if (gitfile_error
== READ_GITFILE_ERR_OPEN_FAILED
||
404 gitfile_error
== READ_GITFILE_ERR_READ_FAILED
)
406 strbuf_setlen(path
, orig_path_len
);
410 int is_inside_git_dir(void)
412 if (inside_git_dir
< 0)
413 inside_git_dir
= is_inside_dir(get_git_dir());
414 return inside_git_dir
;
417 int is_inside_work_tree(void)
419 if (inside_work_tree
< 0)
420 inside_work_tree
= is_inside_dir(get_git_work_tree());
421 return inside_work_tree
;
424 void setup_work_tree(void)
426 const char *work_tree
;
427 static int initialized
= 0;
432 if (work_tree_config_is_bogus
)
433 die(_("unable to set up work tree using invalid config"));
435 work_tree
= get_git_work_tree();
436 if (!work_tree
|| chdir_notify(work_tree
))
437 die(_("this operation must be run in a work tree"));
440 * Make sure subsequent git processes find correct worktree
441 * if $GIT_WORK_TREE is set relative
443 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
444 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
449 static void setup_original_cwd(void)
451 struct strbuf tmp
= STRBUF_INIT
;
452 const char *worktree
= NULL
;
455 if (!tmp_original_cwd
)
459 * startup_info->original_cwd points to the current working
460 * directory we inherited from our parent process, which is a
461 * directory we want to avoid removing.
463 * For convience, we would like to have the path relative to the
464 * worktree instead of an absolute path.
466 * Yes, startup_info->original_cwd is usually the same as 'prefix',
467 * but differs in two ways:
468 * - prefix has a trailing '/'
469 * - if the user passes '-C' to git, that modifies the prefix but
470 * not startup_info->original_cwd.
473 /* Normalize the directory */
474 if (!strbuf_realpath(&tmp
, tmp_original_cwd
, 0)) {
475 trace2_data_string("setup", the_repository
,
476 "realpath-path", tmp_original_cwd
);
477 trace2_data_string("setup", the_repository
,
478 "realpath-failure", strerror(errno
));
479 free((char*)tmp_original_cwd
);
480 tmp_original_cwd
= NULL
;
484 free((char*)tmp_original_cwd
);
485 tmp_original_cwd
= NULL
;
486 startup_info
->original_cwd
= strbuf_detach(&tmp
, NULL
);
489 * Get our worktree; we only protect the current working directory
490 * if it's in the worktree.
492 worktree
= get_git_work_tree();
494 goto no_prevention_needed
;
496 offset
= dir_inside_of(startup_info
->original_cwd
, worktree
);
499 * If startup_info->original_cwd == worktree, that is already
500 * protected and we don't need original_cwd as a secondary
501 * protection measure.
503 if (!*(startup_info
->original_cwd
+ offset
))
504 goto no_prevention_needed
;
507 * original_cwd was inside worktree; precompose it just as
508 * we do prefix so that built up paths will match
510 startup_info
->original_cwd
= \
511 precompose_string_if_needed(startup_info
->original_cwd
516 no_prevention_needed
:
517 free((char*)startup_info
->original_cwd
);
518 startup_info
->original_cwd
= NULL
;
521 static int read_worktree_config(const char *var
, const char *value
, void *vdata
)
523 struct repository_format
*data
= vdata
;
525 if (strcmp(var
, "core.bare") == 0) {
526 data
->is_bare
= git_config_bool(var
, value
);
527 } else if (strcmp(var
, "core.worktree") == 0) {
529 return config_error_nonbool(var
);
530 free(data
->work_tree
);
531 data
->work_tree
= xstrdup(value
);
536 enum extension_result
{
537 EXTENSION_ERROR
= -1, /* compatible with error(), etc */
538 EXTENSION_UNKNOWN
= 0,
543 * Do not add new extensions to this function. It handles extensions which are
544 * respected even in v0-format repositories for historical compatibility.
546 static enum extension_result
handle_extension_v0(const char *var
,
549 struct repository_format
*data
)
551 if (!strcmp(ext
, "noop")) {
553 } else if (!strcmp(ext
, "preciousobjects")) {
554 data
->precious_objects
= git_config_bool(var
, value
);
556 } else if (!strcmp(ext
, "partialclone")) {
557 data
->partial_clone
= xstrdup(value
);
559 } else if (!strcmp(ext
, "worktreeconfig")) {
560 data
->worktree_config
= git_config_bool(var
, value
);
564 return EXTENSION_UNKNOWN
;
568 * Record any new extensions in this function.
570 static enum extension_result
handle_extension(const char *var
,
573 struct repository_format
*data
)
575 if (!strcmp(ext
, "noop-v1")) {
577 } else if (!strcmp(ext
, "objectformat")) {
581 return config_error_nonbool(var
);
582 format
= hash_algo_by_name(value
);
583 if (format
== GIT_HASH_UNKNOWN
)
584 return error(_("invalid value for '%s': '%s'"),
585 "extensions.objectformat", value
);
586 data
->hash_algo
= format
;
589 return EXTENSION_UNKNOWN
;
592 static int check_repo_format(const char *var
, const char *value
, void *vdata
)
594 struct repository_format
*data
= vdata
;
597 if (strcmp(var
, "core.repositoryformatversion") == 0)
598 data
->version
= git_config_int(var
, value
);
599 else if (skip_prefix(var
, "extensions.", &ext
)) {
600 switch (handle_extension_v0(var
, value
, ext
, data
)) {
601 case EXTENSION_ERROR
:
605 case EXTENSION_UNKNOWN
:
609 switch (handle_extension(var
, value
, ext
, data
)) {
610 case EXTENSION_ERROR
:
613 string_list_append(&data
->v1_only_extensions
, ext
);
615 case EXTENSION_UNKNOWN
:
616 string_list_append(&data
->unknown_extensions
, ext
);
621 return read_worktree_config(var
, value
, vdata
);
624 static int check_repository_format_gently(const char *gitdir
, struct repository_format
*candidate
, int *nongit_ok
)
626 struct strbuf sb
= STRBUF_INIT
;
627 struct strbuf err
= STRBUF_INIT
;
630 has_common
= get_common_dir(&sb
, gitdir
);
631 strbuf_addstr(&sb
, "/config");
632 read_repository_format(candidate
, sb
.buf
);
636 * For historical use of check_repository_format() in git-init,
637 * we treat a missing config as a silent "ok", even when nongit_ok
640 if (candidate
->version
< 0)
643 if (verify_repository_format(candidate
, &err
) < 0) {
645 warning("%s", err
.buf
);
646 strbuf_release(&err
);
653 repository_format_precious_objects
= candidate
->precious_objects
;
654 repository_format_worktree_config
= candidate
->worktree_config
;
655 string_list_clear(&candidate
->unknown_extensions
, 0);
656 string_list_clear(&candidate
->v1_only_extensions
, 0);
658 if (repository_format_worktree_config
) {
660 * pick up core.bare and core.worktree from per-worktree
663 strbuf_addf(&sb
, "%s/config.worktree", gitdir
);
664 git_config_from_file(read_worktree_config
, sb
.buf
, candidate
);
670 if (candidate
->is_bare
!= -1) {
671 is_bare_repository_cfg
= candidate
->is_bare
;
672 if (is_bare_repository_cfg
== 1)
673 inside_work_tree
= -1;
675 if (candidate
->work_tree
) {
676 free(git_work_tree_cfg
);
677 git_work_tree_cfg
= xstrdup(candidate
->work_tree
);
678 inside_work_tree
= -1;
685 int upgrade_repository_format(int target_version
)
687 struct strbuf sb
= STRBUF_INIT
;
688 struct strbuf err
= STRBUF_INIT
;
689 struct strbuf repo_version
= STRBUF_INIT
;
690 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
692 strbuf_git_common_path(&sb
, the_repository
, "config");
693 read_repository_format(&repo_fmt
, sb
.buf
);
696 if (repo_fmt
.version
>= target_version
)
699 if (verify_repository_format(&repo_fmt
, &err
) < 0) {
700 error("cannot upgrade repository format from %d to %d: %s",
701 repo_fmt
.version
, target_version
, err
.buf
);
702 strbuf_release(&err
);
705 if (!repo_fmt
.version
&& repo_fmt
.unknown_extensions
.nr
)
706 return error("cannot upgrade repository format: "
707 "unknown extension %s",
708 repo_fmt
.unknown_extensions
.items
[0].string
);
710 strbuf_addf(&repo_version
, "%d", target_version
);
711 git_config_set("core.repositoryformatversion", repo_version
.buf
);
712 strbuf_release(&repo_version
);
716 static void init_repository_format(struct repository_format
*format
)
718 const struct repository_format fresh
= REPOSITORY_FORMAT_INIT
;
720 memcpy(format
, &fresh
, sizeof(fresh
));
723 int read_repository_format(struct repository_format
*format
, const char *path
)
725 clear_repository_format(format
);
726 git_config_from_file(check_repo_format
, path
, format
);
727 if (format
->version
== -1)
728 clear_repository_format(format
);
729 return format
->version
;
732 void clear_repository_format(struct repository_format
*format
)
734 string_list_clear(&format
->unknown_extensions
, 0);
735 string_list_clear(&format
->v1_only_extensions
, 0);
736 free(format
->work_tree
);
737 free(format
->partial_clone
);
738 init_repository_format(format
);
741 int verify_repository_format(const struct repository_format
*format
,
744 if (GIT_REPO_VERSION_READ
< format
->version
) {
745 strbuf_addf(err
, _("Expected git repo version <= %d, found %d"),
746 GIT_REPO_VERSION_READ
, format
->version
);
750 if (format
->version
>= 1 && format
->unknown_extensions
.nr
) {
753 strbuf_addstr(err
, Q_("unknown repository extension found:",
754 "unknown repository extensions found:",
755 format
->unknown_extensions
.nr
));
757 for (i
= 0; i
< format
->unknown_extensions
.nr
; i
++)
758 strbuf_addf(err
, "\n\t%s",
759 format
->unknown_extensions
.items
[i
].string
);
763 if (format
->version
== 0 && format
->v1_only_extensions
.nr
) {
767 Q_("repo version is 0, but v1-only extension found:",
768 "repo version is 0, but v1-only extensions found:",
769 format
->v1_only_extensions
.nr
));
771 for (i
= 0; i
< format
->v1_only_extensions
.nr
; i
++)
772 strbuf_addf(err
, "\n\t%s",
773 format
->v1_only_extensions
.items
[i
].string
);
780 void read_gitfile_error_die(int error_code
, const char *path
, const char *dir
)
782 switch (error_code
) {
783 case READ_GITFILE_ERR_STAT_FAILED
:
784 case READ_GITFILE_ERR_NOT_A_FILE
:
785 /* non-fatal; follow return path */
787 case READ_GITFILE_ERR_OPEN_FAILED
:
788 die_errno(_("error opening '%s'"), path
);
789 case READ_GITFILE_ERR_TOO_LARGE
:
790 die(_("too large to be a .git file: '%s'"), path
);
791 case READ_GITFILE_ERR_READ_FAILED
:
792 die(_("error reading %s"), path
);
793 case READ_GITFILE_ERR_INVALID_FORMAT
:
794 die(_("invalid gitfile format: %s"), path
);
795 case READ_GITFILE_ERR_NO_PATH
:
796 die(_("no path in gitfile: %s"), path
);
797 case READ_GITFILE_ERR_NOT_A_REPO
:
798 die(_("not a git repository: %s"), dir
);
800 BUG("unknown error code");
805 * Try to read the location of the git directory from the .git file,
806 * return path to git directory if found. The return value comes from
809 * On failure, if return_error_code is not NULL, return_error_code
810 * will be set to an error code and NULL will be returned. If
811 * return_error_code is NULL the function will die instead (for most
814 const char *read_gitfile_gently(const char *path
, int *return_error_code
)
816 const int max_file_size
= 1 << 20; /* 1MB */
824 static struct strbuf realpath
= STRBUF_INIT
;
826 if (stat(path
, &st
)) {
827 /* NEEDSWORK: discern between ENOENT vs other errors */
828 error_code
= READ_GITFILE_ERR_STAT_FAILED
;
831 if (!S_ISREG(st
.st_mode
)) {
832 error_code
= READ_GITFILE_ERR_NOT_A_FILE
;
835 if (st
.st_size
> max_file_size
) {
836 error_code
= READ_GITFILE_ERR_TOO_LARGE
;
839 fd
= open(path
, O_RDONLY
);
841 error_code
= READ_GITFILE_ERR_OPEN_FAILED
;
844 buf
= xmallocz(st
.st_size
);
845 len
= read_in_full(fd
, buf
, st
.st_size
);
847 if (len
!= st
.st_size
) {
848 error_code
= READ_GITFILE_ERR_READ_FAILED
;
851 if (!starts_with(buf
, "gitdir: ")) {
852 error_code
= READ_GITFILE_ERR_INVALID_FORMAT
;
855 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
858 error_code
= READ_GITFILE_ERR_NO_PATH
;
864 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
865 size_t pathlen
= slash
+1 - path
;
866 dir
= xstrfmt("%.*s%.*s", (int)pathlen
, path
,
867 (int)(len
- 8), buf
+ 8);
871 if (!is_git_directory(dir
)) {
872 error_code
= READ_GITFILE_ERR_NOT_A_REPO
;
876 strbuf_realpath(&realpath
, dir
, 1);
880 if (return_error_code
)
881 *return_error_code
= error_code
;
883 read_gitfile_error_die(error_code
, path
, dir
);
886 return error_code
? NULL
: path
;
889 static const char *setup_explicit_git_dir(const char *gitdirenv
,
891 struct repository_format
*repo_fmt
,
894 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
895 const char *worktree
;
899 if (PATH_MAX
- 40 < strlen(gitdirenv
))
900 die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT
);
902 gitfile
= (char*)read_gitfile(gitdirenv
);
904 gitfile
= xstrdup(gitfile
);
908 if (!is_git_directory(gitdirenv
)) {
914 die(_("not a git repository: '%s'"), gitdirenv
);
917 if (check_repository_format_gently(gitdirenv
, repo_fmt
, nongit_ok
)) {
922 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
924 set_git_work_tree(work_tree_env
);
925 else if (is_bare_repository_cfg
> 0) {
926 if (git_work_tree_cfg
) {
928 warning("core.bare and core.worktree do not make sense");
929 work_tree_config_is_bogus
= 1;
933 set_git_dir(gitdirenv
, 0);
937 else if (git_work_tree_cfg
) { /* #6, #14 */
938 if (is_absolute_path(git_work_tree_cfg
))
939 set_git_work_tree(git_work_tree_cfg
);
942 if (chdir(gitdirenv
))
943 die_errno(_("cannot chdir to '%s'"), gitdirenv
);
944 if (chdir(git_work_tree_cfg
))
945 die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg
);
946 core_worktree
= xgetcwd();
948 die_errno(_("cannot come back to cwd"));
949 set_git_work_tree(core_worktree
);
953 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, 1)) {
955 set_git_dir(gitdirenv
, 0);
960 set_git_work_tree(".");
962 /* set_git_work_tree() must have been called by now */
963 worktree
= get_git_work_tree();
965 /* both get_git_work_tree() and cwd are already normalized */
966 if (!strcmp(cwd
->buf
, worktree
)) { /* cwd == worktree */
967 set_git_dir(gitdirenv
, 0);
972 offset
= dir_inside_of(cwd
->buf
, worktree
);
973 if (offset
>= 0) { /* cwd inside worktree? */
974 set_git_dir(gitdirenv
, 1);
976 die_errno(_("cannot chdir to '%s'"), worktree
);
977 strbuf_addch(cwd
, '/');
979 return cwd
->buf
+ offset
;
982 /* cwd outside worktree */
983 set_git_dir(gitdirenv
, 0);
988 static const char *setup_discovered_git_dir(const char *gitdir
,
989 struct strbuf
*cwd
, int offset
,
990 struct repository_format
*repo_fmt
,
993 if (check_repository_format_gently(gitdir
, repo_fmt
, nongit_ok
))
996 /* --work-tree is set without --git-dir; use discovered one */
997 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
998 char *to_free
= NULL
;
1001 if (offset
!= cwd
->len
&& !is_absolute_path(gitdir
))
1002 gitdir
= to_free
= real_pathdup(gitdir
, 1);
1003 if (chdir(cwd
->buf
))
1004 die_errno(_("cannot come back to cwd"));
1005 ret
= setup_explicit_git_dir(gitdir
, cwd
, repo_fmt
, nongit_ok
);
1010 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
1011 if (is_bare_repository_cfg
> 0) {
1012 set_git_dir(gitdir
, (offset
!= cwd
->len
));
1013 if (chdir(cwd
->buf
))
1014 die_errno(_("cannot come back to cwd"));
1018 /* #0, #1, #5, #8, #9, #12, #13 */
1019 set_git_work_tree(".");
1020 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
1021 set_git_dir(gitdir
, 0);
1023 inside_work_tree
= 1;
1024 if (offset
>= cwd
->len
)
1027 /* Make "offset" point past the '/' (already the case for root dirs) */
1028 if (offset
!= offset_1st_component(cwd
->buf
))
1030 /* Add a '/' at the end */
1031 strbuf_addch(cwd
, '/');
1032 return cwd
->buf
+ offset
;
1035 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
1036 static const char *setup_bare_git_dir(struct strbuf
*cwd
, int offset
,
1037 struct repository_format
*repo_fmt
,
1042 if (check_repository_format_gently(".", repo_fmt
, nongit_ok
))
1045 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
1047 /* --work-tree is set without --git-dir; use discovered one */
1048 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
1049 static const char *gitdir
;
1051 gitdir
= offset
== cwd
->len
? "." : xmemdupz(cwd
->buf
, offset
);
1052 if (chdir(cwd
->buf
))
1053 die_errno(_("cannot come back to cwd"));
1054 return setup_explicit_git_dir(gitdir
, cwd
, repo_fmt
, nongit_ok
);
1058 inside_work_tree
= 0;
1059 if (offset
!= cwd
->len
) {
1060 if (chdir(cwd
->buf
))
1061 die_errno(_("cannot come back to cwd"));
1062 root_len
= offset_1st_component(cwd
->buf
);
1063 strbuf_setlen(cwd
, offset
> root_len
? offset
: root_len
);
1064 set_git_dir(cwd
->buf
, 0);
1067 set_git_dir(".", 0);
1071 static dev_t
get_device_or_die(const char *path
, const char *prefix
, int prefix_len
)
1074 if (stat(path
, &buf
)) {
1075 die_errno(_("failed to stat '%*s%s%s'"),
1077 prefix
? prefix
: "",
1078 prefix
? "/" : "", path
);
1084 * A "string_list_each_func_t" function that canonicalizes an entry
1085 * from GIT_CEILING_DIRECTORIES using real_pathdup(), or
1086 * discards it if unusable. The presence of an empty entry in
1087 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
1088 * subsequent entries.
1090 static int canonicalize_ceiling_entry(struct string_list_item
*item
,
1093 int *empty_entry_found
= cb_data
;
1094 char *ceil
= item
->string
;
1097 *empty_entry_found
= 1;
1099 } else if (!is_absolute_path(ceil
)) {
1101 } else if (*empty_entry_found
) {
1102 /* Keep entry but do not canonicalize it */
1105 char *real_path
= real_pathdup(ceil
, 0);
1110 item
->string
= real_path
;
1115 struct safe_directory_data
{
1120 static int safe_directory_cb(const char *key
, const char *value
, void *d
)
1122 struct safe_directory_data
*data
= d
;
1124 if (strcmp(key
, "safe.directory"))
1127 if (!value
|| !*value
) {
1129 } else if (!strcmp(value
, "*")) {
1132 const char *interpolated
= NULL
;
1134 if (!git_config_pathname(&interpolated
, key
, value
) &&
1135 !fspathcmp(data
->path
, interpolated
? interpolated
: value
))
1138 free((char *)interpolated
);
1145 * Check if a repository is safe, by verifying the ownership of the
1146 * worktree (if any), the git directory, and the gitfile (if any).
1148 * Exemptions for known-safe repositories can be added via `safe.directory`
1149 * config settings; for non-bare repositories, their worktree needs to be
1150 * added, for bare ones their git directory.
1152 static int ensure_valid_ownership(const char *gitfile
,
1153 const char *worktree
, const char *gitdir
,
1154 struct strbuf
*report
)
1156 struct safe_directory_data data
= {
1157 .path
= worktree
? worktree
: gitdir
1160 if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
1161 (!gitfile
|| is_path_owned_by_current_user(gitfile
, report
)) &&
1162 (!worktree
|| is_path_owned_by_current_user(worktree
, report
)) &&
1163 (!gitdir
|| is_path_owned_by_current_user(gitdir
, report
)))
1167 * data.path is the "path" that identifies the repository and it is
1168 * constant regardless of what failed above. data.is_safe should be
1169 * initialized to false, and might be changed by the callback.
1171 git_protected_config(safe_directory_cb
, &data
);
1173 return data
.is_safe
;
1176 void die_upon_dubious_ownership(const char *gitfile
, const char *worktree
,
1179 struct strbuf report
= STRBUF_INIT
, quoted
= STRBUF_INIT
;
1182 if (ensure_valid_ownership(gitfile
, worktree
, gitdir
, &report
))
1185 strbuf_complete(&report
, '\n');
1186 path
= gitfile
? gitfile
: gitdir
;
1187 sq_quote_buf_pretty("ed
, path
);
1189 die(_("detected dubious ownership in repository at '%s'\n"
1191 "To add an exception for this directory, call:\n"
1193 "\tgit config --global --add safe.directory %s"),
1194 path
, report
.buf
, quoted
.buf
);
1197 static int allowed_bare_repo_cb(const char *key
, const char *value
, void *d
)
1199 enum allowed_bare_repo
*allowed_bare_repo
= d
;
1201 if (strcasecmp(key
, "safe.bareRepository"))
1204 if (!strcmp(value
, "explicit")) {
1205 *allowed_bare_repo
= ALLOWED_BARE_REPO_EXPLICIT
;
1208 if (!strcmp(value
, "all")) {
1209 *allowed_bare_repo
= ALLOWED_BARE_REPO_ALL
;
1215 static enum allowed_bare_repo
get_allowed_bare_repo(void)
1217 enum allowed_bare_repo result
= ALLOWED_BARE_REPO_ALL
;
1218 git_protected_config(allowed_bare_repo_cb
, &result
);
1222 static const char *allowed_bare_repo_to_string(
1223 enum allowed_bare_repo allowed_bare_repo
)
1225 switch (allowed_bare_repo
) {
1226 case ALLOWED_BARE_REPO_EXPLICIT
:
1228 case ALLOWED_BARE_REPO_ALL
:
1231 BUG("invalid allowed_bare_repo %d",
1237 enum discovery_result
{
1242 /* these are errors */
1243 GIT_DIR_HIT_CEILING
= -1,
1244 GIT_DIR_HIT_MOUNT_POINT
= -2,
1245 GIT_DIR_INVALID_GITFILE
= -3,
1246 GIT_DIR_INVALID_OWNERSHIP
= -4,
1247 GIT_DIR_DISALLOWED_BARE
= -5,
1251 * We cannot decide in this function whether we are in the work tree or
1252 * not, since the config can only be read _after_ this function was called.
1254 * Also, we avoid changing any global state (such as the current working
1255 * directory) to allow early callers.
1257 * The directory where the search should start needs to be passed in via the
1258 * `dir` parameter; upon return, the `dir` buffer will contain the path of
1259 * the directory where the search ended, and `gitdir` will contain the path of
1260 * the discovered .git/ directory, if any. If `gitdir` is not absolute, it
1261 * is relative to `dir` (i.e. *not* necessarily the cwd).
1263 static enum discovery_result
setup_git_directory_gently_1(struct strbuf
*dir
,
1264 struct strbuf
*gitdir
,
1265 struct strbuf
*report
,
1268 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
1269 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
1270 const char *gitdirenv
;
1271 int ceil_offset
= -1, min_offset
= offset_1st_component(dir
->buf
);
1272 dev_t current_device
= 0;
1273 int one_filesystem
= 1;
1276 * If GIT_DIR is set explicitly, we're not going
1277 * to do any discovery, but we still do repository
1280 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
1282 strbuf_addstr(gitdir
, gitdirenv
);
1283 return GIT_DIR_EXPLICIT
;
1286 if (env_ceiling_dirs
) {
1287 int empty_entry_found
= 0;
1289 string_list_split(&ceiling_dirs
, env_ceiling_dirs
, PATH_SEP
, -1);
1290 filter_string_list(&ceiling_dirs
, 0,
1291 canonicalize_ceiling_entry
, &empty_entry_found
);
1292 ceil_offset
= longest_ancestor_length(dir
->buf
, &ceiling_dirs
);
1293 string_list_clear(&ceiling_dirs
, 0);
1296 if (ceil_offset
< 0)
1297 ceil_offset
= min_offset
- 2;
1299 if (min_offset
&& min_offset
== dir
->len
&&
1300 !is_dir_sep(dir
->buf
[min_offset
- 1])) {
1301 strbuf_addch(dir
, '/');
1306 * Test in the following order (relative to the dir):
1307 * - .git (file containing "gitdir: <path>")
1316 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
1318 current_device
= get_device_or_die(dir
->buf
, NULL
, 0);
1320 int offset
= dir
->len
, error_code
= 0;
1321 char *gitdir_path
= NULL
;
1322 char *gitfile
= NULL
;
1324 if (offset
> min_offset
)
1325 strbuf_addch(dir
, '/');
1326 strbuf_addstr(dir
, DEFAULT_GIT_DIR_ENVIRONMENT
);
1327 gitdirenv
= read_gitfile_gently(dir
->buf
, die_on_error
?
1328 NULL
: &error_code
);
1331 error_code
== READ_GITFILE_ERR_NOT_A_FILE
) {
1332 /* NEEDSWORK: fail if .git is not file nor dir */
1333 if (is_git_directory(dir
->buf
)) {
1334 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
1335 gitdir_path
= xstrdup(dir
->buf
);
1337 } else if (error_code
!= READ_GITFILE_ERR_STAT_FAILED
)
1338 return GIT_DIR_INVALID_GITFILE
;
1340 gitfile
= xstrdup(dir
->buf
);
1342 * Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
1343 * to check that directory for a repository.
1344 * Now trim that tentative addition away, because we want to
1345 * focus on the real directory we are in.
1347 strbuf_setlen(dir
, offset
);
1349 enum discovery_result ret
;
1350 const char *gitdir_candidate
=
1351 gitdir_path
? gitdir_path
: gitdirenv
;
1353 if (ensure_valid_ownership(gitfile
, dir
->buf
,
1354 gitdir_candidate
, report
)) {
1355 strbuf_addstr(gitdir
, gitdirenv
);
1356 ret
= GIT_DIR_DISCOVERED
;
1358 ret
= GIT_DIR_INVALID_OWNERSHIP
;
1361 * Earlier, during discovery, we might have allocated
1362 * string copies for gitdir_path or gitfile so make
1363 * sure we don't leak by freeing them now, before
1364 * leaving the loop and function.
1366 * Note: gitdirenv will be non-NULL whenever these are
1367 * allocated, therefore we need not take care of releasing
1368 * them outside of this conditional block.
1376 if (is_git_directory(dir
->buf
)) {
1377 trace2_data_string("setup", NULL
, "implicit-bare-repository", dir
->buf
);
1378 if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT
)
1379 return GIT_DIR_DISALLOWED_BARE
;
1380 if (!ensure_valid_ownership(NULL
, NULL
, dir
->buf
, report
))
1381 return GIT_DIR_INVALID_OWNERSHIP
;
1382 strbuf_addstr(gitdir
, ".");
1383 return GIT_DIR_BARE
;
1386 if (offset
<= min_offset
)
1387 return GIT_DIR_HIT_CEILING
;
1389 while (--offset
> ceil_offset
&& !is_dir_sep(dir
->buf
[offset
]))
1391 if (offset
<= ceil_offset
)
1392 return GIT_DIR_HIT_CEILING
;
1394 strbuf_setlen(dir
, offset
> min_offset
? offset
: min_offset
);
1395 if (one_filesystem
&&
1396 current_device
!= get_device_or_die(dir
->buf
, NULL
, offset
))
1397 return GIT_DIR_HIT_MOUNT_POINT
;
1401 int discover_git_directory(struct strbuf
*commondir
,
1402 struct strbuf
*gitdir
)
1404 struct strbuf dir
= STRBUF_INIT
, err
= STRBUF_INIT
;
1405 size_t gitdir_offset
= gitdir
->len
, cwd_len
;
1406 size_t commondir_offset
= commondir
->len
;
1407 struct repository_format candidate
= REPOSITORY_FORMAT_INIT
;
1409 if (strbuf_getcwd(&dir
))
1413 if (setup_git_directory_gently_1(&dir
, gitdir
, NULL
, 0) <= 0) {
1414 strbuf_release(&dir
);
1419 * The returned gitdir is relative to dir, and if dir does not reflect
1420 * the current working directory, we simply make the gitdir absolute.
1422 if (dir
.len
< cwd_len
&& !is_absolute_path(gitdir
->buf
+ gitdir_offset
)) {
1423 /* Avoid a trailing "/." */
1424 if (!strcmp(".", gitdir
->buf
+ gitdir_offset
))
1425 strbuf_setlen(gitdir
, gitdir_offset
);
1427 strbuf_addch(&dir
, '/');
1428 strbuf_insert(gitdir
, gitdir_offset
, dir
.buf
, dir
.len
);
1431 get_common_dir(commondir
, gitdir
->buf
+ gitdir_offset
);
1434 strbuf_addf(&dir
, "%s/config", commondir
->buf
+ commondir_offset
);
1435 read_repository_format(&candidate
, dir
.buf
);
1436 strbuf_release(&dir
);
1438 if (verify_repository_format(&candidate
, &err
) < 0) {
1439 warning("ignoring git dir '%s': %s",
1440 gitdir
->buf
+ gitdir_offset
, err
.buf
);
1441 strbuf_release(&err
);
1442 strbuf_setlen(commondir
, commondir_offset
);
1443 strbuf_setlen(gitdir
, gitdir_offset
);
1444 clear_repository_format(&candidate
);
1448 /* take ownership of candidate.partial_clone */
1449 the_repository
->repository_format_partial_clone
=
1450 candidate
.partial_clone
;
1451 candidate
.partial_clone
= NULL
;
1453 clear_repository_format(&candidate
);
1457 const char *setup_git_directory_gently(int *nongit_ok
)
1459 static struct strbuf cwd
= STRBUF_INIT
;
1460 struct strbuf dir
= STRBUF_INIT
, gitdir
= STRBUF_INIT
, report
= STRBUF_INIT
;
1461 const char *prefix
= NULL
;
1462 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
1465 * We may have read an incomplete configuration before
1466 * setting-up the git directory. If so, clear the cache so
1467 * that the next queries to the configuration reload complete
1468 * configuration (including the per-repo config file that we
1469 * ignored previously).
1474 * Let's assume that we are in a git repository.
1475 * If it turns out later that we are somewhere else, the value will be
1476 * updated accordingly.
1481 if (strbuf_getcwd(&cwd
))
1482 die_errno(_("Unable to read current working directory"));
1483 strbuf_addbuf(&dir
, &cwd
);
1485 switch (setup_git_directory_gently_1(&dir
, &gitdir
, &report
, 1)) {
1486 case GIT_DIR_EXPLICIT
:
1487 prefix
= setup_explicit_git_dir(gitdir
.buf
, &cwd
, &repo_fmt
, nongit_ok
);
1489 case GIT_DIR_DISCOVERED
:
1490 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1491 die(_("cannot change to '%s'"), dir
.buf
);
1492 prefix
= setup_discovered_git_dir(gitdir
.buf
, &cwd
, dir
.len
,
1493 &repo_fmt
, nongit_ok
);
1496 if (dir
.len
< cwd
.len
&& chdir(dir
.buf
))
1497 die(_("cannot change to '%s'"), dir
.buf
);
1498 prefix
= setup_bare_git_dir(&cwd
, dir
.len
, &repo_fmt
, nongit_ok
);
1500 case GIT_DIR_HIT_CEILING
:
1502 die(_("not a git repository (or any of the parent directories): %s"),
1503 DEFAULT_GIT_DIR_ENVIRONMENT
);
1506 case GIT_DIR_HIT_MOUNT_POINT
:
1508 die(_("not a git repository (or any parent up to mount point %s)\n"
1509 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
1513 case GIT_DIR_INVALID_OWNERSHIP
:
1515 struct strbuf quoted
= STRBUF_INIT
;
1517 strbuf_complete(&report
, '\n');
1518 sq_quote_buf_pretty("ed
, dir
.buf
);
1519 die(_("detected dubious ownership in repository at '%s'\n"
1521 "To add an exception for this directory, call:\n"
1523 "\tgit config --global --add safe.directory %s"),
1524 dir
.buf
, report
.buf
, quoted
.buf
);
1528 case GIT_DIR_DISALLOWED_BARE
:
1530 die(_("cannot use bare repository '%s' (safe.bareRepository is '%s')"),
1532 allowed_bare_repo_to_string(get_allowed_bare_repo()));
1538 * As a safeguard against setup_git_directory_gently_1 returning
1539 * this value, fallthrough to BUG. Otherwise it is possible to
1540 * set startup_info->have_repository to 1 when we did nothing to
1541 * find a repository.
1544 BUG("unhandled setup_git_directory_gently_1() result");
1548 * At this point, nongit_ok is stable. If it is non-NULL and points
1549 * to a non-zero value, then this means that we haven't found a
1550 * repository and that the caller expects startup_info to reflect
1553 * Regardless of the state of nongit_ok, startup_info->prefix and
1554 * the GIT_PREFIX environment variable must always match. For details
1555 * see Documentation/config/alias.txt.
1557 if (nongit_ok
&& *nongit_ok
)
1558 startup_info
->have_repository
= 0;
1560 startup_info
->have_repository
= 1;
1563 * Not all paths through the setup code will call 'set_git_dir()' (which
1564 * directly sets up the environment) so in order to guarantee that the
1565 * environment is in a consistent state after setup, explicitly setup
1566 * the environment if we have a repository.
1568 * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
1569 * code paths so we also need to explicitly setup the environment if
1570 * the user has set GIT_DIR. It may be beneficial to disallow bogus
1571 * GIT_DIR values at some point in the future.
1573 if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */
1574 startup_info
->have_repository
||
1575 /* GIT_DIR_EXPLICIT */
1576 getenv(GIT_DIR_ENVIRONMENT
)) {
1577 if (!the_repository
->gitdir
) {
1578 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
1580 gitdir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
1581 setup_git_env(gitdir
);
1583 if (startup_info
->have_repository
) {
1584 repo_set_hash_algo(the_repository
, repo_fmt
.hash_algo
);
1585 /* take ownership of repo_fmt.partial_clone */
1586 the_repository
->repository_format_partial_clone
=
1587 repo_fmt
.partial_clone
;
1588 repo_fmt
.partial_clone
= NULL
;
1592 * Since precompose_string_if_needed() needs to look at
1593 * the core.precomposeunicode configuration, this
1594 * has to happen after the above block that finds
1595 * out where the repository is, i.e. a preparation
1596 * for calling git_config_get_bool().
1599 prefix
= precompose_string_if_needed(prefix
);
1600 startup_info
->prefix
= prefix
;
1601 setenv(GIT_PREFIX_ENVIRONMENT
, prefix
, 1);
1603 startup_info
->prefix
= NULL
;
1604 setenv(GIT_PREFIX_ENVIRONMENT
, "", 1);
1607 setup_original_cwd();
1609 strbuf_release(&dir
);
1610 strbuf_release(&gitdir
);
1611 strbuf_release(&report
);
1612 clear_repository_format(&repo_fmt
);
1617 int git_config_perm(const char *var
, const char *value
)
1625 if (!strcmp(value
, "umask"))
1627 if (!strcmp(value
, "group"))
1629 if (!strcmp(value
, "all") ||
1630 !strcmp(value
, "world") ||
1631 !strcmp(value
, "everybody"))
1632 return PERM_EVERYBODY
;
1634 /* Parse octal numbers */
1635 i
= strtol(value
, &endptr
, 8);
1637 /* If not an octal number, maybe true/false? */
1639 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
1642 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
1643 * a chmod value to restrict to.
1646 case PERM_UMASK
: /* 0 */
1648 case OLD_PERM_GROUP
: /* 1 */
1650 case OLD_PERM_EVERYBODY
: /* 2 */
1651 return PERM_EVERYBODY
;
1654 /* A filemode value was given: 0xxx */
1656 if ((i
& 0600) != 0600)
1657 die(_("problem with core.sharedRepository filemode value "
1658 "(0%.3o).\nThe owner of files must always have "
1659 "read and write permissions."), i
);
1662 * Mask filemode value. Others can not get write permission.
1663 * x flags for directories are handled separately.
1668 void check_repository_format(struct repository_format
*fmt
)
1670 struct repository_format repo_fmt
= REPOSITORY_FORMAT_INIT
;
1673 check_repository_format_gently(get_git_dir(), fmt
, NULL
);
1674 startup_info
->have_repository
= 1;
1675 repo_set_hash_algo(the_repository
, fmt
->hash_algo
);
1676 the_repository
->repository_format_partial_clone
=
1677 xstrdup_or_null(fmt
->partial_clone
);
1678 clear_repository_format(&repo_fmt
);
1682 * Returns the "prefix", a path to the current working directory
1683 * relative to the work tree root, or NULL, if the current working
1684 * directory is not a strict subdirectory of the work tree root. The
1685 * prefix always ends with a '/' character.
1687 const char *setup_git_directory(void)
1689 return setup_git_directory_gently(NULL
);
1692 const char *resolve_gitdir_gently(const char *suspect
, int *return_error_code
)
1694 if (is_git_directory(suspect
))
1696 return read_gitfile_gently(suspect
, return_error_code
);
1699 /* if any standard file descriptor is missing open it to /dev/null */
1700 void sanitize_stdfds(void)
1702 int fd
= xopen("/dev/null", O_RDWR
);
1711 #ifdef NO_POSIX_GOODIES
1719 die_errno(_("fork failed"));
1724 die_errno(_("setsid failed"));
1733 #ifndef DEFAULT_GIT_TEMPLATE_DIR
1734 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
1737 struct template_dir_cb_data
{
1742 static int template_dir_cb(const char *key
, const char *value
, void *d
)
1744 struct template_dir_cb_data
*data
= d
;
1746 if (strcmp(key
, "init.templatedir"))
1754 FREE_AND_NULL(data
->path
);
1755 if (!git_config_pathname((const char **)&path
, key
, value
))
1756 data
->path
= path
? path
: xstrdup(value
);
1762 const char *get_template_dir(const char *option_template
)
1764 const char *template_dir
= option_template
;
1767 template_dir
= getenv(TEMPLATE_DIR_ENVIRONMENT
);
1768 if (!template_dir
) {
1769 static struct template_dir_cb_data data
;
1771 if (!data
.initialized
) {
1772 git_protected_config(template_dir_cb
, &data
);
1773 data
.initialized
= 1;
1775 template_dir
= data
.path
;
1777 if (!template_dir
) {
1781 dir
= system_path(DEFAULT_GIT_TEMPLATE_DIR
);
1784 return template_dir
;