From 56be0a579cf111a5f19f26891a04a89da30e994a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 7 Jul 2025 20:05:59 +0200 Subject: [PATCH] Add Ancestors(), rename Ancestor to Parent --- gitmap.go | 15 ++++++++++++--- gitmap_test.go | 8 ++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/gitmap.go b/gitmap.go index c8747db..2e2ee38 100644 --- a/gitmap.go +++ b/gitmap.go @@ -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, @@ -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 } } } diff --git a/gitmap_test.go b/gitmap_test.go index 47597cb..583854a 100644 --- a/gitmap_test.go +++ b/gitmap_test.go @@ -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, @@ -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) } }