Skip to content

Commit 3fc2099

Browse files
committed
Warn about weird dates
Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
1 parent 088e453 commit 3fc2099

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

internal/plumbing/day.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plumbing
22

33
import (
4+
"log"
45
"time"
56

67
"gopkg.in/src-d/go-git.v4"
@@ -13,6 +14,7 @@ import (
1314
// It is a PipelineItem.
1415
type DaysSinceStart struct {
1516
core.NoopMerger
17+
remote string
1618
day0 *time.Time
1719
previousDay int
1820
commits map[int][]plumbing.Hash
@@ -75,6 +77,9 @@ func (days *DaysSinceStart) Initialize(repository *git.Repository) error {
7577
delete(days.commits, key)
7678
}
7779
}
80+
if r, err := repository.Remotes(); err == nil && len(r) > 0 {
81+
days.remote = r[0].Config().URLs[0]
82+
}
7883
return nil
7984
}
8085

@@ -88,9 +93,13 @@ func (days *DaysSinceStart) Consume(deps map[string]interface{}) (map[string]int
8893
index := deps[core.DependencyIndex].(int)
8994
if index == 0 {
9095
// first iteration - initialize the file objects from the tree
91-
*days.day0 = commit.Committer.When
9296
// our precision is 1 day
93-
*days.day0 = days.day0.Truncate(24 * time.Hour)
97+
*days.day0 = commit.Committer.When.Truncate(24 * time.Hour)
98+
if days.day0.Unix() < 631152000 { // 01.01.1990, that was 30 years ago
99+
log.Println()
100+
log.Printf("Warning: suspicious committer timestamp in %s > %s",
101+
days.remote, commit.Hash.String())
102+
}
94103
}
95104
day := int(commit.Committer.When.Sub(*days.day0).Hours() / 24)
96105
if day < days.previousDay {

internal/plumbing/day_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package plumbing
22

33
import (
4+
"bytes"
5+
"log"
6+
"os"
47
"testing"
8+
"time"
59

610
"github.com/stretchr/testify/assert"
711
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -123,3 +127,29 @@ func TestDaysSinceStartFork(t *testing.T) {
123127
// just for the sake of it
124128
dss1.Merge([]core.PipelineItem{dss2})
125129
}
130+
131+
func TestDaysSinceStartConsumeZero(t *testing.T) {
132+
dss := fixtureDaysSinceStart()
133+
deps := map[string]interface{}{}
134+
commit, _ := test.Repository.CommitObject(plumbing.NewHash(
135+
"cce947b98a050c6d356bc6ba95030254914027b1"))
136+
commit.Committer.When = time.Unix(0, 0)
137+
deps[core.DependencyCommit] = commit
138+
deps[core.DependencyIndex] = 0
139+
// print warning to log
140+
myOutput := &bytes.Buffer{}
141+
log.SetOutput(myOutput)
142+
defer func() {
143+
log.SetOutput(os.Stderr)
144+
}()
145+
res, err := dss.Consume(deps)
146+
assert.Nil(t, err)
147+
assert.Contains(t, myOutput.String(), "Warning")
148+
assert.Contains(t, myOutput.String(), "cce947b98a050c6d356bc6ba95030254914027b1")
149+
assert.Contains(t, myOutput.String(), "src-d/hercules")
150+
assert.Equal(t, res[DependencyDay].(int), 0)
151+
assert.Equal(t, dss.previousDay, 0)
152+
assert.Equal(t, dss.day0.Hour(), 1)
153+
assert.Equal(t, dss.day0.Minute(), 0)
154+
assert.Equal(t, dss.day0.Second(), 0)
155+
}

internal/test/repository.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package test
22

33
import (
44
"io"
5+
"io/ioutil"
56
"os"
7+
"path"
68

7-
git "gopkg.in/src-d/go-git.v4"
9+
"gopkg.in/src-d/go-git.v4"
810
"gopkg.in/src-d/go-git.v4/plumbing"
911
"gopkg.in/src-d/go-git.v4/plumbing/object"
1012
"gopkg.in/src-d/go-git.v4/storage/memory"
11-
"io/ioutil"
12-
"path"
1313
)
1414

1515
// Repository is a boilerplate sample repository (Hercules itself).

0 commit comments

Comments
 (0)