@matheusbsbs / matheus-bagliano / commits / 99d698f1e7
Fork 0

builtin-rerere: more carefully find conflict markers

When a conflicting file contains a line that begin with "=======", rerere failed to parse conflict markers. This result to a wrong preimage file and an unexpected error for the user. The boundary between ours and theirs not just begin with 7 equals, but is followed by either a SP or a LF. This patch enforces parsing rules so that markers match in the right order, and when ambiguous, the command does not autoresolve the conflicted file. Especially because we are introducing rerere.autoupdate configuration (which is off by default for safety) that automatically stages the resolution made by rerere, it is necessary to make sure that we do not autoresolve when there is any ambiguity. Signed-off-by: Olivier Marin <dkr@freesurf.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Olivier Marin committed Jul 7, 2008 at 14:42 UTC 99d698f1e703754422f1dd780487ddbff3726dc3
2 files changed +33 -9
builtin-rerere.c
+13 -3
@@ -112,11 +112,17 @@ static int handle_file(const char *path,
112 strbuf_init(&one, 0);
113 strbuf_init(&two, 0);
114 while (fgets(buf, sizeof(buf), f)) {
115 - if (!prefixcmp(buf, "<<<<<<< "))
115 + if (!prefixcmp(buf, "<<<<<<< ")) {
116 + if (hunk)
117 + goto bad;
118 hunk = 1;
117 - else if (!prefixcmp(buf, "======="))
119 + } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
120 + if (hunk != 1)
121 + goto bad;
122 hunk = 2;
119 - else if (!prefixcmp(buf, ">>>>>>> ")) {
123 + } else if (!prefixcmp(buf, ">>>>>>> ")) {
124 + if (hunk != 2)
125 + goto bad;
126 if (strbuf_cmp(&one, &two) > 0)
127 strbuf_swap(&one, &two);
128 hunk_no++;
@@ -142,6 +148,10 @@ static int handle_file(const char *path,
148 strbuf_addstr(&two, buf);
149 else if (out)
150 fputs(buf, out);
151 + continue;
152 + bad:
153 + hunk = 99; /* force error exit */
154 + break;
155 }
156 strbuf_release(&one);
157 strbuf_release(&two);
t/t4200-rerere.sh
+20 -6
@@ -9,6 +9,8 @@ test_description='git rerere
9 . ./test-lib.sh
10
11 cat > a1 << EOF
12 +Some title
13 +==========
14 Whether 'tis nobler in the mind to suffer
15 The slings and arrows of outrageous fortune,
16 Or to take arms against a sea of troubles,
@@ -24,6 +26,8 @@ git commit -q -a -m initial
26
27 git checkout -b first
28 cat >> a1 << EOF
29 +Some title
30 +==========
31 To die, to sleep;
32 To sleep: perchance to dream: ay, there's the rub;
33 For in that sleep of death what dreams may come
@@ -35,7 +39,7 @@ git commit -q -a -m first
39
40 git checkout -b second master
41 git show first:a1 |
38 -sed -e 's/To die, t/To die! T/' > a1
42 +sed -e 's/To die, t/To die! T/' -e 's/Some title/Some Title/' > a1
43 echo "* END *" >>a1
44 git commit -q -a -m second
45
@@ -55,14 +59,14 @@ test_expect_success 'conflicting merge' '
59
60 sha1=$(sed -e 's/ .*//' .git/rr-cache/MERGE_RR)
61 rr=.git/rr-cache/$sha1
58 -test_expect_success 'recorded preimage' "grep ======= $rr/preimage"
62 +test_expect_success 'recorded preimage' "grep ^=======$ $rr/preimage"
63
64 test_expect_success 'rerere.enabled works, too' '
65 rm -rf .git/rr-cache &&
66 git config rerere.enabled true &&
67 git reset --hard &&
68 ! git merge first &&
65 - grep ======= $rr/preimage
69 + grep ^=======$ $rr/preimage
70 '
71
72 test_expect_success 'no postimage or thisimage yet' \
@@ -71,7 +75,7 @@ test_expect_success 'no postimage or thisimage yet' \
75 test_expect_success 'preimage has right number of lines' '
76
77 cnt=$(sed -ne "/^<<<<<<</,/^>>>>>>>/p" $rr/preimage | wc -l) &&
74 - test $cnt = 9
78 + test $cnt = 13
79
80 '
81
@@ -80,13 +84,23 @@ git show first:a1 > a1
84 cat > expect << EOF
85 --- a/a1
86 +++ b/a1
83 -@@ -6,17 +6,9 @@
87 +@@ -1,4 +1,4 @@
88 +-Some Title
89 ++Some title
90 + ==========
91 + Whether 'tis nobler in the mind to suffer
92 + The slings and arrows of outrageous fortune,
93 +@@ -8,21 +8,11 @@
94 The heart-ache and the thousand natural shocks
95 That flesh is heir to, 'tis a consummation
96 Devoutly to be wish'd.
97 -<<<<<<<
98 +-Some Title
99 +-==========
100 -To die! To sleep;
101 -=======
102 + Some title
103 + ==========
104 To die, to sleep;
105 ->>>>>>>
106 To sleep: perchance to dream: ay, there's the rub;
@@ -124,7 +138,7 @@ test_expect_success 'another conflicting merge' '
138 '
139
140 git show first:a1 | sed 's/To die: t/To die! T/' > expect
127 -test_expect_success 'rerere kicked in' "! grep ======= a1"
141 +test_expect_success 'rerere kicked in' "! grep ^=======$ a1"
142
143 test_expect_success 'rerere prefers first change' 'test_cmp a1 expect'
144