Skip to content

Commit 366e658

Browse files
Merge pull request #236 from avirshup/master
hg: use same logic as git for finding latest tagged commit
2 parents 191618e + b207133 commit 366e658

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

setuptools_scm/hg.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,9 @@ def parse(root):
4646
trace('initial node', root)
4747
return meta('0.0', dirty=dirty)
4848

49-
# the newline is needed for merge stae, see issue 72
50-
cmd = 'hg parents --template "{latesttag} {latesttagdistance}\n"'
51-
out = do(cmd, root)
5249
try:
53-
# in merge state we assume parent 1 is fine
54-
tags, dist = out.splitlines()[0].split()
55-
# pick latest tag from tag list
56-
tag = tags.split(':')[-1]
50+
tag = get_latest_normalizable_tag(root)
51+
dist = get_graph_distance(root, tag)
5752
if tag == 'null':
5853
tag = '0.0'
5954
dist = int(dist) + 1
@@ -62,6 +57,23 @@ def parse(root):
6257
pass # unpacking failed, old hg
6358

6459

60+
def get_latest_normalizable_tag(root):
61+
# Gets all tags containing a '.' (see #229) from oldest to newest
62+
cmd = ['hg', 'log',
63+
'-r', "ancestors(.) and tag('re:\.')", '--template', "{tags}\n"]
64+
outlines = do(cmd, root).split()
65+
if not outlines:
66+
return 'null'
67+
tag = outlines[-1].split()[-1]
68+
return tag
69+
70+
71+
def get_graph_distance(root, rev1, rev2='.'):
72+
cmd = ['hg', 'log', '-q', '-r', '%s::%s' % (rev1, rev2)]
73+
out = do(cmd, root)
74+
return len(out.strip().splitlines()) - 1
75+
76+
6577
def archival_to_version(data):
6678
trace('data', data)
6779
node = data.get('node', '')[:12]

testing/test_mercurial.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,13 @@ def test_version_bump_from_commit_including_hgtag_mods(wd):
152152
assert wd.version.startswith('1.1.dev1+') # bump from dirty version
153153
wd.commit() # commits both the testfile _and_ .hgtags
154154
assert wd.version.startswith('1.1.dev2+')
155+
156+
157+
@pytest.mark.issue(229)
158+
@pytest.mark.usefixtures("version_1_0")
159+
def test_latest_tag_detection(wd):
160+
""" Tests that tags not containing a "." are ignored, the same as for git.
161+
Note that will be superceded by the fix for pypa/setuptools_scm/issues/235
162+
"""
163+
wd('hg tag some-random-tag')
164+
assert wd.version == '1.0'

0 commit comments

Comments
 (0)