@@ -25,57 +25,55 @@ import (
25
25
"time"
26
26
27
27
"github.com/Masterminds/semver/v3"
28
- "github.com/fluxcd/pkg/gitutil"
29
- "github.com/fluxcd/pkg/version"
30
28
extgogit "github.com/go-git/go-git/v5"
31
29
"github.com/go-git/go-git/v5/plumbing"
32
30
"github.com/go-git/go-git/v5/plumbing/object"
33
31
34
- sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
32
+ "github.com/fluxcd/pkg/gitutil"
33
+ "github.com/fluxcd/pkg/version"
34
+
35
35
"github.com/fluxcd/source-controller/pkg/git"
36
36
)
37
37
38
- func CheckoutStrategyForRef (ref * sourcev1.GitRepositoryRef , opt git.CheckoutOptions ) git.CheckoutStrategy {
38
+ // CheckoutStrategyForOptions returns the git.CheckoutStrategy for the given
39
+ // git.CheckoutOptions.
40
+ func CheckoutStrategyForOptions (_ context.Context , opts git.CheckoutOptions ) git.CheckoutStrategy {
39
41
switch {
40
- case ref == nil :
41
- return & CheckoutBranch {branch : git .DefaultBranch }
42
- case ref .SemVer != "" :
43
- return & CheckoutSemVer {semVer : ref .SemVer , recurseSubmodules : opt .RecurseSubmodules }
44
- case ref .Tag != "" :
45
- return & CheckoutTag {tag : ref .Tag , recurseSubmodules : opt .RecurseSubmodules }
46
- case ref .Commit != "" :
47
- strategy := & CheckoutCommit {branch : ref .Branch , commit : ref .Commit , recurseSubmodules : opt .RecurseSubmodules }
48
- if strategy .branch == "" {
49
- strategy .branch = git .DefaultBranch
50
- }
51
- return strategy
52
- case ref .Branch != "" :
53
- return & CheckoutBranch {branch : ref .Branch , recurseSubmodules : opt .RecurseSubmodules }
42
+ case opts .Commit != "" :
43
+ return & CheckoutCommit {Branch : opts .Branch , Commit : opts .Commit , RecurseSubmodules : opts .RecurseSubmodules }
44
+ case opts .SemVer != "" :
45
+ return & CheckoutSemVer {SemVer : opts .SemVer , RecurseSubmodules : opts .RecurseSubmodules }
46
+ case opts .Tag != "" :
47
+ return & CheckoutTag {Tag : opts .Tag , RecurseSubmodules : opts .RecurseSubmodules }
54
48
default :
55
- return & CheckoutBranch {branch : git .DefaultBranch }
49
+ branch := opts .Branch
50
+ if branch == "" {
51
+ branch = git .DefaultBranch
52
+ }
53
+ return & CheckoutBranch {Branch : branch }
56
54
}
57
55
}
58
56
59
57
type CheckoutBranch struct {
60
- branch string
61
- recurseSubmodules bool
58
+ Branch string
59
+ RecurseSubmodules bool
62
60
}
63
61
64
62
func (c * CheckoutBranch ) Checkout (ctx context.Context , path , url string , opts * git.AuthOptions ) (* git.Commit , error ) {
65
63
authMethod , err := transportAuth (opts )
66
64
if err != nil {
67
65
return nil , fmt .Errorf ("failed to construct auth method with options: %w" , err )
68
66
}
69
- ref := plumbing .NewBranchReferenceName (c .branch )
67
+ ref := plumbing .NewBranchReferenceName (c .Branch )
70
68
repo , err := extgogit .PlainCloneContext (ctx , path , false , & extgogit.CloneOptions {
71
69
URL : url ,
72
70
Auth : authMethod ,
73
71
RemoteName : git .DefaultOrigin ,
74
- ReferenceName : plumbing .NewBranchReferenceName (c .branch ),
72
+ ReferenceName : plumbing .NewBranchReferenceName (c .Branch ),
75
73
SingleBranch : true ,
76
74
NoCheckout : false ,
77
75
Depth : 1 ,
78
- RecurseSubmodules : recurseSubmodules (c .recurseSubmodules ),
76
+ RecurseSubmodules : recurseSubmodules (c .RecurseSubmodules ),
79
77
Progress : nil ,
80
78
Tags : extgogit .NoTags ,
81
79
CABundle : caBundle (opts ),
@@ -85,7 +83,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
85
83
}
86
84
head , err := repo .Head ()
87
85
if err != nil {
88
- return nil , fmt .Errorf ("failed to resolve HEAD of branch '%s': %w" , c .branch , err )
86
+ return nil , fmt .Errorf ("failed to resolve HEAD of branch '%s': %w" , c .Branch , err )
89
87
}
90
88
cc , err := repo .CommitObject (head .Hash ())
91
89
if err != nil {
@@ -95,25 +93,25 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
95
93
}
96
94
97
95
type CheckoutTag struct {
98
- tag string
99
- recurseSubmodules bool
96
+ Tag string
97
+ RecurseSubmodules bool
100
98
}
101
99
102
100
func (c * CheckoutTag ) Checkout (ctx context.Context , path , url string , opts * git.AuthOptions ) (* git.Commit , error ) {
103
101
authMethod , err := transportAuth (opts )
104
102
if err != nil {
105
103
return nil , fmt .Errorf ("failed to construct auth method with options: %w" , err )
106
104
}
107
- ref := plumbing .NewTagReferenceName (c .tag )
105
+ ref := plumbing .NewTagReferenceName (c .Tag )
108
106
repo , err := extgogit .PlainCloneContext (ctx , path , false , & extgogit.CloneOptions {
109
107
URL : url ,
110
108
Auth : authMethod ,
111
109
RemoteName : git .DefaultOrigin ,
112
- ReferenceName : plumbing .NewTagReferenceName (c .tag ),
110
+ ReferenceName : plumbing .NewTagReferenceName (c .Tag ),
113
111
SingleBranch : true ,
114
112
NoCheckout : false ,
115
113
Depth : 1 ,
116
- RecurseSubmodules : recurseSubmodules (c .recurseSubmodules ),
114
+ RecurseSubmodules : recurseSubmodules (c .RecurseSubmodules ),
117
115
Progress : nil ,
118
116
Tags : extgogit .NoTags ,
119
117
CABundle : caBundle (opts ),
@@ -123,7 +121,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git.
123
121
}
124
122
head , err := repo .Head ()
125
123
if err != nil {
126
- return nil , fmt .Errorf ("failed to resolve HEAD of tag '%s': %w" , c .tag , err )
124
+ return nil , fmt .Errorf ("failed to resolve HEAD of tag '%s': %w" , c .Tag , err )
127
125
}
128
126
cc , err := repo .CommitObject (head .Hash ())
129
127
if err != nil {
@@ -133,59 +131,60 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git.
133
131
}
134
132
135
133
type CheckoutCommit struct {
136
- branch string
137
- commit string
138
- recurseSubmodules bool
134
+ Branch string
135
+ Commit string
136
+ RecurseSubmodules bool
139
137
}
140
138
141
139
func (c * CheckoutCommit ) Checkout (ctx context.Context , path , url string , opts * git.AuthOptions ) (* git.Commit , error ) {
142
140
authMethod , err := transportAuth (opts )
143
141
if err != nil {
144
142
return nil , fmt .Errorf ("failed to construct auth method with options: %w" , err )
145
143
}
146
- ref := plumbing .NewBranchReferenceName (c .branch )
147
- repo , err := extgogit .PlainCloneContext (ctx , path , false , & extgogit.CloneOptions {
144
+ cloneOpts := & extgogit.CloneOptions {
148
145
URL : url ,
149
146
Auth : authMethod ,
150
147
RemoteName : git .DefaultOrigin ,
151
- ReferenceName : ref ,
152
- SingleBranch : true ,
153
- NoCheckout : false ,
154
- RecurseSubmodules : recurseSubmodules (c .recurseSubmodules ),
148
+ SingleBranch : false ,
149
+ NoCheckout : true ,
150
+ RecurseSubmodules : recurseSubmodules (c .RecurseSubmodules ),
155
151
Progress : nil ,
156
152
Tags : extgogit .NoTags ,
157
153
CABundle : caBundle (opts ),
158
- })
154
+ }
155
+ if c .Branch != "" {
156
+ cloneOpts .SingleBranch = true
157
+ cloneOpts .ReferenceName = plumbing .NewBranchReferenceName (c .Branch )
158
+ }
159
+ repo , err := extgogit .PlainCloneContext (ctx , path , false , cloneOpts )
159
160
if err != nil {
160
161
return nil , fmt .Errorf ("unable to clone '%s', error: %w" , url , gitutil .GoGitError (err ))
161
162
}
162
163
w , err := repo .Worktree ()
163
164
if err != nil {
164
165
return nil , fmt .Errorf ("failed to open Git worktree: %w" , err )
165
166
}
166
- f , _ := repo .Head ()
167
- f .String ()
168
- cc , err := repo .CommitObject (plumbing .NewHash (c .commit ))
167
+ cc , err := repo .CommitObject (plumbing .NewHash (c .Commit ))
169
168
if err != nil {
170
- return nil , fmt .Errorf ("failed to resolve commit object for '%s': %w" , c .commit , err )
169
+ return nil , fmt .Errorf ("failed to resolve commit object for '%s': %w" , c .Commit , err )
171
170
}
172
171
err = w .Checkout (& extgogit.CheckoutOptions {
173
172
Hash : cc .Hash ,
174
173
Force : true ,
175
174
})
176
175
if err != nil {
177
- return nil , fmt .Errorf ("failed to checkout commit '%s': %w" , c .commit , err )
176
+ return nil , fmt .Errorf ("failed to checkout commit '%s': %w" , c .Commit , err )
178
177
}
179
- return commitWithRef (cc , ref )
178
+ return commitWithRef (cc , cloneOpts . ReferenceName )
180
179
}
181
180
182
181
type CheckoutSemVer struct {
183
- semVer string
184
- recurseSubmodules bool
182
+ SemVer string
183
+ RecurseSubmodules bool
185
184
}
186
185
187
186
func (c * CheckoutSemVer ) Checkout (ctx context.Context , path , url string , opts * git.AuthOptions ) (* git.Commit , error ) {
188
- verConstraint , err := semver .NewConstraint (c .semVer )
187
+ verConstraint , err := semver .NewConstraint (c .SemVer )
189
188
if err != nil {
190
189
return nil , fmt .Errorf ("semver parse error: %w" , err )
191
190
}
@@ -201,7 +200,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g
201
200
RemoteName : git .DefaultOrigin ,
202
201
NoCheckout : false ,
203
202
Depth : 1 ,
204
- RecurseSubmodules : recurseSubmodules (c .recurseSubmodules ),
203
+ RecurseSubmodules : recurseSubmodules (c .RecurseSubmodules ),
205
204
Progress : nil ,
206
205
Tags : extgogit .AllTags ,
207
206
CABundle : caBundle (opts ),
@@ -247,7 +246,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g
247
246
matchedVersions = append (matchedVersions , v )
248
247
}
249
248
if len (matchedVersions ) == 0 {
250
- return nil , fmt .Errorf ("no match found for semver: %s" , c .semVer )
249
+ return nil , fmt .Errorf ("no match found for semver: %s" , c .SemVer )
251
250
}
252
251
253
252
// Sort versions
0 commit comments