]> git.ipfire.org Git - thirdparty/git.git/commitdiff
environment: make `get_index_file()` accept a repository
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Sep 2024 11:29:32 +0000 (13:29 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Sep 2024 17:15:39 +0000 (10:15 -0700)
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>
builtin/am.c
builtin/commit.c
builtin/merge.c
builtin/stash.c
builtin/update-index.c
builtin/write-tree.c
environment.c
environment.h
repository.c
repository.h
wt-status.c

index 405214e242aeccc7eaa4ef94b80803a502d1b1fc..5498ddeb6aa0b138ccce32fa791b03a4d2a53145 100644 (file)
@@ -1668,7 +1668,9 @@ static void do_commit(const struct am_state *state)
        if (!state->no_verify && run_hooks(the_repository, "pre-applypatch"))
                exit(1);
 
-       if (write_index_as_tree(&tree, the_repository->index, get_index_file(), 0, NULL))
+       if (write_index_as_tree(&tree, the_repository->index,
+                               repo_get_index_file(the_repository),
+                               0, NULL))
                die(_("git write-tree failed to write a tree"));
 
        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
        if (fast_forward_to(head_tree, head_tree, 1))
                return -1;
 
-       if (write_index_as_tree(&index, the_repository->index, get_index_file(), 0, NULL))
+       if (write_index_as_tree(&index, the_repository->index,
+                               repo_get_index_file(the_repository),
+                               0, NULL))
                return -1;
 
        index_tree = parse_tree_indirect(&index);
index a1c1d16a099f635fc5797811d7e737122af7bcb5..b09320f9070b33145330cd35daf11514d750fdc6 100644 (file)
@@ -473,7 +473,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
                                       COMMIT_LOCK | SKIP_IF_UNCHANGED))
                        die(_("unable to write new index file"));
                commit_style = COMMIT_AS_IS;
-               ret = get_index_file();
+               ret = repo_get_index_file(the_repository);
                goto out;
        }
 
@@ -1874,8 +1874,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
        repo_rerere(the_repository, 0);
        run_auto_maintenance(quiet);
-       run_commit_hook(use_editor, get_index_file(), NULL, "post-commit",
-                       NULL);
+       run_commit_hook(use_editor, repo_get_index_file(the_repository),
+                       NULL, "post-commit", NULL);
        if (amend && !no_post_rewrite) {
                commit_post_rewrite(the_repository, current_head, &oid);
        }
index a2bae0700b4725f28c70fea9cf6e88dc0b569861..7f5475f738c05b737ec8defbd0b013c75396bb2c 100644 (file)
@@ -696,7 +696,9 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
 
 static void write_tree_trivial(struct object_id *oid)
 {
-       if (write_index_as_tree(oid, the_repository->index, get_index_file(), 0, NULL))
+       if (write_index_as_tree(oid, the_repository->index,
+                               repo_get_index_file(the_repository),
+                               0, NULL))
                die(_("git write-tree failed to write a tree"));
 }
 
@@ -758,7 +760,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
                }
                if (write_locked_index(the_repository->index, &lock,
                                       COMMIT_LOCK | SKIP_IF_UNCHANGED))
