()
| 56 | } |
| 57 | |
| 58 | func (h *Handler) setup() error { |
| 59 | h.once.Do(func() { |
| 60 | tempDir, err := os.MkdirTemp("", repoDir) |
| 61 | if err != nil { |
| 62 | h.errSetup = err |
| 63 | return |
| 64 | } |
| 65 | h.tempDir = tempDir |
| 66 | h.gitRepo, err = git.PlainClone(h.tempDir, false, &git.CloneOptions{ |
| 67 | URL: h.cloneURL, |
| 68 | // TODO: auth may be required for private repos |
| 69 | Depth: 1, // currently only use the git repo for files, dont need history |
| 70 | SingleBranch: true, |
| 71 | // https://github.com/go-git/go-git/issues/545#issuecomment-1353681676 |
| 72 | Tags: git.NoTags, |
| 73 | }) |
| 74 | if err != nil { |
| 75 | h.errSetup = err |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | // assume the commit SHA is reachable from the default branch |
| 80 | // this isn't as flexible as the tarball handler, but good enough for now |
| 81 | if h.commitSHA != clients.HeadSHA { |
| 82 | wt, err := h.gitRepo.Worktree() |
| 83 | if err != nil { |
| 84 | h.errSetup = err |
| 85 | return |
| 86 | } |
| 87 | if err := wt.Checkout(&git.CheckoutOptions{Hash: plumbing.NewHash(h.commitSHA)}); err != nil { |
| 88 | h.errSetup = fmt.Errorf("checkout specified commit: %w", err) |
| 89 | return |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // go-git is not thread-safe so list the files inside this sync.Once and save them |
| 94 | // https://github.com/go-git/go-git/issues/773 |
| 95 | files, err := enumerateFiles(h.gitRepo) |
| 96 | if err != nil { |
| 97 | h.errSetup = err |
| 98 | return |
| 99 | } |
| 100 | h.files = files |
| 101 | }) |
| 102 | return h.errSetup |
| 103 | } |
| 104 | |
| 105 | func (h *Handler) GetLocalPath() (string, error) { |
| 106 | if err := h.setup(); err != nil { |
no test coverage detected