Fix a small memory leak in builtin-add
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 06004f15874fbcbab50eafd272c5898a34fcdcfe..145eca50f41d811c4c8fcb21ed2604e6b2971aba 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -227,9 +227,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
                if (PATH_MAX - 40 < strlen(gitdirenv))
                        die("'$%s' too big", GIT_DIR_ENVIRONMENT);
                if (is_git_directory(gitdirenv)) {
+                       static char buffer[1024 + 1];
+                       const char *retval;
+
                        if (!work_tree_env)
                                return set_work_tree(gitdirenv);
-                       return NULL;
+                       retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
+                                       get_git_work_tree());
+                       if (!retval || !*retval)
+                               return NULL;
+                       set_git_dir(make_absolute_path(gitdirenv));
+                       if (chdir(work_tree_env) < 0)
+                               die ("Could not chdir to %s", work_tree_env);
+                       strcat(buffer, "/");
+                       return retval;
                }
                if (nongit_ok) {
                        *nongit_ok = 1;