read-cache.c: kill read_index()
read_index() shares the same problem as hold_locked_index(): it assumes $GIT_DIR/index. Move all call sites to repo_read_index() instead. read_index_preload() and read_index_unmerged() are also killed as a consequence. 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
e1ff0a32e48eb0f3e53970df3f941d183093ff5a
16 files changed
+49
-50
apply.c
+1
-1
@@ -4019,7 +4019,7 @@ static int read_apply_cache(struct apply_state *state)
4019
return read_index_from(state->repo->index, state->index_file,
4020
get_git_dir());
4021
else
4022
- return read_index(state->repo->index);
4022
+ return repo_read_index(state->repo);
4023
}
4024
4025
/* This function tries to read the object name from the current index */
blame.c
+2
-2
@@ -188,7 +188,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,
188
unsigned mode;
189
struct strbuf msg = STRBUF_INIT;
190
191
- read_index(r->index);
191
+ repo_read_index(r);
192
time(&now);
193
commit = alloc_commit_node(r);
194
commit->object.parsed = 1;
@@ -270,7 +270,7 @@ static struct commit *fake_working_tree_commit(struct repository *r,
270
* want to run "diff-index --cached".
271
*/
272
discard_index(r->index);
273
- read_index(r->index);
273
+ repo_read_index(r);
274
275
len = strlen(path);
276
if (!mode) {
builtin/am.c
+1
-1
@@ -2278,7 +2278,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2278
/* Ensure a valid committer ident can be constructed */
2279
git_committer_info(IDENT_STRICT);
2280
2281
- if (read_index_preload(&the_index, NULL, 0) < 0)
2281
+ if (repo_read_index_preload(the_repository, NULL, 0) < 0)
2282
die(_("failed to read the index"));
2283
2284
if (in_progress) {
builtin/commit.c
+1
-1
@@ -1367,7 +1367,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1367
if (status_format != STATUS_FORMAT_PORCELAIN &&
1368
status_format != STATUS_FORMAT_PORCELAIN_V2)
1369
progress_flag = REFRESH_PROGRESS;
1370
- read_index(&the_index);
1370
+ repo_read_index(the_repository);
1371
refresh_index(&the_index,
1372
REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1373
&s.pathspec, NULL, NULL);
builtin/diff-tree.c
+1
-1
@@ -165,7 +165,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
165
166
if (opt->diffopt.detect_rename) {
167
if (!the_index.cache)
168
- read_index(&the_index);
168
+ repo_read_index(the_repository);
169
opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
170
}
171
while (fgets(line, sizeof(line), stdin)) {
builtin/rebase.c
+4
-4
@@ -576,7 +576,7 @@ static int reset_head(struct object_id *oid, const char *action,
576
if (!detach_head)
577
unpack_tree_opts.reset = 1;
578
579
- if (read_index_unmerged(the_repository->index) < 0) {
579
+ if (repo_read_index_unmerged(the_repository) < 0) {
580
ret = error(_("could not read index"));
581
goto leave_reset_head;
582
}
@@ -1015,7 +1015,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1015
die(_("Cannot read HEAD"));
1016
1017
fd = hold_locked_index(&lock_file, 0);
1018
- if (read_index(the_repository->index) < 0)
1018
+ if (repo_read_index(the_repository) < 0)
1019
die(_("could not read index"));
1020
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1021
NULL);
@@ -1368,7 +1368,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1368
get_fork_point(options.upstream_name, head);
1369
}
1370
1371
- if (read_index(the_repository->index) < 0)
1371
+ if (repo_read_index(the_repository) < 0)
1372
die(_("could not read index"));
1373
1374
if (options.autostash) {
@@ -1423,7 +1423,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1423
putchar('\n');
1424
1425
if (discard_index(the_repository->index) < 0 ||
1426
- read_index(the_repository->index) < 0)
1426
+ repo_read_index(the_repository) < 0)
1427
die(_("could not read index"));
1428
}
1429
}
cache.h
+3
-8
@@ -408,11 +408,11 @@ void validate_cache_entries(const struct index_state *istate);
408
#define active_cache_changed (the_index.cache_changed)
409
#define active_cache_tree (the_index.cache_tree)
410
411
-#define read_cache() read_index(&the_index)
411
+#define read_cache() repo_read_index(the_repository)
412
#define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir()))
413
-#define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec), 0)
413
+#define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0)
414
#define is_cache_unborn() is_index_unborn(&the_index)
415
-#define read_cache_unmerged() read_index_unmerged(&the_index)
415
+#define read_cache_unmerged() repo_read_index_unmerged(the_repository)
416
#define discard_cache() discard_index(&the_index)
417
#define unmerged_cache() unmerged_index(&the_index)
418
#define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen))
@@ -661,19 +661,14 @@ extern int daemonize(void);
661
662
/* Initialize and use the cache information */
663
struct lock_file;
664
-extern int read_index(struct index_state *);
664
extern void preload_index(struct index_state *index,
665
const struct pathspec *pathspec,
666
unsigned int refresh_flags);
668
-extern int read_index_preload(struct index_state *,
669
- const struct pathspec *pathspec,
670
- unsigned int refresh_flags);
667
extern int do_read_index(struct index_state *istate, const char *path,
668
int must_exist); /* for testting only! */
669
extern int read_index_from(struct index_state *, const char *path,
670
const char *gitdir);
671
extern int is_index_unborn(struct index_state *);
676
-extern int read_index_unmerged(struct index_state *);
672
673
/* For use with `write_locked_index()`. */
674
#define COMMIT_LOCK (1 << 0)
merge-recursive.c
+1
-1
@@ -3576,7 +3576,7 @@ int merge_recursive(struct merge_options *o,
3576
3577
discard_cache();
3578
if (!o->call_depth)
3579
- read_cache();
3579
+ repo_read_index(the_repository);
3580
3581
o->ancestor = "merged common ancestors";
3582
clean = merge_trees(o, get_commit_tree(h1), get_commit_tree(h2),
merge.c
+1
-1
@@ -37,7 +37,7 @@ int try_merge_command(struct repository *r,
37
argv_array_clear(&args);
38
39
discard_index(r->index);
40
- if (read_index(r->index) < 0)
40
+ if (repo_read_index(r) < 0)
41
die(_("failed to read the cache"));
42
resolve_undo_clear_index(r->index);
43
preload-index.c
+6
-5
@@ -8,6 +8,7 @@
8
#include "config.h"
9
#include "progress.h"
10
#include "thread-utils.h"
11
+#include "repository.h"
12
13
/*
14
* Mostly randomly chosen maximum thread counts: we
@@ -146,12 +147,12 @@ void preload_index(struct index_state *index,
147
trace_performance_leave("preload index");
148
}
149
149
-int read_index_preload(struct index_state *index,
150
- const struct pathspec *pathspec,
151
- unsigned int refresh_flags)
150
+int repo_read_index_preload(struct repository *repo,
151
+ const struct pathspec *pathspec,
152
+ unsigned int refresh_flags)
153
{
153
- int retval = read_index(index);
154
+ int retval = repo_read_index(repo);
155
155
- preload_index(index, pathspec, refresh_flags);
156
+ preload_index(repo->index, pathspec, refresh_flags);
157
return retval;
158
}
read-cache.c
+4
-7
@@ -1733,11 +1733,6 @@ static int read_index_extension(struct index_state *istate,
1733
return 0;
1734
}
1735
1736
-int read_index(struct index_state *istate)
1737
-{
1738
- return read_index_from(istate, get_index_file(), get_git_dir());
1739
-}
1740
-
1736
static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1737
unsigned int version,
1738
struct ondisk_cache_entry *ondisk,
@@ -3218,12 +3213,14 @@ out:
3213
* state can call this and check its return value, instead of calling
3214
* read_cache().
3215
*/
3221
-int read_index_unmerged(struct index_state *istate)
3216
+int repo_read_index_unmerged(struct repository *repo)
3217
{
3218
+ struct index_state *istate;
3219
int i;
3220
int unmerged = 0;
3221
3226
- read_index(istate);
3222
+ repo_read_index(repo);
3223
+ istate = repo->index;
3224
for (i = 0; i < istate->cache_nr; i++) {
3225
struct cache_entry *ce = istate->cache[i];
3226
struct cache_entry *new_ce;
repository.h
+6
@@ -7,6 +7,7 @@ struct config_set;
7
struct git_hash_algo;
8
struct index_state;
9
struct lock_file;
10
+struct pathspec;
11
struct raw_object_store;
12
struct submodule_cache;
13
@@ -135,4 +136,9 @@ int repo_hold_locked_index(struct repository *repo,
136
struct lock_file *lf,
137
int flags);
138
139
+int repo_read_index_preload(struct repository *,
140
+ const struct pathspec *pathspec,
141
+ unsigned refresh_flags);
142
+int repo_read_index_unmerged(struct repository *);
143
+
144
#endif /* REPOSITORY_H */
rerere.c
+3
-3
@@ -561,7 +561,7 @@ static int find_conflict(struct repository *r, struct string_list *conflict)
561
{
562
int i;
563
564
- if (read_index(r->index) < 0)
564
+ if (repo_read_index(r) < 0)
565
return error(_("index file corrupt"));
566
567
for (i = 0; i < r->index->cache_nr;) {
@@ -595,7 +595,7 @@ int rerere_remaining(struct repository *r, struct string_list *merge_rr)
595
596
if (setup_rerere(r, merge_rr, RERERE_READONLY))
597
return 0;
598
- if (read_index(r->index) < 0)
598
+ if (repo_read_index(r) < 0)
599
return error(_("index file corrupt"));
600
601
for (i = 0; i < r->index->cache_nr;) {
@@ -1107,7 +1107,7 @@ int rerere_forget(struct repository *r, struct pathspec *pathspec)
1107
struct string_list conflict = STRING_LIST_INIT_DUP;
1108
struct string_list merge_rr = STRING_LIST_INIT_DUP;
1109
1110
- if (read_index(r->index) < 0)
1110
+ if (repo_read_index(r) < 0)
1111
return error(_("index file corrupt"));
1112
1113
fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE);
revision.c
+2
-2
@@ -1384,7 +1384,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1384
{
1385
struct worktree **worktrees, **p;
1386
1387
- read_index(revs->repo->index);
1387
+ repo_read_index(revs->repo);
1388
do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1389
1390
if (revs->single_worktree)
@@ -1530,7 +1530,7 @@ static void prepare_show_merge(struct rev_info *revs)
1530
head->object.flags |= SYMMETRIC_LEFT;
1531
1532
if (!istate->cache_nr)
1533
- read_index(istate);
1533
+ repo_read_index(revs->repo);
1534
for (i = 0; i < istate->cache_nr; i++) {
1535
const struct cache_entry *ce = istate->cache[i];
1536
if (!ce_stage(ce))
sequencer.c
+10
-10
@@ -446,9 +446,9 @@ static struct tree *empty_tree(struct repository *r)
446
return lookup_tree(r, the_hash_algo->empty_tree);
447
}
448
449
-static int error_dirty_index(struct index_state *istate, struct replay_opts *opts)
449
+static int error_dirty_index(struct repository *repo, struct replay_opts *opts)
450
{
451
- if (read_index_unmerged(istate))
451
+ if (repo_read_index_unmerged(repo))
452
return error_resolve_conflict(_(action_name(opts)));
453
454
error(_("your local changes would be overwritten by %s."),
@@ -483,7 +483,7 @@ static int fast_forward_to(struct repository *r,
483
struct strbuf sb = STRBUF_INIT;
484
struct strbuf err = STRBUF_INIT;
485
486
- read_index(r->index);
486
+ repo_read_index(r);
487
if (checkout_fast_forward(r, from, to, 1))
488
return -1; /* the callee should have complained already */
489
@@ -543,7 +543,7 @@ static int do_recursive_merge(struct repository *r,
543
if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0)
544
return -1;
545
546
- read_index(r->index);
546
+ repo_read_index(r);
547
548
init_merge_options(&o);
549
o.ancestor = base ? base_label : "(empty tree)";
@@ -1766,7 +1766,7 @@ static int do_pick_commit(struct repository *r,
1766
oidcpy(&head, the_hash_algo->empty_tree);
1767
if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD",
1768
NULL, 0))
1769
- return error_dirty_index(r->index, opts);
1769
+ return error_dirty_index(r, opts);
1770
}
1771
discard_index(r->index);
1772
@@ -2854,7 +2854,7 @@ static int do_exec(struct repository *r, const char *command_line)
2854
child_env.argv);
2855
2856
/* force re-reading of the cache */
2857
- if (discard_index(r->index) < 0 || read_index(r->index) < 0)
2857
+ if (discard_index(r->index) < 0 || repo_read_index(r) < 0)
2858
return error(_("could not read index"));
2859
2860
dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1);
@@ -3023,7 +3023,7 @@ static int do_reset(struct repository *r,
3023
unpack_tree_opts.merge = 1;
3024
unpack_tree_opts.update = 1;
3025
3026
- if (read_index_unmerged(r->index)) {
3026
+ if (repo_read_index_unmerged(r)) {
3027
rollback_lock_file(&lock);
3028
strbuf_release(&ref_name);
3029
return error_resolve_conflict(_(action_name(opts)));
@@ -3277,7 +3277,7 @@ static int do_merge(struct repository *r,
3277
3278
/* force re-reading of the cache */
3279
if (!ret && (discard_index(r->index) < 0 ||
3280
- read_index(r->index) < 0))
3280
+ repo_read_index(r) < 0))
3281
ret = error(_("could not read index"));
3282
goto leave_merge;
3283
}
@@ -3299,7 +3299,7 @@ static int do_merge(struct repository *r,
3299
commit_list_insert(j->item, &reversed);
3300
free_commit_list(bases);
3301
3302
- read_index(r->index);
3302
+ repo_read_index(r);
3303
init_merge_options(&o);
3304
o.branch1 = "HEAD";
3305
o.branch2 = ref_name.buf;
@@ -3972,7 +3972,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
3972
goto release_todo_list;
3973
}
3974
if (index_differs_from(r, "HEAD", NULL, 0)) {
3975
- res = error_dirty_index(r->index, opts);
3975
+ res = error_dirty_index(r, opts);
3976
goto release_todo_list;
3977
}
3978
todo_list.current++;
sha1-name.c
+3
-3
@@ -1723,9 +1723,9 @@ static int get_oid_with_context_1(const char *name,
1723
if (flags & GET_OID_RECORD_PATH)
1724
oc->path = xstrdup(cp);
1725
1726
- if (!active_cache)
1727
- read_cache();
1728
- pos = cache_name_pos(cp, namelen);
1726
+ if (!the_index.cache)
1727
+ repo_read_index(the_repository);
1728
+ pos = index_name_pos(&the_index, cp, namelen);
1729
if (pos < 0)
1730
pos = -pos - 1;
1731
while (pos < active_nr) {