blob: 0c03d970ea109e5a365b628128de8dc8885f6c26 [file] [log] [blame]
Jack Neusfc3b5772019-07-03 11:18:42 -06001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4package git
5
6import (
7 "bytes"
8 "context"
9 "fmt"
Jack Neus0296c732019-07-17 09:35:01 -060010 "path/filepath"
Jack Neusfc3b5772019-07-03 11:18:42 -060011 "regexp"
12 "strings"
Jack Neus07511722019-07-12 15:41:10 -060013
14 "go.chromium.org/chromiumos/infra/go/internal/cmd"
Jack Neusfc3b5772019-07-03 11:18:42 -060015)
16
17var (
Jack Neus07511722019-07-12 15:41:10 -060018 CommandRunnerImpl cmd.CommandRunner = cmd.RealCommandRunner{}
Jack Neusfc3b5772019-07-03 11:18:42 -060019)
20
Jack Neusfc3b5772019-07-03 11:18:42 -060021type CommandOutput struct {
22 Stdout string
23 Stderr string
24}
25
26// Struct representing a remote ref.
27type RemoteRef struct {
28 Remote string
29 Ref string
30}
31
32// RunGit the specified git command in the specified repo. It returns
33// stdout and stderr.
34func RunGit(gitRepo string, cmd []string) (CommandOutput, error) {
35 ctx := context.Background()
36 var stdoutBuf, stderrBuf bytes.Buffer
Jack Neus07511722019-07-12 15:41:10 -060037 err := CommandRunnerImpl.RunCommand(ctx, &stdoutBuf, &stderrBuf, gitRepo, "git", cmd...)
Jack Neusfc3b5772019-07-03 11:18:42 -060038 cmdOutput := CommandOutput{stdoutBuf.String(), stderrBuf.String()}
39 return cmdOutput, err
40}
41
Jack Neus2bcf4a62019-07-25 10:35:05 -060042// RunGitIgnore the specified git command in the specified repo and returns
43// only an error, not the command output.
44func RunGitIgnoreOutput(gitRepo string, cmd []string) error {
45 _, err := RunGit(gitRepo, cmd)
46 return err
47}
48
Jack Neusfc3b5772019-07-03 11:18:42 -060049// GetCurrentBranch returns current branch of a repo, and an empty string
50// if repo is on detached HEAD.
51func GetCurrentBranch(cwd string) string {
52 output, err := RunGit(cwd, []string{"symbolic-ref", "-q", "HEAD"})
53 if err != nil {
54 return ""
55 }
56 return StripRefsHead(strings.TrimSpace(output.Stdout))
57}
58
59// MatchBranchName returns the names of branches who match the specified
60// regular expression.
61func MatchBranchName(gitRepo string, pattern *regexp.Regexp) ([]string, error) {
Jack Neus10e6a122019-07-18 10:17:44 -060062 // MatchBranchWithNamespace trims the namespace off the branches it returns.
63 // Here, we need a namespace that matches every string but doesn't match any character
64 // (so that nothing is trimmed).
65 nullNamespace := regexp.MustCompile("")
66 return MatchBranchNameWithNamespace(gitRepo, pattern, nullNamespace)
67}
68
69// MatchBranchNameWithNamespace returns the names of branches who match the specified
70// pattern and start with the specified namespace.
71func MatchBranchNameWithNamespace(gitRepo string, pattern, namespace *regexp.Regexp) ([]string, error) {
Jack Neusfc3b5772019-07-03 11:18:42 -060072 // Regex should be case insensitive.
Jack Neus10e6a122019-07-18 10:17:44 -060073 namespace = regexp.MustCompile("(?i)^" + namespace.String())
74 pattern = regexp.MustCompile("(?i)" + pattern.String())
Jack Neusfc3b5772019-07-03 11:18:42 -060075
Jack Neus901a6bf2019-07-22 08:30:07 -060076 output, err := RunGit(gitRepo, []string{"show-ref"})
Jack Neusfc3b5772019-07-03 11:18:42 -060077 if err != nil {
Jack Neus901a6bf2019-07-22 08:30:07 -060078 if strings.Contains(err.Error(), "exit status 1") {
79 // Not a fatal error, just no branches.
80 return []string{}, nil
81 }
Jack Neusfc3b5772019-07-03 11:18:42 -060082 // Could not read branches.
Jack Neus901a6bf2019-07-22 08:30:07 -060083 return []string{}, fmt.Errorf("git error: %s\nstdout: %s stderr: %s", err.Error(), output.Stdout, output.Stderr)
Jack Neusfc3b5772019-07-03 11:18:42 -060084 }
85 // Find all branches that match the pattern.
86 branches := strings.Split(output.Stdout, "\n")
87 matchedBranches := []string{}
88 for _, branch := range branches {
89 branch = strings.TrimSpace(branch)
90 if branch == "" {
91 continue
92 }
93 branch = strings.Fields(branch)[1]
Jack Neus10e6a122019-07-18 10:17:44 -060094
95 // Only look at branches which match the namespace.
96 if !namespace.Match([]byte(branch)) {
97 continue
98 }
99 branch = namespace.ReplaceAllString(branch, "")
100
Jack Neusfc3b5772019-07-03 11:18:42 -0600101 if pattern.Match([]byte(branch)) {
102 matchedBranches = append(matchedBranches, branch)
103 }
104 }
105 return matchedBranches, nil
106}
107
108// GetGitRepoRevision finds and returns the revision of a branch.
Jack Neusa7287522019-07-23 16:36:18 -0600109func GetGitRepoRevision(cwd, branch string) (string, error) {
110 if branch == "" {
111 branch = "HEAD"
112 }
113 output, err := RunGit(cwd, []string{"rev-parse", branch})
Jack Neusfc3b5772019-07-03 11:18:42 -0600114 return strings.TrimSpace(output.Stdout), err
115}
116
Jack Neusa7287522019-07-23 16:36:18 -0600117// IsReachable determines whether one commit ref is reachable from another.
118func IsReachable(cwd, to_ref, from_ref string) (bool, error) {
119 _, err := RunGit(cwd, []string{"merge-base", "--is-ancestor", to_ref, from_ref})
120 if err != nil {
121 if strings.Contains(err.Error(), "exit status 1") {
122 return false, nil
123 }
124 return false, err
125 }
126 return true, nil
127}
128
Jack Neus7440c762019-07-22 10:45:18 -0600129// StripRefsHead removes leading 'refs/heads/' from a ref name.
Jack Neusfc3b5772019-07-03 11:18:42 -0600130func StripRefsHead(ref string) string {
131 return strings.TrimPrefix(ref, "refs/heads/")
132}
133
134// NormalizeRef converts git branch refs into fully qualified form.
135func NormalizeRef(ref string) string {
136 if ref == "" || strings.HasPrefix(ref, "refs/") {
137 return ref
138 }
139 return fmt.Sprintf("refs/heads/%s", ref)
140}
141
142// StripRefs removes leading 'refs/heads/', 'refs/remotes/[^/]+/' from a ref name.
143func StripRefs(ref string) string {
144 ref = StripRefsHead(ref)
145 // If the ref starts with ref/remotes/, then we want the part of the string
146 // that comes after the third "/".
147 // Example: refs/remotes/origin/master --> master
148 // Example: refs/remotse/origin/foo/bar --> foo/bar
149 if strings.HasPrefix(ref, "refs/remotes/") {
150 refParts := strings.SplitN(ref, "/", 4)
151 return refParts[len(refParts)-1]
152 }
153 return ref
154}
Jack Neusabdbe192019-07-15 12:23:22 -0600155
156// CreateBranch creates a branch.
157func CreateBranch(gitRepo, branch string) error {
Jack Neus0296c732019-07-17 09:35:01 -0600158 output, err := RunGit(gitRepo, []string{"checkout", "-B", branch})
Jack Neus10e6a122019-07-18 10:17:44 -0600159 if err != nil {
160 if strings.Contains(output.Stderr, "not a valid branch name") {
161 return fmt.Errorf("%s is not a valid branch name", branch)
162 } else {
163 return fmt.Errorf(output.Stderr)
164 }
Jack Neus0296c732019-07-17 09:35:01 -0600165 }
166 return err
167}
168
169// CreateTrackingBranch creates a tracking branch.
170func CreateTrackingBranch(gitRepo, branch string, remoteRef RemoteRef) error {
171 refspec := fmt.Sprintf("%s/%s", remoteRef.Remote, remoteRef.Ref)
172 output, err := RunGit(gitRepo, []string{"fetch", remoteRef.Remote, remoteRef.Ref})
173 if err != nil {
174 return fmt.Errorf("could not fetch %s: %s", refspec, output.Stderr)
175 }
176 output, err = RunGit(gitRepo, []string{"checkout", "-b", branch, "-t", refspec})
177 if err != nil {
178 if strings.Contains(output.Stderr, "not a valid branch name") {
179 return fmt.Errorf("%s is not a valid branch name", branch)
180 } else {
181 return fmt.Errorf(output.Stderr)
182 }
183 }
Jack Neusabdbe192019-07-15 12:23:22 -0600184 return err
185}
186
187// CommitAll adds all local changes and commits them.
Jack Neusf116fae2019-07-24 15:05:03 -0600188// Returns the sha1 of the commit.
189func CommitAll(gitRepo, commitMsg string) (string, error) {
Jack Neus0296c732019-07-17 09:35:01 -0600190 if output, err := RunGit(gitRepo, []string{"add", "-A"}); err != nil {
Jack Neusf116fae2019-07-24 15:05:03 -0600191 return "", fmt.Errorf(output.Stderr)
Jack Neusabdbe192019-07-15 12:23:22 -0600192 }
Jack Neus0296c732019-07-17 09:35:01 -0600193 if output, err := RunGit(gitRepo, []string{"commit", "-m", commitMsg}); err != nil {
194 if strings.Contains(output.Stdout, "nothing to commit") {
Jack Neusf116fae2019-07-24 15:05:03 -0600195 return "", fmt.Errorf(output.Stdout)
Jack Neus0296c732019-07-17 09:35:01 -0600196 } else {
Jack Neusf116fae2019-07-24 15:05:03 -0600197 return "", fmt.Errorf(output.Stderr)
Jack Neus0296c732019-07-17 09:35:01 -0600198 }
Jack Neusabdbe192019-07-15 12:23:22 -0600199 }
Jack Neusf116fae2019-07-24 15:05:03 -0600200 output, err := RunGit(gitRepo, []string{"rev-parse", "HEAD"})
201 if err != nil {
202 return "", err
203 }
204 return strings.TrimSpace(output.Stdout), nil
Jack Neusabdbe192019-07-15 12:23:22 -0600205}
206
Jack Neus7440c762019-07-22 10:45:18 -0600207// CommitEmpty makes an empty commit (assuming nothing is staged).
Jack Neusf116fae2019-07-24 15:05:03 -0600208// Returns the sha1 of the commit.
209func CommitEmpty(gitRepo, commitMsg string) (string, error) {
Jack Neus7440c762019-07-22 10:45:18 -0600210 if output, err := RunGit(gitRepo, []string{"commit", "-m", commitMsg, "--allow-empty"}); err != nil {
Jack Neusf116fae2019-07-24 15:05:03 -0600211 return "", fmt.Errorf(output.Stderr)
Jack Neus7440c762019-07-22 10:45:18 -0600212 }
Jack Neusf116fae2019-07-24 15:05:03 -0600213 output, err := RunGit(gitRepo, []string{"rev-parse", "HEAD"})
214 if err != nil {
215 return "", nil
216 }
217 return strings.TrimSpace(output.Stdout), nil
Jack Neus7440c762019-07-22 10:45:18 -0600218}
219
Jack Neusf116fae2019-07-24 15:05:03 -0600220// PushChanges stages and commits any local changes before pushing the commit
221// to the specified remote ref. Returns the sha1 of the commit.
222func PushChanges(gitRepo, localRef, commitMsg string, dryRun bool, pushTo RemoteRef) (string, error) {
223 commit, err := CommitAll(gitRepo, commitMsg)
Jack Neus0296c732019-07-17 09:35:01 -0600224 // It's ok if there's nothing to commit, we can still try to push.
225 if err != nil && !strings.Contains(err.Error(), "nothing to commit") {
Jack Neusf116fae2019-07-24 15:05:03 -0600226 return "", err
Jack Neusabdbe192019-07-15 12:23:22 -0600227 }
Jack Neusf116fae2019-07-24 15:05:03 -0600228 return commit, PushRef(gitRepo, localRef, dryRun, pushTo)
Jack Neus7440c762019-07-22 10:45:18 -0600229}
230
Jack Neuseb25f722019-07-19 16:33:20 -0600231// PushRef pushes the specified local ref to the specified remote ref.
232func PushRef(gitRepo, localRef string, dryRun bool, pushTo RemoteRef) error {
Jack Neusabdbe192019-07-15 12:23:22 -0600233 ref := fmt.Sprintf("%s:%s", localRef, pushTo.Ref)
234 cmd := []string{"push", pushTo.Remote, ref}
235 if dryRun {
236 cmd = append(cmd, "--dry-run")
237 }
Jack Neus7440c762019-07-22 10:45:18 -0600238 _, err := RunGit(gitRepo, cmd)
Jack Neus0296c732019-07-17 09:35:01 -0600239 return err
240}
241
242// Init initializes a repo.
243func Init(gitRepo string, bare bool) error {
244 cmd := []string{"init"}
245 if bare {
246 cmd = append(cmd, "--bare")
247 }
Jack Neusabdbe192019-07-15 12:23:22 -0600248 _, err := RunGit(gitRepo, cmd)
249 return err
250}
Jack Neus0296c732019-07-17 09:35:01 -0600251
252// AddRemote adds a remote.
253func AddRemote(gitRepo, remote, remoteLocation string) error {
254 output, err := RunGit(gitRepo, []string{"remote", "add", remote, remoteLocation})
255 if err != nil {
Jack Neuseb25f722019-07-19 16:33:20 -0600256 if strings.Contains(output.Stderr, "already exists") {
257 return fmt.Errorf("remote already exists")
258 }
Jack Neus0296c732019-07-17 09:35:01 -0600259 }
260 return err
261}
262
263// Checkout checkouts a branch.
264func Checkout(gitRepo, branch string) error {
265 output, err := RunGit(gitRepo, []string{"checkout", branch})
266 if err != nil && strings.Contains(output.Stderr, "did not match any") {
267 return fmt.Errorf(output.Stderr)
268 }
269 return err
270}
271
272// DeleteBranch checks out to master and then deletes the current branch.
273func DeleteBranch(gitRepo, branch string, force bool) error {
274 cmd := []string{"branch"}
275 if force {
276 cmd = append(cmd, "-D")
277 } else {
278 cmd = append(cmd, "-d")
279 }
280 cmd = append(cmd, branch)
281 output, err := RunGit(gitRepo, cmd)
282
283 if err != nil {
284 if strings.Contains(output.Stderr, "checked out at") {
285 return fmt.Errorf(output.Stderr)
286 }
287 if strings.Contains(output.Stderr, "not fully merged") {
288 return fmt.Errorf("branch %s is not fully merged. use the force parameter if you wish to proceed", branch)
289 }
290 }
291 return err
292}
293
294// Clone clones the remote into the specified dir.
295func Clone(remote, dir string) error {
296 output, err := RunGit(filepath.Dir(dir), []string{"clone", remote, filepath.Base(dir)})
297 if err != nil {
298 return fmt.Errorf(output.Stderr)
299 }
300 return nil
301}