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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Sep 12, 2024 at 13:29 UTC
1dc4ec2102971ee5d19331a8ccf09939272bc6ca
11 files changed
+36
-29
builtin/am.c
+6
-2
@@ -1668,7 +1668,9 @@ static void do_commit(const struct am_state *state)
1668
if (!state->no_verify && run_hooks(the_repository, "pre-applypatch"))
1669
exit(1);
1670
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))
1674
die(_("git write-tree failed to write a tree"));
1675
1676
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
2080
if (fast_forward_to(head_tree, head_tree, 1))
2081
return -1;
2082
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))
2086
return -1;
2087
2088
index_tree = parse_tree_indirect(&index);
builtin/commit.c
+3
-3
@@ -473,7 +473,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
473
COMMIT_LOCK | SKIP_IF_UNCHANGED))
474
die(_("unable to write new index file"));
475
commit_style = COMMIT_AS_IS;
476
- ret = get_index_file();
476
+ ret = repo_get_index_file(the_repository);
477
goto out;
478
}
479
@@ -1874,8 +1874,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1874
1875
repo_rerere(the_repository, 0);
1876
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);
1879
if (amend && !no_post_rewrite) {
1880
commit_post_rewrite(the_repository, current_head, &oid);
1881
}
builtin/merge.c
+8
-6
@@ -696,7 +696,9 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
696
697
static void write_tree_trivial(struct object_id *oid)
698
{
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))
702
die(_("git write-tree failed to write a tree"));
703
}
704
@@ -758,7 +760,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
760
}
761
if (write_locked_index(the_repository->index, &lock,
762
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));
764
return clean ? 0 : 1;
765
} else {
766
return try_merge_command(the_repository,
@@ -840,7 +842,7 @@ static void write_merge_heads(struct commit_list *);
842
static void prepare_to_commit(struct commit_list *remoteheads)
843
{
844
struct strbuf msg = STRBUF_INIT;
843
- const char *index_file = get_index_file();
845
+ const char *index_file = repo_get_index_file(the_repository);
846
847
if (!no_verify) {
848
int invoked_hook;
@@ -880,8 +882,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
882
append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0);
883
write_merge_heads(remoteheads);
884
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",
887
git_path_merge_msg(the_repository), "merge", NULL))
888
abort_commit(remoteheads, NULL);
889
if (0 < option_edit) {
@@ -889,7 +891,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
891
abort_commit(remoteheads, NULL);
892
}
893
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),
895
NULL, "commit-msg",
896
git_path_merge_msg(the_repository), NULL))
897
abort_commit(remoteheads, NULL);
builtin/stash.c
+6
-6
@@ -540,8 +540,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
540
NULL, NULL, NULL))
541
return error(_("could not write index"));
542
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))
545
return error(_("cannot apply a stash in the middle of a merge"));
546
547
if (index) {
@@ -566,7 +566,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
566
discard_index(the_repository->index);
567
repo_read_index(the_repository);
568
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))
570
return error(_("could not save index tree"));
571
572
reset_head();
@@ -1406,8 +1406,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
1406
1407
strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
1408
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) ||
1411
commit_tree(commit_tree_label.buf, commit_tree_label.len,
1412
&info->i_tree, parents, &info->i_commit, NULL, NULL)) {
1413
if (!quiet)
@@ -1905,7 +1905,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
1905
prepare_repo_settings(the_repository);
1906
the_repository->settings.command_requires_full_index = 0;
1907
1908
- index_file = get_index_file();
1908
+ index_file = repo_get_index_file(the_repository);
1909
strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
1910
(uintmax_t)pid);
1911
builtin/update-index.c
+1
-1
@@ -1239,7 +1239,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1239
if (newfd < 0) {
1240
if (refresh_args.flags & REFRESH_QUIET)
1241
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);
1243
}
1244
if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
1245
die("Unable to write new index file");
builtin/write-tree.c
+2
-2
@@ -6,7 +6,6 @@
6
7
#include "builtin.h"
8
#include "config.h"
9
-#include "environment.h"
9
#include "gettext.h"
10
#include "hex.h"
11
#include "tree.h"
@@ -44,7 +43,8 @@ int cmd_write_tree(int argc, const char **argv, const char *cmd_prefix)
43
prepare_repo_settings(the_repository);
44
the_repository->settings.command_requires_full_index = 0;
45
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),
48
flags, tree_prefix);
49
switch (ret) {
50
case 0:
environment.c
-7
@@ -306,13 +306,6 @@ int odb_pack_keep(const char *name)
306
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
307
}
308
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
-
309
char *get_graft_file(struct repository *r)
310
{
311
if (!r->graft_file)
environment.h
-1
@@ -106,7 +106,6 @@ int have_git_dir(void);
106
extern int is_bare_repository_cfg;
107
int is_bare_repository(void);
108
extern char *git_work_tree_cfg;
109
-char *get_index_file(void);
109
char *get_graft_file(struct repository *r);
110
void set_git_dir(const char *path, int make_realpath);
111
const char *get_git_namespace(void);
repository.c
+7
@@ -112,6 +112,13 @@ const char *repo_get_object_directory(struct repository *repo)
112
return repo->objects->odb->path;
113
}
114
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
+
122
static void repo_set_commondir(struct repository *repo,
123
const char *commondir)
124
{
repository.h
+1
@@ -209,6 +209,7 @@ extern struct repository *the_repository;
209
const char *repo_get_git_dir(struct repository *repo);
210
const char *repo_get_common_dir(struct repository *repo);
211
const char *repo_get_object_directory(struct repository *repo);
212
+const char *repo_get_index_file(struct repository *repo);
213
214
/*
215
* Define a custom repository layout. Any field can be NULL, which
wt-status.c
+2
-1
@@ -16,6 +16,7 @@
16
#include "revision.h"
17
#include "diffcore.h"
18
#include "quote.h"
19
+#include "repository.h"
20
#include "run-command.h"
21
#include "strvec.h"
22
#include "remote.h"
@@ -152,7 +153,7 @@ void wt_status_prepare(struct repository *r, struct wt_status *s)
153
"HEAD", 0, NULL, NULL);
154
s->reference = "HEAD";
155
s->fp = stdout;
155
- s->index_file = get_index_file();
156
+ s->index_file = repo_get_index_file(the_repository);
157
s->change.strdup_strings = 1;
158
s->untracked.strdup_strings = 1;
159
s->ignored.strdup_strings = 1;