[PATCH 2 of 3 resend] commands: refactor diff --stat and qdiff --stat

Yuya Nishihara yuya at tcha.org
Tue Apr 6 11:08:57 CDT 2010


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1270568720 -32400
# Node ID 884b72e36da4032bf2fe02c21833b7c1a4f2392b
# Parent  f60b5d4a2ad929be67fa2f011f28730a7203f25c
commands: refactor diff --stat and qdiff --stat

`opts['unified'] = '0'` can be replaced by `diffopts.context = 0`.

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -477,25 +477,9 @@ class queue(object):
     def printdiff(self, repo, diffopts, node1, node2=None, files=None,
                   fp=None, changes=None, opts={}):
         stat = opts.get('stat')
-
         m = cmdutil.match(repo, files, opts)
-        if fp is None:
-            write = repo.ui.write
-        else:
-            def write(s, **kw):
-                fp.write(s)
-        if stat:
-            diffopts.context = 0
-            width = self.ui.interactive() and util.termwidth() or 80
-            chunks = patch.diff(repo, node1, node2, m, changes, diffopts)
-            for chunk, label in patch.diffstatui(util.iterlines(chunks),
-                                                 width=width,
-                                                 git=diffopts.git):
-                write(chunk, label=label)
-        else:
-            for chunk, label in patch.diffui(repo, node1, node2, m, changes,
-                                              diffopts):
-                write(chunk, label=label)
+        cmdutil.diffordiffstat(self.ui, repo, diffopts, node1, node2,  m,
+                               changes, stat, fp)
 
     def mergeone(self, repo, mergeq, head, patch, rev, diffopts):
         # first try just applying the patch
diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -700,6 +700,28 @@ def export(repo, revs, template='hg-%h.p
     for seqno, rev in enumerate(revs):
         single(rev, seqno + 1, fp)
 
+def diffordiffstat(ui, repo, diffopts, node1, node2, match,
+                   changes=None, stat=False, fp=None):
+    '''show diff or diffstat.'''
+    if fp is None:
+        write = ui.write
+    else:
+        def write(s, **kw):
+            fp.write(s)
+
+    if stat:
+        diffopts.context = 0
+        width = ui.interactive() and util.termwidth() or 80
+        chunks = patch.diff(repo, node1, node2, match, changes, diffopts)
+        for chunk, label in patch.diffstatui(util.iterlines(chunks),
+                                             width=width,
+                                             git=diffopts.git):
+            write(chunk, label=label)
+    else:
+        for chunk, label in patch.diffui(repo, node1, node2, match,
+                                         changes, diffopts):
+            write(chunk, label=label)
+
 class changeset_printer(object):
     '''show changeset information when templating not requested.'''
 
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1169,21 +1169,10 @@ def diff(ui, repo, *pats, **opts):
     if reverse:
         node1, node2 = node2, node1
 
-    if stat:
-        opts['unified'] = '0'
     diffopts = patch.diffopts(ui, opts)
-
     m = cmdutil.match(repo, pats, opts)
-    if stat:
-        it = patch.diff(repo, node1, node2, match=m, opts=diffopts)
-        width = ui.interactive() and util.termwidth() or 80
-        for chunk, label in patch.diffstatui(util.iterlines(it), width=width,
-                                             git=diffopts.git):
-            ui.write(chunk, label=label)
-    else:
-        it = patch.diffui(repo, node1, node2, match=m, opts=diffopts)
-        for chunk, label in it:
-            ui.write(chunk, label=label)
+    cmdutil.diffordiffstat(ui, repo, diffopts, node1, node2, m,
+                           stat=stat)
 
 def export(ui, repo, *changesets, **opts):
     """dump the header and diffs for one or more changesets


More information about the Mercurial-devel mailing list