Skip to content

Commit c0b7156

Browse files
dmitshursqs
authored andcommitted
Add support for parsing header timestamps without fractional seconds. (#18)
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.
1 parent 748a069 commit c0b7156

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

diff/diff.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ var (
5959

6060
const hunkHeader = "@@ -%d,%d +%d,%d @@"
6161

62-
// diffTimeFormat is the time format string for unified diff file
63-
// header timestamps. See
64-
// http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html.
65-
const diffTimeFormat = "2006-01-02 15:04:05.000000000 -0700"
62+
// diffTimeParseLayout is the layout used to parse the time in unified diff file
63+
// header timestamps.
64+
// See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html.
65+
const diffTimeParseLayout = "2006-01-02 15:04:05 -0700"
66+
67+
// diffTimeFormatLayout is the layout used to format (i.e., print) the time in unified diff file
68+
// header timestamps.
69+
// See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html.
70+
const diffTimeFormatLayout = "2006-01-02 15:04:05.000000000 -0700"
6671

6772
func (s *Stat) add(o Stat) {
6873
s.Added += o.Added

diff/diff_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ func TestParseFileDiffHeaders(t *testing.T) {
108108
NewTime: &pbtypes.Timestamp{Seconds: 1255273950},
109109
},
110110
},
111+
{
112+
filename: "sample_file_no_fractional_seconds.diff",
113+
wantDiff: &FileDiff{
114+
OrigName: "goyaml.go",
115+
OrigTime: &pbtypes.Timestamp{Seconds: 1322164040},
116+
NewName: "goyaml.go",
117+
NewTime: &pbtypes.Timestamp{Seconds: 1322486679},
118+
},
119+
},
111120
{
112121
filename: "sample_file_extended.diff",
113122
wantDiff: &FileDiff{

diff/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time
267267
filename = parts[0]
268268
if len(parts) == 2 {
269269
// Timestamp is optional, but this header has it.
270-
ts, err := time.Parse(diffTimeFormat, parts[1])
270+
ts, err := time.Parse(diffTimeParseLayout, parts[1])
271271
if err != nil {
272272
return "", nil, err
273273
}

diff/print.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func printFileHeader(w io.Writer, prefix string, filename string, timestamp *tim
7171
return err
7272
}
7373
if timestamp != nil {
74-
if _, err := fmt.Fprint(w, "\t", timestamp.Format(diffTimeFormat)); err != nil {
74+
if _, err := fmt.Fprint(w, "\t", timestamp.Format(diffTimeFormatLayout)); err != nil {
7575
return err
7676
}
7777
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- goyaml.go 2011-11-24 19:47:20 +0000
2+
+++ goyaml.go 2011-11-28 13:24:39 +0000
3+
@@ -256,7 +256,7 @@
4+
switch v.Kind() {
5+
case reflect.String:
6+
return len(v.String()) == 0
7+
- case reflect.Interface:
8+
+ case reflect.Interface, reflect.Ptr:
9+
return v.IsNil()
10+
case reflect.Slice:
11+
return v.Len() == 0

0 commit comments

Comments
 (0)