-
Notifications
You must be signed in to change notification settings - Fork 162
/
git_repo.go
104 lines (86 loc) · 2.26 KB
/
git_repo.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package releasedir
import (
"path/filepath"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type FSGitRepo struct {
// Will use CmdRunner's WorkingDir when exec-ing to avoid --git-dir weirdness
dirPath string
runner boshsys.CmdRunner
fs boshsys.FileSystem
}
func NewFSGitRepo(dirPath string, runner boshsys.CmdRunner, fs boshsys.FileSystem) FSGitRepo {
return FSGitRepo{dirPath: dirPath, runner: runner, fs: fs}
}
func (r FSGitRepo) Init() error {
_, _, _, err := r.runner.RunCommand("git", "init", r.dirPath)
if err != nil {
return bosherr.WrapErrorf(err, "Initing git repo")
}
ignoreTpl := `config/private.yml
blobs
dev_releases
releases/*.tgz
releases/**/*.tgz
.dev_builds
.final_builds/jobs/**/*.tgz
.final_builds/packages/**/*.tgz
.DS_Store
.idea
*.swp
*~
*#
#*
`
err = r.fs.WriteFileString(filepath.Join(r.dirPath, ".gitignore"), ignoreTpl)
if err != nil {
return bosherr.WrapError(err, "Creating .gitignore file")
}
return nil
}
func (r FSGitRepo) LastCommitSHA() (string, error) {
cmd := boshsys.Command{
Name: "git",
Args: []string{"rev-parse", "--short", "HEAD"},
WorkingDir: r.dirPath,
}
stdout, stderr, _, err := r.runner.RunComplexCommand(cmd)
if err != nil {
if r.isNotGitRepo(stderr) {
return "non-git", nil
}
if strings.Contains(stderr, "Needed a single revision") {
return "empty", nil
}
return "", bosherr.WrapErrorf(err, "Checking last commit SHA")
}
return strings.TrimSpace(stdout), nil
}
func (r FSGitRepo) MustNotBeDirty(force bool) (bool, error) {
cmd := boshsys.Command{
Name: "git",
Args: []string{"status", "--short"},
WorkingDir: r.dirPath,
}
stdout, stderr, _, err := r.runner.RunComplexCommand(cmd)
if err != nil {
if r.isNotGitRepo(stderr) {
return false, nil
}
return false, bosherr.WrapErrorf(err, "Checking status")
}
// Strip out newline which is added if there are any changes
stdout = strings.TrimSpace(stdout)
if len(stdout) > 0 {
if force {
return true, nil
}
return true, bosherr.Errorf("Git repository has local modifications:\n\n%s", stdout)
}
return false, nil
}
func (r FSGitRepo) isNotGitRepo(stderr string) bool {
return strings.Contains(stderr, "Not a git repository")
}