Move all git commands into their own functions.

This commit is contained in:
voussoir 2020-02-19 14:20:54 -08:00
parent 7fc0a5d83c
commit 2b6bbd276e

View file

@ -32,6 +32,7 @@ class NoUpstreamBranch(GitCheckupException):
################################################################################ ################################################################################
def check_output(command): def check_output(command):
output = subprocess.check_output(command, stderr=subprocess.STDOUT) output = subprocess.check_output(command, stderr=subprocess.STDOUT)
output = output.decode().strip()
return output return output
def read_directories_file(): def read_directories_file():
@ -55,26 +56,40 @@ def read_directories_file():
def git_commits_between(a, b): def git_commits_between(a, b):
command = [GIT, 'log', '--oneline', f'{a}..{b}'] command = [GIT, 'log', '--oneline', f'{a}..{b}']
output = check_output(command) output = check_output(command)
lines = output.strip().decode().splitlines() lines = output.splitlines()
return lines return lines
def git_current_branch():
command = [GIT, 'rev-parse', '--abbrev-ref', 'HEAD']
return check_output(command)
def git_fetch(): def git_fetch():
command = [GIT, 'fetch', '--all'] command = [GIT, 'fetch', '--all']
output = check_output(command) return check_output(command)
def git_merge_base():
command = [GIT, 'merge-base', '@', '@{u}']
return check_output(command)
def git_rev_parse(rev):
command = [GIT, 'rev-parse', '@{u}']
return check_output(command)
def git_status():
command = [GIT, 'status', '--short', '--untracked-files=all']
return check_output(command)
# CHECKUP # CHECKUP
################################################################################ ################################################################################
def checkup_committed(): def checkup_committed():
details = dotdict.DotDict(default=None) details = dotdict.DotDict(default=None)
command = [GIT, 'status', '--short', '--untracked-files=all']
output = check_output(command)
details.added = 0 details.added = 0
details.modified = 0 details.modified = 0
details.deleted = 0 details.deleted = 0
for line in output.splitlines():
status = line.split()[0].strip().decode('ascii') for line in git_status().splitlines():
status = line.split()[0].strip()
# These are ifs instead of elifs because you might have a file that is # These are ifs instead of elifs because you might have a file that is
# added in the index but deleted on disk, etc. Anyway these numbers # added in the index but deleted on disk, etc. Anyway these numbers
@ -93,15 +108,12 @@ def checkup_committed():
def checkup_pushed(): def checkup_pushed():
details = dotdict.DotDict(default=None) details = dotdict.DotDict(default=None)
command = [GIT, 'rev-parse', '@'] my_head = git_rev_parse('@')
my_head = check_output(command).strip().decode()
command = [GIT, 'rev-parse', '@{u}']
try: try:
remote_head = check_output(command).strip().decode() remote_head = git_rev_parse('@{u}')
except subprocess.CalledProcessError as exc: except subprocess.CalledProcessError as exc:
command = [GIT, 'rev-parse', '--abbrev-ref', 'HEAD'] current_branch = git_current_branch()
current_branch = check_output(command).strip().decode()
details.error = NoUpstreamBranch(current_branch) details.error = NoUpstreamBranch(current_branch)
return details return details
@ -109,8 +121,7 @@ def checkup_pushed():
details.to_push = 0 details.to_push = 0
details.to_pull = 0 details.to_pull = 0
else: else:
command = [GIT, 'merge-base', '@', '@{u}'] merge_base = git_merge_base()
merge_base = check_output(command).strip().decode()
if my_head == merge_base: if my_head == merge_base:
details.to_push = 0 details.to_push = 0