-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
119 lines (101 loc) · 2.8 KB
/
config.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
109
110
111
112
113
114
115
116
117
118
119
package gitx
import (
"github.com/gookit/gitw"
"github.com/gookit/goutil/maputil"
)
const (
HostGitHub = "github"
HostGitlab = "gitlab"
HostDefault = "git"
)
// ConfigProviderFn type
type ConfigProviderFn func() *Config
// Config for gitx
type Config struct {
// GitUrl ssh git host url
GitUrl string `json:"git_url"`
// HostUrl http host url. eg: https://gitlab.myself.com
HostUrl string `json:"host_url"`
// HostType eg: HostGitHub
HostType string `json:"host_type"`
// AutoSign auto add author sign on commit
AutoSign bool `json:"auto_sign"`
// ForkMode enable git fork mode for develop.
// If is False, use branch mode, will ignore SourceRemote setting.
ForkMode bool `json:"fork_mode"`
// SourceRemote the source remote name, it is center repo.
SourceRemote string `json:"source_remote"`
// DefaultRemote the default upstream remote name, use for develop. default: origin.
// It should be forked from SourceRemote.
DefaultRemote string `json:"default_remote"`
// DefaultBranch name, default is gitw.DefaultBranchName
DefaultBranch string `json:"default_branch"`
// BranchAliases branch aliases
BranchAliases maputil.Aliases `json:"branch_aliases"`
}
// NewConfig instance
func NewConfig() *Config {
return &Config{
HostType: HostDefault,
// remote
SourceRemote: DefaultSrcRemote,
DefaultRemote: gitw.DefaultRemoteName,
// branch
DefaultBranch: gitw.DefaultBranchName,
}
}
// LoadRepo by given git repo dir.
func (c *Config) LoadRepo(repoDir string) *GitLoc {
return NewGitLoc(repoDir, c)
}
// Init config.
func (c *Config) Init() *Config {
if c.DefaultBranch == "" {
c.DefaultBranch = gitw.DefaultBranchName
}
if c.DefaultRemote == "" {
c.DefaultRemote = gitw.DefaultRemoteName
}
return c
}
func (c *Config) IsDefaultRemote(remote string) bool {
return c.DefaultRemote == remote
}
func (c *Config) IsSourceRemote(remote string) bool {
return c.IsCenterRemote(remote)
}
func (c *Config) IsCenterRemote(remote string) bool {
return c.SourceRemote == remote
}
// IsForkMode check
func (c *Config) IsForkMode() bool {
return c.ForkMode && c.SourceRemote != ""
}
// OriginBranch name build
func (c *Config) OriginBranch(br string) string {
return c.DefaultRemote + "/" + br
}
// SourceBranch name build
func (c *Config) SourceBranch(br string) string {
return c.SourceRemote + "/" + br
}
// BuildRepoURL build
func (c *Config) BuildRepoURL(repoPath string, useSsh bool) string {
if useSsh {
return c.GitUrl + ":" + repoPath
}
return c.HostUrl + "/" + repoPath
}
// ResolveBranch branch name
func (c *Config) ResolveBranch(name string) string {
return c.BranchAliases.ResolveAlias(name)
}
// ResolveAlias branch name
func (c *Config) ResolveAlias(name string) string {
return c.ResolveBranch(name)
}
// Clone new config instance
func (c *Config) Clone() *Config {
c1 := *c
return &c1
}