Skip to content

Commit 15c064a

Browse files
committed
git/libgit2: set CheckoutForce on branch strategy
In the recent update from libgit2 1.1.x to 1.3.x, something seems to have changed upstream. Resulting in the clone of a branch ending up with a semi-bare file system state (in other words: without any files present in the directory). This commit patches the clone behavior to set the `CheckoutForce` strategy as `CheckoutOption`, which mitigates the issue. In addition, test cases have been added to ensure we do not run into this again by asserting the state of the branch after cloning. Signed-off-by: Hidde Beydals <[email protected]>
1 parent b65cf9d commit 15c064a

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

pkg/git/gogit/checkout_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,20 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
5858
tests := []struct {
5959
name string
6060
branch string
61+
filesCreated map[string]string
6162
expectedCommit string
6263
expectedErr string
6364
}{
6465
{
6566
name: "Default branch",
6667
branch: "master",
68+
filesCreated: map[string]string{"branch": "init"},
6769
expectedCommit: firstCommit.String(),
6870
},
6971
{
7072
name: "Other branch",
7173
branch: "test",
74+
filesCreated: map[string]string{"branch": "second"},
7275
expectedCommit: secondCommit.String(),
7376
},
7477
{
@@ -90,12 +93,18 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
9093

9194
cc, err := branch.Checkout(context.TODO(), tmpDir, path, nil)
9295
if tt.expectedErr != "" {
96+
g.Expect(err).To(HaveOccurred())
9397
g.Expect(err.Error()).To(ContainSubstring(tt.expectedErr))
9498
g.Expect(cc).To(BeNil())
9599
return
96100
}
97-
g.Expect(err).To(BeNil())
101+
g.Expect(err).ToNot(HaveOccurred())
98102
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
103+
104+
for k, v := range tt.filesCreated {
105+
g.Expect(filepath.Join(tmpDir, k)).To(BeARegularFile())
106+
g.Expect(os.ReadFile(filepath.Join(tmpDir, k))).To(BeEquivalentTo(v))
107+
}
99108
})
100109
}
101110
}

pkg/git/libgit2/checkout.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
6666
RemoteCallbacks: RemoteCallbacks(ctx, opts),
6767
ProxyOptions: git2go.ProxyOptions{Type: git2go.ProxyTypeAuto},
6868
},
69+
CheckoutOptions: git2go.CheckoutOptions{
70+
Strategy: git2go.CheckoutForce,
71+
},
6972
CheckoutBranch: c.Branch,
7073
})
7174
if err != nil {
@@ -79,7 +82,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
7982
defer head.Free()
8083
cc, err := repo.LookupCommit(head.Target())
8184
if err != nil {
82-
return nil, fmt.Errorf("could not find commit '%s' in branch '%s': %w", head.Target(), c.Branch, err)
85+
return nil, fmt.Errorf("failed to lookup HEAD commit '%s' for branch '%s': %w", head.Target(), c.Branch, err)
8386
}
8487
defer cc.Free()
8588
return buildCommit(cc, "refs/heads/"+c.Branch), nil

pkg/git/libgit2/checkout_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,20 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
7373
tests := []struct {
7474
name string
7575
branch string
76+
filesCreated map[string]string
7677
expectedCommit string
7778
expectedErr string
7879
}{
7980
{
8081
name: "Default branch",
8182
branch: defaultBranch,
83+
filesCreated: map[string]string{"branch": "second"},
8284
expectedCommit: secondCommit.String(),
8385
},
8486
{
8587
name: "Other branch",
8688
branch: "test",
89+
filesCreated: map[string]string{"branch": "init"},
8790
expectedCommit: firstCommit.String(),
8891
},
8992
{
@@ -112,6 +115,11 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
112115
}
113116
g.Expect(err).ToNot(HaveOccurred())
114117
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
118+
119+
for k, v := range tt.filesCreated {
120+
g.Expect(filepath.Join(tmpDir, k)).To(BeARegularFile())
121+
g.Expect(os.ReadFile(filepath.Join(tmpDir, k))).To(BeEquivalentTo(v))
122+
}
115123
})
116124
}
117125
}

0 commit comments

Comments
 (0)