builtin-apply: do not declare patch is creation when we do not know it
When we see no context nor deleted line in the patch, we used to declare that the patch creates a new file. But some people create an empty file and then apply a patch to it. Similarly, a patch that delete everything is not a deletion patch either. This commit corrects these two issues. Together with the previous commit, it allows a diff between an empty file and a line-ful file to be treated as both creation patch and "add stuff to an existing empty file", depending on the context. A new test t4126 demonstrates the fix. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
May 17, 2008 at 02:02 UTC
032bea55a3fda805382398020657f738b8176729
2 files changed
+61
-15
builtin-apply.c
-15
@@ -1143,21 +1143,6 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
1143
if (patch->is_delete < 0 &&
1144
(newlines || (patch->fragments && patch->fragments->next)))
1145
patch->is_delete = 0;
1146
- if (!unidiff_zero || context) {
1147
- /* If the user says the patch is not generated with
1148
- * --unified=0, or if we have seen context lines,
1149
- * then not having oldlines means the patch is creation,
1150
- * and not having newlines means the patch is deletion.
1151
- */
1152
- if (patch->is_new < 0 && !oldlines) {
1153
- patch->is_new = 1;
1154
- patch->old_name = NULL;
1155
- }
1156
- if (patch->is_delete < 0 && !newlines) {
1157
- patch->is_delete = 1;
1158
- patch->new_name = NULL;
1159
- }
1160
- }
1146
1147
if (0 < patch->is_new && oldlines)
1148
die("new file %s depends on old contents", patch->new_name);
t/t4126-apply-empty.sh
new
+61
@@ -0,0 +1,61 @@
1
+#!/bin/sh
2
+
3
+test_description='apply empty'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success setup '
8
+ >empty &&
9
+ git add empty &&
10
+ test_tick &&
11
+ git commit -m initial &&
12
+ for i in a b c d e
13
+ do
14
+ echo $i
15
+ done >empty &&
16
+ cat empty >expect &&
17
+ git diff |
18
+ sed -e "/^diff --git/d" \
19
+ -e "/^index /d" \
20
+ -e "s|a/empty|empty.orig|" \
21
+ -e "s|b/empty|empty|" >patch0 &&
22
+ sed -e "s|empty|missing|" patch0 >patch1 &&
23
+ >empty &&
24
+ git update-index --refresh
25
+'
26
+
27
+test_expect_success 'apply empty' '
28
+ git reset --hard &&
29
+ >empty &&
30
+ rm -f missing &&
31
+ git apply patch0 &&
32
+ test_cmp expect empty
33
+'
34
+
35
+test_expect_success 'apply --index empty' '
36
+ git reset --hard &&
37
+ >empty &&
38
+ rm -f missing &&
39
+ git apply --index patch0 &&
40
+ test_cmp expect empty &&
41
+ git diff --exit-code
42
+'
43
+
44
+test_expect_success 'apply create' '
45
+ git reset --hard &&
46
+ >empty &&
47
+ rm -f missing &&
48
+ git apply patch1 &&
49
+ test_cmp expect missing
50
+'
51
+
52
+test_expect_success 'apply --index create' '
53
+ git reset --hard &&
54
+ >empty &&
55
+ rm -f missing &&
56
+ git apply --index patch1 &&
57
+ test_cmp expect missing &&
58
+ git diff --exit-code
59
+'
60
+
61
+test_done