|
5 | 5 | prehash = ':' |
6 | 6 |
|
7 | 7 | import sys |
| 8 | +import re |
| 9 | +import subprocess |
8 | 10 | from subprocess import Popen, PIPE |
9 | 11 |
|
10 | 12 |
|
| 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 | + |
11 | 24 | # `git status --porcelain -b` can collect all information |
12 | 25 | # branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind |
13 | 26 | po = Popen(['git', 'status', '--porcelain', '-b'], stdout=PIPE, stderr=PIPE) |
|
21 | 34 | status = [(line[0], line[1], line[2:]) for line in stdout.decode('utf-8').splitlines()] |
22 | 35 | for st in status: |
23 | 36 | 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: |
25 | 40 | branch = st[2].strip() |
| 41 | + if branch == 'HEAD (no branch)': |
| 42 | + branch = get_detached_status_name() |
26 | 43 | else: |
27 | 44 | # current and remote branch info |
28 | 45 | branch, rest = st[2].strip().split('...') |
|
0 commit comments