Skip to content

Commit 1dc4ec2

Browse files
pks-tgitster
authored andcommitted
environment: make get_index_file() accept a repository
The `get_index_file()` function retrieves the path to the index file of `the_repository`. Make it accept a `struct repository` such that it can work on arbitrary repositories and make it part of the repository subsystem. This reduces our reliance on `the_repository` and clarifies scope. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3673f4 commit 1dc4ec2

File tree

11 files changed

+36
-29
lines changed

11 files changed

+36
-29
lines changed

builtin/am.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,9 @@ static void do_commit(const struct am_state *state)
16681668
if (!state->no_verify && run_hooks(the_repository, "pre-applypatch"))
16691669
exit(1);
16701670

1671-
if (write_index_as_tree(&tree, the_repository->index, get_index_file(), 0, NULL))
1671+
if (write_index_as_tree(&tree, the_repository->index,
1672+
repo_get_index_file(the_repository),
1673+
0, NULL))
16721674
die(_("git write-tree failed to write a tree"));
16731675

16741676
if (!repo_get_oid_commit(the_repository, "HEAD", &parent)) {
@@ -2078,7 +2080,9 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
20782080
if (fast_forward_to(head_tree, head_tree, 1))
20792081
return -1;
20802082

2081-
if (write_index_as_tree(&index, the_repository->index, get_index_file(), 0, NULL))
2083+
if (write_index_as_tree(&index, the_repository->index,
2084+
repo_get_index_file(the_repository),
2085+
0, NULL))
20822086
return -1;
20832087

20842088
index_tree = parse_tree_indirect(&index);

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
473473
COMMIT_LOCK | SKIP_IF_UNCHANGED))
474474
die(_("unable to write new index file"));
475475
commit_style = COMMIT_AS_IS;
476-
ret = get_index_file();
476+
ret = repo_get_index_file(the_repository);
477477
goto out;
478478
}
479479

@@ -1874,8 +1874,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
18741874

18751875
repo_rerere(the_repository, 0);
18761876
run_auto_maintenance(quiet);
1877-
run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
1878-
NULL);
1877+
run_commit_hook(use_editor, repo_get_index_file(the_repository),
1878+
NULL, "post-commit", NULL);
18791879
if (amend && !no_post_rewrite) {
18801880
commit_post_rewrite(the_repository, current_head, &oid);
18811881
}

builtin/merge.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
696696

697697
static void write_tree_trivial(struct object_id *oid)
698698
{
699-
if (write_index_as_tree(oid, the_repository->index, get_index_file(), 0, NULL))
699+
if (write_index_as_tree(oid, the_repository->index,
700+
repo_get_index_file(the_repository),
701+
0, NULL))
700702
die(_("git write-tree failed to write a tree"));
701703
}
702704

