Skip to content

Commit 1e936be

Browse files
committed
support sorting multi-file diff before printing
Makes it easier to canonicalize the output of `PrintMultiFileDiff` by adding a new `PrintOptions` arg that lets the caller specify that the diffs should be sorted before printing.
1 parent 97e3e6d commit 1e936be

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

diff/diff_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
510510
t.Errorf("%s: got %v instances of diff.FileDiff, expected %v", test.filename, got, want)
511511
}
512512

513-
printed, err := PrintMultiFileDiff(diffs)
513+
printed, err := PrintMultiFileDiff(diffs, nil)
514514
if err != nil {
515515
t.Errorf("%s: PrintMultiFileDiff: %s", test.filename, err)
516516
}

diff/print.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"sort"
78
"time"
89

910
"sourcegraph.com/sqs/pbtypes"
1011
)
1112

12-
// PrintMultiFileDiff prints a multi-file diff in unified diff format.
13-
func PrintMultiFileDiff(ds []*FileDiff) ([]byte, error) {
13+
// PrintOptions specifies how to print a diff.
14+
type PrintOptions struct {
15+
Sort bool // sort by filename
16+
}
17+
18+
// PrintMultiFileDiff prints a multi-file diff in unified diff format with the specified options.
19+
func PrintMultiFileDiff(ds []*FileDiff, opt *PrintOptions) ([]byte, error) {
20+
if opt != nil && opt.Sort {
21+
sort.Slice(ds, func(i, j int) bool {
22+
return ds[i].NewName < ds[j].NewName
23+
})
24+
}
25+
1426
var buf bytes.Buffer
1527
for _, d := range ds {
1628
diff, err := PrintFileDiff(d)

0 commit comments

Comments
 (0)