repository.c: replace hold_locked_index() with repo_hold_locked_index()

hold_locked_index() assumes the index path at $GIT_DIR/index. This is not good for places that take an arbitrary index_state instead of the_index, which is basically everywhere except builtin/. Replace it with repo_hold_locked_index(). hold_locked_index() remains as a wrapper around repo_hold_locked_index() to reduce changes in builtin/ Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 12, 2019 at 09:13 UTC 3a95f31d1cdc93fa4f926c6537f84186edd85ca9
10 files changed +26 -16
apply.c
+2 -1
@@ -4712,7 +4712,8 @@ static int apply_patch(struct apply_state *state,
4712 state->index_file,
4713 LOCK_DIE_ON_ERROR);
4714 else
4715 - hold_locked_index(&state->lock_file, LOCK_DIE_ON_ERROR);
4715 + repo_hold_locked_index(state->repo, &state->lock_file,
4716 + LOCK_DIE_ON_ERROR);
4717 }
4718
4719 if (state->check_index && read_apply_cache(state) < 0) {
cache.h
+1 -1
@@ -433,6 +433,7 @@ void validate_cache_entries(const struct index_state *istate);
433 #define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
434 #define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
435 #define read_blob_data_from_cache(path, sz) read_blob_data_from_index(&the_index, (path), (sz))
436 +#define hold_locked_index(lock_file, flags) repo_hold_locked_index(the_repository, (lock_file), (flags))
437 #endif
438
439 #define TYPE_BITS 3
@@ -833,7 +834,6 @@ extern struct cache_entry *refresh_cache_entry(struct index_state *, struct cach
834 */
835 extern void update_index_if_able(struct index_state *, struct lock_file *);
836
836 -extern int hold_locked_index(struct lock_file *, int);
837 extern void set_alternate_index_output(const char *);
838
839 extern int verify_index_checksum;
merge-recursive.c
+1 -1
@@ -3643,7 +3643,7 @@ int merge_recursive_generic(struct merge_options *o,
3643 }
3644 }
3645
3646 - hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
3646 + repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
3647 clean = merge_recursive(o, head_commit, next_commit, ca,
3648 result);
3649 if (clean < 0) {
merge.c
+1 -1
@@ -58,7 +58,7 @@ int checkout_fast_forward(struct repository *r,
58
59 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
60
61 - if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
61 + if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
62 return -1;
63
64 memset(&trees, 0, sizeof(trees));
read-cache.c
-5
@@ -1733,11 +1733,6 @@ static int read_index_extension(struct index_state *istate,
1733 return 0;
1734 }
1735
1736 -int hold_locked_index(struct lock_file *lk, int lock_flags)
1737 -{
1738 - return hold_lock_file_for_update(lk, get_index_file(), lock_flags);
1739 -}
1740 -
1736 int read_index(struct index_state *istate)
1737 {
1738 return read_index_from(istate, get_index_file(), get_git_dir());
repository.c
+10
@@ -3,6 +3,7 @@
3 #include "object-store.h"
4 #include "config.h"
5 #include "object.h"
6 +#include "lockfile.h"
7 #include "submodule-config.h"
8
9 /* The main repository */
@@ -263,3 +264,12 @@ int repo_read_index(struct repository *repo)
264
265 return read_index_from(repo->index, repo->index_file, repo->gitdir);
266 }
267 +
268 +int repo_hold_locked_index(struct repository *repo,
269 + struct lock_file *lf,
270 + int flags)
271 +{
272 + if (!repo->index_file)
273 + BUG("the repo hasn't been setup");
274 + return hold_lock_file_for_update(lf, repo->index_file, flags);
275 +}
repository.h
+4
@@ -6,6 +6,7 @@
6 struct config_set;
7 struct git_hash_algo;
8 struct index_state;
9 +struct lock_file;
10 struct raw_object_store;
11 struct submodule_cache;
12
@@ -130,5 +131,8 @@ void repo_clear(struct repository *repo);
131 * populated then the number of entries will simply be returned.
132 */
133 int repo_read_index(struct repository *repo);
134 +int repo_hold_locked_index(struct repository *repo,
135 + struct lock_file *lf,
136 + int flags);
137
138 #endif /* REPOSITORY_H */
rerere.c
+1 -1
@@ -705,7 +705,7 @@ static void update_paths(struct repository *r, struct string_list *update)
705 struct lock_file index_lock = LOCK_INIT;
706 int i;
707
708 - hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
708 + repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR);
709
710 for (i = 0; i < update->nr; i++) {
711 struct string_list_item *item = &update->items[i];
sequencer.c
+5 -5
@@ -540,7 +540,7 @@ static int do_recursive_merge(struct repository *r,
540 char **xopt;
541 struct lock_file index_lock = LOCK_INIT;
542
543 - if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0)
543 + if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
544 return -1;
545
546 read_index(r->index);
@@ -1992,8 +1992,8 @@ static int read_and_refresh_cache(struct repository *r,
1992 struct replay_opts *opts)
1993 {
1994 struct lock_file index_lock = LOCK_INIT;
1995 - int index_fd = hold_locked_index(&index_lock, 0);
1996 - if (read_index(r->index) < 0) {
1995 + int index_fd = repo_hold_locked_index(r, &index_lock, 0);
1996 + if (repo_read_index(r) < 0) {
1997 rollback_lock_file(&index_lock);
1998 return error(_("git %s: failed to read the index"),
1999 _(action_name(opts)));
@@ -2978,7 +2978,7 @@ static int do_reset(struct repository *r,
2978 struct unpack_trees_options unpack_tree_opts;
2979 int ret = 0;
2980
2981 - if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
2981 + if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0)
2982 return -1;
2983
2984 if (len == 10 && !strncmp("[new root]", name, len)) {
@@ -3096,7 +3096,7 @@ static int do_merge(struct repository *r,
3096 static struct lock_file lock;
3097 const char *p;
3098
3099 - if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
3099 + if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
3100 ret = -1;
3101 goto leave_merge;
3102 }
wt-status.c
+1 -1
@@ -2375,7 +2375,7 @@ int require_clean_work_tree(struct repository *r,
2375 struct lock_file lock_file = LOCK_INIT;
2376 int err = 0, fd;
2377
2378 - fd = hold_locked_index(&lock_file, 0);
2378 + fd = repo_hold_locked_index(r, &lock_file, 0);
2379 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2380 if (0 <= fd)
2381 update_index_if_able(r->index, &lock_file);