[PATCH 2 of 5] convert: support "preprocess" in filemap
Yury Sulsky
yury.sulsky at gmail.com
Sat Nov 26 21:26:47 UTC 2011
# HG changeset patch
# User Yury Sulsky <yury.sulsky at gmail.com>
# Date 1322288941 18000
# Branch stable
# Node ID 661cf5c054904bcd42179161b1bbf68cbe9df1d7
# Parent 84c30a6c1760a6302f99daf45a992bf59be1a29a
convert: support "preprocess" in filemap
diff -r 84c30a6c1760 -r 661cf5c05490 hgext/convert/__init__.py
--- a/hgext/convert/__init__.py Fri Nov 25 23:42:14 2011 -0500
+++ b/hgext/convert/__init__.py Sat Nov 26 01:29:01 2011 -0500
@@ -89,6 +89,8 @@
rename path/to/source path/to/destination
+ preprocess path/to/file-or-dir "preprocess-command"
+
Comment lines start with ``#``. A specified path matches if it
equals the full relative name of a file or one of its parent
directories. The ``include`` or ``exclude`` directive with the
@@ -102,6 +104,14 @@
it is converted. To rename from a subdirectory into the root of
the repository, use ``.`` as the path to rename to.
+ The ``preprocess`` directive replaces the contents of specified
+ files with the output from an external command. This allows you to
+ e.g. insert a copyright statement at the beginning of every
+ document or to strip out sensitive data from a repository. The
+ command is invoked with the (possibly renamed) file name as a
+ command-line argument and the contents of the file piped into its
+ standard input.
+
The splicemap is a file that allows insertion of synthetic
history, letting you specify the parents of a revision. This is
useful if you want to e.g. give a Subversion merge two parents, or
@@ -135,6 +145,13 @@
branch names. This can be used to (for instance) move code in one
repository from "default" to a named branch.
+ The includerevs and excluderevs options allow you to choose which
+ revisions you would like to appear in the converted repository.
+ The arguments to these commands are files with a revision from the
+ source repository on each line. Note that these directives are
+ only advisory, and in certain situations (i.e. merge nodes and
+ branch endings) the revisions will be converted regardless.
+
Mercurial Source
''''''''''''''''
diff -r 84c30a6c1760 -r 661cf5c05490 hgext/convert/filemap.py
--- a/hgext/convert/filemap.py Fri Nov 25 23:42:14 2011 -0500
+++ b/hgext/convert/filemap.py Sat Nov 26 01:29:01 2011 -0500
@@ -16,7 +16,7 @@
yield '.', name
class filemapper(object):
- '''Map and filter filenames when importing.
+ '''Map and filter file names and contents when importing.
A name can be mapped to itself, a new name, or None (omit from new
repository).'''
@@ -25,6 +25,7 @@
self.include = {}
self.exclude = {}
self.rename = {}
+ self.preprocess = {}
if path:
if self.parse(path):
raise util.Abort(_('errors in filemap'))
@@ -67,6 +68,11 @@
self.rename[src] = dest
elif cmd == 'source':
errs += self.parse(lex.get_token())
+ elif cmd == 'preprocess':
+ name = lex.get_token()
+ prog = lex.get_token()
+ errs += check(name, self.exclude, 'exclude')
+ self.preprocess[name] = prog
else:
self.ui.warn(_('%s:%d: unknown directive %r\n') %
(lex.infile, lex.lineno, cmd))
@@ -104,3 +110,6 @@
def active(self):
return bool(self.include or self.exclude or self.rename)
+
+ def preprocessor(self, name):
+ return self.lookup(name, self.preprocess)[0] or None
diff -r 84c30a6c1760 -r 661cf5c05490 hgext/convert/filtermap.py
--- a/hgext/convert/filtermap.py Fri Nov 25 23:42:14 2011 -0500
+++ b/hgext/convert/filtermap.py Sat Nov 26 01:29:01 2011 -0500
@@ -255,16 +255,11 @@
# or more interesting revisions.
class filtermap_source(filtermap_base):
- def __init__(self, ui, base,
- inrevs = None,
- outrevs = None,
- filemap = None,
- modify_file = None):
+ def __init__(self, ui, base, inrevs = None, outrevs = None, filemap = None):
super(filtermap_source, self).__init__(ui, base)
self.inrevs = self.read_revset(inrevs) if inrevs else None
self.outrevs = self.read_revset(outrevs) if outrevs else None
- self.filemapper = filemapper(ui, filemap) if filemap else None
- self.modify_file = modify_file
+ self.filemapper = filemapper(ui, filemap) if filemap else None
def read_revset(self, fname):
def parse(line):
@@ -331,10 +326,15 @@
return self.filemap_changes(changes)
def getfile(self, name, rev):
+ source_name = name
if self.filemapper is not None:
- name, rev = rev
- file = super(filtermap_source, self).getfile(name, rev)
- if self.modify_file is not None:
- return self.modify_file(name, file)
+ source_name, rev = rev
+ file = super(filtermap_source, self).getfile(source_name, rev)
+ preprocess = self.filemapper.preprocessor(source_name)
+ if preprocess is not None:
+ cmd = preprocess + ' ' + util.shellquote(name)
+ data, mode = file
+ data = util.pipefilter(data, cmd)
+ file = data, mode
return file
More information about the Mercurial-devel
mailing list