From c341ac33641841543229e338985764e14a719fb5 Mon Sep 17 00:00:00 2001 From: Sanskar Jaiswal Date: Fri, 3 Jun 2022 13:44:26 +0530 Subject: [PATCH] libgit2: return CheckoutTag with LastRevision Signed-off-by: Sanskar Jaiswal --- pkg/git/libgit2/checkout.go | 5 ++- pkg/git/libgit2/checkout_test.go | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/pkg/git/libgit2/checkout.go b/pkg/git/libgit2/checkout.go index a4a5721a..056dc0b1 100644 --- a/pkg/git/libgit2/checkout.go +++ b/pkg/git/libgit2/checkout.go @@ -48,7 +48,10 @@ func CheckoutStrategyForOptions(ctx context.Context, opt git.CheckoutOptions) gi case opt.SemVer != "": return &CheckoutSemVer{SemVer: opt.SemVer} case opt.Tag != "": - return &CheckoutTag{Tag: opt.Tag} + return &CheckoutTag{ + Tag: opt.Tag, + LastRevision: opt.LastRevision, + } default: branch := opt.Branch if branch == "" { diff --git a/pkg/git/libgit2/checkout_test.go b/pkg/git/libgit2/checkout_test.go index 46f8628c..0ff5ee88 100644 --- a/pkg/git/libgit2/checkout_test.go +++ b/pkg/git/libgit2/checkout_test.go @@ -498,3 +498,67 @@ func TestInitializeRepoWithRemote(t *testing.T) { _, _, err = initializeRepoWithRemote(ctx, tmp, testRepoURL2, authOpts2) g.Expect(err).To(HaveOccurred()) } + +func TestCheckoutStrategyForOptions(t *testing.T) { + tests := []struct { + name string + opts git.CheckoutOptions + expectedStrat git.CheckoutStrategy + }{ + { + name: "commit works", + opts: git.CheckoutOptions{ + Commit: "commit", + }, + expectedStrat: &CheckoutCommit{ + Commit: "commit", + }, + }, + { + name: "semver works", + opts: git.CheckoutOptions{ + SemVer: ">= 1.0.0", + }, + expectedStrat: &CheckoutSemVer{ + SemVer: ">= 1.0.0", + }, + }, + { + name: "tag with latest revision works", + opts: git.CheckoutOptions{ + Tag: "v0.1.0", + LastRevision: "ar34oi2njrngjrng", + }, + expectedStrat: &CheckoutTag{ + Tag: "v0.1.0", + LastRevision: "ar34oi2njrngjrng", + }, + }, + { + name: "branch with latest revision works", + opts: git.CheckoutOptions{ + Branch: "main", + LastRevision: "rrgij20mkmrg", + }, + expectedStrat: &CheckoutBranch{ + Branch: "main", + LastRevision: "rrgij20mkmrg", + }, + }, + { + name: "empty branch falls back to default", + opts: git.CheckoutOptions{}, + expectedStrat: &CheckoutBranch{ + Branch: git.DefaultBranch, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + strat := CheckoutStrategyForOptions(context.TODO(), tt.opts) + g.Expect(strat).To(Equal(tt.expectedStrat)) + }) + } +}