aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plumbing/format/diff/unified_encoder.go79
-rw-r--r--plumbing/format/diff/unified_encoder_test.go22
2 files changed, 64 insertions, 37 deletions
diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go
index 19c3f0b..413984a 100644
--- a/plumbing/format/diff/unified_encoder.go
+++ b/plumbing/format/diff/unified_encoder.go
@@ -86,48 +86,73 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil
}
isBinary := filePatch.IsBinary()
- sb.WriteString(e.color[Meta])
+ var lines []string
switch {
case from != nil && to != nil:
hashEquals := from.Hash() == to.Hash()
- fmt.Fprintf(sb, "diff --git a/%s b/%s\n", from.Path(), to.Path())
+ lines = append(lines,
+ fmt.Sprintf("diff --git a/%s b/%s", from.Path(), to.Path()),
+ )
if from.Mode() != to.Mode() {
- fmt.Fprintf(sb, "old mode %o\n", from.Mode())
- fmt.Fprintf(sb, "new mode %o\n", to.Mode())
+ lines = append(lines,
+ fmt.Sprintf("old mode %o", from.Mode()),
+ fmt.Sprintf("new mode %o", to.Mode()),
+ )
}
if from.Path() != to.Path() {
- fmt.Fprintf(sb, "rename from %s\n", from.Path())
- fmt.Fprintf(sb, "rename to %s\n", to.Path())
+ lines = append(lines,
+ fmt.Sprintf("rename from %s", from.Path()),
+ fmt.Sprintf("rename to %s", to.Path()),
+ )
}
if from.Mode() != to.Mode() && !hashEquals {
- fmt.Fprintf(sb, "index %s..%s\n", from.Hash(), to.Hash())
+ lines = append(lines,
+ fmt.Sprintf("index %s..%s", from.Hash(), to.Hash()),
+ )
} else if !hashEquals {
- fmt.Fprintf(sb, "index %s..%s %o\n", from.Hash(), to.Hash(), from.Mode())
+ lines = append(lines,
+ fmt.Sprintf("index %s..%s %o", from.Hash(), to.Hash(), from.Mode()),
+ )
}
if !hashEquals {
- e.writePathLines(sb, "a/"+from.Path(), "b/"+to.Path(), isBinary)
+ lines = e.appendPathLines(lines, "a/"+from.Path(), "b/"+to.Path(), isBinary)
}
case from == nil:
- fmt.Fprintf(sb, "diff --git a/%s b/%s\n", to.Path(), to.Path())
- fmt.Fprintf(sb, "new file mode %o\n", to.Mode())
- fmt.Fprintf(sb, "index %s..%s\n", plumbing.ZeroHash, to.Hash())
- e.writePathLines(sb, "/dev/null", "b/"+to.Path(), isBinary)
+ lines = append(lines,
+ fmt.Sprintf("diff --git a/%s b/%s", to.Path(), to.Path()),
+ fmt.Sprintf("new file mode %o", to.Mode()),
+ fmt.Sprintf("index %s..%s", plumbing.ZeroHash, to.Hash()),
+ )
+ lines = e.appendPathLines(lines, "/dev/null", "b/"+to.Path(), isBinary)
case to == nil:
- fmt.Fprintf(sb, "diff --git a/%s b/%s\n", from.Path(), from.Path())
- fmt.Fprintf(sb, "deleted file mode %o\n", from.Mode())
- fmt.Fprintf(sb, "index %s..%s\n", from.Hash(), plumbing.ZeroHash)
- e.writePathLines(sb, "a/"+from.Path(), "/dev/null", isBinary)
+ lines = append(lines,
+ fmt.Sprintf("diff --git a/%s b/%s", from.Path(), from.Path()),
+ fmt.Sprintf("deleted file mode %o", from.Mode()),
+ fmt.Sprintf("index %s..%s", from.Hash(), plumbing.ZeroHash),
+ )
+ lines = e.appendPathLines(lines, "a/"+from.Path(), "/dev/null", isBinary)
+ }
+
+ sb.WriteString(e.color[Meta])
+ sb.WriteString(lines[0])
+ for _, line := range lines[1:] {
+ sb.WriteByte('\n')
+ sb.WriteString(line)
}
sb.WriteString(e.color.Reset(Meta))
+ sb.WriteByte('\n')
}
-func (e *UnifiedEncoder) writePathLines(sb *strings.Builder, fromPath, toPath string, isBinary bool) {
+func (e *UnifiedEncoder) appendPathLines(lines []string, fromPath, toPath string, isBinary bool) []string {
if isBinary {
- fmt.Fprintf(sb, "Binary files %s and %s differ\n", fromPath, toPath)
- } else {
- fmt.Fprintf(sb, "--- %s\n", fromPath)
- fmt.Fprintf(sb, "+++ %s\n", toPath)
+ return append(lines,
+ fmt.Sprintf("Binary files %s and %s differ", fromPath, toPath),
+ )
}
+ return append(lines,
+ fmt.Sprintf("--- %s", fromPath),
+ fmt.Sprintf("+++ %s", toPath),
+ )
}
type hunksGenerator struct {
@@ -341,9 +366,11 @@ func (o *op) writeTo(sb *strings.Builder, color ColorConfig) {
colorKey := operationColorKey[o.t]
sb.WriteString(color[colorKey])
sb.WriteByte(operationChar[o.t])
- sb.WriteString(o.text)
- sb.WriteString(color.Reset(colorKey))
- if !strings.HasSuffix(o.text, "\n") {
- sb.WriteString("\n\\ No newline at end of file\n")
+ if strings.HasSuffix(o.text, "\n") {
+ sb.WriteString(strings.TrimSuffix(o.text, "\n"))
+ } else {
+ sb.WriteString(o.text + "\n\\ No newline at end of file")
}
+ sb.WriteString(color.Reset(colorKey))
+ sb.WriteByte('\n')
}
diff --git a/plumbing/format/diff/unified_encoder_test.go b/plumbing/format/diff/unified_encoder_test.go
index 1e44572..22dc4f1 100644
--- a/plumbing/format/diff/unified_encoder_test.go
+++ b/plumbing/format/diff/unified_encoder_test.go
@@ -894,11 +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 +
+ "+++ b/README.md" + color.Reset + "\n" +
color.Cyan + "@@ -1,2 +1,2 @@" + color.Reset + "\n" +
" hello\n" +
- color.Red + "-world\n" + color.Reset +
- color.Green + "+bug\n" + color.Reset,
+ color.Red + "-world" + color.Reset + "\n" +
+ color.Green + "+bug" + color.Reset + "\n",
}, {
patch: testPatch{
message: "",
@@ -933,10 +933,10 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
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 +
+ "+++ b/test.txt" + color.Reset + "\n" +
color.Cyan + "@@ -1 +1 @@" + color.Reset + "\n" +
- color.Red + "-test\n" + color.Reset +
- color.Green + "+test2\n" + color.Reset,
+ color.Red + "-test" + color.Reset + "\n" +
+ color.Green + "+test2" + color.Reset + "\n",
}, {
patch: oneChunkPatch,
desc: "modified deleting lines file with context to 1 with color",
@@ -948,21 +948,21 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
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 +
+ "+++ b/onechunk.txt" + color.Reset + "\n" +
color.Cyan + "@@ -1,2 +1 @@" + color.Reset + "\n" +
- color.Red + "-A\n" + color.Reset +
+ color.Red + "-A" + color.Reset + "\n" +
" B\n" +
color.Cyan + "@@ -7,3 +6,2 @@" + color.Reset + " " + color.Reverse + "F" + color.Reset + "\n" +
" G\n" +
- color.Red + "-H\n" + color.Reset +
+ color.Red + "-H" + color.Reset + "\n" +
" I\n" +
color.Cyan + "@@ -14,3 +12,2 @@" + color.Reset + " " + color.Reverse + "M" + color.Reset + "\n" +
" N\n" +
- color.Red + "-Ñ\n" + color.Reset +
+ color.Red + "-Ñ" + color.Reset + "\n" +
" O\n" +
color.Cyan + "@@ -21,3 +18,2 @@" + color.Reset + " " + color.Reverse + "S" + color.Reset + "\n" +
" T\n" +
- color.Red + "-U\n" + color.Reset +
+ color.Red + "-U" + color.Reset + "\n" +
" V\n",
}}