2014-06-04 10:36:34 +00:00
|
|
|
#!/usr/bin/env python2
|
2011-04-28 19:05:52 +00:00
|
|
|
# -*- coding: UTF-8 -*-
|
2014-02-22 06:47:56 +00:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
import re
|
2011-04-28 19:05:52 +00:00
|
|
|
|
|
|
|
# change those symbols to whatever you prefer
|
2014-02-22 06:47:56 +00:00
|
|
|
symbols = {
|
|
|
|
'ahead of': '↑',
|
|
|
|
'behind': '↓',
|
|
|
|
'staged': '♦',
|
|
|
|
'changed': '‣',
|
|
|
|
'untracked': '…',
|
|
|
|
'clean': '⚡',
|
|
|
|
'unmerged': '≠',
|
|
|
|
'sha1': ':'
|
|
|
|
}
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2014-02-22 06:47:56 +00:00
|
|
|
output, error = Popen(
|
2014-04-18 21:09:38 +00:00
|
|
|
['git', 'status'], stdout=PIPE, stderr=PIPE, universal_newlines=True).communicate()
|
2011-04-28 19:05:52 +00:00
|
|
|
|
|
|
|
if error:
|
2014-02-22 06:47:56 +00:00
|
|
|
import sys
|
|
|
|
sys.exit(0)
|
2011-04-28 19:05:52 +00:00
|
|
|
lines = output.splitlines()
|
|
|
|
|
2014-02-22 06:47:56 +00:00
|
|
|
behead_re = re.compile(
|
|
|
|
r"^# Your branch is (ahead of|behind) '(.*)' by (\d+) commit")
|
2011-04-28 19:05:52 +00:00
|
|
|
diverge_re = re.compile(r"^# and have (\d+) and (\d+) different")
|
|
|
|
|
|
|
|
status = ''
|
|
|
|
staged = re.compile(r'^# Changes to be committed:$', re.MULTILINE)
|
|
|
|
changed = re.compile(r'^# Changed but not updated:$', re.MULTILINE)
|
|
|
|
untracked = re.compile(r'^# Untracked files:$', re.MULTILINE)
|
|
|
|
unmerged = re.compile(r'^# Unmerged paths:$', re.MULTILINE)
|
|
|
|
|
2014-02-22 06:47:56 +00:00
|
|
|
|
2011-04-28 19:05:52 +00:00
|
|
|
def execute(*command):
|
2014-02-22 06:47:56 +00:00
|
|
|
out, err = Popen(stdout=PIPE, stderr=PIPE, *command).communicate()
|
|
|
|
if not err:
|
|
|
|
nb = len(out.splitlines())
|
|
|
|
else:
|
|
|
|
nb = '?'
|
|
|
|
return nb
|
2011-04-28 19:05:52 +00:00
|
|
|
|
|
|
|
if staged.search(output):
|
2014-02-22 06:47:56 +00:00
|
|
|
nb = execute(
|
|
|
|
['git', 'diff', '--staged', '--name-only', '--diff-filter=ACDMRT'])
|
|
|
|
status += '%s%s' % (symbols['staged'], nb)
|
2011-04-28 19:05:52 +00:00
|
|
|
if unmerged.search(output):
|
2014-02-22 06:47:56 +00:00
|
|
|
nb = execute(['git', 'diff', '--staged', '--name-only', '--diff-filter=U'])
|
|
|
|
status += '%s%s' % (symbols['unmerged'], nb)
|
2011-04-28 19:05:52 +00:00
|
|
|
if changed.search(output):
|
2014-02-22 06:47:56 +00:00
|
|
|
nb = execute(['git', 'diff', '--name-only', '--diff-filter=ACDMRT'])
|
|
|
|
status += '%s%s' % (symbols['changed'], nb)
|
2011-04-28 19:05:52 +00:00
|
|
|
if untracked.search(output):
|
2014-02-22 06:47:56 +00:00
|
|
|
status += symbols['untracked']
|
2011-04-28 19:05:52 +00:00
|
|
|
if status == '':
|
2014-02-22 06:47:56 +00:00
|
|
|
status = symbols['clean']
|
2011-04-28 19:05:52 +00:00
|
|
|
|
|
|
|
remote = ''
|
|
|
|
|
|
|
|
bline = lines[0]
|
|
|
|
if bline.find('Not currently on any branch') != -1:
|
2014-02-22 06:47:56 +00:00
|
|
|
branch = symbols['sha1'] + Popen([
|
|
|
|
'git',
|
|
|
|
'rev-parse',
|
|
|
|
'--short',
|
|
|
|
'HEAD'], stdout=PIPE).communicate()[0][:-1]
|
2011-04-28 19:05:52 +00:00
|
|
|
else:
|
2014-02-22 06:48:38 +00:00
|
|
|
branch = bline.split(' ')[-1]
|
2014-02-22 06:47:56 +00:00
|
|
|
bstatusline = lines[1]
|
|
|
|
match = behead_re.match(bstatusline)
|
|
|
|
if match:
|
|
|
|
remote = symbols[match.groups()[0]]
|
|
|
|
remote += match.groups()[2]
|
|
|
|
elif lines[2:]:
|
|
|
|
div_match = diverge_re.match(lines[2])
|
|
|
|
if div_match:
|
|
|
|
remote = "{behind}{1}{ahead of}{0}".format(
|
|
|
|
*div_match.groups(), **symbols)
|
2011-04-28 19:05:52 +00:00
|
|
|
|
2014-02-22 06:47:56 +00:00
|
|
|
print('\n'.join([branch, remote, status]))
|