Skip to content

Commit

Permalink
Add --filter-contents
Browse files Browse the repository at this point in the history
  • Loading branch information
atykhyy committed Jun 11, 2018
1 parent e200cec commit d94b0a7
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions hg-fast-export.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import re
import sys
import os
import shlex
import subprocess
import binascii

if sys.platform == "win32":
# On Windows, sys.stdout is initially opened in text mode, which means that
Expand Down Expand Up @@ -123,19 +126,31 @@ def get_author(logmessage,committer,authors):
return r
return committer

def export_file_contents(ctx,manifest,files,hgtags,encoding=''):
def export_file_contents(ctx,manifest,files,hgtags,encoding='',filter_contents=None):
count=0
max=len(files)
for file in files:
# Skip .hgtags files. They only get us in trouble.
if not hgtags and file == ".hgtags":
sys.stderr.write('Skip %s\n' % (file))
continue
d=ctx.filectx(file).data()
if encoding:
filename=file.decode(encoding).encode('utf8')
else:
filename=file
f=ctx.filectx(file)
d=f.data()
if filter_contents:
a=filter_contents + [filename,binascii.hexlify(f.filenode()),'1' if f.isbinary() else '0']
try:
p=subprocess.Popen(a,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
d,_=p.communicate(d)
except:
sys.stderr.write('Running filter-contents %s:\n' % a)
raise
r=p.poll()
if r:
raise subprocess.CalledProcessError(r,a)
wr('M %s inline %s' % (gitmode(manifest.flags(file)),
strip_leading_slash(filename)))
wr('data %d' % len(d)) # had some trouble with size()
Expand Down Expand Up @@ -185,7 +200,7 @@ def strip_leading_slash(filename):
return filename

def export_commit(ui,repo,revision,old_marks,max,count,authors,
branchesmap,sob,brmap,hgtags,encoding='',fn_encoding=''):
branchesmap,sob,brmap,hgtags,encoding='',fn_encoding='',filter_contents=None):
def get_branchname(name):
if brmap.has_key(name):
return brmap[name]
Expand Down Expand Up @@ -246,8 +261,8 @@ def get_branchname(name):
removed=[strip_leading_slash(x) for x in removed]

map(lambda r: wr('D %s' % r),removed)
export_file_contents(ctx,man,added,hgtags,fn_encoding)
export_file_contents(ctx,man,changed,hgtags,fn_encoding)
export_file_contents(ctx,man,added,hgtags,fn_encoding,filter_contents)
export_file_contents(ctx,man,changed,hgtags,fn_encoding,filter_contents)
wr()

return checkpoint(count)
Expand Down Expand Up @@ -383,7 +398,7 @@ def verify_heads(ui,repo,cache,force,branchesmap):

def hg2git(repourl,m,marksfile,mappingfile,headsfile,tipfile,
authors={},branchesmap={},tagsmap={},
sob=False,force=False,hgtags=False,notes=False,encoding='',fn_encoding=''):
sob=False,force=False,hgtags=False,notes=False,encoding='',fn_encoding='',filter_contents=None):
def check_cache(filename, contents):
if len(contents) == 0:
sys.stderr.write('Warning: %s does not contain any data, this will probably make an incremental import fail\n' % filename)
Expand Down Expand Up @@ -425,7 +440,7 @@ def check_cache(filename, contents):
brmap={}
for rev in range(min,max):
c=export_commit(ui,repo,rev,old_marks,max,c,authors,branchesmap,
sob,brmap,hgtags,encoding,fn_encoding)
sob,brmap,hgtags,encoding,fn_encoding,filter_contents)
if notes:
for rev in range(min,max):
c=export_note(ui,repo,rev,c,authors, encoding, rev == min and min != 0)
Expand Down Expand Up @@ -485,6 +500,8 @@ def bail(parser,opt):
help="Assume file names from Mercurial are encoded in <filename_encoding>")
parser.add_option("--mappings-are-raw",dest="raw_mappings", default=False,
help="Assume mappings are raw <key>=<value> lines")
parser.add_option("--filter-contents",dest="filter_contents",
help="Pipe file contents through FILTER_CONTENTS <file-path> <hg-hash> <is-binary>")

(options,args)=parser.parse_args()

Expand Down Expand Up @@ -523,8 +540,12 @@ def bail(parser,opt):
if options.fn_encoding!=None:
fn_encoding=options.fn_encoding

filter_contents=None
if options.filter_contents!=None:
filter_contents=shlex.split(options.filter_contents)

sys.exit(hg2git(options.repourl,m,options.marksfile,options.mappingfile,
options.headsfile, options.statusfile,
authors=a,branchesmap=b,tagsmap=t,
sob=options.sob,force=options.force,hgtags=options.hgtags,
notes=options.notes,encoding=encoding,fn_encoding=fn_encoding))
notes=options.notes,encoding=encoding,fn_encoding=fn_encoding,filter_contents=filter_contents))

0 comments on commit d94b0a7

Please sign in to comment.