2014-07-08 22:49:05 +08:00
|
|
|
#!/usr/bin/env python
|
2011-04-29 03:05:52 +08:00
|
|
|
# -*- coding: UTF-8 -*-
|
2014-02-22 14:47:56 +08:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
import re
|
2011-04-29 03:05:52 +08:00
|
|
|
|
|
|
|
# change those symbols to whatever you prefer
|
2014-02-22 14:47:56 +08:00
|
|
|
symbols = {
|
|
|
|
'ahead of': '↑',
|
|
|
|
'behind': '↓',
|
|
|
|
'staged': '♦',
|
|
|
|
'changed': '‣',
|
|
|
|
'untracked': '…',
|
|
|
|
'clean': '⚡',
|
|
|
|
'unmerged': '≠',
|
|
|
|
'sha1': ':'
|
|
|
|
}
|
2011-04-29 03:05:52 +08:00
|
|
|
|
2014-02-22 14:47:56 +08:00
|
|
|
output, error = Popen(
|
2014-04-19 05:09:38 +08:00
|
|
|
['git', 'status'], stdout=PIPE, stderr=PIPE, universal_newlines=True).communicate()
|
2011-04-29 03:05:52 +08:00
|
|
|
|
|
|
|
if error:
|
2014-02-22 14:47:56 +08:00
|
|
|
import sys
|
|
|
|
sys.exit(0)
|
2011-04-29 03:05:52 +08:00
|
|
|
lines = output.splitlines()
|
|
|
|
|
2014-02-22 14:47:56 +08:00
|
|
|
behead_re = re.compile(
|
|
|
|
r"^# Your branch is (ahead of|behind) '(.*)' by (\d+) commit")
|
2011-04-29 03:05:52 +08: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 14:47:56 +08:00
|
|
|
|
2011-04-29 03:05:52 +08:00
|
|
|
def execute(*command):
|
2014-02-22 14:47:56 +08:00
|
|
|
out, err = Popen(stdout=PIPE, stderr=PIPE, *command).communicate()
|
|
|
|
if not err:
|
|
|
|
nb = len(out.splitlines())
|
|
|
|
else:
|
|
|
|
nb = '?'
|
|
|
|
return nb
|
2011-04-29 03:05:52 +08:00
|
|
|
|
|
|
|
if staged.search(output):
|
2014-02-22 14:47:56 +08:00
|
|
|
nb = execute(
|
|
|
|
['git', 'diff', '--staged', '--name-only', '--diff-filter=ACDMRT'])
|
|
|
|
status += '%s%s' % (symbols['staged'], nb)
|
2011-04-29 03:05:52 +08:00
|
|
|
if unmerged.search(output):
|
2014-02-22 14:47:56 +08:00
|
|
|
nb = execute(['git', 'diff', '--staged', '--name-only', '--diff-filter=U'])
|
|
|
|
status += '%s%s' % (symbols['unmerged'], nb)
|
2011-04-29 03:05:52 +08:00
|
|
|
if changed.search(output):
|
2014-02-22 14:47:56 +08:00
|
|
|
nb = execute(['git', 'diff', '--name-only', '--diff-filter=ACDMRT'])
|
|
|
|
status += '%s%s' % (symbols['changed'], nb)
|
2011-04-29 03:05:52 +08:00
|
|
|
if untracked.search(output):
|
2014-02-22 14:47:56 +08:00
|
|
|
status += symbols['untracked']
|
2011-04-29 03:05:52 +08:00
|
|
|
if status == '':
|
2014-02-22 14:47:56 +08:00
|
|
|
status = symbols['clean']
|
2011-04-29 03:05:52 +08:00
|
|
|
|
|
|
|
remote = ''
|
|
|
|
|
|
|
|
bline = lines[0]
|
|
|
|
if bline.find('Not currently on any branch') != -1:
|
2014-02-22 14:47:56 +08:00
|
|
|
branch = symbols['sha1'] + Popen([
|
|
|
|
'git',
|
|
|
|
'rev-parse',
|
|
|
|
'--short',
|
|
|
|
'HEAD'], stdout=PIPE).communicate()[0][:-1]
|
2011-04-29 03:05:52 +08:00
|
|
|
else:
|
2014-02-22 14:48:38 +08:00
|
|
|
branch = bline.split(' ')[-1]
|
2014-02-22 14:47:56 +08: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-29 03:05:52 +08:00
|
|
|
|
2014-02-22 14:47:56 +08:00
|
|
|
print('\n'.join([branch, remote, status]))
|