-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathCmdGitClient.go
More file actions
220 lines (174 loc) · 5.26 KB
/
Copy pathCmdGitClient.go
File metadata and controls
220 lines (174 loc) · 5.26 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package db_lib
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/semaphoreui/semaphore/pkg/ssh"
"github.com/semaphoreui/semaphore/db"
"github.com/semaphoreui/semaphore/util"
log "github.com/sirupsen/logrus"
)
type CmdGitClient struct {
keyInstaller AccessKeyInstaller
}
func (c CmdGitClient) makeCmd(
r GitRepository,
targetDir GitRepositoryDirType,
installation ssh.AccessKeyInstallation,
args ...string,
) *exec.Cmd {
cmd := exec.Command("git") //nolint: gas
cmd.Env = append(getEnvironmentVars(), installation.GetGitEnv()...)
switch targetDir {
case GitRepositoryTmpPath:
cmd.Dir = util.Config.GetProjectTmpDir(r.Repository.ProjectID)
_, err := os.Stat(cmd.Dir)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(cmd.Dir, 0755)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"context": "git",
}).Error("failed to create project temp directory")
}
} else {
log.WithError(err).WithFields(log.Fields{
"context": "git",
}).Error("failed to check existing project temp directory")
}
}
case GitRepositoryFullPath:
cmd.Dir = r.GetFullPath()
default:
panic("unknown Repository directory type")
}
cmd.Args = append(cmd.Args, args...)
cmd.SysProcAttr = util.Config.GetSysProcAttr()
return cmd
}
func (c CmdGitClient) run(r GitRepository, targetDir GitRepositoryDirType, args ...string) error {
var err error
keyInstallation, err := c.keyInstaller.Install(r.Repository.SSHKey, db.AccessKeyRoleGit, r.Logger)
if err != nil {
return err
}
defer keyInstallation.Destroy() //nolint: errcheck
cmd := c.makeCmd(r, targetDir, keyInstallation, args...)
r.Logger.LogCmd(cmd)
return cmd.Run()
}
func (c CmdGitClient) output(r GitRepository, targetDir GitRepositoryDirType, args ...string) (out string, err error) {
keyInstallation, err := c.keyInstaller.Install(r.Repository.SSHKey, db.AccessKeyRoleGit, r.Logger)
if err != nil {
return
}
defer keyInstallation.Destroy() //nolint: errcheck
bytes, err := c.makeCmd(r, targetDir, keyInstallation, args...).Output()
if err != nil {
return
}
out = strings.Trim(string(bytes), " \n")
return
}
func (c CmdGitClient) Clone(r GitRepository) error {
r.Logger.Log("Cloning Repository " + r.Repository.GitURL)
var dirName string
if r.TmpDirName == "" {
dirName = r.Repository.GetDirName(r.TemplateID)
} else {
dirName = r.TmpDirName
}
targetPath := r.GetFullPath()
if err := os.MkdirAll(targetPath, 0755); err != nil {
return err
}
if err := util.ChownDir(targetPath); err != nil {
return err
}
return c.run(r, GitRepositoryTmpPath,
"clone",
"--recursive",
"--branch",
r.Repository.GitBranch,
"--end-of-options",
r.Repository.GetGitURL(false),
dirName)
}
func (c CmdGitClient) Pull(r GitRepository) error {
r.Logger.Log("Updating Repository " + r.Repository.GitURL)
err := c.run(r, GitRepositoryFullPath, "pull", "origin", "--end-of-options", r.Repository.GitBranch)
if err != nil {
return err
}
return c.run(r, GitRepositoryFullPath, "submodule", "update", "--init", "--recursive")
}
func (c CmdGitClient) Checkout(r GitRepository, target string) error {
r.Logger.Log("Checkout repository to " + target)
return c.run(r, GitRepositoryFullPath, "checkout", target)
}
func (c CmdGitClient) CanBePulled(r GitRepository) bool {
err := c.run(r, GitRepositoryFullPath, "fetch")
if err != nil {
return false
}
err = c.run(r, GitRepositoryFullPath,
"merge-base", "--is-ancestor", "HEAD", "origin/"+r.Repository.GitBranch)
return err == nil
}
func (c CmdGitClient) GetLastCommitMessage(r GitRepository) (msg string, err error) {
r.Logger.Log("Get current commit message")
msg, err = c.output(r, GitRepositoryFullPath, "show-branch", "--no-name", "HEAD")
if err != nil {
return
}
return
}
func (c CmdGitClient) GetLastCommitHash(r GitRepository) (hash string, err error) {
r.Logger.Log("Get current commit hash")
hash, err = c.output(r, GitRepositoryFullPath, "rev-parse", "HEAD")
return
}
func (c CmdGitClient) GetLastRemoteCommitHash(r GitRepository) (hash string, err error) {
out, err := c.output(r, GitRepositoryTmpPath, "ls-remote", "--end-of-options", r.Repository.GetGitURL(false), r.Repository.GitBranch)
if err != nil {
return
}
firstSpaceIndex := strings.IndexAny(out, "\t ")
if firstSpaceIndex == -1 {
err = fmt.Errorf("can't retreave remote commit hash")
}
if err != nil {
return
}
hash = out[0:firstSpaceIndex]
return
}
func (c CmdGitClient) GetRemoteBranches(r GitRepository) ([]string, error) {
out, err := c.output(r, GitRepositoryTmpPath, "ls-remote", "--heads", "--end-of-options", r.Repository.GetGitURL(false))
if err != nil {
return nil, err
}
if len(out) == 0 {
return []string{}, nil
}
branches := strings.Split(out, "\n")
branchNames := getRepositoryBranchNames(branches)
return branchNames, nil
}
func getRepositoryBranchNames(branches []string) []string {
branchNames := make([]string, 0, len(branches))
for _, branch := range branches {
parts := strings.Split(branch, "\t")
if len(parts) < 2 {
continue
}
refPath := strings.TrimSpace(parts[1])
const refsHeadsPrefix = "refs/heads/"
if strings.HasPrefix(refPath, refsHeadsPrefix) {
branchName := strings.TrimPrefix(refPath, refsHeadsPrefix)
branchNames = append(branchNames, branchName)
}
}
return branchNames
}