-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
remote_branch_name.go
60 lines (49 loc) · 1.37 KB
/
remote_branch_name.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
package gitdomain
import (
"fmt"
"strings"
)
// RemoteBranchName is the name of a remote branch, e.g. "origin/foo".
type RemoteBranchName string
func EmptyRemoteBranchName() RemoteBranchName {
return ""
}
func NewRemoteBranchName(id string) RemoteBranchName {
if !isValidRemoteBranchName(id) {
panic(fmt.Sprintf("%q is not a valid remote branch name", id))
}
return RemoteBranchName(id)
}
func isValidRemoteBranchName(value string) bool {
if len(value) < 3 {
return false
}
if !strings.Contains(value, "/") {
return false
}
return true
}
// BranchName widens the type of this RemoteBranchName to a more generic BranchName.
func (self RemoteBranchName) BranchName() BranchName {
return BranchName(string(self))
}
func (self RemoteBranchName) IsEmpty() bool {
return self == ""
}
// LocalBranchName provides the name of the local branch that this remote branch tracks.
func (self RemoteBranchName) LocalBranchName() LocalBranchName {
_, localBranch := self.Parts()
return localBranch
}
func (self RemoteBranchName) Parts() (Remote, LocalBranchName) {
parts := strings.SplitN(string(self), "/", 2)
return NewRemote(parts[0]), NewLocalBranchName(parts[1])
}
func (self RemoteBranchName) Remote() Remote {
remote, _ := self.Parts()
return remote
}
// Implementation of the fmt.Stringer interface.
func (self RemoteBranchName) String() string {
return string(self)
}