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 struct strbuf data = STRBUF_INIT;
234 struct strbuf path = STRBUF_INIT;
235 const char *git_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
237 if (git_common_dir) {
238 strbuf_addstr(sb, git_common_dir);
241 strbuf_addf(&path, "%s/commondir", gitdir);
242 if (file_exists(path.buf)) {
243 if (strbuf_read_file(&data, path.buf, 0) <= 0)
244 die_errno(_("failed to read %s"), path.buf);
245 while (data.len && (data.buf[data.len - 1] == '\n' ||
246 data.buf[data.len - 1] == '\r'))
248 data.buf[data.len] = '\0';
250 if (!is_absolute_path(data.buf))
251 strbuf_addf(&path, "%s/", gitdir);
252 strbuf_addbuf(&path, &data);
253 strbuf_addstr(sb, real_path(path.buf));
256 strbuf_addstr(sb, gitdir);
257 strbuf_release(&data);
258 strbuf_release(&path);
263 * Test if it looks like we're at a git directory.
266 * - either an objects/ directory _or_ the proper
267 * GIT_OBJECT_DIRECTORY environment variable
268 * - a refs/ directory
269 * - either a HEAD symlink or a HEAD file that is formatted as
270 * a proper "ref:", or a regular file HEAD that has a properly
271 * formatted sha1 object name.
273 int is_git_directory(const char *suspect)
275 struct strbuf path = STRBUF_INIT;
279 /* Check worktree-related signatures */
280 strbuf_addf(&path, "%s/HEAD", suspect);
281 if (validate_headref(path.buf))
285 get_common_dir(&path, suspect);
288 /* Check non-worktree-related signatures */
289 if (getenv(DB_ENVIRONMENT)) {
290 if (access(getenv(DB_ENVIRONMENT), X_OK))
294 strbuf_setlen(&path, len);
295 strbuf_addstr(&path, "/objects");
296 if (access(path.buf, X_OK))
300 strbuf_setlen(&path, len);
301 strbuf_addstr(&path, "/refs");
302 if (access(path.buf, X_OK))
307 strbuf_release(&path);
311 int is_inside_git_dir(void)
313 if (inside_git_dir < 0)
314 inside_git_dir = is_inside_dir(get_git_dir());
315 return inside_git_dir;
318 int is_inside_work_tree(void)
320 if (inside_work_tree < 0)
321 inside_work_tree = is_inside_dir(get_git_work_tree());
322 return inside_work_tree;
325 void setup_work_tree(void)
327 const char *work_tree, *git_dir;
328 static int initialized = 0;
333 if (work_tree_config_is_bogus)
334 die("unable to set up work tree using invalid config");
336 work_tree = get_git_work_tree();
337 git_dir = get_git_dir();
338 if (!is_absolute_path(git_dir))
339 git_dir = real_path(get_git_dir());
340 if (!work_tree || chdir(work_tree))
341 die("This operation must be run in a work tree");
344 * Make sure subsequent git processes find correct worktree
345 * if $GIT_WORK_TREE is set relative
347 if (getenv(GIT_WORK_TREE_ENVIRONMENT))
348 setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
350 set_git_dir(remove_leading_path(git_dir, work_tree));
354 static int check_repo_format(const char *var, const char *value, void *cb)
358 if (strcmp(var, "core.repositoryformatversion") == 0)
359 repository_format_version = git_config_int(var, value);
360 else if (strcmp(var, "core.sharedrepository") == 0)
361 shared_repository = git_config_perm(var, value);
362 else if (skip_prefix(var, "extensions.", &ext)) {
364 * record any known extensions here; otherwise,
365 * we fall through to recording it as unknown, and
366 * check_repository_format will complain
368 if (!strcmp(ext, "noop"))
371 string_list_append(&unknown_extensions, ext);
376 static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
378 struct strbuf sb = STRBUF_INIT;
379 const char *repo_config;
383 string_list_clear(&unknown_extensions, 0);
385 if (get_common_dir(&sb, gitdir))
386 fn = check_repo_format;
388 fn = check_repository_format_version;
389 strbuf_addstr(&sb, "/config");
390 repo_config = sb.buf;
393 * git_config() can't be used here because it calls git_pathdup()
394 * to get $GIT_CONFIG/config. That call will make setup_git_env()
395 * set git_dir to ".git".
397 * We are in gitdir setup, no git dir has been found useable yet.
398 * Use a gentler version of git_config() to check if this repo
401 git_config_early(fn, NULL, repo_config);
402 if (GIT_REPO_VERSION_READ < repository_format_version) {
404 die ("Expected git repo version <= %d, found %d",
405 GIT_REPO_VERSION_READ, repository_format_version);
406 warning("Expected git repo version <= %d, found %d",
407 GIT_REPO_VERSION_READ, repository_format_version);
408 warning("Please upgrade Git");
413 if (repository_format_version >= 1 && unknown_extensions.nr) {
417 die("unknown repository extension: %s",
418 unknown_extensions.items[0].string);
420 for (i = 0; i < unknown_extensions.nr; i++)
421 warning("unknown repository extension: %s",
422 unknown_extensions.items[i].string);
431 static void update_linked_gitdir(const char *gitfile, const char *gitdir)
433 struct strbuf path = STRBUF_INIT;
436 strbuf_addf(&path, "%s/gitfile", gitdir);
437 if (stat(path.buf, &st) || st.st_mtime + 24 * 3600 < time(NULL))
438 write_file(path.buf, 0, "%s\n", gitfile);
439 strbuf_release(&path);
443 * Try to read the location of the git directory from the .git file,
444 * return path to git directory if found.
446 const char *read_gitfile(const char *path)
457 if (!S_ISREG(st.st_mode))
459 fd = open(path, O_RDONLY);
461 die_errno("Error opening '%s'", path);
462 buf = xmalloc(st.st_size + 1);
463 len = read_in_full(fd, buf, st.st_size);
465 if (len != st.st_size)
466 die("Error reading %s", path);
468 if (!starts_with(buf, "gitdir: "))
469 die("Invalid gitfile format: %s", path);
470 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
473 die("No path in gitfile: %s", path);
477 if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
478 size_t pathlen = slash+1 - path;
479 size_t dirlen = pathlen + len - 8;
480 dir = xmalloc(dirlen + 1);
481 strncpy(dir, path, pathlen);
482 strncpy(dir + pathlen, buf + 8, len - 8);
488 if (!is_git_directory(dir))
489 die("Not a git repository: %s", dir);
491 update_linked_gitdir(path, dir);
492 path = real_path(dir);
498 static const char *setup_explicit_git_dir(const char *gitdirenv,
502 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
503 const char *worktree;
507 if (PATH_MAX - 40 < strlen(gitdirenv))
508 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
510 gitfile = (char*)read_gitfile(gitdirenv);
512 gitfile = xstrdup(gitfile);
516 if (!is_git_directory(gitdirenv)) {
522 die("Not a git repository: '%s'", gitdirenv);
525 if (check_repository_format_gently(gitdirenv, nongit_ok)) {
530 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
532 set_git_work_tree(work_tree_env);
533 else if (is_bare_repository_cfg > 0) {
534 if (git_work_tree_cfg) {
536 warning("core.bare and core.worktree do not make sense");
537 work_tree_config_is_bogus = 1;
541 set_git_dir(gitdirenv);
545 else if (git_work_tree_cfg) { /* #6, #14 */
546 if (is_absolute_path(git_work_tree_cfg))
547 set_git_work_tree(git_work_tree_cfg);
550 if (chdir(gitdirenv))
551 die_errno("Could not chdir to '%s'", gitdirenv);
552 if (chdir(git_work_tree_cfg))
553 die_errno("Could not chdir to '%s'", git_work_tree_cfg);
554 core_worktree = xgetcwd();
556 die_errno("Could not come back to cwd");
557 set_git_work_tree(core_worktree);
561 else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
563 set_git_dir(gitdirenv);
568 set_git_work_tree(".");
570 /* set_git_work_tree() must have been called by now */
571 worktree = get_git_work_tree();
573 /* both get_git_work_tree() and cwd are already normalized */
574 if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
575 set_git_dir(gitdirenv);
580 offset = dir_inside_of(cwd->buf, worktree);
581 if (offset >= 0) { /* cwd inside worktree? */
582 set_git_dir(real_path(gitdirenv));
584 die_errno("Could not chdir to '%s'", worktree);
585 strbuf_addch(cwd, '/');
587 return cwd->buf + offset;
590 /* cwd outside worktree */
591 set_git_dir(gitdirenv);
596 static const char *setup_discovered_git_dir(const char *gitdir,
597 struct strbuf *cwd, int offset,
600 if (check_repository_format_gently(gitdir, nongit_ok))
603 /* --work-tree is set without --git-dir; use discovered one */
604 if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
605 if (offset != cwd->len && !is_absolute_path(gitdir))
606 gitdir = xstrdup(real_path(gitdir));
608 die_errno("Could not come back to cwd");
609 return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
612 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
613 if (is_bare_repository_cfg > 0) {
614 set_git_dir(offset == cwd->len ? gitdir : real_path(gitdir));
616 die_errno("Could not come back to cwd");
620 /* #0, #1, #5, #8, #9, #12, #13 */
621 set_git_work_tree(".");
622 if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
625 inside_work_tree = 1;
626 if (offset == cwd->len)
629 /* Make "offset" point to past the '/', and add a '/' at the end */
631 strbuf_addch(cwd, '/');
632 return cwd->buf + offset;
635 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
636 static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
641 if (check_repository_format_gently(".", nongit_ok))
644 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
646 /* --work-tree is set without --git-dir; use discovered one */
647 if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
650 gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);
652 die_errno("Could not come back to cwd");
653 return setup_explicit_git_dir(gitdir, cwd, nongit_ok);
657 inside_work_tree = 0;
658 if (offset != cwd->len) {
660 die_errno("Cannot come back to cwd");
661 root_len = offset_1st_component(cwd->buf);
662 strbuf_setlen(cwd, offset > root_len ? offset : root_len);
663 set_git_dir(cwd->buf);
670 static const char *setup_nongit(const char *cwd, int *nongit_ok)
673 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
675 die_errno("Cannot come back to cwd");
680 static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len)
683 if (stat(path, &buf)) {
684 die_errno("failed to stat '%*s%s%s'",
686 prefix ? prefix : "",
687 prefix ? "/" : "", path);
693 * A "string_list_each_func_t" function that canonicalizes an entry
694 * from GIT_CEILING_DIRECTORIES using real_path_if_valid(), or
695 * discards it if unusable. The presence of an empty entry in
696 * GIT_CEILING_DIRECTORIES turns off canonicalization for all
697 * subsequent entries.
699 static int canonicalize_ceiling_entry(struct string_list_item *item,
702 int *empty_entry_found = cb_data;
703 char *ceil = item->string;
706 *empty_entry_found = 1;
708 } else if (!is_absolute_path(ceil)) {
710 } else if (*empty_entry_found) {
711 /* Keep entry but do not canonicalize it */
714 const char *real_path = real_path_if_valid(ceil);
718 item->string = xstrdup(real_path);
724 * We cannot decide in this function whether we are in the work tree or
725 * not, since the config can only be read _after_ this function was called.
727 static const char *setup_git_directory_gently_1(int *nongit_ok)
729 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
730 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
731 static struct strbuf cwd = STRBUF_INIT;
732 const char *gitdirenv, *ret;
734 int offset, offset_parent, ceil_offset = -1;
735 dev_t current_device = 0;
736 int one_filesystem = 1;
739 * We may have read an incomplete configuration before
740 * setting-up the git directory. If so, clear the cache so
741 * that the next queries to the configuration reload complete
742 * configuration (including the per-repo config file that we
743 * ignored previously).
748 * Let's assume that we are in a git repository.
749 * If it turns out later that we are somewhere else, the value will be
750 * updated accordingly.
755 if (strbuf_getcwd(&cwd))
756 die_errno("Unable to read current working directory");
760 * If GIT_DIR is set explicitly, we're not going
761 * to do any discovery, but we still do repository
764 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
766 return setup_explicit_git_dir(gitdirenv, &cwd, nongit_ok);
768 if (env_ceiling_dirs) {
769 int empty_entry_found = 0;
771 string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1);
772 filter_string_list(&ceiling_dirs, 0,
773 canonicalize_ceiling_entry, &empty_entry_found);
774 ceil_offset = longest_ancestor_length(cwd.buf, &ceiling_dirs);
775 string_list_clear(&ceiling_dirs, 0);
778 if (ceil_offset < 0 && has_dos_drive_prefix(cwd.buf))
782 * Test in the following order (relative to the cwd):
783 * - .git (file containing "gitdir: <path>")
792 one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
794 current_device = get_device_or_die(".", NULL, 0);
796 gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
798 gitdirenv = gitfile = xstrdup(gitfile);
800 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
801 gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
805 ret = setup_discovered_git_dir(gitdirenv,
813 if (is_git_directory("."))
814 return setup_bare_git_dir(&cwd, offset, nongit_ok);
816 offset_parent = offset;
817 while (--offset_parent > ceil_offset && cwd.buf[offset_parent] != '/');
818 if (offset_parent <= ceil_offset)
819 return setup_nongit(cwd.buf, nongit_ok);
820 if (one_filesystem) {
821 dev_t parent_device = get_device_or_die("..", cwd.buf,
823 if (parent_device != current_device) {
826 die_errno("Cannot come back to cwd");
830 strbuf_setlen(&cwd, offset);
831 die("Not a git repository (or any parent up to mount point %s)\n"
832 "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).",
837 strbuf_setlen(&cwd, offset);
838 die_errno("Cannot change to '%s/..'", cwd.buf);
840 offset = offset_parent;
844 const char *setup_git_directory_gently(int *nongit_ok)
848 prefix = setup_git_directory_gently_1(nongit_ok);
850 setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
852 setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
855 startup_info->have_repository = !nongit_ok || !*nongit_ok;
856 startup_info->prefix = prefix;
861 int git_config_perm(const char *var, const char *value)
869 if (!strcmp(value, "umask"))
871 if (!strcmp(value, "group"))
873 if (!strcmp(value, "all") ||
874 !strcmp(value, "world") ||
875 !strcmp(value, "everybody"))
876 return PERM_EVERYBODY;
878 /* Parse octal numbers */
879 i = strtol(value, &endptr, 8);
881 /* If not an octal number, maybe true/false? */
883 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
886 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
887 * a chmod value to restrict to.
890 case PERM_UMASK: /* 0 */
892 case OLD_PERM_GROUP: /* 1 */
894 case OLD_PERM_EVERYBODY: /* 2 */
895 return PERM_EVERYBODY;
898 /* A filemode value was given: 0xxx */
900 if ((i & 0600) != 0600)
901 die("Problem with core.sharedRepository filemode value "
902 "(0%.3o).\nThe owner of files must always have "
903 "read and write permissions.", i);
906 * Mask filemode value. Others can not get write permission.
907 * x flags for directories are handled separately.
912 int check_repository_format_version(const char *var, const char *value, void *cb)
914 int ret = check_repo_format(var, value, cb);
917 if (strcmp(var, "core.bare") == 0) {
918 is_bare_repository_cfg = git_config_bool(var, value);
919 if (is_bare_repository_cfg == 1)
920 inside_work_tree = -1;
921 } else if (strcmp(var, "core.worktree") == 0) {
923 return config_error_nonbool(var);
924 free(git_work_tree_cfg);
925 git_work_tree_cfg = xstrdup(value);
926 inside_work_tree = -1;
931 int check_repository_format(void)
933 return check_repository_format_gently(get_git_dir(), NULL);
937 * Returns the "prefix", a path to the current working directory
938 * relative to the work tree root, or NULL, if the current working
939 * directory is not a strict subdirectory of the work tree root. The
940 * prefix always ends with a '/' character.
942 const char *setup_git_directory(void)
944 return setup_git_directory_gently(NULL);
947 const char *resolve_gitdir(const char *suspect)
949 if (is_git_directory(suspect))
951 return read_gitfile(suspect);
954 /* if any standard file descriptor is missing open it to /dev/null */
955 void sanitize_stdfds(void)
957 int fd = open("/dev/null", O_RDWR, 0);
958 while (fd != -1 && fd < 2)
961 die_errno("open /dev/null or dup failed");
968 #ifdef NO_POSIX_GOODIES
976 die_errno("fork failed");
981 die_errno("setsid failed");