Skip to content

Commit 0873c39

Browse files
committed
Merge branch 'nd/remove-ignore-env-field'
Code clean-up for the "repository" abstraction. * nd/remove-ignore-env-field: repository.h: add comment and clarify repo_set_gitdir repository: delete ignore_env member sha1_file.c: move delayed getenv(altdb) back to setup_git_env() repository.c: delete dead functions repository.c: move env-related setup code back to environment.c repository: initialize the_repository in main()
2 parents 62c0fd4 + 00a3da2 commit 0873c39

File tree

7 files changed

+101
-65
lines changed

7 files changed

+101
-65
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ static inline enum object_type object_type(unsigned int mode)
459459
*/
460460
extern const char * const local_repo_env[];
461461

462-
extern void setup_git_env(void);
462+
extern void setup_git_env(const char *git_dir);
463463

464464
/*
465465
* Returns true iff we have a configured git repository (either via

common-main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ int main(int argc, const char **argv)
3434

3535
git_setup_gettext();
3636

37+
initialize_the_repository();
38+
3739
attr_start();
3840

3941
git_extract_argv0_path(argv[0]);

environment.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "refs.h"
1414
#include "fmt-merge-msg.h"
1515
#include "commit.h"
16+
#include "argv-array.h"
1617

1718
int trust_executable_bit = 1;
1819
int trust_ctime = 1;
@@ -147,10 +148,35 @@ static char *expand_namespace(const char *raw_namespace)
147148
return strbuf_detach(&buf, NULL);
148149
}
149150

150-
void setup_git_env(void)
151+
/*
152+
* Wrapper of getenv() that returns a strdup value. This value is kept
153+
* in argv to be freed later.
154+
*/
155+
static const char *getenv_safe(struct argv_array *argv, const char *name)
156+
{
157+
const char *value = getenv(name);
158+
159+
if (!value)
160+
return NULL;
161+
162+
argv_array_push(argv, value);
163+
return argv->argv[argv->argc - 1];
164+
}
165+
166+
void setup_git_env(const char *git_dir)
151167
{
152168
const char *shallow_file;
153169
const char *replace_ref_base;
170+
struct set_gitdir_args args = { NULL };
171+
struct argv_array to_free = ARGV_ARRAY_INIT;
172+
173+
args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
174+
args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
175+
args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
176+
args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
177+
args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
178+
repo_set_gitdir(the_repository, git_dir, &args);
179+
argv_array_clear(&to_free);
154180

155181
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
156182
check_replace_refs = 0;
@@ -300,8 +326,7 @@ int set_git_dir(const char *path)
300326
{
301327
if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
302328
return error("Could not set GIT_DIR to '%s'", path);
303-
repo_set_gitdir(the_repository, path);
304-
setup_git_env();
329+
setup_git_env(path);
305330
return 0;
306331
}
307332

repository.c

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,68 @@
44
#include "submodule-config.h"
55

66
/* The main repository */
7-
static struct repository the_repo = {
8-
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &the_index, &hash_algos[GIT_HASH_SHA1], 0, 0
9-
};
10-
struct repository *the_repository = &the_repo;
7+
static struct repository the_repo;
8+
struct repository *the_repository;
119

12-
static char *git_path_from_env(const char *envvar, const char *git_dir,
13-
const char *path, int fromenv)
10+
void initialize_the_repository(void)
1411
{
15-
if (fromenv) {
16-
const char *value = getenv(envvar);
17-
if (value)
18-
return xstrdup(value);
19-
}
12+
the_repository = &the_repo;
2013

21-
return xstrfmt("%s/%s", git_dir, path);
14+
the_repo.index = &the_index;
15+
repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
2216
}
2317

24-
static int find_common_dir(struct strbuf *sb, const char *gitdir, int fromenv)
18+
static void expand_base_dir(char **out, const char *in,
19+
const char *base_dir, const char *def_in)
2520
{
26-
if (fromenv) {
27-
const char *value = getenv(GIT_COMMON_DIR_ENVIRONMENT);
28-
if (value) {
29-
strbuf_addstr(sb, value);
30-
return 1;
31-
}
32-
}
33-
34-
return get_common_dir_noenv(sb, gitdir);
21+
free(*out);
22+
if (in)
23+
*out = xstrdup(in);
24+
else
25+
*out = xstrfmt("%s/%s", base_dir, def_in);
3526
}
3627

37-
static void repo_setup_env(struct repository *repo)
28+
static void repo_set_commondir(struct repository *repo,
29+
const char *commondir)
3830
{
3931
struct strbuf sb = STRBUF_INIT;
4032

41-
repo->different_commondir = find_common_dir(&sb, repo->gitdir,
42-
!repo->ignore_env);
4333
free(repo->commondir);
34+
35+
if (commondir) {
36+
repo->different_commondir = 1;
37+
repo->commondir = xstrdup(commondir);
38+
return;
39+
}
40+
41+
repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
4442
repo->commondir = strbuf_detach(&sb, NULL);
45-
free(repo->objectdir);
46-
repo->objectdir = git_path_from_env(DB_ENVIRONMENT, repo->commondir,
47-
"objects", !repo->ignore_env);
48-
free(repo->graft_file);
49-
repo->graft_file = git_path_from_env(GRAFT_ENVIRONMENT, repo->commondir,
50-
"info/grafts", !repo->ignore_env);
51-
free(repo->index_file);
52-
repo->index_file = git_path_from_env(INDEX_ENVIRONMENT, repo->gitdir,
53-
"index", !repo->ignore_env);
5443
}
5544

56-
void repo_set_gitdir(struct repository *repo, const char *path)
45+
void repo_set_gitdir(struct repository *repo,
46+
const char *root,
47+
const struct set_gitdir_args *o)
5748
{
58-
const char *gitfile = read_gitfile(path);
49+
const char *gitfile = read_gitfile(root);
50+
/*
51+
* repo->gitdir is saved because the caller could pass "root"
52+
* that also points to repo->gitdir. We want to keep it alive
53+
* until after xstrdup(root). Then we can free it.
54+
*/
5955
char *old_gitdir = repo->gitdir;
6056

61-
repo->gitdir = xstrdup(gitfile ? gitfile : path);
62-
repo_setup_env(repo);
63-
57+
repo->gitdir = xstrdup(gitfile ? gitfile : root);
6458
free(old_gitdir);
59+
60+
repo_set_commondir(repo, o->commondir);
61+
expand_base_dir(&repo->objectdir, o->object_dir,
62+
repo->commondir, "objects");
63+
free(repo->alternate_db);
64+
repo->alternate_db = xstrdup_or_null(o->alternate_db);
65+
expand_base_dir(&repo->graft_file, o->graft_file,
66+
repo->commondir, "info/grafts");
67+
expand_base_dir(&repo->index_file, o->index_file,
68+
repo->gitdir, "index");
6569
}
6670

6771
void repo_set_hash_algo(struct repository *repo, int hash_algo)
@@ -79,6 +83,7 @@ static int repo_init_gitdir(struct repository *repo, const char *gitdir)
7983
int error = 0;
8084
char *abspath = NULL;
8185
const char *resolved_gitdir;
86+
struct set_gitdir_args args = { NULL };
8287

8388
abspath = real_pathdup(gitdir, 0);
8489
if (!abspath) {
@@ -93,7 +98,7 @@ static int repo_init_gitdir(struct repository *repo, const char *gitdir)
9398
goto out;
9499
}
95100

96-
repo_set_gitdir(repo, resolved_gitdir);
101+
repo_set_gitdir(repo, resolved_gitdir, &args);
97102

98103
out:
99104
free(abspath);
@@ -128,13 +133,13 @@ static int read_and_verify_repository_format(struct repository_format *format,
128133
* Initialize 'repo' based on the provided 'gitdir'.
129134
* Return 0 upon success and a non-zero value upon failure.
130135
*/
131-
int repo_init(struct repository *repo, const char *gitdir, const char *worktree)
136+
static int repo_init(struct repository *repo,
137+
const char *gitdir,
138+
const char *worktree)
132139
{
133140
struct repository_format format;
134141
memset(repo, 0, sizeof(*repo));
135142

136-
repo->ignore_env = 1;
137-
138143
if (repo_init_gitdir(repo, gitdir))
139144
goto error;
140145

@@ -210,6 +215,7 @@ void repo_clear(struct repository *repo)
210215
FREE_AND_NULL(repo->gitdir);
211216
FREE_AND_NULL(repo->commondir);
212217
FREE_AND_NULL(repo->objectdir);
218+
FREE_AND_NULL(repo->alternate_db);
213219
FREE_AND_NULL(repo->graft_file);
214220
FREE_AND_NULL(repo->index_file);
215221
FREE_AND_NULL(repo->worktree);

repository.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct repository {
2626
*/
2727
char *objectdir;
2828

29+
/* Path to extra alternate object database if not NULL */
30+
char *alternate_db;
31+
2932
/*
3033
* Path to the repository's graft file.
3134
* Cannot be NULL after initialization.
@@ -72,26 +75,31 @@ struct repository {
7275
const struct git_hash_algo *hash_algo;
7376

7477
/* Configurations */
75-
/*
76-
* Bit used during initialization to indicate if repository state (like
77-
* the location of the 'objectdir') should be read from the
78-
* environment. By default this bit will be set at the begining of
79-
* 'repo_init()' so that all repositories will ignore the environment.
80-
* The exception to this is 'the_repository', which doesn't go through
81-
* the normal 'repo_init()' process.
82-
*/
83-
unsigned ignore_env:1;
8478

8579
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
8680
unsigned different_commondir:1;
8781
};
8882

8983
extern struct repository *the_repository;
9084

91-
extern void repo_set_gitdir(struct repository *repo, const char *path);
85+
/*
86+
* Define a custom repository layout. Any field can be NULL, which
87+
* will default back to the path according to the default layout.
88+
*/
89+
struct set_gitdir_args {
90+
const char *commondir;
91+
const char *object_dir;
92+
const char *graft_file;
93+
const char *index_file;
94+
const char *alternate_db;
95+
};
96+
97+
extern void repo_set_gitdir(struct repository *repo,
98+
const char *root,
99+
const struct set_gitdir_args *extra_args);
92100
extern void repo_set_worktree(struct repository *repo, const char *path);
93101
extern void repo_set_hash_algo(struct repository *repo, int algo);
94-
extern int repo_init(struct repository *repo, const char *gitdir, const char *worktree);
102+
extern void initialize_the_repository(void);
95103
extern int repo_submodule_init(struct repository *submodule,
96104
struct repository *superproject,
97105
const char *path);

setup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
11161116
const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
11171117
if (!gitdir)
11181118
gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
1119-
repo_set_gitdir(the_repository, gitdir);
1120-
setup_git_env();
1119+
setup_git_env(gitdir);
11211120
}
11221121
if (startup_info->have_repository)
11231122
repo_set_hash_algo(the_repository, repo_fmt.hash_algo);

sha1_file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,15 +668,11 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
668668

669669
void prepare_alt_odb(void)
670670
{
671-
const char *alt;
672-
673671
if (alt_odb_tail)
674672
return;
675673

676-
alt = getenv(ALTERNATE_DB_ENVIRONMENT);
677-
678674
alt_odb_tail = &alt_odb_list;
679-
link_alt_odb_entries(alt, PATH_SEP, NULL, 0);
675+
link_alt_odb_entries(the_repository->alternate_db, PATH_SEP, NULL, 0);
680676

681677
read_info_alternates(get_object_directory(), 0);
682678
}

0 commit comments

Comments
 (0)