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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 24, 2014 at 16:58 UTC
cb6c38d5cce7d8d48a57346b332a68cea1489df1
1 file changed
+9
-9
environment.c
+9
-9
@@ -124,6 +124,12 @@ static char *expand_namespace(const char *raw_namespace)
124
return strbuf_detach(&buf, NULL);
125
}
126
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
+
133
static void setup_git_env(void)
134
{
135
const char *gitfile;
@@ -134,15 +140,9 @@ static void setup_git_env(void)
140
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
141
gitfile = read_gitfile(git_dir);
142
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");
146
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
147
check_replace_refs = 0;
148
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));