mirror of
https://github.com/b-ryan/powerline-shell.git
synced 2026-09-10 07:26:28 -04:00
We must be sure we are able to parse the git status output. If we don't use this while calling popen we will get language specific output that we couldn't parse.
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import re
|
|
import subprocess
|
|
|
|
def get_git_status():
|
|
has_pending_commits = True
|
|
has_untracked_files = False
|
|
origin_position = ""
|
|
output = subprocess.Popen(['git', 'status', '--ignore-submodules'],
|
|
env={"LANG": "C"}, stdout=subprocess.PIPE).communicate()[0]
|
|
for line in output.split('\n'):
|
|
origin_status = re.findall(
|
|
r"Your branch is (ahead|behind).*?(\d+) comm", line)
|
|
if origin_status:
|
|
origin_position = " %d" % int(origin_status[0][1])
|
|
if origin_status[0][0] == 'behind':
|
|
origin_position += u'\u21E3'
|
|
if origin_status[0][0] == 'ahead':
|
|
origin_position += u'\u21E1'
|
|
|
|
if line.find('nothing to commit') >= 0:
|
|
has_pending_commits = False
|
|
if line.find('Untracked files') >= 0:
|
|
has_untracked_files = True
|
|
return has_pending_commits, has_untracked_files, origin_position
|
|
|
|
|
|
def add_git_segment():
|
|
# See http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
|
|
p = subprocess.Popen(['git', 'symbolic-ref', '-q', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
out, err = p.communicate()
|
|
|
|
if 'Not a git repo' in err:
|
|
return
|
|
|
|
if out:
|
|
branch = out[len('refs/heads/'):].rstrip()
|
|
else:
|
|
branch = '(Detached)'
|
|
|
|
has_pending_commits, has_untracked_files, origin_position = get_git_status()
|
|
branch += origin_position
|
|
if has_untracked_files:
|
|
branch += ' +'
|
|
|
|
bg = Color.REPO_CLEAN_BG
|
|
fg = Color.REPO_CLEAN_FG
|
|
if has_pending_commits:
|
|
bg = Color.REPO_DIRTY_BG
|
|
fg = Color.REPO_DIRTY_FG
|
|
|
|
powerline.append(' %s ' % branch, fg, bg)
|
|
|
|
try:
|
|
add_git_segment()
|
|
except OSError:
|
|
pass
|
|
except subprocess.CalledProcessError:
|
|
pass
|