generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commits_on_branch_test.go
90 lines (64 loc) · 1.88 KB
/
commits_on_branch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package git
import (
"log"
"testing"
"time"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/stretchr/testify/assert"
)
func createBranch(repo *git.Repository) {
headRef, _ := repo.Head()
ref := plumbing.NewHashReference("refs/heads/my-branch", headRef.Hash())
storerErr := repo.Storer.SetReference(ref)
if storerErr != nil {
log.Println(storerErr)
}
worktree, _ := repo.Worktree()
checkoutErr := worktree.Checkout(&git.CheckoutOptions{
Branch: ref.Name(),
})
if checkoutErr != nil {
log.Println(checkoutErr)
}
}
func setupRepo() *git.Repository {
repo, _ := git.Init(memory.NewStorage(), memfs.New())
return repo
}
func createCommit(repo *git.Repository, message string) *object.Commit {
w, _ := repo.Worktree()
commit, _ := w.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
},
})
obj, _ := repo.CommitObject(commit)
return obj
}
func createTestHistory(repo *git.Repository) {
createCommit(repo, "test commit on master")
createBranch(repo)
createCommit(repo, "commit on new branch")
createCommit(repo, "second commit on new branch\n\n Long message")
createCommit(repo, "third commit on new branch")
}
func TestCommitsOnBranch(t *testing.T) {
repo := setupRepo()
createTestHistory(repo)
head, _ := repo.Head()
testGit := &Git{repo: repo}
commits, err := testGit.CommitsOnBranch(head.Hash())
assert.Equal(t, 4, len(commits))
commit, commitErr := repo.CommitObject(commits[0])
assert.NoError(t, commitErr)
assert.Equal(t, "third commit on new branch", commit.Message)
assert.Equal(t, err, nil)
lastCommit, _ := repo.CommitObject(commits[3])
assert.Equal(t, "test commit on master", lastCommit.Message)
}