environment: place key repository state in the_repository
authorBrandon Williams <bmwill@google.com>
Thu, 22 Jun 2017 18:43:33 +0000 (11:43 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 24 Jun 2017 01:24:34 +0000 (18:24 -0700)
Migrate 'git_dir', 'git_common_dir', 'git_object_dir', 'git_index_file',
'git_graft_file', and 'namespace' to be stored in 'the_repository'.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
environment.c
path.c
setup.c
diff --git a/cache.h b/cache.h
index 7c81749a9800062c0f3d812060968151a7e42951..cd64cbc81f7dfacf7621a08889a39dccca9dc331 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -771,7 +771,6 @@ extern int core_apply_sparse_checkout;
 extern int precomposed_unicode;
 extern int protect_hfs;
 extern int protect_ntfs;
-extern int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
 
 /*
  * Include broken refs in all ref iterations, which will
index e035f6372efd47a7fe70f8527377e33d24444418..aa79ef83e469435be26a2838f61c179e93c77a47 100644 (file)
@@ -8,6 +8,7 @@
  * are.
  */
 #include "cache.h"
+#include "repository.h"
 #include "config.h"
 #include "refs.h"
 #include "fmt-merge-msg.h"
@@ -101,10 +102,6 @@ static const char *namespace;
 
 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.
  */
@@ -148,41 +145,11 @@ static char *expand_namespace(const char *raw_namespace)
        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);
@@ -203,21 +170,21 @@ int is_bare_repository(void)
 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)
@@ -273,9 +240,9 @@ const char *get_git_work_tree(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)
@@ -313,22 +280,23 @@ int odb_pack_keep(const char *name)
 
 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;
 }
diff --git a/path.c b/path.c
index c1cb1cf6273f4a5a7ed91d495c55a7d283497460..e4abea0830fb5279657036c10455963e22b156a1 100644 (file)
--- a/path.c
+++ b/path.c
@@ -2,6 +2,7 @@
  * Utilities for paths and pathnames
  */
 #include "cache.h"
+#include "repository.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "dir.h"
@@ -355,7 +356,7 @@ void report_linked_checkout_garbage(void)
        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;
@@ -374,17 +375,17 @@ void report_linked_checkout_garbage(void)
 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);
 }
 
diff --git a/setup.c b/setup.c
index b477faa44243d71f24a912615e906c479e6f51b1..860507e1fdb2d61da71b27eb00d8413f2fabcfa9 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "repository.h"
 #include "config.h"
 #include "dir.h"
 #include "string-list.h"
@@ -398,6 +399,11 @@ void setup_work_tree(void)
        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;
 }
@@ -1108,8 +1114,15 @@ const char *setup_git_directory_gently(int *nongit_ok)
         * 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);