wt-status.c: remove implicit dependency on the_index
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
Nov 10, 2018 at 06:48 UTC
5b02ca38a30298a963b3595f2cd884e11cf10c09
6 files changed
+66
-47
builtin/commit.c
+1
-1
@@ -185,7 +185,7 @@ static void determine_whence(struct wt_status *s)
185
186
static void status_init_config(struct wt_status *s, config_fn_t fn)
187
{
188
- wt_status_prepare(s);
188
+ wt_status_prepare(the_repository, s);
189
init_diff_ui_defaults();
190
git_config(fn, s);
191
determine_whence(s);
builtin/pull.c
+2
-1
@@ -888,7 +888,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
888
die(_("Updating an unborn branch with changes added to the index."));
889
890
if (!autostash)
891
- require_clean_work_tree(N_("pull with rebase"),
891
+ require_clean_work_tree(the_repository,
892
+ N_("pull with rebase"),
893
_("please commit or stash them."), 1, 0);
894
895
if (get_rebase_fork_point(&rebase_fork_point, repo, *refspecs))
builtin/rebase.c
+4
-3
@@ -983,7 +983,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
983
&lock_file);
984
rollback_lock_file(&lock_file);
985
986
- if (has_unstaged_changes(1)) {
986
+ if (has_unstaged_changes(the_repository, 1)) {
987
puts(_("You must edit all merge conflicts and then\n"
988
"mark them as resolved using git add"));
989
exit(1);
@@ -1351,7 +1351,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1351
update_index_if_able(&the_index, &lock_file);
1352
rollback_lock_file(&lock_file);
1353
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)) {
1356
const char *autostash =
1357
state_dir_path("autostash", &options);
1358
struct child_process stash = CHILD_PROCESS_INIT;
@@ -1397,7 +1398,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1398
}
1399
}
1400
1400
- if (require_clean_work_tree("rebase",
1401
+ if (require_clean_work_tree(the_repository, "rebase",
1402
_("Please commit or stash them."), 1, 1)) {
1403
ret = 1;
1404
goto cleanup;
sequencer.c
+4
-4
@@ -2773,7 +2773,7 @@ static int do_exec(const char *command_line)
2773
if (discard_cache() < 0 || read_cache() < 0)
2774
return error(_("could not read index"));
2775
2776
- dirty = require_clean_work_tree("rebase", NULL, 1, 1);
2776
+ dirty = require_clean_work_tree(the_repository, "rebase", NULL, 1, 1);
2777
2778
if (status) {
2779
warning(_("execution failed: %s\n%s"
@@ -3714,10 +3714,10 @@ static int commit_staged_changes(struct replay_opts *opts,
3714
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
3715
unsigned int final_fixup = 0, is_clean;
3716
3717
- if (has_unstaged_changes(1))
3717
+ if (has_unstaged_changes(the_repository, 1))
3718
return error(_("cannot rebase: You have unstaged changes."));
3719
3720
- is_clean = !has_uncommitted_changes(0);
3720
+ is_clean = !has_uncommitted_changes(the_repository, 0);
3721
3722
if (file_exists(rebase_path_amend())) {
3723
struct strbuf rev = STRBUF_INIT;
@@ -4847,7 +4847,7 @@ int complete_action(struct replay_opts *opts, unsigned flags,
4847
if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
4848
return -1;
4849
;
4850
- if (require_clean_work_tree("rebase", "", 1, 1))
4850
+ if (require_clean_work_tree(the_repository, "rebase", "", 1, 1))
4851
return -1;
4852
4853
return sequencer_continue(opts);
wt-status.c
+43
-33
@@ -119,9 +119,10 @@ static void status_printf_more(struct wt_status *s, const char *color,
119
va_end(ap);
120
}
121
122
-void wt_status_prepare(struct wt_status *s)
122
+void wt_status_prepare(struct repository *r, struct wt_status *s)
123
{
124
memset(s, 0, sizeof(*s));
125
+ s->repo = r;
126
memcpy(s->color_palette, default_wt_status_colors,
127
sizeof(default_wt_status_colors));
128
s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
@@ -494,19 +495,19 @@ static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
495
}
496
}
497
497
-static int unmerged_mask(const char *path)
498
+static int unmerged_mask(struct index_state *istate, const char *path)
499
{
500
int pos, mask;
501
const struct cache_entry *ce;
502
502
- pos = cache_name_pos(path, strlen(path));
503
+ pos = index_name_pos(istate, path, strlen(path));
504
if (0 <= pos)
505
return 0;
506
507
mask = 0;
508
pos = -pos-1;
508
- while (pos < active_nr) {
509
- ce = active_cache[pos++];
509
+ while (pos < istate->cache_nr) {
510
+ ce = istate->cache[pos++];
511
if (strcmp(ce->name, path) || !ce_stage(ce))
512
break;
513
mask |= (1 << (ce_stage(ce) - 1));
@@ -566,7 +567,8 @@ static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
567
s->committable = 1;
568
break;
569
case DIFF_STATUS_UNMERGED:
569
- d->stagemask = unmerged_mask(p->two->path);
570
+ d->stagemask = unmerged_mask(s->repo->index,
571
+ p->two->path);
572
/*
573
* Don't bother setting {mode,oid}_{head,index} since the print
574
* 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)
587
{
588
struct rev_info rev;
589
588
- repo_init_revisions(the_repository, &rev, NULL);
590
+ repo_init_revisions(s->repo, &rev, NULL);
591
setup_revisions(0, NULL, &rev, NULL);
592
rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
593
rev.diffopt.flags.dirty_submodules = 1;
@@ -610,7 +612,7 @@ static void wt_status_collect_changes_index(struct wt_status *s)
612
struct rev_info rev;
613
struct setup_revision_opt opt;
614
613
- repo_init_revisions(the_repository, &rev, NULL);
615
+ repo_init_revisions(s->repo, &rev, NULL);
616
memset(&opt, 0, sizeof(opt));
617
opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
618
setup_revisions(0, NULL, &rev, &opt);
@@ -643,14 +645,15 @@ static void wt_status_collect_changes_index(struct wt_status *s)
645
646
static void wt_status_collect_changes_initial(struct wt_status *s)
647
{
648
+ struct index_state *istate = s->repo->index;
649
int i;
650
648
- for (i = 0; i < active_nr; i++) {
651
+ for (i = 0; i < istate->cache_nr; i++) {
652
struct string_list_item *it;
653
struct wt_status_change_data *d;
651
- const struct cache_entry *ce = active_cache[i];
654
+ const struct cache_entry *ce = istate->cache[i];
655
653
- if (!ce_path_match(&the_index, ce, &s->pathspec, NULL))
656
+ if (!ce_path_match(istate, ce, &s->pathspec, NULL))
657
continue;
658
if (ce_intent_to_add(ce))
659
continue;
@@ -684,6 +687,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
687
int i;
688
struct dir_struct dir;
689
uint64_t t_begin = getnanotime();
690
+ struct index_state *istate = s->repo->index;
691
692
if (!s->show_untracked_files)
693
return;
@@ -698,25 +702,25 @@ static void wt_status_collect_untracked(struct wt_status *s)
702
if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
703
dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
704
} else {
701
- dir.untracked = the_index.untracked;
705
+ dir.untracked = istate->untracked;
706
}
707
708
setup_standard_excludes(&dir);
709
706
- fill_directory(&dir, &the_index, &s->pathspec);
710
+ fill_directory(&dir, istate, &s->pathspec);
711
712
for (i = 0; i < dir.nr; i++) {
713
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))
716
string_list_insert(&s->untracked, ent->name);
717
free(ent);
718
}
719
720
for (i = 0; i < dir.ignored_nr; i++) {
721
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))
724
string_list_insert(&s->ignored, ent->name);
725
free(ent);
726
}
@@ -1009,7 +1013,7 @@ static void wt_longstatus_print_verbose(struct wt_status *s)
1013
int dirty_submodules;
1014
const char *c = color(WT_STATUS_HEADER, s);
1015
1012
- repo_init_revisions(the_repository, &rev, NULL);
1016
+ repo_init_revisions(s->repo, &rev, NULL);
1017
rev.diffopt.flags.allow_textconv = 1;
1018
rev.diffopt.ita_invisible_in_index = 1;
1019
@@ -1326,7 +1330,7 @@ static void show_rebase_in_progress(struct wt_status *s,
1330
_(" (use \"git rebase --abort\" to check out the original branch)"));
1331
}
1332
} 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)) {
1334
print_rebase_state(s, color);
1335
if (s->hints)
1336
status_printf_ln(s, color,
@@ -2135,6 +2139,7 @@ static void wt_porcelain_v2_print_unmerged_entry(
2139
struct wt_status *s)
2140
{
2141
struct wt_status_change_data *d = it->util;
2142
+ struct index_state *istate = s->repo->index;
2143
const struct cache_entry *ce;
2144
struct strbuf buf_index = STRBUF_INIT;
2145
const char *path_index = NULL;
@@ -2173,11 +2178,11 @@ static void wt_porcelain_v2_print_unmerged_entry(
2178
*/
2179
memset(stages, 0, sizeof(stages));
2180
sum = 0;
2176
- pos = cache_name_pos(it->string, strlen(it->string));
2181
+ pos = index_name_pos(istate, it->string, strlen(it->string));
2182
assert(pos < 0);
2183
pos = -pos-1;
2179
- while (pos < active_nr) {
2180
- ce = active_cache[pos++];
2184
+ while (pos < istate->cache_nr) {
2185
+ ce = istate->cache[pos++];
2186
stage = ce_stage(ce);
2187
if (strcmp(ce->name, it->string) || !stage)
2188
break;
@@ -2302,12 +2307,12 @@ void wt_status_print(struct wt_status *s)
2307
/**
2308
* Returns 1 if there are unstaged changes, 0 otherwise.
2309
*/
2305
-int has_unstaged_changes(int ignore_submodules)
2310
+int has_unstaged_changes(struct repository *r, int ignore_submodules)
2311
{
2312
struct rev_info rev_info;
2313
int result;
2314
2310
- repo_init_revisions(the_repository, &rev_info, NULL);
2315
+ repo_init_revisions(r, &rev_info, NULL);
2316
if (ignore_submodules) {
2317
rev_info.diffopt.flags.ignore_submodules = 1;
2318
rev_info.diffopt.flags.override_submodule_config = 1;
@@ -2321,15 +2326,16 @@ int has_unstaged_changes(int ignore_submodules)
2326
/**
2327
* Returns 1 if there are uncommitted changes, 0 otherwise.
2328
*/
2324
-int has_uncommitted_changes(int ignore_submodules)
2329
+int has_uncommitted_changes(struct repository *r,
2330
+ int ignore_submodules)
2331
{
2332
struct rev_info rev_info;
2333
int result;
2334
2329
- if (is_cache_unborn())
2335
+ if (is_index_unborn(r->index))
2336
return 0;
2337
2332
- repo_init_revisions(the_repository, &rev_info, NULL);
2338
+ repo_init_revisions(r, &rev_info, NULL);
2339
if (ignore_submodules)
2340
rev_info.diffopt.flags.ignore_submodules = 1;
2341
rev_info.diffopt.flags.quick = 1;
@@ -2340,7 +2346,7 @@ int has_uncommitted_changes(int ignore_submodules)
2346
* We have no head (or it's corrupt); use the empty tree,
2347
* which will complain if the index is non-empty.
2348
*/
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);
2350
add_pending_object(&rev_info, &tree->object, "");
2351
}
2352
@@ -2353,24 +2359,28 @@ int has_uncommitted_changes(int ignore_submodules)
2359
* If the work tree has unstaged or uncommitted changes, dies with the
2360
* appropriate message.
2361
*/
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)
2367
{
2368
struct lock_file lock_file = LOCK_INIT;
2369
int err = 0, fd;
2370
2371
fd = hold_locked_index(&lock_file, 0);
2362
- refresh_cache(REFRESH_QUIET);
2372
+ refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2373
if (0 <= fd)
2364
- update_index_if_able(&the_index, &lock_file);
2374
+ update_index_if_able(r->index, &lock_file);
2375
rollback_lock_file(&lock_file);
2376
2367
- if (has_unstaged_changes(ignore_submodules)) {
2377
+ if (has_unstaged_changes(r, ignore_submodules)) {
2378
/* TRANSLATORS: the action is e.g. "pull with rebase" */
2379
error(_("cannot %s: You have unstaged changes."), _(action));
2380
err = 1;
2381
}
2382
2373
- if (has_uncommitted_changes(ignore_submodules)) {
2383
+ if (has_uncommitted_changes(r, ignore_submodules)) {
2384
if (err)
2385
error(_("additionally, your index contains uncommitted changes."));
2386
else
wt-status.h
+12
-5
@@ -7,6 +7,7 @@
7
#include "pathspec.h"
8
#include "remote.h"
9
10
+struct repository;
11
struct worktree;
12
13
enum color_wt_status {
@@ -83,6 +84,7 @@ struct wt_status_state {
84
};
85
86
struct wt_status {
87
+ struct repository *repo;
88
int is_initial;
89
char *branch;
90
const char *reference;
@@ -128,7 +130,7 @@ struct wt_status {
130
131
size_t wt_status_locate_end(const char *s, size_t len);
132
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);
134
void wt_status_print(struct wt_status *s);
135
void wt_status_collect(struct wt_status *s);
136
void wt_status_collect_free_buffers(struct wt_status *s);
@@ -144,9 +146,14 @@ __attribute__((format (printf, 3, 4)))
146
void status_printf(struct wt_status *s, const char *color, const char *fmt, ...);
147
148
/* 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);
158
159
#endif /* STATUS_H */