mirror of
https://github.com/frej/fast-export.git
synced 2026-07-01 00:20:14 +02:00
Move filter_contents to plugin system
This commit is contained in:
30
plugins/shell_filter_file_contents/README.md
Normal file
30
plugins/shell_filter_file_contents/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
## Shell Script File Filter
|
||||
|
||||
This plugin uses shell scripts in order to perform filtering of files.
|
||||
If your preferred scripting is done via shell, this tool is for you.
|
||||
Be noted, though, that this method can cause an order of magnitude slow
|
||||
down. For small repositories, this wont be an issue.
|
||||
|
||||
To use the plugin, add
|
||||
`--plugin shell_filter_file_contents=path/to/shell/script.sh`.
|
||||
The filter script is supplied to the plugin option after the plugin name,
|
||||
which is in turned passed to the plugin initialization. hg-fast-export
|
||||
runs the filter for each exported file, pipes its content to the filter's
|
||||
standard input, and uses the filter's standard output in place
|
||||
of the file's original content. An example use of this feature
|
||||
is to convert line endings in text files from CRLF to git's preferred LF,
|
||||
although this task is faster performed using the native plugin.
|
||||
|
||||
The script is called with the following syntax:
|
||||
`FILTER_CONTENTS <file-path> <hg-hash> <is-binary>`
|
||||
|
||||
```
|
||||
-- Start of crlf-filter.sh --
|
||||
#!/bin/sh
|
||||
# $1 = pathname of exported file relative to the root of the repo
|
||||
# $2 = Mercurial's hash of the file
|
||||
# $3 = "1" if Mercurial reports the file as binary, otherwise "0"
|
||||
|
||||
if [ "$3" == "1" ]; then cat; else dos2unix; fi
|
||||
-- End of crlf-filter.sh --
|
||||
```
|
||||
28
plugins/shell_filter_file_contents/__init__.py
Normal file
28
plugins/shell_filter_file_contents/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#Pipe contents of each exported file through FILTER_CONTENTS <file-path> <hg-hash> <is-binary>"
|
||||
import subprocess
|
||||
import shlex
|
||||
import sys
|
||||
from mercurial import node
|
||||
|
||||
def build_filter(args):
|
||||
return Filter(args)
|
||||
|
||||
class Filter:
|
||||
def __init__(self, args):
|
||||
self.filter_contents = shlex.split(args)
|
||||
|
||||
def file_data_filter(self,file_data):
|
||||
d = file_data['data']
|
||||
file_ctx = file_data['file_ctx']
|
||||
filename = file_data['filename']
|
||||
filter_cmd = self.filter_contents + [filename, node.hex(file_ctx.filenode()), '1' if file_ctx.isbinary() else '0']
|
||||
try:
|
||||
filter_proc = subprocess.Popen(filter_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
d, _ = filter_proc.communicate(d)
|
||||
except:
|
||||
sys.stderr.write('Running filter-contents %s:\n' % filter_cmd)
|
||||
raise
|
||||
filter_ret = filter_proc.poll()
|
||||
if filter_ret:
|
||||
raise subprocess.CalledProcessError(filter_ret, filter_cmd)
|
||||
file_data['data'] = d
|
||||
Reference in New Issue
Block a user