Skip to content

Commit e805dc1

Browse files
committed
Merge branch 'ed/simplify-setup-git-dir'
Code simplification. * ed/simplify-setup-git-dir: Simplify handling of setup_git_directory_gently() failure cases.
2 parents b84e297 + 07098b8 commit e805dc1

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

setup.c

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,6 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
831831
return NULL;
832832
}
833833

834-
static const char *setup_nongit(const char *cwd, int *nongit_ok)
835-
{
836-
if (!nongit_ok)
837-
die(_("not a git repository (or any of the parent directories): %s"), DEFAULT_GIT_DIR_ENVIRONMENT);
838-
if (chdir(cwd))
839-
die_errno(_("cannot come back to cwd"));
840-
*nongit_ok = 1;
841-
return NULL;
842-
}
843-
844834
static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len)
845835
{
846836
struct stat buf;
@@ -1054,7 +1044,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
10541044
{
10551045
static struct strbuf cwd = STRBUF_INIT;
10561046
struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
1057-
const char *prefix;
1047+
const char *prefix = NULL;
10581048
struct repository_format repo_fmt;
10591049

10601050
/*
@@ -1079,9 +1069,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
10791069
strbuf_addbuf(&dir, &cwd);
10801070

10811071
switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
1082-
case GIT_DIR_NONE:
1083-
prefix = NULL;
1084-
break;
10851072
case GIT_DIR_EXPLICIT:
10861073
prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
10871074
break;
@@ -1097,29 +1084,51 @@ const char *setup_git_directory_gently(int *nongit_ok)
10971084
prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok);
10981085
break;
10991086
case GIT_DIR_HIT_CEILING:
1100-
prefix = setup_nongit(cwd.buf, nongit_ok);
1087+
if (!nongit_ok)
1088+
die(_("not a git repository (or any of the parent directories): %s"),
1089+
DEFAULT_GIT_DIR_ENVIRONMENT);
1090+
*nongit_ok = 1;
11011091
break;
11021092
case GIT_DIR_HIT_MOUNT_POINT:
1103-
if (nongit_ok) {
1104-
*nongit_ok = 1;
1105-
strbuf_release(&cwd);
1106-
strbuf_release(&dir);
1107-
return NULL;
1108-
}
1109-
die(_("not a git repository (or any parent up to mount point %s)\n"
1110-
"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
1111-
dir.buf);
1093+
if (!nongit_ok)
1094+
die(_("not a git repository (or any parent up to mount point %s)\n"
1095+
"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
1096+
dir.buf);
1097+
*nongit_ok = 1;
1098+
break;
1099+
case GIT_DIR_NONE:
1100+
/*
1101+
* As a safeguard against setup_git_directory_gently_1 returning
1102+
* this value, fallthrough to BUG. Otherwise it is possible to
1103+
* set startup_info->have_repository to 1 when we did nothing to
1104+
* find a repository.
1105+
*/
11121106
default:
11131107
BUG("unhandled setup_git_directory_1() result");
11141108
}
11151109

1116-
if (prefix)
1117-
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
1118-
else
1110+
/*
1111+
* At this point, nongit_ok is stable. If it is non-NULL and points
1112+
* to a non-zero value, then this means that we haven't found a
1113+
* repository and that the caller expects startup_info to reflect
1114+
* this.
1115+
*
1116+
* Regardless of the state of nongit_ok, startup_info->prefix and
1117+
* the GIT_PREFIX environment variable must always match. For details
1118+
* see Documentation/config/alias.txt.
1119+
*/
1120+
if (nongit_ok && *nongit_ok) {
1121+
startup_info->have_repository = 0;
1122+
startup_info->prefix = NULL;
11191123
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
1120-
1121-
startup_info->have_repository = !nongit_ok || !*nongit_ok;
1122-
startup_info->prefix = prefix;
1124+
} else {
1125+
startup_info->have_repository = 1;
1126+
startup_info->prefix = prefix;
1127+
if (prefix)
1128+
setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
1129+
else
1130+
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
1131+
}
11231132

11241133
/*
11251134
* Not all paths through the setup code will call 'set_git_dir()' (which
@@ -1132,7 +1141,10 @@ const char *setup_git_directory_gently(int *nongit_ok)
11321141
* the user has set GIT_DIR. It may be beneficial to disallow bogus
11331142
* GIT_DIR values at some point in the future.
11341143
*/
1135-
if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT)) {
1144+
if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */
1145+
startup_info->have_repository ||
1146+
/* GIT_DIR_EXPLICIT */
1147+
getenv(GIT_DIR_ENVIRONMENT)) {
11361148
if (!the_repository->gitdir) {
11371149
const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
11381150
if (!gitdir)

0 commit comments

Comments
 (0)