hgkw/keyword.py
changeset 227 8ae160f24b9f
parent 226 5255b00fbba5
child 228 88362a9b5afd
equal deleted inserted replaced
226:5255b00fbba5 227:8ae160f24b9f
    78 Caveat: "hg import" fails if the patch context contains an active
    78 Caveat: "hg import" fails if the patch context contains an active
    79         keyword. In that case run "hg kwshrink", and then reimport.
    79         keyword. In that case run "hg kwshrink", and then reimport.
    80         Or, better, use bundle/unbundle to share changes.
    80         Or, better, use bundle/unbundle to share changes.
    81 '''
    81 '''
    82 
    82 
    83 from mercurial import commands, cmdutil, context, fancyopts
    83 from mercurial import commands, cmdutil, context, fancyopts, filelog
    84 from mercurial import filelog, localrepo, templater, util, hg
    84 from mercurial import localrepo, templater, util, hg
    85 from mercurial.i18n import gettext as _
    85 from mercurial.i18n import gettext as _
    86 import re, shutil, sys, tempfile, time
    86 import getopt, re, shutil, sys, tempfile, time
       
    87 
       
    88 # backwards compatibility hacks
    87 
    89 
    88 # findcmd, bail_if_changed were in commands until 0c61124ad877
    90 # findcmd, bail_if_changed were in commands until 0c61124ad877
    89 try:
    91 try:
    90     findcmd = cmdutil.findcmd
    92     findcmd = cmdutil.findcmd
    91     bail_if_changed = cmdutil.bail_if_changed
    93     bail_if_changed = cmdutil.bail_if_changed
    92 except AttributeError:
    94 except AttributeError:
    93     findcmd = commands.findcmd
    95     findcmd = commands.findcmd
    94     bail_if_changed = commands.bail_if_changed
    96     bail_if_changed = commands.bail_if_changed
       
    97 
       
    98 # cmdutil.parse moves to dispatch._parse in 18a9fbb5cd78
       
    99 try:
       
   100     from mercurial import dispatch
       
   101 except ImportError:
       
   102     pass
       
   103 
       
   104 # commands.parse/cmdutil.parse returned nothing for
       
   105 # "hg diff --rev" before 88803a69b24a due to bug in fancyopts
       
   106 def _fancyopts(args, options, state):
       
   107     '''Fixed fancyopts from 88803a69b24a.'''
       
   108     long = []
       
   109     short = ''
       
   110     map = {}
       
   111     dt = {}
       
   112     for s, l, d, c in options:
       
   113         pl = l.replace('-', '_')
       
   114         map['-'+s] = map['--'+l] = pl
       
   115         if isinstance(d, list):
       
   116             state[pl] = d[:]
       
   117         else:
       
   118             state[pl] = d
       
   119         dt[pl] = type(d)
       
   120         if (d is not None and d is not True and d is not False and
       
   121             not callable(d)):
       
   122             if s: s += ':'
       
   123             if l: l += '='
       
   124         if s: short = short + s
       
   125         if l: long.append(l)
       
   126     opts, args = getopt.getopt(args, short, long)
       
   127     for opt, arg in opts:
       
   128         if dt[map[opt]] is type(fancyopts): state[map[opt]](state, map[opt], arg)
       
   129         elif dt[map[opt]] is type(1): state[map[opt]] = int(arg)
       
   130         elif dt[map[opt]] is type(''): state[map[opt]] = arg
       
   131         elif dt[map[opt]] is type([]): state[map[opt]].append(arg)
       
   132         elif dt[map[opt]] is type(None): state[map[opt]] = True
       
   133         elif dt[map[opt]] is type(False): state[map[opt]] = True
       
   134     return args
       
   135 
       
   136 fancyopts.fancyopts = _fancyopts
       
   137 
    95 
   138 
    96 commands.optionalrepo += ' kwdemo'
   139 commands.optionalrepo += ' kwdemo'
    97 
   140 
    98 def utcdate(date):
   141 def utcdate(date):
    99     '''Returns hgdate in cvs-like UTC format.'''
   142     '''Returns hgdate in cvs-like UTC format.'''
   395     nokwcommands = ['add', 'addremove', 'bundle', 'clone', 'copy', 'export',
   438     nokwcommands = ['add', 'addremove', 'bundle', 'clone', 'copy', 'export',
   396                     'grep', 'identify', 'incoming', 'init', 'outgoing', 'push',
   439                     'grep', 'identify', 'incoming', 'init', 'outgoing', 'push',
   397                     'remove', 'rename', 'rollback']
   440                     'remove', 'rename', 'rollback']
   398 
   441 
   399     def _getcmd():
   442     def _getcmd():
   400         # commands or cmdutil.parse(ui, sys.argv[1:])[0]
   443         try:
   401         # is broken before 88803a69b24a (fancyopts)
   444             return dispatch._parse(ui, sys.argv[1:])[0]
   402         # results in no output from "hg diff --rev" with older hg versions
   445         except (ImportError, NameError):
   403         args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts, {})
   446             try:
   404         if args:
   447                 return cmdutil.parse(ui, sys.argv[1:])[0]
   405             cmd = args[0]
   448             except AttributeError:
   406             aliases, i = findcmd(ui, cmd)
   449                 return commands.parse(ui, sys.argv[1:])[0]
   407             return aliases[0]
       
   408 
   450 
   409     if not repo.local() or _getcmd() in nokwcommands:
   451     if not repo.local() or _getcmd() in nokwcommands:
   410         return
   452         return
   411 
   453 
   412     kwfmatcher = _keywordmatcher(ui, repo)
   454     kwfmatcher = _keywordmatcher(ui, repo)