hgkw/keyword.py
author Christian Ebert <blacktrash@gmx.net>
Fri, 30 Mar 2007 09:08:48 +0200
branchself_initializing_hook
changeset 157 64dce6787d82
parent 98 121da9c0a325
child 192 0d2a6c9f8343
permissions -rw-r--r--
Incorporate changes in self_initializing_hook branch Implement configurable expansion based on Mercurial templates. NOTE: Relying on pretxncommit-hook to write in working directory might break in the future as it relies on a buggy race condidition. [issue273]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     1
# keyword.py - keyword expansion for Mercurial
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     2
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     3
# Copyright 2007 Christian Ebert <blacktrash@gmx.net>
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     4
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     5
# This software may be used and distributed according to the terms
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     7
#
82
9bf0f7db5928 Add keyword; comment backwards compatible import
Christian Ebert <blacktrash@gmx.net>
parents: 81
diff changeset
     8
# $Id$
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
     9
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    10
# Keyword expansion hack against the grain of a DSCM
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    11
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    12
# There are many good reasons why this is not needed in a distributed
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    13
# SCM, still it may be useful in very small projects based on single
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    14
# files (like LaTeX packages), that are mostly addressed to an audience
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    15
# not running a version control system.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    16
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    17
# For in-depth discussion refer to
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    18
# <http://www.selenic.com/mercurial/wiki/index.cgi/KeywordPlan>.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    19
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    20
# Keyword expansion is based on Mercurial's changeset template mappings.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    21
# The extension provides an additional UTC-date filter ({date|utcdate}).
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    22
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    23
# The user has the choice either to create his own keywords and their
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    24
# expansions or to use the CVS-like default ones.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    25
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    26
# Expansions spanning more than one line are truncated to their first line.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    27
# Incremental expansion (like CVS' $Log$) is not supported.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    28
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    29
# Binary files are not touched.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    30
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    31
# Setup in hgrc:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    32
#
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    33
#     # enable extension
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    34
#     keyword = /full/path/to/keyword.py
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    35
#     # or, if script in hgext folder:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    36
#     # hgext.keyword =
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    37
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    38
'''keyword expansion in local repositories
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    39
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    40
This extension expands RCS/CVS-like or self-customized keywords in
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    41
the text files selected by your configuration.
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    42
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    43
Keywords are only expanded in local repositories and not logged by
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    44
Mercurial internally. The mechanism can be regarded as a convenience
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    45
for the current user and may be turned off anytime.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    46
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    47
The exansion works in 2 modes:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    48
    1) working mode: substitution takes place on every commit and
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    49
       update of the working repository.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    50
    2) archive mode: substitution is only triggered by "hg archive".
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    51
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    52
Caveat: "hg import" might fail if the patches were exported from a
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    53
repo with a different/no keyword setup, whereas "hg unbundle" is
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    54
safe.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    55
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    56
Configuration is done in the [keyword] and [keywordmaps] sections of
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    57
hgrc files.
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    58
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    59
Example:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    60
     [keyword]
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    61
     # filename patterns for expansion are configured in this section
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    62
     **.py =          ## expand keywords in all python files
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    63
     x* = ignore      ## but ignore files matching "x*"
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    64
     ** = archive     ## keywords in all textfiles are expanded
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    65
                      ## when creating a distribution
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    66
     y* = noarchive   ## keywords in files matching "y*" are not expanded
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    67
                      ## on archive creation
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    68
     ...
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    69
     [keywordmaps]
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    70
     # custom hg template maps _replace_ the CVS-like default ones
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    71
     HGdate = {date|rfc822date}
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    72
     lastlog = {desc} ## same as {desc|firstline} in this context
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    73
     checked in by = {author}
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    74
     ...
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    75
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    76
If no [keywordmaps] are configured the extension falls back on the
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    77
following defaults:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    78
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    79
     Revision: changeset id
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    80
     Author: username
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    81
     Date: %Y/%m/%d %H:%M:%S      ## [UTC]
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    82
     RCSFile: basename,v
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    83
     Source: /path/to/basename,v
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    84
     Id: basename,v csetid %Y/%m/%d %H:%M:%S username
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    85
     Header: /path/to/basename,v csetid %Y/%m/%d %H:%M:%S username
48
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    86
'''
59fedb6b41da add header and start documentation
Christian Ebert <blacktrash@gmx.net>
parents: 47
diff changeset
    87
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    88
from mercurial.node import *
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    89
try:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    90
    from mercurial.demandload import * # stable
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    91
    from mercurial.i18n import gettext as _
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    92
    demandload(globals(), 'mercurial:commands,fancyopts,templater,util')
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    93
    demandload(globals(), 'mercurial:cmdutil,context,filelog')
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    94
    demandload(globals(), 'os re sys time')
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    95
except ImportError:                    # demandimport
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    96
    from mercurial.i18n import _
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    97
    from mercurial import commands, fancyopts, templater, util
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    98
    from mercurial import cmdutil, context, filelog
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
    99
    import os, re, sys, time
