hgkw/keyword.py
changeset 264 7c637046c0e2
parent 263 fc7f60b109b6
child 265 1b6b7835e3f9
--- a/hgkw/keyword.py	Sat Oct 06 23:11:40 2007 +0200
+++ b/hgkw/keyword.py	Thu Oct 11 13:10:33 2007 +0200
@@ -83,7 +83,7 @@
 from mercurial import commands, cmdutil, context, fancyopts
 from mercurial import filelog, localrepo, revlog, templater, util
 from mercurial.i18n import gettext as _
-import getopt, re, shutil, sys, tempfile, time
+import getopt, os.path, re, shutil, sys, tempfile, time
 
 # backwards compatibility hacks
 
@@ -104,6 +104,13 @@
 except AttributeError:
     bail_if_changed = commands.bail_if_changed
 
+def _pathto(repo, cwd, f):
+    '''kwfiles behaves similar to status, using pathto since 78b6add1f966.'''
+    try:
+        return repo.pathto(f, cwd)
+    except AttributeError:
+        return f
+
 # commands.parse/cmdutil.parse returned nothing for
 # "hg diff --rev" before 88803a69b24a due to bug in fancyopts
 def _fancyopts(args, options, state):
@@ -300,74 +307,79 @@
             return t2 != text
         return revlog.revlog.cmp(self, node, text)
 
-_nokwfiles = 'no files configured for keyword expansion'
+def _weedfiles(ui, repo, *pats, **opts):
+    '''Collects files matching arguments and/or walkopts,
+    sorts out those that are configured for keyword expansion
+    and not links, and returns both lists.'''
+    files, match, anypats = cmdutil.matchpats(repo, pats, opts)
+    status = repo.status(files=files, match=match, list_clean=True)
+    modified, added, removed, deleted, unknown, ignored, clean = status
+    files = modified + added + clean
+    files.sort()
+    try:
+        # use the full definition of repo._link for backwards compatibility
+        kwfiles = [f for f in files if ui.kwfmatcher(f)
+                   and not os.path.islink(repo.wjoin(f))]
+        return kwfiles, files
+    except AttributeError:
+        if ui.configitems('keyword'):
+            err = _('[keyword] patterns cannot match')
+        else:
+            err = _('no [keyword] patterns configured')
+        raise util.Abort(err)
 
-def _weedcandidates(ui, man, candidates):
-    '''Weeds out files that do not match keyword file matcher,
-    are not tracked, or are links.'''
-    files = man.keys()
-    if candidates:
-        return [f for f in candidates if ui.kwfmatcher(f)
-                and f in files and not man.linkf(f)]
-    return [f for f in files if ui.kwfmatcher(f) and not man.linkf(f)]
-
-def _overwrite(ui, repo, files, expand):
+def _overwrite(ui, repo, expand, *pats, **opts):
     '''Expands/shrinks keywords in working directory.'''
-    if not hasattr(ui, 'kwfmatcher'):
-        ui.warn(_('%s\n') % _nokwfiles)
-        return
     bail_if_changed(repo)
     wlock = lock = None
     try:
         wlock = repo.wlock()
         lock = repo.lock()
-        ctx = repo.changectx()
-        man = ctx.manifest()
-        if files:
-            cwd = repo.getcwd()
-            files = [util.canonpath(repo.root, cwd, f) for f in files]
-        files = _weedcandidates(ui, man, files)
-        if files:
+        kwfiles, files = _weedfiles(ui, repo, *pats, **opts)
+        if kwfiles:
+            ctx = repo.changectx()
             kwt = kwtemplater(ui, repo, expand, node=ctx.node())
             # 3rd argument sets commit to False
-            kwt.overwrite(files, man, False)
+            kwt.overwrite(kwfiles, ctx.manifest(), False)
     finally:
         del wlock, lock
 
 
-def shrink(ui, repo, *args):
+def shrink(ui, repo, *pats, **opts):
     '''revert expanded keywords in working directory
 
     Run before changing/disabling active keywords
     or if you experience problems with "hg import" or "hg merge".
     '''
-    # 4th argument sets expansion to False
-    _overwrite(ui, repo, args, False)
+    # 3rd argument sets expansion to False
+    _overwrite(ui, repo, False, *pats, **opts)
 
-def expand(ui, repo, *args):
+def expand(ui, repo, *pats, **opts):
     '''expand keywords in working directory
 
     Run after (re)enabling keyword expansion.
     '''
-    # 4th argument sets expansion to True
-    _overwrite(ui, repo, args, True)
+    # 3rd argument sets expansion to True
+    _overwrite(ui, repo, True, *pats, **opts)
 
-def files(ui, repo):
+def files(ui, repo, *pats, **opts):
     '''print files currently configured for keyword expansion
 
     Crosscheck which files in working directory
     are matched by [keyword] config patterns.
     '''
-    if not hasattr(ui, 'kwfmatcher'):
-        ui.note(_('%s\n') % _nokwfiles)
-        return
-    ctx = repo.workingctx()
-    man = ctx.manifest().copy()
-    for f in ctx.unknown():
-        del man[f]
-    files = _weedcandidates(ui, man, None)
-    files.sort()
-    ui.write('\n'.join(files) + '\n')
+    kwfiles, files = _weedfiles(ui, repo, *pats, **opts)
+    cwd = pats and repo.getcwd() or ''
+    flag = opts['all'] and 1 or 0
+    ignored = opts['ignored']
+    if not ignored:
+        format = ('%s\n', 'K %s\n')[flag]
+        for k in kwfiles:
+            ui.write(format % _pathto(repo, cwd, k))
+    if flag or ignored:
+        format = ('%s\n', 'I %s\n')[flag]
+        for i in [f for f in files if f not in kwfiles]:
+            ui.write(format % _pathto(repo, cwd, i))
 
 def demo(ui, repo, *args, **opts):
     '''print [keywordmaps] configuration and an expansion example
@@ -519,7 +531,8 @@
                 if node is not None:
                     cl = self.changelog.read(node)
                     mn = self.manifest.read(cl[0])
-                    candidates = _weedcandidates(ui, mn, cl[3])
+                    candidates = [f for f in cl[3] if ui.kwfmatcher(f)
+                                  and mn.has_key(f) and not mn.linkf(f)]
                     if candidates:
                         # 3rd argument sets expansion to True
                         kwt = kwtemplater(ui, self, True, node=node)
@@ -538,7 +551,14 @@
          [('d', 'default', None, _('show default keyword template maps')),
           ('f', 'rcfile', [], _('read maps from RCFILE'))],
          _('hg kwdemo [-d] [-f RCFILE] [TEMPLATEMAP ...]')),
-    'kwfiles': (files, [], _('hg kwfiles')),
-    'kwshrink': (shrink, [], _('hg kwshrink [NAME] ...')),
-    'kwexpand': (expand, [], _('hg kwexpand [NAME] ...')),
+    'kwfiles':
+        (files,
+         [('i', 'ignored', None, _('show files ignored by [keyword] patterns')),
+          ('a', 'all', None, _('show keyword status flags of all files')),
+         ] + commands.walkopts,
+         _('hg kwfiles [OPTION]... [FILE]...')),
+    'kwshrink': (shrink, commands.walkopts,
+                 _('hg kwshrink [OPTION]... [FILE]...')),
+    'kwexpand': (expand, commands.walkopts,
+                 _('hg kwexpand [OPTION]... [FILE]...')),
 }