hgext/keyword.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Mon, 31 Mar 2008 18:44:12 +0200
changeset 6419 7c36aee46bf5
parent 6416 c0476a6df325
child 6502 ba8a0338baf7
permissions -rw-r--r--
hg serve: add clearer message when starting the server with --verbose Explicitly tell the address is the one the process is bound to. Fix the printing of IPv6 addresses.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     1
# keyword.py - $Keyword$ expansion for Mercurial
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     2
#
5831
4a40b7066525 keyword: fix some doc strings; update copyright
Christian Ebert <blacktrash@gmx.net>
parents: 5825
diff changeset
     3
# Copyright 2007, 2008 Christian Ebert <blacktrash@gmx.net>
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
# $Id$
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
# Keyword expansion hack against the grain of a DSCM
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
# There are many good reasons why this is not needed in a distributed
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
# SCM, still it may be useful in very small projects based on single
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
# files (like LaTeX packages), that are mostly addressed to an audience
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
# not running a version control system.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
# For in-depth discussion refer to
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    18
# <http://www.selenic.com/mercurial/wiki/index.cgi/KeywordPlan>.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    19
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
# Keyword expansion is based on Mercurial's changeset template mappings.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    22
# Binary files are not touched.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
# Setup in hgrc:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    26
#   [extensions]
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    27
#   # enable extension
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    28
#   hgext.keyword =
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    29
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    30
# Files to act upon/ignore are specified in the [keyword] section.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    31
# Customized keyword template mappings in the [keywordmaps] section.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    32
#
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    33
# Run "hg help keyword" and "hg kwdemo" to get info on configuration.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    35
'''keyword expansion in local repositories
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    36
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    37
This extension expands RCS/CVS-like or self-customized $Keywords$
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
in tracked text files selected by your configuration.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
Keywords are only expanded in local repositories and not stored in
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    41
the change history. The mechanism can be regarded as a convenience
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    42
for the current user or for archive distribution.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    43
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    44
Configuration is done in the [keyword] and [keywordmaps] sections
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    45
of hgrc files.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    46
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    47
Example:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    48
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    49
    [keyword]
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    50
    # expand keywords in every python file except those matching "x*"
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    51
    **.py =
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    52
    x*    = ignore
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    53
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    54
Note: the more specific you are in your filename patterns
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    55
      the less you lose speed in huge repos.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    56
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    57
For [keywordmaps] template mapping and expansion demonstration and
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    58
control run "hg kwdemo".
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    59
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    60
An additional date template filter {date|utcdate} is provided.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    61
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    62
The default template mappings (view with "hg kwdemo -d") can be replaced
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    63
with customized keywords and templates.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    64
Again, run "hg kwdemo" to control the results of your config changes.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    65
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    66
Before changing/disabling active keywords, run "hg kwshrink" to avoid
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    67
the risk of inadvertedly storing expanded keywords in the change history.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    68
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    69
To force expansion after enabling it, or a configuration change, run
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    70
"hg kwexpand".
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    71
5884
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
    72
Also, when committing with the record extension or using mq's qrecord, be aware
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
    73
that keywords cannot be updated. Again, run "hg kwexpand" on the files in
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
    74
question to update keyword expansions after all changes have been checked in.
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
    75
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    76
Expansions spanning more than one line and incremental expansions,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    77
like CVS' $Log$, are not supported. A keyword template map
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    78
"Log = {desc}" expands to the first line of the changeset description.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    79
'''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    80
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents: 5961
diff changeset
    81
from mercurial import commands, cmdutil, context, dispatch, filelog, revlog
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents: 5961
diff changeset
    82
from mercurial import patch, localrepo, templater, templatefilters, util
6072
e521ec1ad985 keyword: no expansion in web diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6071
diff changeset
    83
from mercurial.hgweb import webcommands
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 6157
diff changeset
    84
from mercurial.node import nullid, hex
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    85
from mercurial.i18n import _
6069
212bfb5c50e1 keyword: avoid os import by using util.splitpath
Christian Ebert <blacktrash@gmx.net>
parents: 6060
diff changeset
    86
import re, shutil, tempfile, time
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    87
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    88
commands.optionalrepo += ' kwdemo'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    89
6024
3121f0feefb4 keyword: nokwcommands, restricted string variables at top level
Christian Ebert <blacktrash@gmx.net>
parents: 6023
diff changeset
    90
