diff options
| author | Marc Cornellà <marc.cornella@live.com> | 2015-08-17 12:43:23 +0200 | 
|---|---|---|
| committer | Marc Cornellà <marc.cornella@live.com> | 2015-08-17 12:43:23 +0200 | 
| commit | 9121f3e9d7719a9bbfa638d7e889d534c5359100 (patch) | |
| tree | 3e1d802f35472bc0d456deaac612d32aeafc559d /plugins | |
| parent | 3c698743fa4265ab2b70b880e12f957792669438 (diff) | |
| parent | c4ba3065a1e9beb76f2f0ac64f3436b5448576b2 (diff) | |
| download | zsh-9121f3e9d7719a9bbfa638d7e889d534c5359100.tar.gz zsh-9121f3e9d7719a9bbfa638d7e889d534c5359100.tar.bz2 zsh-9121f3e9d7719a9bbfa638d7e889d534c5359100.zip | |
Merge pull request #4 from wkentaro/fix-git-prompt-plugin
Show tag name when detached status if possible
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/git-prompt/gitstatus.py | 26 | 
1 files changed, 24 insertions, 2 deletions
| diff --git a/plugins/git-prompt/gitstatus.py b/plugins/git-prompt/gitstatus.py index 1beae541a..a8eb8284b 100644 --- a/plugins/git-prompt/gitstatus.py +++ b/plugins/git-prompt/gitstatus.py @@ -3,9 +3,31 @@ from __future__ import print_function  import sys  import re +import shlex  from subprocess import Popen, PIPE, check_output +def get_tagname_or_hash(): +    """return tagname if exists else hash""" +    cmd = 'git log -1 --format="%h%d"' +    output = check_output(shlex.split(cmd)).decode('utf-8').strip() +    hash_, tagname = None, None +    # get hash +    m = re.search('\(.*\)$', output) +    if m: +        hash_ = output[:m.start()-1] +    # get tagname +    m = re.search('tag: .*[,\)]', output) +    if m: +        tagname = 'tags/' + output[m.start()+len('tag: '): m.end()-1] + +    if tagname: +        return tagname +    elif hash_: +        return hash_ +    return None + +  # `git status --porcelain --branch` can collect all information  # branch, remote_branch, untracked, staged, changed, conflicts, ahead, behind  po = Popen(['git', 'status', '--porcelain', '--branch'], stdout=PIPE, stderr=PIPE) @@ -21,8 +43,8 @@ for st in status:      if st[0] == '#' and st[1] == '#':          if re.search('Initial commit on', st[2]):              branch = st[2].split(' ')[-1] -        elif re.search('no branch', st[2]): -            branch = check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip() +        elif re.search('no branch', st[2]):  # detached status +            branch = get_tagname_or_hash()          elif len(st[2].strip().split('...')) == 1:              branch = st[2].strip()          else: | 
