Skip to content

Commit 8c9a952

Browse files
john-caigitster
authored andcommitted
git: remove is_bare_repository_cfg global variable
The is_bare_repository_cfg global variable is used for storing a bare repository setting, either through the config, an env var, or the commandline. This variable is global, and hence introduces global state everywhere it is used. In order to reduce global state, add a member to the repository struct to keep track of the setting there. For now, the_repository is what's used to set the member, which still represents global state. However, there is a parallel effort to replace calls to the_repository with a repository struct that is passed into builtins, see [1]. Hence, this change will help the overall effort in reducing global state. 1. 9b1cb50 (builtin: add a repository parameter for builtin functions, Fri Sep 13 21:16:14 2024 +0000) Signed-off-by: John Cai <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8f8d6ee commit 8c9a952

28 files changed

+70
-55
lines changed

attr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ static enum git_attr_direction direction;
716716

717717
void git_attr_set_direction(enum git_attr_direction new_direction)
718718
{
719-
if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
719+
if (repo_is_bare(the_repository) && new_direction != GIT_ATTR_INDEX)
720720
BUG("non-INDEX attr direction in a bare repo");
721721

722722
if (new_direction != direction)
@@ -883,7 +883,7 @@ static struct attr_stack *read_attr(struct index_state *istate,
883883
res = read_attr_from_index(istate, path, flags);
884884
} else if (tree_oid) {
885885
res = read_attr_from_blob(istate, tree_oid, path, flags);
886-
} else if (!is_bare_repository()) {
886+
} else if (!repo_is_bare(the_repository)) {
887887
if (direction == GIT_ATTR_CHECKOUT) {
888888
res = read_attr_from_index(istate, path, flags);
889889
if (!res)

builtin/bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc,
705705
struct object_id oid;
706706
const char *head;
707707

708-
if (is_bare_repository())
708+
if (repo_is_bare(the_repository))
709709
no_checkout = 1;
710710

711711
/*

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ int cmd_blame(int argc,
10921092

10931093
revs.disable_stdin = 1;
10941094
setup_revisions(argc, argv, &revs, NULL);
1095-
if (!revs.pending.nr && is_bare_repository()) {
1095+
if (!revs.pending.nr && repo_is_bare(the_repository)) {
10961096
struct commit *head_commit;
10971097
struct object_id head_oid;
10981098

builtin/check-attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int cmd_check_attr(int argc,
116116
struct object_id initialized_oid;
117117
int cnt, i, doubledash, filei;
118118

119-
if (!is_bare_repository())
119+
if (!repo_is_bare(the_repository))
120120
setup_work_tree();
121121

122122
git_config(git_default_config, NULL);

builtin/clone.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ int cmd_clone(int argc,
14151415
repo_clear(the_repository);
14161416

14171417
/* At this point, we need the_repository to match the cloned repo. */
1418-
if (repo_init(the_repository, git_dir, work_tree))
1418+
if (repo_init(the_repository, git_dir, work_tree, -1))
14191419
warning(_("failed to initialize the repo, skipping bundle URI"));
14201420
else if (fetch_bundle_uri(the_repository, bundle_uri, &has_heuristic))
14211421
warning(_("failed to fetch objects from bundle URI '%s'"),
@@ -1446,7 +1446,7 @@ int cmd_clone(int argc,
14461446
repo_clear(the_repository);
14471447

14481448
/* At this point, we need the_repository to match the cloned repo. */
1449-
if (repo_init(the_repository, git_dir, work_tree))
1449+
if (repo_init(the_repository, git_dir, work_tree, -1))
14501450
warning(_("failed to initialize the repo, skipping bundle URI"));
14511451
else if (fetch_bundle_list(the_repository,
14521452
transport->bundles))

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ struct repository *repo UNUSED)
712712
die(_("failed to parse gc.logExpiry value %s"), cfg.gc_log_expire);
713713

714714
if (cfg.pack_refs < 0)
715-
cfg.pack_refs = !is_bare_repository();
715+
cfg.pack_refs = !repo_is_bare(the_repository);
716716

717717
argc = parse_options(argc, argv, prefix, builtin_gc_options,
718718
builtin_gc_usage, 0);

builtin/init-db.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ int cmd_init_db(int argc,
8989
const struct option init_db_options[] = {
9090
OPT_STRING(0, "template", &template_dir, N_("template-directory"),
9191
N_("directory from which templates will be used")),
92-
OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
92+
OPT_SET_INT(0, "bare", &the_repository->is_bare_cfg,
9393
N_("create a bare repository"), 1),
9494
{ OPTION_CALLBACK, 0, "shared", &init_shared_repository,
9595
N_("permissions"),
@@ -109,7 +109,7 @@ int cmd_init_db(int argc,
109109

110110
argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
111111

112-
if (real_git_dir && is_bare_repository_cfg == 1)
112+
if (real_git_dir && the_repository->is_bare_cfg == 1)
113113
die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare");
114114

115115
if (real_git_dir && !is_absolute_path(real_git_dir))
@@ -155,7 +155,7 @@ int cmd_init_db(int argc,
155155
} else if (0 < argc) {
156156
usage(init_db_usage[0]);
157157
}
158-
if (is_bare_repository_cfg == 1) {
158+
if (the_repository->is_bare_cfg == 1) {
159159
char *cwd = xgetcwd();
160160
setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
161161
free(cwd);
@@ -182,7 +182,7 @@ int cmd_init_db(int argc,
182182
*/
183183
git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
184184
work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
185-
if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
185+
if ((!git_dir || the_repository->is_bare_cfg == 1) && work_tree)
186186
die(_("%s (or --work-tree=<directory>) not allowed without "
187187
"specifying %s (or --git-dir=<directory>)"),
188188
GIT_WORK_TREE_ENVIRONMENT,
@@ -218,10 +218,10 @@ int cmd_init_db(int argc,
218218
strbuf_release(&sb);
219219
}
220220

221-
if (is_bare_repository_cfg < 0)
222-
is_bare_repository_cfg = guess_repository_type(git_dir);
221+
if (the_repository->is_bare_cfg < 0)
222+
the_repository->is_bare_cfg = guess_repository_type(git_dir);
223223

224-
if (!is_bare_repository_cfg) {
224+
if (!the_repository->is_bare_cfg) {
225225
const char *git_dir_parent = strrchr(git_dir, '/');
226226
if (git_dir_parent) {
227227
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ int cmd_repack(int argc,
12661266

12671267
if (write_bitmaps < 0) {
12681268
if (!write_midx &&
1269-
(!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1269+
(!(pack_everything & ALL_INTO_ONE) || !repo_is_bare(the_repository)))
12701270
write_bitmaps = 0;
12711271
}
12721272
if (pack_kept_objects < 0)

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ int cmd_reset(int argc,
448448
if (reset_type != SOFT && (reset_type != MIXED || repo_get_work_tree(the_repository)))
449449
setup_work_tree();
450450

451-
if (reset_type == MIXED && is_bare_repository())
451+
if (reset_type == MIXED && repo_is_bare(the_repository))
452452
die(_("%s reset is not allowed in a bare repository"),
453453
_(reset_type_names[reset_type]));
454454

builtin/rev-parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ int cmd_rev_parse(int argc,
10631063
continue;
10641064
}
10651065
if (!strcmp(arg, "--is-bare-repository")) {
1066-
printf("%s\n", is_bare_repository() ? "true"
1066+
printf("%s\n", repo_is_bare(the_repository) ? "true"
10671067
: "false");
10681068
continue;
10691069
}

0 commit comments

Comments
 (0)