annotate mercurial/hgweb/webutil.py @ 18388:083daee1b749

hgweb: simplify return value creation in for navgen We now have access to better syntax allowing a clearer version.
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Mon, 14 Jan 2013 16:52:35 +0100
parents 39616410aa32
children 82572533bc00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
1 # hgweb/webutil.py - utility library for the web interface.
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
2 #
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
5 #
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7717
diff changeset
6 # This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9402
diff changeset
7 # GNU General Public License version 2 or any later version.
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
8
17302
5c64ce6168da hgweb: fixes traceback for invalid files by removing top-level template
wujek srujek <wujek.srujek@googlemail.com>
parents: 17289
diff changeset
9 import os, copy
14490
1d3e2349304a web: provide diffstat to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14055
diff changeset
10 from mercurial import match, patch, scmutil, error, ui, util
14570
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
11 from mercurial.i18n import _
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
12 from mercurial.node import hex, nullid
17289
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
13 from common import ErrorResponse
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
14 from common import HTTP_NOT_FOUND
17202
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
15 import difflib
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
16
6393
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
17 def up(p):
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
18 if p[0] != "/":
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
19 p = "/" + p
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
20 if p[-1] == "/":
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
21 p = p[:-1]
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
22 up = os.path.dirname(p)
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
23 if up == "/":
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
24 return "/"
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
25 return up + "/"
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
26
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
27 def revnavgen(pos, pagelen, limit, nodefunc):
18320
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
28 """computes label and revision id for navigation link
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
29
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
30 :pos: is the revision relative to which we generate navigation.
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
31 :pagelen: the size of each navigation page
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
32 :limit: how far shall we link
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
33 :nodefun: factory for a changectx from a revision
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
34
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
35 The return is:
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
36 - a single element tuple
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
37 - containing a dictionary with a `before` and `after` key
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
38 - values are generator functions taking an arbitrary number of kwargs
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
39 - yield items are dictionaries with `label` and `node` keys
60680d691a0b hgweb: document the revnavgen function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17991
diff changeset
40 """
6393
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
41 def seq(factor, limit=None):
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
42 if limit:
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
43 yield limit
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
44 if limit >= 20 and limit <= 40:
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
45 yield 50
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
46 else:
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
47 yield 1 * factor
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
48 yield 3 * factor
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
49 for f in seq(factor * 10):
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
50 yield f
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
51
10254
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
52 navbefore = []
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
53 navafter = []
6393
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
54
10254
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
55 last = 0
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
56 for f in seq(1, pagelen):
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
57 if f < pagelen or f <= last:
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
58 continue
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
59 if f > limit:
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
60 break
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
61 last = f
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
62 if pos + f < limit:
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
63 navafter.append(("+%d" % f, hex(nodefunc(pos + f).node())))
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
64 if pos - f >= 0:
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
65 navbefore.insert(0, ("-%d" % f, hex(nodefunc(pos - f).node())))
6393
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
66
10254
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
67 navafter.append(("tip", "tip"))
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
68 try:
18387
39616410aa32 hgweb: do not access first changeset with a string
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18367
diff changeset
69 navbefore.insert(0, ("(0)", hex(nodefunc(0).node())))
10254
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
70 except error.RepoError:
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
71 pass
8d5de52431f2 hgweb: changenav: separate pages before and after the current position
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9402
diff changeset
72
18388
083daee1b749 hgweb: simplify return value creation in for navgen
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18387
diff changeset
73 data = lambda i: {"label": i[0], "node": i[1]}
083daee1b749 hgweb: simplify return value creation in for navgen
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18387
diff changeset
74 return ({'before': lambda **map: (data(i) for i in navbefore),
083daee1b749 hgweb: simplify return value creation in for navgen
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18387
diff changeset
75 'after': lambda **map: (data(i) for i in navafter)},)
6393
894875eae49b hgweb: refactor hgweb code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6392
diff changeset
76
7671
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
77 def _siblings(siblings=[], hiderev=None):
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
78 siblings = [s for s in siblings if s.node() != nullid]
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
79 if len(siblings) == 1 and siblings[0].rev() == hiderev:
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
80 return
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
81 for s in siblings:
14055
421d56a055fd drop {short,hex}(ctx.node()) calls in favor of ctx methods
Alexander Solovyov <alexander@solovyov.net>
parents: 13971
diff changeset
82 d = {'node': s.hex(), 'rev': s.rev()}
7294
f933076a19fc hgweb: pass more information about parent/child csets to templates
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6750
diff changeset
83 d['user'] = s.user()
f933076a19fc hgweb: pass more information about parent/child csets to templates
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6750
diff changeset
84 d['date'] = s.date()
f933076a19fc hgweb: pass more information about parent/child csets to templates
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6750
diff changeset
85 d['description'] = s.description()
7717
f9ba30cb7ee4 hgweb: expose sibling branches to templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7671
diff changeset
86 d['branch'] = s.branch()
14957
16e5271b216f hgweb: move remaining hasattr calls to safehasattr
Augie Fackler <durin42@gmail.com>
parents: 14570
diff changeset
87 if util.safehasattr(s, 'path'):
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
88 d['file'] = s.path()
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
89 yield d
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
90
7671
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
91 def parents(ctx, hide=None):
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
92 return _siblings(ctx.parents(), hide)
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
93
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
94 def children(ctx, hide=None):
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
95 return _siblings(ctx.children(), hide)
06cf09c822c4 hgweb: simplify parents/children generation code
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7637
diff changeset
96
6434
62e0bb41e682 hgweb: minor improvements for new web style
Matt Mackall <mpm@selenic.com>
parents: 6413
diff changeset
97 def renamelink(fctx):
6437
101526031d06 hgweb: fix merge breakage
Matt Mackall <mpm@selenic.com>
parents: 6434
diff changeset
98 r = fctx.renamed()
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
99 if r:
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
100 return [dict(file=r[0], node=hex(r[1]))]
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
101 return []
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
102
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
103 def nodetagsdict(repo, node):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
104 return [{"name": i} for i in repo.nodetags(node)]
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
105
13596
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
106 def nodebookmarksdict(repo, node):
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
107 return [{"name": i} for i in repo.nodebookmarks(node)]
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
108
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
109 def nodebranchdict(repo, ctx):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
110 branches = []
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
111 branch = ctx.branch()
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
112 # If this is an empty repo, ctx.node() == nullid,
16719
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
113 # ctx.branch() == 'default'.
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
114 try:
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
115 branchnode = repo.branchtip(branch)
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
116 except error.RepoLookupError:
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
117 branchnode = None
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
118 if branchnode == ctx.node():
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
119 branches.append({"name": branch})
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
120 return branches
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
121
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
122 def nodeinbranch(repo, ctx):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
123 branches = []
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
124 branch = ctx.branch()
16719
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
125 try:
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
126 branchnode = repo.branchtip(branch)
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
127 except error.RepoLookupError:
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
128 branchnode = None
e7bf09acd410 localrepo: add branchtip() method for faster single-branch lookups
Brodie Rao <brodie@sf.io>
parents: 16308
diff changeset
129 if branch != 'default' and branchnode != ctx.node():
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
130 branches.append({"name": branch})
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
131 return branches
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
132
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
133 def nodebranchnodefault(ctx):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
134 branches = []
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
135 branch = ctx.branch()
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
136 if branch != 'default':
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
137 branches.append({"name": branch})
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
138 return branches
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
139
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
140 def showtag(repo, tmpl, t1, node=nullid, **args):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
141 for t in repo.nodetags(node):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
142 yield tmpl(t1, tag=t, **args)
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
143
13596
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
144 def showbookmark(repo, tmpl, t1, node=nullid, **args):
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
145 for t in repo.nodebookmarks(node):
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
146 yield tmpl(t1, bookmark=t, **args)
270f57d35525 hgweb: add display of bookmarks for changelog and changeset
Alexander Solovyov <alexander@solovyov.net>
parents: 12691
diff changeset
147
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
148 def cleanpath(repo, path):
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
149 path = path.lstrip('/')
13971
bfeaa88b875d move canonpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13596
diff changeset
150 return scmutil.canonpath(repo.root, '', path)
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
151
17991
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
152 def changeidctx (repo, changeid):
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
153 try:
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6437
diff changeset
154 ctx = repo[changeid]
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7361
diff changeset
155 except error.RepoError:
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
156 man = repo.manifest
7361
9fe97eea5510 linkrev: take a revision number rather than a hash
Matt Mackall <mpm@selenic.com>
parents: 7345
diff changeset
157 ctx = repo[man.linkrev(man.rev(man.lookup(changeid)))]
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
158
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
159 return ctx
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
160
17991
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
161 def changectx (repo, req):
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
162 changeid = "tip"
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
163 if 'node' in req.form:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
164 changeid = req.form['node'][0]
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
165 ipos=changeid.find(':')
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
166 if ipos != -1:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
167 changeid = changeid[(ipos + 1):]
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
168 elif 'manifest' in req.form:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
169 changeid = req.form['manifest'][0]
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
170
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
171 return changeidctx(repo, changeid)
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
172
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
173 def basechangectx(repo, req):
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
174 if 'node' in req.form:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
175 changeid = req.form['node'][0]
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
176 ipos=changeid.find(':')
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
177 if ipos != -1:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
178 changeid = changeid[:ipos]
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
179 return changeidctx(repo, changeid)
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
180
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
181 return None
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
182
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
183 def filectx(repo, req):
17289
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
184 if 'file' not in req.form:
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
185 raise ErrorResponse(HTTP_NOT_FOUND, 'file not given')
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
186 path = cleanpath(repo, req.form['file'][0])
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
187 if 'node' in req.form:
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
188 changeid = req.form['node'][0]
17289
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
189 elif 'filenode' in req.form:
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
190 changeid = req.form['filenode'][0]
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
191 else:
17289
f2d6b4f8e78c hgweb: avoid traceback when file or node parameters are missing
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17202
diff changeset
192 raise ErrorResponse(HTTP_NOT_FOUND, 'node or filenode not given')
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
193 try:
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6437
diff changeset
194 fctx = repo[changeid][path]
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7361
diff changeset
195 except error.RepoError:
6392
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
196 fctx = repo.filectx(path, fileid=changeid)
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
197
2540521dc7c1 hgweb: separate out utility functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
diff changeset
198 return fctx
7310
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
199
7311
de9c87fe1620 hgweb: move another utility function into the webutil module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7310
diff changeset
200 def listfilediffs(tmpl, files, node, max):
de9c87fe1620 hgweb: move another utility function into the webutil module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7310
diff changeset
201 for f in files[:max]:
de9c87fe1620 hgweb: move another utility function into the webutil module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7310
diff changeset
202 yield tmpl('filedifflink', node=hex(node), file=f)
de9c87fe1620 hgweb: move another utility function into the webutil module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7310
diff changeset
203 if len(files) > max:
de9c87fe1620 hgweb: move another utility function into the webutil module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7310
diff changeset
204 yield tmpl('fileellipses')
de9c87fe1620 hgweb: move another utility function into the webutil module
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7310
diff changeset
205
17991
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
206 def diffs(repo, tmpl, ctx, basectx, files, parity, style):
7310
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
207
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
208 def countgen():
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
209 start = 1
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
210 while True:
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
211 yield start
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
212 start += 1
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
213
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
214 blockcount = countgen()
16308
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
215 def prettyprintlines(diff, blockno):
7310
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
216 for lineno, l in enumerate(diff.splitlines(True)):
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
217 lineno = "%d.%d" % (blockno, lineno + 1)
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
218 if l.startswith('+'):
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
219 ltype = "difflineplus"
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
220 elif l.startswith('-'):
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
221 ltype = "difflineminus"
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
222 elif l.startswith('@'):
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
223 ltype = "difflineat"
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
224 else:
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
225 ltype = "diffline"
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
226 yield tmpl(ltype,
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
227 line=l,
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
228 lineid="l%s" % lineno,
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
229 linenumber="% 8s" % lineno)
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
230
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
231 if files:
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
232 m = match.exact(repo.root, repo.getcwd(), files)
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
233 else:
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
234 m = match.always(repo.root, repo.getcwd())
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
235
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
236 diffopts = patch.diffopts(repo.ui, untrusted=True)
17991
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
237 if basectx is None:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
238 parents = ctx.parents()
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
239 node1 = parents and parents[0].node() or nullid
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
240 else:
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
241 node1 = basectx.node()
7310
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
242 node2 = ctx.node()
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
243
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
244 block = []
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
245 for chunk in patch.diff(repo, node1, node2, m, opts=diffopts):
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
246 if chunk.startswith('diff') and block:
16308
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
247 blockno = blockcount.next()
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
248 yield tmpl('diffblock', parity=parity.next(), blockno=blockno,
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
249 lines=prettyprintlines(''.join(block), blockno))
7310
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
250 block = []
9402
5d49fdef6fd0 hgweb: show diff header line in raw diffs
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8225
diff changeset
251 if chunk.startswith('diff') and style != 'raw':
7310
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
252 chunk = ''.join(chunk.splitlines(True)[1:])
bd522d09d5e3 hgweb: move the diffs() generator into webutil
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7294
diff changeset
253 block.append(chunk)
16308
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
254 blockno = blockcount.next()
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
255 yield tmpl('diffblock', parity=parity.next(), blockno=blockno,
2695aaf4eb72 hgweb: add block numbers to diff regions and related links
Paul Boddie <paul@boddie.org.uk>
parents: 14957
diff changeset
256 lines=prettyprintlines(''.join(block), blockno))
7345
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
257
17302
5c64ce6168da hgweb: fixes traceback for invalid files by removing top-level template
wujek srujek <wujek.srujek@googlemail.com>
parents: 17289
diff changeset
258 def compare(tmpl, context, leftlines, rightlines):
17202
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
259 '''Generator function that provides side-by-side comparison data.'''
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
260
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
261 def compline(type, leftlineno, leftline, rightlineno, rightline):
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
262 lineid = leftlineno and ("l%s" % leftlineno) or ''
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
263 lineid += rightlineno and ("r%s" % rightlineno) or ''
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
264 return tmpl('comparisonline',
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
265 type=type,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
266 lineid=lineid,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
267 leftlinenumber="% 6s" % (leftlineno or ''),
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
268 leftline=leftline or '',
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
269 rightlinenumber="% 6s" % (rightlineno or ''),
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
270 rightline=rightline or '')
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
271
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
272 def getblock(opcodes):
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
273 for type, llo, lhi, rlo, rhi in opcodes:
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
274 len1 = lhi - llo
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
275 len2 = rhi - rlo
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
276 count = min(len1, len2)
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
277 for i in xrange(count):
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
278 yield compline(type=type,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
279 leftlineno=llo + i + 1,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
280 leftline=leftlines[llo + i],
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
281 rightlineno=rlo + i + 1,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
282 rightline=rightlines[rlo + i])
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
283 if len1 > len2:
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
284 for i in xrange(llo + count, lhi):
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
285 yield compline(type=type,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
286 leftlineno=i + 1,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
287 leftline=leftlines[i],
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
288 rightlineno=None,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
289 rightline=None)
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
290 elif len2 > len1:
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
291 for i in xrange(rlo + count, rhi):
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
292 yield compline(type=type,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
293 leftlineno=None,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
294 leftline=None,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
295 rightlineno=i + 1,
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
296 rightline=rightlines[i])
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
297
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
298 s = difflib.SequenceMatcher(None, leftlines, rightlines)
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
299 if context < 0:
17302
5c64ce6168da hgweb: fixes traceback for invalid files by removing top-level template
wujek srujek <wujek.srujek@googlemail.com>
parents: 17289
diff changeset
300 yield tmpl('comparisonblock', lines=getblock(s.get_opcodes()))
17202
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
301 else:
17302
5c64ce6168da hgweb: fixes traceback for invalid files by removing top-level template
wujek srujek <wujek.srujek@googlemail.com>
parents: 17289
diff changeset
302 for oc in s.get_grouped_opcodes(n=context):
5c64ce6168da hgweb: fixes traceback for invalid files by removing top-level template
wujek srujek <wujek.srujek@googlemail.com>
parents: 17289
diff changeset
303 yield tmpl('comparisonblock', lines=getblock(oc))
17202
1ae119269ddc hgweb: side-by-side comparison functionality
wujek srujek
parents: 16719
diff changeset
304
17991
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
305 def diffstatgen(ctx, basectx):
14570
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
306 '''Generator function that provides the diffstat data.'''
14490
1d3e2349304a web: provide diffstat to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14055
diff changeset
307
17991
d605a82cf189 hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents: 17302
diff changeset
308 stats = patch.diffstatdata(util.iterlines(ctx.diff(basectx)))
14490
1d3e2349304a web: provide diffstat to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14055
diff changeset
309 maxname, maxtotal, addtotal, removetotal, binary = patch.diffstatsum(stats)
14570
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
310 while True:
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
311 yield stats, maxname, maxtotal, addtotal, removetotal, binary
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
312
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
313 def diffsummary(statgen):
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
314 '''Return a short summary of the diff.'''
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
315
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
316 stats, maxname, maxtotal, addtotal, removetotal, binary = statgen.next()
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
317 return _(' %d files changed, %d insertions(+), %d deletions(-)\n') % (
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
318 len(stats), addtotal, removetotal)
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
319
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
320 def diffstat(tmpl, ctx, statgen, parity):
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
321 '''Return a diffstat template for each file in the diff.'''
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
322
9f908ef5a595 web: provide diff summary to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14562
diff changeset
323 stats, maxname, maxtotal, addtotal, removetotal, binary = statgen.next()
14561
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
324 files = ctx.files()
14490
1d3e2349304a web: provide diffstat to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14055
diff changeset
325
14561
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
326 def pct(i):
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
327 if maxtotal == 0:
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
328 return 0
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
329 return (float(i) / maxtotal) * 100
14490
1d3e2349304a web: provide diffstat to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14055
diff changeset
330
14562
fccd3b966da7 web: provide the file number to the diffstat templates
Steven Brown <StevenGBrown@gmail.com>
parents: 14561
diff changeset
331 fileno = 0
14561
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
332 for filename, adds, removes, isbinary in stats:
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
333 template = filename in files and 'diffstatlink' or 'diffstatnolink'
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
334 total = adds + removes
14562
fccd3b966da7 web: provide the file number to the diffstat templates
Steven Brown <StevenGBrown@gmail.com>
parents: 14561
diff changeset
335 fileno += 1
fccd3b966da7 web: provide the file number to the diffstat templates
Steven Brown <StevenGBrown@gmail.com>
parents: 14561
diff changeset
336 yield tmpl(template, node=ctx.hex(), file=filename, fileno=fileno,
14561
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
337 total=total, addpct=pct(adds), removepct=pct(removes),
925d9f2b188b web: include all files in the diffstat
Steven Brown <StevenGBrown@gmail.com>
parents: 14490
diff changeset
338 parity=parity.next())
14490
1d3e2349304a web: provide diffstat to the changeset page
Steven Brown <StevenGBrown@gmail.com>
parents: 14055
diff changeset
339
7345
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
340 class sessionvars(object):
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
341 def __init__(self, vars, start='?'):
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
342 self.start = start
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
343 self.vars = vars
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
344 def __getitem__(self, key):
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
345 return self.vars[key]
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
346 def __setitem__(self, key, value):
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
347 self.vars[key] = value
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
348 def __copy__(self):
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
349 return sessionvars(copy.copy(self.vars), self.start)
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
350 def __iter__(self):
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
351 separator = self.start
18367
ae7215f4f7b9 hgweb: generate query strings with parameters sorted by key
Mads Kiilerich <mads@kiilerich.com>
parents: 18320
diff changeset
352 for key, value in sorted(self.vars.iteritems()):
7345
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
353 yield {'name': key, 'value': str(value), 'separator': separator}
55651328dfcc hgweb: fix up the less/more links on the graph page
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7311
diff changeset
354 separator = '&'
12691
1b1a9038a71a hgweb: fix hgweb_mod as well as hgwebdir_mod
Augie Fackler <durin42@gmail.com>
parents: 10282
diff changeset
355
1b1a9038a71a hgweb: fix hgweb_mod as well as hgwebdir_mod
Augie Fackler <durin42@gmail.com>
parents: 10282
diff changeset
356 class wsgiui(ui.ui):
1b1a9038a71a hgweb: fix hgweb_mod as well as hgwebdir_mod
Augie Fackler <durin42@gmail.com>
parents: 10282
diff changeset
357 # default termwidth breaks under mod_wsgi
1b1a9038a71a hgweb: fix hgweb_mod as well as hgwebdir_mod
Augie Fackler <durin42@gmail.com>
parents: 10282
diff changeset
358 def termwidth(self):
1b1a9038a71a hgweb: fix hgweb_mod as well as hgwebdir_mod
Augie Fackler <durin42@gmail.com>
parents: 10282
diff changeset
359 return 80