Skip to content

Commit 6826881

Browse files
committed
factor out unmanaged checkout into its own functions
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 5dfced7 commit 6826881

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

pkg/git/libgit2/checkout.go

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -184,33 +184,37 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
184184

185185
return buildCommit(cc, "refs/heads/"+c.Branch), nil
186186
} else {
187-
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
188-
FetchOptions: git2go.FetchOptions{
189-
DownloadTags: git2go.DownloadTagsNone,
190-
RemoteCallbacks: RemoteCallbacks(ctx, opts),
191-
ProxyOptions: git2go.ProxyOptions{Type: git2go.ProxyTypeAuto},
192-
},
193-
CheckoutOptions: git2go.CheckoutOptions{
194-
Strategy: git2go.CheckoutForce,
195-
},
196-
CheckoutBranch: c.Branch,
197-
})
198-
if err != nil {
199-
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
200-
}
201-
defer repo.Free()
202-
head, err := repo.Head()
203-
if err != nil {
204-
return nil, fmt.Errorf("git resolve HEAD error: %w", err)
205-
}
206-
defer head.Free()
207-
cc, err := repo.LookupCommit(head.Target())
208-
if err != nil {
209-
return nil, fmt.Errorf("failed to lookup HEAD commit '%s' for branch '%s': %w", head.Target(), c.Branch, err)
210-
}
211-
defer cc.Free()
212-
return buildCommit(cc, "refs/heads/"+c.Branch), nil
187+
return c.checkoutUnmanaged(ctx, path, url, opts)
188+
}
189+
}
190+
191+
func (c *CheckoutBranch) checkoutUnmanaged(ctx context.Context, path, url string, opts *git.AuthOptions) (_ *git.Commit, err error) {
192+
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
193+
FetchOptions: git2go.FetchOptions{
194+
DownloadTags: git2go.DownloadTagsNone,
195+
RemoteCallbacks: RemoteCallbacks(ctx, opts),
196+
ProxyOptions: git2go.ProxyOptions{Type: git2go.ProxyTypeAuto},
197+
},
198+
CheckoutOptions: git2go.CheckoutOptions{
199+
Strategy: git2go.CheckoutForce,
200+
},
201+
CheckoutBranch: c.Branch,
202+
})
203+
if err != nil {
204+
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
205+
}
206+
defer repo.Free()
207+
head, err := repo.Head()
208+
if err != nil {
209+
return nil, fmt.Errorf("git resolve HEAD error: %w", err)
210+
}
211+
defer head.Free()
212+
cc, err := repo.LookupCommit(head.Target())
213+
if err != nil {
214+
return nil, fmt.Errorf("failed to lookup HEAD commit '%s' for branch '%s': %w", head.Target(), c.Branch, err)
213215
}
216+
defer cc.Free()
217+
return buildCommit(cc, "refs/heads/"+c.Branch), nil
214218
}
215219

216220
type CheckoutTag struct {
@@ -305,26 +309,30 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git.
305309
defer cc.Free()
306310
return buildCommit(cc, "refs/tags/"+c.Tag), nil
307311
} else {
308-
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
309-
FetchOptions: git2go.FetchOptions{
310-
DownloadTags: git2go.DownloadTagsAll,
311-
RemoteCallbacks: RemoteCallbacks(ctx, opts),
312-
ProxyOptions: git2go.ProxyOptions{Type: git2go.ProxyTypeAuto},
313-
},
314-
})
315-
if err != nil {
316-
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
317-
}
318-
defer repo.Free()
319-
cc, err := checkoutDetachedDwim(repo, c.Tag)
320-
if err != nil {
321-
return nil, err
322-
}
323-
defer cc.Free()
324-
return buildCommit(cc, "refs/tags/"+c.Tag), nil
312+
return c.checkoutUnmanaged(ctx, path, url, opts)
325313
}
326314
}
327315

316+
func (c *CheckoutTag) checkoutUnmanaged(ctx context.Context, path, url string, opts *git.AuthOptions) (_ *git.Commit, err error) {
317+
repo, err := git2go.Clone(url, path, &git2go.CloneOptions{
318+
FetchOptions: git2go.FetchOptions{
319+
DownloadTags: git2go.DownloadTagsAll,
320+
RemoteCallbacks: RemoteCallbacks(ctx, opts),
321+
ProxyOptions: git2go.ProxyOptions{Type: git2go.ProxyTypeAuto},
322+
},
323+
})
324+
if err != nil {
325+
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
326+
}
327+
defer repo.Free()
328+
cc, err := checkoutDetachedDwim(repo, c.Tag)
329+
if err != nil {
330+
return nil, err
331+
}
332+
defer cc.Free()
333+
return buildCommit(cc, "refs/tags/"+c.Tag), nil
334+
}
335+
328336
type CheckoutCommit struct {
329337
Commit string
330338
}

pkg/git/libgit2/checkout_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
. "github.com/onsi/gomega"
3131
)
3232

33-
func TestCheckoutBranch_Checkout(t *testing.T) {
33+
func TestCheckoutBranch_checkoutUnmanaged(t *testing.T) {
3434
repo, err := initBareRepo(t)
3535
if err != nil {
3636
t.Fatal(err)
@@ -126,7 +126,7 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
126126
}
127127
}
128128

129-
func TestCheckoutTag_Checkout(t *testing.T) {
129+
func TestCheckoutTag_checkoutUnmanaged(t *testing.T) {
130130
type testTag struct {
131131
name string
132132
annotated bool

0 commit comments

Comments
 (0)