Skip to content

Add handling added/deleted files in diff #50

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

Merged
Merged
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
4 changes: 3 additions & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ func (h *Hunk) Stat() Stat {
}

var (
hunkPrefix = []byte("@@ ")
hunkPrefix = []byte("@@ ")
onlyInMessagePrefix = []byte("Only in ")
)

const hunkHeader = "@@ -%d,%d +%d,%d @@"
const onlyInMessage = "Only in %s: %s\n"

// diffTimeParseLayout is the layout used to parse the time in unified diff file
// header timestamps.
Expand Down
121 changes: 121 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,123 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
},
},
},
{
filename: "sample_contains_added_deleted_files.diff",
wantDiffs: []*FileDiff{
{
OrigName: "source_a/file_1.txt",
OrigTime: nil,
NewName: "source_b/file_1.txt",
NewTime: nil,
Extended: []string{
"diff -u source_a/file_1.txt source_b/file_1.txt",
},
},
{
OrigName: "source_a/file_2.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
{
OrigName: "source_b/file_3.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
},
},
{
filename: "sample_contains_only_added_deleted_files.diff",
wantDiffs: []*FileDiff{
{
OrigName: "source_a/file_1.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
{
OrigName: "source_a/file_2.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
{
OrigName: "source_b/file_3.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
},
},
{
filename: "sample_onlyin_line_isnt_a_file_header.diff",
wantDiffs: []*FileDiff{
{
OrigName: "source_a/file_1.txt",
OrigTime: nil,
NewName: "source_b/file_1.txt",
NewTime: nil,
Extended: []string{
"diff -u source_a/file_1.txt source_b/file_1.txt",
},
},
{
OrigName: "source_a/file_2.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: []string{
"Only in universe!",
},
},
{
OrigName: "source_b/file_3.txt some unrelated stuff here.",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
{
OrigName: "source_b/file_3.txt",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
},
},
{
filename: "sample_onlyin_complex_filenames.diff",
wantDiffs: []*FileDiff{
{
OrigName: "internal/trace/foo bar/bam",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
{
OrigName: "internal/trace/foo bar/bam: bar",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
{
OrigName: "internal/trace/hello/world: bazz",
OrigTime: nil,
NewName: "",
NewTime: nil,
Extended: nil,
},
},
},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
Expand Down Expand Up @@ -574,6 +691,10 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
{filename: "long_line_multi.diff", wantFileDiffs: 3},
{filename: "empty.diff", wantFileDiffs: 0},
{filename: "empty_multi.diff", wantFileDiffs: 2},
{filename: "sample_contains_added_deleted_files.diff", wantFileDiffs: 3},
{filename: "sample_contains_only_added_deleted_files.diff", wantFileDiffs: 3},
{filename: "sample_onlyin_line_isnt_a_file_header.diff", wantFileDiffs: 4},
{filename: "sample_onlyin_complex_filenames.diff", wantFileDiffs: 3},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
Expand Down
40 changes: 39 additions & 1 deletion diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -72,6 +73,12 @@ func (r *MultiFileDiffReader) ReadFile() (*FileDiff, error) {
}
}

// FileDiff is added/deleted file
// No further collection of hunks needed
if fd.NewName == "" {
return fd, nil
}

// Before reading hunks, check to see if there are any. If there
// aren't any, and there's another file after this file in the
// diff, then the hunks reader will complain ErrNoHunkHeader. It's
Expand Down Expand Up @@ -223,8 +230,16 @@ func (r *FileDiffReader) HunksReader() *HunksReader {

// ReadFileHeaders reads the unified file diff header (the lines that
// start with "---" and "+++" with the orig/new file names and
// timestamps).
// timestamps). Or which starts with "Only in " with dir path and filename.
// "Only in" message is supported in POSIX locale: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/diff.html#tag_20_34_10
func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimestamp, newTimestamp *time.Time, err error) {
if r.fileHeaderLine != nil {
if isOnlyMessage, source, filename := parseOnlyInMessage(r.fileHeaderLine); isOnlyMessage {
return filepath.Join(string(source), string(filename)),
"", nil, nil, nil
}
}

origName, origTimestamp, err = r.readOneFileHeader([]byte("--- "))
if err != nil {
return "", "", nil, nil, err
Expand Down Expand Up @@ -330,6 +345,12 @@ func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error) {
return xheaders, nil
}

// Reached message that file is added/deleted
if isOnlyInMessage, _, _ := parseOnlyInMessage(line); isOnlyInMessage {
r.fileHeaderLine = line // pass to readOneFileHeader (see fileHeaderLine field doc)
return xheaders, nil
}

r.line++
r.offset += int64(len(line))
xheaders = append(xheaders, string(line))
Expand Down Expand Up @@ -403,6 +424,10 @@ var (

// ErrExtendedHeadersEOF is when an EOF was encountered while reading extended file headers, which means that there were no ---/+++ headers encountered before hunks (if any) began.
ErrExtendedHeadersEOF = errors.New("expected file header while reading extended headers, got EOF")

// ErrBadOnlyInMessage is when a file have a malformed `only in` message
// Should be in format `Only in {source}: {filename}`
ErrBadOnlyInMessage = errors.New("bad 'only in' message")
)

// ParseHunks parses hunks from a unified diff. The diff must consist
Expand Down Expand Up @@ -612,6 +637,19 @@ func (r *HunksReader) ReadAllHunks() ([]*Hunk, error) {
}
}

// parseOnlyInMessage checks if line is a "Only in {source}: {filename}" and returns source and filename
func parseOnlyInMessage(line []byte) (bool, []byte, []byte) {
if !bytes.HasPrefix(line, onlyInMessagePrefix) {
return false, nil, nil
}
line = line[len(onlyInMessagePrefix):]
idx := bytes.Index(line, []byte(": "))
if idx < 0 {
return false, nil, nil
}
return true, line[:idx], line[idx+2:]
}

// A ParseError is a description of a unified diff syntax error.
type ParseError struct {
Line int // Line where the error occurred
Expand Down
11 changes: 11 additions & 0 deletions diff/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"path/filepath"
"time"

"sourcegraph.com/sqs/pbtypes"
Expand Down Expand Up @@ -36,6 +37,16 @@ func PrintFileDiff(d *FileDiff) ([]byte, error) {
}
}

// FileDiff is added/deleted file
// No further hunks printing needed
if d.NewName == "" {
_, err := fmt.Fprintf(&buf, onlyInMessage, filepath.Dir(d.OrigName), filepath.Base(d.OrigName))
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

if d.Hunks == nil {
return buf.Bytes(), nil
}
Expand Down
11 changes: 11 additions & 0 deletions diff/testdata/sample_contains_added_deleted_files.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
diff -u source_a/file_1.txt source_b/file_1.txt
--- source_a/file_1.txt
+++ source_b/file_1.txt
@@ -2,3 +3,4 @@
To be, or not to be, that is the question:
-Whether 'tis nobler in the mind to suffer
+The slings and arrows of outrageous fortune,
+Or to take arms against a sea of troubles
And by opposing end them. To die—to sleep,
Only in source_a: file_2.txt
Only in source_b: file_3.txt
3 changes: 3 additions & 0 deletions diff/testdata/sample_contains_only_added_deleted_files.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Only in source_a: file_1.txt
Only in source_a: file_2.txt
Only in source_b: file_3.txt
3 changes: 3 additions & 0 deletions diff/testdata/sample_onlyin_complex_filenames.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Only in internal/trace/foo bar: bam
Only in internal/trace/foo bar: bam: bar
Only in internal/trace/hello: world: bazz
13 changes: 13 additions & 0 deletions diff/testdata/sample_onlyin_line_isnt_a_file_header.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff -u source_a/file_1.txt source_b/file_1.txt
--- source_a/file_1.txt
+++ source_b/file_1.txt
@@ -2,3 +3,4 @@
To be, or not to be, that is the question:
-Whether 'tis nobler in the mind to suffer
+The slings and arrows of outrageous fortune,
+Or to take arms against a sea of troubles
And by opposing end them. To die—to sleep,
Only in universe!
Only in source_a: file_2.txt
Only in source_b: file_3.txt some unrelated stuff here.
Only in source_b: file_3.txt