2022-04-09 05:45:42 -07:00
|
|
|
#!/usr/bin/env python3
|
2015-08-01 09:06:36 -07:00
|
|
|
from __future__ import print_function
|
2011-04-28 12:05:52 -07:00
|
|
|
|
2019-04-10 11:30:30 -07:00
|
|
|
import os
|
2015-08-01 09:26:18 -07:00
|
|
|
import sys
|
2015-08-10 20:04:51 -07:00
|
|
|
import re
|
2015-08-11 02:29:05 -07:00
|
|
|
from subprocess import Popen, PIPE, check_output
|
2011-04-28 12:05:52 -07:00
|
|
|
|
|
|
|
|
2015-08-17 02:22:27 -07:00
|
|
|
def get_tagname_or_hash():
|
|
|
|
"""return tagname if exists else hash"""
|
|
|
|
# get hash
|
2019-12-21 00:33:39 -07:00
|
|
|
hash_cmd = ['git', 'rev-parse', '--short', 'HEAD']
|
2020-12-03 04:00:56 -07:00
|
|
|
hash_ = check_output(hash_cmd).decode('utf-8').strip()
|
2019-12-21 00:33:39 -07:00
|
|
|
|
2015-08-17 02:22:27 -07:00
|
|
|
# get tagname
|
2019-12-21 00:33:39 -07:00
|
|
|
tags_cmd = ['git', 'for-each-ref', '--points-at=HEAD', '--count=2', '--sort=-version:refname', '--format=%(refname:short)', 'refs/tags']
|
2020-12-03 04:00:56 -07:00
|
|
|
tags = check_output(tags_cmd).decode('utf-8').split()
|
2015-08-17 02:22:27 -07:00
|
|
|
|
2019-12-21 00:33:39 -07:00
|
|
|
if tags:
|
|
|
|
return tags[0] + ('+' if len(tags) > 1 else '')
|
2015-08-17 02:22:27 -07:00
|
|
|
elif hash_:
|
|
|
|
return hash_
|
|
|
|
return None
|
|
|
|
|
2016-02-25 08:36:31 -07:00
|
|
|
# Re-use method from https://github.com/magicmonty/bash-git-prompt to get stashs count
|
|
|
|
def get_stash():
|
|
|
|
cmd = Popen(['git', 'rev-parse', '--git-dir'], stdout=PIPE, stderr=PIPE)
|
|
|
|
so, se = cmd.communicate()
|
|
|
|
stash_file = '%s%s' % (so.decode('utf-8').rstrip(), '/logs/refs/stash')
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(stash_file) as f:
|
|
|
|
return sum(1 for _ in f)
|
|
|
|
except IOError:
|
|
|
|
return 0
|
|
|
|
|
2015-08-17 02:22:27 -07:00
|
|
|
|
2015-08-11 02:29:05 -07:00
|
|
|
# `git status --porcelain --branch` can collect all information
|
2015-08-01 16:50:56 -07:00
|
|
|
# branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind
|
2019-04-10 11:30:30 -07:00
|
|
|
po = Popen(['git', 'status', '--porcelain', '--branch'], env=dict(os.environ, LANG="C"), stdout=PIPE, stderr=PIPE)
|
2015-08-01 16:50:56 -07:00
|
|
|
stdout, sterr = po.communicate()
|
|
|
|
if po.returncode != 0:
|
|
|
|
sys.exit(0) # Not a git repository
|
2014-02-21 23:47:56 -07:00
|
|
|
|
2015-08-01 16:50:56 -07:00
|
|
|
# collect git status information
|
2022-11-12 03:46:06 -07:00
|
|
|
untracked, staged, changed, deleted, conflicts = [], [], [], [], []
|
2015-08-01 16:50:56 -07:00
|
|
|
ahead, behind = 0, 0
|
2015-08-04 07:34:03 -07:00
|
|
|
status = [(line[0], line[1], line[2:]) for line in stdout.decode('utf-8').splitlines()]
|
2015-08-01 09:13:46 -07:00
|
|
|
for st in status:
|
2015-08-01 16:50:56 -07:00
|
|
|
if st[0] == '#' and st[1] == '#':
|
2018-04-30 07:25:02 -07:00
|
|
|
if re.search('Initial commit on', st[2]) or re.search('No commits yet on', st[2]):
|
2015-08-10 20:04:51 -07:00
|
|
|
branch = st[2].split(' ')[-1]
|
2015-08-17 02:22:27 -07:00
|
|
|
elif re.search('no branch', st[2]): # detached status
|
|
|
|
branch = get_tagname_or_hash()
|
2015-08-10 20:04:51 -07:00
|
|
|
elif len(st[2].strip().split('...')) == 1:
|
2015-08-01 16:50:56 -07: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 09:13:46 -07:00
|
|
|
untracked.append(st)
|
|
|
|
else:
|
|
|
|
if st[1] == 'M':
|
|
|
|
changed.append(st)
|
2022-11-12 03:46:06 -07:00
|
|
|
if st[1] == 'D':
|
|
|
|
deleted.append(st)
|
2015-08-01 09:13:46 -07:00
|
|
|
if st[0] == 'U':
|
|
|
|
conflicts.append(st)
|
|
|
|
elif st[0] != ' ':
|
|
|
|
staged.append(st)
|
2011-04-28 12:05:52 -07:00
|
|
|
|
2016-02-25 08:36:31 -07:00
|
|
|
stashed = get_stash()
|
2022-11-12 03:46:06 -07:00
|
|
|
if not changed and not deleted and not staged and not conflicts and not untracked:
|
2016-02-25 08:36:31 -07:00
|
|
|
clean = 1
|
|
|
|
else:
|
|
|
|
clean = 0
|
|
|
|
|
2015-08-01 09:06:36 -07:00
|
|
|
out = ' '.join([
|
2015-08-01 09:13:46 -07:00
|
|
|
branch,
|
|
|
|
str(ahead),
|
|
|
|
str(behind),
|
|
|
|
str(len(staged)),
|
|
|
|
str(len(conflicts)),
|
|
|
|
str(len(changed)),
|
|
|
|
str(len(untracked)),
|
2016-02-25 08:36:31 -07:00
|
|
|
str(stashed),
|
2022-11-12 03:46:06 -07:00
|
|
|
str(clean),
|
|
|
|
str(len(deleted))
|
2015-08-01 09:13:46 -07:00
|
|
|
])
|
2015-08-01 09:06:36 -07:00
|
|
|
print(out, end='')
|