/
branch.go
53 lines (41 loc) · 1003 Bytes
/
branch.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
package github
import (
"fmt"
"regexp"
"strings"
"github.com/hilalisadev/hub/git"
)
type Branch struct {
Repo *GitHubRepo
Name string
}
func (b *Branch) ShortName() string {
reg := regexp.MustCompile("^refs/(remotes/)?.+?/")
return reg.ReplaceAllString(b.Name, "")
}
func (b *Branch) LongName() string {
reg := regexp.MustCompile("^refs/(remotes/)?")
return reg.ReplaceAllString(b.Name, "")
}
func (b *Branch) RemoteName() string {
reg := regexp.MustCompile("^refs/remotes/([^/]+)")
if reg.MatchString(b.Name) {
return reg.FindStringSubmatch(b.Name)[1]
}
return ""
}
func (b *Branch) Upstream() (u *Branch, err error) {
name, err := git.SymbolicFullName(fmt.Sprintf("%s@{upstream}", b.ShortName()))
if err != nil {
return
}
u = &Branch{b.Repo, name}
return
}
func (b *Branch) IsMaster() bool {
masterName := b.Repo.MasterBranch().ShortName()
return b.ShortName() == masterName
}
func (b *Branch) IsRemote() bool {
return strings.HasPrefix(b.Name, "refs/remotes")
}