Skip to content

Commit

Permalink
Show tag name when detached status if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Aug 31, 2015
1 parent 825f8ad commit b863cd4
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions gitstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@

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)
Expand All @@ -21,8 +43,8 @@
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']).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:
Expand Down

0 comments on commit b863cd4

Please sign in to comment.