From 4f53b2cab7f4e31f89580e8394195d4212e775b8 Mon Sep 17 00:00:00 2001 From: Filip Pytloun Date: Mon, 13 Oct 2025 15:26:30 +0200 Subject: [PATCH] fix(git): optimize fetch to selectively include tags and checkout exact refs - Disabled unconditional addition of "remote.origin.tagOpt" config; only add if not fetching exact ref - Introduced exactRef flag to detect when fetching a specific origin ref - When fetching exact ref, checkout uses "FETCH_HEAD" instead of the ref name to avoid detached head advice - Cleaned up commented-out code and improved fetch argument logic accordingly Signed-off-by: Filip Pytloun --- pkg/vendir/fetch/git/git.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/vendir/fetch/git/git.go b/pkg/vendir/fetch/git/git.go index 07883a7f..fee00694 100644 --- a/pkg/vendir/fetch/git/git.go +++ b/pkg/vendir/fetch/git/git.go @@ -166,17 +166,20 @@ func (t *Git) fetch(dstPath string, tempArea ctlfetch.TempArea, bundle string) e } } - argss = append(argss, []string{"config", "remote.origin.tagOpt", "--tags"}) - if bundle != "" { argss = append(argss, []string{"bundle", "unbundle", bundle}) } + exactRef := false { fetchArgs := []string{"fetch", "origin"} if strings.HasPrefix(t.opts.Ref, "origin/") { // only fetch the exact ref we're seeking fetchArgs = append(fetchArgs, t.opts.Ref[7:]) + exactRef = true + } else { + // Only fetch tags if not fetching exact ref + argss = append(argss, []string{"config", "remote.origin.tagOpt", "--tags"}) } if t.opts.Depth > 0 { fetchArgs = append(fetchArgs, "--depth", strconv.Itoa(t.opts.Depth)) @@ -201,7 +204,12 @@ func (t *Git) fetch(dstPath string, tempArea ctlfetch.TempArea, bundle string) e } } - _, _, err = t.cmdRunner.Run([]string{"-c", "advice.detachedHead=false", "checkout", ref}, env, dstPath) + checkoutRef := ref + if exactRef { + checkoutRef = "FETCH_HEAD" + } + + _, _, err = t.cmdRunner.Run([]string{"-c", "advice.detachedHead=false", "checkout", checkoutRef}, env, dstPath) if err != nil { return err }