Skip to content

Commit 392a78f

Browse files
authored
Merge pull request go-git#66 from twpayne/diff-reset-color-on-line
plumbing: diff, reset color at end of line
2 parents 6f9eb4e + 7011136 commit 392a78f

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

plumbing/format/diff/unified_encoder.go

+53-26
Original file line numberDiff line numberDiff line change
@@ -86,48 +86,73 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil
8686
}
8787
isBinary := filePatch.IsBinary()
8888

89-
sb.WriteString(e.color[Meta])
89+
var lines []string
9090
switch {
9191
case from != nil && to != nil:
9292
hashEquals := from.Hash() == to.Hash()
93-
fmt.Fprintf(sb, "diff --git a/%s b/%s\n", from.Path(), to.Path())
93+
lines = append(lines,
94+
fmt.Sprintf("diff --git a/%s b/%s", from.Path(), to.Path()),
95+
)
9496
if from.Mode() != to.Mode() {
95-
fmt.Fprintf(sb, "old mode %o\n", from.Mode())
96-
fmt.Fprintf(sb, "new mode %o\n", to.Mode())
97+
lines = append(lines,
98+
fmt.Sprintf("old mode %o", from.Mode()),
99+
fmt.Sprintf("new mode %o", to.Mode()),
100+
)
97101
}
98102
if from.Path() != to.Path() {
99-
fmt.Fprintf(sb, "rename from %s\n", from.Path())
100-
fmt.Fprintf(sb, "rename to %s\n", to.Path())
103+
lines = append(lines,
104+
fmt.Sprintf("rename from %s", from.Path()),
105+
fmt.Sprintf("rename to %s", to.Path()),
106+
)
101107
}
102108
if from.Mode() != to.Mode() && !hashEquals {
103-
fmt.Fprintf(sb, "index %s..%s\n", from.Hash(), to.Hash())
109+
lines = append(lines,
110+
fmt.Sprintf("index %s..%s", from.Hash(), to.Hash()),
111+
)
104112
} else if !hashEquals {
105-
fmt.Fprintf(sb, "index %s..%s %o\n", from.Hash(), to.Hash(), from.Mode())
113+
lines = append(lines,
114+
fmt.Sprintf("index %s..%s %o", from.Hash(), to.Hash(), from.Mode()),
115+
)
106116
}
107117
if !hashEquals {
108-
e.writePathLines(sb, "a/"+from.Path(), "b/"+to.Path(), isBinary)
118+
lines = e.appendPathLines(lines, "a/"+from.Path(), "b/"+to.Path(), isBinary)
109119
}
110120
case from == nil:
111-
fmt.Fprintf(sb, "diff --git a/%s b/%s\n", to.Path(), to.Path())
112-
fmt.Fprintf(sb, "new file mode %o\n", to.Mode())
113-
fmt.Fprintf(sb, "index %s..%s\n", plumbing.ZeroHash, to.Hash())
114-
e.writePathLines(sb, "/dev/null", "b/"+to.Path(), isBinary)
121+
lines = append(lines,
122+
fmt.Sprintf("diff --git a/%s b/%s", to.Path(), to.Path()),
123+
fmt.Sprintf("new file mode %o", to.Mode()),
124+
fmt.Sprintf("index %s..%s", plumbing.ZeroHash, to.Hash()),
125+
)
126+
lines = e.appendPathLines(lines, "/dev/null", "b/"+to.Path(), isBinary)
115127
case to == nil:
116-
fmt.Fprintf(sb, "diff --git a/%s b/%s\n", from.Path(), from.Path())
117-
fmt.Fprintf(sb, "deleted file mode %o\n", from.Mode())
118-
fmt.Fprintf(sb, "index %s..%s\n", from.Hash(), plumbing.ZeroHash)
119-
e.writePathLines(sb, "a/"+from.Path(), "/dev/null", isBinary)
128+
lines = append(lines,
129+
fmt.Sprintf("diff --git a/%s b/%s", from.Path(), from.Path()),
130+
fmt.Sprintf("deleted file mode %o", from.Mode()),
131+
fmt.Sprintf("index %s..%s", from.Hash(), plumbing.ZeroHash),
132+
)
133+
lines = e.appendPathLines(lines, "a/"+from.Path(), "/dev/null", isBinary)
134+
}
135+
136+
sb.WriteString(e.color[Meta])
137+
sb.WriteString(lines[0])
138+
for _, line := range lines[1:] {
139+
sb.WriteByte('\n')
140+
sb.WriteString(line)
120141
}
121142
sb.WriteString(e.color.Reset(Meta))
143+
sb.WriteByte('\n')
122144
}
123145

