2014-07-08 14:49:05 +00:00
|
|
|
#!/usr/bin/env python
|
2015-08-01 16:06:36 +00:00
|
|
|
from __future__ import print_function
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
# change this symbol to whatever you prefer
|
|
|
|
prehash = ':'
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:13:46 +00:00
|
|
|
import subprocess
|
2015-08-01 16:06:36 +00:00
|
|
|
from subprocess import Popen, PIPE
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
import sys
|
|
|
|
gitsym = Popen(['git', 'symbolic-ref', 'HEAD'], stdout=PIPE, stderr=PIPE)
|
|
|
|
branch, error = gitsym.communicate()
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
error_string = error.decode('utf-8')
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
if 'fatal: Not a git repository' in error_string:
|
2015-08-01 16:13:46 +00:00
|
|
|
sys.exit(0)
|
2014-02-22 06:47:56 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
branch = branch.decode("utf-8").strip()[11:]
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:13:46 +00:00
|
|
|
# Get git status (staged, change, conflicts and untracked)
|
|
|
|
try:
|
|
|
|
res = subprocess.check_output(['git', 'status', '--porcelain'])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
sys.exit(0)
|
|
|
|
status = [(st[0], st[1], st[2:]) for st in res.splitlines()]
|
|
|
|
untracked, staged, changed, conflicts = [], [], [], []
|
|
|
|
for st in status:
|
|
|
|
if st[0] == '?' and st[1] == '?':
|
|
|
|
untracked.append(st)
|
|
|
|
else:
|
|
|
|
if st[1] == 'M':
|
|
|
|
changed.append(st)
|
|
|
|
if st[0] == 'U':
|
|
|
|
conflicts.append(st)
|
|
|
|
elif st[0] != ' ':
|
|
|
|
staged.append(st)
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
ahead, behind = 0,0
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2015-08-01 16:06:36 +00:00
|
|
|
if not branch: # not on any branch
|
2015-08-01 16:13:46 +00:00
|
|
|
branch = prehash + Popen(['git','rev-parse','--short','HEAD'], stdout=PIPE).communicate()[0].decode("utf-8")[:-1]
|
2011-04-28 19:05:52 +00:00
|
|
|
else:
|
2015-08-01 16:13:46 +00:00
|
|
|
remote_name = Popen(['git','config','branch.%s.remote' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
|
|
|
|
if remote_name:
|
|
|
|
merge_name = Popen(['git','config','branch.%s.merge' % branch], stdout=PIPE).communicate()[0].decode("utf-8").strip()
|
|
|
|
if remote_name == '.': # local
|
|
|
|
remote_ref = merge_name
|
|
|
|
else:
|
|
|
|
remote_ref = 'refs/remotes/%s/%s' % (remote_name, merge_name[11:])
|
|
|
|
revgit = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % remote_ref],stdout=PIPE, stderr=PIPE)
|
|
|
|
revlist = revgit.communicate()[0]
|
|
|
|
if revgit.poll(): # fallback to local
|
|
|
|
revlist = Popen(['git', 'rev-list', '--left-right', '%s...HEAD' % merge_name],stdout=PIPE, stderr=PIPE).communicate()[0]
|
|
|
|
behead = revlist.decode("utf-8").splitlines()
|
|
|
|
ahead = len([x for x in behead if x[0]=='>'])
|
|
|
|
behind = len(behead) - ahead
|
2015-08-01 16:06:36 +00:00
|
|
|
|
|
|
|
out = ' '.join([
|
2015-08-01 16:13:46 +00:00
|
|
|
branch,
|
|
|
|
str(ahead),
|
|
|
|
str(behind),
|
|
|
|
str(len(staged)),
|
|
|
|
str(len(conflicts)),
|
|
|
|
str(len(changed)),
|
|
|
|
str(len(untracked)),
|
|
|
|
])
|
2015-08-01 16:06:36 +00:00
|
|
|
print(out, end='')
|
2011-04-28 19:05:52 +00:00
|
|
|
|