hgext/keyword.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 28 Feb 2008 19:08:04 +0100
changeset 6182 07f2236c4dee
parent 6157 09b00faa8670
child 6211 f89fd07fc51d
permissions -rw-r--r--
add a blame alias for annotate The original cvs command was called blame. SVN has both, and other VC systems typically also have this as an alias. Makes things easier for converts.
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
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    84
from mercurial.node import *
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
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   111
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   112
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
   113
    '''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
   114
    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
   115
    _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
   116
    # 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
   117
    kwt = kwtools['templater']
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   118
    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
   119
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   120
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
   121
             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
   122
    '''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
   123
    comparing against working dir.'''
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   124
    if node2 is not None:
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   125
        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
   126
    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
   127
        kwtools['templater'].restrict = True
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   128
    _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
   129
                fp=fp, changes=changes, opts=opts)
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   130
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   131
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
   132
    '''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
   133
    kwtools['templater'].matcher = util.never
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   134
    return web.changeset(tmpl, web.changectx(req))
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   135
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   136
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
   137
    '''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
   138
    kwtools['templater'].matcher = util.never
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   139
    return web.filediff(tmpl, web.filectx(req))
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   140
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   141
def _kwdispatch_parse(ui, args):
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   142
    '''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
   143
    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
   144
    kwtools['hgcmd'] = cmd
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   145
    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
   146
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   147
# dispatch._parse is run before reposetup, so wrap it here
6052
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   148
dispatch._parse = _kwdispatch_parse
75425961e955 keyword: monkeypatch dispatch._parse to avoid redundant run
Christian Ebert <blacktrash@gmx.net>
parents: 6051
diff changeset
   149
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   150
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   151
class kwtemplater(object):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   152
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   153
    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
   154
    provides keyword substitution functions.
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
    templates = {
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   157
        'Revision': '{node|short}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   158
        'Author': '{author|user}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   159
        'Date': '{date|utcdate}',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   160
        'RCSFile': '{file|basename},v',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   161
        'Source': '{root}/{file},v',
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   162
        '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
   163
        '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
   164
    }
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   165
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   166
    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
   167
        self.ui = ui
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   168
        self.repo = repo
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   169
        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
   170
        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
   171
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   172
        kwmaps = self.ui.configitems('keywordmaps')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   173
        if kwmaps: # override default templates
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   174
            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
   175
                      for (k, v) in kwmaps]
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   176
            self.templates = dict(kwmaps)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   177
        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
   178
        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
   179
        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
   180
5976
9f1e6ab76069 templates: move filters to their own module
Matt Mackall <mpm@selenic.com>
parents: 5961
diff changeset
   181
        templatefilters.filters['utcdate'] = utcdate
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   182
        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
   183
                                              False, '', False)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   184
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   185
    def getnode(self, path, fnode):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   186
        '''Derives changenode from file path and filenode.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   187
        # used by kwfilelog.read and kwexpand
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   188
        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
   189
        return c.node()
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   190
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   191
    def substitute(self, data, path, node, subfunc):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   192
        '''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
   193
        def kwsub(mobj):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   194
            kw = mobj.group(1)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   195
            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
   196
            self.ui.pushbuffer()
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   197
            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
   198
            ekw = templatefilters.firstline(self.ui.popbuffer())
