diff gut/main.py @ 0:9688c72a93c3

initial commit of gut, probably doesnt actually work
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 16 Jul 2010 13:55:58 -0700
parents
children 54b30e8f6f82
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gut/main.py	Fri Jul 16 13:55:58 2010 -0700
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+"""
+a workflow for git
+"""
+
+import os
+import subprocess
+import sys
+
+from command import CommandParser
+
+def call(command, **kw):
+    if isinstance(command, basestring):
+        kw['shell'] = True
+    output = kw.pop('output', True)
+    if output or kw.pop('pipe', False):
+        kw['stdout'] = subprocess.PIPE
+        kw['stderr'] = subprocess.PIPE
+    check = kw.pop('check', True)
+    process = subprocess.Popen(command, **kw)
+    stdout, stderr = process.communicate()
+    code = process.poll()
+    if check and code:
+        if isinstance(command, basestring):
+            cmdstr = command
+        else:
+            cmdstr = ' '.join(command)
+        raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
+    if output:
+        print stdout
+        print stderr
+    return dict(stdout=stdout, stderr=stderr, code=code)
+
+class gut(object):
+    """
+    a workflow for git
+    """
+
+    def __init__(self, remote=None):
+        self.remote = remote 
+
+    def hello(self, name='world'):
+        print 'hello %s' % name
+
+    def update(self):
+        """update the master and the branch you're on"""
+        call(['git', 'checkout', 'master'])
+        call(['git', 'pull', 'origin', 'master'])
+        if self.remote:
+            call(['git', 'pull', self.remote, 'master'])
+
+    def feature(self, name):
+        """make a new feature branch"""
+        call(['git', 'checkout', 'master'])
+        call(['git', 'checkout', '-b', name])
+        call(['git', 'push', 'origin', name])
+
+    def patch(self, output=None):
+        """generate a patch for review"""
+        if not output:
+            output = self.branch() + '.diff'
+        diff = call(['git', 'diff', 'master'], pipe=True, output=False)
+        diff = diff['stdout']
+        log = call(['git', 'log', 'master..'], pipe=True, output=False)
+        log = log['stdout']
+        f = file(output) # write the output to a patch file
+        return log
+
+    def branch(self):
+        """print what branch you're on"""
+        output = call(['git', 'branch'], output=False)
+        for line in output['stdout'].splitlines():
+            if line.startswith('*'):
+                return line[1:].strip()
+
+def main(args=sys.argv[1:]):
+    parser = CommandParser(gut)
+    options, args = parser.parse_args(args)
+    parser.invoke(args)
+
+if __name__ == '__main__':
+    main()