Patchwork [1,of,3] cat: move most of the implementation into cmdutils.cat()

login
register
mail settings
Submitter Matt Harbison
Date March 22, 2014, 5:08 p.m.
Message ID <9c5c56f97c4df13462ad.1395508096@Envy>
Download mbox | patch
Permalink /patch/4037/
State Accepted
Commit bdf5ed5246d2a1f28879ee27d833da58eb666050
Headers show

Comments

Matt Harbison - March 22, 2014, 5:08 p.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1394768718 14400
# Node ID 9c5c56f97c4df13462ad4e5684ead8b24466bcd1
# Parent  3d1d16b19e7dd5e96e242daed86512429bc1d3f6
cat: move most of the implementation into cmdutils.cat()

This will allow access to the reusable parts from subrepos, similar to add(),
forget(), etc.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1812,6 +1812,32 @@ 
     forgot.extend(forget)
     return bad, forgot
 
+def cat(ui, repo, ctx, matcher, **opts):
+    err = 1
+
+    def write(path):
+        fp = makefileobj(repo, opts.get('output'), ctx.node(), pathname=path)
+        data = ctx[path].data()
+        if opts.get('decode'):
+            data = repo.wwritedata(path, data)
+        fp.write(data)
+        fp.close()
+
+    # Automation often uses hg cat on single files, so special case it
+    # for performance to avoid the cost of parsing the manifest.
+    if len(matcher.files()) == 1 and not matcher.anypats():
+        file = matcher.files()[0]
+        mf = repo.manifest
+        mfnode = ctx._changeset[0]
+        if mf.find(mfnode, file)[0]:
+            write(file)
+            return 0
+
+    for abs in ctx.walk(matcher):
+        write(abs)
+        err = 0
+    return err
+
 def duplicatecopies(repo, rev, fromrev):
     '''reproduce copies from fromrev to rev in the dirstate'''
     for dst, src in copies.pathcopies(repo[fromrev], repo[rev]).iteritems():
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1169,32 +1169,9 @@ 
     Returns 0 on success.
     """
     ctx = scmutil.revsingle(repo, opts.get('rev'))
-    err = 1
     m = scmutil.match(ctx, (file1,) + pats, opts)
 
-    def write(path):
-        fp = cmdutil.makefileobj(repo, opts.get('output'), ctx.node(),
-                                 pathname=path)
-        data = ctx[path].data()
-        if opts.get('decode'):
-            data = repo.wwritedata(path, data)
-        fp.write(data)
-        fp.close()
-
-    # Automation often uses hg cat on single files, so special case it
-    # for performance to avoid the cost of parsing the manifest.
-    if len(m.files()) == 1 and not m.anypats():
-        file = m.files()[0]
-        mf = repo.manifest
-        mfnode = ctx._changeset[0]
-        if mf.find(mfnode, file)[0]:
-            write(file)
-            return 0
-
-    for abs in ctx.walk(m):
-        write(abs)
-        err = 0
-    return err
+    return cmdutil.cat(ui, repo, ctx, m, **opts)
 
 @command('^clone',
     [('U', 'noupdate', None,