From d80594f2d5cb1a246d75a4726a8af099e94acd88 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Mon, 30 Oct 2017 23:54:56 -0400 Subject: [PATCH] Add support for parsing header timestamps without fractional seconds. We need to use a different layout string for parsing and printing, since we only want to change the parsing behavior (not printing behavior). Add test for it. Resolves #17. --- diff/diff.go | 13 +++++++++---- diff/diff_test.go | 9 +++++++++ diff/parse.go | 2 +- diff/print.go | 2 +- .../testdata/sample_file_no_fractional_seconds.diff | 11 +++++++++++ 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 diff/testdata/sample_file_no_fractional_seconds.diff diff --git a/diff/diff.go b/diff/diff.go index 0f1c530..646602a 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -59,10 +59,15 @@ var ( const hunkHeader = "@@ -%d,%d +%d,%d @@" -// diffTimeFormat is the time format string for unified diff file -// header timestamps. See -// http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html. -const diffTimeFormat = "2006-01-02 15:04:05.000000000 -0700" +// diffTimeParseLayout is the layout used to parse the time in unified diff file +// header timestamps. +// See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html. +const diffTimeParseLayout = "2006-01-02 15:04:05 -0700" + +// 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. +const diffTimeFormatLayout = "2006-01-02 15:04:05.000000000 -0700" func (s *Stat) add(o Stat) { s.Added += o.Added diff --git a/diff/diff_test.go b/diff/diff_test.go index 8f22173..6982239 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -108,6 +108,15 @@ func TestParseFileDiffHeaders(t *testing.T) { NewTime: &pbtypes.Timestamp{Seconds: 1255273950}, }, }, + { + filename: "sample_file_no_fractional_seconds.diff", + wantDiff: &FileDiff{ + OrigName: "goyaml.go", + OrigTime: &pbtypes.Timestamp{Seconds: 1322164040}, + NewName: "goyaml.go", + NewTime: &pbtypes.Timestamp{Seconds: 1322486679}, + }, + }, { filename: "sample_file_extended.diff", wantDiff: &FileDiff{ diff --git a/diff/parse.go b/diff/parse.go index c19311b..1e77df4 100644 --- a/diff/parse.go +++ b/diff/parse.go @@ -267,7 +267,7 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time filename = parts[0] if len(parts) == 2 { // Timestamp is optional, but this header has it. - ts, err := time.Parse(diffTimeFormat, parts[1]) + ts, err := time.Parse(diffTimeParseLayout, parts[1]) if err != nil { return "", nil, err } diff --git a/diff/print.go b/diff/print.go index 801d2ce..d440cb9 100644 --- a/diff/print.go +++ b/diff/print.go @@ -71,7 +71,7 @@ func printFileHeader(w io.Writer, prefix string, filename string, timestamp *tim return err } if timestamp != nil { - if _, err := fmt.Fprint(w, "\t", timestamp.Format(diffTimeFormat)); err != nil { + if _, err := fmt.Fprint(w, "\t", timestamp.Format(diffTimeFormatLayout)); err != nil { return err } } diff --git a/diff/testdata/sample_file_no_fractional_seconds.diff b/diff/testdata/sample_file_no_fractional_seconds.diff new file mode 100644 index 0000000..7e05f0f --- /dev/null +++ b/diff/testdata/sample_file_no_fractional_seconds.diff @@ -0,0 +1,11 @@ +--- goyaml.go 2011-11-24 19:47:20 +0000 ++++ goyaml.go 2011-11-28 13:24:39 +0000 +@@ -256,7 +256,7 @@ + switch v.Kind() { + case reflect.String: + return len(v.String()) == 0 +- case reflect.Interface: ++ case reflect.Interface, reflect.Ptr: + return v.IsNil() + case reflect.Slice: + return v.Len() == 0