comparison mercurial/hgweb/webcommands.py @ 35218:d61f2a3d5e53

hgweb: only include graph-related data in jsdata variable on /graph pages (BC) Historically, client-side graph code was not only rendering the graph itself, but it was also adding all of the changeset information to the page as well. It meant that JavaScript code needed to construct valid HTML as a string (although proper escaping was done server-side). It wasn't too clunky, even though it meant that a lot of server-side things were duplicated client-side for no good reason, but the worst thing about it was the data format it used. It was somewhat future-proof, but not human-friendly, because it was just a tuple: it was possible to append things to it (as was done in e.g. 270f57d35525), but you'd then have to remember the indices and reading the resulting JS code wasn't easy, because cur[8] is not descriptive at all. So what would need to happen for graph to have more features, such as more changeset information or a different vertex style (branch-closing, obsolete)? First you'd need to take some property, process it (e.g. escape and pass through templatefilters function, and mind the encoding too), append it to jsdata and remember its index, then go add nearly identical JavaScript code to 4 different hgweb themes that use jsdata to render HTML, and finally try and forget how brittle it all felt. Oh yeah, and the indices go to double digits if we add 2 more items, say phase and obsolescence, and there are more to come. Rendering vertex in a different style would need another property (say, character "o", "_", or "x"), except if you want to be backwards-compatible, it would need to go after tags and bookmarks, and that just doesn't feel right. So here I'm trying to fix both the duplication of code and the data format: - changesets will be rendered by hgweb templates the same way as changelog and other such pages, so jsdata won't need any information that's not needed for rendering the graph itself - jsdata will be a dict, or an Object in JS, which is a lot nicer to humans and is a lot more future-proof in the long run, because it doesn't use numeric indices What about hgweb themes? Obviously, this will break all hgweb themes that render graph in JavaScript, including 3rd-party custom ones. But this will also reduce the size of client-side code and make it more uniform, so that it can be shared across hgweb themes, further reducing its size. The next few patches demonstrate that it's not hard to adapt a theme to these changes. And in a later series, I'm planning to move duplicate JS code from */graph.tmpl to mercurial.js and leave only 4 lines of code embedded in those <script> elements, and even that would be just to allow redefining graph.vertex function. So adapting a custom 3rd-party theme to these changes would mean: - creating or copying graphnode.tmpl and adding it to the map file (if a theme doesn't already use __base__) - modifying one line in graph.tmpl and simply removing the bigger part of JavaScript code from there Making these changes in this patch and not updating every hgweb theme that uses jsdata at the same time is a bit of a cheat to make this series more manageable: /graph pages that use jsdata are broken by this patch, but since there are no tests that would detect this, bisect works fine; and themes are updated separately, in the next 4 patches of this series to ease reviewing.
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 01 Dec 2017 16:00:40 +0800
parents 23bba755cf80
children 1fe3c8296cfe
comparison
equal deleted inserted replaced
35216:fcc96cf0983d 35218:d61f2a3d5e53
34 pycompat, 34 pycompat,
35 revset, 35 revset,
36 revsetlang, 36 revsetlang,
37 scmutil, 37 scmutil,
38 smartset, 38 smartset,
39 templatefilters,
40 templater, 39 templater,
41 url,
42 util, 40 util,
43 ) 41 )
44 42
45 from . import ( 43 from . import (
46 webutil, 44 webutil,
1240 cols = max(cols, max([edge[0] for edge in edges] or [0]), 1238 cols = max(cols, max([edge[0] for edge in edges] or [0]),
1241 max([edge[1] for edge in edges] or [0])) 1239 max([edge[1] for edge in edges] or [0]))
1242 return cols 1240 return cols
1243 1241
1244 def graphdata(usetuples): 1242 def graphdata(usetuples):
1245 # {jsdata} will be passed to |json, so it must be in utf-8
1246 encodestr = encoding.fromlocal
1247 data = [] 1243 data = []
1248 1244
1249 row = 0 1245 row = 0
1250 for (id, type, ctx, vtx, edges) in tree: 1246 for (id, type, ctx, vtx, edges) in tree:
1251 if type != graphmod.CHANGESET: 1247 if type != graphmod.CHANGESET:
1252 continue 1248 continue
1253 1249
1254 if usetuples: 1250 if usetuples:
1255 node = pycompat.bytestr(ctx) 1251 node = pycompat.bytestr(ctx)
1256 age = encodestr(templatefilters.age(ctx.date())) 1252 data.append({'node': node, 'vertex': vtx, 'edges': edges})
1257 desc = templatefilters.firstline(encodestr(ctx.description()))
1258 desc = url.escape(templatefilters.nonempty(desc))
1259 user = templatefilters.person(encodestr(ctx.user()))
1260 user = url.escape(user)
1261 branch = url.escape(encodestr(ctx.branch()))
1262 try:
1263 branchnode = web.repo.branchtip(ctx.branch())
1264 except error.RepoLookupError:
1265 branchnode = None
1266 branch = branch, branchnode == ctx.node()
1267
1268 data.append((node, vtx, edges, desc, user, age, branch,
1269 [url.escape(encodestr(x)) for x in ctx.tags()],
1270 [url.escape(encodestr(x))
1271 for x in ctx.bookmarks()]))
1272 else: 1253 else:
1273 entry = webutil.commonentry(web.repo, ctx) 1254 entry = webutil.commonentry(web.repo, ctx)
1274 edgedata = [{'col': edge[0], 'nextcol': edge[1], 1255 edgedata = [{'col': edge[0], 'nextcol': edge[1],
1275 'color': (edge[2] - 1) % 6 + 1, 1256 'color': (edge[2] - 1) % 6 + 1,
1276 'width': edge[3], 'bcolor': edge[4]} 1257 'width': edge[3], 'bcolor': edge[4]}