Skip to content

support sorting multi-file diff before printing #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
t.Errorf("%s: got %v instances of diff.FileDiff, expected %v", test.filename, got, want)
}

printed, err := PrintMultiFileDiff(diffs)
printed, err := PrintMultiFileDiff(diffs, nil)
if err != nil {
t.Errorf("%s: PrintMultiFileDiff: %s", test.filename, err)
}
Expand Down
16 changes: 14 additions & 2 deletions diff/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import (
"bytes"
"fmt"
"io"
"sort"
"time"

"sourcegraph.com/sqs/pbtypes"
)

// PrintMultiFileDiff prints a multi-file diff in unified diff format.
func PrintMultiFileDiff(ds []*FileDiff) ([]byte, error) {
// PrintOptions specifies how to print a diff.
type PrintOptions struct {
Sort bool // sort by filename
}

// PrintMultiFileDiff prints a multi-file diff in unified diff format with the specified options.
func PrintMultiFileDiff(ds []*FileDiff, opt *PrintOptions) ([]byte, error) {
if opt != nil && opt.Sort {
sort.Slice(ds, func(i, j int) bool {
return ds[i].NewName < ds[j].NewName
})
}

var buf bytes.Buffer
for _, d := range ds {
diff, err := PrintFileDiff(d)
Expand Down