generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_git.go
More file actions
38 lines (32 loc) · 864 Bytes
/
open_git.go
File metadata and controls
38 lines (32 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package git
import (
"os/exec"
"path/filepath"
"strings"
)
// Git is the struct used to house all methods in use in Commitsar.
type Git struct {
Path string
}
// OpenGit loads Repo on path and returns a new Git struct to work with.
func OpenGit(path string) (*Git, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
// Verify it's a git repository
cmd := exec.Command("git", "-C", absPath, "rev-parse", "--git-dir")
if err := cmd.Run(); err != nil {
return nil, err
}
return &Git{Path: absPath}, nil
}
// runGitCommand runs a git command in the repository directory
func (g *Git) runGitCommand(args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", g.Path}, args...)...)
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}