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