index_has_changes(): avoid assuming operating on the_index
authorElijah Newren <newren@gmail.com>
Sun, 1 Jul 2018 01:24:56 +0000 (18:24 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Jul 2018 20:13:18 +0000 (13:13 -0700)
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>
builtin/am.c
cache.h
merge-recursive.c
read-cache.c
index 1151b5c73aec81dbfca36a799cd4049429c75973..2d800568ac6e63e6542bdb53780fcb7640b0bd49 100644 (file)
@@ -1763,7 +1763,7 @@ static void am_run(struct am_state *state, int resume)
 
        refresh_and_write_cache();
 
-       if (index_has_changes(&sb)) {
+       if (index_has_changes(&the_index, &sb)) {
                write_state_bool(state, "dirtyindex", 1);
                die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
        }
@@ -1820,7 +1820,7 @@ static void am_run(struct am_state *state, int resume)
                         * Applying the patch to an earlier tree and merging
                         * the result may have produced the same tree as ours.
                         */
-                       if (!apply_status && !index_has_changes(NULL)) {
+                       if (!apply_status && !index_has_changes(&the_index, NULL)) {
                                say(state, stdout, _("No changes -- Patch already applied."));
                                goto next;
                        }
@@ -1878,7 +1878,7 @@ static void am_resolve(struct am_state *state)
 
        say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
 
-       if (!index_has_changes(NULL)) {
+       if (!index_has_changes(&the_index, NULL)) {
                printf_ln(_("No changes - did you forget to use 'git add'?\n"
                        "If there is nothing left to stage, chances are that something else\n"
                        "already introduced the same changes; you might want to skip this patch."));
diff --git a/cache.h b/cache.h
index 0323853c99e75a1edc2e4415db9490ae36bd2c0f..169c809c6c4321f7a18aefb92c06a050d45afcdf 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -627,12 +627,13 @@ extern void move_index_extensions(struct index_state *dst, struct index_state *s
 extern int unmerged_index(const struct index_state *);
 
 /**
- * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
- * branch, returns 1 if there are entries in the index, 0 otherwise. If an
- * strbuf is provided, the space-separated list of files that differ will be
- * appended to it.
+ * Returns 1 if istate differs from HEAD, 0 otherwise.  When on an unborn
+ * branch, returns 1 if there are entries in istate, 0 otherwise.  If an
+ * strbuf is provided, the space-separated list of files that differ will
+ * be appended to it.
  */
-extern int index_has_changes(struct strbuf *sb);
+extern int index_has_changes(const struct index_state *istate,
+                            struct strbuf *sb);
 
 extern int verify_path(const char *path, unsigned mode);
 extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
index 869092f7b9bc0ce1104a1c43c7201acdee2eb4f8..32529ca5b6afec8129be20037ee85cf8d8cdf5c2 100644 (file)
@@ -1983,7 +1983,7 @@ int merge_trees(struct merge_options *o,
        if (oid_eq(&common->object.oid, &merge->object.oid)) {
                struct strbuf sb = STRBUF_INIT;
 
-               if (!o->call_depth && index_has_changes(&sb)) {
+               if (!o->call_depth && index_has_changes(&the_index, &sb)) {
                        err(o, _("Dirty index: cannot merge (dirty: %s)"),
                            sb.buf);
                        return 0;
index ba094fd48553efdad083b83df11fa30ac6c282cf..f333a517f7d674e95b42ccab9808f276438e64b9 100644 (file)
@@ -1986,11 +1986,14 @@ int unmerged_index(const struct index_state *istate)
        return 0;
 }
 
-int index_has_changes(struct strbuf *sb)
+int index_has_changes(const struct index_state *istate, struct strbuf *sb)
 {
        struct object_id head;
        int i;
 
+       if (istate != &the_index) {
+               BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
+       }
        if (!get_oid_tree("HEAD", &head)) {
                struct diff_options opt;
 
@@ -2008,12 +2011,12 @@ int index_has_changes(struct strbuf *sb)
                diff_flush(&opt);
                return opt.flags.has_changes != 0;
        } else {
-               for (i = 0; sb && i < the_index.cache_nr; i++) {
+               for (i = 0; sb && i < istate->cache_nr; i++) {
                        if (i)
                                strbuf_addch(sb, ' ');
-                       strbuf_addstr(sb, the_index.cache[i]->name);
+                       strbuf_addstr(sb, istate->cache[i]->name);
                }
-               return !!the_index.cache_nr;
+               return !!istate->cache_nr;
        }
 }