forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
125 lines (109 loc) · 3.35 KB
/
client.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
116
117
118
119
120
121
122
123
124
125
package git
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
)
// Client is a generic git client interface
type Client interface {
CloneOrFetch(url string, username string, password string, sshPrivateKey string, repoPath string) error
Checkout(repoPath string, sha string) (string, error)
CommitSHA(repoPath string) (string, error)
Reset(repoPath string) error
}
// NativeGitClient implements Client interface using git CLI
type NativeGitClient struct {
rootDirectoryPath string
}
// CloneOrFetch either clone or fetch repository into specified directory path.
func (m *NativeGitClient) CloneOrFetch(repo string, username string, password string, sshPrivateKey string, repoPath string) error {
var needClone bool
if _, err := os.Stat(repoPath); os.IsNotExist(err) {
needClone = true
} else {
cmd := exec.Command("git", "status")
cmd.Dir = repoPath
_, err = cmd.Output()
needClone = err != nil
}
repoURL, env, err := GetGitCommandEnvAndURL(repo, username, password, sshPrivateKey)
if err != nil {
return err
}
if needClone {
_, err := exec.Command("rm", "-rf", repoPath).Output()
if err != nil {
return fmt.Errorf("unable to clean repo cache at %s: %v", repoPath, err)
}
log.Infof("Cloning %s to %s", repo, repoPath)
cmd := exec.Command("git", "clone", repoURL, repoPath)
cmd.Env = env
_, err = cmd.Output()
if err != nil {
return fmt.Errorf("unable to clone repository %s: %v", repo, err)
}
} else {
log.Infof("Fetching %s", repo)
// Fetch remote changes and delete all local branches
cmd := exec.Command("sh", "-c", "git fetch --all && git checkout --detach HEAD")
cmd.Env = env
cmd.Dir = repoPath
_, err := cmd.Output()
if err != nil {
return fmt.Errorf("unable to fetch repo %s: %v", repoPath, err)
}
cmd = exec.Command("sh", "-c", "for i in $(git branch --merged | grep -v \\*); do git branch -D $i; done")
cmd.Dir = repoPath
_, err = cmd.Output()
if err != nil {
return fmt.Errorf("unable to delete local branches for %s: %v", repoPath, err)
}
}
return nil
}
// Reset resets local changes
func (m *NativeGitClient) Reset(repoPath string) error {
cmd := exec.Command("sh", "-c", "git reset --hard HEAD && git clean -f")
cmd.Dir = repoPath
_, err := cmd.Output()
if err != nil {
return fmt.Errorf("unable to reset repository %s: %v", repoPath, err)
}
return nil
}
// Checkout checkout specified git sha
func (m *NativeGitClient) Checkout(repoPath string, sha string) (string, error) {
if sha == "" {
sha = "origin/HEAD"
}
checkoutCmd := exec.Command("git", "checkout", sha)
checkoutCmd.Dir = repoPath
_, err := checkoutCmd.Output()
if err != nil {
return "", fmt.Errorf("unable to checkout revision %s: %v", sha, err)
}
return m.CommitSHA(repoPath)
}
// CommitSHA returns current commit sha from `git rev-parse HEAD`
func (m *NativeGitClient) CommitSHA(repoPath string) (string, error) {
revisionCmd := exec.Command("git", "rev-parse", "HEAD")
revisionCmd.Dir = repoPath
output, err := revisionCmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
// NewNativeGitClient creates new instance of NativeGitClient
func NewNativeGitClient() (Client, error) {
rootDirPath, err := ioutil.TempDir("", "argo-git")
if err != nil {
return nil, err
}
return &NativeGitClient{
rootDirectoryPath: rootDirPath,
}, nil
}