github.com/jingweno/gh@v2.1.1-0.20221007190738-04a7985fa9a1+incompatible/github/branch.go (about) 1 package github 2 3 import ( 4 "fmt" 5 "github.com/jingweno/gh/git" 6 "regexp" 7 "strings" 8 ) 9 10 type Branch struct { 11 Name string 12 } 13 14 func (b *Branch) ShortName() string { 15 reg := regexp.MustCompile("^refs/(remotes/)?.+?/") 16 return reg.ReplaceAllString(b.Name, "") 17 } 18 19 func (b *Branch) LongName() string { 20 reg := regexp.MustCompile("^refs/(remotes/)?") 21 return reg.ReplaceAllString(b.Name, "") 22 } 23 24 func (b *Branch) RemoteName() string { 25 reg := regexp.MustCompile("^refs/remotes/([^/]+)") 26 if reg.MatchString(b.Name) { 27 return reg.FindStringSubmatch(b.Name)[1] 28 } 29 30 return "" 31 } 32 33 func (b *Branch) Upstream() (u *Branch, err error) { 34 name, err := git.SymbolicFullName(fmt.Sprintf("%s@{upstream}", b.ShortName())) 35 if err != nil { 36 return 37 } 38 39 u = &Branch{name} 40 41 return 42 } 43 44 func (b *Branch) IsRemote() bool { 45 return strings.HasPrefix(b.Name, "refs/remotes") 46 }