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

Side by Side 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: more tests and refactors Created 6 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « go/src/infra/tools/drover/disambiguate.go ('k') | go/src/infra/tools/drover/merge.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium 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 main
5
6 import (
7 "bufio"
8 "flag"
9 "fmt"
10 "io/ioutil"
11 "os"
12 "strings"
13
14 "github.com/daviddengcn/go-colortext"
15
16 "infra/libs/git"
17 "infra/libs/git/repo"
18 "infra/libs/gitiles"
19 )
20
21 var commitish = flag.String("commit", "", "The commit to process (or commit posi tion number).")
22 var action = flag.String("action", "", "The action to take. Must specify `cherry -pick|revert`.")
23 var ref = flag.String("ref", "", "The target ref to change.")
24 var workdir string
25
26 func getCodereviewSettings(g *gitiles.Gitiles, ref string) (ret map[string]strin g) {
27 crSettings, err := g.Text("+", ref, "codereview.settings")
28 if err == nil {
29 lines := strings.Split(string(crSettings), "\n")
30 ret = make(map[string]string, len(lines))
31 for _, line := range lines {
32 if len(line) == 0 || line[0] == '#' {
33 continue
34 }
35 keyVal := strings.Split(line, ":")
36 if len(keyVal) == 2 {
37 ret[keyVal[0]] = strings.TrimSpace(keyVal[1])
38 }
39 }
40 }
41 return ret
42 }
43
44 func getLandingCommit(g *gitiles.Gitiles, ref string, settings map[string]string ) (*git.Commit, string) {
45 prefix, ok := settings["PENDING_REF_PREFIX"]
46 if ok {
47 ref = strings.Replace(ref, "refs/", prefix, 1)
48 }
49 result, err := g.GetObjectFromPath(ref)
50 failIf(err)
51 if result.Type() != git.CommitType {
52 panic(fmt.Errorf("ref points at non-commit? %#v", result))
53 }
54 return result.(*git.Commit), ref
55 }
56
57 func confirmAction(URL, action, ref string, commit *git.Commit, diff string) {
58 verbPart := "to"
59 if action == "revert" {
60 verbPart = "from"
61 }
62
63 fmt.Printf("Planning to %s commit %s %s %s in %s:\n\n",
64 action, commit.ID(), verbPart, ref, URL)
65
66 ct.ChangeColor(ct.Yellow, false, ct.None, false)
67 fmt.Println("commit", commit.ID())
68 ct.ResetColor()
69 fmt.Printf("Author: %s <%s>\n", commit.Author().Name, commit.Author().Em ail)
70 fmt.Printf("Date: %s\n", commit.Author().Time)
71 fmt.Println()
72 for _, l := range commit.MessageLines() {
73 fmt.Printf(" %s\n", l)
74 }
75 fmt.Println()
76 for _, pair := range commit.FooterPairs() {
77 ct.ChangeColor(ct.White, true, ct.None, false)
78 fmt.Printf(" %s: ", pair.Key)
79 ct.ResetColor()
80 fmt.Println(pair.Value)
81 }
82 fmt.Println()
83 for _, line := range strings.Split(diff, "\n") {
84 if len(line) > 0 {
85 switch {
86 case strings.HasPrefix(line, "+++") || strings.HasPrefix (line, "---"):
87 ct.ChangeColor(ct.White, true, ct.None, false)
88 case line[0] == '@':
89 ct.ChangeColor(ct.Cyan, false, ct.None, false)
90 bits := strings.SplitN(line, "@@", 3)
91 fmt.Print("@@", bits[1], "@@")
92 ct.ResetColor()
93 fmt.Println(bits[2])
94 continue
95 case line[0] == '+':
96 ct.ChangeColor(ct.Green, false, ct.None, false)
97 case line[0] == '-':
98 ct.ChangeColor(ct.Red, false, ct.None, false)
99 case line[0] == ' ':
100 ct.ResetColor()
101 default:
102 ct.ChangeColor(ct.White, true, ct.None, false)
103 }
104 }
105 fmt.Println(line)
106 }
107 ct.ResetColor()
108
109 fmt.Println()
110 fmt.Printf("Continue? [y/N] ")
111 answerRaw, err := bufio.NewReader(os.Stdin).ReadBytes('\n')
112 failIf(err)
113
114 answer := strings.TrimSpace(string(answerRaw))
115
116 if len(answer) != 0 && strings.HasPrefix("yes", strings.ToLower(answer)) {
117 return
118 }
119
120 fmt.Println("Action aborted")
121 os.Exit(1)
122 }
123
124 func getCommitData(g *gitiles.Gitiles, gitSha string) (*git.Commit, git.TreeDiff ) {
125 commitRslt, err := g.GetObjectFromPath(gitSha)
126 failIf(err)
127
128 commit, ok := commitRslt.(*git.Commit)
129 if !ok {
130 failIf(fmt.Errorf("%s is not a commit?", gitSha))
131 }
132
133 commitDiffRslt, err := g.GetCommitDiff(gitSha)
134 failIf(err)
135
136 return commit, commitDiffRslt
137 }
138
139 func main() {
140 cwd, err := os.Getwd()
141 if err != nil {
142 panic(err)
143 }
144 flag.StringVar(&workdir, "workdir", cwd, "The work directory to use.")
145
146 flag.Usage = func() {
147 fmt.Println("Usage: git drover -commit deadbeef -action cherry-p ick -ref 2125")
148 flag.PrintDefaults()
149 }
150 flag.Parse()
151 if *commitish == "" || *action == "" || *ref == "" {
152 flag.PrintDefaults()
153 fmt.Println("must specify commitish, action and ref")
154 os.Exit(1)
155 }
156 if *action != "cherry-pick" && *action != "revert" {
157 flag.PrintDefaults()
158 fmt.Printf(
159 "must action must be 'cherry-pick' or 'revert'. Got '%s' .\n", *action)
160 os.Exit(1)
161 }
162
163 repo := repo.NewRepo(workdir)
164 if repo.RunOk("rev-parse", "--git-dir") {
165 if repo.RunOutputS("config", "drover.repo") != "true" {
166 fmt.Println("must be run from an empty directory or a dr over git repo")
167 os.Exit(1)
168 }
169 } else {
170 files, err := ioutil.ReadDir(workdir)
171 failIf(err)
172 if len(files) == 0 {
173 fmt.Println("Initializing current directory as a drover git repo")
174 repo.MustRunOk("init")
175 repo.MustRunOk("config", "drover.repo", "true")
176 } else {
177 flag.PrintDefaults()
178 fmt.Println("must be run from an empty directory or a dr over git repo")
179 os.Exit(1)
180 }
181 }
182
183 gitSha, gitilesURL, err := disambiguateCommit(*commitish)
184 failIf(err)
185
186 g := gitiles.NewGitiles(gitilesURL, 8)
187
188 *ref, err = disambiguateRef(g, gitSha, *ref)
189 failIf(err)
190
191 commit, treeDiff := getCommitData(g, gitSha)
192 textDiff, err := g.GetTextCommitDiff(commit.ID())
193 failIf(err)
194
195 settings := getCodereviewSettings(g, *ref)
196 landCommit, real_ref := getLandingCommit(g, *ref, settings)
197
198 confirmAction(g.URL(), *action, *ref, commit, textDiff)
199
200 acquireObjects(repo, g, commit, landCommit, treeDiff)
201
202 var left, right *git.ObjectID
203 if *action == "cherry-pick" {
204 left, right = commit.Parents()[0], commit.ID()
205 } else if *action == "revert" {
206 right, left = commit.Parents()[0], commit.ID()
207 }
208 toPush := createCommit(*action, repo, left, right, landCommit.ID())
209 id := toPush.ID().String()
210
211 diff, err := repo.GetTextDiff(id+"~", id)
212 failIf(err)
213
214 confirmAction(g.URL(), "push", real_ref, toPush, diff)
215 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/drover/disambiguate.go ('k') | go/src/infra/tools/drover/merge.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698