Skip to content

Commit bd3d781

Browse files
authored
Merge pull request #589 from fluxcd/libgit2-branch-checkout
2 parents b65cf9d + eff40e2 commit bd3d781

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
5151

5252
// ignores the error here because it can be defaulted
5353
// https://github.blog/2020-07-27-highlights-from-git-2-28/#introducing-init-defaultbranch
54-
defaultBranch := "main"
55-
if v, err := cfg.LookupString("init.defaultBranch"); err != nil {
54+
defaultBranch := "master"
55+
if v, err := cfg.LookupString("init.defaultBranch"); err != nil && v != "" {
5656
defaultBranch = v
5757
}
5858

@@ -61,10 +61,12 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
6161
t.Fatal(err)
6262
}
6363

64+
// Branch off on first commit
6465
if err = createBranch(repo, "test", nil); err != nil {
6566
t.Fatal(err)
6667
}
6768

69+
// Create second commit on default branch
6870
secondCommit, err := commitFile(repo, "branch", "second", time.Now())
6971
if err != nil {
7072
t.Fatal(err)
@@ -73,17 +75,20 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
7375
tests := []struct {
7476
name string
7577
branch string
78+
filesCreated map[string]string
7679
expectedCommit string
7780
expectedErr string
7881
}{
7982
{
8083
name: "Default branch",
8184
branch: defaultBranch,
85+
filesCreated: map[string]string{"branch": "second"},
8286
expectedCommit: secondCommit.String(),
8387
},
8488
{
8589
name: "Other branch",
8690
branch: "test",
91+
filesCreated: map[string]string{"branch": "init"},
8792
expectedCommit: firstCommit.String(),
8893
},
8994
{
@@ -112,6 +117,11 @@ func TestCheckoutBranch_Checkout(t *testing.T) {
112117
}
113118
g.Expect(err).ToNot(HaveOccurred())
114119
g.Expect(cc.String()).To(Equal(tt.branch + "/" + tt.expectedCommit))
120+
121+
for k, v := range tt.filesCreated {
122+
g.Expect(filepath.Join(tmpDir, k)).To(BeARegularFile())
123+
g.Expect(os.ReadFile(filepath.Join(tmpDir, k))).To(BeEquivalentTo(v))
124+
}
115125
})
116126
}
117127
}

0 commit comments

Comments
 (0)