mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-08 10:47:02 +00:00
chore: cleanup unused git functionality
This commit is contained in:
parent
5e52f82a28
commit
9f7c59ce08
2 changed files with 0 additions and 165 deletions
85
git.go
85
git.go
|
|
@ -2,12 +2,8 @@ package rp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
|
|
@ -50,84 +46,3 @@ func CloneRepo(ctx context.Context, cloneURL, branch string, auth transport.Auth
|
||||||
|
|
||||||
return repo, nil
|
return repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReleasableCommits(repo *git.Repository) ([]Commit, *Tag, error) {
|
|
||||||
|
|
||||||
ref, err := repo.Head()
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
iter, err := repo.Log(&git.LogOptions{From: ref.Hash()})
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tags, err := buildTagRefMap(repo)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
commits := make([]Commit, 0)
|
|
||||||
var tag *Tag
|
|
||||||
for {
|
|
||||||
commit, err := iter.Next()
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, io.EOF) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if tagRef, exists := tags[commit.Hash]; exists {
|
|
||||||
// We found the nearest tag, return results
|
|
||||||
tagName, _ := strings.CutPrefix(tagRef.Name().String(), "refs/tags/")
|
|
||||||
|
|
||||||
tag = &Tag{
|
|
||||||
Hash: tagRef.Hash().String(),
|
|
||||||
Name: tagName,
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
commits = append(commits, Commit{
|
|
||||||
Hash: commit.Hash.String(),
|
|
||||||
Message: commit.Message,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// We discover the commits from HEAD, but want to process them in "chronological" order
|
|
||||||
slices.Reverse(commits)
|
|
||||||
|
|
||||||
return commits, tag, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// From go-git PR
|
|
||||||
func buildTagRefMap(r *git.Repository) (map[plumbing.Hash]*plumbing.Reference, error) {
|
|
||||||
iter, err := r.Tags()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tags := map[plumbing.Hash]*plumbing.Reference{}
|
|
||||||
for {
|
|
||||||
t, err := iter.Next()
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, io.EOF) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
to, err := r.TagObject(t.Hash())
|
|
||||||
if errors.Is(err, plumbing.ErrObjectNotFound) {
|
|
||||||
// t is a lightweight tag
|
|
||||||
tags[t.Hash()] = t
|
|
||||||
} else if err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
// t is an annotated tag
|
|
||||||
tags[to.Target] = t
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tags, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
80
git_test.go
80
git_test.go
|
|
@ -1,81 +1 @@
|
||||||
package rp
|
package rp
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
|
||||||
"github.com/apricote/releaser-pleaser/internal/testutils"
|
|
||||||
)
|
|
||||||
|
|
||||||
var InitCommit = Commit{
|
|
||||||
Hash: "fca23c2d67580780d5bbb4f73987624b81926aad",
|
|
||||||
Message: "chore: init",
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReleasableCommits(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
repoFn testutils.Repo
|
|
||||||
wantCommits []Commit
|
|
||||||
wantTag *Tag
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Empty Repo",
|
|
||||||
repoFn: testutils.WithTestRepo(),
|
|
||||||
wantCommits: []Commit{
|
|
||||||
InitCommit,
|
|
||||||
},
|
|
||||||
wantTag: nil,
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Single Commit",
|
|
||||||
repoFn: testutils.WithTestRepo(
|
|
||||||
testutils.WithCommit("feat: foobar"),
|
|
||||||
),
|
|
||||||
wantCommits: []Commit{
|
|
||||||
InitCommit,
|
|
||||||
{
|
|
||||||
Hash: "ff0815947e8211485d4f97ff8cf5deb49866e228",
|
|
||||||
Message: "feat: foobar",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
wantTag: nil,
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Single Commit After Tag",
|
|
||||||
repoFn: testutils.WithTestRepo(
|
|
||||||
testutils.WithCommit("feat: foobar", testutils.WithTag("v1.0.0")),
|
|
||||||
testutils.WithCommit("feat: baz"),
|
|
||||||
),
|
|
||||||
wantCommits: []Commit{
|
|
||||||
{
|
|
||||||
Hash: "ccdc724ef1755095d5a58c2421eec75d4010b3b7",
|
|
||||||
Message: "feat: baz",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
wantTag: &Tag{
|
|
||||||
Hash: "ff0815947e8211485d4f97ff8cf5deb49866e228",
|
|
||||||
Name: "v1.0.0",
|
|
||||||
},
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
repo := tt.repoFn(t)
|
|
||||||
|
|
||||||
commits, tag, err := ReleasableCommits(repo)
|
|
||||||
if tt.wantErr {
|
|
||||||
assert.Error(t, err)
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.wantCommits, commits)
|
|
||||||
assert.Equal(t, tt.wantTag, tag)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue