Skip to content
Merged

MIsc #20

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
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ jobs:
test:
strategy:
matrix:
go-version: [1.21.x, 1.22.x]
go-version: [1.23.x, 1.24.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
with:
go-version: ${{ matrix.go-version }}
- name: Install staticcheck
Expand All @@ -22,7 +22,7 @@ jobs:
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
shell: bash
- name: Checkout code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
- name: Fmt
Expand Down
17 changes: 14 additions & 3 deletions gitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,29 @@ 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
Parent *GitInfo `json:"parent"` // The file-filtered ancestor commit, if any
Parent *GitInfo `json:"-"` // 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
func (g *GitInfo) Ancestors() GitInfos {
var ancestors GitInfos
for parent := g.Parent; parent != nil; parent = parent.Parent {
ancestors = append(ancestors, parent)
}
return ancestors
}

type GitInfos []*GitInfo

// Reverse creates a copy of the GitInfos slice in reverse order.
func (g GitInfos) Reverse() GitInfos {
reversed := make(GitInfos, len(g))
for i, v := range g {
reversed[len(g)-1-i] = v
}
return reversed
}

// Runner is an interface for running Git commands,
// as implemented buy *exec.Cmd.
type Runner interface {
Expand Down
92 changes: 51 additions & 41 deletions gitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"strings"
"testing"

qt "github.com/frankban/quicktest"
)

type expectedGitInfo struct {
Expand All @@ -32,23 +34,16 @@ func init() {
}

func TestMap(t *testing.T) {
var (
gm GitMap
gr *GitRepo
err error
)
c := qt.New(t)

if gr, err = Map(Options{Repository: repository, Revision: revision}); err != nil {
t.Fatal(err)
}
gr, err := Map(Options{Repository: repository, Revision: revision})
c.Assert(err, qt.IsNil)

gm = gr.Files
gm := gr.Files

if len(gm) != 11 {
t.Fatalf("Wrong number of files, got %d, expected %d", len(gm), 9)
}
c.Assert(len(gm), qt.Equals, 11)

assertFile(t, gm, "testfiles/d1/d1.txt",
assertFile(c, gm, "testfiles/d1/d1.txt",
expectedGitInfo{
"39120eb",
"39120eb28a2f8a0312f9b45f91b6abb687b7fd3c",
Expand All @@ -69,7 +64,7 @@ func TestMap(t *testing.T) {
},
)

assertFile(t, gm, "testfiles/d2/d2.txt",
assertFile(c, gm, "testfiles/d2/d2.txt",
expectedGitInfo{
"39120eb",
"39120eb28a2f8a0312f9b45f91b6abb687b7fd3c",
Expand All @@ -83,7 +78,7 @@ func TestMap(t *testing.T) {
},
)

assertFile(t, gm, "testfiles/amended.txt",
assertFile(c, gm, "testfiles/amended.txt",
expectedGitInfo{
"7d46b65",
"7d46b653c9674510d808815c4c92c7dc10bedc16",
Expand All @@ -92,7 +87,7 @@ func TestMap(t *testing.T) {
},
)

assertFile(t, gm, "README.md",
assertFile(c, gm, "README.md",
expectedGitInfo{
"0b830e4",
"0b830e458446fdb774b1688af9b402acf388d6ab",
Expand All @@ -103,7 +98,7 @@ func TestMap(t *testing.T) {
}

func assertFile(
t *testing.T,
t *qt.C,
gm GitMap,
filename string,
expected ...expectedGitInfo,
Expand All @@ -126,7 +121,6 @@ func assertFile(
if gi == nil {
t.Fatalf("Wrong number of ancestor commits, got %d, expected at least %d", i-1, i)
}

}
assertGitInfo(t, *gi,
filename,
Expand All @@ -139,7 +133,7 @@ func assertFile(
}

func assertGitInfo(
t *testing.T,
t *qt.C,
gi GitInfo,
filename string,
expectedAbbreviatedHash,
Expand Down Expand Up @@ -263,38 +257,45 @@ func TestGitExecutableNotFound(t *testing.T) {
}

func TestEncodeJSON(t *testing.T) {
var (
gm GitMap
gr *GitRepo
gi *GitInfo
err error
ok bool
const (
filename = "README.md"
// Commit ref for README.md with 1 ancestor commit,
// so we don't have to write a *lot* of JSON below
revision = "1cb4bde80efbcc203ad14f8869c1fcca6ec830da"
)

if gr, err = Map(Options{Repository: repository, Revision: revision}); err != nil {
t.Fatal(err)
}
c := qt.New(t)

gm = gr.Files

if gi, ok = gm[filename]; !ok {
t.Fatal(filename)
}
gi := getOne(c, revision, filename)

b, err := json.Marshal(&gi)
if err != nil {
t.Fatal(err)
}
c.Assert(err, qt.IsNil)

s := string(b)
c.Assert(s, qt.Equals, `{"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":""}`)
}

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)
}
func TestAncestors(t *testing.T) {
const (
filename = "README.md"
revision = "HEAD"
)

c := qt.New(t)

gi := getOne(c, revision, filename)

ancestors := gi.Ancestors()
first := ancestors[0]
last := ancestors[len(ancestors)-1]
c.Assert(len(ancestors), qt.Equals, 5)
c.Assert(first.Subject, qt.Equals, "Add some more to README")
c.Assert(last.Subject, qt.Equals, "Create README.md")

reversed := ancestors.Reverse()
// Verify that the original slice is not modified.
c.Assert(ancestors[0].Subject, qt.Equals, first.Subject)
c.Assert(len(reversed), qt.Equals, 5)
c.Assert(reversed[0].Subject, qt.Equals, last.Subject)
c.Assert(reversed[len(reversed)-1].Subject, qt.Equals, first.Subject)
}

func TestGitRevisionNotFound(t *testing.T) {
Expand Down Expand Up @@ -340,3 +341,12 @@ func BenchmarkMap(b *testing.B) {
}
}
}

func getOne(c *qt.C, revision, filename string) *GitInfo {
c.Helper()
gr, err := Map(Options{Repository: repository, Revision: revision})
c.Assert(err, qt.IsNil)
gi, ok := gr.Files[filename]
c.Assert(ok, qt.IsTrue, qt.Commentf("File %s not found in GitMap", filename))
return gi
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/bep/gitmap

go 1.18

require (
github.com/frankban/quicktest v1.14.6 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
Loading