git-apply: prepare for upcoming GNU diff -u format change.
The latest GNU diff from CVS emits an empty line to express an empty context line, instead of more traditional "single white space followed by a newline". Do not get broken by it. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Oct 19, 2006 at 19:26 UTC
b507b465f7831612b9d9fc643e3e5218b64e5bfa
2 files changed
+64
builtin-apply.c
+9
@@ -934,6 +934,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
934
switch (*line) {
935
default:
936
return -1;
937
+ case '\n': /* newer GNU diff, an empty context line */
938
case ' ':
939
oldlines--;
940
newlines--;
@@ -1623,6 +1624,14 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1624
first = '-';
1625
}
1626
switch (first) {
1627
+ case '\n':
1628
+ /* Newer GNU diff, empty context line */
1629
+ if (plen < 0)
1630
+ /* ... followed by '\No newline'; nothing */
1631
+ break;
1632
+ old[oldsize++] = '\n';
1633
+ new[newsize++] = '\n';
1634
+ break;
1635
case ' ':
1636
case '-':
1637
memcpy(old + oldsize, patch + 1, plen);
t/t4118-apply-empty-context.sh
new
+55
@@ -0,0 +1,55 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2006 Junio C Hamano
4
+#
5
+
6
+test_description='git-apply with new style GNU diff with empty context
7
+
8
+'
9
+
10
+. ./test-lib.sh
11
+
12
+test_expect_success setup '
13
+ {
14
+ echo; echo;
15
+ echo A; echo B; echo C;
16
+ echo;
17
+ } >file1 &&
18
+ cat file1 >file1.orig &&
19
+ {
20
+ cat file1 &&
21
+ echo Q | tr -d "\\012"
22
+ } >file2 &&
23
+ cat file2 >file2.orig
24
+ git add file1 file2 &&
25
+ sed -e "/^B/d" <file1.orig >file1 &&
26
+ sed -e "/^B/d" <file2.orig >file2 &&
27
+ cat file1 >file1.mods &&
28
+ cat file2 >file2.mods &&
29
+ git diff |
30
+ sed -e "s/^ \$//" >diff.output
31
+'
32
+
33
+test_expect_success 'apply --numstat' '
34
+
35
+ git apply --numstat diff.output >actual &&
36
+ {
37
+ echo "0 1 file1" &&
38
+ echo "0 1 file2"
39
+ } >expect &&
40
+ diff -u expect actual
41
+
42
+'
43
+
44
+test_expect_success 'apply --apply' '
45
+
46
+ cat file1.orig >file1 &&
47
+ cat file2.orig >file2 &&
48
+ git update-index file1 file2 &&
49
+ git apply --index diff.output &&
50
+ diff -u file1.mods file1 &&
51
+ diff -u file2.mods file2
52
+'
53
+
54
+test_done
55
+