From d2f3e896cd244b64cab8951eced77dbe8321e91c Mon Sep 17 00:00:00 2001 From: Guillaume Calmettes Date: Fri, 24 Dec 2021 21:13:45 +0100 Subject: [PATCH] Fix regression: can fetch branches and tags references without specifying commit hashes for private git repository used as context (#1823) * [FIX] can fetch tags and branches refs without specifying commit hashes * [ENH] more precise comment --- pkg/buildcontext/git.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 6137e9bc81..64b37fd5c1 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -68,11 +68,19 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { RecurseSubmodules: getRecurseSubmodules(g.opts.GitRecurseSubmodules), } var fetchRef string + var checkoutRef string if len(parts) > 1 { if plumbing.IsHash(parts[1]) || !strings.HasPrefix(parts[1], "refs/pull/") { // Handle any non-branch refs separately. First, clone the repo HEAD, and // then fetch and check out the fetchRef. fetchRef = parts[1] + if plumbing.IsHash(parts[1]) { + checkoutRef = fetchRef + } else { + // The ReferenceName still needs to be present in the options passed + // to the clone operation for non-hash references of private repositories. + options.ReferenceName = plumbing.ReferenceName(fetchRef) + } } else { // Branches will be cloned directly. options.ReferenceName = plumbing.ReferenceName(parts[1]) @@ -104,7 +112,6 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { } } - checkoutRef := fetchRef if len(parts) > 2 { checkoutRef = parts[2] }