Skip to content

Commit 4f53b2c

Browse files
committed
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 <filip@pytloun.cz>
1 parent 7326502 commit 4f53b2c

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

pkg/vendir/fetch/git/git.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,20 @@ func (t *Git) fetch(dstPath string, tempArea ctlfetch.TempArea, bundle string) e
166166
}
167167
}
168168

169-
argss = append(argss, []string{"config", "remote.origin.tagOpt", "--tags"})
170-
171169
if bundle != "" {
172170
argss = append(argss, []string{"bundle", "unbundle", bundle})
173171
}
174172

173+
exactRef := false
175174
{
176175
fetchArgs := []string{"fetch", "origin"}
177176
if strings.HasPrefix(t.opts.Ref, "origin/") {
178177
// only fetch the exact ref we're seeking
179178
fetchArgs = append(fetchArgs, t.opts.Ref[7:])
179+
exactRef = true
180+
} else {
181+
// Only fetch tags if not fetching exact ref
182+
argss = append(argss, []string{"config", "remote.origin.tagOpt", "--tags"})
180183
}
181184
if t.opts.Depth > 0 {
182185
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
201204
}
202205
}
203206

204-
_, _, err = t.cmdRunner.Run([]string{"-c", "advice.detachedHead=false", "checkout", ref}, env, dstPath)
207+
checkoutRef := ref
208+
if exactRef {
209+
checkoutRef = "FETCH_HEAD"
210+
}
211+
212+
_, _, err = t.cmdRunner.Run([]string{"-c", "advice.detachedHead=false", "checkout", checkoutRef}, env, dstPath)
205213
if err != nil {
206214
return err
207215
}

0 commit comments

Comments
 (0)