From e557c1a64f2a2a09c27cad779a7056675fedfc57 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 19 Feb 2020 12:01:13 -0800 Subject: [PATCH] Raise custom exception for NoUpstreamBranch. Originally I merged these two commands to reduce the number of subprocess spawns, but I found that the majority of execution time was git status anyway. These ones are fairly cheap. --- gitcheckup.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gitcheckup.py b/gitcheckup.py index 3864bf2..b42d72a 100644 --- a/gitcheckup.py +++ b/gitcheckup.py @@ -16,6 +16,9 @@ GIT = winwhich.which('git') # A file4 # ?? file3 +class NoUpstreamBranch(Exception): + pass + def check_output(command): output = subprocess.check_output(command, stderr=subprocess.STDOUT) return output @@ -46,9 +49,16 @@ def checkup_committed(): return (committed, details) def checkup_pushed(): - command = [GIT, 'rev-parse', '@', '@{u}'] - output = check_output(command) - (my_head, remote_head) = output.strip().decode().splitlines() + command = [GIT, 'rev-parse', '@'] + my_head = check_output(command).strip().decode() + + command = [GIT, 'rev-parse', '@{u}'] + try: + remote_head = check_output(command).strip().decode() + except subprocess.CalledProcessError as exc: + command = [GIT, 'rev-parse', '--abbrev-ref', 'HEAD'] + current_branch = check_output(command).strip().decode() + raise NoUpstreamBranch(current_branch) if my_head == remote_head: to_push = 0