(ctx context.Context, git *gitutil.GitCLI, depth int, includeTags bool, refs []*RemoteGitRef)
| 346 | } |
| 347 | |
| 348 | func (repo *RemoteGitRepository) fetch(ctx context.Context, git *gitutil.GitCLI, depth int, includeTags bool, refs []*RemoteGitRef) error { |
| 349 | query, err := CurrentQuery(ctx) |
| 350 | if err != nil { |
| 351 | return err |
| 352 | } |
| 353 | |
| 354 | if len(refs) == 0 && !includeTags { |
| 355 | // Nothing requested: avoid an implicit broad fetch from origin. |
| 356 | return nil |
| 357 | } |
| 358 | |
| 359 | // Fetch by object SHA in the hot path (`--no-tags`), and only retry by named refs for SHA-incompatible remotes. |
| 360 | logger := slog.SpanLogger(ctx, InstrumentationLibrary) |
| 361 | |
| 362 | gitDir, err := git.GitDir(ctx) |
| 363 | if err != nil { |
| 364 | return err |
| 365 | } |
| 366 | |
| 367 | shaRefSpecs := make([]string, len(refs)) |
| 368 | for i, ref := range refs { |
| 369 | // Default hot path: fetch exact objects by SHA; ref names are already resolved via ls-remote. |
| 370 | shaRefSpecs[i] = ref.SHA |
| 371 | } |
| 372 | |
| 373 | runFetch := func(refSpecs []string) error { |
| 374 | args := []string{ |
| 375 | "fetch", |
| 376 | "--no-tags", |
| 377 | "--update-head-ok", |
| 378 | "--force", |
| 379 | } |
| 380 | if depth <= 0 { |
| 381 | if _, err := os.Lstat(filepath.Join(gitDir, "shallow")); err == nil { |
| 382 | args = append(args, "--unshallow") |
| 383 | } |
| 384 | } else { |
| 385 | args = append(args, "--depth="+fmt.Sprint(depth)) |
| 386 | } |
| 387 | args = append(args, "origin") |
| 388 | args = append(args, refSpecs...) |
| 389 | |
| 390 | if _, err := git.Run(ctx, args...); err != nil { |
| 391 | if errors.Is(err, gitutil.ErrShallowNotSupported) { |
| 392 | // fallback to full fetch |
| 393 | args = slices.DeleteFunc(args, func(s string) bool { |
| 394 | return strings.HasPrefix(s, "--depth") |
| 395 | }) |
| 396 | _, err = git.Run(ctx, args...) |
| 397 | } |
| 398 | if err != nil { |
| 399 | return err |
| 400 | } |
| 401 | } |
| 402 | return nil |
| 403 | } |
| 404 | |
| 405 | runFetchTags := func() error { |
no test coverage detected