59
94b26168791d Only 1 all-purpose regex, compiled at load
Christian Ebert <blacktrash@gmx.net>
parents: 58
diff changeset
   100
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   101
deftemplates = {
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   102
        'Revision': '{node|short}',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   103
        'Author': '{author|user}',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   104
        'Date': '{date|utcdate}',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   105
        'RCSFile': '{file|basename},v',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   106
        'Source': '{root}/{file},v',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   107
        'Id': '{file|basename},v {node|short} {date|utcdate} {author|user}',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   108
        'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   109
        }
65
188849659487 Unify obtaining filename matchers; only debug messages
Christian Ebert <blacktrash@gmx.net>
parents: 64
diff changeset
   110
93
9f70f953dd3b Clean keyword arg assignment in kwfilelog.add; rename utc() to utcdate()
Christian Ebert <blacktrash@gmx.net>
parents: 92
diff changeset
   111
def utcdate(date):
92
3c7c187e4001 Init context.filectx only once per file with class kwfilectx
Christian Ebert <blacktrash@gmx.net>
parents: 90
diff changeset
   112
    '''Returns hgdate in cvs-like UTC format.'''
93
9f70f953dd3b Clean keyword arg assignment in kwfilelog.add; rename utc() to utcdate()
Christian Ebert <blacktrash@gmx.net>
parents: 92
diff changeset
   113
    return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
92
3c7c187e4001 Init context.filectx only once per file with class kwfilectx
Christian Ebert <blacktrash@gmx.net>
parents: 90
diff changeset
   114
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   115
def getcmd(ui):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   116
    '''Returns current hg command.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   117
    # commands.parse(ui, sys.argv[1:])[0] breaks "hg diff -r"
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   118
    try:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   119
        args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {})
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   120
    except fancyopts.getopt.GetoptError, inst:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   121
        raise commands.ParseError(None, inst)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   122
    if args:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   123
        cmd = args[0]
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   124
        aliases, i = commands.findcmd(ui, cmd)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   125
        return aliases[0]
92
3c7c187e4001 Init context.filectx only once per file with class kwfilectx
Christian Ebert <blacktrash@gmx.net>
parents: 90
diff changeset
   126
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   127
class kwtemplater(object):
92
3c7c187e4001 Init context.filectx only once per file with class kwfilectx
Christian Ebert <blacktrash@gmx.net>
parents: 90
diff changeset
   128
    '''
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   129
    Sets up keyword templates, corresponding keyword regex, and
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   130
    provides keyword substitution functions.
