-
Notifications
You must be signed in to change notification settings - Fork 47
/
git.go
115 lines (96 loc) · 3.45 KB
/
git.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
105
106
107
108
109
110
111
112
113
114
115
package dstask
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
)
// RunGitCmd shells out to git in the context of the dstask repo.
func RunGitCmd(repoPath string, args ...string) error {
args = append([]string{"-C", repoPath}, args...)
return RunCmd("git", args...)
}
// MustRunGitCmd delegates to RunGitCmd and exits the program on any error.
func MustRunGitCmd(repoPath string, args ...string) {
err := RunGitCmd(repoPath, args...)
if err != nil {
ExitFail("Failed to run git cmd.")
}
}
// MustGitCommit is like GitCommit, except if any error is
// encountered, the program exits.
func MustGitCommit(repoPath, format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Printf("\n%s\n", msg)
fmt.Printf("\033[38;5;245m")
if err := GitCommit(repoPath, format, a...); err != nil {
ExitFail("error: %s", err)
}
fmt.Printf("\033[0m")
}
// GitCommit stages changes in the dstask repository and commits them.
func GitCommit(repoPath, format string, a ...interface{}) error {
msg := fmt.Sprintf(format, a...)
// needed before add cmd, see diff-index command
bins, err := ioutil.ReadDir(path.Join(repoPath, ".git/objects"))
if err != nil {
return fmt.Errorf("failed to run git commit: %s", err)
}
brandNew := len(bins) <= 2
// git add all changed/created files
// could optimise this to be given an explicit list of
// added/modified/deleted files -- only if slow.
// tell git to stage (all) changes
if err = RunGitCmd(repoPath, "add", "."); err != nil {
return fmt.Errorf("failed to add changes to repo: %s", err)
}
// check for changes -- returns exit status 1 on change. Make sure git repo
// has commits first, to avoid missing HEAD error.
if !brandNew && RunGitCmd(repoPath, "diff-index", "--quiet", "HEAD", "--") == nil {
fmt.Println("No changes detected")
return nil
}
if err = RunGitCmd(repoPath, "commit", "--no-gpg-sign", "-m", msg); err != nil {
return fmt.Errorf("failed to commit changes: %s", err)
}
return nil
}
// MustGetRepoPath returns the full path to a file within the dstask git repo.
// Pass file as an empty string to return the git repo directory itself.
func MustGetRepoPath(repoPath, directory, file string) string {
dir := path.Join(repoPath, directory)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, 0700)
if err != nil {
ExitFail("Failed to create directory in git repository")
}
}
return path.Join(dir, file)
}
// EnsureRepoExists checks for the existence of a dstask repository, or exits the program.
func EnsureRepoExists(repoPath string) {
_, err := exec.LookPath("git")
if err != nil {
ExitFail("git required, please install")
}
gitDotGitLocation := path.Join(repoPath, ".git")
if _, err := os.Stat(gitDotGitLocation); os.IsNotExist(err) {
if StdoutIsTTY() {
ConfirmOrAbort("Could not find dstask repository at %s -- create?", repoPath)
}
err = os.Mkdir(repoPath, 0700)
if err != nil {
ExitFail("Failed to create directory in git repository")
}
MustRunGitCmd(repoPath, "init")
fmt.Println("\nAdd a remote repository with:\n\n\tdstask git remote add origin <repo>")
fmt.Println() // must be a separate call else compiler complains of redundant \n
}
}
// Sync performs a git pull, and then a git push. If any conflicts are encountered,
// the user will need to resolve them.
func Sync(repoPath string) {
MustRunGitCmd(repoPath, "pull", "--ff", "--no-rebase", "--no-edit", "--commit", "origin", "master")
MustRunGitCmd(repoPath, "push", "origin", "master")
}