-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitx.go
108 lines (89 loc) · 2.18 KB
/
gitx.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package gitx
import (
"github.com/gookit/gitw"
"github.com/gookit/goutil"
"github.com/gookit/goutil/errorx"
)
// GitLoc repo struct
type GitLoc struct {
*Config
// local repo
*gitw.Repo
}
// NewGitLoc instance
func NewGitLoc(repoDir string, cfg *Config) *GitLoc {
if cfg == nil {
cfg = NewConfig()
} else {
cfg.Init()
}
return &GitLoc{
Repo: gitw.NewRepo(repoDir).PrintCmdOnExec(),
// config
Config: cfg,
}
}
// FetchOrigin fetch default remote
func (g *GitLoc) FetchOrigin() error {
return g.Cmd("fetch", g.DefaultRemote, "-np").Run()
}
// FetchSource fetch source remote
func (g *GitLoc) FetchSource() error {
return g.Cmd("fetch", g.SourceRemote, "-np").Run()
}
// HasDefaultBranch check
func (g *GitLoc) HasDefaultBranch(br string) bool {
return g.HasOriginBranch(br)
}
// HasOriginBranch check
func (g *GitLoc) HasOriginBranch(br string) bool {
return g.HasRemoteBranch(br, g.DefaultRemote)
}
// HasSourceBranch check
func (g *GitLoc) HasSourceBranch(br string) bool {
return g.HasRemoteBranch(br, g.SourceRemote)
}
func (g *GitLoc) HasDefaultRemote() bool {
return g.Repo.HasRemote(g.DefaultRemote)
}
func (g *GitLoc) HasSourceRemote() bool {
return g.Repo.HasRemote(g.SourceRemote)
}
// DefRemoteInfo data.
func (g *GitLoc) DefRemoteInfo() *gitw.RemoteInfo {
ri := g.Repo.RemoteInfo(g.DefaultRemote)
if ri != nil {
goutil.Panicf("gitx: default remote %q is not found", g.DefaultRemote)
}
return ri
}
// SrcRemoteInfo data.
func (g *GitLoc) SrcRemoteInfo() *gitw.RemoteInfo {
ri := g.Repo.RemoteInfo(g.SourceRemote)
if ri != nil {
goutil.Panicf("gitx: main repo remote %q is not found", g.SourceRemote)
}
return ri
}
// Check git config.
func (g *GitLoc) Check() error {
if err := g.CheckRemote(); err != nil {
return err
}
// TODO check others
return nil
}
// CheckRemote git config.
func (g *GitLoc) CheckRemote() error {
if !g.HasRemote(g.DefaultRemote) {
return errorx.Rawf("the default remote %q is not exists", g.DefaultRemote)
}
if g.ForkMode && !g.HasRemote(g.SourceRemote) {
return errorx.Rawf("the source remote %q is not exists", g.SourceRemote)
}
return nil
}
// RepoDir path.
func (g *GitLoc) RepoDir() string {
return g.Repo.Dir()
}