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 <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Jun 30, 2018 at 18:24 UTC 1b9fbefbe08ff2bbcbb8a6089867b32fca603eac
4 files changed +17 -13
builtin/am.c
+3 -3
@@ -1763,7 +1763,7 @@ static void am_run(struct am_state *state, int resume)
1763
1764 refresh_and_write_cache();
1765
1766 - if (index_has_changes(&sb)) {
1766 + if (index_has_changes(&the_index, &sb)) {
1767 write_state_bool(state, "dirtyindex", 1);
1768 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1769 }
@@ -1820,7 +1820,7 @@ static void am_run(struct am_state *state, int resume)
1820 * Applying the patch to an earlier tree and merging
1821 * the result may have produced the same tree as ours.
1822 */
1823 - if (!apply_status && !index_has_changes(NULL)) {
1823 + if (!apply_status && !index_has_changes(&the_index, NULL)) {
1824 say(state, stdout, _("No changes -- Patch already applied."));
1825 goto next;
1826 }
@@ -1878,7 +1878,7 @@ static void am_resolve(struct am_state *state)
1878
1879 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1880
1881 - if (!index_has_changes(NULL)) {
1881 + if (!index_has_changes(&the_index, NULL)) {
1882 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1883 "If there is nothing left to stage, chances are that something else\n"
1884 "already introduced the same changes; you might want to skip this patch."));
cache.h
+6 -5
@@ -627,12 +627,13 @@ extern void move_index_extensions(struct index_state *dst, struct index_state *s
627 extern int unmerged_index(const struct index_state *);
628
629 /**
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.
634 */
635 -extern int index_has_changes(struct strbuf *sb);
635 +extern int index_has_changes(const struct index_state *istate,
636 + struct strbuf *sb);
637
638 extern int verify_path(const char *path, unsigned mode);
639 extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
merge-recursive.c
+1 -1
@@ -1983,7 +1983,7 @@ int merge_trees(struct merge_options *o,
1983 if (oid_eq(&common->object.oid, &merge->object.oid)) {
1984 struct strbuf sb = STRBUF_INIT;
1985
1986 - if (!o->call_depth && index_has_changes(&sb)) {
1986 + if (!o->call_depth && index_has_changes(&the_index, &sb)) {
1987 err(o, _("Dirty index: cannot merge (dirty: %s)"),
1988 sb.buf);
1989 return 0;
read-cache.c
+7 -4
@@ -1986,11 +1986,14 @@ int unmerged_index(const struct index_state *istate)
1986 return 0;
1987 }
1988
1989 -int index_has_changes(struct strbuf *sb)
1989 +int index_has_changes(const struct index_state *istate, struct strbuf *sb)
1990 {
1991 struct object_id head;
1992 int i;
1993
1994 + if (istate != &the_index) {
1995 + BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
1996 + }
1997 if (!get_oid_tree("HEAD", &head)) {
1998 struct diff_options opt;
1999
@@ -2008,12 +2011,12 @@ int index_has_changes(struct strbuf *sb)
2011 diff_flush(&opt);
2012 return opt.flags.has_changes != 0;
2013 } else {
2011 - for (i = 0; sb && i < the_index.cache_nr; i++) {
2014 + for (i = 0; sb && i < istate->cache_nr; i++) {
2015 if (i)
2016 strbuf_addch(sb, ' ');
2014 - strbuf_addstr(sb, the_index.cache[i]->name);
2017 + strbuf_addstr(sb, istate->cache[i]->name);
2018 }
2016 - return !!the_index.cache_nr;
2019 + return !!istate->cache_nr;
2020 }
2021 }
2022