|
1 # hgweb/webutil.py - utility library for the web interface. |
|
2 # |
|
3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 # |
|
6 # This software may be used and distributed according to the terms |
|
7 # of the GNU General Public License, incorporated herein by reference. |
|
8 |
|
9 from mercurial.node import hex, nullid |
|
10 from mercurial.repo import RepoError |
|
11 from mercurial import util |
|
12 |
|
13 def siblings(siblings=[], hiderev=None, **args): |
|
14 siblings = [s for s in siblings if s.node() != nullid] |
|
15 if len(siblings) == 1 and siblings[0].rev() == hiderev: |
|
16 return |
|
17 for s in siblings: |
|
18 d = {'node': hex(s.node()), 'rev': s.rev()} |
|
19 if hasattr(s, 'path'): |
|
20 d['file'] = s.path() |
|
21 d.update(args) |
|
22 yield d |
|
23 |
|
24 def renamelink(fl, node): |
|
25 r = fl.renamed(node) |
|
26 if r: |
|
27 return [dict(file=r[0], node=hex(r[1]))] |
|
28 return [] |
|
29 |
|
30 def nodetagsdict(repo, node): |
|
31 return [{"name": i} for i in repo.nodetags(node)] |
|
32 |
|
33 def nodebranchdict(repo, ctx): |
|
34 branches = [] |
|
35 branch = ctx.branch() |
|
36 # If this is an empty repo, ctx.node() == nullid, |
|
37 # ctx.branch() == 'default', but branchtags() is |
|
38 # an empty dict. Using dict.get avoids a traceback. |
|
39 if repo.branchtags().get(branch) == ctx.node(): |
|
40 branches.append({"name": branch}) |
|
41 return branches |
|
42 |
|
43 def nodeinbranch(repo, ctx): |
|
44 branches = [] |
|
45 branch = ctx.branch() |
|
46 if branch != 'default' and repo.branchtags().get(branch) != ctx.node(): |
|
47 branches.append({"name": branch}) |
|
48 return branches |
|
49 |
|
50 def nodebranchnodefault(ctx): |
|
51 branches = [] |
|
52 branch = ctx.branch() |
|
53 if branch != 'default': |
|
54 branches.append({"name": branch}) |
|
55 return branches |
|
56 |
|
57 def showtag(repo, tmpl, t1, node=nullid, **args): |
|
58 for t in repo.nodetags(node): |
|
59 yield tmpl(t1, tag=t, **args) |
|
60 |
|
61 def cleanpath(repo, path): |
|
62 path = path.lstrip('/') |
|
63 return util.canonpath(repo.root, '', path) |
|
64 |
|
65 def changectx(repo, req): |
|
66 if 'node' in req.form: |
|
67 changeid = req.form['node'][0] |
|
68 elif 'manifest' in req.form: |
|
69 changeid = req.form['manifest'][0] |
|
70 else: |
|
71 changeid = self.repo.changelog.count() - 1 |
|
72 |
|
73 try: |
|
74 ctx = repo.changectx(changeid) |
|
75 except RepoError: |
|
76 man = repo.manifest |
|
77 mn = man.lookup(changeid) |
|
78 ctx = repo.changectx(man.linkrev(mn)) |
|
79 |
|
80 return ctx |
|
81 |
|
82 def filectx(repo, req): |
|
83 path = cleanpath(repo, req.form['file'][0]) |
|
84 if 'node' in req.form: |
|
85 changeid = req.form['node'][0] |
|
86 else: |
|
87 changeid = req.form['filenode'][0] |
|
88 try: |
|
89 ctx = repo.changectx(changeid) |
|
90 fctx = ctx.filectx(path) |
|
91 except RepoError: |
|
92 fctx = repo.filectx(path, fileid=changeid) |
|
93 |
|
94 return fctx |