* are.
  */
 #include "cache.h"
+#include "repository.h"
 #include "config.h"
 #include "refs.h"
 #include "fmt-merge-msg.h"
 
 static const char *super_prefix;
 
-static const char *git_dir, *git_common_dir;
-static char *git_object_dir, *git_index_file, *git_graft_file;
-int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
-
 /*
  * Repository-local GIT_* environment variables; see cache.h for details.
  */
        return strbuf_detach(&buf, NULL);
 }
 
-static char *git_path_from_env(const char *envvar, const char *git_dir,
-                              const char *path, int *fromenv)
-{
-       const char *value = getenv(envvar);
-       if (!value)
-               return xstrfmt("%s/%s", git_dir, path);
-       if (fromenv)
-               *fromenv = 1;
-       return xstrdup(value);
-}
-
 void setup_git_env(void)
 {
-       struct strbuf sb = STRBUF_INIT;
-       const char *gitfile;
        const char *shallow_file;
        const char *replace_ref_base;
 
-       git_dir = getenv(GIT_DIR_ENVIRONMENT);
-       if (!git_dir) {
-               if (!startup_info->have_repository)
-                       BUG("setup_git_env called without repository");
-               git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
-       }
-       gitfile = read_gitfile(git_dir);
-       git_dir = xstrdup(gitfile ? gitfile : git_dir);
-       if (get_common_dir(&sb, git_dir))
-               git_common_dir_env = 1;
-       git_common_dir = strbuf_detach(&sb, NULL);
-       git_object_dir = git_path_from_env(DB_ENVIRONMENT, git_common_dir,
-                                          "objects", &git_db_env);
-       git_index_file = git_path_from_env(INDEX_ENVIRONMENT, git_dir,
-                                          "index", &git_index_env);
-       git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, git_common_dir,
-                                          "info/grafts", &git_graft_env);
        if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
                check_replace_refs = 0;
        replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
 int have_git_dir(void)
 {
        return startup_info->have_repository
-               || git_dir;
+               || the_repository->gitdir;
 }
 
 const char *get_git_dir(void)
 {
-       if (!git_dir)
+       if (!the_repository->gitdir)
                BUG("git environment hasn't been setup");
-       return git_dir;
+       return the_repository->gitdir;
 }
 
 const char *get_git_common_dir(void)
 {
-       if (!git_dir)
+       if (!the_repository->commondir)
                BUG("git environment hasn't been setup");
-       return git_common_dir;
+       return the_repository->commondir;
 }
 
 const char *get_git_namespace(void)
 
 char *get_object_directory(void)
 {
-       if (!git_object_dir)
+       if (!the_repository->objectdir)
                BUG("git environment hasn't been setup");
-       return git_object_dir;
+       return the_repository->objectdir;
 }
 
 int odb_mkstemp(struct strbuf *template, const char *pattern)
 
 char *get_index_file(void)
 {
-       if (!git_index_file)
+       if (!the_repository->index_file)
                BUG("git environment hasn't been setup");
-       return git_index_file;
+       return the_repository->index_file;
 }
 
 char *get_graft_file(void)
 {
-       if (!git_graft_file)
+       if (!the_repository->graft_file)
                BUG("git environment hasn't been setup");
-       return git_graft_file;
+       return the_repository->graft_file;
 }
 
 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();
        return 0;
 }
 
  * Utilities for paths and pathnames
  */
 #include "cache.h"
+#include "repository.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "dir.h"
        const struct common_dir *p;
        int len;
 
-       if (!git_common_dir_env)
+       if (!the_repository->different_commondir)
                return;
        strbuf_addf(&sb, "%s/", get_git_dir());
        len = sb.len;
 static void adjust_git_path(struct strbuf *buf, int git_dir_len)
 {
        const char *base = buf->buf + git_dir_len;
-       if (git_graft_env && is_dir_file(base, "info", "grafts"))
+       if (is_dir_file(base, "info", "grafts"))
                strbuf_splice(buf, 0, buf->len,
                              get_graft_file(), strlen(get_graft_file()));
-       else if (git_index_env && !strcmp(base, "index"))
+       else if (!strcmp(base, "index"))
                strbuf_splice(buf, 0, buf->len,
                              get_index_file(), strlen(get_index_file()));
-       else if (git_db_env && dir_prefix(base, "objects"))
+       else if (dir_prefix(base, "objects"))
                replace_dir(buf, git_dir_len + 7, get_object_directory());
        else if (git_hooks_path && dir_prefix(base, "hooks"))
                replace_dir(buf, git_dir_len + 5, git_hooks_path);
-       else if (git_common_dir_env)
+       else if (the_repository->different_commondir)
                update_common_dir(buf, git_dir_len, NULL);
 }
 
 
 #include "cache.h"
+#include "repository.h"
 #include "config.h"
 #include "dir.h"
 #include "string-list.h"
        if (getenv(GIT_WORK_TREE_ENVIRONMENT))
                setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
 
+       /*
+        * NEEDSWORK: this call can essentially be set_git_dir(get_git_dir())
+        * which can cause some problems when trying to free the old value of
+        * gitdir.
+        */
        set_git_dir(remove_leading_path(git_dir, work_tree));
        initialized = 1;
 }
         * the user has set GIT_DIR.  It may be beneficial to disallow bogus
         * GIT_DIR values at some point in the future.
         */
-       if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT))
-               setup_git_env();
+       if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT)) {
+               if (!the_repository->gitdir) {
+                       const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
+                       if (!gitdir)
+                               gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
+                       repo_set_gitdir(the_repository, gitdir);
+                       setup_git_env();
+               }
+       }
 
        strbuf_release(&dir);
        strbuf_release(&gitdir);