# hg commands that do not act on keywords
6157
09b00faa8670 keyword: remove "identify" and "remove" from nokwcommands
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6117
diff changeset
    91
nokwcommands = ('add addremove bundle copy export grep incoming init'
09b00faa8670 keyword: remove "identify" and "remove" from nokwcommands
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6117
diff changeset
    92
                ' log outgoing push rename rollback tip'
6082
875b745996e8 keyword: add glog to nokwcommands
Christian Ebert <blacktrash@gmx.net>
parents: 6081
diff changeset
    93
                ' convert email glog')
6024
3121f0feefb4 keyword: nokwcommands, restricted string variables at top level
Christian Ebert <blacktrash@gmx.net>
parents: 6023
diff changeset
    94
5961
ed4d55c2366f keyword: detect restricted commands thru variable
Christian Ebert <blacktrash@gmx.net>
parents: 5946
diff changeset
    95
# hg commands that trigger expansion only when writing to working dir,
ed4d55c2366f keyword: detect restricted commands thru variable
Christian Ebert <blacktrash@gmx.net>
parents: 5946
diff changeset
    96
# not when reading filelog, and unexpand when reading from working dir
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
    97
restricted = 'record qfold qimport qnew qpush qrefresh qrecord'
5961
ed4d55c2366f keyword: detect restricted commands thru variable
Christian Ebert <blacktrash@gmx.net>
parents: 5946
diff changeset
    98
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    99
def utcdate(date):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   100
    '''Returns hgdate in cvs-like UTC format.'''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   101
    return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   102
5884
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   103
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   104
# make keyword tools accessible
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   105
kwtools = {'templater': None, 'hgcmd': None}
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   106
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   107
# store originals of monkeypatches
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   108
_patchfile_init = patch.patchfile.__init__
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   109
_patch_diff = patch.diff
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   110
_dispatch_parse = dispatch._parse
6416
c0476a6df325 keyword: regroup monkey patch code, underscore prefix private vars
Christian Ebert <blacktrash@gmx.net>
parents: 6393
diff changeset
   111
_webcommands_changeset = webcommands.changeset
c0476a6df325 keyword: regroup monkey patch code, underscore prefix private vars
Christian Ebert <blacktrash@gmx.net>
parents: 6393
diff changeset
   112
_webcommands_filediff = webcommands.filediff
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   113
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   114
def _kwpatchfile_init(self, ui, fname, missing=False):
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   115
    '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   116
    rejects or conflicts due to expanded keywords in working dir.'''
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   117
    _patchfile_init(self, ui, fname, missing=missing)
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   118
    # shrink keywords read from working dir
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   119
    kwt = kwtools['templater']
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   120
    self.lines = kwt.shrinklines(self.fname, self.lines)
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   121
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   122
def _kw_diff(repo, node1=None, node2=None, files=None, match=util.always,
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   123
             fp=None, changes=None, opts=None):
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   124
    '''Monkeypatch patch.diff to avoid expansion except when
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   125
    comparing against working dir.'''
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   126
    if node2 is not None:
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   127
        kwtools['templater'].matcher = util.never
6117
c74f1d9092f8 keyword: do not inspect node1 for diff if node2 is given
Christian Ebert <blacktrash@gmx.net>
parents: 6115
diff changeset
   128
    elif node1 is not None and node1 != repo.changectx().node():
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   129
        kwtools['templater'].restrict = True
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   130
    _patch_diff(repo, node1=node1, node2=node2, files=files, match=match,
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   131
                fp=fp, changes=changes, opts=opts)
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   132
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   133
def _kwweb_changeset(web, req, tmpl):
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   134
    '''Wraps webcommands.changeset turning off keyword expansion.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   135
    kwtools['templater'].matcher = util.never
