-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathgit.go
104 lines (90 loc) · 3.27 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
// Copyright The Helm Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package git
import (
"fmt"
"os"
"os/exec"
"strings"
)
type Git struct{}
// AddWorktree creates a new Git worktree with a detached HEAD for the given committish and returns its path.
func (g *Git) AddWorktree(workingDir string, committish string) (string, error) {
dir, err := os.MkdirTemp("", "chart-releaser-")
if err != nil {
return "", err
}
command := exec.Command("git", "worktree", "add", "--detach", dir, committish)
if err := runCommand(workingDir, command); err != nil {
return "", err
}
return dir, nil
}
// RemoveWorktree removes the Git worktree with the given path.
func (g *Git) RemoveWorktree(workingDir string, path string) error {
command := exec.Command("git", "worktree", "remove", path, "--force")
return runCommand(workingDir, command)
}
// Add runs 'git add' with the given args.
func (g *Git) Add(workingDir string, args ...string) error {
if len(args) == 0 {
return fmt.Errorf("no args specified")
}
addArgs := []string{"add"}
addArgs = append(addArgs, args...)
command := exec.Command("git", addArgs...)
return runCommand(workingDir, command)
}
// Commit runs 'git commit' with the given message. the commit is signed off.
func (g *Git) Commit(workingDir string, message string) error {
command := exec.Command("git", "commit", "--message", message, "--signoff")
return runCommand(workingDir, command)
}
// UpdateBranch runs 'git pull' with the given args.
func (g *Git) Pull(workingDir string, args ...string) error {
pullArgs := []string{"pull"}
pullArgs = append(pullArgs, args...)
command := exec.Command("git", pullArgs...)
return runCommand(workingDir, command)
}
// Push runs 'git push' with the given args.
func (g *Git) Push(workingDir string, args ...string) error {
pushArgs := []string{"push"}
pushArgs = append(pushArgs, args...)
command := exec.Command("git", pushArgs...)
return runCommand(workingDir, command)
}
// GetPushURL returns the push url with a token inserted
func (g *Git) GetPushURL(remote string, token string) (string, error) {
pushURL, err := exec.Command("git", "remote", "get-url", "--push", remote).Output()
if err != nil {
return "", err
}
pushURLStr := string(pushURL)
found := false
if pushURLStr, found = strings.CutPrefix(pushURLStr, "git@"); found {
pushURLStr = strings.ReplaceAll(pushURLStr, ":", "/")
pushURLStr = strings.TrimSuffix(pushURLStr, ".git\n")
} else {
pushURLStr = strings.TrimPrefix(pushURLStr, "https://")
}
pushURLWithToken := fmt.Sprintf("https://x-access-token:%s@%s", token, strings.Trim(pushURLStr, "\n"))
return pushURLWithToken, nil
}
func runCommand(workingDir string, command *exec.Cmd) error {
command.Dir = workingDir
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()
}