| 254 | } |
| 255 | |
| 256 | func getRepoSwitchedCommit(dirName, commit string) (string, string, error) { |
| 257 | r, err := git.PlainOpen(dirName) |
| 258 | if err != nil { |
| 259 | return "", "", fmt.Errorf("opening git: %w", err) |
| 260 | } |
| 261 | |
| 262 | defaultBranch, err := r.Reference(plumbing.NewRemoteHEADReferenceName("origin"), true) |
| 263 | if err != nil { |
| 264 | return "", "", fmt.Errorf("resolving default branch: %w", err) |
| 265 | } |
| 266 | |
| 267 | if commit == "" || commit == "main" { |
| 268 | commit = defaultBranch.Hash().String() |
| 269 | } |
| 270 | |
| 271 | w, err := r.Worktree() |
| 272 | if err != nil { |
| 273 | return "", "", fmt.Errorf("getting work tree: %w", err) |
| 274 | } |
| 275 | |
| 276 | if err = w.Checkout(&git.CheckoutOptions{Hash: plumbing.NewHash(commit)}); err != nil { //nolint:exhaustruct |
| 277 | return "", "", fmt.Errorf("checking out %q: %w", commit, err) |
| 278 | } |
| 279 | |
| 280 | return defaultBranch.Name().Short(), commit, nil |
| 281 | } |
| 282 | |
| 283 | func enumerateProto(dirName string, repo filter.Repo) ([]content.File, error) { |
| 284 | res := make([]content.File, 0, minNumberOfFiles) |