Skip to content

Commit 107ca31

Browse files
committed
libgit2: fix checkout logic for CheckoutBranch
Use the target commit, to checkout tree and set the head to the desired branch instead of doing a hard reset to the target commit. Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent fe31ff9 commit 107ca31

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

pkg/git/libgit2/checkout.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,50 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
161161
}
162162
defer upstreamCommit.Free()
163163

164-
// Once the index has been updated with Fetch, and we know the tip commit,
165-
// a hard reset can be used to align the local worktree with the remote branch's.
166-
err = repo.ResetToCommit(upstreamCommit, git2go.ResetHard, &git2go.CheckoutOptions{
164+
// We try to lookup the branch (and create it if it doesn't exist), so that we can
165+
// switch the repo to the specified branch. This is done so that users of this api
166+
// can expect the repo to be at the desired branch, when cloned.
167+
localBranch, err := repo.LookupBranch(c.Branch, git2go.BranchLocal)
168+
if git2go.IsErrorCode(err, git2go.ErrorCodeNotFound) {
169+
localBranch, err = repo.CreateBranch(c.Branch, upstreamCommit, false)
170+
if err != nil {
171+
return nil, fmt.Errorf("unable to create local branch '%s': %w", c.Branch, err)
172+
}
173+
} else if err != nil {
174+
return nil, fmt.Errorf("unable to lookup branch '%s': %w", c.Branch, err)
175+
}
176+
defer localBranch.Free()
177+
178+
tree, err := repo.LookupTree(upstreamCommit.TreeId())
179+
if err != nil {
180+
return nil, fmt.Errorf("unable to lookup tree for branch '%s': %w", c.Branch, err)
181+
}
182+
defer tree.Free()
183+
184+
err = repo.CheckoutTree(tree, &git2go.CheckoutOpts{
185+
// the remote branch should take precedence if it exists at this point in time.
167186
Strategy: git2go.CheckoutForce,
168187
})
169188
if err != nil {
170-
return nil, fmt.Errorf("unable to hard reset to commit for '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
189+
return nil, fmt.Errorf("unable to checkout tree for branch '%s': %w", c.Branch, err)
190+
}
191+
192+
// Set the current head to point to the requested branch.
193+
err = repo.SetHead("refs/heads/" + c.Branch)
194+
if err != nil {
195+
return nil, fmt.Errorf("unable to set HEAD to branch '%s':%w", c.Branch, err)
171196
}
172197

173198
// Use the current worktree's head as reference for the commit to be returned.
174199
head, err := repo.Head()
175200
if err != nil {
176-
return nil, fmt.Errorf("git resolve HEAD error: %w", err)
201+
return nil, fmt.Errorf("unable to resolve HEAD: %w", err)
177202
}
178203
defer head.Free()
179204

180205
cc, err := repo.LookupCommit(head.Target())
181206
if err != nil {
182-
return nil, fmt.Errorf("failed to lookup HEAD commit '%s' for branch '%s': %w", head.Target(), c.Branch, err)
207+
return nil, fmt.Errorf("unable to lookup HEAD commit '%s' for branch '%s': %w", head.Target(), c.Branch, err)
183208
}
184209
defer cc.Free()
185210

pkg/git/libgit2/managed_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,31 @@ func TestManagedCheckoutBranch_Checkout(t *testing.T) {
552552
g.Expect(err).ToNot(HaveOccurred())
553553
g.Expect(cc.String()).To(Equal(git.DefaultBranch + "/" + commit.Id().String()))
554554
g.Expect(git.IsConcreteCommit(*cc)).To(Equal(true))
555+
556+
// Create a new branch and push it.
557+
err = createBranch(repo, "test", nil)
558+
g.Expect(err).ToNot(HaveOccurred())
559+
transportOptsURL := getTransportOptionsURL(git.HTTP)
560+
managed.AddTransportOptions(transportOptsURL, managed.TransportOptions{
561+
TargetURL: repoURL,
562+
})
563+
defer managed.RemoveTransportOptions(transportOptsURL)
564+
origin, err := repo.Remotes.Create("origin", transportOptsURL)
565+
g.Expect(err).ToNot(HaveOccurred())
566+
err = origin.Push([]string{"refs/heads/test:refs/heads/test"}, &git2go.PushOptions{})
567+
g.Expect(err).ToNot(HaveOccurred())
568+
569+
branch.Branch = "test"
570+
tmpDir2 := t.TempDir()
571+
cc, err = branch.Checkout(ctx, tmpDir2, repoURL, authOpts)
572+
g.Expect(err).ToNot(HaveOccurred())
573+
574+
// Check if the repo HEAD points to the branch.
575+
repo, err = git2go.OpenRepository(tmpDir2)
576+
g.Expect(err).ToNot(HaveOccurred())
577+
head, err := repo.Head()
578+
g.Expect(err).ToNot(HaveOccurred())
579+
g.Expect(head.Branch().Name()).To(Equal("test"))
555580
}
556581

557582
func TestManagedCheckoutTag_Checkout(t *testing.T) {

0 commit comments

Comments
 (0)