Skip to content
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
15 changes: 12 additions & 3 deletions gitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ type GitInfo struct {
AuthorDate time.Time `json:"authorDate"` // The author date
CommitDate time.Time `json:"commitDate"` // The commit date
Body string `json:"body"` // The commit message body
Ancestor *GitInfo `json:"ancestor"` // The file-filtered ancestor commit, if any
Parent *GitInfo `json:"parent"` // The file-filtered ancestor commit, if any
}

// Ancestors returns a slice of GitInfo objects representing the ancestors.
func (g *GitInfo) Ancestors() []*GitInfo {
var ancestors []*GitInfo
for parent := g.Parent; parent != nil; parent = parent.Parent {
ancestors = append(ancestors, parent)
}
return ancestors
}

// Runner is an interface for running Git commands,
Expand Down Expand Up @@ -129,8 +138,8 @@ func Map(opts Options) (*GitRepo, error) {
if ancInfo, ok = a[filename]; !ok {
ancInfo = rootInfo
}
ancInfo.Ancestor = &gitInfoCopy
a[filename] = ancInfo.Ancestor
ancInfo.Parent = &gitInfoCopy
a[filename] = ancInfo.Parent
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions gitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ func assertFile(

for i, e := range expected {
if i > 0 {
gi = gi.Ancestor
if len(gi.Ancestors()) == 0 {
t.Fatalf("Expected at least 1 ancestor commit for %s, but got none", filename)
}
gi = gi.Parent
if gi == nil {
t.Fatalf("Wrong number of ancestor commits, got %d, expected at least %d", i-1, i)
}

}
assertGitInfo(t, *gi,
filename,
Expand Down Expand Up @@ -288,7 +292,7 @@ func TestEncodeJSON(t *testing.T) {

s := string(b)

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}}` {
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}}` {
t.Errorf("JSON marshal error: \n%s", s)
}
}
Expand Down
Loading