Skip to content

Commit d5e9b08

Browse files
committed
libgit2: add remaining checkout strategy tests
This commit is a follow up on 4dc3185 and adds tests for the remaining checkout strategies, while consolidating some of the logic. Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent 4dc3185 commit d5e9b08

File tree

2 files changed

+286
-101
lines changed

2 files changed

+286
-101
lines changed

pkg/git/libgit2/checkout.go

+50-50
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,12 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.
9898
},
9999
})
100100
if err != nil {
101-
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
102-
}
103-
ref, err := repo.References.Dwim(c.tag)
104-
if err != nil {
105-
return nil, "", fmt.Errorf("unable to find tag '%s': %w", c.tag, err)
106-
}
107-
err = repo.SetHeadDetached(ref.Target())
108-
if err != nil {
109-
return nil, "", fmt.Errorf("git checkout error: %w", err)
110-
}
111-
head, err := repo.Head()
112-
if err != nil {
113-
return nil, "", fmt.Errorf("git resolve HEAD error: %w", err)
101+
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, gitutil.LibGit2Error(err))
114102
}
115-
commit, err := repo.LookupCommit(head.Target())
103+
commit, err := checkoutRef(repo, c.tag)
116104
if err != nil {
117-
return nil, "", fmt.Errorf("git commit '%s' not found: %w", head.Target(), err)
118-
}
119-
err = repo.CheckoutHead(&git2go.CheckoutOptions{
120-
Strategy: git2go.CheckoutForce,
121-
})
122-
if err != nil {
123-
return nil, "", fmt.Errorf("git checkout error: %w", err)
105+
return nil, "", err
124106
}
125-
126107
return &Commit{commit}, fmt.Sprintf("%s/%s", c.tag, commit.Id().String()), nil
127108
}
128109

@@ -140,30 +121,19 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *g
140121
CertificateCheckCallback: auth.CertCallback,
141122
},
142123
},
143-
CheckoutBranch: c.branch,
144124
})
145125
if err != nil {
146-
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
126+
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, gitutil.LibGit2Error(err))
147127
}
128+
148129
oid, err := git2go.NewOid(c.commit)
149130
if err != nil {
150-
return nil, "", fmt.Errorf("git commit '%s' could not be parsed", c.commit)
151-
}
152-
commit, err := repo.LookupCommit(oid)
153-
if err != nil {
154-
return nil, "", fmt.Errorf("git commit '%s' not found: %w", c.commit, err)
155-
}
156-
tree, err := repo.LookupTree(commit.TreeId())
157-
if err != nil {
158-
return nil, "", fmt.Errorf("git worktree error: %w", err)
131+
return nil, "", fmt.Errorf("could not create oid for '%s': %w", c.commit, err)
159132
}
160-
err = repo.CheckoutTree(tree, &git2go.CheckoutOptions{
161-
Strategy: git2go.CheckoutForce,
162-
})
133+
commit, err := checkoutCommit(repo, oid)
163134
if err != nil {
164135
return nil, "", fmt.Errorf("git checkout error: %w", err)
165136
}
166-
167137
return &Commit{commit}, fmt.Sprintf("%s/%s", c.branch, commit.Id().String()), nil
168138
}
169139

@@ -187,7 +157,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
187157
},
188158
})
189159
if err != nil {
190-
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
160+
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, gitutil.LibGit2Error(err))
191161
}
192162

193163
tags := make(map[string]string)
@@ -255,28 +225,58 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
255225
v := matchedVersions[len(matchedVersions)-1]
256226
t := v.Original()
257227

258-
ref, err := repo.References.Dwim(t)
228+
commit, err := checkoutRef(repo, t)
229+
return &Commit{commit}, fmt.Sprintf("%s/%s", t, commit.Id().String()), nil
230+
}
231+
232+
func checkoutRef(repo *git2go.Repository, name string) (*git2go.Commit, error) {
233+
ref, err := repo.References.Dwim(name)
259234
if err != nil {
260-
return nil, "", fmt.Errorf("unable to find tag '%s': %w", t, err)
235+
return nil, fmt.Errorf("unable to find '%s': %w", name, err)
261236
}
262-
err = repo.SetHeadDetached(ref.Target())
237+
defer ref.Free()
238+
c, err := ref.Peel(git2go.ObjectCommit)
263239
if err != nil {
264-
return nil, "", fmt.Errorf("git checkout error: %w", err)
240+
return nil, fmt.Errorf("could not get commit for ref '%s': %w", ref.Name(), err)
265241
}
266-
head, err := repo.Head()
242+
defer c.Free()
243+
commit, err := c.AsCommit()
267244
if err != nil {
268-
return nil, "", fmt.Errorf("git resolve HEAD error: %w", err)
245+
return nil, fmt.Errorf("could not get commit object for ref '%s': %w", ref.Name(), err)
269246
}
270-
commit, err := repo.LookupCommit(head.Target())
247+
defer commit.Free()
248+
return checkoutCommit(repo, commit.Id())
249+
}
250+
251+
func checkoutCommit(repo *git2go.Repository, oid *git2go.Oid) (*git2go.Commit, error) {
252+
commit, err := repo.LookupCommit(oid)
271253
if err != nil {
272-
return nil, "", fmt.Errorf("git commit '%s' not found: %w", head.Target().String(), err)
254+
return nil, fmt.Errorf("git commit '%s' not found: %w", oid.String(), err)
273255
}
274-
err = repo.CheckoutHead(&git2go.CheckoutOptions{
256+
if err = repo.SetHeadDetached(commit.Id()); err != nil {
257+
commit.Free()
258+
return nil, fmt.Errorf("could not detach HEAD at '%s': %w", oid.String(), err)
259+
}
260+
if err = repo.CheckoutHead(&git2go.CheckoutOptions{
275261
Strategy: git2go.CheckoutForce,
276-
})
262+
}); err != nil {
263+
commit.Free()
264+
return nil, fmt.Errorf("git checkout error: %w", err)
265+
}
266+
return commit, nil
267+
}
268+
269+
func headCommit(repo *git2go.Repository) (*git2go.Commit, error) {
270+
head, err := repo.Head()
277271
if err != nil {
278-
return nil, "", fmt.Errorf("git checkout error: %w", err)
272+
return nil, err
279273
}
274+
defer head.Free()
280275

281-
return &Commit{commit}, fmt.Sprintf("%s/%s", t, commit.Id().String()), nil
276+
commit, err := repo.LookupCommit(head.Target())
277+
if err != nil {
278+
return nil, err
279+
}
280+
281+
return commit, nil
282282
}

0 commit comments

Comments
 (0)