Mercurial > hg > gut
annotate gut/main.py @ 4:190ce22e7e83
scour the log and format it like a civilized human being
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 16 Jul 2010 16:18:06 -0700 |
parents | 4d38d14cf1d4 |
children | ef895ddba2d3 |
rev | line source |
---|---|
0
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 """ |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 a workflow for git |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """ |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 import os |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 import subprocess |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 import sys |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 from command import CommandParser |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 def call(command, **kw): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 if isinstance(command, basestring): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 kw['shell'] = True |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 output = kw.pop('output', True) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 if output or kw.pop('pipe', False): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 kw['stdout'] = subprocess.PIPE |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 kw['stderr'] = subprocess.PIPE |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 check = kw.pop('check', True) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 process = subprocess.Popen(command, **kw) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 stdout, stderr = process.communicate() |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 code = process.poll() |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 if check and code: |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 if isinstance(command, basestring): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 cmdstr = command |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 else: |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 cmdstr = ' '.join(command) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 if output: |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 print stdout |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 print stderr |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 return dict(stdout=stdout, stderr=stderr, code=code) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 |
2 | 34 def fake_call(command, **kw): |
35 if isinstance(command, basestring): | |
36 print command | |
37 else: | |
38 print ' '.join(command) | |
39 | |
0
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 class gut(object): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 """ |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 a workflow for git |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 """ |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 |
2 | 45 def __init__(self, remote=None, simulate=False): |
46 """ | |
47 - remote: name of the remote repository in .git/config | |
48 - simulate: print what calls will be used but don't run them | |
49 """ | |
50 self.remote = remote | |
51 self.simulate = simulate | |
52 if simulate: | |
53 globals()['call'] = fake_call | |
0
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 def update(self): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 """update the master and the branch you're on""" |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 call(['git', 'checkout', 'master']) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 call(['git', 'pull', 'origin', 'master']) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 if self.remote: |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 call(['git', 'pull', self.remote, 'master']) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
62 def feature(self, name): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 """make a new feature branch""" |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
64 call(['git', 'checkout', 'master']) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 call(['git', 'checkout', '-b', name]) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
66 call(['git', 'push', 'origin', name]) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 def patch(self, output=None): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 """generate a patch for review""" |
2 | 70 diff = call(['git', 'diff', 'master'], pipe=True, output=False) |
71 log = call(['git', 'log', 'master..'], pipe=True, output=False) | |
72 if self.simulate: | |
73 return | |
0
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 if not output: |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
75 output = self.branch() + '.diff' |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 diff = diff['stdout'] |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 log = log['stdout'] |
4
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
78 |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
79 # format the log |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
80 lines = [] |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
81 oldline = None |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
82 for line in log.splitlines(): |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
83 if not line: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
84 continue |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
85 if line[0].strip(): |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
86 if oldline: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
87 lines.append(oldline) |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
88 oldline = None |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
89 incomment = False |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
90 continue |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
91 line = line.strip() |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
92 if line.startswith('*'): |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
93 if oldline: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
94 lines.append(oldline) |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
95 oldline = None |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
96 lines.append(line[1:].strip()) |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
97 continue |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
98 if oldline: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
99 oldline = oldline + ' ' + line |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
100 else: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
101 oldline = line |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
102 else: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
103 if oldline: |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
104 lines.append(oldline) |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
105 log = '\n\n'.join(['* %s' % line for line in lines]) |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
106 |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
107 f = file(output, 'w') # write the output to a patch file |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
108 print >> f, diff |
190ce22e7e83
scour the log and format it like a civilized human being
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
109 f.close() |
0
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
110 return log |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
111 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
112 def branch(self): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
113 """print what branch you're on""" |
3 | 114 output = call(['git', 'branch'], output=False, pipe=True) |
2 | 115 if self.simulate: |
116 return | |
0
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
117 for line in output['stdout'].splitlines(): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
118 if line.startswith('*'): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
119 return line[1:].strip() |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
120 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
121 def main(args=sys.argv[1:]): |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
122 parser = CommandParser(gut) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
123 parser.invoke(args) |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
124 |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
125 if __name__ == '__main__': |
9688c72a93c3
initial commit of gut, probably doesnt actually work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
126 main() |