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:26:18 +00:00
|
|
|
import sys
|
2015-08-11 03:04:51 +00:00
|
|
|
import re
|
2015-08-17 09:22:27 +00:00
|
|
|
import shlex
|
2015-08-11 09:29:05 +00:00
|
|
|
from subprocess import Popen, PIPE, check_output
|
2011-04-28 19:05:52 +00:00
|
|
|
|
|
|
|
|
2015-08-17 09:22:27 +00:00
|
|
|
def get_tagname_or_hash():
|
|
|
|
"""return tagname if exists else hash"""
|
|
|
|
cmd = 'git log -1 --format="%h%d"'
|
|
|
|
output = check_output(shlex.split(cmd)).decode('utf-8').strip()
|
|
|
|
hash_, tagname = None, None
|
|
|
|
# get hash
|
|
|
|
m = re.search('\(.*\)$', output)
|
|
|
|
if m:
|
|
|
|
hash_ = output[:m.start()-1]
|
|
|
|
# get tagname
|
|
|
|
m = re.search('tag: .*[,\)]', output)
|
|
|
|
if m:
|
|
|
|
tagname = 'tags/' + output[m.start()+len('tag: '): m.end()-1]
|
|
|
|
|
|
|
|
if tagname:
|
|
|
|
return tagname
|
|
|
|
elif hash_:
|
|
|
|
return hash_
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-08-11 09:29:05 +00:00
|
|
|
# `git status --porcelain --branch` can collect all information
|
2015-08-01 23:50:56 +00:00
|
|
|
# branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind
|
2017-05-13 11:16:53 +00:00
|
|
|
po = Popen(['git', 'status', '--porcelain', '--branch'], env={"LANG": "C"}, stdout=PIPE, stderr=PIPE)
|
2015-08-01 23:50:56 +00:00
|
|
|
stdout, sterr = po.communicate()
|
|
|
|
if po.returncode != 0:
|
|
|
|
sys.exit(0) # Not a git repository
|
2014-02-22 06:47:56 +00:00
|
|
|
|
2015-08-01 23:50:56 +00:00
|
|
|
# collect git status information
|
2015-08-01 16:13:46 +00:00
|
|
|
untracked, staged, changed, conflicts = [], [], [], []
|
2015-08-01 23:50:56 +00:00
|
|
|
ahead, behind = 0, 0
|
2015-08-04 14:34:03 +00:00
|
|
|
status = [(line[0], line[1], line[2:]) for line in stdout.decode('utf-8').splitlines()]
|
2015-08-01 16:13:46 +00:00
|
|
|
for st in status:
|
2015-08-01 23:50:56 +00:00
|
|
|
if st[0] == '#' and st[1] == '#':
|
2015-08-11 03:04:51 +00:00
|
|
|
if re.search('Initial commit on', st[2]):
|
|
|
|
branch = st[2].split(' ')[-1]
|
2015-08-17 09:22:27 +00:00
|
|
|
elif re.search('no branch', st[2]): # detached status
|
|
|
|
branch = get_tagname_or_hash()
|
2015-08-11 03:04:51 +00:00
|
|
|
elif len(st[2].strip().split('...')) == 1:
|
2015-08-01 23:50:56 +00:00
|
|
|
branch = st[2].strip()
|
|
|
|
else:
|
|
|
|
# current and remote branch info
|
|
|
|
branch, rest = st[2].strip().split('...')
|
|
|
|
if len(rest.split(' ')) == 1:
|
|
|
|
# remote_branch = rest.split(' ')[0]
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# ahead or behind
|
|
|
|
divergence = ' '.join(rest.split(' ')[1:])
|
|
|
|
divergence = divergence.lstrip('[').rstrip(']')
|
|
|
|
for div in divergence.split(', '):
|
|
|
|
if 'ahead' in div:
|
|
|
|
ahead = int(div[len('ahead '):].strip())
|
|
|
|
elif 'behind' in div:
|
|
|
|
behind = int(div[len('behind '):].strip())
|
|
|
|
elif st[0] == '?' and st[1] == '?':
|
2015-08-01 16:13:46 +00:00
|
|
|
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
|
|
|
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='')
|