Skip to content

Commit 884a449

Browse files
committed
Fix for initial or detached status branch info
1 parent ce669f1 commit 884a449

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

gitstatus.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
prehash = ':'
66

77
import sys
8+
import re
9+
import subprocess
810
from subprocess import Popen, PIPE
911

1012

13+
def get_detached_status_name():
14+
cmd = ['git', 'describe', '--all']
15+
output = subprocess.check_output(cmd).strip()
16+
tag_hash = output.split('-g')
17+
if len(tag_hash) == 1:
18+
return tag_hash[0] # tag
19+
elif len(tag_hash) == 2:
20+
return tag_hash[1] # hash
21+
return output # unexpected status
22+
23+
1124
# `git status --porcelain -b` can collect all information
1225
# branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind
1326
po = Popen(['git', 'status', '--porcelain', '-b'], stdout=PIPE, stderr=PIPE)
@@ -21,8 +34,12 @@
2134
status = [(line[0], line[1], line[2:]) for line in stdout.decode('utf-8').splitlines()]
2235
for st in status:
2336
if st[0] == '#' and st[1] == '#':
24-
if len(st[2].strip().split('...')) == 1:
37+
if re.search('Initial commit on', st[2]):
38+
branch = st[2].split(' ')[-1]
39+
elif len(st[2].strip().split('...')) == 1:
2540
branch = st[2].strip()
41+
if branch == 'HEAD (no branch)':
42+
branch = get_detached_status_name()
2643
else:
2744
# current and remote branch info
2845
branch, rest = st[2].strip().split('...')

0 commit comments

Comments
 (0)