ee7df90d1daa keyword: split line continuation in 2 steps (style)
Christian Ebert <blacktrash@gmx.net>
parents: 6022
diff changeset
   199
            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
   200
        return subfunc(kwsub, data)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   201
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   202
    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
   203
        '''Returns data with keywords expanded.'''
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   204
        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
   205
            changenode = self.getnode(path, node)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   206
            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
   207
        return data
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   208
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   209
    def iskwfile(self, path, islink):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   210
        '''Returns true if path matches [keyword] pattern
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   211
        and is not a symbolic link.
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   212
        Caveat: localrepository._link fails on Windows.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   213
        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
   214
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   215
    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
   216
        '''Overwrites selected files expanding/shrinking keywords.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   217
        ctx = self.repo.changectx(node)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   218
        mf = ctx.manifest()
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   219
        if node is not None:     # commit
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   220
            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
   221
            notify = self.ui.debug
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   222
        else:                    # kwexpand/kwshrink
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   223
            notify = self.ui.note
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   224
        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
   225
        if candidates:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   226
            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
   227
            candidates.sort()
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   228
            action = expand and 'expanding' or 'shrinking'
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   229
            for f in candidates:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   230
                fp = self.repo.file(f)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   231
                data = fp.read(mf[f])
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   232
                if util.binary(data):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   233
                    continue
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   234
                if expand:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   235
                    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
   236
                    data, found = self.substitute(data, f, changenode,
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   237
                                                  self.re_kw.subn)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   238
                else:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   239
                    found = self.re_kw.search(data)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   240
                if found:
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   241
                    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
   242
                    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
   243
                    self.repo.dirstate.normal(f)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   244
            self.restrict = False
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   245
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   246
    def shrinktext(self, text):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   247
        '''Unconditionally removes all keyword substitutions from text.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   248
        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
   249
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   250
    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
   251
        '''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
   252
        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
   253
            return self.shrinktext(text)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   254
        return text
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   255
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   256
    def shrinklines(self, fname, lines):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   257
        '''Returns lines with keyword substitutions removed.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   258
        if self.matcher(fname):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   259
            text = ''.join(lines)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   260
            if not util.binary(text):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   261
                return self.shrinktext(text).splitlines(True)
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   262
        return lines
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   263
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   264
    def wread(self, fname, data):
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   265
        '''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
   266
        keyword substitutions removed.'''
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   267
        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
   268
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   269
class kwfilelog(filelog.filelog):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   270
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   271
    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
   272
    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
   273
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   274
    def __init__(self, opener, path):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   275
        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
   276
        self.kwt = kwtools['templater']
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   277
        self.path = path
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   278
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   279
    def read(self, node):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   280
        '''Expands keywords when reading filelog.'''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   281
        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
   282
        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
   283
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   284
    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
   285
        '''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
   286
        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
   287
        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
   288
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   289
    def cmp(self, node, text):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   290
        '''Removes keyword substitutions for comparison.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   291
        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
   292
        if self.renamed(node):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   293
            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
   294
            return t2 != text
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   295
        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
   296
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   297
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
   298
    '''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
   299
    Returns status of working directory.'''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   300
    if kwt:
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   301
        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
   302
        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
   303
    if ui.configitems('keyword'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   304
        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
   305
    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
   306
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   307
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
   308
    '''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
   309
    kwt = kwtools['templater']
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   310
    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
   311
    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
   312
    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
   313
        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
   314
    wlock = lock = None
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   315
    try:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   316
        wlock = repo.wlock()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   317
        lock = repo.lock()
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   318
        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
   319
    finally:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   320
        del wlock, lock
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   321
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   322
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   323
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
   324
    '''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
   325
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   326
    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
   327
    and their expansion.
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
    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
   330
    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
   331
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   332
    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
   333
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   334
    def demostatus(stat):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   335
        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
   336
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   337
    def demoitems(section, items):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   338
        ui.write('[%s]\n' % section)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   339
        for k, v in items:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   340
            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
   341
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   342
    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
   343
    kwstatus = 'current'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   344
    fn = 'demo.txt'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   345
    branchname = 'demobranch'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   346
    tmpdir = tempfile.mkdtemp('', 'kwdemo.')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   347
    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
   348
    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
   349
    ui.setconfig('keyword', fn, '')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   350
    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
   351
        kwstatus = 'custom'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   352
    if opts.get('rcfile'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   353
        ui.readconfig(opts.get('rcfile'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   354
    if opts.get('default'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   355
        kwstatus = 'default'
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   356
        kwmaps = kwtemplater.templates
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   357
        if ui.configitems('keywordmaps'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   358
            # override maps from optional rcfile
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   359
            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
   360
                ui.setconfig('keywordmaps', k, v)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   361
    elif args:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   362
        # simulate hgrc parsing
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   363
        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
   364
        fp = repo.opener('hgrc', 'w')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   365
        fp.writelines(rcmaps)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   366
        fp.close()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   367
        ui.readconfig(repo.join('hgrc'))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   368
    if not opts.get('default'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   369
        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
   370
    reposetup(ui, repo)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   371
    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
   372
        if k.endswith('keyword'):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   373
            extension = '%s = %s' % (k, v)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   374
            break
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   375
    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
   376
    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
   377
    demoitems('keyword', ui.configitems('keyword'))
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   378
    demoitems('keywordmaps', kwmaps.iteritems())
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   379
    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
   380
    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
   381
    repo.add([fn])
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   382
    path = repo.wjoin(fn)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   383
    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
   384
    ui.note(keywords)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   385
    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
   386
    # 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
   387
    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
   388
    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
   389
    commands.branch(ui, repo, branchname)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   390
    ui.quiet = quiet
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   391
    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
   392
        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
   393
            repo.ui.setconfig('hooks', name, '')
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   394
    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
   395
    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
   396
    repo.commit(text=msg)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   397
    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
   398
    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
   399
    ui.write(repo.wread(fn))
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   400
    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
   401
    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
   402
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   403
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
   404
    '''expand keywords in working directory
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
    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
   407
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   408
    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
   409
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   410
    # 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
   411
    _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
   412
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   413
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
   414
    '''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
   415
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   416
    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
   417
    keyword expansion.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   418
    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
   419
    '''
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   420
    kwt = kwtools['templater']
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   421
    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
   422
    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
   423
    files = modified + added + clean
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   424
    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
   425
        files += unknown
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   426
    files.sort()
6060
e4d74100d41b keyword: fix symlink detection under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 6059
diff changeset
   427
    wctx = repo.workingctx()