92
3c7c187e4001 Init context.filectx only once per file with class kwfilectx
Christian Ebert <blacktrash@gmx.net>
parents: 90
diff changeset
   131
    '''
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   132
    def __init__(self, ui, repo):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   133
        self.ui = ui
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   134
        self.repo = repo
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   135
        templates = dict(ui.configitems('keywordmaps'))
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   136
        if templates:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   137
            # parse templates here for less overhead in kwsub matchfunc
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   138
            for k in templates.keys():
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   139
                templates[k] = templater.parsestring(templates[k],
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   140
                        quoted=False)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   141
        self.templates = templates or deftemplates
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   142
        self.re_kw = re.compile(r'\$(%s)[^$]*?\$' %
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   143
                '|'.join([re.escape(k) for k in self.templates.keys()]))
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   144
        templater.common_filters['utcdate'] = utcdate
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   145
        try:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   146
            self.t = cmdutil.changeset_templater(ui, repo,
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   147
                    False, '', False)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   148
        except TypeError:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   149
            # depending on hg rev changeset_templater has extra "brinfo" arg
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   150
            self.t = cmdutil.changeset_templater(ui, repo,
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   151
                    False, None, '', False)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   152
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   153
    def kwsub(self, mobj, path, node):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   154
        '''Substitutes keyword using corresponding template.'''
96
682e684a15e9 Expansion entirely in kwfilectx.expand()
Christian Ebert <blacktrash@gmx.net>
parents: 94
diff changeset
   155
        kw = mobj.group(1)
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   156
        self.t.use_template(self.templates[kw])
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   157
        self.ui.pushbuffer()
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   158
        self.t.show(changenode=node, root=self.repo.root, file=path)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   159
        keywordsub = templater.firstline(self.ui.popbuffer())
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   160
        return '$%s: %s $' % (kw, keywordsub)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   161
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   162
    def expand(self, path, node, filelog, data):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   163
        '''Returns data with expanded keywords.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   164
        if util.binary(data):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   165
            return data
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   166
        c = context.filectx(self.repo, path, fileid=node, filelog=filelog)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   167
        cnode = c.node()
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   168
        return self.re_kw.sub(lambda m: self.kwsub(m, path, cnode), data)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   169
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   170
    def shrink(self, text):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   171
        '''Returns text with all keyword substitutions removed.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   172
        if util.binary(text):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   173
            return text
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   174
        return self.re_kw.sub(r'$\1$', text)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   175
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   176
    def overwrite(self, candidates, node):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   177
        '''Overwrites candidates in working dir expanding keywords.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   178
        for f in candidates:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   179
            data = self.repo.wfile(f).read()
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   180
            if not util.binary(data):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   181
                data, kwct = self.re_kw.subn(lambda m:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   182
                        self.kwsub(m, f, node), data)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   183
                if kwct:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   184
                    self.ui.debug(_('overwriting %s expanding keywords\n') % f)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   185
                    self.repo.wfile(f, 'w').write(data)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   186
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   187
class kwfilelog(filelog.filelog):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   188
    '''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   189
    Superclass over filelog to customize its read, add, cmp methods.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   190
    Keywords are "stored" unexpanded, and expanded on reading.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   191
    '''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   192
    def __init__(self, opener, path, kwtemplater):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   193
        super(kwfilelog, self).__init__(opener, path)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   194
        self.path = path
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   195
        self.kwtemplater = kwtemplater
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   196
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   197
    def read(self, node):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   198
        '''Substitutes keywords when reading filelog.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   199
        data = super(kwfilelog, self).read(node)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   200
        return self.kwtemplater.expand(self.path, node, self, data)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   201
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   202
    def add(self, text, meta, tr, link, p1=None, p2=None):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   203
        '''Removes keyword substitutions when adding to filelog.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   204
        text = self.kwtemplater.shrink(text)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   205
        return super(kwfilelog, self).add(text,
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   206
                        meta, tr, link, p1=p1, p2=p2)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   207
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   208
    def cmp(self, node, text):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   209
        '''Removes keyword substitutions for comparison.'''
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   210
        text = self.kwtemplater.shrink(text)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   211
        return super(kwfilelog, self).cmp(node, text)
92
3c7c187e4001 Init context.filectx only once per file with class kwfilectx
Christian Ebert <blacktrash@gmx.net>
parents: 90
diff changeset
   212
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
   213
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   214
def reposetup(ui, repo):
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   215
    '''Sets up repo as kwrepo for keyword substitution.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   216
    Overrides file method to return kwfilelog instead of filelog
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   217
    if file matches user configuration.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   218
    Uses self-initializing pretxncommit-hook to overwrite configured files with
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   219
    updated keyword substitutions.
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   220
    This is done for local repos only, and only if there are
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   221
    files configured at all for keyword substitution.'''
