@@ -53,12 +53,34 @@ func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, opt git.CheckoutOpti
53
53
}
54
54
}
55
55
56
+ // clone is a wrapper around git2go.Clone that respects the provided context.
57
+ func clone (ctx context.Context , path , url string , auth * git.Auth , opts * git2go.CloneOptions ) (* git2go.Repository , error ) {
58
+ var repo * git2go.Repository
59
+ errCh := make (chan error , 1 )
60
+ go func () {
61
+ var err error
62
+ repo , err = git2go .Clone (url , path , opts )
63
+ errCh <- err
64
+ }()
65
+
66
+ select {
67
+ case err := <- errCh :
68
+ if err != nil {
69
+ return nil , fmt .Errorf ("unable to clone '%s': %w" , url , gitutil .LibGit2Error (err ))
70
+ }
71
+ case <- ctx .Done ():
72
+ return nil , fmt .Errorf ("clone context cancelled: %w" , ctx .Err ())
73
+ }
74
+
75
+ return repo , nil
76
+ }
77
+
56
78
type CheckoutBranch struct {
57
79
branch string
58
80
}
59
81
60
82
func (c * CheckoutBranch ) Checkout (ctx context.Context , path , url string , auth * git.Auth ) (git.Commit , string , error ) {
61
- repo , err := git2go . Clone ( url , path , & git2go.CloneOptions {
83
+ cloneOpts := & git2go.CloneOptions {
62
84
FetchOptions : & git2go.FetchOptions {
63
85
DownloadTags : git2go .DownloadTagsNone ,
64
86
RemoteCallbacks : git2go.RemoteCallbacks {
@@ -67,9 +89,10 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *g
67
89
},
68
90
},
69
91
CheckoutBranch : c .branch ,
70
- })
92
+ }
93
+ repo , err := clone (ctx , path , url , auth , cloneOpts )
71
94
if err != nil {
72
- return nil , "" , fmt . Errorf ( "unable to clone '%s', error: %w" , url , gitutil . LibGit2Error ( err ))
95
+ return nil , "" , err
73
96
}
74
97
head , err := repo .Head ()
75
98
if err != nil {
@@ -87,17 +110,18 @@ type CheckoutTag struct {
87
110
}
88
111
89
112
func (c * CheckoutTag ) Checkout (ctx context.Context , path , url string , auth * git.Auth ) (git.Commit , string , error ) {
90
- repo , err := git2go . Clone ( url , path , & git2go.CloneOptions {
113
+ cloneOpts := & git2go.CloneOptions {
91
114
FetchOptions : & git2go.FetchOptions {
92
115
DownloadTags : git2go .DownloadTagsAll ,
93
116
RemoteCallbacks : git2go.RemoteCallbacks {
94
117
CredentialsCallback : auth .CredCallback ,
95
118
CertificateCheckCallback : auth .CertCallback ,
96
119
},
97
120
},
98
- })
121
+ }
122
+ repo , err := clone (ctx , path , url , auth , cloneOpts )
99
123
if err != nil {
100
- return nil , "" , fmt . Errorf ( "unable to clone '%s', error: %w" , url , err )
124
+ return nil , "" , err
101
125
}
102
126
ref , err := repo .References .Dwim (c .tag )
103
127
if err != nil {
@@ -131,7 +155,7 @@ type CheckoutCommit struct {
131
155
}
132
156
133
157
func (c * CheckoutCommit ) Checkout (ctx context.Context , path , url string , auth * git.Auth ) (git.Commit , string , error ) {
134
- repo , err := git2go . Clone ( url , path , & git2go.CloneOptions {
158
+ cloneOpts := & git2go.CloneOptions {
135
159
FetchOptions : & git2go.FetchOptions {
136
160
DownloadTags : git2go .DownloadTagsNone ,
137
161
RemoteCallbacks : git2go.RemoteCallbacks {
@@ -140,9 +164,10 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *g
140
164
},
141
165
},
142
166
CheckoutBranch : c .branch ,
143
- })
167
+ }
168
+ repo , err := clone (ctx , path , url , auth , cloneOpts )
144
169
if err != nil {
145
- return nil , "" , fmt . Errorf ( "unable to clone '%s', error: %w" , url , err )
170
+ return nil , "" , err
146
171
}
147
172
oid , err := git2go .NewOid (c .commit )
148
173
if err != nil {
@@ -176,17 +201,18 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
176
201
return nil , "" , fmt .Errorf ("semver parse range error: %w" , err )
177
202
}
178
203
179
- repo , err := git2go . Clone ( url , path , & git2go.CloneOptions {
204
+ cloneOpts := & git2go.CloneOptions {
180
205
FetchOptions : & git2go.FetchOptions {
181
206
DownloadTags : git2go .DownloadTagsAll ,
182
207
RemoteCallbacks : git2go.RemoteCallbacks {
183
208
CredentialsCallback : auth .CredCallback ,
184
209
CertificateCheckCallback : auth .CertCallback ,
185
210
},
186
211
},
187
- })
212
+ }
213
+ repo , err := clone (ctx , path , url , auth , cloneOpts )
188
214
if err != nil {
189
- return nil , "" , fmt . Errorf ( "unable to clone '%s', error: %w" , url , err )
215
+ return nil , "" , err
190
216
}
191
217
192
218
tags := make (map [string ]string )
0 commit comments