Skip to content

Commit ed866b0

Browse files
committed
add support for trailing spaces
1 parent 9f2e185 commit ed866b0

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

.github/workflows/scip.yml

-18
This file was deleted.

cmd/go-diff/go-diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"log"
88
"os"
99

10-
"github.com/sourcegraph/go-diff/diff"
10+
"github.com/prassoai/go-diff/diff"
1111
)
1212

1313
// A diagnostic program to aid in debugging diff parsing or printing

diff/diff_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,15 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
723723
{
724724
filename: "complicated_filenames.diff",
725725
wantDiffs: []*FileDiff{
726+
{
727+
OrigName: "/dev/null",
728+
NewName: "b/trailing_space ",
729+
Extended: []string{
730+
"diff --git a/trailing_space b/trailing_space ",
731+
"new file mode 100644",
732+
"index 0000000..c3ed4be",
733+
},
734+
},
726735
{
727736
OrigName: "/dev/null",
728737
NewName: "b/new empty file with spaces",

diff/parse.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,13 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time
302302
r.line++
303303
line = line[len(prefix):]
304304

305-
trimmedLine := strings.TrimSpace(string(line)) // filenames that contain spaces may be terminated by a tab
306-
parts := strings.SplitN(trimmedLine, "\t", 2)
305+
// Trim only newline characters so that trailing spaces (and a tab delimiter) are preserved.
306+
cleanedLine := strings.TrimRight(string(line), "\r\n")
307+
parts := strings.SplitN(cleanedLine, "\t", 2)
307308
filename = parts[0]
308-
if len(parts) == 2 {
309+
310+
// Only try to parse the timestamp if a non-empty value is present.
311+
if len(parts) == 2 && parts[1] != "" {
309312
var ts time.Time
310313
// Timestamp is optional, but this header has it.
311314
ts, err = time.Parse(diffTimeParseLayout, parts[1])

diff/testdata/complicated_filenames.diff

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
diff --git a/trailing_space b/trailing_space
2+
new file mode 100644
3+
index 0000000..c3ed4be
4+
--- /dev/null
5+
+++ b/trailing_space
16
diff --git a/new empty file with spaces b/new empty file with spaces
27
new file mode 100644
38
index 0000000..e69de29

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/sourcegraph/go-diff
1+
module github.com/prassoai/go-diff
22

33
go 1.14
44

0 commit comments

Comments
 (0)