]> git.ipfire.org Git - thirdparty/git.git/commitdiff
add-patch: fix inverted return code of repo_read_index()
authorJeff King <peff@peff.net>
Mon, 7 Sep 2020 08:08:53 +0000 (04:08 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 8 Sep 2020 21:48:29 +0000 (14:48 -0700)
After applying hunks to a file with "add -p", the C patch_update_file()
function tries to refresh the index (just like the perl version does).
We can only refresh the index if we're able to read it in, so we first
check the return value of repo_read_index(). But unlike many functions,
where "0" is success, that function is documented to return the number
of entries in the index.  Hence we should be checking for success with a
non-negative return value.

Neither the tests nor any users seem to have noticed this, probably due
to a combination of:

  - this affects only the C version, which is not yet the default

  - following it up with any porcelain command like "git diff" or "git
    commit" would refresh the index automatically.

But you can see the problem by running the plumbing "git diff-files"
immediately after "add -p" stages all hunks. Running the new test with
GIT_TEST_ADD_I_USE_BUILTIN=1 fails without the matching code change.

Signed-off-by: Jeff King <peff@peff.net>
Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
add-patch.c
t/t3701-add-interactive.sh

index f899389e2cc2359d255f9a74430ef2a995fba004..e6fa10715d44b09d5c0c0154762d2934c43a835c 100644 (file)
@@ -1624,7 +1624,7 @@ soft_increment:
                                         NULL, 0, NULL, 0))
                                error(_("'git apply' failed"));
                }
-               if (!repo_read_index(s->s.r))
+               if (repo_read_index(s->s.r) >= 0)
                        repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
                                                     1, NULL, NULL, NULL);
        }
index 49decbac711386b497b19f22d16324422c0609b5..f0a491ade189eb3b017cb5adf6957f6fc4a3a601 100755 (executable)
@@ -560,6 +560,13 @@ test_expect_success 'patch mode ignores unmerged entries' '
        diff_cmp expected diff
 '
 
+test_expect_success 'index is refreshed after applying patch' '
+       git reset --hard &&
+       echo content >test &&
+       printf y | git add -p &&
+       git diff-files --exit-code
+'
+
 test_expect_success 'diffs can be colorized' '
        git reset --hard &&