// Push pushes the current branch to Gerrit. func Push(run *runutil.Run, clOpts CLOpts) error { remote := clOpts.Remote if remote == "" { var err error remote, err = getRemoteURL(run, clOpts) if err != nil { return err } } refspec := "HEAD:" + Reference(clOpts) args := []string{"push", remote, refspec} // TODO(jamesr): This should really reuse gitutil/git.go's Push which knows // how to set this option but doesn't yet know how to pipe stdout/stderr the way // this function wants. if clOpts.Verify { args = append(args, "--verify") } else { args = append(args, "--no-verify") } var stdout, stderr bytes.Buffer opts := run.Opts() opts.Stdout = &stdout opts.Stderr = &stderr if err := run.CommandWithOpts(opts, "git", args...); err != nil { return gitutil.Error(stdout.String(), stderr.String(), args...) } for _, line := range strings.Split(stderr.String(), "\n") { if remoteRE.MatchString(line) { fmt.Println(line) } } return nil }
// getRemoteURL returns the URL of the Gerrit project with respect to the // project identified by the current working directory. func getRemoteURL(run *runutil.Run, clOpts CLOpts) (string, error) { args := []string{"config", "--get", "remote.origin.url"} var stdout, stderr bytes.Buffer opts := run.Opts() opts.Stdout = &stdout opts.Stderr = &stderr if err := run.CommandWithOpts(opts, "git", args...); err != nil { return "", gitutil.Error(stdout.String(), stderr.String(), args...) } baseUrl := clOpts.Host if !strings.HasSuffix(baseUrl, "/") { baseUrl = baseUrl + "/" } return baseUrl + filepath.Base(strings.TrimSpace(stdout.String())), nil }