forked from project-stacker/stacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
30 lines (23 loc) · 662 Bytes
/
git.go
File metadata and controls
30 lines (23 loc) · 662 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
package stacker
import (
"os/exec"
"strings"
)
// Git version generates a version string similar to what git describe --always
// does, with -dirty on the end if the git repo had local changes.
func GitVersion(path string) (string, error) {
output, err := exec.Command("git", "-C", path, "status", "--porcelain", "--untracked-files=no").CombinedOutput()
if err != nil {
return "", err
}
isClean := len(output) == 0
output, err = exec.Command("git", "-C", path, "rev-parse", "HEAD").CombinedOutput()
if err != nil {
return "", err
}
hash := strings.TrimSpace(string(output))
if isClean {
return hash, nil
}
return hash + "-dirty", nil
}