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

Commit

Permalink
Merge pull request #932 from flant/fix-negative-range-info
Browse files Browse the repository at this point in the history
Fix `fatal: corrupt patch` error in unified diff format
  • Loading branch information
mcuadros authored Sep 10, 2018
2 parents 208b3c3 + 80170bd commit a2d62f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
8 changes: 6 additions & 2 deletions plumbing/format/diff/unified_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,13 @@ func (c *hunksGenerator) addLineNumbers(la, lb int, linesBefore int, i int, op O
// we need to search for a reference for the next diff
switch {
case linesBefore != 0 && c.ctxLines != 0:
clb = lb - c.ctxLines + 1
if lb > c.ctxLines {
clb = lb - c.ctxLines + 1
} else {
clb = 1
}
case c.ctxLines == 0:
clb = lb - c.ctxLines
clb = lb
case i != len(c.chunks)-1:
next := c.chunks[i+1]
if next.Type() == op || next.Type() == Equal {
Expand Down
37 changes: 37 additions & 0 deletions plumbing/format/diff/unified_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,43 @@ var oneChunkPatchInverted Patch = testPatch{
}

var fixtures []*fixture = []*fixture{{
patch: testPatch{
message: "",
filePatches: []testFilePatch{{
from: &testFile{
mode: filemode.Regular,
path: "README.md",
seed: "hello\nworld\n",
},
to: &testFile{
mode: filemode.Regular,
path: "README.md",
seed: "hello\nbug\n",
},
chunks: []testChunk{{
content: "hello",
op: Equal,
}, {
content: "world",
op: Delete,
}, {
content: "bug",
op: Add,
}},
}},
},
desc: "positive negative number",
context: 2,
diff: `diff --git a/README.md b/README.md
index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
hello
-world
+bug
`,
}, {
patch: testPatch{
message: "",
filePatches: []testFilePatch{{
Expand Down

0 comments on commit a2d62f5

Please sign in to comment.