Skip to content

Commit cb6c38d

Browse files
peffgitster
authored andcommitted
setup_git_env(): introduce git_path_from_env() helper
"Check the value of an environment and fall back to a known path inside $GIT_DIR" is repeated a few times to determine the location of the data store, the index and the graft file, but the return value of getenv is not guaranteed to survive across further invocations of setenv or even getenv. Make sure to xstrdup() the value we receive from getenv(3), and encapsulate the pattern into a helper function. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 45bc131 commit cb6c38d

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

environment.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ static char *expand_namespace(const char *raw_namespace)
124124
return strbuf_detach(&buf, NULL);
125125
}
126126

127+
static char *git_path_from_env(const char *envvar, const char *path)
128+
{
129+
const char *value = getenv(envvar);
130+
return value ? xstrdup(value) : git_pathdup("%s", path);
131+
}
132+
127133
static void setup_git_env(void)
128134
{
129135
const char *gitfile;
@@ -134,15 +140,9 @@ static void setup_git_env(void)
134140
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
135141
gitfile = read_gitfile(git_dir);
136142
git_dir = xstrdup(gitfile ? gitfile : git_dir);
137-
git_object_dir = getenv(DB_ENVIRONMENT);
138-
if (!git_object_dir)
139-
git_object_dir = git_pathdup("objects");
140-
git_index_file = getenv(INDEX_ENVIRONMENT);
141-
if (!git_index_file)
142-
git_index_file = git_pathdup("index");
143-
git_graft_file = getenv(GRAFT_ENVIRONMENT);
144-
if (!git_graft_file)
145-
git_graft_file = git_pathdup("info/grafts");
143+
git_object_dir = git_path_from_env(DB_ENVIRONMENT, "objects");
144+
git_index_file = git_path_from_env(INDEX_ENVIRONMENT, "index");
145+
git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, "info/grafts");
146146
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
147147
check_replace_refs = 0;
148148
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));

0 commit comments

Comments
 (0)