Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit a2d62f5

Browse files
authored
Merge pull request #932 from flant/fix-negative-range-info
Fix `fatal: corrupt patch` error in unified diff format
2 parents 208b3c3 + 80170bd commit a2d62f5

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

plumbing/format/diff/unified_encoder.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,13 @@ func (c *hunksGenerator) addLineNumbers(la, lb int, linesBefore int, i int, op O
237237
// we need to search for a reference for the next diff
238238
switch {
239239
case linesBefore != 0 && c.ctxLines != 0:
240-
clb = lb - c.ctxLines + 1
240+
if lb > c.ctxLines {
241+
clb = lb - c.ctxLines + 1
242+
} else {
243+
clb = 1
244+
}
241245
case c.ctxLines == 0:
242-
clb = lb - c.ctxLines
246+
clb = lb
243247
case i != len(c.chunks)-1:
244248
next := c.chunks[i+1]
245249
if next.Type() == op || next.Type() == Equal {

plumbing/format/diff/unified_encoder_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,43 @@ var oneChunkPatchInverted Patch = testPatch{
150150
}
151151

152152
var fixtures []*fixture = []*fixture{{
153+
patch: testPatch{
154+
message: "",
155+
filePatches: []testFilePatch{{
156+
from: &testFile{
157+
mode: filemode.Regular,
158+
path: "README.md",
159+
seed: "hello\nworld\n",
160+
},
161+
to: &testFile{
162+
mode: filemode.Regular,
163+
path: "README.md",
164+
seed: "hello\nbug\n",
165+
},
166+
chunks: []testChunk{{
167+
content: "hello",
168+
op: Equal,
169+
}, {
170+
content: "world",
171+
op: Delete,
172+
}, {
173+
content: "bug",
174+
op: Add,
175+
}},
176+
}},
177+
},
178+
desc: "positive negative number",
179+
context: 2,
180+
diff: `diff --git a/README.md b/README.md
181+
index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644
182+
--- a/README.md
183+
+++ b/README.md
184+
@@ -1,2 +1,2 @@
185+
hello
186+
-world
187+
+bug
188+
`,
189+
}, {
153190
patch: testPatch{
154191
message: "",
155192
filePatches: []testFilePatch{{

0 commit comments

Comments
 (0)