c4ba3065a1e9beb76f2f0ac64f3436b5448576b2

Author
Kentaro Wada <www.kentaro.wada@gmail.com>
Committer
Kentaro Wada <www.kentaro.wada@gmail.com>
Date

Message

Show tag name when detached status if possible

Diff

 1diff --git a/plugins/git-prompt/gitstatus.py b/plugins/git-prompt/gitstatus.py
 2index 1beae541a770ef6267cc63dba1a92230a41012e3..a8eb8284b7ad8ed601890d84f0ffbfe92f6c00a1 100644
 3--- a/plugins/git-prompt/gitstatus.py
 4+++ b/plugins/git-prompt/gitstatus.py
 5@@ -3,9 +3,31 @@ from __future__ import print_function
 6 
 7 import sys
 8 import re
 9+import shlex
10 from subprocess import Popen, PIPE, check_output
11 
12 
13+def get_tagname_or_hash():
14+    """return tagname if exists else hash"""
15+    cmd = 'git log -1 --format="%h%d"'
16+    output = check_output(shlex.split(cmd)).decode('utf-8').strip()
17+    hash_, tagname = None, None
18+    # get hash
19+    m = re.search('\(.*\)$', output)
20+    if m:
21+        hash_ = output[:m.start()-1]
22+    # get tagname
23+    m = re.search('tag: .*[,\)]', output)
24+    if m:
25+        tagname = 'tags/' + output[m.start()+len('tag: '): m.end()-1]
26+
27+    if tagname:
28+        return tagname
29+    elif hash_:
30+        return hash_
31+    return None
32+
33+
34 # `git status --porcelain --branch` can collect all information
35 # branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind
36 po = Popen(['git', 'status', '--porcelain', '--branch'], stdout=PIPE, stderr=PIPE)
37@@ -21,8 +43,8 @@ for st in status:
38     if st[0] == '#' and st[1] == '#':
39         if re.search('Initial commit on', st[2]):
40             branch = st[2].split(' ')[-1]
41-        elif re.search('no branch', st[2]):
42-            branch = check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip()
43+        elif re.search('no branch', st[2]):  # detached status
44+            branch = get_tagname_or_hash()
45         elif len(st[2].strip().split('...')) == 1:
46             branch = st[2].strip()
47         else: