e204c596ef5a1075914c5e7810ec1eba4bbe7411

Author
lieryan <lie.1296@gmail.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Rewrite gitstatus collection to be more robust (#7322)

Fix the finicky parsing logic and just ask git the necessary information
directly.

Diff

 1diff --git a/plugins/git-prompt/gitstatus.py b/plugins/git-prompt/gitstatus.py
 2index 390a50a6f491804a5b8d10f5f5a5a26ffa753fe8..300365d71f5c9aca9926e30a85de4d5a0bdf77ed 100644
 3--- a/plugins/git-prompt/gitstatus.py
 4+++ b/plugins/git-prompt/gitstatus.py
 5@@ -4,26 +4,21 @@ from __future__ import print_function
 6 import os
 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+    hash_cmd = ['git', 'rev-parse', '--short', 'HEAD']
23+    hash_ = check_output(hash_cmd).strip()
24+
25     # get tagname
26-    m = re.search('tag: .*[,\)]', output)
27-    if m:
28-        tagname = 'tags/' + output[m.start()+len('tag: '): m.end()-1]
29+    tags_cmd = ['git', 'for-each-ref', '--points-at=HEAD', '--count=2', '--sort=-version:refname', '--format=%(refname:short)', 'refs/tags']
30+    tags = check_output(tags_cmd).split()
31 
32-    if tagname:
33-        return tagname.replace(' ', '')
34+    if tags:
35+        return tags[0] + ('+' if len(tags) > 1 else '')
36     elif hash_:
37         return hash_
38     return None