Skip to content

Commit e0afa0d

Browse files
committed
Keep data in UTC & preserve public API
This addresses PR feedback.
1 parent 040f1fa commit e0afa0d

6 files changed

+60
-43
lines changed

diff/diff.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ type FileDiff struct {
1515
// the original name of the file
1616
OrigName string
1717
// the original timestamp (nil if not present)
18-
OrigTime *time.Time
19-
OrigTimeHasTZ bool
18+
OrigTime *time.Time
2019
// the new name of the file (often same as OrigName)
2120
NewName string
2221
// the new timestamp (nil if not present)
23-
NewTime *time.Time
24-
NewTimeHasTZ bool
22+
NewTime *time.Time
2523
// extended header lines (e.g., git's "new mode <mode>", "rename from <path>", etc.)
2624
Extended []string
2725
// hunks that were changed from orig to new
2826
Hunks []*Hunk
29-
30-
includeTZ bool
3127
}
3228

3329
// A Hunk represents a series of changes (additions or deletions) in a file's
@@ -133,11 +129,6 @@ const diffTimeParseWithoutTZLayout = "2006-01-02 15:04:05"
133129
// See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html.
134130
const diffTimeFormatLayout = "2006-01-02 15:04:05.000000000 -0700"
135131

136-
// diffTimeFormatWithoutTZLayout is the layout used to format (i.e., print) the time in unified diff file
137-
// header timestamps without the timezone offset and the fractional seconds. This is used when the diff
138-
// does not include the timezone offset and fractional seconds.
139-
const diffTimeFormatWithoutTZLayout = "2006-01-02 15:04:05"
140-
141132
func (s *Stat) add(o Stat) {
142133
s.Added += o.Added
143134
s.Changed += o.Changed

diff/diff_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,13 +850,14 @@ func TestParseFileDiffAndPrintFileDiff(t *testing.T) {
850850

851851
func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
852852
tests := []struct {
853-
filename string
854-
wantParseErr error
855-
wantFileDiffs int // How many instances of diff.FileDiff are expected.
853+
filename string
854+
wantParseErr error
855+
wantFileDiffs int // How many instances of diff.FileDiff are expected.
856+
wantOutFileName string // If non-empty, the name of the file containing the expected output.
856857
}{
857858
{filename: "sample_multi_file.diff", wantFileDiffs: 2},
858859
{filename: "sample_multi_file_single.diff", wantFileDiffs: 1},
859-
{filename: "sample_multi_file_single_apple.diff", wantFileDiffs: 1},
860+
{filename: "sample_multi_file_single_apple_in.diff", wantFileDiffs: 1, wantOutFileName: "sample_multi_file_single_apple_out.diff"},
860861
{filename: "sample_multi_file_new.diff", wantFileDiffs: 3},
861862
{filename: "sample_multi_file_deleted.diff", wantFileDiffs: 3},
862863
{filename: "sample_multi_file_rename.diff", wantFileDiffs: 3},
@@ -893,6 +894,12 @@ func TestParseMultiFileDiffAndPrintMultiFileDiff(t *testing.T) {
893894
if err != nil {
894895
t.Errorf("%s: PrintMultiFileDiff: %s", test.filename, err)
895896
}
897+
if test.wantOutFileName != "" {
898+
diffData, err = ioutil.ReadFile(filepath.Join("testdata", test.wantOutFileName))
899+
if err != nil {
900+
t.Fatal(err)
901+
}
902+
}
896903
if !bytes.Equal(printed, diffData) {
897904
t.Errorf("%s: printed multi-file diff != original multi-file diff\n\n# PrintMultiFileDiff output - Original:\n%s", test.filename, cmp.Diff(diffData, printed))
898905
}

diff/parse.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,15 @@ func (r *FileDiffReader) ReadAllHeaders() (*FileDiff, error) {
217217
}
218218

219219
var origTime, newTime *time.Time
220-
var origTZ, newTZ bool
221-
fd.OrigName, fd.NewName, origTime, newTime, origTZ, newTZ, err = r.ReadFileHeaders()
220+
fd.OrigName, fd.NewName, origTime, newTime, err = r.ReadFileHeaders()
222221
if err != nil {
223222
return nil, err
224223
}
225224
if origTime != nil {
226225
fd.OrigTime = origTime
227-
fd.OrigTimeHasTZ = origTZ
228226
}
229227
if newTime != nil {
230228
fd.NewTime = newTime
231-
fd.NewTimeHasTZ = newTZ
232229
}
233230

234231
return fd, nil
@@ -250,21 +247,21 @@ func (r *FileDiffReader) HunksReader() *HunksReader {
250247
// start with "---" and "+++" with the orig/new file names and
251248
// timestamps). Or which starts with "Only in " with dir path and filename.
252249
// "Only in" message is supported in POSIX locale: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/diff.html#tag_20_34_10
253-
func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimestamp, newTimestamp *time.Time, origTZ, newTZ bool, err error) {
250+
func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimestamp, newTimestamp *time.Time, err error) {
254251
if r.fileHeaderLine != nil {
255252
if isOnlyMessage, source, filename := parseOnlyInMessage(r.fileHeaderLine); isOnlyMessage {
256253
return filepath.Join(string(source), string(filename)),
257-
"", nil, nil, false, false, nil
254+
"", nil, nil, nil
258255
}
259256
}
260-
origName, origTimestamp, origTZ, err = r.readOneFileHeader([]byte("--- "))
257+
origName, origTimestamp, err = r.readOneFileHeader([]byte("--- "))
261258
if err != nil {
262-
return "", "", nil, nil, false, false, err
259+
return "", "", nil, nil, err
263260
}
264261

265-
newName, newTimestamp, newTZ, err = r.readOneFileHeader([]byte("+++ "))
262+
newName, newTimestamp, err = r.readOneFileHeader([]byte("+++ "))
266263
if err != nil {
267-
return "", "", nil, nil, false, false, err
264+
return "", "", nil, nil, err
268265
}
269266

270267
unquotedOrigName, err := strconv.Unquote(origName)
@@ -276,29 +273,29 @@ func (r *FileDiffReader) ReadFileHeaders() (origName, newName string, origTimest
276273
newName = unquotedNewName
277274
}
278275

279-
return origName, newName, origTimestamp, newTimestamp, origTZ, newTZ, nil
276+
return origName, newName, origTimestamp, newTimestamp, nil
280277
}
281278

282279
// readOneFileHeader reads one of the file headers (prefix should be
283280
// either "+++ " or "--- ").
284-
func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, timestamp *time.Time, hadTZ bool, err error) {
281+
func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, timestamp *time.Time, err error) {
285282
var line []byte
286283

287284
if r.fileHeaderLine == nil {
288285
var err error
289286
line, err = r.reader.readLine()
290287
if err == io.EOF {
291-
return "", nil, false, &ParseError{r.line, r.offset, ErrNoFileHeader}
288+
return "", nil, &ParseError{r.line, r.offset, ErrNoFileHeader}
292289
} else if err != nil {
293-
return "", nil, false, err
290+
return "", nil, err
294291
}
295292
} else {
296293
line = r.fileHeaderLine
297294
r.fileHeaderLine = nil
298295
}
299296

300297
if !bytes.HasPrefix(line, prefix) {
301-
return "", nil, false, &ParseError{r.line, r.offset, ErrBadFileHeader}
298+
return "", nil, &ParseError{r.line, r.offset, ErrBadFileHeader}
302299
}
303300

304301
r.offset += int64(len(line))
@@ -308,7 +305,6 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time
308305
trimmedLine := strings.TrimSpace(string(line)) // filenames that contain spaces may be terminated by a tab
309306
parts := strings.SplitN(trimmedLine, "\t", 2)
310307
filename = parts[0]
311-
hadTZ = true
312308
if len(parts) == 2 {
313309
var ts time.Time
314310
// Timestamp is optional, but this header has it.
@@ -317,16 +313,14 @@ func (r *FileDiffReader) readOneFileHeader(prefix []byte) (filename string, time
317313
var err1 error
318314
ts, err1 = time.Parse(diffTimeParseWithoutTZLayout, parts[1])
319315
if err1 != nil {
320-
return "", nil, hadTZ, err
316+
return "", nil, err
321317
}
322-
hadTZ = false
323-
ts = ts.In(time.Now().Location())
324318
err = nil
325319
}
326320
timestamp = &ts
327321
}
328322

329-
return filename, timestamp, hadTZ, err
323+
return filename, timestamp, err
330324
}
331325

332326
// OverflowError is returned when we have overflowed into the start

diff/print.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ func PrintFileDiff(d *FileDiff) ([]byte, error) {
4949
return buf.Bytes(), nil
5050
}
5151

52-
if err := printFileHeader(&buf, "--- ", d.OrigName, d.OrigTime, d.OrigTimeHasTZ); err != nil {
52+
if err := printFileHeader(&buf, "--- ", d.OrigName, d.OrigTime); err != nil {
5353
return nil, err
5454
}
55-
if err := printFileHeader(&buf, "+++ ", d.NewName, d.NewTime, d.NewTimeHasTZ); err != nil {
55+
if err := printFileHeader(&buf, "+++ ", d.NewName, d.NewTime); err != nil {
5656
return nil, err
5757
}
5858

@@ -67,16 +67,12 @@ func PrintFileDiff(d *FileDiff) ([]byte, error) {
6767
return buf.Bytes(), nil
6868
}
6969

70-
func printFileHeader(w io.Writer, prefix string, filename string, timestamp *time.Time, tz bool) error {
70+
func printFileHeader(w io.Writer, prefix string, filename string, timestamp *time.Time) error {
7171
if _, err := fmt.Fprint(w, prefix, filename); err != nil {
7272
return err
7373
}
74-
format := diffTimeFormatLayout
75-
if !tz {
76-
format = diffTimeFormatWithoutTZLayout
77-
}
7874
if timestamp != nil {
79-
if _, err := fmt.Fprint(w, "\t", timestamp.Format(format)); err != nil {
75+
if _, err := fmt.Fprint(w, "\t", timestamp.Format(diffTimeFormatLayout)); err != nil {
8076
return err
8177
}
8278
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
diff -u a/oldname1 b/newname1
2+
--- oldname1 2009-10-11 15:12:20.000000000 +0000
3+
+++ newname1 2009-10-11 15:12:30.000000000 +0000
4+
@@ -1,3 +1,9 @@
5+
+This is an important
6+
+notice! It should
7+
+therefore be located at
8+
+the beginning of this
9+
+document!
10+
+
11+
This part of the
12+
document has stayed the
13+
same from version to
14+
@@ -5,16 +11,10 @@
15+
be shown if it doesn't
16+
change. Otherwise, that
17+
would not be helping to
18+
-compress the size of the
19+
-changes.
20+
-
21+
-This paragraph contains
22+
-text that is outdated.
23+
-It will be deleted in the
24+
-near future.
25+
+compress anything.
26+
27+
It is important to spell
28+
-check this dokument. On
29+
+check this document. On

0 commit comments

Comments
 (0)