Skip to content

Commit de4bc2b

Browse files
author
Max Kostyukevich
committed
apply: git_apply_to_tree fails to apply patches that add new files
git_apply_to_tree() cannot be used apply patches with new files. An attempt to apply such a patch fails because git_apply_to_tree() tries to remove a non-existing file from an old index. The solution is to modify git_apply_to_tree() to git_index_remove() when the patch states that the modified files is removed.
1 parent 0f40e68 commit de4bc2b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/apply.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,12 @@ int git_apply_to_tree(
649649
for (i = 0; i < git_diff_num_deltas(diff); i++) {
650650
delta = git_diff_get_delta(diff, i);
651651

652-
if ((error = git_index_remove(postimage,
653-
delta->old_file.path, 0)) < 0)
654-
goto done;
652+
if (delta->status == GIT_DELTA_DELETED ||
653+
delta->status == GIT_DELTA_RENAMED) {
654+
if ((error = git_index_remove(postimage,
655+
delta->old_file.path, 0)) < 0)
656+
goto done;
657+
}
655658
}
656659

657660
if ((error = apply_deltas(repo, pre_reader, NULL, post_reader, postimage, diff, &opts)) < 0)

0 commit comments

Comments
 (0)