summaryrefslogtreecommitdiff
path: root/environment.c
diff options
context:
space:
mode:
Diffstat (limited to 'environment.c')
-rw-r--r--environment.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/environment.c b/environment.c
index d6dd64662c..21565c3c52 100644
--- a/environment.c
+++ b/environment.c
@@ -13,6 +13,7 @@
#include "refs.h"
#include "fmt-merge-msg.h"
#include "commit.h"
+#include "argv-array.h"
int trust_executable_bit = 1;
int trust_ctime = 1;
@@ -147,10 +148,35 @@ static char *expand_namespace(const char *raw_namespace)
return strbuf_detach(&buf, NULL);
}
-void setup_git_env(void)
+/*
+ * Wrapper of getenv() that returns a strdup value. This value is kept
+ * in argv to be freed later.
+ */
+static const char *getenv_safe(struct argv_array *argv, const char *name)
+{
+ const char *value = getenv(name);
+
+ if (!value)
+ return NULL;
+
+ argv_array_push(argv, value);
+ return argv->argv[argv->argc - 1];
+}
+
+void setup_git_env(const char *git_dir)
{
const char *shallow_file;
const char *replace_ref_base;
+ struct set_gitdir_args args = { NULL };
+ struct argv_array to_free = ARGV_ARRAY_INIT;
+
+ args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
+ args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
+ args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
+ args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
+ args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
+ repo_set_gitdir(the_repository, git_dir, &args);
+ argv_array_clear(&to_free);
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
check_replace_refs = 0;
@@ -300,8 +326,7 @@ int set_git_dir(const char *path)
{
if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
return error("Could not set GIT_DIR to '%s'", path);
- repo_set_gitdir(the_repository, path);
- setup_git_env();
+ setup_git_env(path);
return 0;
}