64
4cd7b993c5f8 Imports specific to functions
Christian Ebert <blacktrash@gmx.net>
parents: 63
diff changeset
   222
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   223
    if not repo.local():
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   224
        return
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   225
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   226
    archivemode = (getcmd(repo.ui) == 'archive')
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   227
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   228
    inc, exc, archive, noarchive = [], ['.hg*'], [], ['.hg*']
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   229
    for pat, opt in repo.ui.configitems('keyword'):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   230
        if opt == 'archive':
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   231
            archive.append(pat)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   232
        elif opt == 'noarchive':
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   233
            noarchive.append(pat)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   234
        elif opt == 'ignore':
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   235
            exc.append(pat)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   236
        else:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   237
            inc.append(pat)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   238
    if archivemode:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   239
        inc, exc = archive, noarchive
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   240
    if not inc:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   241
        return
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   242
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   243
    repo.kwfmatcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   244
33
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   245
    class kwrepo(repo.__class__):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   246
        def file(self, f):
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   247
            if f[0] == '/':
ebb39c6a1476 Add original keyword extension by Thomas Arendsen Hain
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
   248
                f = f[1:]
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   249
            # only use kwfilelog when needed
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   250
            if self.kwfmatcher(f):
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   251
                kwt = kwtemplater(self.ui, self)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   252
                return kwfilelog(self.sopener, f, kwt)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   253
            else:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   254
                return filelog.filelog(self.sopener, f)
78
474b415433a1 Unexpanded storage hopefully covered now by adding kwfilelog.add (again!)
Christian Ebert <blacktrash@gmx.net>
parents: 77
diff changeset
   255
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   256
    repo.__class__ = kwrepo
42
ba000e29ecf3 Implement near CVS compability with more than one keyword
Christian Ebert <blacktrash@gmx.net>
parents: 41
diff changeset
   257
87
c193da9eb3e4 Only extend sys.path if needed
Christian Ebert <blacktrash@gmx.net>
parents: 86
diff changeset
   258
    # make pretxncommit hook import kwmodule regardless of where it's located
88
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   259
    for k, v in sys.modules.iteritems():
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   260
        if v is None:
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   261
            continue
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   262
        if not hasattr(v, '__file__'):
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   263
            continue
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   264
        if v.__file__.startswith(__file__):
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   265
            mod = k
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   266
            break
87
c193da9eb3e4 Only extend sys.path if needed
Christian Ebert <blacktrash@gmx.net>
parents: 86
diff changeset
   267
    else:
88
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   268
        sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   269
        mod = os.path.splitext(os.path.basename(__file__))[0]
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   270
    ui.setconfig('hooks', 'pretxncommit.keyword', 'python:%s.pretxnkw' % mod)
ffec227fced3 Module detection cleaner and more reliable
Christian Ebert <blacktrash@gmx.net>
parents: 87
diff changeset
   271
    del mod
81
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   272
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   273
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   274
def pretxnkw(ui, repo, hooktype, **args):
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   275
    '''pretxncommit hook that collects candidates for keyword expansion
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   276
    on commit and expands keywords in working dir.'''
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   277
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   278
    cmd, sysargs, globalopts, cmdopts = commands.parse(ui, sys.argv[1:])[1:]
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   279
    if repr(cmd).split()[1] in ('tag', 'import_'):
98
121da9c0a325 Omit hook test; return w/o explicit value (==False)
Christian Ebert <blacktrash@gmx.net>
parents: 96
diff changeset
   280
        return
81
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   281
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   282
    files, match, anypats = cmdutil.matchpats(repo, sysargs, cmdopts)
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   283
    modified, added = repo.status(files=files, match=match)[:2]
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   284
    candidates = [f for f in modified + added if repo.kwfmatcher(f)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   285
            and not os.path.islink(repo.wjoin(f))]
81
fd5d3a974ea7 Implement self initializing pretxncommit hook
Christian Ebert <blacktrash@gmx.net>
parents: 80
diff changeset
   286
157
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   287
    if candidates:
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   288
        kwt = kwtemplater(ui, repo)
64dce6787d82 Incorporate changes in self_initializing_hook branch
Christian Ebert <blacktrash@gmx.net>
parents: 98
diff changeset
   289
        kwt.overwrite(candidates, bin(args['node']))