Repository: pypa/setuptools_scm

Issue:
Regression: hg repos with setuptools_scm>=1.15 are missing dev version bumps
`setuptools_scm` 1.15.* is reporting incorrect version numbers for my hg repositories.

Specifically, a merge commit that follows a tag commit does not seem to trigger the version bump like I would expect. In the screenshot below, for instance, I would expect the tip to be version `1.1.dev[N]+[sha]`. However, versions 1.15.* report the tip's version as `1.0`.

To reproduce:
```bash
mkdir hgmergetest && cd hgmergetest && hg init

# create initial commit and tag it
touch a && hg add a && hg commit -m "initial commit" 
hg tag 1.0

# create a branch
hg branch branch1
touch b && hg add b && hg commit -m "create branch1"

# merge the branch into default
hg update default && hg merge branch1 && hg commit -m "merge branch1 into default"
```

Expected behavior (setuptools_scm 1.13 and 1.14):
```bash
> pip install "setuptools_scm<1.15"
> python -m setuptools_scm
Guessed Version 1.1.dev3+n8dce1535e70a
```

Behavior with `1.15.*`:
```bash
> pip install setuptools_scm==1.15.7
> python -m setuptools_scm
Guessed Version 1.0
```

![image](https://user-images.githubusercontent.com/9388007/37140764-b80a0c36-2267-11e8-87f8-bd52d199140c.png)

Discussion:
avirshup: Here's my stab at a fix for #219. Please let me know if this seems reasonable and/or if there are any changes that should be made.

Patch:
diff --git a/setuptools_scm/hg.py b/setuptools_scm/hg.py
index 9322fb3..0ba1774 100644
--- a/setuptools_scm/hg.py
+++ b/setuptools_scm/hg.py
@@ -8,8 +8,14 @@ FILES_COMMAND = 'hg locate -I .'
 def _hg_tagdist_normalize_tagcommit(root, tag, dist, node):
     dirty = node.endswith('+')
     node = 'h' + node.strip('+')
-    revset = ("(branch(.) and tag({tag!r})::. and file('re:^(?!\.hgtags).*$')"
-              " - tag({tag!r}))").format(tag=tag)
+
+    # Detect changes since the specified tag
+    revset = ("(branch(.)"  # look for revisions in this branch only
+              " and tag({tag!r})::."  # after the last tag
+              # ignore commits that only modify .hgtags and nothing else:
+              " and (merge() or file('re:^(?!\.hgtags).*$'))"
+              " and not tag({tag!r}))"  # ignore the tagged commit itself
+              ).format(tag=tag)
     if tag != '0.0':
         commits = do(['hg', 'log', '-r', revset, '--template', '{node|short}'],
                      root)


Test patch:
diff --git a/testing/test_mercurial.py b/testing/test_mercurial.py
index 1fe6841..1d91444 100644
--- a/testing/test_mercurial.py
+++ b/testing/test_mercurial.py
@@ -108,3 +108,47 @@ def test_version_in_merge(wd):
 def test_parse_no_worktree(tmpdir):
     ret = parse(str(tmpdir))
     assert ret is None
+
+
+@pytest.fixture
+def version_1_0(wd):
+    wd('hg branch default')
+    wd.commit_testfile()
+    wd('hg tag 1.0 -u test -d "0 0"')
+    return wd
+
+
+@pytest.fixture
+def pre_merge_commit_after_tag(wd, version_1_0):
+    wd('hg branch testbranch')
+    wd.write('branchfile', 'branchtext')
+    wd(wd.add_command)
+    wd.commit()
+    wd('hg update default')
+    wd('hg merge testbranch')
+    return wd
+
+
+@pytest.mark.usefixtures("pre_merge_commit_after_tag")
+def test_version_bump_before_merge_commit(wd):
+    assert wd.version.startswith('1.1.dev1+')
+
+
+@pytest.mark.issue(219)
+@pytest.mark.usefixtures("pre_merge_commit_after_tag")
+def test_version_bump_from_merge_commit(wd):
+    wd.commit()
+    assert wd.version.startswith('1.1.dev3+')  # issue 219
+
+
+@pytest.mark.usefixtures("version_1_0")
+def test_version_bump_from_commit_including_hgtag_mods(wd):
+    """ Test the case where a commit includes changes to .hgtags and other files
+    """
+    with wd.cwd.join('.hgtags').open('a') as tagfile:
+        tagfile.write('0  0\n')
+    wd.write('branchfile', 'branchtext')
+    wd(wd.add_command)
+    assert wd.version.startswith('1.1.dev1+')  # bump from dirty version
+    wd.commit()  # commits both the testfile _and_ .hgtags
+    assert wd.version.startswith('1.1.dev2+')