6416
c0476a6df325 keyword: regroup monkey patch code, underscore prefix private vars
Christian Ebert <blacktrash@gmx.net>
parents: 6393
diff changeset
   136
    return _webcommands_changeset(web, req, tmpl)
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   137
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   138
def _kwweb_filediff(web, req, tmpl):
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   139
    '''Wraps webcommands.filediff turning off keyword expansion.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   140
    kwtools['templater'].matcher = util.never
6416
c0476a6df325 keyword: regroup monkey patch code, underscore prefix private vars
Christian Ebert <blacktrash@gmx.net>
parents: 6393
diff changeset
   141
    return _webcommands_filediff(web, req, tmpl)
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   142
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   143
def _kwdispatch_parse(ui, args):
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   144
    '''Monkeypatch dispatch._parse to obtain running hg command.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   145
    cmd, func, args, options, cmdoptions = _dispatch_parse(ui, args)
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   146
    kwtools['hgcmd'] = cmd
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   147
    return cmd, func, args, options, cmdoptions
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   148
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   149
# dispatch._parse is run before reposetup, so wrap it here
6416
c0476a6df325 keyword: regroup monkey patch code, underscore prefix private vars
Christian Ebert <blacktrash@gmx.net>
parents: 6393
diff changeset
   150
# all other actual monkey patching is done at end of reposetup
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   151
dispatch._parse = _kwdispatch_parse
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   152
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   153
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   154
class kwtemplater(object):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   155
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   156
    Sets up keyword templates, corresponding keyword regex, and
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   157
    provides keyword substitution functions.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   158
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   159
    templates = {
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   160
        'Revision': '{node|short}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   161
        'Author': '{author|user}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   162
        'Date': '{date|utcdate}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   163
        'RCSFile': '{file|basename},v',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   164
        'Source': '{root}/{file},v',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   165
        'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   166
        'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   167
    }
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   168
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   169
    def __init__(self, ui, repo, inc, exc):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   170
        self.ui = ui
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   171
        self.repo = repo
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   172
        self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   173
        self.restrict = kwtools['hgcmd'] in restricted.split()
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   174
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   175
        kwmaps = self.ui.configitems('keywordmaps')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   176
        if kwmaps: # override default templates
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   177
            kwmaps = [(k, templater.parsestring(v, quoted=False))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   178
                      for (k, v) in kwmaps]
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   179
            self.templates = dict(kwmaps)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   180
        escaped = map(re.escape, self.templates.keys())
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   181
        kwpat = r'\$(%s)(: [^$\n\r]*? )??\$' % '|'.join(escaped)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   182
        self.re_kw = re.compile(kwpat)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   183
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents: 5961
diff changeset
   184
        templatefilters.filters['utcdate'] = utcdate
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   185
        self.ct = cmdutil.changeset_templater(self.ui, self.repo,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   186
                                              False, '', False)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   187
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   188
    def getnode(self, path, fnode):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   189
        '''Derives changenode from file path and filenode.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   190
        # used by kwfilelog.read and kwexpand
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   191
        c = context.filectx(self.repo, path, fileid=fnode)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   192
        return c.node()
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   193
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   194
    def substitute(self, data, path, node, subfunc):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   195
        '''Replaces keywords in data with expanded template.'''
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   196
        def kwsub(mobj):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   197
            kw = mobj.group(1)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   198
            self.ct.use_template(self.templates[kw])
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   199
            self.ui.pushbuffer()
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   200
            self.ct.show(changenode=node, root=self.repo.root, file=path)
6023
ee7df90d1daa keyword: split line continuation in 2 steps (style)
Christian Ebert <blacktrash@gmx.net>
parents: 6022
diff changeset
   201
            ekw = templatefilters.firstline(self.ui.popbuffer())
ee7df90d1daa keyword: split line continuation in 2 steps (style)
Christian Ebert <blacktrash@gmx.net>
parents: 6022
diff changeset
   202
            return '$%s: %s $' % (kw, ekw)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   203
        return subfunc(kwsub, data)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   204
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   205
    def expand(self, path, node, data):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   206
        '''Returns data with keywords expanded.'''
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   207
        if not self.restrict and self.matcher(path) and not util.binary(data):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   208
            changenode = self.getnode(path, node)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   209
            return self.substitute(data, path, changenode, self.re_kw.sub)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   210
        return data
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   211
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   212
    def iskwfile(self, path, islink):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   213
        '''Returns true if path matches [keyword] pattern
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   214
        and is not a symbolic link.
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   215
        Caveat: localrepository._link fails on Windows.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   216
        return self.matcher(path) and not islink(path)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   217
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   218
    def overwrite(self, node=None, expand=True, files=None):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   219
        '''Overwrites selected files expanding/shrinking keywords.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   220
        ctx = self.repo.changectx(node)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   221
        mf = ctx.manifest()
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   222
        if node is not None:     # commit
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   223
            files = [f for f in ctx.files() if f in mf]
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   224
            notify = self.ui.debug
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   225
        else:                    # kwexpand/kwshrink
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   226
            notify = self.ui.note
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   227
        candidates = [f for f in files if self.iskwfile(f, mf.linkf)]
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   228
        if candidates:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   229
            self.restrict = True # do not expand when reading
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   230
            candidates.sort()
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   231
            action = expand and 'expanding' or 'shrinking'
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   232
            for f in candidates:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   233
                fp = self.repo.file(f)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   234
                data = fp.read(mf[f])
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   235
                if util.binary(data):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   236
                    continue
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   237
                if expand:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   238
                    changenode = node or self.getnode(f, mf[f])
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   239
                    data, found = self.substitute(data, f, changenode,
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   240
                                                  self.re_kw.subn)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   241
                else:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   242
                    found = self.re_kw.search(data)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   243
                if found:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   244
                    notify(_('overwriting %s %s keywords\n') % (f, action))
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   245
                    self.repo.wwrite(f, data, mf.flags(f))
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   246
                    self.repo.dirstate.normal(f)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   247
            self.restrict = False
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   248
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   249
    def shrinktext(self, text):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   250
        '''Unconditionally removes all keyword substitutions from text.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   251
        return self.re_kw.sub(r'$\1$', text)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   252
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   253
    def shrink(self, fname, text):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   254
        '''Returns text with all keyword substitutions removed.'''
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   255
        if self.matcher(fname) and not util.binary(text):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   256
            return self.shrinktext(text)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   257
        return text
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   258
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   259
    def shrinklines(self, fname, lines):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   260
        '''Returns lines with keyword substitutions removed.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   261
        if self.matcher(fname):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   262
            text = ''.join(lines)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   263
            if not util.binary(text):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   264
                return self.shrinktext(text).splitlines(True)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   265
        return lines
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   266
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   267
    def wread(self, fname, data):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   268
        '''If in restricted mode returns data read from wdir with
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   269
        keyword substitutions removed.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   270
        return self.restrict and self.shrink(fname, data) or data
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   271
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   272
class kwfilelog(filelog.filelog):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   273
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   274
    Subclass of filelog to hook into its read, add, cmp methods.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   275
    Keywords are "stored" unexpanded, and processed on reading.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   276
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   277
    def __init__(self, opener, path):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   278
        super(kwfilelog, self).__init__(opener, path)
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   279
        self.kwt = kwtools['templater']
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   280
        self.path = path
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   281
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   282
    def read(self, node):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   283
        '''Expands keywords when reading filelog.'''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   284
        data = super(kwfilelog, self).read(node)
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   285
        return self.kwt.expand(self.path, node, data)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   286
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   287
    def add(self, text, meta, tr, link, p1=None, p2=None):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   288
        '''Removes keyword substitutions when adding to filelog.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   289
        text = self.kwt.shrink(self.path, text)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   290
        return super(kwfilelog, self).add(text, meta, tr, link, p1=p1, p2=p2)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   291
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   292
    def cmp(self, node, text):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   293
        '''Removes keyword substitutions for comparison.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   294
        text = self.kwt.shrink(self.path, text)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   295
        if self.renamed(node):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   296
            t2 = super(kwfilelog, self).read(node)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   297
            return t2 != text
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   298
        return revlog.revlog.cmp(self, node, text)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   299
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   300
def _status(ui, repo, kwt, *pats, **opts):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   301
    '''Bails out if [keyword] configuration is not active.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   302
    Returns status of working directory.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   303
    if kwt:
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   304
        files, match, anypats = cmdutil.matchpats(repo, pats, opts)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   305
        return repo.status(files=files, match=match, list_clean=True)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   306
    if ui.configitems('keyword'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   307
        raise util.Abort(_('[keyword] patterns cannot match'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   308
    raise util.Abort(_('no [keyword] patterns configured'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   309
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   310
def _kwfwrite(ui, repo, expand, *pats, **opts):
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   311
    '''Selects files and passes them to kwtemplater.overwrite.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   312
    kwt = kwtools['templater']
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   313
    status = _status(ui, repo, kwt, *pats, **opts)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   314
    modified, added, removed, deleted, unknown, ignored, clean = status
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   315
    if modified or added or removed or deleted:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   316
        raise util.Abort(_('outstanding uncommitted changes in given files'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   317
    wlock = lock = None
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   318
    try:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   319
        wlock = repo.wlock()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   320
        lock = repo.lock()
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   321
        kwt.overwrite(expand=expand, files=clean)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   322
    finally:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   323
        del wlock, lock
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   324
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   325
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   326
def demo(ui, repo, *args, **opts):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   327
    '''print [keywordmaps] configuration and an expansion example
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   328
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   329
    Show current, custom, or default keyword template maps
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   330
    and their expansion.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   331
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   332
    Extend current configuration by specifying maps as arguments
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   333
    and optionally by reading from an additional hgrc file.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   334
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   335
    Override current keyword template maps with "default" option.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   336
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   337
    def demostatus(stat):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   338
        ui.status(_('\n\t%s\n') % stat)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   339
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   340
    def demoitems(section, items):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   341
        ui.write('[%s]\n' % section)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   342
        for k, v in items:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   343
            ui.write('%s = %s\n' % (k, v))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   344
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   345
    msg = 'hg keyword config and expansion example'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   346
    kwstatus = 'current'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   347
    fn = 'demo.txt'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   348
    branchname = 'demobranch'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   349
    tmpdir = tempfile.mkdtemp('', 'kwdemo.')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   350
    ui.note(_('creating temporary repo at %s\n') % tmpdir)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   351
    repo = localrepo.localrepository(ui, path=tmpdir, create=True)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   352
    ui.setconfig('keyword', fn, '')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   353
    if args or opts.get('rcfile'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   354
        kwstatus = 'custom'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   355
    if opts.get('rcfile'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   356
        ui.readconfig(opts.get('rcfile'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   357
    if opts.get('default'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   358
        kwstatus = 'default'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   359
        kwmaps = kwtemplater.templates
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   360
        if ui.configitems('keywordmaps'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   361
            # override maps from optional rcfile
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   362
            for k, v in kwmaps.iteritems():
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   363
                ui.setconfig('keywordmaps', k, v)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   364
    elif args:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   365
        # simulate hgrc parsing
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   366
        rcmaps = ['[keywordmaps]\n'] + [a + '\n' for a in args]
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   367
        fp = repo.opener('hgrc', 'w')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   368
        fp.writelines(rcmaps)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   369
        fp.close()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   370
        ui.readconfig(repo.join('hgrc'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   371
    if not opts.get('default'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   372
        kwmaps = dict(ui.configitems('keywordmaps')) or kwtemplater.templates
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   373
    reposetup(ui, repo)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   374
    for k, v in ui.configitems('extensions'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   375
        if k.endswith('keyword'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   376
            extension = '%s = %s' % (k, v)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   377
            break
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   378
    demostatus('config using %s keyword template maps' % kwstatus)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   379
    ui.write('[extensions]\n%s\n' % extension)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   380
    demoitems('keyword', ui.configitems('keyword'))
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   381
    demoitems('keywordmaps', kwmaps.iteritems())
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   382
    keywords = '$' + '$\n$'.join(kwmaps.keys()) + '$\n'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   383
    repo.wopener(fn, 'w').write(keywords)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   384
    repo.add([fn])
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   385
    path = repo.wjoin(fn)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   386
    ui.note(_('\n%s keywords written to %s:\n') % (kwstatus, path))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   387
    ui.note(keywords)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   388
    ui.note('\nhg -R "%s" branch "%s"\n' % (tmpdir, branchname))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   389
    # silence branch command if not verbose
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   390
    quiet = ui.quiet
5825
2b67acc404f6 keyword: clean up quiet setting in kwdemo and adding of untracked kwfiles
Christian Ebert <blacktrash@gmx.net>
parents: 5824
diff changeset
   391
    ui.quiet = not ui.verbose
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   392
    commands.branch(ui, repo, branchname)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   393
    ui.quiet = quiet
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   394
    for name, cmd in ui.configitems('hooks'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   395
        if name.split('.', 1)[0].find('commit') > -1:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   396
            repo.ui.setconfig('hooks', name, '')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   397
    ui.note(_('unhooked all commit hooks\n'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   398
    ui.note('hg -R "%s" ci -m "%s"\n' % (tmpdir, msg))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   399
    repo.commit(text=msg)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   400
    format = ui.verbose and ' in %s' % path or ''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   401
    demostatus('%s keywords expanded%s' % (kwstatus, format))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   402
    ui.write(repo.wread(fn))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   403
    ui.debug(_('\nremoving temporary repo %s\n') % tmpdir)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   404
    shutil.rmtree(tmpdir, ignore_errors=True)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   405
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   406
def expand(ui, repo, *pats, **opts):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   407
    '''expand keywords in working directory
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   408
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   409
    Run after (re)enabling keyword expansion.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   410
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   411
    kwexpand refuses to run if given files contain local changes.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   412
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   413
    # 3rd argument sets expansion to True
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   414
    _kwfwrite(ui, repo, True, *pats, **opts)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   415
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   416
def files(ui, repo, *pats, **opts):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   417
    '''print files currently configured for keyword expansion
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   418
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   419
    Crosscheck which files in working directory are potential targets for
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   420
    keyword expansion.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   421
    That is, files matched by [keyword] config patterns but not symlinks.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   422
    '''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   423
    kwt = kwtools['templater']
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   424
    status = _status(ui, repo, kwt, *pats, **opts)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   425
    modified, added, removed, deleted, unknown, ignored, clean = status
5825
2b67acc404f6 keyword: clean up quiet setting in kwdemo and adding of untracked kwfiles
Christian Ebert <blacktrash@gmx.net>
parents: 5824
diff changeset
   426
    files = modified + added + clean
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   427
    if opts.get('untracked'):
5825
2b67acc404f6 keyword: clean up quiet setting in kwdemo and adding of untracked kwfiles
Christian Ebert <blacktrash@gmx.net>
parents: 5824
diff changeset
   428
        files += unknown
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   429
    files.sort()
6060
e4d74100d41b keyword: fix symlink detection under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 6059
diff changeset
   430
    wctx = repo.workingctx()
e4d74100d41b keyword: fix symlink detection under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 6059
diff changeset
   431
    islink = lambda p: 'l' in wctx.fileflags(p)
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   432
    kwfiles = [f for f in files if kwt.iskwfile(f, islink)]
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   433
    cwd = pats and repo.getcwd() or ''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   434
    kwfstats = not opts.get('ignore') and (('K', kwfiles),) or ()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   435
    if opts.get('all') or opts.get('ignore'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   436
        kwfstats += (('I', [f for f in files if f not in kwfiles]),)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   437
    for char, filenames in kwfstats:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   438
        format = (opts.get('all') or ui.verbose) and '%s %%s\n' % char or '%s\n'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   439
        for f in filenames:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   440
            ui.write(format % repo.pathto(f, cwd))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   441
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   442
def shrink(ui, repo, *pats, **opts):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   443
    '''revert expanded keywords in working directory
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   444
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   445
    Run before changing/disabling active keywords
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   446
    or if you experience problems with "hg import" or "hg merge".
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   447
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   448
    kwshrink refuses to run if given files contain local changes.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   449
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   450
    # 3rd argument sets expansion to False
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   451
    _kwfwrite(ui, repo, False, *pats, **opts)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   452
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   453
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   454
def reposetup(ui, repo):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   455
    '''Sets up repo as kwrepo for keyword substitution.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   456
    Overrides file method to return kwfilelog instead of filelog
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   457
    if file matches user configuration.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   458
    Wraps commit to overwrite configured files with updated
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   459
    keyword substitutions.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   460
    This is done for local repos only, and only if there are
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   461
    files configured at all for keyword substitution.'''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   462
6051
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   463
    try:
6117
c74f1d9092f8 keyword: do not inspect node1 for diff if node2 is given
Christian Ebert <blacktrash@gmx.net>
parents: 6115
diff changeset
   464
        if (not repo.local() or kwtools['hgcmd'] in nokwcommands.split()
6069
212bfb5c50e1 keyword: avoid os import by using util.splitpath
Christian Ebert <blacktrash@gmx.net>
parents: 6060
diff changeset
   465
            or '.hg' in util.splitpath(repo.root)
6051
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   466
            or repo._url.startswith('bundle:')):
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   467
            return
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   468
    except AttributeError:
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   469
        pass
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   470
6051
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   471
    inc, exc = [], ['.hg*']
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   472
    for pat, opt in ui.configitems('keyword'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   473
        if opt != 'ignore':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   474
            inc.append(pat)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   475
        else:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   476
            exc.append(pat)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   477
    if not inc:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   478
        return
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   479
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   480
    kwtools['templater'] = kwt = kwtemplater(ui, repo, inc, exc)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   481
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   482
    class kwrepo(repo.__class__):
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   483
        def file(self, f):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   484
            if f[0] == '/':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   485
                f = f[1:]
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   486
            return kwfilelog(self.sopener, f)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   487
5884
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   488
        def wread(self, filename):
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   489
            data = super(kwrepo, self).wread(filename)
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   490
            return kwt.wread(filename, data)
5884
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   491
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   492
        def commit(self, files=None, text='', user=None, date=None,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   493
                   match=util.always, force=False, force_editor=False,
6022
e5b5010ff7da keyword: add empty_ok argument to kwrepo.commit
Christian Ebert <blacktrash@gmx.net>
parents: 6021
diff changeset
   494
                   p1=None, p2=None, extra={}, empty_ok=False):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   495
            wlock = lock = None
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   496
            _p1 = _p2 = None
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   497
            try:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   498
                wlock = self.wlock()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   499
                lock = self.lock()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   500
                # store and postpone commit hooks
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   501
                commithooks = {}
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   502
                for name, cmd in ui.configitems('hooks'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   503
                    if name.split('.', 1)[0] == 'commit':
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   504
                        commithooks[name] = cmd
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   505
                        ui.setconfig('hooks', name, None)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   506
                if commithooks:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   507
                    # store parents for commit hook environment
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   508
                    if p1 is None:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   509
                        _p1, _p2 = repo.dirstate.parents()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   510
                    else:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   511
                        _p1, _p2 = p1, p2 or nullid
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   512
                    _p1 = hex(_p1)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   513
                    if _p2 == nullid:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   514
                        _p2 = ''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   515
                    else:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   516
                        _p2 = hex(_p2)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   517
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   518
                node = super(kwrepo,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   519
                             self).commit(files=files, text=text, user=user,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   520
                                          date=date, match=match, force=force,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   521
                                          force_editor=force_editor,
6022
e5b5010ff7da keyword: add empty_ok argument to kwrepo.commit
Christian Ebert <blacktrash@gmx.net>
parents: 6021
diff changeset
   522
                                          p1=p1, p2=p2, extra=extra,
e5b5010ff7da keyword: add empty_ok argument to kwrepo.commit
Christian Ebert <blacktrash@gmx.net>
parents: 6021
diff changeset
   523
                                          empty_ok=empty_ok)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   524
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   525
                # restore commit hooks
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   526
                for name, cmd in commithooks.iteritems():
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   527
                    ui.setconfig('hooks', name, cmd)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   528
                if node is not None:
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   529
                    kwt.overwrite(node=node)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   530
                    repo.hook('commit', node=node, parent1=_p1, parent2=_p2)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   531
                return node
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   532
            finally:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   533
                del wlock, lock
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   534
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   535
    repo.__class__ = kwrepo
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   536
    patch.patchfile.__init__ = _kwpatchfile_init
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   537
    patch.diff = _kw_diff
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   538
    webcommands.changeset = webcommands.rev = _kwweb_changeset
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   539
    webcommands.filediff = webcommands.diff = _kwweb_filediff
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   540
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   541
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   542
cmdtable = {
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   543
    'kwdemo':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   544
        (demo,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   545
         [('d', 'default', None, _('show default keyword template maps')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   546
          ('f', 'rcfile', [], _('read maps from rcfile'))],
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   547
         _('hg kwdemo [-d] [-f RCFILE] [TEMPLATEMAP]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   548
    'kwexpand': (expand, commands.walkopts,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   549
                 _('hg kwexpand [OPTION]... [FILE]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   550
    'kwfiles':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   551
        (files,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   552
         [('a', 'all', None, _('show keyword status flags of all files')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   553
          ('i', 'ignore', None, _('show files excluded from expansion')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   554
          ('u', 'untracked', None, _('additionally show untracked files')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   555
         ] + commands.walkopts,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   556
         _('hg kwfiles [OPTION]... [FILE]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   557
    'kwshrink': (shrink, commands.walkopts,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   558
                 _('hg kwshrink [OPTION]... [FILE]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   559
}