@@ -86,48 +86,73 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil
86
86
}
87
87
isBinary := filePatch .IsBinary ()
88
88
89
- sb . WriteString ( e . color [ Meta ])
89
+ var lines [] string
90
90
switch {
91
91
case from != nil && to != nil :
92
92
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
+ )
94
96
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
+ )
97
101
}
98
102
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
+ )
101
107
}
102
108
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
+ )
104
112
} 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
+ )
106
116
}
107
117
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 )
109
119
}
110
120
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 )
115
127
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 )
120
141
}
121
142
sb .WriteString (e .color .Reset (Meta ))
143
+ sb .WriteByte ('\n' )
122
144
}
123
145
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 {
125
147
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
+ )
130
151
}
152
+ return append (lines ,
153
+ fmt .Sprintf ("--- %s" , fromPath ),
154
+ fmt .Sprintf ("+++ %s" , toPath ),
155
+ )
131
156
}
132
157
133
158
type hunksGenerator struct {
@@ -341,9 +366,11 @@ func (o *op) writeTo(sb *strings.Builder, color ColorConfig) {
341
366
colorKey := operationColorKey [o .t ]
342
367
sb .WriteString (color [colorKey ])
343
368
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" )
348
373
}
374
+ sb .WriteString (color .Reset (colorKey ))
375
+ sb .WriteByte ('\n' )
349
376
}
0 commit comments