changeset 5111:12930b97a729

imerge: gussy up dispatcher to support subcommand opts. Add -n, --resolved, --unresolved to status.
author Brendan Cully <brendan@kublai.com>
date Sat, 04 Aug 2007 12:26:48 -0700
parents 420e1166a876
children 568bb3b4b815 35f67dd712d0
files hgext/imerge.py tests/test-imerge tests/test-imerge.out
diffstat 3 files changed, 59 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/imerge.py	Sat Aug 04 09:33:10 2007 -0700
+++ b/hgext/imerge.py	Sat Aug 04 12:26:48 2007 -0700
@@ -7,7 +7,7 @@
 
 from mercurial.i18n import _
 from mercurial.node import *
-from mercurial import commands, cmdutil, hg, merge, util
+from mercurial import commands, cmdutil, fancyopts, hg, merge, util
 import os, tarfile
 
 class InvalidStateFileException(Exception): pass
@@ -135,27 +135,6 @@
         if p1.node() != dp[0] or p2.node() != dp[1]:
             raise util.Abort('imerge state does not match working directory')
 
-    def status(self):
-        p1, p2 = self.wctx.parents()
-        self.ui.write('merging %s and %s\n' % \
-                      (short(p1.node()), short(p2.node())))
-
-        if self.resolved:
-            self.ui.write('resolved:\n')
-            for fn in self.resolved:
-                self.ui.write('  %s\n' % fn)
-        remaining = [f for f in self.conflicts if f not in self.resolved]
-        if remaining:
-            self.ui.write('remaining:\n')
-            for fn in remaining:
-                (fd, fo) = self.conflicts[fn]
-                if fn == fo:
-                    self.ui.write('  %s\n' % (fn,))
-                else:
-                    self.ui.write('  %s (%s)\n' % (fn, fd))
-        else:
-            self.ui.write('all conflicts resolved\n')
-
     def next(self):
         remaining = self.remaining()
         return remaining and remaining[0]
@@ -202,7 +181,7 @@
 
     rc = im.unpickle(source)
     if not rc:
-        im.status()
+        status(im)
     return rc
 
 def merge_(im, filename=None):
@@ -236,8 +215,39 @@
 def save(im, dest):
     return im.pickle(dest)
 
-def status(im):
-    im.status()
+def status(im, **opts):
+    if not opts.get('resolved') and not opts.get('unresolved'):
+        opts['resolved'] = True
+        opts['unresolved'] = True
+
+    if im.ui.verbose:
+        p1, p2 = [short(p.node()) for p in im.wctx.parents()]
+        im.ui.note(_('merging %s and %s\n') % (p1, p2))
+
+    conflicts = im.conflicts.keys()
+    conflicts.sort()
+    remaining = dict.fromkeys(im.remaining())
+    st = []
+    for fn in conflicts:
+        if opts.get('no_status'):
+            mode = ''
+        elif fn in remaining:
+            mode = 'U '
+        else:
+            mode = 'R '
+        if ((opts.get('resolved') and fn not in remaining)
+            or (opts.get('unresolved') and fn in remaining)):
+            st.append((mode, fn))
+    st.sort()
+    for (mode, fn) in st:
+        if im.ui.verbose:
+            fo, fd = im.conflicts[fn]
+            if fd != fn:
+                fn = '%s (%s)' % (fn, fd)
+        im.ui.write('%s%s\n' % (mode, fn))
+    if opts.get('unresolved') and not remaining:
+        im.ui.write(_('all conflicts resolved\n'))
+
     return 0
 
 def unresolve(im, *files):
@@ -246,13 +256,16 @@
     return im.unresolve(files)
 
 subcmdtable = {
-    'load': load,
-    'merge': merge_,
-    'next': next,
-    'resolve': resolve,
-    'save': save,
-    'status': status,
-    'unresolve': unresolve
+    'load': (load, []),
+    'merge': (merge_, []),
+    'next': (next, []),
+    'resolve': (resolve, []),
+    'save': (save, []),
+    'status': (status,
+               [('n', 'no-status', None, _('hide status prefix')),
+                ('', 'resolved', None, _('only show resolved conflicts')),
+                ('', 'unresolved', None, _('only show unresolved conflicts'))]),
+    'unresolve': (unresolve, [])
 }
 
 def dispatch(im, args, opts):
@@ -263,7 +276,7 @@
                 candidates.append(choice)
         return candidates
 
-    c, args = args[0], args[1:]
+    c, args = args[0], list(args[1:])
     cmd = complete(c, subcmdtable.keys())
     if not cmd:
         raise cmdutil.UnknownCommand('imerge ' + c)
@@ -272,11 +285,15 @@
         raise cmdutil.AmbiguousCommand('imerge ' + c, cmd)
     cmd = cmd[0]
 
-    func = subcmdtable[cmd]
+    func, optlist = subcmdtable[cmd]
+    opts = {}
     try:
-        return func(im, *args)
+        args = fancyopts.fancyopts(args, optlist, opts)
+        return func(im, *args, **opts)
+    except fancyopts.getopt.GetoptError, inst:
+        raise cmdutil.ParseError('imerge', '%s: %s' % (cmd, inst))
     except TypeError:
-        raise cmdutil.ParseError('imerge', '%s: invalid arguments' % cmd)
+        raise cmdutil.ParseError('imerge', _('%s: invalid arguments') % cmd)
 
 def imerge(ui, repo, *args, **opts):
     '''interactive merge
--- a/tests/test-imerge	Sat Aug 04 09:33:10 2007 -0700
+++ b/tests/test-imerge	Sat Aug 04 12:26:48 2007 -0700
@@ -27,8 +27,8 @@
 cat foo2
 cat bar
 
-echo % status
-hg imerge st
+echo % status -v
+hg -v imerge st
 
 echo % next
 hg imerge next
--- a/tests/test-imerge.out	Sat Aug 04 09:33:10 2007 -0700
+++ b/tests/test-imerge.out	Sat Aug 04 12:26:48 2007 -0700
@@ -4,17 +4,14 @@
 % start imerge
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 (branch merge, don't forget to commit)
-merging e6da46716401 and 30d266f502e7
-remaining:
-  foo (foo2)
+U foo
 foo
 bar
 bar
 bar
-% status
+% status -v
 merging e6da46716401 and 30d266f502e7
-remaining:
-  foo (foo2)
+U foo (foo2)
 % next
 foo
 % merge next
@@ -30,9 +27,7 @@
 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 (branch merge, don't forget to commit)
-merging e6da46716401 and 30d266f502e7
-resolved:
-  foo
+R foo
 all conflicts resolved
 foo
 changeset:   3:fa9a6defdcaf