(path string)
| 16 | } |
| 17 | |
| 18 | func NewRepository(path string) (*Repository, error) { |
| 19 | cmd := exec.Command("git", "rev-parse", "--git-dir") |
| 20 | cmd.Dir = path |
| 21 | output, err := cmd.Output() |
| 22 | if err != nil { |
| 23 | return nil, fmt.Errorf("not a git repository") |
| 24 | } |
| 25 | |
| 26 | gitDir := strings.TrimSpace(string(output)) |
| 27 | if !filepath.IsAbs(gitDir) { |
| 28 | gitDir = filepath.Join(path, gitDir) |
| 29 | } |
| 30 | |
| 31 | workTreeCmd := exec.Command("git", "rev-parse", "--show-toplevel") |
| 32 | workTreeCmd.Dir = path |
| 33 | workTreeOutput, err := workTreeCmd.Output() |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("could not determine work tree: %v", err) |
| 36 | } |
| 37 | |
| 38 | workTree := strings.TrimSpace(string(workTreeOutput)) |
| 39 | |
| 40 | return &Repository{ |
| 41 | gitDir: gitDir, |
| 42 | workTree: workTree, |
| 43 | }, nil |
| 44 | } |
| 45 | |
| 46 | func (r *Repository) GitDir() string { |
| 47 | return r.gitDir |
no outgoing calls