From 4c10270302979f76d3bf143a2c3b3374c1b36e2c Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Thu, 2 Mar 2023 14:26:24 -0600 Subject: [PATCH 1/3] Fix data handling The length should be exactly the same as the data, for example if the data is "hello" only 5 characters should be written on the stream. Thus it should always be `len(data)`, not `len(data)+1` as it currently is in some places. Since the first commit of hg2git.py there was a wtf comment, presumably Rocco was confused about this common discrepancy. We can shuffle the logic around by adding '\n' to the data, and removing +1 to the length. Also, the data should be written without a newline (wr_no_nl). Signed-off-by: Felipe Contreras --- hg-fast-export.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hg-fast-export.py b/hg-fast-export.py index 406f952..205e3ec 100755 --- a/hg-fast-export.py +++ b/hg-fast-export.py @@ -197,7 +197,7 @@ def refresh_gitmodules(ctx): if len(gitmodules): wr(b'M 100644 inline .gitmodules') - wr(b'data %d' % (len(gitmodules)+1)) + wr(b'data %d' % (len(gitmodules))) wr(gitmodules) def export_file_contents(ctx,manifest,files,hgtags,encoding='',plugins={}): @@ -315,7 +315,7 @@ def export_commit(ui,repo,revision,old_marks,max,count,authors, parents = commit_data['parents'] author = commit_data['author'] user = commit_data['committer'] - desc = commit_data['desc'] + desc = commit_data['desc'] + b'\n' if len(parents)==0 and revision != 0: wr(b'reset refs/heads/%s' % branch) @@ -325,9 +325,8 @@ def export_commit(ui,repo,revision,old_marks,max,count,authors, if sob: wr(b'author %s %d %s' % (author,time,timezone)) wr(b'committer %s %d %s' % (user,time,timezone)) - wr(b'data %d' % (len(desc)+1)) # wtf? + wr(b'data %d' % (len(desc))) wr(desc) - wr() ctx=revsymbol(repo, b"%d" % revision) man=ctx.manifest() @@ -389,7 +388,7 @@ def export_note(ui,repo,revision,count,authors,encoding,is_first): wr(b'N inline :%d' % (revision+1)) hg_hash=revsymbol(repo,b"%d" % revision).hex() wr(b'data %d' % (len(hg_hash))) - wr_no_nl(hg_hash) + wr(hg_hash) wr() return checkpoint(count) From c3cbf1e04d3a01a9a66e015d192377bb0a000873 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Fri, 3 Mar 2023 11:19:59 -0600 Subject: [PATCH 2/3] Add wr_data helper No functional changes. Signed-off-by: Felipe Contreras --- hg-fast-export.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hg-fast-export.py b/hg-fast-export.py index 205e3ec..1c07ae4 100755 --- a/hg-fast-export.py +++ b/hg-fast-export.py @@ -55,6 +55,10 @@ def wr(msg=b''): stdout_buffer.write(b'\n') #map(lambda x: sys.stderr.write('\t[%s]\n' % x),msg.split('\n')) +def wr_data(data): + wr(b'data %d' % (len(data))) + wr(data) + def checkpoint(count): count=count+1 if cfg_checkpoint_count>0 and count%cfg_checkpoint_count==0: @@ -197,8 +201,7 @@ def refresh_gitmodules(ctx): if len(gitmodules): wr(b'M 100644 inline .gitmodules') - wr(b'data %d' % (len(gitmodules))) - wr(gitmodules) + wr_data(gitmodules) def export_file_contents(ctx,manifest,files,hgtags,encoding='',plugins={}): count=0 @@ -325,8 +328,7 @@ def export_commit(ui,repo,revision,old_marks,max,count,authors, if sob: wr(b'author %s %d %s' % (author,time,timezone)) wr(b'committer %s %d %s' % (user,time,timezone)) - wr(b'data %d' % (len(desc))) - wr(desc) + wr_data(desc) ctx=revsymbol(repo, b"%d" % revision) man=ctx.manifest() @@ -387,8 +389,7 @@ def export_note(ui,repo,revision,count,authors,encoding,is_first): wr(b'from refs/notes/hg^0') wr(b'N inline :%d' % (revision+1)) hg_hash=revsymbol(repo,b"%d" % revision).hex() - wr(b'data %d' % (len(hg_hash))) - wr(hg_hash) + wr_data(hg_hash) wr() return checkpoint(count) From bbab981130fa0d7d265805f991c623728049c579 Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Fri, 3 Mar 2023 11:23:22 -0600 Subject: [PATCH 3/3] Trivial simplification of wr No need to issue two write commands. Signed-off-by: Felipe Contreras --- hg-fast-export.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hg-fast-export.py b/hg-fast-export.py index 1c07ae4..bec82ec 100755 --- a/hg-fast-export.py +++ b/hg-fast-export.py @@ -51,8 +51,7 @@ def wr_no_nl(msg=b''): stdout_buffer.write(msg) def wr(msg=b''): - wr_no_nl(msg) - stdout_buffer.write(b'\n') + wr_no_nl(msg + b'\n') #map(lambda x: sys.stderr.write('\t[%s]\n' % x),msg.split('\n')) def wr_data(data):