comparison mercurial/hgweb/webcommands.py @ 18402:bfba6d954108

hgweb: `limit` argument is actually `latestonly` renames and enforce The `limit` argument of several generator have only two possible values in practice: 0 and 1. We rename this parameter to `latestonly` and simplify it's handling. The simplification allows us to save fetching of data that we are sure to not consume. Having a function minimal function perimeter will helps future refactoring.
author Pierre-Yves David <pierre-yves.david@logilab.fr>
date Wed, 16 Jan 2013 12:51:24 +0100
parents e33b9b92a200
children bfaee31a83d2
comparison
equal deleted inserted replaced
18401:683a76a07325 18402:bfba6d954108
192 try: 192 try:
193 ctx = web.repo[hi] 193 ctx = web.repo[hi]
194 except error.RepoError: 194 except error.RepoError:
195 return _search(web, req, tmpl) # XXX redirect to 404 page? 195 return _search(web, req, tmpl) # XXX redirect to 404 page?
196 196
197 def changelist(limit=0, **map): 197 def changelist(latestonly, **map):
198 l = [] # build a list in forward order for efficiency 198 l = [] # build a list in forward order for efficiency
199 for i in xrange(start, end): 199 if latestonly:
200 revs = (end - 1,)
201 else:
202 revs = xrange(start, end)
203 for i in revs:
200 ctx = web.repo[i] 204 ctx = web.repo[i]
201 n = ctx.node() 205 n = ctx.node()
202 showtags = webutil.showtag(web.repo, tmpl, 'changelogtag', n) 206 showtags = webutil.showtag(web.repo, tmpl, 'changelogtag', n)
203 files = webutil.listfilediffs(tmpl, ctx.files(), n, web.maxfiles) 207 files = webutil.listfilediffs(tmpl, ctx.files(), n, web.maxfiles)
204 208
215 "tags": webutil.nodetagsdict(web.repo, n), 219 "tags": webutil.nodetagsdict(web.repo, n),
216 "bookmarks": webutil.nodebookmarksdict(web.repo, n), 220 "bookmarks": webutil.nodebookmarksdict(web.repo, n),
217 "inbranch": webutil.nodeinbranch(web.repo, ctx), 221 "inbranch": webutil.nodeinbranch(web.repo, ctx),
218 "branches": webutil.nodebranchdict(web.repo, ctx) 222 "branches": webutil.nodebranchdict(web.repo, ctx)
219 }) 223 })
220 if limit > 0:
221 l = l[-limit:]
222
223 for e in reversed(l): 224 for e in reversed(l):
224 yield e 225 yield e
225 226
226 revcount = shortlog and web.maxshortchanges or web.maxchanges 227 revcount = shortlog and web.maxshortchanges or web.maxchanges
227 if 'revcount' in req.form: 228 if 'revcount' in req.form:
243 244
244 changenav = webutil.revnavgen(pos, revcount, count, web.repo.changectx) 245 changenav = webutil.revnavgen(pos, revcount, count, web.repo.changectx)
245 246
246 return tmpl(shortlog and 'shortlog' or 'changelog', changenav=changenav, 247 return tmpl(shortlog and 'shortlog' or 'changelog', changenav=changenav,
247 node=ctx.hex(), rev=pos, changesets=count, 248 node=ctx.hex(), rev=pos, changesets=count,
248 entries=lambda **x: changelist(limit=0,**x), 249 entries=lambda **x: changelist(latestonly=False, **x),
249 latestentry=lambda **x: changelist(limit=1,**x), 250 latestentry=lambda **x: changelist(latestonly=True, **x),
250 archives=web.archivelist("tip"), revcount=revcount, 251 archives=web.archivelist("tip"), revcount=revcount,
251 morevars=morevars, lessvars=lessvars) 252 morevars=morevars, lessvars=lessvars)
252 253
253 def shortlog(web, req, tmpl): 254 def shortlog(web, req, tmpl):
254 return changelog(web, req, tmpl, shortlog = True) 255 return changelog(web, req, tmpl, shortlog = True)
399 400
400 def tags(web, req, tmpl): 401 def tags(web, req, tmpl):
401 i = list(reversed(web.repo.tagslist())) 402 i = list(reversed(web.repo.tagslist()))
402 parity = paritygen(web.stripecount) 403 parity = paritygen(web.stripecount)
403 404
404 def entries(notip=False, limit=0, **map): 405 def entries(notip, latestonly, **map):
405 count = 0 406 t = i
406 for k, n in i: 407 if notip:
407 if notip and k == "tip": 408 t = [(k, n) for k, n in i if k != "tip"]
408 continue 409 if latestonly:
409 if limit > 0 and count >= limit: 410 t = t[:1]
410 continue 411 for k, n in t:
411 count = count + 1
412 yield {"parity": parity.next(), 412 yield {"parity": parity.next(),
413 "tag": k, 413 "tag": k,
414 "date": web.repo[n].date(), 414 "date": web.repo[n].date(),
415 "node": hex(n)} 415 "node": hex(n)}
416 416
417 return tmpl("tags", 417 return tmpl("tags",
418 node=hex(web.repo.changelog.tip()), 418 node=hex(web.repo.changelog.tip()),
419 entries=lambda **x: entries(False, 0, **x), 419 entries=lambda **x: entries(False, False, **x),
420 entriesnotip=lambda **x: entries(True, 0, **x), 420 entriesnotip=lambda **x: entries(True, False, **x),
421 latestentry=lambda **x: entries(True, 1, **x)) 421 latestentry=lambda **x: entries(True, True, **x))
422 422
423 def bookmarks(web, req, tmpl): 423 def bookmarks(web, req, tmpl):
424 i = web.repo._bookmarks.items() 424 i = web.repo._bookmarks.items()
425 parity = paritygen(web.stripecount) 425 parity = paritygen(web.stripecount)
426 426
427 def entries(limit=0, **map): 427 def entries(latestonly, **map):
428 count = 0 428 if latestonly:
429 for k, n in sorted(i): 429 t = [min(i)]
430 if limit > 0 and count >= limit: 430 else:
431 continue 431 t = sorted(i)
432 count = count + 1 432 for k, n in t:
433 yield {"parity": parity.next(), 433 yield {"parity": parity.next(),
434 "bookmark": k, 434 "bookmark": k,
435 "date": web.repo[n].date(), 435 "date": web.repo[n].date(),
436 "node": hex(n)} 436 "node": hex(n)}
437 437
438 return tmpl("bookmarks", 438 return tmpl("bookmarks",
439 node=hex(web.repo.changelog.tip()), 439 node=hex(web.repo.changelog.tip()),
440 entries=lambda **x: entries(0, **x), 440 entries=lambda **x: entries(latestonly=False, **x),
441 latestentry=lambda **x: entries(1, **x)) 441 latestentry=lambda **x: entries(latestonly=True, **x))
442 442
443 def branches(web, req, tmpl): 443 def branches(web, req, tmpl):
444 tips = [] 444 tips = []
445 heads = web.repo.heads() 445 heads = web.repo.heads()
446 parity = paritygen(web.stripecount) 446 parity = paritygen(web.stripecount)
739 count = fctx.filerev() + 1 739 count = fctx.filerev() + 1
740 start = max(0, fctx.filerev() - revcount + 1) # first rev on this page 740 start = max(0, fctx.filerev() - revcount + 1) # first rev on this page
741 end = min(count, start + revcount) # last rev on this page 741 end = min(count, start + revcount) # last rev on this page
742 parity = paritygen(web.stripecount, offset=start - end) 742 parity = paritygen(web.stripecount, offset=start - end)
743 743
744 def entries(limit=0, **map): 744 def entries(latestonly, **map):
745 l = [] 745 l = []
746 746
747 repo = web.repo 747 repo = web.repo
748 for i in xrange(start, end): 748 if latestonly:
749 revs = (end - 1,)
750 else:
751 revs = xrange(start, end)
752 for i in revs:
749 iterfctx = fctx.filectx(i) 753 iterfctx = fctx.filectx(i)
750 754
751 l.append({"parity": parity.next(), 755 l.append({"parity": parity.next(),
752 "filerev": i, 756 "filerev": i,
753 "file": f, 757 "file": f,
762 "bookmarks": webutil.nodebookmarksdict( 766 "bookmarks": webutil.nodebookmarksdict(
763 repo, iterfctx.node()), 767 repo, iterfctx.node()),
764 "branch": webutil.nodebranchnodefault(iterfctx), 768 "branch": webutil.nodebranchnodefault(iterfctx),
765 "inbranch": webutil.nodeinbranch(repo, iterfctx), 769 "inbranch": webutil.nodeinbranch(repo, iterfctx),
766 "branches": webutil.nodebranchdict(repo, iterfctx)}) 770 "branches": webutil.nodebranchdict(repo, iterfctx)})
767
768 if limit > 0:
769 l = l[-limit:]
770
771 for e in reversed(l): 771 for e in reversed(l):
772 yield e 772 yield e
773 773
774 nodefunc = lambda x: fctx.filectx(fileid=x) 774 nodefunc = lambda x: fctx.filectx(fileid=x)
775 nav = webutil.revnavgen(end - 1, revcount, count, nodefunc) 775 nav = webutil.revnavgen(end - 1, revcount, count, nodefunc)
776 return tmpl("filelog", file=f, node=fctx.hex(), nav=nav, 776 return tmpl("filelog", file=f, node=fctx.hex(), nav=nav,
777 entries=lambda **x: entries(limit=0, **x), 777 entries=lambda **x: entries(latestonly=False, **x),
778 latestentry=lambda **x: entries(limit=1, **x), 778 latestentry=lambda **x: entries(latestonly=True, **x),
779 revcount=revcount, morevars=morevars, lessvars=lessvars) 779 revcount=revcount, morevars=morevars, lessvars=lessvars)
780 780
781 def archive(web, req, tmpl): 781 def archive(web, req, tmpl):
782 type_ = req.form.get('type', [None])[0] 782 type_ = req.form.get('type', [None])[0]
783 allowed = web.configlist("web", "allow_archive") 783 allowed = web.configlist("web", "allow_archive")