124-
func (e *UnifiedEncoder) writePathLines(sb *strings.Builder, fromPath, toPath string, isBinary bool) {
146+
func (e *UnifiedEncoder) appendPathLines(lines []string, fromPath, toPath string, isBinary bool) []string {
125147
if isBinary {
126-
fmt.Fprintf(sb, "Binary files %s and %s differ\n", fromPath, toPath)
127-
} else {
128-
fmt.Fprintf(sb, "--- %s\n", fromPath)
129-
fmt.Fprintf(sb, "+++ %s\n", toPath)
148+
return append(lines,
149+
fmt.Sprintf("Binary files %s and %s differ", fromPath, toPath),
150+
)
130151
}
152+
return append(lines,
153+
fmt.Sprintf("--- %s", fromPath),
154+
fmt.Sprintf("+++ %s", toPath),
155+
)
131156
}
132157

133158
type hunksGenerator struct {
@@ -341,9 +366,11 @@ func (o *op) writeTo(sb *strings.Builder, color ColorConfig) {
341366
colorKey := operationColorKey[o.t]
342367
sb.WriteString(color[colorKey])
343368
sb.WriteByte(operationChar[o.t])
344-
sb.WriteString(o.text)
345-
sb.WriteString(color.Reset(colorKey))
346-
if !strings.HasSuffix(o.text, "\n") {
347-
sb.WriteString("\n\\ No newline at end of file\n")
369+
if strings.HasSuffix(o.text, "\n") {
370+
sb.WriteString(strings.TrimSuffix(o.text, "\n"))
371+
} else {
372+
sb.WriteString(o.text + "\n\\ No newline at end of file")
348373
}
374+
sb.WriteString(color.Reset(colorKey))
375+
sb.WriteByte('\n')
349376
}

plumbing/format/diff/unified_encoder_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,11 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
894894
color.Bold + "diff --git a/README.md b/README.md\n" +
895895
"index 94954abda49de8615a048f8d2e64b5de848e27a1..f3dad9514629b9ff9136283ae331ad1fc95748a8 100644\n" +
896896
"--- a/README.md\n" +
897-
"+++ b/README.md\n" + color.Reset +
897+
"+++ b/README.md" + color.Reset + "\n" +
898898
color.Cyan + "@@ -1,2 +1,2 @@" + color.Reset + "\n" +
899899
" hello\n" +
900-
color.Red + "-world\n" + color.Reset +
901-
color.Green + "+bug\n" + color.Reset,
900+
color.Red + "-world" + color.Reset + "\n" +
901+
color.Green + "+bug" + color.Reset + "\n",
902902
}, {
903903
patch: testPatch{
904904
message: "",
@@ -933,10 +933,10 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
933933
color.Bold + "diff --git a/test.txt b/test.txt\n" +
934934
"index 9daeafb9864cf43055ae93beb0afd6c7d144bfa4..180cf8328022becee9aaa2577a8f84ea2b9f3827 100644\n" +
935935
"--- a/test.txt\n" +
936-
"+++ b/test.txt\n" + color.Reset +
936+
"+++ b/test.txt" + color.Reset + "\n" +
937937
color.Cyan + "@@ -1 +1 @@" + color.Reset + "\n" +
938-
color.Red + "-test\n" + color.Reset +
939-
color.Green + "+test2\n" + color.Reset,
938+
color.Red + "-test" + color.Reset + "\n" +
939+
color.Green + "+test2" + color.Reset + "\n",
940940
}, {
941941
patch: oneChunkPatch,
942942
desc: "modified deleting lines file with context to 1 with color",
@@ -948,21 +948,21 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
948948
color.Bold + "diff --git a/onechunk.txt b/onechunk.txt\n" +
949949
"index ab5eed5d4a2c33aeef67e0188ee79bed666bde6f..0adddcde4fd38042c354518351820eb06c417c82 100644\n" +
950950
"--- a/onechunk.txt\n" +
951-
"+++ b/onechunk.txt\n" + color.Reset +
951+
"+++ b/onechunk.txt" + color.Reset + "\n" +
952952
color.Cyan + "@@ -1,2 +1 @@" + color.Reset + "\n" +
953-
color.Red + "-A\n" + color.Reset +
953+
color.Red + "-A" + color.Reset + "\n" +
954954
" B\n" +
955955
color.Cyan + "@@ -7,3 +6,2 @@" + color.Reset + " " + color.Reverse + "F" + color.Reset + "\n" +
956956
" G\n" +
957-
color.Red + "-H\n" + color.Reset +
957+
color.Red + "-H" + color.Reset + "\n" +
958958
" I\n" +
959959
color.Cyan + "@@ -14,3 +12,2 @@" + color.Reset + " " + color.Reverse + "M" + color.Reset + "\n" +
960960
" N\n" +
961-
color.Red + "-Ñ\n" + color.Reset +
961+
color.Red + "-Ñ" + color.Reset + "\n" +
962962
" O\n" +
963963
color.Cyan + "@@ -21,3 +18,2 @@" + color.Reset + " " + color.Reverse + "S" + color.Reset + "\n" +
964964
" T\n" +
965-
color.Red + "-U\n" + color.Reset +
965+
color.Red + "-U" + color.Reset + "\n" +
966966
" V\n",
967967
}}
968968

0 commit comments

Comments
 (0)