-                       die(_("unable to write %s"), get_index_file());
+                       die(_("unable to write %s"), repo_get_index_file(the_repository));
                return clean ? 0 : 1;
        } else {
                return try_merge_command(the_repository,
@@ -840,7 +842,7 @@ static void write_merge_heads(struct commit_list *);
 static void prepare_to_commit(struct commit_list *remoteheads)
 {
        struct strbuf msg = STRBUF_INIT;
-       const char *index_file = get_index_file();
+       const char *index_file = repo_get_index_file(the_repository);
 
        if (!no_verify) {
                int invoked_hook;
@@ -880,8 +882,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0);
        write_merge_heads(remoteheads);
        write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
-       if (run_commit_hook(0 < option_edit, get_index_file(), NULL,
-                           "prepare-commit-msg",
+       if (run_commit_hook(0 < option_edit, repo_get_index_file(the_repository),
+                           NULL, "prepare-commit-msg",
                            git_path_merge_msg(the_repository), "merge", NULL))
                abort_commit(remoteheads, NULL);
        if (0 < option_edit) {
@@ -889,7 +891,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                        abort_commit(remoteheads, NULL);
        }
 
-       if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
+       if (!no_verify && run_commit_hook(0 < option_edit, repo_get_index_file(the_repository),
                                          NULL, "commit-msg",
                                          git_path_merge_msg(the_repository), NULL))
                abort_commit(remoteheads, NULL);
index ad6bcefb77097462dca0d94fd43e4742a15b070d..f2ec9549a47c263f59370210e78e5aa08571f6d2 100644 (file)
@@ -540,8 +540,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
                                         NULL, NULL, NULL))
                return error(_("could not write index"));
 
-       if (write_index_as_tree(&c_tree, the_repository->index, get_index_file(), 0,
-                               NULL))
+       if (write_index_as_tree(&c_tree, the_repository->index,
+                               repo_get_index_file(the_repository), 0, NULL))
                return error(_("cannot apply a stash in the middle of a merge"));
 
        if (index) {
@@ -566,7 +566,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
                        discard_index(the_repository->index);
                        repo_read_index(the_repository);
                        if (write_index_as_tree(&index_tree, the_repository->index,
-                                               get_index_file(), 0, NULL))
+                                               repo_get_index_file(the_repository), 0, NULL))
                                return error(_("could not save index tree"));
 
                        reset_head();
@@ -1406,8 +1406,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 
        strbuf_addf(&commit_tree_label, "index on %s\n", msg.buf);
        commit_list_insert(head_commit, &parents);
-       if (write_index_as_tree(&info->i_tree, the_repository->index, get_index_file(), 0,
-                               NULL) ||
+       if (write_index_as_tree(&info->i_tree, the_repository->index,
+                               repo_get_index_file(the_repository), 0, NULL) ||
            commit_tree(commit_tree_label.buf, commit_tree_label.len,
                        &info->i_tree, parents, &info->i_commit, NULL, NULL)) {
                if (!quiet)
@@ -1905,7 +1905,7 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
        prepare_repo_settings(the_repository);
        the_repository->settings.command_requires_full_index = 0;
 
-       index_file = get_index_file();
+       index_file = repo_get_index_file(the_repository);
        strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file,
                    (uintmax_t)pid);
 
index 35a1f957adc8608613ec05b7ac3668048adef4e5..86c5d40e40036341e4db02328ffef75a96ce765a 100644 (file)
@@ -1239,7 +1239,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
                if (newfd < 0) {
                        if (refresh_args.flags & REFRESH_QUIET)
                                exit(128);
-                       unable_to_lock_die(get_index_file(), lock_error);
+                       unable_to_lock_die(repo_get_index_file(the_repository), lock_error);
                }
                if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
                        die("Unable to write new index file");
index 8c75b4609b5692d3c41836793d4cad878219a2ac..9bcc4470ce1e145c0dd0f67b5b27ed4128fd13f4 100644 (file)
@@ -6,7 +6,6 @@
 
 #include "builtin.h"
 #include "config.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "tree.h"
@@ -44,7 +43,8 @@ int cmd_write_tree(int argc, const char **argv, const char *cmd_prefix)
        prepare_repo_settings(the_repository);
        the_repository->settings.command_requires_full_index = 0;
 
-       ret = write_index_as_tree(&oid, the_repository->index, get_index_file(),
+       ret = write_index_as_tree(&oid, the_repository->index,
+                                 repo_get_index_file(the_repository),
                                  flags, tree_prefix);
        switch (ret) {
        case 0:
index 0a2057399e0305a8437bce6978bf691c1e482149..10ef77576c392ad0af3f4c3703d28aaba05a74f9 100644 (file)
@@ -306,13 +306,6 @@ int odb_pack_keep(const char *name)
        return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 }
 
-char *get_index_file(void)
-{
-       if (!the_repository->index_file)
-               BUG("git environment hasn't been setup");
-       return the_repository->index_file;
-}
-
 char *get_graft_file(struct repository *r)
 {
        if (!r->graft_file)
index 91125d829915ad0a309e031b7d3587bd0d74354d..ff590cfff73315191707ebf61e6e6e168ee4f206 100644 (file)
@@ -106,7 +106,6 @@ int have_git_dir(void);
 extern int is_bare_repository_cfg;
 int is_bare_repository(void);
 extern char *git_work_tree_cfg;
-char *get_index_file(void);
 char *get_graft_file(struct repository *r);
 void set_git_dir(const char *path, int make_realpath);
 const char *get_git_namespace(void);
index 49c42c25daaf9e0b6a6fd96e2a9bed1a92bdc4a1..849a912b03199f81527ac35cb99bcec2f6c2aeda 100644 (file)
@@ -112,6 +112,13 @@ const char *repo_get_object_directory(struct repository *repo)
        return repo->objects->odb->path;
 }
 
+const char *repo_get_index_file(struct repository *repo)
+{
+       if (!repo->index_file)
+               BUG("repository hasn't been set up");
+       return repo->index_file;
+}
+
 static void repo_set_commondir(struct repository *repo,
                               const char *commondir)
 {
index 778f1511ab1500d3d0edbbef3afc20e5c6e8e734..15660ac2f19e7fea5930128f088544cf11536d2c 100644 (file)
@@ -209,6 +209,7 @@ extern struct repository *the_repository;
 const char *repo_get_git_dir(struct repository *repo);
 const char *repo_get_common_dir(struct repository *repo);
 const char *repo_get_object_directory(struct repository *repo);
+const char *repo_get_index_file(struct repository *repo);
 
 /*
  * Define a custom repository layout. Any field can be NULL, which
index b477239039dd93ce70a58c56c0f5e070aa166cf0..b813d3fe1c4ceeb39238fd48c41b3849258983e3 100644 (file)
@@ -16,6 +16,7 @@
 #include "revision.h"
 #include "diffcore.h"
 #include "quote.h"
+#include "repository.h"
 #include "run-command.h"
 #include "strvec.h"
 #include "remote.h"
@@ -152,7 +153,7 @@ void wt_status_prepare(struct repository *r, struct wt_status *s)
                                        "HEAD", 0, NULL, NULL);
        s->reference = "HEAD";
        s->fp = stdout;
-       s->index_file = get_index_file();
+       s->index_file = repo_get_index_file(the_repository);
        s->change.strdup_strings = 1;
        s->untracked.strdup_strings = 1;
        s->ignored.strdup_strings = 1;