##// END OF EJS Templates
annotate: introduce attr for storing per-line annotate data...
Siddharth Agarwal -
r34433:2e32c6a3 default
parent child Browse files
Show More
@@ -326,12 +326,12 b' def annotate(ui, repo, *pats, **opts):'
326 326 hexfn = rootfm.hexfunc
327 327 formatrev = formathex = pycompat.bytestr
328 328
329 opmap = [('user', ' ', lambda x: x[0].user(), ui.shortuser),
330 ('number', ' ', lambda x: x[0].rev(), formatrev),
331 ('changeset', ' ', lambda x: hexfn(x[0].node()), formathex),
332 ('date', ' ', lambda x: x[0].date(), util.cachefunc(datefunc)),
333 ('file', ' ', lambda x: x[0].path(), str),
334 ('line_number', ':', lambda x: x[1], str),
329 opmap = [('user', ' ', lambda x: x.fctx.user(), ui.shortuser),
330 ('number', ' ', lambda x: x.fctx.rev(), formatrev),
331 ('changeset', ' ', lambda x: hexfn(x.fctx.node()), formathex),
332 ('date', ' ', lambda x: x.fctx.date(), util.cachefunc(datefunc)),
333 ('file', ' ', lambda x: x.fctx.path(), str),
334 ('line_number', ':', lambda x: x.lineno, str),
335 335 ]
336 336 fieldnamemap = {'number': 'rev', 'changeset': 'node'}
337 337
@@ -25,6 +25,9 b' from .node import ('
25 25 wdirnodes,
26 26 wdirrev,
27 27 )
28 from .thirdparty import (
29 attr,
30 )
28 31 from . import (
29 32 encoding,
30 33 error,
@@ -983,10 +986,11 b' class basefilectx(object):'
983 986
984 987 if linenumber:
985 988 def decorate(text, rev):
986 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text)
989 return ([annotateline(fctx=rev, lineno=i)
990 for i in xrange(1, lines(text) + 1)], text)
987 991 else:
988 992 def decorate(text, rev):
989 return ([(rev, False)] * lines(text), text)
993 return ([annotateline(fctx=rev)] * lines(text), text)
990 994
991 995 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
992 996
@@ -1103,6 +1107,11 b' class basefilectx(object):'
1103 1107 """
1104 1108 return self._repo.wwritedata(self.path(), self.data())
1105 1109
1110 @attr.s(slots=True, frozen=True)
1111 class annotateline(object):
1112 fctx = attr.ib()
1113 lineno = attr.ib(default=False)
1114
1106 1115 def _annotatepair(parents, childfctx, child, skipchild, diffopts):
1107 1116 r'''
1108 1117 Given parent and child fctxes and annotate data for parents, for all lines
@@ -1148,7 +1157,7 b' def _annotatepair(parents, childfctx, ch'
1148 1157 for (a1, a2, b1, b2), _t in blocks:
1149 1158 if a2 - a1 >= b2 - b1:
1150 1159 for bk in xrange(b1, b2):
1151 if child[0][bk][0] == childfctx:
1160 if child[0][bk].fctx == childfctx:
1152 1161 ak = min(a1 + (bk - b1), a2 - 1)
1153 1162 child[0][bk] = parent[0][ak]
1154 1163 else:
@@ -1159,7 +1168,7 b' def _annotatepair(parents, childfctx, ch'
1159 1168 for parent, blocks in remaining:
1160 1169 for a1, a2, b1, b2 in blocks:
1161 1170 for bk in xrange(b1, b2):
1162 if child[0][bk][0] == childfctx:
1171 if child[0][bk].fctx == childfctx:
1163 1172 ak = min(a1 + (bk - b1), a2 - 1)
1164 1173 child[0][bk] = parent[0][ak]
1165 1174 return child
@@ -906,7 +906,8 b' def annotate(web, req, tmpl):'
906 906
907 907 previousrev = None
908 908 blockparitygen = paritygen(1)
909 for lineno, ((f, targetline), l) in enumerate(lines):
909 for lineno, (aline, l) in enumerate(lines):
910 f = aline.fctx
910 911 rev = f.rev()
911 912 if rev != previousrev:
912 913 blockhead = True
@@ -924,7 +925,7 b' def annotate(web, req, tmpl):'
924 925 "file": f.path(),
925 926 "blockhead": blockhead,
926 927 "blockparity": blockparity,
927 "targetline": targetline,
928 "targetline": aline.lineno,
928 929 "line": l,
929 930 "lineno": lineno + 1,
930 931 "lineid": "l%d" % (lineno + 1),
@@ -7,6 +7,7 b' from mercurial import ('
7 7 mdiff,
8 8 )
9 9 from mercurial.context import (
10 annotateline,
10 11 _annotatepair,
11 12 )
12 13
@@ -25,50 +26,76 b' class AnnotateTests(unittest.TestCase):'
25 26 diffopts = mdiff.diffopts()
26 27
27 28 def decorate(text, rev):
28 return ([(rev, i) for i in xrange(1, text.count(b'\n') + 1)], text)
29 return ([annotateline(fctx=rev, lineno=i)
30 for i in xrange(1, text.count(b'\n') + 1)],
31 text)
29 32
30 33 # Basic usage
31 34
32 35 oldann = decorate(olddata, oldfctx)
33 36 p1ann = decorate(p1data, p1fctx)
34 37 p1ann = _annotatepair([oldann], p1fctx, p1ann, False, diffopts)
35 self.assertEqual(p1ann[0], [('old', 1), ('old', 2), ('p1', 3)])
38 self.assertEqual(p1ann[0], [
39 annotateline('old', 1),
40 annotateline('old', 2),
41 annotateline('p1', 3),
42 ])
36 43
37 44 p2ann = decorate(p2data, p2fctx)
38 45 p2ann = _annotatepair([oldann], p2fctx, p2ann, False, diffopts)
39 self.assertEqual(p2ann[0], [('old', 1), ('p2', 2), ('p2', 3)])
46 self.assertEqual(p2ann[0], [
47 annotateline('old', 1),
48 annotateline('p2', 2),
49 annotateline('p2', 3),
50 ])
40 51
41 52 # Test with multiple parents (note the difference caused by ordering)
42 53
43 54 childann = decorate(childdata, childfctx)
44 55 childann = _annotatepair([p1ann, p2ann], childfctx, childann, False,
45 56 diffopts)
46 self.assertEqual(childann[0],
47 [('old', 1), ('c', 2), ('p2', 2), ('c', 4), ('p2', 3)]
48 )
57 self.assertEqual(childann[0], [
58 annotateline('old', 1),
59 annotateline('c', 2),
60 annotateline('p2', 2),
61 annotateline('c', 4),
62 annotateline('p2', 3),
63 ])
49 64
50 65 childann = decorate(childdata, childfctx)
51 66 childann = _annotatepair([p2ann, p1ann], childfctx, childann, False,
52 67 diffopts)
53 self.assertEqual(childann[0],
54 [('old', 1), ('c', 2), ('p1', 3), ('c', 4), ('p2', 3)]
55 )
68 self.assertEqual(childann[0], [
69 annotateline('old', 1),
70 annotateline('c', 2),
71 annotateline('p1', 3),
72 annotateline('c', 4),
73 annotateline('p2', 3),
74 ])
56 75
57 76 # Test with skipchild (note the difference caused by ordering)
58 77
59 78 childann = decorate(childdata, childfctx)
60 79 childann = _annotatepair([p1ann, p2ann], childfctx, childann, True,
61 80 diffopts)
62 self.assertEqual(childann[0],
63 [('old', 1), ('old', 2), ('p2', 2), ('p2', 2), ('p2', 3)]
64 )
81 self.assertEqual(childann[0], [
82 annotateline('old', 1),
83 annotateline('old', 2),
84 annotateline('p2', 2),
85 annotateline('p2', 2),
86 annotateline('p2', 3),
87 ])
65 88
66 89 childann = decorate(childdata, childfctx)
67 90 childann = _annotatepair([p2ann, p1ann], childfctx, childann, True,
68 91 diffopts)
69 self.assertEqual(childann[0],
70 [('old', 1), ('old', 2), ('p1', 3), ('p1', 3), ('p2', 3)]
71 )
92 self.assertEqual(childann[0], [
93 annotateline('old', 1),
94 annotateline('old', 2),
95 annotateline('p1', 3),
96 annotateline('p1', 3),
97 annotateline('p2', 3),
98 ])
72 99
73 100 if __name__ == '__main__':
74 101 import silenttestrunner
General Comments 0
You need to be logged in to leave comments. Login now