Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/test/pkg/integration/git-client.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package integration
6
7
import (
8
"fmt"
9
"strings"
10
11
agent "github.com/gitpod-io/gitpod/test/pkg/agent/workspace/api"
12
)
13
14
type GitClient struct {
15
rpcClient *RpcClient
16
}
17
18
func Git(rsa *RpcClient) GitClient {
19
return GitClient{rsa}
20
}
21
22
func (g GitClient) GetBranch(workspaceRoot string, ignoreError bool) (string, error) {
23
var resp agent.ExecResponse
24
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
25
Dir: workspaceRoot,
26
Command: "git",
27
Args: []string{"rev-parse", "--abbrev-ref", "HEAD"},
28
}, &resp)
29
if !ignoreError {
30
if err != nil {
31
return "", fmt.Errorf("getBranch error: %w", err)
32
}
33
if resp.ExitCode != 0 {
34
return "", fmt.Errorf("getBranch returned rc: %d err: %v", resp.ExitCode, resp.Stderr)
35
}
36
}
37
return strings.Trim(resp.Stdout, " \t\n"), nil
38
}
39
40
func (g GitClient) Add(dir string, files ...string) error {
41
args := []string{"add"}
42
if files == nil {
43
args = append(args, ".")
44
} else {
45
args = append(args, files...)
46
}
47
var resp agent.ExecResponse
48
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
49
Dir: dir,
50
Command: "git",
51
Args: args,
52
}, &resp)
53
if err != nil {
54
return err
55
}
56
if resp.ExitCode != 0 {
57
return fmt.Errorf("add returned rc: %d err: %v", resp.ExitCode, resp.Stderr)
58
}
59
return nil
60
}
61
62
func (g GitClient) ConfigSafeDirectory() error {
63
var resp agent.ExecResponse
64
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
65
Command: "git",
66
Args: []string{"config", "--global", "--add", "safe.directory", "'*'"},
67
}, &resp)
68
if err != nil {
69
return err
70
}
71
if resp.ExitCode != 0 {
72
return fmt.Errorf("config returned rc: %d err: %v", resp.ExitCode, resp.Stderr)
73
}
74
return nil
75
}
76
77
func (g GitClient) ConfigUserName(dir string, username string) error {
78
var userName string
79
if username == "" {
80
userName = "integration-test"
81
} else {
82
userName = username
83
}
84
args := []string{"config", "--local", "user.name", userName}
85
var resp agent.ExecResponse
86
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
87
Dir: dir,
88
Command: "git",
89
Args: args,
90
}, &resp)
91
if err != nil {
92
return err
93
}
94
if resp.ExitCode != 0 {
95
return fmt.Errorf("config user name returned rc: %d err: %v", resp.ExitCode, resp.Stderr)
96
}
97
return nil
98
}
99
100
func (g GitClient) ConfigUserEmail(dir string, files ...string) error {
101
args := []string{"config", "--local", "user.email", "[email protected]"}
102
var resp agent.ExecResponse
103
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
104
Dir: dir,
105
Command: "git",
106
Args: args,
107
}, &resp)
108
if err != nil {
109
return err
110
}
111
if resp.ExitCode != 0 {
112
return fmt.Errorf("config user email returned rc: %d err: %v", resp.ExitCode, resp.Stderr)
113
}
114
return nil
115
}
116
117
func (g GitClient) Commit(dir string, message string, all bool, moreArgs ...string) error {
118
args := []string{"commit", "-m", message}
119
if all {
120
args = append(args, "--all")
121
}
122
if moreArgs != nil {
123
args = append(args, moreArgs...)
124
}
125
var resp agent.ExecResponse
126
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
127
Dir: dir,
128
Command: "git",
129
Args: args,
130
}, &resp)
131
if err != nil {
132
return err
133
}
134
if resp.ExitCode != 0 {
135
return fmt.Errorf("commit returned rc: %d, out: %v, err: %v", resp.ExitCode, resp.Stdout, resp.Stderr)
136
}
137
return nil
138
}
139
140
func (g GitClient) Push(dir string, force bool, moreArgs ...string) error {
141
args := []string{"push"}
142
if moreArgs != nil {
143
args = append(args, moreArgs...)
144
}
145
if force {
146
args = append(args, "--force")
147
}
148
var resp agent.ExecResponse
149
err := g.rpcClient.Call("WorkspaceAgent.Exec", &agent.ExecRequest{
150
Dir: dir,
151
Command: "git",
152
Args: args,
153
}, &resp)
154
if err != nil {
155
return err
156
}
157
if resp.ExitCode != 0 {
158
return fmt.Errorf("push returned rc: %d err: %v", resp.ExitCode, resp.Stderr)
159
}
160
return nil
161
}
162
163