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 1 commit
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
56 changes: 56 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,60 @@ 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,
},
},
},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
Expand Down Expand Up @@ -574,6 +628,8 @@ 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},
}
for _, test := range tests {
diffData, err := ioutil.ReadFile(filepath.Join("testdata", test.filename))
Expand Down
38 changes: 36 additions & 2 deletions 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,31 @@ 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 starts with "Only in " message with dir path filename.
func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimestamp, newTimestamp *time.Time, err error) {
if (r.fileHeaderLine != nil) && (bytes.HasPrefix(r.fileHeaderLine, onlyInMessagePrefix)) {
path := bytes.Split(bytes.TrimPrefix(
bytes.TrimSuffix(r.fileHeaderLine, []byte("\n")),
onlyInMessagePrefix), []byte(":"))
if len(path) != 2 {
return "", "", nil, nil,
&ParseError{r.line, r.offset, ErrBadOnlyInMessage}
}

source, filename := string(bytes.TrimSpace(path[0])), string(bytes.TrimSpace(path[1]))

unquotedSource, err := strconv.Unquote(source)
if err == nil {
source = unquotedSource
}
unquotedFilename, err := strconv.Unquote(filename)
if err == nil {
filename = unquotedFilename
}

return filepath.Join(source, filename), "", nil, nil, nil
}

origName, origTimestamp, err = r.readOneFileHeader([]byte("--- "))
if err != nil {
return "", "", nil, nil, err
Expand Down Expand Up @@ -324,7 +354,7 @@ func (r *FileDiffReader) ReadExtendedHeaders() ([]string, error) {
return xheaders, OverflowError(line)
}
}
if bytes.HasPrefix(line, []byte("--- ")) {
if bytes.HasPrefix(line, []byte("--- ")) || (bytes.HasPrefix(line, onlyInMessagePrefix)) {
// We've reached the file header.
r.fileHeaderLine = line // pass to readOneFileHeader (see fileHeaderLine field doc)
return xheaders, nil
Expand Down Expand Up @@ -403,6 +433,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
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