-
Notifications
You must be signed in to change notification settings - Fork 199
/
raw_git.go
189 lines (145 loc) · 4.33 KB
/
raw_git.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package config
import (
"regexp"
)
type rawGit struct {
rawGitExport `yaml:",inline"`
Url string `yaml:"url,omitempty"`
Branch string `yaml:"branch,omitempty"`
Tag string `yaml:"tag,omitempty"`
Commit string `yaml:"commit,omitempty"`
RawStageDependencies *rawStageDependencies `yaml:"stageDependencies,omitempty"`
rawStapelImage *rawStapelImage `yaml:"-"` // parent
UnsupportedAttributes map[string]interface{} `yaml:",inline"`
}
func (c *rawGit) configSection() interface{} {
return c
}
func (c *rawGit) doc() *doc {
return c.rawStapelImage.doc
}
func (c *rawGit) gitType() string {
if c.Url != "" {
return "remote"
}
return "local"
}
func (c *rawGit) UnmarshalYAML(unmarshal func(interface{}) error) error {
c.rawGitExport = newRawGitExport()
if parent, ok := parentStack.Peek().(*rawStapelImage); ok {
c.rawStapelImage = parent
}
parentStack.Push(c)
type plain rawGit
err := unmarshal((*plain)(c))
parentStack.Pop()
if err != nil {
return err
}
c.rawGitExport.inlinedIntoRaw(c)
if err := checkOverflow(c.UnsupportedAttributes, c, c.rawStapelImage.doc); err != nil {
return err
}
return nil
}
func (c *rawGit) toGitLocalDirective() (gitLocal *GitLocal, err error) {
gitLocal = &GitLocal{}
if gitLocalExport, err := c.toGitLocalExportDirective(); err != nil {
return nil, err
} else {
gitLocal.GitLocalExport = gitLocalExport
}
gitLocal.raw = c
if err := c.validateGitLocalDirective(gitLocal); err != nil {
return nil, err
}
return gitLocal, nil
}
func (c *rawGit) validateGitLocalDirective(gitLocal *GitLocal) (err error) {
if c.Branch != "" || c.Commit != "" || c.Tag != "" {
return newDetailedConfigError("specify `branch: BRANCH`, `tag: TAG` and `commit: COMMIT` only for remote git!", nil, c.rawStapelImage.doc)
}
if err := gitLocal.validate(); err != nil {
return err
}
return nil
}
func (c *rawGit) toGitLocalExportDirective() (gitLocalExport *GitLocalExport, err error) {
gitLocalExport = &GitLocalExport{}
gitLocalExport.GitExportBase = &GitExportBase{}
if gitExport, err := c.rawGitExport.toDirective(); err != nil {
return nil, err
} else {
gitLocalExport.GitExportBase.GitExport = gitExport
}
if c.RawStageDependencies != nil {
if stageDependencies, err := c.RawStageDependencies.toDirective(); err != nil {
return nil, err
} else {
gitLocalExport.StageDependencies = stageDependencies
}
}
gitLocalExport.raw = c
if err := c.validateGitLocalExportDirective(gitLocalExport); err != nil {
return nil, err
}
return gitLocalExport, nil
}
func (c *rawGit) validateGitLocalExportDirective(gitLocalExport *GitLocalExport) (err error) {
if err := gitLocalExport.validate(); err != nil {
return err
}
return nil
}
func (c *rawGit) toGitRemoteDirective() (gitRemote *GitRemote, err error) {
gitRemote = &GitRemote{}
if gitRemoteExport, err := c.toGitRemoteExportDirective(); err != nil {
return nil, err
} else {
gitRemote.GitRemoteExport = gitRemoteExport
}
gitRemote.Url = c.Url
gitRemote.Name = getRepositoryID(c.Url)
gitRemote.raw = c
if err := c.validateGitRemoteDirective(gitRemote); err != nil {
return nil, err
}
return gitRemote, nil
}
func (c *rawGit) validateGitRemoteDirective(gitRemote *GitRemote) (err error) {
if err := gitRemote.validate(); err != nil {
return err
}
return nil
}
func (c *rawGit) toGitRemoteExportDirective() (gitRemoteExport *GitRemoteExport, err error) {
gitRemoteExport = &GitRemoteExport{}
if gitLocalExport, err := c.toGitLocalExportDirective(); err != nil {
return nil, err
} else {
gitRemoteExport.GitLocalExport = gitLocalExport
}
gitRemoteExport.Branch = c.Branch
gitRemoteExport.Tag = c.Tag
gitRemoteExport.Commit = c.Commit
gitRemoteExport.raw = c
if err := c.validateGitRemoteExportDirective(gitRemoteExport); err != nil {
return nil, err
}
return gitRemoteExport, nil
}
func (c *rawGit) validateGitRemoteExportDirective(gitRemoteExport *GitRemoteExport) (err error) {
if err := gitRemoteExport.validate(); err != nil {
return err
}
return nil
}
func getRepositoryID(repository string) string {
r := regexp.MustCompile(`(?U).*?[:/](.*/[^/ ]+)(\.git)?/?$`)
match := r.FindStringSubmatch(repository)
if len(match) == 3 {
return match[1]
} else {
return repository
}
}