Skip to content

Commit 887d2c5

Browse files
authored
Merge pull request #394 from bburky/libgit2-tag-checkout
Fix tag checkout with libgit2
2 parents 76aa40d + 0df2b0e commit 887d2c5

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

pkg/git/libgit2/checkout.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,14 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.
112112
if err != nil {
113113
return nil, "", fmt.Errorf("git commit '%s' not found: %w", head.Target(), err)
114114
}
115-
return &Commit{commit}, fmt.Sprintf("%s/%s", c.tag, head.Target().String()), nil
115+
err = repo.CheckoutHead(&git2go.CheckoutOpts{
116+
Strategy: git2go.CheckoutForce,
117+
})
118+
if err != nil {
119+
return nil, "", fmt.Errorf("git checkout error: %w", err)
120+
}
121+
122+
return &Commit{commit}, fmt.Sprintf("%s/%s", c.tag, commit.Id().String()), nil
116123
}
117124

118125
type CheckoutCommit struct {
@@ -218,6 +225,12 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *g
218225
if err != nil {
219226
return nil, "", fmt.Errorf("git commit '%s' not found: %w", head.Target().String(), err)
220227
}
228+
err = repo.CheckoutHead(&git2go.CheckoutOpts{
229+
Strategy: git2go.CheckoutForce,
230+
})
231+
if err != nil {
232+
return nil, "", fmt.Errorf("git checkout error: %w", err)
233+
}
221234

222-
return &Commit{commit}, fmt.Sprintf("%s/%s", t, head.Target().String()), nil
235+
return &Commit{commit}, fmt.Sprintf("%s/%s", t, commit.Id().String()), nil
223236
}

pkg/git/libgit2/checkout_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ package libgit2
1818

1919
import (
2020
"context"
21+
"crypto/sha256"
22+
"encoding/hex"
23+
"io"
2124
"io/ioutil"
2225
"os"
26+
"path"
2327
"testing"
2428

2529
git2go "github.com/libgit2/git2go/v31"
@@ -44,6 +48,21 @@ func TestCheckoutTagSemVer_Checkout(t *testing.T) {
4448
t.Error(err)
4549
}
4650

51+
// Ensure the correct files are checked out on disk
52+
f, err := os.Open(path.Join(tmpDir, "README.md"))
53+
if err != nil {
54+
t.Error(err)
55+
}
56+
defer f.Close()
57+
h := sha256.New()
58+
if _, err := io.Copy(h, f); err != nil {
59+
t.Error(err)
60+
}
61+
fileHash := hex.EncodeToString(h.Sum(nil))
62+
if fileHash != "2bd1707542a11f987ee24698dcc095a9f57639f401133ef6a29da97bf8f3f302" {
63+
t.Errorf("expected files not checked out. Expected hash %s, got %s", "2bd1707542a11f987ee24698dcc095a9f57639f401133ef6a29da97bf8f3f302", fileHash)
64+
}
65+
4766
semVer := CheckoutSemVer{
4867
semVer: ">=1.0.0 <=1.7.0",
4968
}

0 commit comments

Comments
 (0)