@@ -28,9 +28,11 @@ import (
2828 "strings"
2929 "time"
3030
31- "github.com/ProtonMail/go-crypto/openpgp"
32- "github.com/ProtonMail/go-crypto/openpgp/armor"
3331 "github.com/fluxcd/pkg/apis/acl"
32+ git2go "github.com/libgit2/git2go/v33"
33+ "golang.org/x/crypto/openpgp"
34+ "golang.org/x/crypto/openpgp/armor"
35+
3436 "github.com/go-git/go-billy/v5/memfs"
3537 "github.com/go-git/go-git/v5"
3638 "github.com/go-git/go-git/v5/config"
@@ -61,9 +63,15 @@ import (
6163
6264const timeout = 10 * time .Second
6365
64- // Copied from
65- // https://github.com/fluxcd/source-controller/blob/master/controllers/suite_test.go
66- var letterRunes = []rune ("abcdefghijklmnopqrstuvwxyz1234567890" )
66+ var (
67+ // Copied from
68+ // https://github.com/fluxcd/source-controller/blob/master/controllers/suite_test.go
69+ letterRunes = []rune ("abcdefghijklmnopqrstuvwxyz1234567890" )
70+
71+ gitServer * gittestserver.GitServer
72+
73+ repositoryPath string
74+ )
6775
6876func randStringRunes (n int ) string {
6977 b := make ([]rune , n )
@@ -76,10 +84,8 @@ func randStringRunes(n int) string {
7684var _ = Describe ("ImageUpdateAutomation" , func () {
7785 var (
7886 branch string
79- repositoryPath string
8087 namespace * corev1.Namespace
8188 username , password string
82- gitServer * gittestserver.GitServer
8389 )
8490
8591 // Start the git server
@@ -782,7 +788,7 @@ Images:
782788 // verify commit
783789 ent , err := commit .Verify (b .String ())
784790 Expect (err ).ToNot (HaveOccurred ())
785- Expect (ent .PrimaryKey .Fingerprint ).To (Equal (pgpEntity .PrimaryKey .Fingerprint ))
791+ Expect (ent .PrimaryKey .Fingerprint ).To (Equal (pgpEntity .PrimaryKey .Fingerprint [:] ))
786792 })
787793 })
788794
@@ -1353,11 +1359,27 @@ func initGitRepo(gitServer *gittestserver.GitServer, fixture, branch, repository
13531359 return err
13541360 }
13551361
1356- return remote .Push (& git.PushOptions {
1362+ err = remote .Push (& git.PushOptions {
13571363 RefSpecs : []config.RefSpec {
13581364 config .RefSpec (fmt .Sprintf ("refs/heads/%s:refs/heads/%s" , branch , branch )),
13591365 },
13601366 })
1367+ if err != nil {
1368+ return err
1369+ }
1370+
1371+ // Adhoc fix to a bug in which most clone operations during test fail.
1372+ // This started with the upgrade from libgit2 1.1.1 to 1.3.0, and
1373+ // requires further investigation as to why repositories created and
1374+ // amended by go-git stopped working for libgit2 clone operations.
1375+ repo2 , err := git2go .OpenRepository (gitServer .Root () + repositoryPath )
1376+ if err != nil {
1377+ return err
1378+ }
1379+ defer repo2 .Free ()
1380+ _ , err = commitFile (repo2 , "branch" , "init" , time .Now ())
1381+
1382+ return err
13611383}
13621384
13631385func checkoutBranch (repo * git.Repository , branch string ) error {
@@ -1413,3 +1435,77 @@ func mergeBranchIntoHead(repo *git.Repository, pushBranch string) {
14131435 })
14141436 Expect (err ).NotTo (HaveOccurred ())
14151437}
1438+
1439+ // copied from source-controller/pkg/git/libgit2/checkout.go
1440+ func commitFile (repo * git2go.Repository , path , content string , time time.Time ) (* git2go.Oid , error ) {
1441+ var parentC []* git2go.Commit
1442+ head , err := headCommit (repo )
1443+ if err == nil {
1444+ defer head .Free ()
1445+ parentC = append (parentC , head )
1446+ }
1447+
1448+ index , err := repo .Index ()
1449+ if err != nil {
1450+ return nil , err
1451+ }
1452+ defer index .Free ()
1453+
1454+ blobOID , err := repo .CreateBlobFromBuffer ([]byte (content ))
1455+ if err != nil {
1456+ return nil , err
1457+ }
1458+
1459+ entry := & git2go.IndexEntry {
1460+ Mode : git2go .FilemodeBlob ,
1461+ Id : blobOID ,
1462+ Path : path ,
1463+ }
1464+
1465+ if err := index .Add (entry ); err != nil {
1466+ return nil , err
1467+ }
1468+ if err := index .Write (); err != nil {
1469+ return nil , err
1470+ }
1471+
1472+ treeID , err := index .WriteTree ()
1473+ if err != nil {
1474+ return nil , err
1475+ }
1476+
1477+ tree , err := repo .LookupTree (treeID )
1478+ if err != nil {
1479+ return nil , err
1480+ }
1481+ defer tree .Free ()
1482+
1483+ c , err := repo .CreateCommit ("HEAD" , mockSignature (time ), mockSignature (time ), "Committing " + path , tree , parentC ... )
1484+ if err != nil {
1485+ return nil , err
1486+ }
1487+ return c , nil
1488+ }
1489+
1490+ // copied from source-controller/pkg/git/libgit2/checkout.go
1491+ func mockSignature (time time.Time ) * git2go.Signature {
1492+ return & git2go.Signature {
1493+ Name : "Jane Doe" ,
1494+ 1495+ When : time ,
1496+ }
1497+ }
1498+
1499+ // copied from source-controller/pkg/git/libgit2/checkout.go
1500+ func headCommit (repo * git2go.Repository ) (* git2go.Commit , error ) {
1501+ head , err := repo .Head ()
1502+ if err != nil {
1503+ return nil , err
1504+ }
1505+ defer head .Free ()
1506+ c , err := repo .LookupCommit (head .Target ())
1507+ if err != nil {
1508+ return nil , err
1509+ }
1510+ return c , nil
1511+ }
0 commit comments