Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Unified Diff: go/src/infra/tools/drover/main.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/src/infra/tools/drover/main.go
diff --git a/go/src/infra/tools/drover/main.go b/go/src/infra/tools/drover/main.go
new file mode 100644
index 0000000000000000000000000000000000000000..830b33497719924b789aaea5d24d55381a9d62c6
--- /dev/null
+++ b/go/src/infra/tools/drover/main.go
@@ -0,0 +1,217 @@
+package main
+
+import "bufio"
+import "flag"
+import "fmt"
+import "io/ioutil"
+import "os"
+import "strings"
+
+import "github.com/daviddengcn/go-colortext"
+
+import "infra/libs/git"
+import "infra/libs/gitiles"
+
+var commitish = flag.String("commit", "", "The commit to process (or commit position number).")
+var action = flag.String("action", "", "The action to take. Must specify `cherry-pick|revert`.")
M-A Ruel 2014/10/18 00:47:06 Why not two boolean flags instead?
iannucci 2014/10/20 21:11:58 They're mutually exclusive.
+var ref = flag.String("ref", "", "The target ref to change.")
+var workdir string
+
+func init() {
M-A Ruel 2014/10/18 00:47:06 No need for init in package main, just put it at t
iannucci 2014/10/20 21:11:58 Done.
+ cwd, err := os.Getwd()
+ if err != nil {
+ panic(err)
+ }
+ flag.StringVar(&workdir, "workdir", cwd, "The work directory to use.")
+}
+
+func getCodereviewSettings(g *gitiles.Gitiles, ref string) (ret map[string]string) {
+ crSettings := <-g.Text("+", ref, "codereview.settings")
+ if crSettings.Err == nil {
+ lines := strings.Split(string(crSettings.Data), "\n")
+ ret = make(map[string]string, len(lines))
+ for _, line := range lines {
+ if len(line) == 0 || line[0] == '#' {
+ continue
+ }
+ keyVal := strings.Split(line, ":")
+ if len(keyVal) == 2 {
+ ret[keyVal[0]] = strings.TrimSpace(keyVal[1])
+ }
+ }
+ }
+ return ret
+}
+
+func getLandingCommit(g *gitiles.Gitiles, ref string, settings map[string]string) (*git.Commit, string) {
+ prefix, ok := settings["PENDING_REF_PREFIX"]
+ if ok {
+ ref = strings.Replace(ref, "refs/", prefix, 1)
+ }
+ result := <-g.GetObjectFromPath(ref)
+ failIf(result.Err)
+ if result.Object.Type() != "commit" {
+ panic(fmt.Errorf("ref points at non-commit? %#v", result.Object))
+ }
+ return result.Object.(*git.Commit), ref
+}
+
+func confirmAction(URL, action, ref string, commit *git.Commit, diff []string) {
+ verbPart := "to"
+ if action == "revert" {
+ verbPart = "from"
+ }
+
+ fmt.Printf("Planning to %s commit %s %s %s in %s:\n\n",
+ action, commit.ID(), verbPart, ref, URL)
+
+ ct.ChangeColor(ct.Yellow, false, ct.None, false)
+ fmt.Println("commit", commit.ID())
+ ct.ResetColor()
+ fmt.Printf("Author: %s <%s>\n", commit.Author().Name, commit.Author().Email)
+ fmt.Printf("Date: %s\n", commit.Author().Time)
+ fmt.Println()
+ for _, l := range commit.MessageLines() {
+ fmt.Printf(" %s\n", l)
+ }
+ fmt.Println()
+ for _, pair := range commit.FooterPairs() {
+ ct.ChangeColor(ct.White, true, ct.None, false)
+ fmt.Printf(" %s: ", pair.Key)
+ ct.ResetColor()
+ fmt.Println(pair.Value)
+ }
+ fmt.Println()
+ for _, line := range diff {
+ if len(line) > 0 {
+ switch {
+ case strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---"):
+ ct.ChangeColor(ct.White, true, ct.None, false)
+ case line[0] == '@':
+ ct.ChangeColor(ct.Cyan, false, ct.None, false)
+ bits := strings.SplitN(line, "@@", 3)
+ fmt.Print("@@", bits[1], "@@")
+ ct.ResetColor()
+ fmt.Println(bits[2])
+ continue
+ case line[0] == '+':
+ ct.ChangeColor(ct.Green, false, ct.None, false)
+ case line[0] == '-':
+ ct.ChangeColor(ct.Red, false, ct.None, false)
+ case line[0] == ' ':
+ ct.ResetColor()
+ default:
+ ct.ChangeColor(ct.White, true, ct.None, false)
+ }
+ }
+ fmt.Println(line)
+ }
+ ct.ResetColor()
+
+ fmt.Println()
+ fmt.Printf("Continue? [y/N] ")
+ answerRaw, err := bufio.NewReader(os.Stdin).ReadBytes('\n')
+ failIf(err)
+
+ answer := strings.TrimSpace(string(answerRaw))
+
+ if len(answer) != 0 && strings.HasPrefix("yes", strings.ToLower(answer)) {
+ return
+ }
+
+ fmt.Println("Action aborted")
+ os.Exit(1)
+}
+
+func getCommitData(g *gitiles.Gitiles, gitSha string) (*git.Commit, git.TreeDiff) {
+ chCommit := g.GetObjectFromPath(gitSha)
+ chCommitDiff := g.GetCommitDiff(gitSha)
+
+ commitRslt := <-chCommit
+ failIf(commitRslt.Err)
+
+ commit, ok := commitRslt.Object.(*git.Commit)
+ if !ok {
+ failIf(fmt.Errorf("%s is not a commit?", gitSha))
+ }
+
+ commitDiffRslt := chCommitDiff
+ failIf(commitRslt.Err)
+
+ return commit, commitDiffRslt.Diff
+}
+
+func main() {
+ flag.Usage = func() {
+ fmt.Println("Usage: git drover -commit deadbeef -action cherry-pick -ref 2125")
+ flag.PrintDefaults()
+ }
+ flag.Parse()
+ if *commitish == "" || *action == "" || *ref == "" {
+ flag.PrintDefaults()
+ fmt.Println("must specify commitish, action and ref")
+ os.Exit(1)
M-A Ruel 2014/10/18 00:47:06 I generally prefer to have something that looks li
+ }
+ if *action != "cherry-pick" && *action != "revert" {
+ flag.PrintDefaults()
+ fmt.Printf(
+ "must action must be 'cherry-pick' or 'revert'. Got '%s'.\n", *action)
+ os.Exit(1)
+ }
+
+ repo := &git.Repo{Path: workdir}
+ if repo.RunOk("rev-parse", "--git-dir") {
+ if !repo.RunOk("config", "drover.repo") {
+ fmt.Println("must be run from an empty directory or a drover git repo")
+ os.Exit(1)
+ }
+ } else {
+ files, err := ioutil.ReadDir(workdir)
+ failIf(err)
+ if len(files) == 0 {
+ fmt.Println("Initializing current directory as a drover git repo")
+ if !repo.RunOk("init") {
+ flag.PrintDefaults()
+ panic("Couldn't initialize git repo!")
+ }
+ if !repo.RunOk("config", "drover.repo", "true") {
+ flag.PrintDefaults()
+ panic("Couldn't configure git repo to be owned by drover!")
+ }
+ } else {
+ flag.PrintDefaults()
+ fmt.Println("must be run from an empty directory or a drover git repo")
+ os.Exit(1)
+ }
+ }
+
+ gitSha, gitilesURL, err := disambiguateCommit(*commitish)
+ failIf(err)
+
+ g := gitiles.NewGitiles(gitilesURL, 8)
+
+ *ref, err = disambiguateRef(g, gitSha, *ref)
+ failIf(err)
+
+ commit, treeDiff := getCommitData(g, gitSha)
+ textDiff := g.GetTextCommitDiff(commit.ID())
+ failIf(textDiff.Err)
+
+ settings := getCodereviewSettings(g, *ref)
+ landCommit, real_ref := getLandingCommit(g, *ref, settings)
+
+ confirmAction(g.URL(), *action, *ref, commit, textDiff.Diff)
+
+ acquireObjects(repo, g, commit, landCommit, treeDiff)
+
+ var left, right git.ObjectID
+ if *action == "cherry-pick" {
+ left, right = commit.Parents()[0], commit.ID()
+ } else if *action == "revert" {
+ right, left = commit.Parents()[0], commit.ID()
+ }
+ toPush := createCommit(*action, repo, left, right, landCommit.ID())
+ id := toPush.ID().String()
+
+ confirmAction(g.URL(), "push", real_ref, toPush, repo.GetTextDiff(id+"~", id))
+}

Powered by Google App Engine
This is Rietveld 408576698