@matheusbsbs / matheus-bagliano / commits / 7f95aef28f
Fork 0

apply: handle type-changing patch correctly.

A type-change diff is always split into a patch to delete old, immediately followed by a patch to create new. check_patch() routine noticed that the path to be created already exists in the working tree and/or in the index when looking at the creation patch and mistakenly thought it to be an error. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jul 17, 2006 at 00:10 UTC 7f95aef28fa1e2662aebb4556c71ad6912d395e5
1 file changed +24 -5
builtin-apply.c
+24 -5
@@ -1664,13 +1664,14 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1664 return 0;
1665 }
1666
1667 -static int check_patch(struct patch *patch)
1667 +static int check_patch(struct patch *patch, struct patch *prev_patch)
1668 {
1669 struct stat st;
1670 const char *old_name = patch->old_name;
1671 const char *new_name = patch->new_name;
1672 const char *name = old_name ? old_name : new_name;
1673 struct cache_entry *ce = NULL;
1674 + int ok_if_exists;
1675
1676 if (old_name) {
1677 int changed = 0;
@@ -1728,13 +1729,28 @@ static int check_patch(struct patch *patch)
1729 old_name, st_mode, patch->old_mode);
1730 }
1731
1732 + if (new_name && prev_patch && prev_patch->is_delete &&
1733 + !strcmp(prev_patch->old_name, new_name))
1734 + /* A type-change diff is always split into a patch to
1735 + * delete old, immediately followed by a patch to
1736 + * create new (see diff.c::run_diff()); in such a case
1737 + * it is Ok that the entry to be deleted by the
1738 + * previous patch is still in the working tree and in
1739 + * the index.
1740 + */
1741 + ok_if_exists = 1;
1742 + else
1743 + ok_if_exists = 0;
1744 +
1745 if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
1732 - if (check_index && cache_name_pos(new_name, strlen(new_name)) >= 0)
1746 + if (check_index &&
1747 + cache_name_pos(new_name, strlen(new_name)) >= 0 &&
1748 + !ok_if_exists)
1749 return error("%s: already exists in index", new_name);
1750 if (!cached) {
1751 struct stat nst;
1752 if (!lstat(new_name, &nst)) {
1737 - if (S_ISDIR(nst.st_mode))
1753 + if (S_ISDIR(nst.st_mode) || ok_if_exists)
1754 ; /* ok */
1755 else
1756 return error("%s: already exists in working directory", new_name);
@@ -1767,10 +1783,13 @@ static int check_patch(struct patch *patch)
1783
1784 static int check_patch_list(struct patch *patch)
1785 {
1786 + struct patch *prev_patch = NULL;
1787 int error = 0;
1788
1772 - for (;patch ; patch = patch->next)
1773 - error |= check_patch(patch);
1789 + for (prev_patch = NULL; patch ; patch = patch->next) {
1790 + error |= check_patch(patch, prev_patch);
1791 + prev_patch = patch;
1792 + }
1793 return error;
1794 }
1795