Skip to content

Commit dc62641

Browse files
peffgitster
authored andcommitted
add-patch: fix inverted return code of repo_read_index()
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 <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit dc62641

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

add-patch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ static int patch_update_file(struct add_p_state *s,
16241624
NULL, 0, NULL, 0))
16251625
error(_("'git apply' failed"));
16261626
}
1627-
if (!repo_read_index(s->s.r))
1627+
if (repo_read_index(s->s.r) >= 0)
16281628
repo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,
16291629
1, NULL, NULL, NULL);
16301630
}

t/t3701-add-interactive.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ test_expect_success 'patch mode ignores unmerged entries' '
560560
diff_cmp expected diff
561561
'
562562

563+
test_expect_success 'index is refreshed after applying patch' '
564+
git reset --hard &&
565+
echo content >test &&
566+
printf y | git add -p &&
567+
git diff-files --exit-code
568+
'
569+
563570
test_expect_success 'diffs can be colorized' '
564571
git reset --hard &&
565572

0 commit comments

Comments
 (0)