f74da28d8726 — Steve Fink 4 years ago
cmdconvert extension for adding --command to filter files during hg convert
2 files changed, 96 insertions(+), 0 deletions(-)

A => __init__.py
A => test.sh
A => __init__.py +69 -0
@@ 0,0 1,69 @@ 
+# cmdconvert - Convert a repo by running a command on every revision.
+#
+# Copyright 2019 Steve Fink <sphink@gmail.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+import hgext.convert.convcmd
+import os
+import pipes
+import tempfile
+
+from hgext.convert.hg import mercurial_source
+from mercurial import extensions, registrar
+
+testedwith = '4.7 4.7.1 4.9 5.1'
+
+def cmdconvert(orig, ui, *args, **kwargs):
+    filter_command = kwargs['command']
+    del kwargs['command']
+
+    file_extensions = []
+    for e in kwargs['ext']:
+        file_extensions.append(e if e.startswith('.') else '.' + e)
+    if not file_extensions:
+        file_extensions = ['']  # Match everything
+    del kwargs['ext']
+
+    pipe = pipes.Template()
+    pipe.append(filter_command, '--')
+
+    class customsource(mercurial_source):
+        def getfile(self, name, rev):
+            data, flags = super(customsource, self).getfile(name, rev)
+
+            if any(name.endswith(ext) for ext in file_extensions):
+                ui.note("Filtering {} through {}\n".format(name, filter_command))
+                tmp = tempfile.NamedTemporaryFile()
+                os.environ['CONVERT_FILENAME'] = name
+                f = pipe.open(tmp.name, 'w')
+                f.write(data)
+                f.close()
+                tmp.seek(0)
+                data = tmp.read()
+
+            return data, flags
+
+    hgext.convert.convcmd.source_converters.insert(0, ('hg', customsource, 'sourcesort'))
+
+    return orig(ui, *args, **kwargs)
+
+def uisetup(ui):
+    ext = extensions.find('convert')
+    if not ext:
+        return
+    try:
+        entry = extensions.wrapcommand(ext.cmdtable, 'convert', cmdconvert, ' [--command CMD] [--ext EXT]', '''
+The ``cmdconvert`` extension adds the ``--command`` flag to the convert
+command. The given command will be used as a shell command to pipe the data
+from every file through, or only files with the extension EXT if the --ext EXT
+option is given (may be given multiple times). While the command is running,
+the enviroment variable CONVERT_FILENAME will be set to the filename that is
+being processed.
+        ''')
+    except TypeError:
+        entry = extensions.wrapcommand(ext.cmdtable, 'convert', cmdconvert)
+
+    entry[1].extend([('', 'command', '', 'shell command to filter files through'),
+                     ('', 'ext', [], 'extensions to filter through commad')])

          
A => test.sh +27 -0
@@ 0,0 1,27 @@ 
+#!/bin/bash
+
+rm -rf /tmp/repo
+rm -rf /tmp/repo2
+set -e
+mkdir /tmp/repo
+cd /tmp/repo
+hg init
+echo -n smurf > file.dat
+hg add file.dat
+hg commit -m 'initial'
+echo "s" >> file.dat
+echo "must" >> file.dat
+echo "die" >> file.dat
+echo "oingo" > message.txt
+hg add message.txt
+hg commit -m 'fixed'
+echo "!" >> file.dat
+echo "....." >> message.txt
+hg commit -m 'whee'
+hg-4.7.1 convert --command 'tac' --ext .dat /tmp/repo /tmp/repo2
+cd /tmp/repo2
+hg up -r tip
+echo "-- file.dat --"
+cat file.dat
+echo "-- message.txt --"
+cat message.txt