Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 1 | // 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. |
| 4 | package git |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "fmt" |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 10 | "path/filepath" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 11 | "regexp" |
| 12 | "strings" |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 13 | |
| 14 | "go.chromium.org/chromiumos/infra/go/internal/cmd" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | var ( |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 18 | CommandRunnerImpl cmd.CommandRunner = cmd.RealCommandRunner{} |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 19 | ) |
| 20 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 21 | type CommandOutput struct { |
| 22 | Stdout string |
| 23 | Stderr string |
| 24 | } |
| 25 | |
| 26 | // Struct representing a remote ref. |
| 27 | type 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. |
| 34 | func RunGit(gitRepo string, cmd []string) (CommandOutput, error) { |
| 35 | ctx := context.Background() |
| 36 | var stdoutBuf, stderrBuf bytes.Buffer |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 37 | err := CommandRunnerImpl.RunCommand(ctx, &stdoutBuf, &stderrBuf, gitRepo, "git", cmd...) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 38 | cmdOutput := CommandOutput{stdoutBuf.String(), stderrBuf.String()} |
| 39 | return cmdOutput, err |
| 40 | } |
| 41 | |
Jack Neus | 2bcf4a6 | 2019-07-25 10:35:05 -0600 | [diff] [blame] | 42 | // RunGitIgnore the specified git command in the specified repo and returns |
| 43 | // only an error, not the command output. |
| 44 | func RunGitIgnoreOutput(gitRepo string, cmd []string) error { |
| 45 | _, err := RunGit(gitRepo, cmd) |
| 46 | return err |
| 47 | } |
| 48 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 49 | // GetCurrentBranch returns current branch of a repo, and an empty string |
| 50 | // if repo is on detached HEAD. |
| 51 | func 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. |
| 61 | func MatchBranchName(gitRepo string, pattern *regexp.Regexp) ([]string, error) { |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 62 | // 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. |
| 71 | func MatchBranchNameWithNamespace(gitRepo string, pattern, namespace *regexp.Regexp) ([]string, error) { |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 72 | // Regex should be case insensitive. |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 73 | namespace = regexp.MustCompile("(?i)^" + namespace.String()) |
| 74 | pattern = regexp.MustCompile("(?i)" + pattern.String()) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 75 | |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 76 | output, err := RunGit(gitRepo, []string{"show-ref"}) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 77 | if err != nil { |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 78 | if strings.Contains(err.Error(), "exit status 1") { |
| 79 | // Not a fatal error, just no branches. |
| 80 | return []string{}, nil |
| 81 | } |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 82 | // Could not read branches. |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 83 | return []string{}, fmt.Errorf("git error: %s\nstdout: %s stderr: %s", err.Error(), output.Stdout, output.Stderr) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 84 | } |
| 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 Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 94 | |
| 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 Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 101 | 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 Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 109 | func GetGitRepoRevision(cwd, branch string) (string, error) { |
| 110 | if branch == "" { |
| 111 | branch = "HEAD" |
| 112 | } |
| 113 | output, err := RunGit(cwd, []string{"rev-parse", branch}) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 114 | return strings.TrimSpace(output.Stdout), err |
| 115 | } |
| 116 | |
Jack Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 117 | // IsReachable determines whether one commit ref is reachable from another. |
| 118 | func 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 Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 129 | // StripRefsHead removes leading 'refs/heads/' from a ref name. |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 130 | func StripRefsHead(ref string) string { |
| 131 | return strings.TrimPrefix(ref, "refs/heads/") |
| 132 | } |
| 133 | |
| 134 | // NormalizeRef converts git branch refs into fully qualified form. |
| 135 | func 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. |
| 143 | func 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 Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 155 | |
| 156 | // CreateBranch creates a branch. |
| 157 | func CreateBranch(gitRepo, branch string) error { |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 158 | output, err := RunGit(gitRepo, []string{"checkout", "-B", branch}) |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 159 | 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 Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 165 | } |
| 166 | return err |
| 167 | } |
| 168 | |
| 169 | // CreateTrackingBranch creates a tracking branch. |
| 170 | func 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 Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 184 | return err |
| 185 | } |
| 186 | |
| 187 | // CommitAll adds all local changes and commits them. |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 188 | // Returns the sha1 of the commit. |
| 189 | func CommitAll(gitRepo, commitMsg string) (string, error) { |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 190 | if output, err := RunGit(gitRepo, []string{"add", "-A"}); err != nil { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 191 | return "", fmt.Errorf(output.Stderr) |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 192 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 193 | if output, err := RunGit(gitRepo, []string{"commit", "-m", commitMsg}); err != nil { |
| 194 | if strings.Contains(output.Stdout, "nothing to commit") { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 195 | return "", fmt.Errorf(output.Stdout) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 196 | } else { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 197 | return "", fmt.Errorf(output.Stderr) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 198 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 199 | } |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 200 | output, err := RunGit(gitRepo, []string{"rev-parse", "HEAD"}) |
| 201 | if err != nil { |
| 202 | return "", err |
| 203 | } |
| 204 | return strings.TrimSpace(output.Stdout), nil |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 205 | } |
| 206 | |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 207 | // CommitEmpty makes an empty commit (assuming nothing is staged). |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 208 | // Returns the sha1 of the commit. |
| 209 | func CommitEmpty(gitRepo, commitMsg string) (string, error) { |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 210 | if output, err := RunGit(gitRepo, []string{"commit", "-m", commitMsg, "--allow-empty"}); err != nil { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 211 | return "", fmt.Errorf(output.Stderr) |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 212 | } |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 213 | output, err := RunGit(gitRepo, []string{"rev-parse", "HEAD"}) |
| 214 | if err != nil { |
| 215 | return "", nil |
| 216 | } |
| 217 | return strings.TrimSpace(output.Stdout), nil |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 218 | } |
| 219 | |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 220 | // PushChanges stages and commits any local changes before pushing the commit |
| 221 | // to the specified remote ref. Returns the sha1 of the commit. |
| 222 | func PushChanges(gitRepo, localRef, commitMsg string, dryRun bool, pushTo RemoteRef) (string, error) { |
| 223 | commit, err := CommitAll(gitRepo, commitMsg) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 224 | // 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 Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 226 | return "", err |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 227 | } |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 228 | return commit, PushRef(gitRepo, localRef, dryRun, pushTo) |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 229 | } |
| 230 | |
Jack Neus | eb25f72 | 2019-07-19 16:33:20 -0600 | [diff] [blame] | 231 | // PushRef pushes the specified local ref to the specified remote ref. |
| 232 | func PushRef(gitRepo, localRef string, dryRun bool, pushTo RemoteRef) error { |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 233 | 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 Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 238 | _, err := RunGit(gitRepo, cmd) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 239 | return err |
| 240 | } |
| 241 | |
| 242 | // Init initializes a repo. |
| 243 | func Init(gitRepo string, bare bool) error { |
| 244 | cmd := []string{"init"} |
| 245 | if bare { |
| 246 | cmd = append(cmd, "--bare") |
| 247 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 248 | _, err := RunGit(gitRepo, cmd) |
| 249 | return err |
| 250 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 251 | |
| 252 | // AddRemote adds a remote. |
| 253 | func AddRemote(gitRepo, remote, remoteLocation string) error { |
| 254 | output, err := RunGit(gitRepo, []string{"remote", "add", remote, remoteLocation}) |
| 255 | if err != nil { |
Jack Neus | eb25f72 | 2019-07-19 16:33:20 -0600 | [diff] [blame] | 256 | if strings.Contains(output.Stderr, "already exists") { |
| 257 | return fmt.Errorf("remote already exists") |
| 258 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 259 | } |
| 260 | return err |
| 261 | } |
| 262 | |
| 263 | // Checkout checkouts a branch. |
| 264 | func 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. |
| 273 | func 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. |
| 295 | func 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 | } |