Skip to content

Commit 0df2b0e

Browse files
author
Blake Burkhart
committed
Fix tag checkout with libgit2
SetHeadDetached (git_repository_set_head_detached) only changes HEAD, and does not actually checkout the files on disk. Use CheckoutHead with the CheckoutForce Strategy to actually check the files out on disk. Additionally add a test that validates the hash of a checked out file's contents. Previously, the hash of the desired tag was being reported as the checked out revision by the GitRepository. However the wrong files were checked out and an incorrect revision would be deployed by Flux. Signed-off-by: Blake Burkhart <blake.burkhart@us.af.mil>
1 parent 76aa40d commit 0df2b0e

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)