diff --git a/diff/diff.go b/diff/diff.go index 0f465b9..81aa655 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -120,6 +120,10 @@ const onlyInMessage = "Only in %s: %s\n" // See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html. const diffTimeParseLayout = "2006-01-02 15:04:05 -0700" +// Apple's diff is based on freebsd diff, which uses a timestamp format that does +// not include the timezone offset. +const diffTimeParseWithoutTZLayout = "2006-01-02 15:04:05" + // diffTimeFormatLayout is the layout used to format (i.e., print) the time in unified diff file // header timestamps. // See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html. diff --git a/diff/diff_test.go b/diff/diff_test.go index 11c7b01..aaaa261 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -850,12 +850,14 @@ func TestParseFileDiffAndPrintFileDiff(t *testing.T) { func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) { tests := []struct { - filename string - wantParseErr error - wantFileDiffs int // How many instances of diff.FileDiff are expected. + filename string + wantParseErr error + wantFileDiffs int // How many instances of diff.FileDiff are expected. + wantOutFileName string // If non-empty, the name of the file containing the expected output. }{ {filename: "sample_multi_file.diff", wantFileDiffs: 2}, {filename: "sample_multi_file_single.diff", wantFileDiffs: 1}, + {filename: "sample_multi_file_single_apple_in.diff", wantFileDiffs: 1, wantOutFileName: "sample_multi_file_single_apple_out.diff"}, {filename: "sample_multi_file_new.diff", wantFileDiffs: 3}, {filename: "sample_multi_file_deleted.diff", wantFileDiffs: 3}, {filename: "sample_multi_file_rename.diff", wantFileDiffs: 3}, @@ -892,6 +894,12 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) { if err != nil { t.Errorf("%s: PrintMultiFileDiff: %s", test.filename, err) } + if test.wantOutFileName != "" { + diffData, err = ioutil.ReadFile(filepath.Join("testdata", test.wantOutFileName)) + if err != nil { + t.Fatal(err) + } + } if !bytes.Equal(printed, diffData) { t.Errorf("%s: printed multi-file diff != original multi-file diff\n\n# PrintMultiFileDiff output - Original:\n%s", test.filename, cmp.Diff(diffData, printed)) } diff --git a/diff/parse.go b/diff/parse.go index 4a0b823..f45c2f2 100644 --- a/diff/parse.go +++ b/diff/parse.go @@ -254,7 +254,6 @@ func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimest "", nil, nil, nil } } - origName, origTimestamp, err = r.readOneFileHeader([]byte("--- ")) if err != nil { return "", "", nil, nil, err @@ -307,10 +306,16 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time parts := strings.SplitN(trimmedLine, "\t", 2) filename = parts[0] if len(parts) == 2 { + var ts time.Time // Timestamp is optional, but this header has it. - ts, err := time.Parse(diffTimeParseLayout, parts[1]) + ts, err = time.Parse(diffTimeParseLayout, parts[1]) if err != nil { - return "", nil, err + var err1 error + ts, err1 = time.Parse(diffTimeParseWithoutTZLayout, parts[1]) + if err1 != nil { + return "", nil, err + } + err = nil } timestamp = &ts } diff --git a/diff/testdata/sample_multi_file_single_apple_in.diff b/diff/testdata/sample_multi_file_single_apple_in.diff new file mode 100644 index 0000000..9100c9d --- /dev/null +++ b/diff/testdata/sample_multi_file_single_apple_in.diff @@ -0,0 +1,29 @@ +diff -u a/oldname1 b/newname1 +--- oldname1 2009-10-11 15:12:20 ++++ newname1 2009-10-11 15:12:30 +@@ -1,3 +1,9 @@ ++This is an important ++notice! It should ++therefore be located at ++the beginning of this ++document! ++ + This part of the + document has stayed the + same from version to +@@ -5,16 +11,10 @@ + be shown if it doesn't + change. Otherwise, that + would not be helping to +-compress the size of the +-changes. +- +-This paragraph contains +-text that is outdated. +-It will be deleted in the +-near future. ++compress anything. + + It is important to spell +-check this dokument. On ++check this document. On diff --git a/diff/testdata/sample_multi_file_single_apple_out.diff b/diff/testdata/sample_multi_file_single_apple_out.diff new file mode 100644 index 0000000..3bcad6c --- /dev/null +++ b/diff/testdata/sample_multi_file_single_apple_out.diff @@ -0,0 +1,29 @@ +diff -u a/oldname1 b/newname1 +--- oldname1 2009-10-11 15:12:20.000000000 +0000 ++++ newname1 2009-10-11 15:12:30.000000000 +0000 +@@ -1,3 +1,9 @@ ++This is an important ++notice! It should ++therefore be located at ++the beginning of this ++document! ++ + This part of the + document has stayed the + same from version to +@@ -5,16 +11,10 @@ + be shown if it doesn't + change. Otherwise, that + would not be helping to +-compress the size of the +-changes. +- +-This paragraph contains +-text that is outdated. +-It will be deleted in the +-near future. ++compress anything. + + It is important to spell +-check this dokument. On ++check this document. On