@@ -758,7 +760,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
758760
}
759761
if (write_locked_index(the_repository->index, &lock,
760762
COMMIT_LOCK | SKIP_IF_UNCHANGED))
761-
die(_("unable to write %s"), get_index_file());
763+
die(_("unable to write %s"), repo_get_index_file(the_repository));
762764
return clean ? 0 : 1;
763765
} else {
764766
return try_merge_command(the_repository,
@@ -840,7 +842,7 @@ static void write_merge_heads(struct commit_list *);
840842
static void prepare_to_commit(struct commit_list *remoteheads)
841843
{
842844
struct strbuf msg = STRBUF_INIT;
843-
const char *index_file = get_index_file();
845+
const char *index_file = repo_get_index_file(the_repository);
844846

845847
if (!no_verify) {
846848
int invoked_hook;
@@ -880,16 +882,16 @@ static void prepare_to_commit(struct commit_list *remoteheads)
880882
append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0);
881883
write_merge_heads(remoteheads);
882884
write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
883-
if (run_commit_hook(0 < option_edit, get_index_file(), NULL,
884-
"prepare-commit-msg",
885+
if (run_commit_hook(0 < option_edit, repo_get_index_file(the_repository),
886+
NULL, "prepare-commit-msg",
885887
git_path_merge_msg(the_repository), "merge", NULL))
886888
abort_commit(remoteheads, NULL);
887889
if (0 < option_edit) {
888890
if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
889891
abort_commit(remoteheads, NULL);
890892
}
891893

892-
if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
894+
if (!no_verify && run_commit_hook(0 < option_edit, repo_get_index_file(the_repository),
893895
NULL, "commit-msg",
894896
git_path_merge_msg(the_repository), NULL))
895897
abort_commit(remoteheads, NULL);

builtin/stash.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
540540
NULL, NULL, NULL))
541541
return error(_("could not write index"));
542542

543-
if (write_index_as_tree(&c_tree, the_repository->index, get_index_file(), 0,
544-
NULL))
543+
if (write_index_as_tree(&c_tree, the_repository->index,
544+
repo_get_index_file(the_repository), 0, NULL))
545545
return error(_("cannot apply a stash in the middle of a merge"));
546546

547547
if (index) {
@@ -566,7 +566,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
566566
discard_index(the_repository->index);
567567
repo_read_index(the_repository);
568568
if (write_index_as_tree(&index_tree, the_repository->index,
569-
get_index_file(), 0, NULL))
569+
repo_get_index_file(the_repository), 0, NULL))
570570
return error(_("could not save index tree"));
571571

572572
reset_head();
@@ -1406,8 +1406,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14061406

14071407
strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
14081408
commit_list_insert(head_commit, &parents);
1409-
if (write_index_as_tree(&info->i_tree, the_repository->index, get_index_file(), 0,
1410-
NULL) ||
1409+
if (write_index_as_tree(&info->i_tree, the_repository->index,
1410+
repo_get_index_file(the_repository), 0, NULL) ||
14111411
commit_tree(commit_tree_label.buf, commit_tree_label.len,
14121412
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
14131413
if (!quiet)
@@ -1905,7 +1905,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
19051905
prepare_repo_settings(the_repository);
19061906
the_repository->settings.command_requires_full_index = 0;
19071907

1908-
index_file = get_index_file();
1908+
index_file = repo_get_index_file(the_repository);
19091909
strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
19101910
(uintmax_t)pid);
19111911

builtin/update-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
12391239
if (newfd < 0) {
12401240
if (refresh_args.flags & REFRESH_QUIET)
12411241
exit(128);
1242-
unable_to_lock_die(get_index_file(), lock_error);
1242+
unable_to_lock_die(repo_get_index_file(the_repository), lock_error);
12431243
}
12441244
if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
12451245
die("Unable to write new index file");

builtin/write-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "builtin.h"
88
#include "config.h"
9-
#include "environment.h"
109
#include "gettext.h"
1110
#include "hex.h"
1211
#include "tree.h"
@@ -44,7 +43,8 @@ int cmd_write_tree(int argc, const char **argv, const char *cmd_prefix)
4443
prepare_repo_settings(the_repository);
4544
the_repository->settings.command_requires_full_index = 0;
4645

47-
ret = write_index_as_tree(&oid, the_repository->index, get_index_file(),
46+
ret = write_index_as_tree(&oid, the_repository->index,
47+
repo_get_index_file(the_repository),
4848
flags, tree_prefix);
4949
switch (ret) {
5050
case 0:

environment.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,6 @@ int odb_pack_keep(const char *name)
306306
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
307307
}
308308

309-
char *get_index_file(void)
310-
{
311-
if (!the_repository->index_file)
312-
BUG("git environment hasn't been setup");
313-
return the_repository->index_file;
314-
}
315-
316309
char *get_graft_file(struct repository *r)
317310
{
318311
if (!r->graft_file)

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ int have_git_dir(void);
106106
extern int is_bare_repository_cfg;
107107
int is_bare_repository(void);
108108
extern char *git_work_tree_cfg;
109-
char *get_index_file(void);
110109
char *get_graft_file(struct repository *r);
111110
void set_git_dir(const char *path, int make_realpath);
112111
const char *get_git_namespace(void);

repository.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ const char *repo_get_object_directory(struct repository *repo)
112112
return repo->objects->odb->path;
113113
}
114114

115+
const char *repo_get_index_file(struct repository *repo)
116+
{
117+
if (!repo->index_file)
118+
BUG("repository hasn't been set up");
119+
return repo->index_file;
120+
}
121+
115122
static void repo_set_commondir(struct repository *repo,
116123
const char *commondir)
117124
{

repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ extern struct repository *the_repository;
209209
const char *repo_get_git_dir(struct repository *repo);
210210
const char *repo_get_common_dir(struct repository *repo);
211211
const char *repo_get_object_directory(struct repository *repo);
212+
const char *repo_get_index_file(struct repository *repo);
212213

213214
/*
214215
* Define a custom repository layout. Any field can be NULL, which

0 commit comments

Comments
 (0)