Skip to content

Support Apple Diff timestamps #65

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 2 commits into from
Oct 31, 2022
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: 4 additions & 0 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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))
}
Expand Down
11 changes: 8 additions & 3 deletions diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions diff/testdata/sample_multi_file_single_apple_in.diff
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions diff/testdata/sample_multi_file_single_apple_out.diff
Original file line number Diff line number Diff line change
@@ -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