Skip to content

Commit 8ad8efd

Browse files
committed
Add Ancestors(), rename Ancestor to Parent
1 parent b0b541b commit 8ad8efd

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

‎gitmap.go‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ type GitInfo struct {
4848
AuthorDate time.Time `json:"authorDate"` // The author date
4949
CommitDate time.Time `json:"commitDate"` // The commit date
5050
Body string `json:"body"` // The commit message body
51-
Ancestor *GitInfo `json:"ancestor"` // The file-filtered ancestor commit, if any
51+
Parent *GitInfo `json:"parent"` // The file-filtered ancestor commit, if any
52+
}
53+
54+
// Ancestors returns a slice of GitInfo objects representing the ancestors.
55+
func (g *GitInfo) Ancestors() []*GitInfo {
56+
var ancestors []*GitInfo
57+
for ancestor := g.Parent; ancestor != nil; ancestor = ancestor.Parent {
58+
ancestors = append(ancestors, ancestor)
59+
}
60+
return ancestors
5261
}
5362

5463
// Runner is an interface for running Git commands,
@@ -129,8 +138,8 @@ func Map(opts Options) (*GitRepo, error) {
129138
if ancInfo, ok = a[filename]; !ok {
130139
ancInfo = rootInfo
131140
}
132-
ancInfo.Ancestor = &gitInfoCopy
133-
a[filename] = ancInfo.Ancestor
141+
ancInfo.Parent = &gitInfoCopy
142+
a[filename] = ancInfo.Parent
134143
}
135144
}
136145
}

‎gitmap_test.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ func assertFile(
119119

120120
for i, e := range expected {
121121
if i > 0 {
122-
gi = gi.Ancestor
122+
if len(gi.Ancestors()) == 0 {
123+
t.Fatalf("Expected at least 1 ancestor commit for %s, but got none", filename)
124+
}
125+
gi = gi.Parent
123126
if gi == nil {
124127
t.Fatalf("Wrong number of ancestor commits, got %d, expected at least %d", i-1, i)
125128
}
129+
126130
}
127131
assertGitInfo(t, *gi,
128132
filename,
@@ -288,7 +292,7 @@ func TestEncodeJSON(t *testing.T) {
288292

289293
s := string(b)
290294

291-
if s != `{"hash":"1cb4bde80efbcc203ad14f8869c1fcca6ec830da","abbreviatedHash":"1cb4bde","subject":"Add some badges to README","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-20T00:11:54+02:00","commitDate":"2016-07-20T00:11:54+02:00","body":"","ancestor":{"hash":"527cb5db32c76a269e444bb0de4cc22b574f0366","abbreviatedHash":"527cb5d","subject":"Create README.md","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-19T21:21:03+02:00","commitDate":"2016-07-19T21:21:03+02:00","body":"","ancestor":null}}` {
295+
if s != `{"hash":"1cb4bde80efbcc203ad14f8869c1fcca6ec830da","abbreviatedHash":"1cb4bde","subject":"Add some badges to README","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-20T00:11:54+02:00","commitDate":"2016-07-20T00:11:54+02:00","body":"","parent":{"hash":"527cb5db32c76a269e444bb0de4cc22b574f0366","abbreviatedHash":"527cb5d","subject":"Create README.md","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-19T21:21:03+02:00","commitDate":"2016-07-19T21:21:03+02:00","body":"","parent":null}}` {
292296
t.Errorf("JSON marshal error: \n%s", s)
293297
}
294298
}

0 commit comments

Comments
 (0)