aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing')
-rw-r--r--plumbing/color/color.go38
-rw-r--r--plumbing/format/diff/colorconfig.go11
-rw-r--r--plumbing/format/diff/unified_encoder.go34
-rw-r--r--plumbing/format/diff/unified_encoder_test.go53
4 files changed, 106 insertions, 30 deletions
diff --git a/plumbing/color/color.go b/plumbing/color/color.go
new file mode 100644
index 0000000..2cd74bd
--- /dev/null
+++ b/plumbing/color/color.go
@@ -0,0 +1,38 @@
+package color
+
+// TODO read colors from a github.com/go-git/go-git/plumbing/format/config.Config struct
+// TODO implement color parsing, see https://github.com/git/git/blob/v2.26.2/color.c
+
+// Colors. See https://github.com/git/git/blob/v2.26.2/color.h#L24-L53.
+const (
+ Normal = ""
+ Reset = "\033[m"
+ Bold = "\033[1m"
+ Red = "\033[31m"
+ Green = "\033[32m"
+ Yellow = "\033[33m"
+ Blue = "\033[34m"
+ Magenta = "\033[35m"
+ Cyan = "\033[36m"
+ BoldRed = "\033[1;31m"
+ BoldGreen = "\033[1;32m"
+ BoldYellow = "\033[1;33m"
+ BoldBlue = "\033[1;34m"
+ BoldMagenta = "\033[1;35m"
+ BoldCyan = "\033[1;36m"
+ FaintRed = "\033[2;31m"
+ FaintGreen = "\033[2;32m"
+ FaintYellow = "\033[2;33m"
+ FaintBlue = "\033[2;34m"
+ FaintMagenta = "\033[2;35m"
+ FaintCyan = "\033[2;36m"
+ BgRed = "\033[41m"
+ BgGreen = "\033[42m"
+ BgYellow = "\033[43m"
+ BgBlue = "\033[44m"
+ BgMagenta = "\033[45m"
+ BgCyan = "\033[46m"
+ Faint = "\033[2m"
+ FaintItalic = "\033[2;3m"
+ Reverse = "\033[7m"
+)
diff --git a/plumbing/format/diff/colorconfig.go b/plumbing/format/diff/colorconfig.go
index b7c32e6..6fd4158 100644
--- a/plumbing/format/diff/colorconfig.go
+++ b/plumbing/format/diff/colorconfig.go
@@ -1,6 +1,6 @@
package diff
-import "github.com/go-git/go-git/v5/internal/color"
+import "github.com/go-git/go-git/v5/plumbing/color"
// A ColorKey is a key into a ColorConfig map and also equal to the key in the
// diff.color subsection of the config. See
@@ -86,10 +86,11 @@ func NewColorConfig(options ...ColorConfigOption) ColorConfig {
return cc
}
-// Reset returns the ANSI escape sequence to reset a color set from cc. If cc is
-// nil or empty then no reset is needed so it returns the empty string.
-func (cc ColorConfig) Reset() string {
- if len(cc) == 0 {
+// Reset returns the ANSI escape sequence to reset the color with key set from
+// cc. If no color was set then no reset is needed so it returns the empty
+// string.
+func (cc ColorConfig) Reset(key ColorKey) string {
+ if cc[key] == "" {
return ""
}
return color.Reset
diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go
index 7b0c31e..bd115f8 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"
@@ -142,21 +142,21 @@ func (e *UnifiedEncoder) header(from, to File, isBinary bool) error {
e.pathLines(isBinary, aDir+from.Path(), bDir+to.Path())
}
- e.buf.WriteString(e.color.Reset())
+ e.buf.WriteString(e.color.Reset(Meta))
case from == nil:
e.buf.WriteString(e.color[Meta])
fmt.Fprintf(&e.buf, diffInit, to.Path(), to.Path())
fmt.Fprintf(&e.buf, newFileMode, to.Mode())
fmt.Fprintf(&e.buf, indexNoMode, plumbing.ZeroHash, to.Hash())
e.pathLines(isBinary, noFilePath, bDir+to.Path())
- e.buf.WriteString(e.color.Reset())
+ e.buf.WriteString(e.color.Reset(Meta))
case to == nil:
e.buf.WriteString(e.color[Meta])
fmt.Fprintf(&e.buf, diffInit, from.Path(), from.Path())
fmt.Fprintf(&e.buf, deletedFileMode, from.Mode())
fmt.Fprintf(&e.buf, indexNoMode, from.Hash(), plumbing.ZeroHash)
e.pathLines(isBinary, aDir+from.Path(), noFilePath)
- e.buf.WriteString(e.color.Reset())
+ e.buf.WriteString(e.color.Reset(Meta))
}
return nil
@@ -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,8 +337,17 @@ 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(color.Reset())
+ buf.WriteString(chunkEnd)
+ buf.WriteString(color.Reset(Frag))
+
+ if c.ctxPrefix != "" {
+ buf.WriteByte(' ')
+ buf.WriteString(color[Func])
+ buf.WriteString(c.ctxPrefix)
+ buf.WriteString(color.Reset(Func))
+ }
+
+ buf.WriteByte('\n')
for _, d := range c.ops {
buf.WriteString(d.String(color))
@@ -368,22 +377,23 @@ type op struct {
}
func (o *op) String(color ColorConfig) string {
- var setColor, prefix, suffix string
+ var prefix, suffix string
+ var colorKey ColorKey
switch o.t {
case Add:
prefix = addLine
- setColor = color[New]
+ colorKey = New
case Delete:
prefix = deleteLine
- setColor = color[Old]
+ colorKey = Old
case Equal:
prefix = equalLine
- setColor = color[Context]
+ colorKey = Context
}
n := len(o.text)
if n > 0 && o.text[n-1] != '\n' {
suffix = noNewLine
}
- return fmt.Sprintf(prefix, setColor, o.text, color.Reset(), suffix)
+ return fmt.Sprintf(prefix, color[colorKey], o.text, color.Reset(colorKey), suffix)
}
diff --git a/plumbing/format/diff/unified_encoder_test.go b/plumbing/format/diff/unified_encoder_test.go
index f72424a..1e44572 100644
--- a/plumbing/format/diff/unified_encoder_test.go
+++ b/plumbing/format/diff/unified_encoder_test.go
@@ -4,8 +4,8 @@ import (
"bytes"
"testing"
- "github.com/go-git/go-git/v5/internal/color"
"github.com/go-git/go-git/v5/plumbing"
+ "github.com/go-git/go-git/v5/plumbing/color"
"github.com/go-git/go-git/v5/plumbing/filemode"
. "gopkg.in/check.v1"
@@ -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" +
+ " hello\n" +
+ 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 +
+ " B\n" +
+ color.Cyan + "@@ -7,3 +6,2 @@" + color.Reset + " " + color.Reverse + "F" + color.Reset + "\n" +
+ " G\n" +
+ color.Red + "-H\n" + color.Reset +
+ " I\n" +
+ color.Cyan + "@@ -14,3 +12,2 @@" + color.Reset + " " + color.Reverse + "M" + color.Reset + "\n" +
+ " N\n" +
+ color.Red + "-Ñ\n" + color.Reset +
+ " O\n" +
+ color.Cyan + "@@ -21,3 +18,2 @@" + color.Reset + " " + color.Reverse + "S" + color.Reset + "\n" +
+ " T\n" +
+ color.Red + "-U\n" + color.Reset +
+ " V\n",
}}
type testPatch struct {