-
Notifications
You must be signed in to change notification settings - Fork 251
/
writer.go
118 lines (104 loc) · 3.49 KB
/
writer.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
package git
import (
"fmt"
"strings"
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
)
// CommitOptions holds options for a git commit operation
type CommitOptions struct {
// CommitMessageText holds a short commit message (-m option)
CommitMessageText string
// CommitMessagePath holds the path to a file to be used for the commit message (-F option)
CommitMessagePath string
// SigningKey holds a GnuPG key ID used to sign the commit with (-S option)
SigningKey string
// SignOff specifies whether to sign-off a commit (-s option)
SignOff bool
}
// Commit perfoms a git commit for the given pathSpec to the currently checked
// out branch. If pathSpec is empty, or the special value "*", all pending
// changes will be commited. If message is not the empty string, it will be
// used as the commit message, otherwise a default commit message will be used.
// If signingKey is not the empty string, commit will be signed with the given
// GPG key.
func (m *nativeGitClient) Commit(pathSpec string, opts *CommitOptions) error {
defaultCommitMsg := "Update parameters"
args := []string{"commit"}
if pathSpec == "" || pathSpec == "*" {
args = append(args, "-a")
}
if opts.SigningKey != "" {
args = append(args, "-S", opts.SigningKey)
}
if opts.SignOff {
args = append(args, "-s")
}
if opts.CommitMessageText != "" {
args = append(args, "-m", opts.CommitMessageText)
} else if opts.CommitMessagePath != "" {
args = append(args, "-F", opts.CommitMessagePath)
} else {
args = append(args, "-m", defaultCommitMsg)
}
out, err := m.runCmd(args...)
if err != nil {
log.Errorf(out)
return err
}
return nil
}
// Branch creates a new target branch from a given source branch
func (m *nativeGitClient) Branch(sourceBranch string, targetBranch string) error {
if sourceBranch != "" {
_, err := m.runCmd("checkout", sourceBranch)
if err != nil {
return fmt.Errorf("could not checkout source branch: %v", err)
}
}
_, err := m.runCmd("branch", targetBranch)
if err != nil {
return fmt.Errorf("could not create new branch: %v", err)
}
return nil
}
// Push pushes local changes to the remote branch. If force is true, will force
// the remote to accept the push.
func (m *nativeGitClient) Push(remote string, branch string, force bool) error {
args := []string{"push"}
if force {
args = append(args, "-f")
}
args = append(args, remote, branch)
err := m.runCredentialedCmd("git", args...)
if err != nil {
return fmt.Errorf("could not push %s to %s: %v", branch, remote, err)
}
return nil
}
// Add adds a path spec to the repository
func (m *nativeGitClient) Add(path string) error {
return m.runCredentialedCmd("git", "add", path)
}
// SymRefToBranch retrieves the branch name a symbolic ref points to
func (m *nativeGitClient) SymRefToBranch(symRef string) (string, error) {
output, err := m.runCmd("symbolic-ref", symRef)
if err != nil {
return "", fmt.Errorf("could not resolve symbolic ref '%s': %v", symRef, err)
}
if a := strings.SplitN(output, "refs/heads/", 2); len(a) == 2 {
return a[1], nil
}
return "", fmt.Errorf("no symbolic ref named '%s' could be found", symRef)
}
// Config configures username and email address for the repository
func (m *nativeGitClient) Config(username string, email string) error {
_, err := m.runCmd("config", "user.name", username)
if err != nil {
return fmt.Errorf("could not set git username: %v", err)
}
_, err = m.runCmd("config", "user.email", email)
if err != nil {
return fmt.Errorf("could not set git email: %v", err)
}
return nil
}