Skip to content

Commit 5b02ca3

Browse files
pcloudsgitster
authored andcommitted
wt-status.c: remove implicit dependency on the_index
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8858448 commit 5b02ca3

File tree

6 files changed

+66
-47
lines changed

6 files changed

+66
-47
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void determine_whence(struct wt_status *s)
185185

186186
static void status_init_config(struct wt_status *s, config_fn_t fn)
187187
{
188-
wt_status_prepare(s);
188+
wt_status_prepare(the_repository, s);
189189
init_diff_ui_defaults();
190190
git_config(fn, s);
191191
determine_whence(s);

builtin/pull.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
888888
die(_("Updating an unborn branch with changes added to the index."));
889889

890890
if (!autostash)
891-
require_clean_work_tree(N_("pull with rebase"),
891+
require_clean_work_tree(the_repository,
892+
N_("pull with rebase"),
892893
_("please commit or stash them."), 1, 0);
893894

894895
if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs))

builtin/rebase.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
983983
&lock_file);
984984
rollback_lock_file(&lock_file);
985985

986-
if (has_unstaged_changes(1)) {
986+
if (has_unstaged_changes(the_repository, 1)) {
987987
puts(_("You must edit all merge conflicts and then\n"
988988
"mark them as resolved using git add"));
989989
exit(1);
@@ -1351,7 +1351,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13511351
update_index_if_able(&the_index, &lock_file);
13521352
rollback_lock_file(&lock_file);
13531353

1354-
if (has_unstaged_changes(1) || has_uncommitted_changes(1)) {
1354+
if (has_unstaged_changes(the_repository, 1) ||
1355+
has_uncommitted_changes(the_repository, 1)) {
13551356
const char *autostash =
13561357
state_dir_path("autostash", &options);
13571358
struct child_process stash = CHILD_PROCESS_INIT;
@@ -1397,7 +1398,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
13971398
}
13981399
}
13991400

1400-
if (require_clean_work_tree("rebase",
1401+
if (require_clean_work_tree(the_repository, "rebase",
14011402
_("Please commit or stash them."), 1, 1)) {
14021403
ret = 1;
14031404
goto cleanup;

sequencer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,7 @@ static int do_exec(const char *command_line)
27732773
if (discard_cache() < 0 || read_cache() < 0)
27742774
return error(_("could not read index"));
27752775

2776-
dirty = require_clean_work_tree("rebase", NULL, 1, 1);
2776+
dirty = require_clean_work_tree(the_repository, "rebase", NULL, 1, 1);
27772777

27782778
if (status) {
27792779
warning(_("execution failed: %s\n%s"
@@ -3714,10 +3714,10 @@ static int commit_staged_changes(struct replay_opts *opts,
37143714
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
37153715
unsigned int final_fixup = 0, is_clean;
37163716

3717-
if (has_unstaged_changes(1))
3717+
if (has_unstaged_changes(the_repository, 1))
37183718
return error(_("cannot rebase: You have unstaged changes."));
37193719

3720-
is_clean = !has_uncommitted_changes(0);
3720+
is_clean = !has_uncommitted_changes(the_repository, 0);
37213721

37223722
if (file_exists(rebase_path_amend())) {
37233723
struct strbuf rev = STRBUF_INIT;
@@ -4847,7 +4847,7 @@ int complete_action(struct replay_opts *opts, unsigned flags,
48474847
if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
48484848
return -1;
48494849
;
4850-
if (require_clean_work_tree("rebase", "", 1, 1))
4850+
if (require_clean_work_tree(the_repository, "rebase", "", 1, 1))
48514851
return -1;
48524852

48534853
return sequencer_continue(opts);

wt-status.c

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ static void status_printf_more(struct wt_status *s, const char *color,
119119
va_end(ap);
120120
}
121121

122-
void wt_status_prepare(struct wt_status *s)
122+
void wt_status_prepare(struct repository *r, struct wt_status *s)
123123
{
124124
memset(s, 0, sizeof(*s));
125+
s->repo = r;
125126
memcpy(s->color_palette, default_wt_status_colors,
126127
sizeof(default_wt_status_colors));
127128
s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
@@ -494,19 +495,19 @@ static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
494495
}
495496
}
496497

497-
static int unmerged_mask(const char *path)
498+
static int unmerged_mask(struct index_state *istate, const char *path)
498499
{
499500
int pos, mask;
500501
const struct cache_entry *ce;
501502

502-
pos = cache_name_pos(path, strlen(path));
503+
pos = index_name_pos(istate, path, strlen(path));
503504
if (0 <= pos)
504505
return 0;
505506

506507
mask = 0;
507508
pos = -pos-1;
508-
while (pos < active_nr) {
509-
ce = active_cache[pos++];
509+
while (pos < istate->cache_nr) {
510+
ce = istate->cache[pos++];
510511
if (strcmp(ce->name, path) || !ce_stage(ce))
511512
break;
512513
mask |= (1 << (ce_stage(ce) - 1));
@@ -566,7 +567,8 @@ static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
566567
s->committable = 1;
567568
break;
568569
case DIFF_STATUS_UNMERGED:
569-
d->stagemask = unmerged_mask(p->two->path);
570+
d->stagemask = unmerged_mask(s->repo->index,
571+
p->two->path);
570572
/*
571573
* Don't bother setting {mode,oid}_{head,index} since the print
572574
* code will output the stage values directly and not use the
@@ -585,7 +587,7 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)
585587
{
586588
struct rev_info rev;
587589

588-
repo_init_revisions(the_repository, &rev, NULL);
590+
repo_init_revisions(s->repo, &rev, NULL);
589591
setup_revisions(0, NULL, &rev, NULL);
590592
rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
591593
rev.diffopt.flags.dirty_submodules = 1;
@@ -610,7 +612,7 @@ static void wt_status_collect_changes_index(struct wt_status *s)
610612
struct rev_info rev;
611613
struct setup_revision_opt opt;
612614

613-
repo_init_revisions(the_repository, &rev, NULL);
615+
repo_init_revisions(s->repo, &rev, NULL);
614616
memset(&opt, 0, sizeof(opt));
615617
opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
616618
setup_revisions(0, NULL, &rev, &opt);
@@ -643,14 +645,15 @@ static void wt_status_collect_changes_index(struct wt_status *s)
643645

644646
static void wt_status_collect_changes_initial(struct wt_status *s)
645647
{
648+
struct index_state *istate = s->repo->index;
646649
int i;
647650

648-
for (i = 0; i < active_nr; i++) {
651+
for (i = 0; i < istate->cache_nr; i++) {
649652
struct string_list_item *it;
650653
struct wt_status_change_data *d;
651-
const struct cache_entry *ce = active_cache[i];
654+
const struct cache_entry *ce = istate->cache[i];
652655

653-
if (!ce_path_match(&the_index, ce, &s->pathspec, NULL))
656+
if (!ce_path_match(istate, ce, &s->pathspec, NULL))
654657
continue;
655658
if (ce_intent_to_add(ce))
656659
continue;
@@ -684,6 +687,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
684687
int i;
685688
struct dir_struct dir;
686689
uint64_t t_begin = getnanotime();
690+
struct index_state *istate = s->repo->index;
687691

688692
if (!s->show_untracked_files)
689693
return;
@@ -698,25 +702,25 @@ static void wt_status_collect_untracked(struct wt_status *s)
698702
if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
699703
dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
700704
} else {
701-
dir.untracked = the_index.untracked;
705+
dir.untracked = istate->untracked;
702706
}
703707

704708
setup_standard_excludes(&dir);
705709

706-
fill_directory(&dir, &the_index, &s->pathspec);
710+
fill_directory(&dir, istate, &s->pathspec);
707711

708712
for (i = 0; i < dir.nr; i++) {
709713
struct dir_entry *ent = dir.entries[i];
710-
if (cache_name_is_other(ent->name, ent->len) &&
711-
dir_path_match(&the_index, ent, &s->pathspec, 0, NULL))
714+
if (index_name_is_other(istate, ent->name, ent->len) &&
715+
dir_path_match(istate, ent, &s->pathspec, 0, NULL))
712716
string_list_insert(&s->untracked, ent->name);
713717
free(ent);
714718
}
715719

716720
for (i = 0; i < dir.ignored_nr; i++) {
717721
struct dir_entry *ent = dir.ignored[i];
718-
if (cache_name_is_other(ent->name, ent->len) &&
719-
dir_path_match(&the_index, ent, &s->pathspec, 0, NULL))
722+
if (index_name_is_other(istate, ent->name, ent->len) &&
723+
dir_path_match(istate, ent, &s->pathspec, 0, NULL))
720724
string_list_insert(&s->ignored, ent->name);
721725
free(ent);
722726
}
@@ -1009,7 +1013,7 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
10091013
int dirty_submodules;
10101014
const char *c = color(WT_STATUS_HEADER, s);
10111015

1012-
repo_init_revisions(the_repository, &rev, NULL);
1016+
repo_init_revisions(s->repo, &rev, NULL);
10131017
rev.diffopt.flags.allow_textconv = 1;
10141018
rev.diffopt.ita_invisible_in_index = 1;
10151019

@@ -1326,7 +1330,7 @@ static void show_rebase_in_progress(struct wt_status *s,
13261330
_(" (use \"git rebase --abort\" to check out the original branch)"));
13271331
}
13281332
} else if (s->state.rebase_in_progress ||
1329-
!stat(git_path_merge_msg(the_repository), &st)) {
1333+
!stat(git_path_merge_msg(s->repo), &st)) {
13301334
print_rebase_state(s, color);
13311335
if (s->hints)
13321336
status_printf_ln(s, color,
@@ -2135,6 +2139,7 @@ static void wt_porcelain_v2_print_unmerged_entry(
21352139
struct wt_status *s)
21362140
{
21372141
struct wt_status_change_data *d = it->util;
2142+
struct index_state *istate = s->repo->index;
21382143
const struct cache_entry *ce;
21392144
struct strbuf buf_index = STRBUF_INIT;
21402145
const char *path_index = NULL;
@@ -2173,11 +2178,11 @@ static void wt_porcelain_v2_print_unmerged_entry(
21732178
*/
21742179
memset(stages, 0, sizeof(stages));
21752180
sum = 0;
2176-
pos = cache_name_pos(it->string, strlen(it->string));
2181+
pos = index_name_pos(istate, it->string, strlen(it->string));
21772182
assert(pos < 0);
21782183
pos = -pos-1;
2179-
while (pos < active_nr) {
2180-
ce = active_cache[pos++];
2184+
while (pos < istate->cache_nr) {
2185+
ce = istate->cache[pos++];
21812186
stage = ce_stage(ce);
21822187
if (strcmp(ce->name, it->string) || !stage)
21832188
break;
@@ -2302,12 +2307,12 @@ void wt_status_print(struct wt_status *s)
23022307
/**
23032308
* Returns 1 if there are unstaged changes, 0 otherwise.
23042309
*/
2305-
int has_unstaged_changes(int ignore_submodules)
2310+
int has_unstaged_changes(struct repository *r, int ignore_submodules)
23062311
{
23072312
struct rev_info rev_info;
23082313
int result;
23092314

2310-
repo_init_revisions(the_repository, &rev_info, NULL);
2315+
repo_init_revisions(r, &rev_info, NULL);
23112316
if (ignore_submodules) {
23122317
rev_info.diffopt.flags.ignore_submodules = 1;
23132318
rev_info.diffopt.flags.override_submodule_config = 1;
@@ -2321,15 +2326,16 @@ int has_unstaged_changes(int ignore_submodules)
23212326
/**
23222327
* Returns 1 if there are uncommitted changes, 0 otherwise.
23232328
*/
2324-
int has_uncommitted_changes(int ignore_submodules)
2329+
int has_uncommitted_changes(struct repository *r,
2330+
int ignore_submodules)
23252331
{
23262332
struct rev_info rev_info;
23272333
int result;
23282334

2329-
if (is_cache_unborn())
2335+
if (is_index_unborn(r->index))
23302336
return 0;
23312337

2332-
repo_init_revisions(the_repository, &rev_info, NULL);
2338+
repo_init_revisions(r, &rev_info, NULL);
23332339
if (ignore_submodules)
23342340
rev_info.diffopt.flags.ignore_submodules = 1;
23352341
rev_info.diffopt.flags.quick = 1;
@@ -2340,7 +2346,7 @@ int has_uncommitted_changes(int ignore_submodules)
23402346
* We have no head (or it's corrupt); use the empty tree,
23412347
* which will complain if the index is non-empty.
23422348
*/
2343-
struct tree *tree = lookup_tree(the_repository, the_hash_algo->empty_tree);
2349+
struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
23442350
add_pending_object(&rev_info, &tree->object, "");
23452351
}
23462352

@@ -2353,24 +2359,28 @@ int has_uncommitted_changes(int ignore_submodules)
23532359
* If the work tree has unstaged or uncommitted changes, dies with the
23542360
* appropriate message.
23552361
*/
2356-
int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
2362+
int require_clean_work_tree(struct repository *r,
2363+
const char *action,
2364+
const char *hint,
2365+
int ignore_submodules,
2366+
int gently)
23572367
{
23582368
struct lock_file lock_file = LOCK_INIT;
23592369
int err = 0, fd;
23602370

23612371
fd = hold_locked_index(&lock_file, 0);
2362-
refresh_cache(REFRESH_QUIET);
2372+
refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
23632373
if (0 <= fd)
2364-
update_index_if_able(&the_index, &lock_file);
2374+
update_index_if_able(r->index, &lock_file);
23652375
rollback_lock_file(&lock_file);
23662376

2367-
if (has_unstaged_changes(ignore_submodules)) {
2377+
if (has_unstaged_changes(r, ignore_submodules)) {
23682378
/* TRANSLATORS: the action is e.g. "pull with rebase" */
23692379
error(_("cannot %s: You have unstaged changes."), _(action));
23702380
err = 1;
23712381
}
23722382

2373-
if (has_uncommitted_changes(ignore_submodules)) {
2383+
if (has_uncommitted_changes(r, ignore_submodules)) {
23742384
if (err)
23752385
error(_("additionally, your index contains uncommitted changes."));
23762386
else

wt-status.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pathspec.h"
88
#include "remote.h"
99

10+
struct repository;
1011
struct worktree;
1112

1213
enum color_wt_status {
@@ -83,6 +84,7 @@ struct wt_status_state {
8384
};
8485

8586
struct wt_status {
87+
struct repository *repo;
8688
int is_initial;
8789
char *branch;
8890
const char *reference;
@@ -128,7 +130,7 @@ struct wt_status {
128130

129131
size_t wt_status_locate_end(const char *s, size_t len);
130132
void wt_status_add_cut_line(FILE *fp);
131-
void wt_status_prepare(struct wt_status *s);
133+
void wt_status_prepare(struct repository *r, struct wt_status *s);
132134
void wt_status_print(struct wt_status *s);
133135
void wt_status_collect(struct wt_status *s);
134136
void wt_status_collect_free_buffers(struct wt_status *s);
@@ -144,9 +146,14 @@ __attribute__((format (printf, 3, 4)))
144146
void status_printf(struct wt_status *s, const char *color, const char *fmt, ...);
145147

146148
/* The following functions expect that the caller took care of reading the index. */
147-
int has_unstaged_changes(int ignore_submodules);
148-
int has_uncommitted_changes(int ignore_submodules);
149-
int require_clean_work_tree(const char *action, const char *hint,
150-
int ignore_submodules, int gently);
149+
int has_unstaged_changes(struct repository *repo,
150+
int ignore_submodules);
151+
int has_uncommitted_changes(struct repository *repo,
152+
int ignore_submodules);
153+
int require_clean_work_tree(struct repository *repo,
154+
const char *action,
155+
const char *hint,
156+
int ignore_submodules,
157+
int gently);
151158

152159
#endif /* STATUS_H */

0 commit comments

Comments
 (0)