Skip to content

Commit 8a65f68

Browse files
committed
Fix for mercurial-4.2
1 parent ce52c5f commit 8a65f68

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ env:
1010
- TOXENV=hg35
1111
- TOXENV=hg36
1212
- TOXENV=hg41
13+
- TOXENV=hg42
1314
- TOXENV=coverage
1415
install: pip install docutils tox
1516
script: tox

src/diff_highlight.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
from hgext import color
18-
from mercurial import extensions
17+
from mercurial import extensions, ui, util
1918
from mercurial.i18n import _
2019
from highlights.pprint import INSERTED, DELETED, pprint_hunk
2120

@@ -25,7 +24,7 @@
2524
DELETE_EMPH = 'diff.deleted_highlight'
2625

2726

28-
class colorui(color.colorui):
27+
class colorui(ui.ui):
2928
hunk = None
3029
tab = None
3130

@@ -89,23 +88,37 @@ def uisetup(ui):
8988
return
9089

9190
try:
92-
extensions.find('color')
93-
except KeyError:
94-
ui.warn(_("warning: 'diff-highlight' requires 'color' extension "
95-
"to be enabled, but not\n"))
96-
return
91+
from hgext import color as colorext
92+
93+
try:
94+
extensions.find('color')
95+
except KeyError:
96+
ui.warn(_("warning: 'diff-highlight' requires 'color' extension "
97+
"to be enabled, but not\n"))
98+
return
99+
except ImportError:
100+
colorext = None
101+
try:
102+
from mercurial import color
103+
except ImportError:
104+
pass
97105

98106
if not isinstance(ui, colorui):
99107
colorui.__bases__ = (ui.__class__,)
100108
ui.__class__ = colorui
101109

110+
ver = tuple(int(s) if s.isdigit() else s for s in util.version().split('.'))
111+
102112
def colorconfig(orig, *args, **kwargs):
103113
ret = orig(*args, **kwargs)
104114

105-
try:
106-
from mercurial.color import _styles as styles
107-
except ImportError:
115+
if ver < (4, 1):
116+
styles = colorext._styles
117+
elif ver < (4, 2):
108118
styles = color._styles
119+
else:
120+
styles = color._defaultstyles
121+
109122
if INSERT_EMPH not in styles:
110123
styles[INSERT_EMPH] = styles[INSERT_NORM] + ' inverse'
111124

@@ -114,4 +127,4 @@ def colorconfig(orig, *args, **kwargs):
114127

115128
return ret
116129

117-
extensions.wrapfunction(color, 'configstyles', colorconfig)
130+
extensions.wrapfunction(colorext if ver < (4, 2) else color, 'configstyles', colorconfig)

tests/test_diff_highlight.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@
77
import unittest
88

99
if sys.version_info < (3, 0):
10+
try:
11+
from hgext import color
12+
13+
colorext = color
14+
except ImportError:
15+
colorext = None
1016
try:
1117
from mercurial import color
1218
except ImportError:
13-
from hgext import color
19+
pass
1420
from diff_highlight import colorui
1521
from mercurial.util import version as mercurial_version
1622
else:
@@ -26,10 +32,18 @@ def test_colorui(self):
2632
curses.setupterm("xterm", 1)
2733
except ImportError:
2834
pass
29-
color._styles['diff.inserted_highlight'] = 'green inverse'
30-
color._styles['diff.deleted_highlight'] = 'red inverse'
3135

3236
ui = colorui()
37+
if mercurial_version() >= "4.2.0":
38+
ui.setconfig('ui', 'color', 'always')
39+
color.setup(ui)
40+
styles = ui._styles
41+
else:
42+
colorui.__bases__ = (colorext.colorui,)
43+
styles = color._styles
44+
styles['diff.inserted_highlight'] = 'green inverse'
45+
styles['diff.deleted_highlight'] = 'red inverse'
46+
3347
if mercurial_version() >= "3.7.0":
3448
ui.pushbuffer(labeled=True)
3549
else:
@@ -48,13 +62,22 @@ def test_colorui(self):
4862
ui.write(" ", '')
4963
ui.write("\n", '')
5064

51-
stop = "\x1b(B\x1b[m"
65+
if mercurial_version() >= "4.2.0":
66+
stop = "\x1b[0m"
67+
68+
def start(*colors):
69+
return "\x1b[0;" + ";".join(str(c) for c in colors) + "m"
70+
71+
def restart(*colors):
72+
return stop + start(*colors)
73+
else:
74+
stop = "\x1b(B\x1b[m"
5275

53-
def start(*colors):
54-
return stop + "".join("\x1b[%dm" % c for c in colors)
76+
def start(*colors):
77+
return stop + "".join("\x1b[%dm" % c for c in colors)
5578

56-
def restart(*colors):
57-
return stop + start(*colors)
79+
def restart(*colors):
80+
return stop + start(*colors)
5881

5982
if mercurial_version() >= "3.7.0":
6083
lines = ui.popbuffer().splitlines()

tox.ini

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist=py26,py27,py32,py33,py34,py35,hg35,hg36,hg41
2+
envlist=py26,py27,py32,py33,py34,py35,hg35,hg36,hg41,hg42
33

44
[testenv]
55
deps=
@@ -71,6 +71,12 @@ deps=
7171
{[testenv:py3_common]deps}
7272
mercurial<4.2
7373

74+
[testenv:hg42]
75+
basepython= python2.7
76+
deps=
77+
{[testenv:py3_common]deps}
78+
mercurial<4.3
79+
7480
[testenv:coverage]
7581
basepython= python2.7
7682
deps=

0 commit comments

Comments
 (0)