diff options
author | Tom Payne <twpayne@gmail.com> | 2020-04-29 03:47:19 +0100 |
---|---|---|
committer | Tom Payne <twpayne@gmail.com> | 2020-04-29 04:08:02 +0100 |
commit | 9f7a8726be44235a65a1f2cee18b4fa4f56341fe (patch) | |
tree | dc06f8cdc28ddf687d1915e3a0995657070c4dc5 /plumbing/format/diff | |
parent | 06dad218498f62255799d03dc6411dba481ce71b (diff) | |
download | go-git-9f7a8726be44235a65a1f2cee18b4fa4f56341fe.tar.gz |
plumbing: diff, fix coloring of hunk context
Diffstat (limited to 'plumbing/format/diff')
-rw-r--r-- | plumbing/format/diff/unified_encoder.go | 15 | ||||
-rw-r--r-- | plumbing/format/diff/unified_encoder_test.go | 51 |
2 files changed, 51 insertions, 15 deletions
diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go index 7b0c31e..6472db2 100644 --- a/plumbing/format/diff/unified_encoder.go +++ b/plumbing/format/diff/unified_encoder.go @@ -15,7 +15,7 @@ const ( chunkStart = "@@ -" chunkMiddle = " +" - chunkEnd = " @@%s\n" + chunkEnd = " @@" chunkCount = "%d,%d" noFilePath = "/dev/null" @@ -230,7 +230,7 @@ func (c *hunksGenerator) processHunk(i int, op Operation) { var ctxPrefix string linesBefore := len(c.beforeContext) if linesBefore > c.ctxLines { - ctxPrefix = " " + c.beforeContext[linesBefore-c.ctxLines-1] + ctxPrefix = c.beforeContext[linesBefore-c.ctxLines-1] c.beforeContext = c.beforeContext[linesBefore-c.ctxLines:] linesBefore = c.ctxLines } @@ -337,9 +337,18 @@ func (c *hunk) WriteTo(buf *bytes.Buffer, color ColorConfig) { fmt.Fprintf(buf, chunkCount, c.toLine, c.toCount) } - fmt.Fprintf(buf, chunkEnd, c.ctxPrefix) + buf.WriteString(chunkEnd) buf.WriteString(color.Reset()) + if c.ctxPrefix != "" { + buf.WriteByte(' ') + buf.WriteString(color[Func]) + buf.WriteString(c.ctxPrefix) + buf.WriteString(color.Reset()) + } + + buf.WriteByte('\n') + for _, d := range c.ops { buf.WriteString(d.String(color)) } diff --git a/plumbing/format/diff/unified_encoder_test.go b/plumbing/format/diff/unified_encoder_test.go index f72424a..6eaec72 100644 --- a/plumbing/format/diff/unified_encoder_test.go +++ b/plumbing/format/diff/unified_encoder_test.go @@ -894,12 +894,11 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f color.Bold + "diff --git a/README.md b/README.md\n" + "index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644\n" + "--- a/README.md\n" + - "+++ b/README.md\n" + - color.Reset + color.Cyan + "@@ -1,2 +1,2 @@\n" + - color.Normal + color.Reset + " hello\n" + - color.Reset + color.Red + "-world\n" + - color.Reset + color.Green + "+bug\n" + - color.Reset, + "+++ b/README.md\n" + color.Reset + + color.Cyan + "@@ -1,2 +1,2 @@" + color.Reset + "\n" + + color.Normal + " hello\n" + color.Reset + + color.Red + "-world\n" + color.Reset + + color.Green + "+bug\n" + color.Reset, }, { patch: testPatch{ message: "", @@ -927,16 +926,44 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f desc: "one line change with color", context: 1, - color: NewColorConfig(), + color: NewColorConfig( + WithColor(Func, color.Reverse), + ), diff: "" + color.Bold + "diff --git a/test.txt b/test.txt\n" + "index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..180cf8328022becee9aaa2577a8f84ea2b9f3827 100644\n" + "--- a/test.txt\n" + - "+++ b/test.txt\n" + - color.Reset + color.Cyan + "@@ -1 +1 @@\n" + - color.Reset + color.Red + "-test\n" + - color.Reset + color.Green + "+test2\n" + - color.Reset, + "+++ b/test.txt\n" + color.Reset + + color.Cyan + "@@ -1 +1 @@" + color.Reset + "\n" + + color.Red + "-test\n" + color.Reset + + color.Green + "+test2\n" + color.Reset, +}, { + patch: oneChunkPatch, + desc: "modified deleting lines file with context to 1 with color", + context: 1, + color: NewColorConfig( + WithColor(Func, color.Reverse), + ), + diff: "" + + color.Bold + "diff --git a/onechunk.txt b/onechunk.txt\n" + + "index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644\n" + + "--- a/onechunk.txt\n" + + "+++ b/onechunk.txt\n" + color.Reset + + color.Cyan + "@@ -1,2 +1 @@" + color.Reset + "\n" + + color.Red + "-A\n" + color.Reset + + color.Normal + " B\n" + color.Reset + + color.Cyan + "@@ -7,3 +6,2 @@" + color.Reset + " " + color.Reverse + "F" + color.Reset + "\n" + + color.Normal + " G\n" + color.Reset + + color.Red + "-H\n" + color.Reset + + color.Normal + " I\n" + color.Reset + + color.Cyan + "@@ -14,3 +12,2 @@" + color.Reset + " " + color.Reverse + "M" + color.Reset + "\n" + + color.Normal + " N\n" + color.Reset + + color.Red + "-Ñ\n" + color.Reset + + color.Normal + " O\n" + color.Reset + + color.Cyan + "@@ -21,3 +18,2 @@" + color.Reset + " " + color.Reverse + "S" + color.Reset + "\n" + + color.Normal + " T\n" + color.Reset + + color.Red + "-U\n" + color.Reset + + color.Normal + " V\n" + color.Reset, }} type testPatch struct { |