3 #include "string-list.h"
5 static int inside_git_dir
= -1;
6 static int inside_work_tree
= -1;
7 static int work_tree_config_is_bogus
;
8 static struct string_list unknown_extensions
= STRING_LIST_INIT_DUP
;
11 * The input parameter must contain an absolute path, and it must already be
14 * Find the part of an absolute path that lies inside the work tree by
15 * dereferencing symlinks outside the work tree, for example:
16 * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
17 * /dir/file (work tree is /) -> dir/file
18 * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
19 * /dir/repolink/file (repolink points to /dir/repo) -> file
20 * /dir/repo (exactly equal to work tree) -> (empty string)
22 static int abspath_part_inside_repo(char *path
)
28 const char *work_tree
= get_git_work_tree();
32 wtlen
= strlen(work_tree
);
34 off
= offset_1st_component(path
);
36 /* check if work tree is already the prefix */
37 if (wtlen
<= len
&& !strncmp(path
, work_tree
, wtlen
)) {
38 if (path
[wtlen
] == '/') {
39 memmove(path
, path
+ wtlen
+ 1, len
- wtlen
);
41 } else if (path
[wtlen
- 1] == '/' || path
[wtlen
] == '\0') {
42 /* work tree is the root, or the whole path */
43 memmove(path
, path
+ wtlen
, len
- wtlen
+ 1);
46 /* work tree might match beginning of a symlink to work tree */
52 /* check each '/'-terminated level */
57 if (strcmp(real_path(path0
), work_tree
) == 0) {
58 memmove(path0
, path
+ 1, len
- (path
- path0
));
65 /* check whole path */
66 if (strcmp(real_path(path0
), work_tree
) == 0) {
75 * Normalize "path", prepending the "prefix" for relative paths. If
76 * remaining_prefix is not NULL, return the actual prefix still
77 * remains in the path. For example, prefix = sub1/sub2/ and path is
79 * foo -> sub1/sub2/foo (full prefix)
80 * ../foo -> sub1/foo (remaining prefix is sub1/)
81 * ../../bar -> bar (no remaining prefix)
82 * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
83 * `pwd`/../bar -> sub1/bar (no remaining prefix)
85 char *prefix_path_gently(const char *prefix
, int len
,
86 int *remaining_prefix
, const char *path
)
88 const char *orig
= path
;
90 if (is_absolute_path(orig
)) {
91 sanitized
= xmalloc(strlen(path
) + 1);
93 *remaining_prefix
= 0;
94 if (normalize_path_copy_len(sanitized
, path
, remaining_prefix
)) {
98 if (abspath_part_inside_repo(sanitized
)) {
103 sanitized
= xmalloc(len
+ strlen(path
) + 1);
105 memcpy(sanitized
, prefix
, len
);
106 strcpy(sanitized
+ len
, path
);
107 if (remaining_prefix
)
108 *remaining_prefix
= len
;
109 if (normalize_path_copy_len(sanitized
, sanitized
, remaining_prefix
)) {
117 char *prefix_path(const char *prefix
, int len
, const char *path
)
119 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
121 die("'%s' is outside repository", path
);
125 int path_inside_repo(const char *prefix
, const char *path
)
127 int len
= prefix
? strlen(prefix
) : 0;
128 char *r
= prefix_path_gently(prefix
, len
, NULL
, path
);
136 int check_filename(const char *prefix
, const char *arg
)
141 if (starts_with(arg
, ":/")) {
142 if (arg
[2] == '\0') /* ":/" is root dir, always exists */
145 } else if (!no_wildcard(arg
))
148 name
= prefix_filename(prefix
, strlen(prefix
), arg
);
151 if (!lstat(name
, &st
))
152 return 1; /* file exists */
153 if (errno
== ENOENT
|| errno
== ENOTDIR
)
154 return 0; /* file does not exist */
155 die_errno("failed to stat '%s'", arg
);
158 static void NORETURN
die_verify_filename(const char *prefix
,
160 int diagnose_misspelt_rev
)
162 if (!diagnose_misspelt_rev
)
163 die("%s: no such path in the working tree.\n"
164 "Use 'git <command> -- <path>...' to specify paths that do not exist locally.",
167 * Saying "'(icase)foo' does not exist in the index" when the
168 * user gave us ":(icase)foo" is just stupid. A magic pathspec
169 * begins with a colon and is followed by a non-alnum; do not
170 * let maybe_die_on_misspelt_object_name() even trigger.
172 if (!(arg
[0] == ':' && !isalnum(arg
[1])))
173 maybe_die_on_misspelt_object_name(arg
, prefix
);
175 /* ... or fall back the most general message. */
176 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
177 "Use '--' to separate paths from revisions, like this:\n"
178 "'git <command> [<revision>...] -- [<file>...]'", arg
);
183 * Verify a filename that we got as an argument for a pathspec
184 * entry. Note that a filename that begins with "-" never verifies
185 * as true, because even if such a filename were to exist, we want
186 * it to be preceded by the "--" marker (or we want the user to
187 * use a format like "./-filename")
189 * The "diagnose_misspelt_rev" is used to provide a user-friendly
190 * diagnosis when dying upon finding that "name" is not a pathname.
191 * If set to 1, the diagnosis will try to diagnose "name" as an
192 * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
193 * will only complain about an inexisting file.
195 * This function is typically called to check that a "file or rev"
196 * argument is unambiguous. In this case, the caller will want
197 * diagnose_misspelt_rev == 1 when verifying the first non-rev
198 * argument (which could have been a revision), and
199 * diagnose_misspelt_rev == 0 for the next ones (because we already
200 * saw a filename, there's not ambiguity anymore).
202 void verify_filename(const char *prefix
,
204 int diagnose_misspelt_rev
)
207 die("bad flag '%s' used after filename", arg
);
208 if (check_filename(prefix
, arg
))
210 die_verify_filename(prefix
, arg
, diagnose_misspelt_rev
);
214 * Opposite of the above: the command line did not have -- marker
215 * and we parsed the arg as a refname. It should not be interpretable
218 void verify_non_filename(const char *prefix
, const char *arg
)
220 if (!is_inside_work_tree() || is_inside_git_dir())
224 if (!check_filename(prefix
, arg
))
226 die("ambiguous argument '%s': both revision and filename\n"
227 "Use '--' to separate paths from revisions, like this:\n"
228 "'git <command> [<revision>...] -- [<file>...]'", arg
);
231 int get_common_dir(struct strbuf
*sb
, const char *gitdir
)
233 const char *git_env_common_dir
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
234 if (git_env_common_dir
) {
235 strbuf_addstr(sb
, git_env_common_dir
);
238 return get_common_dir_noenv(sb
, gitdir
);
242 int get_common_dir_noenv(struct strbuf
*sb
, const char *gitdir
)
244 struct strbuf data
= STRBUF_INIT
;
245 struct strbuf path
= STRBUF_INIT
;
248 strbuf_addf(&path
, "%s/commondir", gitdir
);
249 if (file_exists(path
.buf
)) {
250 if (strbuf_read_file(&data
, path
.buf
, 0) <= 0)
251 die_errno(_("failed to read %s"), path
.buf
);
252 while (data
.len
&& (data
.buf
[data
.len
- 1] == '\n' ||
253 data
.buf
[data
.len
- 1] == '\r'))
255 data
.buf
[data
.len
] = '\0';
257 if (!is_absolute_path(data
.buf
))
258 strbuf_addf(&path
, "%s/", gitdir
);
259 strbuf_addbuf(&path
, &data
);
260 strbuf_addstr(sb
, real_path(path
.buf
));
263 strbuf_addstr(sb
, gitdir
);
264 strbuf_release(&data
);
265 strbuf_release(&path
);
270 * Test if it looks like we're at a git directory.
273 * - either an objects/ directory _or_ the proper
274 * GIT_OBJECT_DIRECTORY environment variable
275 * - a refs/ directory
276 * - either a HEAD symlink or a HEAD file that is formatted as
277 * a proper "ref:", or a regular file HEAD that has a properly
278 * formatted sha1 object name.
280 int is_git_directory(const char *suspect
)
282 struct strbuf path
= STRBUF_INIT
;
286 /* Check worktree-related signatures */
287 strbuf_addf(&path
, "%s/HEAD", suspect
);
288 if (validate_headref(path
.buf
))
292 get_common_dir(&path
, suspect
);
295 /* Check non-worktree-related signatures */
296 if (getenv(DB_ENVIRONMENT
)) {
297 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
301 strbuf_setlen(&path
, len
);
302 strbuf_addstr(&path
, "/objects");
303 if (access(path
.buf
, X_OK
))
307 strbuf_setlen(&path
, len
);
308 strbuf_addstr(&path
, "/refs");
309 if (access(path
.buf
, X_OK
))
314 strbuf_release(&path
);
318 int is_inside_git_dir(void)
320 if (inside_git_dir
< 0)
321 inside_git_dir
= is_inside_dir(get_git_dir());
322 return inside_git_dir
;
325 int is_inside_work_tree(void)
327 if (inside_work_tree
< 0)
328 inside_work_tree
= is_inside_dir(get_git_work_tree());
329 return inside_work_tree
;
332 void setup_work_tree(void)
334 const char *work_tree
, *git_dir
;
335 static int initialized
= 0;
340 if (work_tree_config_is_bogus
)
341 die("unable to set up work tree using invalid config");
343 work_tree
= get_git_work_tree();
344 git_dir
= get_git_dir();
345 if (!is_absolute_path(git_dir
))
346 git_dir
= real_path(get_git_dir());
347 if (!work_tree
|| chdir(work_tree
))
348 die("This operation must be run in a work tree");
351 * Make sure subsequent git processes find correct worktree
352 * if $GIT_WORK_TREE is set relative
354 if (getenv(GIT_WORK_TREE_ENVIRONMENT
))
355 setenv(GIT_WORK_TREE_ENVIRONMENT
, ".", 1);
357 set_git_dir(remove_leading_path(git_dir
, work_tree
));
361 static int check_repo_format(const char *var
, const char *value
, void *cb
)
365 if (strcmp(var
, "core.repositoryformatversion") == 0)
366 repository_format_version
= git_config_int(var
, value
);
367 else if (strcmp(var
, "core.sharedrepository") == 0)
368 shared_repository
= git_config_perm(var
, value
);
369 else if (skip_prefix(var
, "extensions.", &ext
)) {
371 * record any known extensions here; otherwise,
372 * we fall through to recording it as unknown, and
373 * check_repository_format will complain
375 if (!strcmp(ext
, "noop"))
377 else if (!strcmp(ext
, "preciousobjects"))
378 repository_format_precious_objects
= git_config_bool(var
, value
);
380 string_list_append(&unknown_extensions
, ext
);
385 static int check_repository_format_gently(const char *gitdir
, int *nongit_ok
)
387 struct strbuf sb
= STRBUF_INIT
;
388 const char *repo_config
;
392 string_list_clear(&unknown_extensions
, 0);
394 if (get_common_dir(&sb
, gitdir
))
395 fn
= check_repo_format
;
397 fn
= check_repository_format_version
;
398 strbuf_addstr(&sb
, "/config");
399 repo_config
= sb
.buf
;
402 * git_config() can't be used here because it calls git_pathdup()
403 * to get $GIT_CONFIG/config. That call will make setup_git_env()
404 * set git_dir to ".git".
406 * We are in gitdir setup, no git dir has been found useable yet.
407 * Use a gentler version of git_config() to check if this repo
410 git_config_early(fn
, NULL
, repo_config
);
411 if (GIT_REPO_VERSION_READ
< repository_format_version
) {
413 die ("Expected git repo version <= %d, found %d",
414 GIT_REPO_VERSION_READ
, repository_format_version
);
415 warning("Expected git repo version <= %d, found %d",
416 GIT_REPO_VERSION_READ
, repository_format_version
);
417 warning("Please upgrade Git");
422 if (repository_format_version
>= 1 && unknown_extensions
.nr
) {
426 die("unknown repository extension: %s",
427 unknown_extensions
.items
[0].string
);
429 for (i
= 0; i
< unknown_extensions
.nr
; i
++)
430 warning("unknown repository extension: %s",
431 unknown_extensions
.items
[i
].string
);
440 static void update_linked_gitdir(const char *gitfile
, const char *gitdir
)
442 struct strbuf path
= STRBUF_INIT
;
445 strbuf_addf(&path
, "%s/gitdir", gitdir
);
446 if (stat(path
.buf
, &st
) || st
.st_mtime
+ 24 * 3600 < time(NULL
))
447 write_file(path
.buf
, "%s", gitfile
);
448 strbuf_release(&path
);
452 * Try to read the location of the git directory from the .git file,
453 * return path to git directory if found.
455 * On failure, if return_error_code is not NULL, return_error_code
456 * will be set to an error code and NULL will be returned. If
457 * return_error_code is NULL the function will die instead (for most
460 const char *read_gitfile_gently(const char *path
, int *return_error_code
)
462 const int max_file_size
= 1 << 20; /* 1MB */
471 if (stat(path
, &st
)) {
472 error_code
= READ_GITFILE_ERR_STAT_FAILED
;
475 if (!S_ISREG(st
.st_mode
)) {
476 error_code
= READ_GITFILE_ERR_NOT_A_FILE
;
479 if (st
.st_size
> max_file_size
) {
480 error_code
= READ_GITFILE_ERR_TOO_LARGE
;
483 fd
= open(path
, O_RDONLY
);
485 error_code
= READ_GITFILE_ERR_OPEN_FAILED
;
488 buf
= xmalloc(st
.st_size
+ 1);
489 len
= read_in_full(fd
, buf
, st
.st_size
);
491 if (len
!= st
.st_size
) {
492 error_code
= READ_GITFILE_ERR_READ_FAILED
;
496 if (!starts_with(buf
, "gitdir: ")) {
497 error_code
= READ_GITFILE_ERR_INVALID_FORMAT
;
500 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
503 error_code
= READ_GITFILE_ERR_NO_PATH
;
509 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
510 size_t pathlen
= slash
+1 - path
;
511 size_t dirlen
= pathlen
+ len
- 8;
512 dir
= xmalloc(dirlen
+ 1);
513 strncpy(dir
, path
, pathlen
);
514 strncpy(dir
+ pathlen
, buf
+ 8, len
- 8);
519 if (!is_git_directory(dir
)) {
520 error_code
= READ_GITFILE_ERR_NOT_A_REPO
;
523 update_linked_gitdir(path
, dir
);
524 path
= real_path(dir
);
527 if (return_error_code
)
528 *return_error_code
= error_code
;
529 else if (error_code
) {
530 switch (error_code
) {
531 case READ_GITFILE_ERR_STAT_FAILED
:
532 case READ_GITFILE_ERR_NOT_A_FILE
:
533 /* non-fatal; follow return path */
535 case READ_GITFILE_ERR_OPEN_FAILED
:
536 die_errno("Error opening '%s'", path
);
537 case READ_GITFILE_ERR_TOO_LARGE
:
538 die("Too large to be a .git file: '%s'", path
);
539 case READ_GITFILE_ERR_READ_FAILED
:
540 die("Error reading %s", path
);
541 case READ_GITFILE_ERR_INVALID_FORMAT
:
542 die("Invalid gitfile format: %s", path
);
543 case READ_GITFILE_ERR_NO_PATH
:
544 die("No path in gitfile: %s", path
);
545 case READ_GITFILE_ERR_NOT_A_REPO
:
546 die("Not a git repository: %s", dir
);
553 return error_code
? NULL
: path
;
556 static const char *setup_explicit_git_dir(const char *gitdirenv
,
560 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
561 const char *worktree
;
565 if (PATH_MAX
- 40 < strlen(gitdirenv
))
566 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
568 gitfile
= (char*)read_gitfile(gitdirenv
);
570 gitfile
= xstrdup(gitfile
);
574 if (!is_git_directory(gitdirenv
)) {
580 die("Not a git repository: '%s'", gitdirenv
);
583 if (check_repository_format_gently(gitdirenv
, nongit_ok
)) {
588 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
590 set_git_work_tree(work_tree_env
);
591 else if (is_bare_repository_cfg
> 0) {
592 if (git_work_tree_cfg
) {
594 warning("core.bare and core.worktree do not make sense");
595 work_tree_config_is_bogus
= 1;
599 set_git_dir(gitdirenv
);
603 else if (git_work_tree_cfg
) { /* #6, #14 */
604 if (is_absolute_path(git_work_tree_cfg
))
605 set_git_work_tree(git_work_tree_cfg
);
608 if (chdir(gitdirenv
))
609 die_errno("Could not chdir to '%s'", gitdirenv
);
610 if (chdir(git_work_tree_cfg
))
611 die_errno("Could not chdir to '%s'", git_work_tree_cfg
);
612 core_worktree
= xgetcwd();
614 die_errno("Could not come back to cwd");
615 set_git_work_tree(core_worktree
);
619 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, 1)) {
621 set_git_dir(gitdirenv
);
626 set_git_work_tree(".");
628 /* set_git_work_tree() must have been called by now */
629 worktree
= get_git_work_tree();
631 /* both get_git_work_tree() and cwd are already normalized */
632 if (!strcmp(cwd
->buf
, worktree
)) { /* cwd == worktree */
633 set_git_dir(gitdirenv
);
638 offset
= dir_inside_of(cwd
->buf
, worktree
);
639 if (offset
>= 0) { /* cwd inside worktree? */
640 set_git_dir(real_path(gitdirenv
));
642 die_errno("Could not chdir to '%s'", worktree
);
643 strbuf_addch(cwd
, '/');
645 return cwd
->buf
+ offset
;
648 /* cwd outside worktree */
649 set_git_dir(gitdirenv
);
654 static const char *setup_discovered_git_dir(const char *gitdir
,
655 struct strbuf
*cwd
, int offset
,
658 if (check_repository_format_gently(gitdir
, nongit_ok
))
661 /* --work-tree is set without --git-dir; use discovered one */
662 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
663 if (offset
!= cwd
->len
&& !is_absolute_path(gitdir
))
664 gitdir
= xstrdup(real_path(gitdir
));
666 die_errno("Could not come back to cwd");
667 return setup_explicit_git_dir(gitdir
, cwd
, nongit_ok
);
670 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
671 if (is_bare_repository_cfg
> 0) {
672 set_git_dir(offset
== cwd
->len
? gitdir
: real_path(gitdir
));
674 die_errno("Could not come back to cwd");
678 /* #0, #1, #5, #8, #9, #12, #13 */
679 set_git_work_tree(".");
680 if (strcmp(gitdir
, DEFAULT_GIT_DIR_ENVIRONMENT
))
683 inside_work_tree
= 1;
684 if (offset
== cwd
->len
)
687 /* Make "offset" point to past the '/', and add a '/' at the end */
689 strbuf_addch(cwd
, '/');
690 return cwd
->buf
+ offset
;
693 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
694 static const char *setup_bare_git_dir(struct strbuf
*cwd
, int offset
,
699 if (check_repository_format_gently(".", nongit_ok
))
702 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
704 /* --work-tree is set without --git-dir; use discovered one */
705 if (getenv(GIT_WORK_TREE_ENVIRONMENT
) || git_work_tree_cfg
) {
708 gitdir
= offset
== cwd
->len
? "." : xmemdupz(cwd
->buf
, offset
);
710 die_errno("Could not come back to cwd");
711 return setup_explicit_git_dir(gitdir
, cwd
, nongit_ok
);
715 inside_work_tree
= 0;
716 if (offset
!= cwd
->len
) {
718 die_errno("Cannot come back to cwd");
719 root_len
= offset_1st_component(cwd
->buf
);
720 strbuf_setlen(cwd
, offset
> root_len
? offset
: root_len
);
721 set_git_dir(cwd
->buf
);
728 static const char *setup_nongit(const char *cwd
, int *nongit_ok
)
731 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
733 die_errno("Cannot come back to cwd");
738 static dev_t
get_device_or_die(const char *path
, const char *prefix
, int prefix_len
)
741 if (stat(path
, &buf
)) {
742 die_errno("failed to stat '%*s%s%s'",
744 prefix
? prefix
: "",
745 prefix
? "/" : "", path
);
751 * A "string_list_each_func_t" function that canonicalizes an entry
752 * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or
753 * discards it if unusable. The presence of an empty entry in
754 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
755 * subsequent entries.
757 static int canonicalize_ceiling_entry(struct string_list_item
*item
,
760 int *empty_entry_found
= cb_data
;
761 char *ceil
= item
->string
;
764 *empty_entry_found
= 1;
766 } else if (!is_absolute_path(ceil
)) {
768 } else if (*empty_entry_found
) {
769 /* Keep entry but do not canonicalize it */
772 const char *real_path
= real_path_if_valid(ceil
);
776 item
->string
= xstrdup(real_path
);
782 * We cannot decide in this function whether we are in the work tree or
783 * not, since the config can only be read _after_ this function was called.
785 static const char *setup_git_directory_gently_1(int *nongit_ok
)
787 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
788 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
789 static struct strbuf cwd
= STRBUF_INIT
;
790 const char *gitdirenv
, *ret
;
792 int offset
, offset_parent
, ceil_offset
= -1;
793 dev_t current_device
= 0;
794 int one_filesystem
= 1;
797 * We may have read an incomplete configuration before
798 * setting-up the git directory. If so, clear the cache so
799 * that the next queries to the configuration reload complete
800 * configuration (including the per-repo config file that we
801 * ignored previously).
806 * Let's assume that we are in a git repository.
807 * If it turns out later that we are somewhere else, the value will be
808 * updated accordingly.
813 if (strbuf_getcwd(&cwd
))
814 die_errno("Unable to read current working directory");
818 * If GIT_DIR is set explicitly, we're not going
819 * to do any discovery, but we still do repository
822 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
824 return setup_explicit_git_dir(gitdirenv
, &cwd
, nongit_ok
);
826 if (env_ceiling_dirs
) {
827 int empty_entry_found
= 0;
829 string_list_split(&ceiling_dirs
, env_ceiling_dirs
, PATH_SEP
, -1);
830 filter_string_list(&ceiling_dirs
, 0,
831 canonicalize_ceiling_entry
, &empty_entry_found
);
832 ceil_offset
= longest_ancestor_length(cwd
.buf
, &ceiling_dirs
);
833 string_list_clear(&ceiling_dirs
, 0);
836 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
.buf
))
840 * Test in the following order (relative to the cwd):
841 * - .git (file containing "gitdir: <path>")
850 one_filesystem
= !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
852 current_device
= get_device_or_die(".", NULL
, 0);
854 gitfile
= (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT
);
856 gitdirenv
= gitfile
= xstrdup(gitfile
);
858 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
859 gitdirenv
= DEFAULT_GIT_DIR_ENVIRONMENT
;
863 ret
= setup_discovered_git_dir(gitdirenv
,
871 if (is_git_directory("."))
872 return setup_bare_git_dir(&cwd
, offset
, nongit_ok
);
874 offset_parent
= offset
;
875 while (--offset_parent
> ceil_offset
&& cwd
.buf
[offset_parent
] != '/');
876 if (offset_parent
<= ceil_offset
)
877 return setup_nongit(cwd
.buf
, nongit_ok
);
878 if (one_filesystem
) {
879 dev_t parent_device
= get_device_or_die("..", cwd
.buf
,
881 if (parent_device
!= current_device
) {
884 die_errno("Cannot come back to cwd");
888 strbuf_setlen(&cwd
, offset
);
889 die("Not a git repository (or any parent up to mount point %s)\n"
890 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).",
895 strbuf_setlen(&cwd
, offset
);
896 die_errno("Cannot change to '%s/..'", cwd
.buf
);
898 offset
= offset_parent
;
902 const char *setup_git_directory_gently(int *nongit_ok
)
906 prefix
= setup_git_directory_gently_1(nongit_ok
);
908 setenv(GIT_PREFIX_ENVIRONMENT
, prefix
, 1);
910 setenv(GIT_PREFIX_ENVIRONMENT
, "", 1);
913 startup_info
->have_repository
= !nongit_ok
|| !*nongit_ok
;
914 startup_info
->prefix
= prefix
;
919 int git_config_perm(const char *var
, const char *value
)
927 if (!strcmp(value
, "umask"))
929 if (!strcmp(value
, "group"))
931 if (!strcmp(value
, "all") ||
932 !strcmp(value
, "world") ||
933 !strcmp(value
, "everybody"))
934 return PERM_EVERYBODY
;
936 /* Parse octal numbers */
937 i
= strtol(value
, &endptr
, 8);
939 /* If not an octal number, maybe true/false? */
941 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
944 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
945 * a chmod value to restrict to.
948 case PERM_UMASK
: /* 0 */
950 case OLD_PERM_GROUP
: /* 1 */
952 case OLD_PERM_EVERYBODY
: /* 2 */
953 return PERM_EVERYBODY
;
956 /* A filemode value was given: 0xxx */
958 if ((i
& 0600) != 0600)
959 die("Problem with core.sharedRepository filemode value "
960 "(0%.3o).\nThe owner of files must always have "
961 "read and write permissions.", i
);
964 * Mask filemode value. Others can not get write permission.
965 * x flags for directories are handled separately.
970 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
972 int ret
= check_repo_format(var
, value
, cb
);
975 if (strcmp(var
, "core.bare") == 0) {
976 is_bare_repository_cfg
= git_config_bool(var
, value
);
977 if (is_bare_repository_cfg
== 1)
978 inside_work_tree
= -1;
979 } else if (strcmp(var
, "core.worktree") == 0) {
981 return config_error_nonbool(var
);
982 free(git_work_tree_cfg
);
983 git_work_tree_cfg
= xstrdup(value
);
984 inside_work_tree
= -1;
989 int check_repository_format(void)
991 return check_repository_format_gently(get_git_dir(), NULL
);
995 * Returns the "prefix", a path to the current working directory
996 * relative to the work tree root, or NULL, if the current working
997 * directory is not a strict subdirectory of the work tree root. The
998 * prefix always ends with a '/' character.
1000 const char *setup_git_directory(void)
1002 return setup_git_directory_gently(NULL
);
1005 const char *resolve_gitdir(const char *suspect
)
1007 if (is_git_directory(suspect
))
1009 return read_gitfile(suspect
);
1012 /* if any standard file descriptor is missing open it to /dev/null */
1013 void sanitize_stdfds(void)
1015 int fd
= open("/dev/null", O_RDWR
, 0);
1016 while (fd
!= -1 && fd
< 2)
1019 die_errno("open /dev/null or dup failed");
1026 #ifdef NO_POSIX_GOODIES
1034 die_errno("fork failed");
1039 die_errno("setsid failed");