e4d74100d41b keyword: fix symlink detection under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 6059
diff changeset
   428
    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
   429
    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
   430
    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
   431
    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
   432
    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
   433
        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
   434
    for char, filenames in kwfstats:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   435
        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
   436
        for f in filenames:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   437
            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
   438
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   439
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
   440
    '''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
   441
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   442
    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
   443
    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
   444
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   445
    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
   446
    '''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   447
    # 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
   448
    _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
   449
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   450
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   451
def reposetup(ui, repo):
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   452
    '''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
   453
    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
   454
    if file matches user configuration.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   455
    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
   456
    keyword substitutions.
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   457
    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
   458
    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
   459
6051
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   460
    try:
6117
c74f1d9092f8 keyword: do not inspect node1 for diff if node2 is given
Christian Ebert <blacktrash@gmx.net>
parents: 6115
diff changeset
   461
        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
   462
            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
   463
            or repo._url.startswith('bundle:')):
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   464
            return
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   465
    except AttributeError:
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   466
        pass
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   467
6051
1038b1458d7a keyword: safeguards against erroneous behaviour or aborts
Christian Ebert <blacktrash@gmx.net>
parents: 6050
diff changeset
   468
    inc, exc = [], ['.hg*']
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   469
    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
   470
        if opt != 'ignore':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   471
            inc.append(pat)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   472
        else:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   473
            exc.append(pat)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   474
    if not inc:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   475
        return
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   476
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   477
    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
   478
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   479
    class kwrepo(repo.__class__):
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   480
        def file(self, f):
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   481
            if f[0] == '/':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   482
                f = f[1:]
6114
ee83510fe567 keyword: move expand/shrink decisions into kwtemplater
Christian Ebert <blacktrash@gmx.net>
parents: 6092
diff changeset
   483
            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
   484
5884
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   485
        def wread(self, filename):
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   486
            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
   487
            return kwt.wread(filename, data)
5884
a139f141dcae keyword: support mq; handle (q)record more gracefully
Christian Ebert <blacktrash@gmx.net>
parents: 5831
diff changeset
   488
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   489
        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
   490
                   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
   491
                   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
   492
            wlock = lock = None
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   493
            _p1 = _p2 = None
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   494
            try:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   495
                wlock = self.wlock()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   496
                lock = self.lock()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   497
                # store and postpone commit hooks
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   498
                commithooks = {}
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   499
                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
   500
                    if name.split('.', 1)[0] == 'commit':
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   501
                        commithooks[name] = cmd
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   502
                        ui.setconfig('hooks', name, None)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   503
                if commithooks:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   504
                    # 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
   505
                    if p1 is None:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   506
                        _p1, _p2 = repo.dirstate.parents()
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   507
                    else:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   508
                        _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
   509
                    _p1 = hex(_p1)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   510
                    if _p2 == nullid:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   511
                        _p2 = ''
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   512
                    else:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   513
                        _p2 = hex(_p2)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   514
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   515
                node = super(kwrepo,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   516
                             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
   517
                                          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
   518
                                          force_editor=force_editor,
6022
e5b5010ff7da keyword: add empty_ok argument to kwrepo.commit
Christian Ebert <blacktrash@gmx.net>
parents: 6021
diff changeset
   519
                                          p1=p1, p2=p2, extra=extra,
e5b5010ff7da keyword: add empty_ok argument to kwrepo.commit
Christian Ebert <blacktrash@gmx.net>
parents: 6021
diff changeset
   520
                                          empty_ok=empty_ok)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   521
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   522
                # restore commit hooks
5946
ee0dc0f3804b keyword: improve use of dicts
Christian Ebert <blacktrash@gmx.net>
parents: 5945
diff changeset
   523
                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
   524
                    ui.setconfig('hooks', name, cmd)
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   525
                if node is not None:
6115
8121e9d7bfd2 keyword: make main class and hg command accessible
Christian Ebert <blacktrash@gmx.net>
parents: 6114
diff changeset
   526
                    kwt.overwrite(node=node)
5815
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   527
                    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
   528
                return node
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   529
            finally:
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   530
                del wlock, lock
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   531
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   532
    repo.__class__ = kwrepo
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   533
    patch.patchfile.__init__ = _kwpatchfile_init
6092
911f5be5d159 keyword: monkeypatch patch.diff for non-interactive diffs
Christian Ebert <blacktrash@gmx.net>
parents: 6082
diff changeset
   534
    patch.diff = _kw_diff
6081
beb775df5630 keyword: enable all monkey patches using _kwtemplater at reposetup
Christian Ebert <blacktrash@gmx.net>
parents: 6072
diff changeset
   535
    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
   536
    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
   537
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   538
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   539
cmdtable = {
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   540
    'kwdemo':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   541
        (demo,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   542
         [('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
   543
          ('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
   544
         _('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
   545
    'kwexpand': (expand, commands.walkopts,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   546
                 _('hg kwexpand [OPTION]... [FILE]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   547
    'kwfiles':
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   548
        (files,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   549
         [('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
   550
          ('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
   551
          ('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
   552
         ] + commands.walkopts,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   553
         _('hg kwfiles [OPTION]... [FILE]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   554
    'kwshrink': (shrink, commands.walkopts,
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   555
                 _('hg kwshrink [OPTION]... [FILE]...')),
0637d97a8cb4 Add extension for filewise RCS-keyword expansion in working dir
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   556
}