Skip to content

Commit e89f522

Browse files
committed
Add "-dirty" flag to versions build out of uncommitted source
1 parent ecb8a8a commit e89f522

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/lsd/version.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,27 @@ def _minimal_ext_cmd(cmd):
1717
env['LANGUAGE'] = 'C'
1818
env['LANG'] = 'C'
1919
env['LC_ALL'] = 'C'
20-
out = subprocess.Popen(cmd, stdout = subprocess.PIPE, env=env).communicate()[0]
20+
out = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=open('/dev/null', 'w'), env=env).communicate()[0]
2121
return out
2222

2323
import os, subprocess
2424
base, _ = os.path.split(__file__)
2525

2626
tag = "<<unknown>>"
2727
try:
28-
cmd = ['git', '--work-tree', '%s/../..' % base, 'describe', '--long', '--abbrev=8']
28+
# Have to call 'git status' first, because of a git bug where it incorrectly detects
29+
# a tree as dirty under conditions triggered by things like './setup.py sdist'
30+
cmd = ['git', '--work-tree', '%s/../..' % base, 'status']
31+
_minimal_ext_cmd(cmd)
32+
33+
# Try with the --dirty flag (newer versions of git support this)
34+
cmd = ['git', '--work-tree', '%s/../..' % base, 'describe', '--long', '--abbrev=8', '--dirty']
2935
tag = _minimal_ext_cmd(cmd)
36+
37+
if tag == "":
38+
# Try without the --dirty flag (for older versions of git)
39+
cmd = ['git', '--work-tree', '%s/../..' % base, 'describe', '--long', '--abbrev=8']
40+
tag = _minimal_ext_cmd(cmd)
3041
except OSError:
3142
pass
3243

@@ -36,17 +47,19 @@ def _minimal_ext_cmd(cmd):
3647
if tag[:1] == 'v':
3748
tag = tag[1:]
3849
# Parse the tag into components
39-
(version, additional_commits, hash) = tag.split('-')
50+
tagparts = tag.split('-')
51+
(version, additional_commits, hash) = tagparts[:3]
52+
dirty = len(tagparts) > 3
4053
ver_tuple = tuple( int(v) for v in version.split('.') )
4154
additional_commits = int(additional_commits)
4255
hash = hash[1:]
4356

44-
if additional_commits == 0:
57+
if additional_commits == 0 and not dirty:
4558
__version__ = version
4659
else:
4760
__version__ = tag
4861

49-
__version_info__ = (ver_tuple, additional_commits, hash)
62+
__version_info__ = (ver_tuple, additional_commits, hash, dirty)
5063
else:
5164
__version__ = tag
52-
__version_info__ = ((99, 99, 99), 99, '0'*8)
65+
__version_info__ = ((99, 99, 99), 99, '0'*8, True)

0 commit comments

Comments
 (0)