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 | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 15 | "go.chromium.org/luci/common/errors" |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | var ( |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 19 | CommandRunnerImpl cmd.CommandRunner = cmd.RealCommandRunner{} |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 20 | ) |
| 21 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 22 | type CommandOutput struct { |
| 23 | Stdout string |
| 24 | Stderr string |
| 25 | } |
| 26 | |
| 27 | // Struct representing a remote ref. |
| 28 | type RemoteRef struct { |
| 29 | Remote string |
| 30 | Ref string |
| 31 | } |
| 32 | |
| 33 | // RunGit the specified git command in the specified repo. It returns |
| 34 | // stdout and stderr. |
| 35 | func RunGit(gitRepo string, cmd []string) (CommandOutput, error) { |
| 36 | ctx := context.Background() |
| 37 | var stdoutBuf, stderrBuf bytes.Buffer |
Jack Neus | 0751172 | 2019-07-12 15:41:10 -0600 | [diff] [blame] | 38 | err := CommandRunnerImpl.RunCommand(ctx, &stdoutBuf, &stderrBuf, gitRepo, "git", cmd...) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 39 | cmdOutput := CommandOutput{stdoutBuf.String(), stderrBuf.String()} |
Jack Neus | 758a70f | 2019-08-20 10:32:12 -0600 | [diff] [blame^] | 40 | return cmdOutput, errors.Annotate(err, cmdOutput.Stderr).Err() |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 41 | } |
| 42 | |
Jack Neus | 2bcf4a6 | 2019-07-25 10:35:05 -0600 | [diff] [blame] | 43 | // RunGitIgnore the specified git command in the specified repo and returns |
| 44 | // only an error, not the command output. |
| 45 | func RunGitIgnoreOutput(gitRepo string, cmd []string) error { |
| 46 | _, err := RunGit(gitRepo, cmd) |
| 47 | return err |
| 48 | } |
| 49 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 50 | // GetCurrentBranch returns current branch of a repo, and an empty string |
| 51 | // if repo is on detached HEAD. |
| 52 | func GetCurrentBranch(cwd string) string { |
| 53 | output, err := RunGit(cwd, []string{"symbolic-ref", "-q", "HEAD"}) |
| 54 | if err != nil { |
| 55 | return "" |
| 56 | } |
| 57 | return StripRefsHead(strings.TrimSpace(output.Stdout)) |
| 58 | } |
| 59 | |
| 60 | // MatchBranchName returns the names of branches who match the specified |
| 61 | // regular expression. |
| 62 | func MatchBranchName(gitRepo string, pattern *regexp.Regexp) ([]string, error) { |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 63 | // MatchBranchWithNamespace trims the namespace off the branches it returns. |
| 64 | // Here, we need a namespace that matches every string but doesn't match any character |
| 65 | // (so that nothing is trimmed). |
| 66 | nullNamespace := regexp.MustCompile("") |
| 67 | return MatchBranchNameWithNamespace(gitRepo, pattern, nullNamespace) |
| 68 | } |
| 69 | |
| 70 | // MatchBranchNameWithNamespace returns the names of branches who match the specified |
| 71 | // pattern and start with the specified namespace. |
| 72 | func MatchBranchNameWithNamespace(gitRepo string, pattern, namespace *regexp.Regexp) ([]string, error) { |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 73 | // Regex should be case insensitive. |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 74 | namespace = regexp.MustCompile("(?i)^" + namespace.String()) |
| 75 | pattern = regexp.MustCompile("(?i)" + pattern.String()) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 76 | |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 77 | output, err := RunGit(gitRepo, []string{"show-ref"}) |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 78 | if err != nil { |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 79 | if strings.Contains(err.Error(), "exit status 1") { |
| 80 | // Not a fatal error, just no branches. |
| 81 | return []string{}, nil |
| 82 | } |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 83 | // Could not read branches. |
Jack Neus | 901a6bf | 2019-07-22 08:30:07 -0600 | [diff] [blame] | 84 | 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] | 85 | } |
| 86 | // Find all branches that match the pattern. |
| 87 | branches := strings.Split(output.Stdout, "\n") |
| 88 | matchedBranches := []string{} |
| 89 | for _, branch := range branches { |
| 90 | branch = strings.TrimSpace(branch) |
| 91 | if branch == "" { |
| 92 | continue |
| 93 | } |
| 94 | branch = strings.Fields(branch)[1] |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 95 | |
| 96 | // Only look at branches which match the namespace. |
| 97 | if !namespace.Match([]byte(branch)) { |
| 98 | continue |
| 99 | } |
| 100 | branch = namespace.ReplaceAllString(branch, "") |
| 101 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 102 | if pattern.Match([]byte(branch)) { |
| 103 | matchedBranches = append(matchedBranches, branch) |
| 104 | } |
| 105 | } |
| 106 | return matchedBranches, nil |
| 107 | } |
| 108 | |
Jack Neus | e237037 | 2019-08-14 17:25:16 -0600 | [diff] [blame] | 109 | // IsSHA checks whether or not the given ref is a SHA. |
| 110 | func IsSHA(ref string) bool { |
| 111 | shaRegexp := regexp.MustCompile("^[0-9a-f]{40}$") |
| 112 | return shaRegexp.MatchString(ref) |
| 113 | } |
| 114 | |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 115 | // GetGitRepoRevision finds and returns the revision of a branch. |
Jack Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 116 | func GetGitRepoRevision(cwd, branch string) (string, error) { |
| 117 | if branch == "" { |
| 118 | branch = "HEAD" |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 119 | } else if branch != "HEAD" { |
Jack Neus | 8b77083 | 2019-08-01 15:33:04 -0600 | [diff] [blame] | 120 | branch = NormalizeRef(branch) |
Jack Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 121 | } |
| 122 | output, err := RunGit(cwd, []string{"rev-parse", branch}) |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 123 | return strings.TrimSpace(output.Stdout), errors.Annotate(err, output.Stderr).Err() |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Jack Neus | a728752 | 2019-07-23 16:36:18 -0600 | [diff] [blame] | 126 | // IsReachable determines whether one commit ref is reachable from another. |
| 127 | func IsReachable(cwd, to_ref, from_ref string) (bool, error) { |
| 128 | _, err := RunGit(cwd, []string{"merge-base", "--is-ancestor", to_ref, from_ref}) |
| 129 | if err != nil { |
| 130 | if strings.Contains(err.Error(), "exit status 1") { |
| 131 | return false, nil |
| 132 | } |
| 133 | return false, err |
| 134 | } |
| 135 | return true, nil |
| 136 | } |
| 137 | |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 138 | // StripRefsHead removes leading 'refs/heads/' from a ref name. |
Jack Neus | fc3b577 | 2019-07-03 11:18:42 -0600 | [diff] [blame] | 139 | func StripRefsHead(ref string) string { |
| 140 | return strings.TrimPrefix(ref, "refs/heads/") |
| 141 | } |
| 142 | |
| 143 | // NormalizeRef converts git branch refs into fully qualified form. |
| 144 | func NormalizeRef(ref string) string { |
| 145 | if ref == "" || strings.HasPrefix(ref, "refs/") { |
| 146 | return ref |
| 147 | } |
| 148 | return fmt.Sprintf("refs/heads/%s", ref) |
| 149 | } |
| 150 | |
| 151 | // StripRefs removes leading 'refs/heads/', 'refs/remotes/[^/]+/' from a ref name. |
| 152 | func StripRefs(ref string) string { |
| 153 | ref = StripRefsHead(ref) |
| 154 | // If the ref starts with ref/remotes/, then we want the part of the string |
| 155 | // that comes after the third "/". |
| 156 | // Example: refs/remotes/origin/master --> master |
| 157 | // Example: refs/remotse/origin/foo/bar --> foo/bar |
| 158 | if strings.HasPrefix(ref, "refs/remotes/") { |
| 159 | refParts := strings.SplitN(ref, "/", 4) |
| 160 | return refParts[len(refParts)-1] |
| 161 | } |
| 162 | return ref |
| 163 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 164 | |
| 165 | // CreateBranch creates a branch. |
| 166 | func CreateBranch(gitRepo, branch string) error { |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 167 | output, err := RunGit(gitRepo, []string{"checkout", "-B", branch}) |
Jack Neus | 10e6a12 | 2019-07-18 10:17:44 -0600 | [diff] [blame] | 168 | if err != nil { |
| 169 | if strings.Contains(output.Stderr, "not a valid branch name") { |
| 170 | return fmt.Errorf("%s is not a valid branch name", branch) |
| 171 | } else { |
| 172 | return fmt.Errorf(output.Stderr) |
| 173 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 174 | } |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | // CreateTrackingBranch creates a tracking branch. |
| 179 | func CreateTrackingBranch(gitRepo, branch string, remoteRef RemoteRef) error { |
| 180 | refspec := fmt.Sprintf("%s/%s", remoteRef.Remote, remoteRef.Ref) |
| 181 | output, err := RunGit(gitRepo, []string{"fetch", remoteRef.Remote, remoteRef.Ref}) |
| 182 | if err != nil { |
| 183 | return fmt.Errorf("could not fetch %s: %s", refspec, output.Stderr) |
| 184 | } |
| 185 | output, err = RunGit(gitRepo, []string{"checkout", "-b", branch, "-t", refspec}) |
| 186 | if err != nil { |
| 187 | if strings.Contains(output.Stderr, "not a valid branch name") { |
| 188 | return fmt.Errorf("%s is not a valid branch name", branch) |
| 189 | } else { |
| 190 | return fmt.Errorf(output.Stderr) |
| 191 | } |
| 192 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 193 | return err |
| 194 | } |
| 195 | |
| 196 | // CommitAll adds all local changes and commits them. |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 197 | // Returns the sha1 of the commit. |
| 198 | func CommitAll(gitRepo, commitMsg string) (string, error) { |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 199 | if output, err := RunGit(gitRepo, []string{"add", "-A"}); err != nil { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 200 | return "", fmt.Errorf(output.Stderr) |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 201 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 202 | if output, err := RunGit(gitRepo, []string{"commit", "-m", commitMsg}); err != nil { |
| 203 | if strings.Contains(output.Stdout, "nothing to commit") { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 204 | return "", fmt.Errorf(output.Stdout) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 205 | } else { |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 206 | return "", fmt.Errorf(output.Stderr) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 207 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 208 | } |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 209 | output, err := RunGit(gitRepo, []string{"rev-parse", "HEAD"}) |
| 210 | if err != nil { |
| 211 | return "", err |
| 212 | } |
| 213 | return strings.TrimSpace(output.Stdout), nil |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 214 | } |
| 215 | |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 216 | // CommitEmpty makes an empty commit (assuming nothing is staged). |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 217 | // Returns the sha1 of the commit. |
| 218 | func CommitEmpty(gitRepo, commitMsg string) (string, error) { |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 219 | 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] | 220 | return "", fmt.Errorf(output.Stderr) |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 221 | } |
Jack Neus | f116fae | 2019-07-24 15:05:03 -0600 | [diff] [blame] | 222 | output, err := RunGit(gitRepo, []string{"rev-parse", "HEAD"}) |
| 223 | if err != nil { |
| 224 | return "", nil |
| 225 | } |
| 226 | return strings.TrimSpace(output.Stdout), nil |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Jack Neus | eb25f72 | 2019-07-19 16:33:20 -0600 | [diff] [blame] | 229 | // PushRef pushes the specified local ref to the specified remote ref. |
| 230 | func PushRef(gitRepo, localRef string, dryRun bool, pushTo RemoteRef) error { |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 231 | ref := fmt.Sprintf("%s:%s", localRef, pushTo.Ref) |
| 232 | cmd := []string{"push", pushTo.Remote, ref} |
| 233 | if dryRun { |
| 234 | cmd = append(cmd, "--dry-run") |
| 235 | } |
Jack Neus | 7440c76 | 2019-07-22 10:45:18 -0600 | [diff] [blame] | 236 | _, err := RunGit(gitRepo, cmd) |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 237 | return err |
| 238 | } |
| 239 | |
| 240 | // Init initializes a repo. |
| 241 | func Init(gitRepo string, bare bool) error { |
| 242 | cmd := []string{"init"} |
| 243 | if bare { |
| 244 | cmd = append(cmd, "--bare") |
| 245 | } |
Jack Neus | abdbe19 | 2019-07-15 12:23:22 -0600 | [diff] [blame] | 246 | _, err := RunGit(gitRepo, cmd) |
| 247 | return err |
| 248 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 249 | |
| 250 | // AddRemote adds a remote. |
| 251 | func AddRemote(gitRepo, remote, remoteLocation string) error { |
| 252 | output, err := RunGit(gitRepo, []string{"remote", "add", remote, remoteLocation}) |
| 253 | if err != nil { |
Jack Neus | eb25f72 | 2019-07-19 16:33:20 -0600 | [diff] [blame] | 254 | if strings.Contains(output.Stderr, "already exists") { |
| 255 | return fmt.Errorf("remote already exists") |
| 256 | } |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 257 | } |
| 258 | return err |
| 259 | } |
| 260 | |
| 261 | // Checkout checkouts a branch. |
| 262 | func Checkout(gitRepo, branch string) error { |
| 263 | output, err := RunGit(gitRepo, []string{"checkout", branch}) |
Jack Neus | 91de240 | 2019-08-14 08:26:03 -0600 | [diff] [blame] | 264 | if err != nil { |
Jack Neus | 0296c73 | 2019-07-17 09:35:01 -0600 | [diff] [blame] | 265 | return fmt.Errorf(output.Stderr) |
| 266 | } |
| 267 | return err |
| 268 | } |
| 269 | |
| 270 | // DeleteBranch checks out to master and then deletes the current branch. |
| 271 | func DeleteBranch(gitRepo, branch string, force bool) error { |
| 272 | cmd := []string{"branch"} |
| 273 | if force { |
| 274 | cmd = append(cmd, "-D") |
| 275 | } else { |
| 276 | cmd = append(cmd, "-d") |
| 277 | } |
| 278 | cmd = append(cmd, branch) |
| 279 | output, err := RunGit(gitRepo, cmd) |
| 280 | |
| 281 | if err != nil { |
| 282 | if strings.Contains(output.Stderr, "checked out at") { |
| 283 | return fmt.Errorf(output.Stderr) |
| 284 | } |
| 285 | if strings.Contains(output.Stderr, "not fully merged") { |
| 286 | return fmt.Errorf("branch %s is not fully merged. use the force parameter if you wish to proceed", branch) |
| 287 | } |
| 288 | } |
| 289 | return err |
| 290 | } |
| 291 | |
| 292 | // Clone clones the remote into the specified dir. |
| 293 | func Clone(remote, dir string) error { |
| 294 | output, err := RunGit(filepath.Dir(dir), []string{"clone", remote, filepath.Base(dir)}) |
| 295 | if err != nil { |
| 296 | return fmt.Errorf(output.Stderr) |
| 297 | } |
| 298 | return nil |
| 299 | } |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 300 | |
| 301 | // RemoteBranches returns a list of branches on the specified remote. |
| 302 | func RemoteBranches(gitRepo, remote string) ([]string, error) { |
| 303 | output, err := RunGit(gitRepo, []string{"ls-remote", remote}) |
| 304 | if err != nil { |
| 305 | if strings.Contains(output.Stderr, "not appear to be a git repository") { |
| 306 | return []string{}, fmt.Errorf("%s is not a valid remote", remote) |
| 307 | } |
Jack Neus | e237037 | 2019-08-14 17:25:16 -0600 | [diff] [blame] | 308 | return []string{}, fmt.Errorf(output.Stderr) |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 309 | } |
| 310 | remotes := []string{} |
| 311 | for _, line := range strings.Split(strings.TrimSpace(output.Stdout), "\n") { |
| 312 | if line == "" { |
| 313 | continue |
| 314 | } |
| 315 | remotes = append(remotes, StripRefs(strings.Fields(line)[1])) |
| 316 | } |
| 317 | return remotes, nil |
| 318 | } |
| 319 | |
| 320 | // RemoteHasBranch checks whether or not a branch exists on a remote. |
| 321 | func RemoteHasBranch(gitRepo, remote, branch string) (bool, error) { |
Jack Neus | e237037 | 2019-08-14 17:25:16 -0600 | [diff] [blame] | 322 | output, err := RunGit(gitRepo, []string{"ls-remote", remote, branch}) |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 323 | if err != nil { |
Jack Neus | e237037 | 2019-08-14 17:25:16 -0600 | [diff] [blame] | 324 | if strings.Contains(output.Stderr, "not appear to be a git repository") { |
| 325 | return false, fmt.Errorf("%s is not a valid remote", remote) |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 326 | } |
Jack Neus | e237037 | 2019-08-14 17:25:16 -0600 | [diff] [blame] | 327 | return false, fmt.Errorf(output.Stderr) |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 328 | } |
Jack Neus | e237037 | 2019-08-14 17:25:16 -0600 | [diff] [blame] | 329 | return output.Stdout != "", nil |
Jack Neus | 1f489f6 | 2019-08-06 12:01:54 -0600 | [diff] [blame] | 330 | } |