-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
51 lines (40 loc) · 892 Bytes
/
git.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
package main
import (
"log"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
func GetBranches() []string {
var branches []string
repo, err := git.PlainOpen(".")
if err != nil {
log.Fatal(err)
}
branchIter, err := repo.Branches()
if err != nil {
log.Print(err)
}
myFunc := func(ref *plumbing.Reference) error {
trim := strings.TrimPrefix(ref.Name().String(), "refs/heads/")
branches = append(branches, trim)
return nil
}
err = branchIter.ForEach(myFunc)
if err != nil {
log.Print(err)
}
return branches
}
// An alternative to fetching branches in the tests
func GetBranchesHelper(r *git.Repository, branchNames []string) []string {
var branches []string
for _, b := range branchNames {
b, err := r.Branch(b)
if err != nil {
log.Print("Error", err)
}
branches = append(branches, b.Name)
}
return branches
}