Commits

David Wolever committed 0c67578

Fix pullpull so it will work properly when the local branch exists

Comments (0)

Files changed (2)

 import json
 import urllib2
 import urlparse
+import subprocess as sp
 
 pull_re = re.compile("/(.*?)/(.*?)/pull/([0-9]+)")
 
 class CmdError(Exception):
     pass
 
-def system(cmd, *args):
-    cmd_actual = cmd %tuple("'%s'" %(a.replace("'", "\\'"), ) for a in args)
-    res = os.system(cmd_actual)
+def _escape_cmd(cmd, args):
+    return cmd %tuple("'%s'" %(a.replace("'", "\\'"), ) for a in args)
+
+def _call(cmd, args):
+    cmd_actual = _escape_cmd(cmd, args)
+    print cmd_actual
+    p = sp.Popen(cmd_actual, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
+    stdout, stderr = p.communicate()
+    return p.returncode, stdout, stderr
+
+def call_nocheck(cmd, *args):
+    return _call(cmd, args)
+
+def call(cmd, *args):
+    res, stdout, stderr = _call(cmd, args)
     if res != 0:
-        raise CmdError("%r exited with status %s" %(cmd_actual, res))
-    return 0
+        raise CmdError("%r exited with status %s and stderr:\n%s"
+                       %(_escape_cmd(cmd, args), res, stderr))
+    return stdout
 
 def main(argv):
     argv = list(argv)
             return 1
         remote_base_repo = pull_data["base"]["repo"]["git_url"]
         remote_base_name = pull_data["base"]["repo"]["name"]
-        system("git clone %s %s", remote_base_repo, remote_base_name)
+        call("git clone %s %s", remote_base_repo, remote_base_name)
         os.chdir(remote_base_name)
         did_create = True
 
     try:
         base_sha = pull_data["base"]["sha"]
-        system("git show %s >/dev/null", base_sha)
+        call("git show %s >/dev/null", base_sha)
     except CmdError as e:
         expected_url = pull_data["base"]["repo"]["git_url"]
         print "error: %s" %(e, )
     remote_repo = pull_data["head"]["repo"]["git_url"]
     remote_branch = pull_data["head"]["ref"]
     local_branch = "pull-%s" %(num, )
-    system("git remote add -t %s %s %s",
-           remote_branch, local_branch, remote_repo)
-    system("git fetch %s %s:%s", remote_repo, remote_branch, local_branch)
-    system("git checkout %s", local_branch)
-    
+
+    branch_exists = call("git branch --list %s", local_branch)
+    if branch_exists:
+        overwrite = raw_input("branch %r already exists. overwrite? (Y/n) "
+                              %(local_branch, ))
+        if overwrite.lower()[:1] == "n":
+            print "aborting."
+            return 1
+        call("git checkout --quiet master")
+        call("git branch -D %s", local_branch)
+
+    remote_exists_code, _, _ = call_nocheck("git remote show %s", local_branch)
+    if remote_exists_code == 0:
+        call("git remote remove %s", local_branch)
+    call("git remote add -t %s %s %s", remote_branch, local_branch, remote_repo)
+    call("git fetch %s %s:%s", remote_repo, remote_branch, local_branch)
+    call("git checkout %s", local_branch)
+
     if did_create:
         print "repository cloned and pull request applied to '%s/'" %(
             remote_base_name,
 }
 
 alias glog="vs glog | less"
-alias viq="vim \$(vs root)/.$(vsname)/patches/series"
+alias viq="vim \$(vs root)/.\$(vsname)/patches/series"
 for vscmd in tip qimport qser qpo qpop qpu qpush qfin qref qnew qrm qmv di ci qdi st rebase pull nudge sum co bm
 do
     alias $vscmd="vs $vscmd"