hgkw/keyword.py
branchstable
changeset 404 45d3ea301c03
parent 401 05d0c3e4efdc
child 406 2069334ec1d0
equal deleted inserted replaced
403:9a93b5d66e65 404:45d3ea301c03
    94                 ' log outgoing push remove rename rollback tip'
    94                 ' log outgoing push remove rename rollback tip'
    95                 ' convert email glog')
    95                 ' convert email glog')
    96 
    96 
    97 # hg commands that trigger expansion only when writing to working dir,
    97 # hg commands that trigger expansion only when writing to working dir,
    98 # not when reading filelog, and unexpand when reading from working dir
    98 # not when reading filelog, and unexpand when reading from working dir
    99 restricted = 'diff1 record qfold qimport qnew qpush qrefresh qrecord'
    99 restricted = 'record qfold qimport qnew qpush qrefresh qrecord'
   100 
   100 
   101 def utcdate(date):
   101 def utcdate(date):
   102     '''Returns hgdate in cvs-like UTC format.'''
   102     '''Returns hgdate in cvs-like UTC format.'''
   103     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   103     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
   104 
   104 
   105 
   105 
   106 _kwtemplater = _cmd = _cmdoptions = None
   106 _kwtemplater = _cmd = _cmdoptions = None
   107  
   107  
   108 # store originals of monkeypatches
   108 # store originals of monkeypatches
   109 _patchfile_init = patch.patchfile.__init__
   109 _patchfile_init = patch.patchfile.__init__
       
   110 _patch_diff = patch.diff
   110 _dispatch_parse = dispatch._parse
   111 _dispatch_parse = dispatch._parse
   111 
   112 
   112 def _kwpatchfile_init(self, ui, fname, missing=False):
   113 def _kwpatchfile_init(self, ui, fname, missing=False):
   113     '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
   114     '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
   114     rejects or conflicts due to expanded keywords in working dir.'''
   115     rejects or conflicts due to expanded keywords in working dir.'''
   116     if _kwtemplater.matcher(self.fname):
   117     if _kwtemplater.matcher(self.fname):
   117         # shrink keywords read from working dir
   118         # shrink keywords read from working dir
   118         kwshrunk = _kwtemplater.shrink(''.join(self.lines))
   119         kwshrunk = _kwtemplater.shrink(''.join(self.lines))
   119         self.lines = kwshrunk.splitlines(True)
   120         self.lines = kwshrunk.splitlines(True)
   120 
   121 
       
   122 def _kw_diff(repo, node1=None, node2=None, files=None, match=util.always,
       
   123              fp=None, changes=None, opts=None):
       
   124     # only expand if comparing against working dir
       
   125     if node2 is not None:
       
   126         _kwtemplater.matcher = util.never
       
   127     if node1 is not None and node1 != repo.changectx().node():
       
   128         _kwtemplater.restrict = True
       
   129     _patch_diff(repo, node1=node1, node2=node2, files=files, match=match,
       
   130                 fp=fp, changes=changes, opts=opts)
       
   131 
   121 def _kwweb_changeset(web, req, tmpl):
   132 def _kwweb_changeset(web, req, tmpl):
   122     '''Wraps webcommands.changeset turning off keyword expansion.'''
   133     '''Wraps webcommands.changeset turning off keyword expansion.'''
   123     _kwtemplater.matcher = util.never
   134     _kwtemplater.matcher = util.never
   124     return web.changeset(tmpl, web.changectx(req))
   135     return web.changeset(tmpl, web.changectx(req))
   125 
   136 
   127     '''Wraps webcommands.filediff turning off keyword expansion.'''
   138     '''Wraps webcommands.filediff turning off keyword expansion.'''
   128     _kwtemplater.matcher = util.never
   139     _kwtemplater.matcher = util.never
   129     return web.filediff(tmpl, web.filectx(req))
   140     return web.filediff(tmpl, web.filectx(req))
   130 
   141 
   131 def _kwdispatch_parse(ui, args):
   142 def _kwdispatch_parse(ui, args):
   132     '''Monkeypatch dispatch._parse to obtain
   143     '''Monkeypatch dispatch._parse to obtain running hg command.'''
   133     current command and command options (global _cmd, _cmdoptions).'''
   144     global _cmd
   134     global _cmd, _cmdoptions
   145     _cmd, func, args, options, cmdoptions = _dispatch_parse(ui, args)
   135     _cmd, func, args, options, _cmdoptions = _dispatch_parse(ui, args)
   146     return _cmd, func, args, options, cmdoptions
   136     return _cmd, func, args, options, _cmdoptions
       
   137 
   147 
   138 # dispatch._parse is run before reposetup, so wrap it here
   148 # dispatch._parse is run before reposetup, so wrap it here
   139 dispatch._parse = _kwdispatch_parse
   149 dispatch._parse = _kwdispatch_parse
   140 
   150 
   141 
   151 
   434     keyword substitutions.
   444     keyword substitutions.
   435     This is done for local repos only, and only if there are
   445     This is done for local repos only, and only if there are
   436     files configured at all for keyword substitution.'''
   446     files configured at all for keyword substitution.'''
   437 
   447 
   438     global _kwtemplater
   448     global _kwtemplater
   439     hgcmd, hgcmdopts = _cmd, _cmdoptions
       
   440 
   449 
   441     try:
   450     try:
   442         if (not repo.local() or hgcmd in nokwcommands.split() 
   451         if (not repo.local() or _cmd in nokwcommands.split() 
   443             or '.hg' in util.splitpath(repo.root)
   452             or '.hg' in util.splitpath(repo.root)
   444             or repo._url.startswith('bundle:')):
   453             or repo._url.startswith('bundle:')):
   445             return
   454             return
   446     except AttributeError:
   455     except AttributeError:
   447         pass
   456         pass
   453         else:
   462         else:
   454             exc.append(pat)
   463             exc.append(pat)
   455     if not inc:
   464     if not inc:
   456         return
   465         return
   457 
   466 
   458     if hgcmd == 'diff':
   467     _kwtemplater = kwtemplater(ui, repo, inc, exc, _cmd)
   459         # only expand if comparing against working dir
       
   460         node1, node2 = cmdutil.revpair(repo, hgcmdopts.get('rev'))
       
   461         if node2 is not None:
       
   462             return
       
   463         # shrink if rev is not current node
       
   464         if node1 is not None and node1 != repo.changectx().node():
       
   465             hgcmd = 'diff1'
       
   466 
       
   467     _kwtemplater = kwtemplater(ui, repo, inc, exc, hgcmd)
       
   468 
   468 
   469     class kwrepo(repo.__class__):
   469     class kwrepo(repo.__class__):
   470         def file(self, f, kwmatch=False):
   470         def file(self, f, kwmatch=False):
   471             if f[0] == '/':
   471             if f[0] == '/':
   472                 f = f[1:]
   472                 f = f[1:]
   523             finally:
   523             finally:
   524                 del wlock, lock
   524                 del wlock, lock
   525 
   525 
   526     repo.__class__ = kwrepo
   526     repo.__class__ = kwrepo
   527     patch.patchfile.__init__ = _kwpatchfile_init
   527     patch.patchfile.__init__ = _kwpatchfile_init
       
   528     patch.diff = _kw_diff
   528     webcommands.changeset = webcommands.rev = _kwweb_changeset
   529     webcommands.changeset = webcommands.rev = _kwweb_changeset
   529     webcommands.filediff = webcommands.diff = _kwweb_filediff
   530     webcommands.filediff = webcommands.diff = _kwweb_filediff
   530 
   531 
   531 
   532 
   532 cmdtable = {
   533 cmdtable = {