fetchBase updates the local copy of a remote-tracking base ref (e.g. "origin/main") so the worktree branches from the latest remote state. Refs that don't name a remote (a local branch, tag, or commit) are left alone. A fetch failure is reported as [ErrInvalidBase]. A fully-qualified refspec (refs/
(ctx context.Context, root, base string)
| 359 | // would leave the subsequent "git worktree add ... <remote>/<branch>" |
| 360 | // resolving a stale remote-tracking ref. |
| 361 | func fetchBase(ctx context.Context, root, base string) error { |
| 362 | remote, branch, ok := strings.Cut(base, "/") |
| 363 | if !ok { |
| 364 | return nil |
| 365 | } |
| 366 | if !isRemote(ctx, root, remote) { |
| 367 | return nil |
| 368 | } |
| 369 | refspec := "refs/heads/" + branch + ":refs/remotes/" + remote + "/" + branch |
| 370 | if err := git(ctx, root, "fetch", remote, refspec); err != nil { |
| 371 | return fmt.Errorf("%w: fetching %s: %w", ErrInvalidBase, base, err) |
| 372 | } |
| 373 | return nil |
| 374 | } |
| 375 | |
| 376 | // isRemote reports whether name is a configured git remote. |
| 377 | func isRemote(ctx context.Context, root, name string) bool { |