environment: place key repository state in the_repository
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>
Brandon Williams committed
Jun 22, 2017 at 11:43 UTC
c14c234f22e0656a61f5718baf155118e6e609c9
4 files changed
+34
-53
cache.h
-1
@@ -771,7 +771,6 @@ extern int core_apply_sparse_checkout;
771
extern int precomposed_unicode;
772
extern int protect_hfs;
773
extern int protect_ntfs;
774
-extern int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
774
775
/*
776
* Include broken refs in all ref iterations, which will
environment.c
+13
-45
@@ -8,6 +8,7 @@
8
* are.
9
*/
10
#include "cache.h"
11
+#include "repository.h"
12
#include "config.h"
13
#include "refs.h"
14
#include "fmt-merge-msg.h"
@@ -101,10 +102,6 @@ static const char *namespace;
102
103
static const char *super_prefix;
104
104
-static const char *git_dir, *git_common_dir;
105
-static char *git_object_dir, *git_index_file, *git_graft_file;
106
-int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
107
-
105
/*
106
* Repository-local GIT_* environment variables; see cache.h for details.
107
*/
@@ -148,41 +145,11 @@ static char *expand_namespace(const char *raw_namespace)
145
return strbuf_detach(&buf, NULL);
146
}
147
151
-static char *git_path_from_env(const char *envvar, const char *git_dir,
152
- const char *path, int *fromenv)
153
-{
154
- const char *value = getenv(envvar);
155
- if (!value)
156
- return xstrfmt("%s/%s", git_dir, path);
157
- if (fromenv)
158
- *fromenv = 1;
159
- return xstrdup(value);
160
-}
161
-
148
void setup_git_env(void)
149
{
164
- struct strbuf sb = STRBUF_INIT;
165
- const char *gitfile;
150
const char *shallow_file;
151
const char *replace_ref_base;
152
169
- git_dir = getenv(GIT_DIR_ENVIRONMENT);
170
- if (!git_dir) {
171
- if (!startup_info->have_repository)
172
- BUG("setup_git_env called without repository");
173
- git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
174
- }
175
- gitfile = read_gitfile(git_dir);
176
- git_dir = xstrdup(gitfile ? gitfile : git_dir);
177
- if (get_common_dir(&sb, git_dir))
178
- git_common_dir_env = 1;
179
- git_common_dir = strbuf_detach(&sb, NULL);
180
- git_object_dir = git_path_from_env(DB_ENVIRONMENT, git_common_dir,
181
- "objects", &git_db_env);
182
- git_index_file = git_path_from_env(INDEX_ENVIRONMENT, git_dir,
183
- "index", &git_index_env);
184
- git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, git_common_dir,
185
- "info/grafts", &git_graft_env);
153
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
154
check_replace_refs = 0;
155
replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
@@ -203,21 +170,21 @@ int is_bare_repository(void)
170
int have_git_dir(void)
171
{
172
return startup_info->have_repository
206
- || git_dir;
173
+ || the_repository->gitdir;
174
}
175
176
const char *get_git_dir(void)
177
{
211
- if (!git_dir)
178
+ if (!the_repository->gitdir)
179
BUG("git environment hasn't been setup");
213
- return git_dir;
180
+ return the_repository->gitdir;
181
}
182
183
const char *get_git_common_dir(void)
184
{
218
- if (!git_dir)
185
+ if (!the_repository->commondir)
186
BUG("git environment hasn't been setup");
220
- return git_common_dir;
187
+ return the_repository->commondir;
188
}
189
190
const char *get_git_namespace(void)
@@ -273,9 +240,9 @@ const char *get_git_work_tree(void)
240
241
char *get_object_directory(void)
242
{
276
- if (!git_object_dir)
243
+ if (!the_repository->objectdir)
244
BUG("git environment hasn't been setup");
278
- return git_object_dir;
245
+ return the_repository->objectdir;
246
}
247
248
int odb_mkstemp(struct strbuf *template, const char *pattern)
@@ -313,22 +280,23 @@ int odb_pack_keep(const char *name)
280
281
char *get_index_file(void)
282
{
316
- if (!git_index_file)
283
+ if (!the_repository->index_file)
284
BUG("git environment hasn't been setup");
318
- return git_index_file;
285
+ return the_repository->index_file;
286
}
287
288
char *get_graft_file(void)
289
{
323
- if (!git_graft_file)
290
+ if (!the_repository->graft_file)
291
BUG("git environment hasn't been setup");
325
- return git_graft_file;
292
+ return the_repository->graft_file;
293
}
294
295
int set_git_dir(const char *path)
296
{
297
if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
298
return error("Could not set GIT_DIR to '%s'", path);
299
+ repo_set_gitdir(the_repository, path);
300
setup_git_env();
301
return 0;
302
}
path.c
+6
-5
@@ -2,6 +2,7 @@
2
* Utilities for paths and pathnames
3
*/
4
#include "cache.h"
5
+#include "repository.h"
6
#include "strbuf.h"
7
#include "string-list.h"
8
#include "dir.h"
@@ -355,7 +356,7 @@ void report_linked_checkout_garbage(void)
356
const struct common_dir *p;
357
int len;
358
358
- if (!git_common_dir_env)
359
+ if (!the_repository->different_commondir)
360
return;
361
strbuf_addf(&sb, "%s/", get_git_dir());
362
len = sb.len;
@@ -374,17 +375,17 @@ void report_linked_checkout_garbage(void)
375
static void adjust_git_path(struct strbuf *buf, int git_dir_len)
376
{
377
const char *base = buf->buf + git_dir_len;
377
- if (git_graft_env && is_dir_file(base, "info", "grafts"))
378
+ if (is_dir_file(base, "info", "grafts"))
379
strbuf_splice(buf, 0, buf->len,
380
get_graft_file(), strlen(get_graft_file()));
380
- else if (git_index_env && !strcmp(base, "index"))
381
+ else if (!strcmp(base, "index"))
382
strbuf_splice(buf, 0, buf->len,
383
get_index_file(), strlen(get_index_file()));
383
- else if (git_db_env && dir_prefix(base, "objects"))
384
+ else if (dir_prefix(base, "objects"))
385
replace_dir(buf, git_dir_len + 7, get_object_directory());
386
else if (git_hooks_path && dir_prefix(base, "hooks"))
387
replace_dir(buf, git_dir_len + 5, git_hooks_path);
387
- else if (git_common_dir_env)
388
+ else if (the_repository->different_commondir)
389
update_common_dir(buf, git_dir_len, NULL);
390
}
391
setup.c
+15
-2
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include "repository.h"
3
#include "config.h"
4
#include "dir.h"
5
#include "string-list.h"
@@ -398,6 +399,11 @@ void setup_work_tree(void)
399
if (getenv(GIT_WORK_TREE_ENVIRONMENT))
400
setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
401
402
+ /*
403
+ * NEEDSWORK: this call can essentially be set_git_dir(get_git_dir())
404
+ * which can cause some problems when trying to free the old value of
405
+ * gitdir.
406
+ */
407
set_git_dir(remove_leading_path(git_dir, work_tree));
408
initialized = 1;
409
}
@@ -1108,8 +1114,15 @@ const char *setup_git_directory_gently(int *nongit_ok)
1114
* the user has set GIT_DIR. It may be beneficial to disallow bogus
1115
* GIT_DIR values at some point in the future.
1116
*/
1111
- if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT))
1112
- setup_git_env();
1117
+ if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT)) {
1118
+ if (!the_repository->gitdir) {
1119
+ const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
1120
+ if (!gitdir)
1121
+ gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
1122
+ repo_set_gitdir(the_repository, gitdir);
1123
+ setup_git_env();
1124
+ }
1125
+ }
1126
1127
strbuf_release(&dir);
1128
strbuf_release(&gitdir);