Skip to content
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

Add support for parsing header timestamps without fractional seconds. #18

Merged
merged 1 commit into from
Oct 31, 2017
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
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.
  • Loading branch information
dmitshur committed Oct 31, 2017
commit d80594f2d5cb1a246d75a4726a8af099e94acd88
13 changes: 9 additions & 4 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion diff/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion diff/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
11 changes: 11 additions & 0 deletions diff/testdata/sample_file_no_fractional_seconds.diff
Original file line number Diff line number Diff line change
@@ -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