NewRepository creates a new repository object that can be used for running `git` commands within that repository.
(path string)
| 35 | // NewRepository creates a new repository object that can be used for |
| 36 | // running `git` commands within that repository. |
| 37 | func NewRepository(path string) (*Repository, error) { |
| 38 | // Find the `git` executable to be used: |
| 39 | gitBin, err := findGitBin() |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf( |
| 42 | "could not find 'git' executable (is it in your PATH?): %w", err, |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | //nolint:gosec // `gitBin` is chosen carefully, and `path` is the |
| 47 | // path to the repository. |
| 48 | cmd := exec.Command(gitBin, "-C", path, "rev-parse", "--git-dir") |
| 49 | out, err := cmd.Output() |
| 50 | if err != nil { |
| 51 | switch err := err.(type) { |
| 52 | case *exec.Error: |
| 53 | return nil, fmt.Errorf( |
| 54 | "could not run '%s': %w", gitBin, err.Err, |
| 55 | ) |
| 56 | case *exec.ExitError: |
| 57 | return nil, fmt.Errorf( |
| 58 | "git rev-parse failed: %s", err.Stderr, |
| 59 | ) |
| 60 | default: |
| 61 | return nil, err |
| 62 | } |
| 63 | } |
| 64 | gitDir := smartJoin(path, string(bytes.TrimSpace(out))) |
| 65 | |
| 66 | //nolint:gosec // `gitBin` is chosen carefully. |
| 67 | cmd = exec.Command(gitBin, "rev-parse", "--git-path", "shallow") |
| 68 | cmd.Dir = gitDir |
| 69 | out, err = cmd.Output() |
| 70 | if err != nil { |
| 71 | return nil, fmt.Errorf( |
| 72 | "could not run 'git rev-parse --git-path shallow': %w", err, |
| 73 | ) |
| 74 | } |
| 75 | shallow := smartJoin(gitDir, string(bytes.TrimSpace(out))) |
| 76 | _, err = os.Lstat(shallow) |
| 77 | if err == nil { |
| 78 | return nil, errors.New("this appears to be a shallow clone; full clone required") |
| 79 | } |
| 80 | |
| 81 | return &Repository{ |
| 82 | path: gitDir, |
| 83 | gitBin: gitBin, |
| 84 | }, nil |
| 85 | } |
| 86 | |
| 87 | func (repo *Repository) GitCommand(callerArgs ...string) *exec.Cmd { |
| 88 | args := []string{ |
no test coverage detected