Skip to content
Closed
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 .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: go
go:
- "1.14.x"
- "1.26.4"
script:
- GO111MODULE=on go get -t -v ./...
- diff -u <(echo -n) <(gofmt -d -s .)
Expand Down
22 changes: 11 additions & 11 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"github.com/google/go-cmp/cmp"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
Expand All @@ -30,7 +30,7 @@ func init() {

func TestParseHunkNoChunksize(t *testing.T) {
filename := "sample_no_chunksize.diff"
diffData, err := ioutil.ReadFile(filepath.Join("testdata", filename))
diffData, err := os.ReadFile(filepath.Join("testdata", filename))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestParseHunksAndPrintHunks(t *testing.T) {
{filename: "sample_hunk_lines_start_with_minuses_pluses.diff"},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
diffData, err := os.ReadFile(filepath.Join("testdata", test.filename))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestParseFileDiffHeaders(t *testing.T) {
}
for _, test := range tests {
t.Run(test.filename, func(t *testing.T) {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
diffData, err := os.ReadFile(filepath.Join("testdata", test.filename))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -835,7 +835,7 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
}
for _, test := range tests {
t.Run(test.filename, func(t *testing.T) {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
diffData, err := os.ReadFile(filepath.Join("testdata", test.filename))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -874,7 +874,7 @@ func TestParseFileDiffAndPrintFileDiff(t *testing.T) {
},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
diffData, err := os.ReadFile(filepath.Join("testdata", test.filename))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -922,7 +922,7 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
{filename: "sample_multi_file_without_extended.diff", wantFileDiffs: 2},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
diffData, err := os.ReadFile(filepath.Join("testdata", test.filename))
if err != nil {
t.Fatal(err)
}
Expand All @@ -944,7 +944,7 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
t.Errorf("%s: PrintMultiFileDiff: %s", test.filename, err)
}
if test.wantOutFileName != "" {
diffData, err = ioutil.ReadFile(filepath.Join("testdata", test.wantOutFileName))
diffData, err = os.ReadFile(filepath.Join("testdata", test.wantOutFileName))
if err != nil {
t.Fatal(err)
}
Expand All @@ -956,11 +956,11 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
}

func TestParseMultiFileDiffAndPrintMultiFileDiffIncludingTrailingContent(t *testing.T) {
testInput, err := ioutil.ReadFile(filepath.Join("testdata", "sample_multi_file_trailing_content.diff"))
testInput, err := os.ReadFile(filepath.Join("testdata", "sample_multi_file_trailing_content.diff"))
if err != nil {
t.Fatal(err)
}
expectedDiffs, err := ioutil.ReadFile(filepath.Join("testdata", "sample_multi_file_trailing_content_diffsonly.diff"))
expectedDiffs, err := os.ReadFile(filepath.Join("testdata", "sample_multi_file_trailing_content_diffsonly.diff"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1124,7 +1124,7 @@ func TestFileDiff_Stat(t *testing.T) {
}

func TestParseMultiFileDiff_Comprehensive(t *testing.T) {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", "sample_multi_file.diff"))
diffData, err := os.ReadFile(filepath.Join("testdata", "sample_multi_file.diff"))
if err != nil {
t.Fatal(err)
}
Expand Down
18 changes: 7 additions & 11 deletions diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -679,7 +680,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) {

// Parse hunk header.
r.hunk = &Hunk{}
items := []interface{}{
items := []any{
&r.hunk.OrigStartLine, &r.hunk.OrigLines,
&r.hunk.NewStartLine, &r.hunk.NewLines,
}
Expand Down Expand Up @@ -772,12 +773,7 @@ var linePrefixes = []byte{' ', '-', '+', '\\'}

// linePrefix returns true if 'c' is in 'linePrefixes'.
func linePrefix(c byte) bool {
for _, p := range linePrefixes {
if p == c {
return true
}
}
return false
return slices.Contains(linePrefixes, c)
}

// normalizeHeader takes a header of the form:
Expand Down Expand Up @@ -846,12 +842,12 @@ func parseOnlyInMessage(line []byte) (bool, []byte, []byte) {
return false, nil, nil
}
line = line[len(onlyInMessagePrefix):]
idx := bytes.Index(line, []byte(": "))
if idx < 0 {
before, after, ok := bytes.Cut(line, []byte(": "))
if !ok {
return false, nil, nil
}
filename := bytes.TrimSuffix(line[idx+2:], []byte("\r"))
return true, line[:idx], filename
filename := bytes.TrimSuffix(after, []byte("\r"))
return true, before, filename
}

// A ParseError is a description of a unified diff syntax error.
Expand Down
2 changes: 1 addition & 1 deletion diff/reader_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ index 0000000..3be2928

in := newLineReader(strings.NewReader(input))
out := []string{}
for i := 0; i < 4; i++ {
for range 4 {
l, err := in.readLine()
if err != nil {
t.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion diff/reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ func TestReverseRoundTripOnTestdata(t *testing.T) {
}

for _, fixture := range fixtures {
fixture := fixture
name := filepath.Base(fixture)

t.Run(name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/sourcegraph/go-diff

go 1.20
go 1.26.4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this forces consumer of this library to be on this version. I'd prefer supporting at most 1.24 if it provides improvements that actually are worth it. Otherwise we can use toolchain in our go.mod to use a newer version I think?


require github.com/google/go-cmp v0.5.2
Loading