Skip to content

Commit 1b9fbef

Browse files
newrengitster
authored andcommitted
index_has_changes(): avoid assuming operating on the_index
Modify index_has_changes() to take a struct istate* instead of just operating on the_index. This is only a partial conversion, though, because we call do_diff_cache() which implicitly assumes work is to be done on the_index. Ongoing work is being done elsewhere to do the remainder of the conversion, and thus is not duplicated here. Instead, a simple check is put in place until that work is complete. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cffbfad commit 1b9fbef

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

builtin/am.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ static void am_run(struct am_state *state, int resume)
17631763

17641764
refresh_and_write_cache();
17651765

1766-
if (index_has_changes(&sb)) {
1766+
if (index_has_changes(&the_index, &sb)) {
17671767
write_state_bool(state, "dirtyindex", 1);
17681768
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
17691769
}
@@ -1820,7 +1820,7 @@ static void am_run(struct am_state *state, int resume)
18201820
* Applying the patch to an earlier tree and merging
18211821
* the result may have produced the same tree as ours.
18221822
*/
1823-
if (!apply_status && !index_has_changes(NULL)) {
1823+
if (!apply_status && !index_has_changes(&the_index, NULL)) {
18241824
say(state, stdout, _("No changes -- Patch already applied."));
18251825
goto next;
18261826
}
@@ -1878,7 +1878,7 @@ static void am_resolve(struct am_state *state)
18781878

18791879
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
18801880

1881-
if (!index_has_changes(NULL)) {
1881+
if (!index_has_changes(&the_index, NULL)) {
18821882
printf_ln(_("No changes - did you forget to use 'git add'?\n"
18831883
"If there is nothing left to stage, chances are that something else\n"
18841884
"already introduced the same changes; you might want to skip this patch."));

cache.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,13 @@ extern void move_index_extensions(struct index_state *dst, struct index_state *s
627627
extern int unmerged_index(const struct index_state *);
628628

629629
/**
630-
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
631-
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
632-
* strbuf is provided, the space-separated list of files that differ will be
633-
* appended to it.
630+
* Returns 1 if istate differs from HEAD, 0 otherwise. When on an unborn
631+
* branch, returns 1 if there are entries in istate, 0 otherwise. If an
632+
* strbuf is provided, the space-separated list of files that differ will
633+
* be appended to it.
634634
*/
635-
extern int index_has_changes(struct strbuf *sb);
635+
extern int index_has_changes(const struct index_state *istate,
636+
struct strbuf *sb);
636637

637638
extern int verify_path(const char *path, unsigned mode);
638639
extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ int merge_trees(struct merge_options *o,
19831983
if (oid_eq(&common->object.oid, &merge->object.oid)) {
19841984
struct strbuf sb = STRBUF_INIT;
19851985

1986-
if (!o->call_depth && index_has_changes(&sb)) {
1986+
if (!o->call_depth && index_has_changes(&the_index, &sb)) {
19871987
err(o, _("Dirty index: cannot merge (dirty: %s)"),
19881988
sb.buf);
19891989
return 0;

read-cache.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,11 +1986,14 @@ int unmerged_index(const struct index_state *istate)
19861986
return 0;
19871987
}
19881988

1989-
int index_has_changes(struct strbuf *sb)
1989+
int index_has_changes(const struct index_state *istate, struct strbuf *sb)
19901990
{
19911991
struct object_id head;
19921992
int i;
19931993

1994+
if (istate != &the_index) {
1995+
BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
1996+
}
19941997
if (!get_oid_tree("HEAD", &head)) {
19951998
struct diff_options opt;
19961999

@@ -2008,12 +2011,12 @@ int index_has_changes(struct strbuf *sb)
20082011
diff_flush(&opt);
20092012
return opt.flags.has_changes != 0;
20102013
} else {
2011-
for (i = 0; sb && i < the_index.cache_nr; i++) {
2014+
for (i = 0; sb && i < istate->cache_nr; i++) {
20122015
if (i)
20132016
strbuf_addch(sb, ' ');
2014-
strbuf_addstr(sb, the_index.cache[i]->name);
2017+
strbuf_addstr(sb, istate->cache[i]->name);
20152018
}
2016-
return !!the_index.cache_nr;
2019+
return !!istate->cache_nr;
20172020
}
20182021
}
20192022

0 commit comments

Comments
 (0)