2023-11-12 21:01:31 +03:00
|
|
|
#!/usr/bin/env python3
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2008-11-25 11:25:22 +01:00
|
|
|
# Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
|
2024-09-11 15:48:26 +02:00
|
|
|
# Copyright (c) 2025 Siemens
|
2007-03-14 10:29:24 +00:00
|
|
|
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2008-09-18 21:10:16 +02:00
|
|
|
from hg2git import setup_repo,fixup_user,get_branch,get_changeset
|
2008-09-19 18:31:53 +02:00
|
|
|
from hg2git import load_cache,save_cache,get_git_sha1,set_default_branch,set_origin_name
|
2007-03-12 07:33:40 +00:00
|
|
|
from optparse import OptionParser
|
2007-03-06 17:00:25 +00:00
|
|
|
import re
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
from binascii import hexlify
|
2018-12-05 09:23:35 -08:00
|
|
|
import pluginloader
|
2024-09-11 15:48:26 +02:00
|
|
|
from hgext.largefiles import lfutil
|
2012-09-30 07:24:16 +01:00
|
|
|
|
2007-03-09 12:07:08 +00:00
|
|
|
# silly regex to catch Signed-off-by lines in log message
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
sob_re=re.compile(b'^Signed-[Oo]ff-[Bb]y: (.+)$')
|
2007-03-08 09:37:23 +00:00
|
|
|
# insert 'checkpoint' command after this many commits or none at all if 0
|
|
|
|
|
cfg_checkpoint_count=0
|
2007-03-13 10:59:22 +00:00
|
|
|
# write some progress message every this many file contents written
|
|
|
|
|
cfg_export_boundary=1000
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2019-01-02 12:38:34 +01:00
|
|
|
subrepo_cache={}
|
|
|
|
|
submodule_mappings=None
|
|
|
|
|
|
2019-05-10 18:52:57 +02:00
|
|
|
# True if fast export should automatically try to sanitize
|
|
|
|
|
# author/branch/tag names.
|
|
|
|
|
auto_sanitize = None
|
|
|
|
|
|
2008-08-17 14:11:53 +02:00
|
|
|
def gitmode(flags):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
return b'l' in flags and b'120000' or b'x' in flags and b'100755' or b'100644'
|
2007-03-06 17:00:25 +00:00
|
|
|
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
def wr_no_nl(msg=b''):
|
|
|
|
|
assert isinstance(msg, bytes)
|
2012-03-21 23:16:11 +00:00
|
|
|
if msg:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stdout.buffer.write(msg)
|
2014-04-22 23:00:44 +02:00
|
|
|
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
def wr(msg=b''):
|
2023-03-03 11:23:22 -06:00
|
|
|
wr_no_nl(msg + b'\n')
|
2007-03-13 16:43:20 +00:00
|
|
|
#map(lambda x: sys.stderr.write('\t[%s]\n' % x),msg.split('\n'))
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2023-03-03 11:19:59 -06:00
|
|
|
def wr_data(data):
|
|
|
|
|
wr(b'data %d' % (len(data)))
|
|
|
|
|
wr(data)
|
|
|
|
|
|
2007-03-06 17:00:25 +00:00
|
|
|
def checkpoint(count):
|
|
|
|
|
count=count+1
|
2007-03-08 09:37:23 +00:00
|
|
|
if cfg_checkpoint_count>0 and count%cfg_checkpoint_count==0:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(b"Checkpoint after %d commits\n" % count)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'checkpoint')
|
2007-03-06 17:00:25 +00:00
|
|
|
wr()
|
|
|
|
|
return count
|
|
|
|
|
|
2010-09-20 10:55:24 +01:00
|
|
|
def revnum_to_revref(rev, old_marks):
|
|
|
|
|
"""Convert an hg revnum to a git-fast-import rev reference (an SHA1
|
|
|
|
|
or a mark)"""
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
return old_marks.get(rev) or b':%d' % (rev+1)
|
2007-03-07 01:52:58 +00:00
|
|
|
|
2023-03-13 21:38:26 -06:00
|
|
|
def get_filechanges(repo,revision,parents,files):
|
2007-03-07 01:52:58 +00:00
|
|
|
"""Given some repository and revision, find all changed/deleted files."""
|
2023-03-13 20:37:19 -06:00
|
|
|
if not parents:
|
|
|
|
|
# first revision: feed in full manifest
|
2023-03-14 00:47:53 -06:00
|
|
|
return files,[]
|
2023-03-13 20:37:19 -06:00
|
|
|
else:
|
2023-03-13 21:38:26 -06:00
|
|
|
# take the changes from the first parent
|
|
|
|
|
f=repo.status(parents[0],revision)
|
2023-03-14 00:47:53 -06:00
|
|
|
return f.modified+f.added,f.removed
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2007-03-09 12:07:08 +00:00
|
|
|
def get_author(logmessage,committer,authors):
|
|
|
|
|
"""As git distincts between author and committer of a patch, try to
|
|
|
|
|
extract author by detecting Signed-off-by lines.
|
|
|
|
|
|
|
|
|
|
This walks from the end of the log message towards the top skipping
|
|
|
|
|
empty lines. Upon the first non-empty line, it walks all Signed-off-by
|
|
|
|
|
lines upwards to find the first one. For that (if found), it extracts
|
|
|
|
|
authorship information the usual way (authors table, cleaning, etc.)
|
|
|
|
|
|
|
|
|
|
If no Signed-off-by line is found, this defaults to the committer.
|
|
|
|
|
|
|
|
|
|
This may sound stupid (and it somehow is), but in log messages we
|
|
|
|
|
accidentially may have lines in the middle starting with
|
|
|
|
|
"Signed-off-by: foo" and thus matching our detection regex. Prevent
|
|
|
|
|
that."""
|
|
|
|
|
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
loglines=logmessage.split(b'\n')
|
2007-03-09 12:07:08 +00:00
|
|
|
i=len(loglines)
|
|
|
|
|
# from tail walk to top skipping empty lines
|
|
|
|
|
while i>=0:
|
|
|
|
|
i-=1
|
|
|
|
|
if len(loglines[i].strip())==0: continue
|
|
|
|
|
break
|
|
|
|
|
if i>=0:
|
|
|
|
|
# walk further upwards to find first sob line, store in 'first'
|
|
|
|
|
first=None
|
|
|
|
|
while i>=0:
|
|
|
|
|
m=sob_re.match(loglines[i])
|
|
|
|
|
if m==None: break
|
|
|
|
|
first=m
|
|
|
|
|
i-=1
|
|
|
|
|
# if the last non-empty line matches our Signed-Off-by regex: extract username
|
|
|
|
|
if first!=None:
|
|
|
|
|
r=fixup_user(first.group(1),authors)
|
|
|
|
|
return r
|
|
|
|
|
return committer
|
|
|
|
|
|
2019-08-16 02:40:44 +03:00
|
|
|
def remove_gitmodules(ctx):
|
2019-08-15 03:13:52 +03:00
|
|
|
"""Removes all submodules of ctx parents"""
|
|
|
|
|
# Removing all submoduies coming from all parents is safe, as the submodules
|
|
|
|
|
# of the current commit will be re-added below. A possible optimization would
|
|
|
|
|
# be to only remove the submodules of the first parent.
|
|
|
|
|
for parent_ctx in ctx.parents():
|
|
|
|
|
for submodule in parent_ctx.substate.keys():
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'D %s' % submodule)
|
|
|
|
|
wr(b'D .gitmodules')
|
2019-08-16 02:40:44 +03:00
|
|
|
|
2019-11-22 08:52:43 -08:00
|
|
|
def refresh_git_submodule(name,subrepo_info):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'M 160000 %s %s' % (subrepo_info[1],name))
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b"Adding/updating submodule %s, revision %s\n" % (name, subrepo_info[1])
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
)
|
|
|
|
|
return b'[submodule "%s"]\n\tpath = %s\n\turl = %s\n' % (name, name, subrepo_info[0])
|
2019-11-22 08:52:43 -08:00
|
|
|
|
2019-11-22 08:48:44 -08:00
|
|
|
def refresh_hg_submodule(name,subrepo_info):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
gitRepoLocation=submodule_mappings[name] + b"/.git"
|
2019-11-22 08:48:44 -08:00
|
|
|
|
|
|
|
|
# Populate the cache to map mercurial revision to git revision
|
|
|
|
|
if not name in subrepo_cache:
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
subrepo_cache[name]=(load_cache(gitRepoLocation+b"/hg2git-mapping"),
|
|
|
|
|
load_cache(gitRepoLocation+b"/hg2git-marks",
|
2019-11-22 08:48:44 -08:00
|
|
|
lambda s: int(s)-1))
|
|
|
|
|
|
|
|
|
|
(mapping_cache,marks_cache)=subrepo_cache[name]
|
|
|
|
|
subrepo_hash=subrepo_info[1]
|
|
|
|
|
if subrepo_hash in mapping_cache:
|
|
|
|
|
revnum=mapping_cache[subrepo_hash]
|
|
|
|
|
gitSha=marks_cache[int(revnum)]
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'M 160000 %s %s' % (gitSha,name))
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b"Adding/updating submodule %s, revision %s->%s\n"
|
|
|
|
|
% (name, subrepo_hash, gitSha)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
)
|
|
|
|
|
return b'[submodule "%s"]\n\tpath = %s\n\turl = %s\n' % (name,name,
|
2019-11-22 08:48:44 -08:00
|
|
|
submodule_mappings[name])
|
|
|
|
|
else:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b"Warning: Could not find hg revision %s for %s in git %s\n"
|
|
|
|
|
% (subrepo_hash, name, gitRepoLocation,)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
)
|
|
|
|
|
return b''
|
2019-11-22 08:48:44 -08:00
|
|
|
|
2019-08-16 02:40:44 +03:00
|
|
|
def refresh_gitmodules(ctx):
|
|
|
|
|
"""Updates list of ctx submodules according to .hgsubstate file"""
|
|
|
|
|
remove_gitmodules(ctx)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
gitmodules=b""
|
2019-08-16 02:40:44 +03:00
|
|
|
# Create the .gitmodules file and all submodules
|
2019-08-15 03:05:21 +03:00
|
|
|
for name,subrepo_info in ctx.substate.items():
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if subrepo_info[2]==b'git':
|
2019-11-22 08:52:43 -08:00
|
|
|
gitmodules+=refresh_git_submodule(name,subrepo_info)
|
|
|
|
|
elif submodule_mappings and name in submodule_mappings:
|
2019-12-07 10:21:26 -08:00
|
|
|
gitmodules+=refresh_hg_submodule(name,subrepo_info)
|
2019-08-16 02:40:44 +03:00
|
|
|
|
|
|
|
|
if len(gitmodules):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'M 100644 inline .gitmodules')
|
2023-03-03 11:19:59 -06:00
|
|
|
wr_data(gitmodules)
|
2019-08-16 02:40:44 +03:00
|
|
|
|
2024-09-11 15:48:26 +02:00
|
|
|
def is_largefile(filename):
|
|
|
|
|
return filename[:6] == b'.hglf/'
|
|
|
|
|
|
|
|
|
|
def largefile_orig_name(filename):
|
|
|
|
|
return filename[6:]
|
|
|
|
|
|
|
|
|
|
def largefile_data(ctx, file, filename):
|
|
|
|
|
lf_file_ctx=ctx.filectx(file)
|
|
|
|
|
lf_hash=lf_file_ctx.data().strip(b'\n')
|
|
|
|
|
sys.stderr.write("Detected large file hash %s\n" % lf_hash.decode())
|
|
|
|
|
#should detect where the large files are located
|
|
|
|
|
file_with_data = lfutil.findfile(ctx.repo(), lf_hash)
|
|
|
|
|
if file_with_data is None:
|
|
|
|
|
# Autodownloading from the mercurial repository would be an issue as there
|
|
|
|
|
# is a good chance that we may need to input some username and password.
|
|
|
|
|
# This will surely break fast-export as there will be some unexpected
|
|
|
|
|
# output.
|
|
|
|
|
sys.stderr.write("Large file wasn't found in local cache.\n")
|
|
|
|
|
sys.stderr.write("Please clone with --all-largefiles\n")
|
|
|
|
|
sys.stderr.write("or pull all large files with 'hg lfpull --rev "
|
|
|
|
|
"\"all()\"'\n")
|
|
|
|
|
# closing in the middle of import will revert everything to the last checkpoint
|
|
|
|
|
sys.exit(3)
|
|
|
|
|
with open(os.path.normpath(file_with_data), 'rb') as file_with_data_handle:
|
|
|
|
|
return file_with_data_handle.read()
|
|
|
|
|
|
2018-12-05 09:24:56 -08:00
|
|
|
def export_file_contents(ctx,manifest,files,hgtags,encoding='',plugins={}):
|
2007-03-13 10:59:22 +00:00
|
|
|
count=0
|
|
|
|
|
max=len(files)
|
2020-01-22 21:50:37 +03:00
|
|
|
is_submodules_refreshed=False
|
2007-03-13 10:59:22 +00:00
|
|
|
for file in files:
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if not is_submodules_refreshed and (file==b'.hgsub' or file==b'.hgsubstate'):
|
2020-01-22 21:50:37 +03:00
|
|
|
is_submodules_refreshed=True
|
2019-08-16 02:40:44 +03:00
|
|
|
refresh_gitmodules(ctx)
|
2008-12-11 09:05:05 -05:00
|
|
|
# Skip .hgtags files. They only get us in trouble.
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if not hgtags and file == b".hgtags":
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(b'Skip %s\n' % file)
|
2008-12-11 09:05:05 -05:00
|
|
|
continue
|
2014-10-26 20:53:20 +03:00
|
|
|
if encoding:
|
|
|
|
|
filename=file.decode(encoding).encode('utf8')
|
|
|
|
|
else:
|
|
|
|
|
filename=file
|
2020-03-07 22:38:21 -05:00
|
|
|
if b'.git' in filename.split(b'/'): # Even on Windows, the path separator is / here.
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b'Ignoring file %s which cannot be tracked by git\n' % filename
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
)
|
2020-02-07 17:50:59 -05:00
|
|
|
continue
|
Fix UnboundLocalError with plugins and largefiles
When Plugins are used in a repository that contains largefiles,
the following exception is thrown as soon as the first largefile
is converted:
```
Traceback (most recent call last):
File "fast-export/hg-fast-export.py", line 728, in <module>
sys.exit(hg2git(options.repourl,m,options.marksfile,options.mappingfile,
File "fast-export/hg-fast-export.py", line 581, in hg2git
c=export_commit(ui,repo,rev,old_marks,max,c,authors,branchesmap,
File "fast-export/hg-fast-export.py", line 366, in export_commit
export_file_contents(ctx,man,modified,hgtags,fn_encoding,plugins)
File "fast-export/hg-fast-export.py", line 222, in export_file_contents
file_data = {'filename':filename,'file_ctx':file_ctx,'data':d}
UnboundLocalError: local variable 'file_ctx' referenced before assignment
```
This commit fixes the error by:
* initializing the file_ctx before the largefile handling takes place
* Providing a new `is_largefile` value for plugins so they can detect
if largefile handling was applied (and therefore the file_ctx
object may no longer be in sync with the git version of the file)
2025-07-25 12:28:23 +02:00
|
|
|
|
|
|
|
|
largefile = False
|
|
|
|
|
file_ctx=ctx.filectx(file)
|
2024-09-11 15:48:26 +02:00
|
|
|
if is_largefile(filename):
|
Fix UnboundLocalError with plugins and largefiles
When Plugins are used in a repository that contains largefiles,
the following exception is thrown as soon as the first largefile
is converted:
```
Traceback (most recent call last):
File "fast-export/hg-fast-export.py", line 728, in <module>
sys.exit(hg2git(options.repourl,m,options.marksfile,options.mappingfile,
File "fast-export/hg-fast-export.py", line 581, in hg2git
c=export_commit(ui,repo,rev,old_marks,max,c,authors,branchesmap,
File "fast-export/hg-fast-export.py", line 366, in export_commit
export_file_contents(ctx,man,modified,hgtags,fn_encoding,plugins)
File "fast-export/hg-fast-export.py", line 222, in export_file_contents
file_data = {'filename':filename,'file_ctx':file_ctx,'data':d}
UnboundLocalError: local variable 'file_ctx' referenced before assignment
```
This commit fixes the error by:
* initializing the file_ctx before the largefile handling takes place
* Providing a new `is_largefile` value for plugins so they can detect
if largefile handling was applied (and therefore the file_ctx
object may no longer be in sync with the git version of the file)
2025-07-25 12:28:23 +02:00
|
|
|
largefile = True
|
2024-09-11 15:48:26 +02:00
|
|
|
filename = largefile_orig_name(filename)
|
|
|
|
|
d = largefile_data(ctx, file, filename)
|
|
|
|
|
else:
|
|
|
|
|
d=file_ctx.data()
|
2018-12-05 09:23:35 -08:00
|
|
|
|
|
|
|
|
if plugins and plugins['file_data_filters']:
|
Fix UnboundLocalError with plugins and largefiles
When Plugins are used in a repository that contains largefiles,
the following exception is thrown as soon as the first largefile
is converted:
```
Traceback (most recent call last):
File "fast-export/hg-fast-export.py", line 728, in <module>
sys.exit(hg2git(options.repourl,m,options.marksfile,options.mappingfile,
File "fast-export/hg-fast-export.py", line 581, in hg2git
c=export_commit(ui,repo,rev,old_marks,max,c,authors,branchesmap,
File "fast-export/hg-fast-export.py", line 366, in export_commit
export_file_contents(ctx,man,modified,hgtags,fn_encoding,plugins)
File "fast-export/hg-fast-export.py", line 222, in export_file_contents
file_data = {'filename':filename,'file_ctx':file_ctx,'data':d}
UnboundLocalError: local variable 'file_ctx' referenced before assignment
```
This commit fixes the error by:
* initializing the file_ctx before the largefile handling takes place
* Providing a new `is_largefile` value for plugins so they can detect
if largefile handling was applied (and therefore the file_ctx
object may no longer be in sync with the git version of the file)
2025-07-25 12:28:23 +02:00
|
|
|
file_data = {'filename':filename,'file_ctx':file_ctx,'data':d, 'is_largefile':largefile}
|
2018-12-05 09:23:35 -08:00
|
|
|
for filter in plugins['file_data_filters']:
|
|
|
|
|
filter(file_data)
|
|
|
|
|
d=file_data['data']
|
|
|
|
|
filename=file_data['filename']
|
|
|
|
|
file_ctx=file_data['file_ctx']
|
|
|
|
|
|
2024-02-20 17:07:23 +01:00
|
|
|
if d is not None:
|
|
|
|
|
wr(b'M %s inline %s' % (gitmode(manifest.flags(file)),
|
|
|
|
|
strip_leading_slash(filename)))
|
|
|
|
|
wr(b'data %d' % len(d)) # had some trouble with size()
|
|
|
|
|
wr(d)
|
|
|
|
|
count+=1
|
|
|
|
|
if count%cfg_export_boundary==0:
|
|
|
|
|
sys.stderr.buffer.write(b'Exported %d/%d files\n' % (count,max))
|
2007-03-13 10:59:22 +00:00
|
|
|
if max>cfg_export_boundary:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(b'Exported %d/%d files\n' % (count,max))
|
2007-03-13 10:59:22 +00:00
|
|
|
|
2016-04-02 14:57:19 +02:00
|
|
|
def sanitize_name(name,what="branch", mapping={}):
|
2007-10-26 16:06:40 +02:00
|
|
|
"""Sanitize input roughly according to git-check-ref-format(1)"""
|
|
|
|
|
|
2016-04-02 12:28:03 +02:00
|
|
|
# NOTE: Do not update this transform to work around
|
|
|
|
|
# incompatibilities on your platform. If you change it and it starts
|
|
|
|
|
# modifying names which previously were not touched it will break
|
|
|
|
|
# preexisting setups which are doing incremental imports.
|
|
|
|
|
#
|
2018-02-01 06:56:21 +01:00
|
|
|
# Fast-export tries to not inflict arbitrary naming policy on the
|
|
|
|
|
# user, instead it aims to provide mechanisms allowing the user to
|
|
|
|
|
# apply their own policy. Therefore do not add a transform which can
|
|
|
|
|
# already be implemented with the -B and -T options to mangle branch
|
|
|
|
|
# and tag names. If you have a source repository where this is too
|
|
|
|
|
# much work to do manually, write a tool that does it for you.
|
|
|
|
|
#
|
2016-04-02 12:28:03 +02:00
|
|
|
|
2007-10-26 16:06:40 +02:00
|
|
|
def dot(name):
|
2017-05-14 14:32:59 +02:00
|
|
|
if not name: return name
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if name[0:1] == b'.': return b'_'+name[1:]
|
2007-10-26 16:06:40 +02:00
|
|
|
return name
|
|
|
|
|
|
2019-05-10 18:52:57 +02:00
|
|
|
if not auto_sanitize:
|
|
|
|
|
return mapping.get(name,name)
|
2016-04-02 14:57:19 +02:00
|
|
|
n=mapping.get(name,name)
|
2024-02-19 23:40:05 +01:00
|
|
|
p=re.compile(b'([\\[ ~^:?\\\\*]|\\.\\.)')
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
n=p.sub(b'_', n)
|
|
|
|
|
if n[-1:] in (b'/', b'.'): n=n[:-1]+b'_'
|
|
|
|
|
n=b'/'.join([dot(s) for s in n.split(b'/')])
|
|
|
|
|
p=re.compile(b'_+')
|
|
|
|
|
n=p.sub(b'_', n)
|
2007-10-26 16:06:40 +02:00
|
|
|
|
|
|
|
|
if n!=name:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b'Warning: sanitized %s [%s] to [%s]\n' % (what.encode(), name, n)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
)
|
2007-10-26 16:06:40 +02:00
|
|
|
return n
|
|
|
|
|
|
2015-08-15 19:32:59 +02:00
|
|
|
def strip_leading_slash(filename):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if filename[0:1] == b'/':
|
2015-08-15 19:32:59 +02:00
|
|
|
return filename[1:]
|
|
|
|
|
return filename
|
|
|
|
|
|
2015-08-16 17:13:04 +02:00
|
|
|
def export_commit(ui,repo,revision,old_marks,max,count,authors,
|
2018-12-05 09:24:56 -08:00
|
|
|
branchesmap,sob,brmap,hgtags,encoding='',fn_encoding='',
|
2018-12-05 09:23:35 -08:00
|
|
|
plugins={}):
|
2007-10-26 17:11:57 +02:00
|
|
|
def get_branchname(name):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if name in brmap:
|
2007-10-26 17:11:57 +02:00
|
|
|
return brmap[name]
|
2016-04-02 14:57:19 +02:00
|
|
|
n=sanitize_name(name, "branch", branchesmap)
|
2007-10-26 17:11:57 +02:00
|
|
|
brmap[name]=n
|
|
|
|
|
return n
|
|
|
|
|
|
2023-03-09 19:17:57 -06:00
|
|
|
ctx=repo[revision]
|
|
|
|
|
|
|
|
|
|
if ctx.hidden():
|
2019-10-19 10:26:15 -07:00
|
|
|
return count
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2023-03-09 19:21:04 -06:00
|
|
|
(_,user,(time,timezone),files,desc,branch,extra)=get_changeset(ui,repo,revision,authors,encoding)
|
|
|
|
|
|
2007-10-26 17:11:57 +02:00
|
|
|
branch=get_branchname(branch)
|
2007-10-26 16:06:40 +02:00
|
|
|
|
2011-12-05 23:13:57 +00:00
|
|
|
parents = [p for p in repo.changelog.parentrevs(revision) if p >= 0]
|
2018-12-05 09:23:35 -08:00
|
|
|
author = get_author(desc,user,authors)
|
2023-03-09 19:17:57 -06:00
|
|
|
hg_hash=ctx.hex()
|
2018-12-05 09:23:35 -08:00
|
|
|
|
|
|
|
|
if plugins and plugins['commit_message_filters']:
|
2020-09-30 17:42:31 +02:00
|
|
|
commit_data = {'branch': branch, 'parents': parents,
|
|
|
|
|
'author': author, 'desc': desc,
|
|
|
|
|
'revision': revision, 'hg_hash': hg_hash,
|
2021-02-19 13:23:49 -07:00
|
|
|
'committer': user, 'extra': extra}
|
2018-12-05 09:23:35 -08:00
|
|
|
for filter in plugins['commit_message_filters']:
|
|
|
|
|
filter(commit_data)
|
|
|
|
|
branch = commit_data['branch']
|
|
|
|
|
parents = commit_data['parents']
|
|
|
|
|
author = commit_data['author']
|
2020-09-30 17:42:31 +02:00
|
|
|
user = commit_data['committer']
|
2024-07-05 05:20:35 -07:00
|
|
|
desc = commit_data['desc']
|
2011-12-05 23:13:57 +00:00
|
|
|
|
|
|
|
|
if len(parents)==0 and revision != 0:
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'reset refs/heads/%s' % branch)
|
2011-12-05 23:13:57 +00:00
|
|
|
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'commit refs/heads/%s' % branch)
|
|
|
|
|
wr(b'mark :%d' % (revision+1))
|
2007-03-12 08:00:18 +00:00
|
|
|
if sob:
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'author %s %d %s' % (author,time,timezone))
|
|
|
|
|
wr(b'committer %s %d %s' % (user,time,timezone))
|
2024-07-05 05:20:35 -07:00
|
|
|
wr_data(desc + b'\n')
|
2007-03-06 17:00:25 +00:00
|
|
|
|
|
|
|
|
man=ctx.manifest()
|
|
|
|
|
|
2023-03-14 00:12:19 -06:00
|
|
|
if not parents:
|
2007-03-14 10:02:15 +00:00
|
|
|
type='full'
|
|
|
|
|
else:
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'from %s' % revnum_to_revref(parents[0], old_marks))
|
2010-09-20 10:55:24 +01:00
|
|
|
if len(parents) == 1:
|
|
|
|
|
type='simple delta'
|
|
|
|
|
else: # a merge with two parents
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'merge %s' % revnum_to_revref(parents[1], old_marks))
|
2010-09-20 10:55:24 +01:00
|
|
|
type='thorough delta'
|
2007-03-14 10:02:15 +00:00
|
|
|
|
2023-03-13 21:38:26 -06:00
|
|
|
modified,removed=get_filechanges(repo,revision,parents,files)
|
2023-03-13 20:37:19 -06:00
|
|
|
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
2023-03-09 15:32:54 -06:00
|
|
|
b'%s: Exporting %s revision %d/%d with %d/%d modified/removed files\n'
|
|
|
|
|
% (branch, type.encode(), revision + 1, max, len(modified), len(removed))
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
)
|
2007-03-14 10:02:15 +00:00
|
|
|
|
2024-02-16 17:12:49 +01:00
|
|
|
for file in removed:
|
2019-08-16 02:41:54 +03:00
|
|
|
if fn_encoding:
|
2024-02-16 17:12:49 +01:00
|
|
|
filename=file.decode(fn_encoding).encode('utf8')
|
|
|
|
|
else:
|
|
|
|
|
filename=file
|
|
|
|
|
|
|
|
|
|
if plugins and plugins['file_data_filters']:
|
|
|
|
|
file_data = {'filename':filename, 'file_ctx':None, 'data':None}
|
|
|
|
|
for filter in plugins['file_data_filters']:
|
|
|
|
|
filter(file_data)
|
|
|
|
|
filename=file_data['filename']
|
|
|
|
|
|
2019-08-16 02:41:54 +03:00
|
|
|
filename=strip_leading_slash(filename)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if filename==b'.hgsub':
|
2019-08-15 03:13:52 +03:00
|
|
|
remove_gitmodules(ctx)
|
2024-09-11 15:48:26 +02:00
|
|
|
if is_largefile(filename):
|
|
|
|
|
filename=largefile_orig_name(filename)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'D %s' % filename)
|
2014-10-26 20:53:20 +03:00
|
|
|
|
2023-03-09 15:32:54 -06:00
|
|
|
export_file_contents(ctx,man,modified,hgtags,fn_encoding,plugins)
|
2007-03-06 17:00:25 +00:00
|
|
|
wr()
|
2007-03-14 10:02:15 +00:00
|
|
|
|
2016-01-10 14:00:02 +01:00
|
|
|
return checkpoint(count)
|
|
|
|
|
|
|
|
|
|
def export_note(ui,repo,revision,count,authors,encoding,is_first):
|
2023-03-09 19:17:57 -06:00
|
|
|
ctx = repo[revision]
|
|
|
|
|
|
|
|
|
|
if ctx.hidden():
|
2019-10-19 10:26:15 -07:00
|
|
|
return count
|
2016-01-10 14:00:02 +01:00
|
|
|
|
2023-03-09 19:21:04 -06:00
|
|
|
(_,user,(time,timezone),_,_,_,_)=get_changeset(ui,repo,revision,authors,encoding)
|
|
|
|
|
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'commit refs/notes/hg')
|
|
|
|
|
wr(b'committer %s %d %s' % (user,time,timezone))
|
|
|
|
|
wr(b'data 0')
|
2016-01-10 14:00:02 +01:00
|
|
|
if is_first:
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'from refs/notes/hg^0')
|
|
|
|
|
wr(b'N inline :%d' % (revision+1))
|
2023-03-09 19:17:57 -06:00
|
|
|
hg_hash=ctx.hex()
|
2023-03-03 11:19:59 -06:00
|
|
|
wr_data(hg_hash)
|
2014-04-22 23:00:44 +02:00
|
|
|
wr()
|
|
|
|
|
return checkpoint(count)
|
2016-01-10 14:00:02 +01:00
|
|
|
|
2015-08-16 17:13:04 +02:00
|
|
|
def export_tags(ui,repo,old_marks,mapping_cache,count,authors,tagsmap):
|
2007-03-06 17:00:25 +00:00
|
|
|
l=repo.tagslist()
|
|
|
|
|
for tag,node in l:
|
2015-08-16 17:13:04 +02:00
|
|
|
# Remap the branch name
|
2016-04-02 14:57:19 +02:00
|
|
|
tag=sanitize_name(tag,"tag",tagsmap)
|
2007-03-07 11:33:03 +00:00
|
|
|
# ignore latest revision
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if tag==b'tip': continue
|
2008-12-11 09:05:05 -05:00
|
|
|
# ignore tags to nodes that are missing (ie, 'in the future')
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if hexlify(node) not in mapping_cache:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(b'Tag %s refers to unseen node %s\n' % (tag, hexlify(node)))
|
2008-12-11 09:05:05 -05:00
|
|
|
continue
|
|
|
|
|
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
rev=int(mapping_cache[hexlify(node)])
|
2007-03-07 11:33:03 +00:00
|
|
|
|
2010-09-20 10:55:24 +01:00
|
|
|
ref=revnum_to_revref(rev, old_marks)
|
2007-03-06 17:00:25 +00:00
|
|
|
if ref==None:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b'Failed to find reference for creating tag %s at r%d\n' % (tag, rev)
|
|
|
|
|
)
|
2007-03-06 17:00:25 +00:00
|
|
|
continue
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(b'Exporting tag [%s] at [hg r%d] [git %s]\n' % (tag, rev, ref))
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
wr(b'reset refs/tags/%s' % tag)
|
|
|
|
|
wr(b'from %s' % ref)
|
2007-03-06 17:00:25 +00:00
|
|
|
wr()
|
|
|
|
|
count=checkpoint(count)
|
|
|
|
|
return count
|
|
|
|
|
|
2017-09-30 14:51:24 +02:00
|
|
|
def load_mapping(name, filename, mapping_is_raw):
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
raw_regexp=re.compile(b'^([^=]+)[ ]*=[ ]*(.+)$')
|
|
|
|
|
string_regexp=b'"(((\\.)|(\\")|[^"])*)"'
|
|
|
|
|
quoted_regexp=re.compile(b'^'+string_regexp+b'[ ]*=[ ]*'+string_regexp+b'$')
|
2017-09-29 18:50:41 +02:00
|
|
|
|
|
|
|
|
def parse_raw_line(line):
|
|
|
|
|
m=raw_regexp.match(line)
|
|
|
|
|
if m==None:
|
|
|
|
|
return None
|
|
|
|
|
return (m.group(1).strip(), m.group(2).strip())
|
|
|
|
|
|
2020-03-25 12:31:16 -04:00
|
|
|
def process_unicode_escape_sequences(s):
|
|
|
|
|
# Replace unicode escape sequences in the otherwise UTF8-encoded bytestring s with
|
|
|
|
|
# the UTF8-encoded characters they represent. We need to do an additional
|
2022-10-23 11:51:33 +11:00
|
|
|
# .decode('utf8').encode('ascii', 'backslashreplace') to convert any non-ascii
|
|
|
|
|
# characters into their escape sequences so that the subsequent
|
|
|
|
|
# .decode('unicode-escape') succeeds:
|
|
|
|
|
return (
|
|
|
|
|
s.decode('utf8')
|
|
|
|
|
.encode('ascii', 'backslashreplace')
|
|
|
|
|
.decode('unicode-escape')
|
|
|
|
|
.encode('utf8')
|
|
|
|
|
)
|
2020-03-25 12:31:16 -04:00
|
|
|
|
2017-09-30 14:51:24 +02:00
|
|
|
def parse_quoted_line(line):
|
|
|
|
|
m=quoted_regexp.match(line)
|
|
|
|
|
if m==None:
|
2023-11-12 21:01:31 +03:00
|
|
|
return
|
|
|
|
|
|
2020-03-25 12:31:16 -04:00
|
|
|
return (process_unicode_escape_sequences(m.group(1)),
|
|
|
|
|
process_unicode_escape_sequences(m.group(5)))
|
2017-09-30 14:51:24 +02:00
|
|
|
|
2007-03-12 08:54:30 +00:00
|
|
|
cache={}
|
|
|
|
|
if not os.path.exists(filename):
|
2016-04-02 14:59:47 +02:00
|
|
|
sys.stderr.write('Could not open mapping file [%s]\n' % (filename))
|
2007-03-12 08:54:30 +00:00
|
|
|
return cache
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
f=open(filename,'rb')
|
2007-03-12 08:54:30 +00:00
|
|
|
l=0
|
2014-03-15 16:48:10 -07:00
|
|
|
a=0
|
2007-03-12 08:54:30 +00:00
|
|
|
for line in f.readlines():
|
|
|
|
|
l+=1
|
2014-03-15 16:48:10 -07:00
|
|
|
line=line.strip()
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
if l==1 and line[0:1]==b'#' and line==b'# quoted-escaped-strings':
|
2017-09-29 18:50:41 +02:00
|
|
|
continue
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
elif line==b'' or line[0:1]==b'#':
|
2014-03-15 16:48:10 -07:00
|
|
|
continue
|
2017-09-30 14:51:24 +02:00
|
|
|
m=parse_raw_line(line) if mapping_is_raw else parse_quoted_line(line)
|
2007-03-12 08:54:30 +00:00
|
|
|
if m==None:
|
|
|
|
|
sys.stderr.write('Invalid file format in [%s], line %d\n' % (filename,l))
|
|
|
|
|
continue
|
|
|
|
|
# put key:value in cache, key without ^:
|
2017-09-29 18:50:41 +02:00
|
|
|
cache[m[0]]=m[1]
|
2014-03-15 16:48:10 -07:00
|
|
|
a+=1
|
2007-03-12 08:54:30 +00:00
|
|
|
f.close()
|
2015-08-16 13:09:51 +02:00
|
|
|
sys.stderr.write('Loaded %d %s\n' % (a, name))
|
2007-03-12 08:54:30 +00:00
|
|
|
return cache
|
|
|
|
|
|
2014-02-10 08:32:27 -02:00
|
|
|
def branchtip(repo, heads):
|
|
|
|
|
'''return the tipmost branch head in heads'''
|
|
|
|
|
tip = heads[-1]
|
|
|
|
|
for h in reversed(heads):
|
2014-03-14 22:18:08 -07:00
|
|
|
if 'close' not in repo.changelog.read(h)[5]:
|
2014-02-10 08:32:27 -02:00
|
|
|
tip = h
|
|
|
|
|
break
|
|
|
|
|
return tip
|
|
|
|
|
|
2020-07-31 10:30:53 +02:00
|
|
|
def verify_heads(ui,repo,cache,force,ignore_unnamed_heads,branchesmap):
|
2014-03-14 21:06:53 -07:00
|
|
|
branches={}
|
2020-05-06 11:59:35 -04:00
|
|
|
for bn, heads in repo.branchmap().iteritems():
|
2014-03-14 21:06:53 -07:00
|
|
|
branches[bn] = branchtip(repo, heads)
|
2007-03-10 14:28:45 +00:00
|
|
|
l=[(-repo.changelog.rev(n), n, t) for t, n in branches.items()]
|
|
|
|
|
l.sort()
|
|
|
|
|
|
2007-03-13 16:31:57 +00:00
|
|
|
# get list of hg's branches to verify, don't take all git has
|
2007-03-10 14:28:45 +00:00
|
|
|
for _,_,b in l:
|
|
|
|
|
b=get_branch(b)
|
2016-04-15 15:46:47 +02:00
|
|
|
sanitized_name=sanitize_name(b,"branch",branchesmap)
|
|
|
|
|
sha1=get_git_sha1(sanitized_name)
|
|
|
|
|
c=cache.get(sanitized_name)
|
2021-08-27 15:47:26 +02:00
|
|
|
if not c and sha1:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
2021-08-27 15:47:26 +02:00
|
|
|
b'Error: Branch [%s] already exists and was not created by hg-fast-export, '
|
|
|
|
|
b'export would overwrite unrelated branch\n' % b)
|
|
|
|
|
if not force: return False
|
|
|
|
|
elif sha1!=c:
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
b'Error: Branch [%s] modified outside hg-fast-export:'
|
2020-03-06 11:02:38 -05:00
|
|
|
b'\n%s (repo) != %s (cache)\n' % (b, b'<None>' if sha1 is None else sha1, c)
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
)
|
2007-03-13 16:43:20 +00:00
|
|
|
if not force: return False
|
2007-03-13 16:31:57 +00:00
|
|
|
|
|
|
|
|
# verify that branch has exactly one head
|
|
|
|
|
t={}
|
2019-11-13 15:47:05 -08:00
|
|
|
unnamed_heads=False
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
for h in repo.filtered(b'visible').heads():
|
2023-03-09 18:13:04 -06:00
|
|
|
branch=get_branch(repo[h].branch())
|
2007-03-13 16:31:57 +00:00
|
|
|
if t.get(branch,False):
|
2023-11-12 21:01:31 +03:00
|
|
|
sys.stderr.buffer.write(
|
2019-11-13 15:47:05 -08:00
|
|
|
b'Error: repository has an unnamed head: hg r%d\n'
|
Fix issue #203: Resolve stderr encoding issues
In Python 3, `sys.stderr.write()` requires unicode strings, and all
output on standard streams is UTF8 encoded. Therefore in the port to
Python 3, we `.decode()`d all strings that are used in `%` formatting of
strings to be printed to stderr.
However, in Python 2, `sys.stderr` accepts either bytestrings or unicode
strings, and:
- `%s` formatting of a bytestring with a unicode string, i.e `"%s" %
u"foo"` results in a unicode string.
- Writing a unicode string to stderr/stdout uses that stream's encoding
- When the output of the process is being piped somewhere other than a
terminal (as it is when called with pipes and shell redirection from
hg-fast-export.sh), that encoding is None, which implies ASCII.
- This raises UnicodeEncodeError if the unicode strings passed to
`stderr.write()` have non-ascii characters.
We cannot fix this problem simply by encoding UTF8 again before writing
to stderr on Python 2. This is because the *decoding* of filenames with
the UTF8 codec may fail - filenames may not even be valid UTF8 desite
this being the declared filesystem encoding.
We could `fsdecode()` filenames on Python 3, which would use the
`surrogateescape` error handler, but stderr does not use this error
handler for output, meaning we would just have to encode again (with the
same error handler) anyway. And Python 2 lacks the `surrogateescape`
error handler in any case - we would need to reimplement it just to do a
round-trip decode and encode for no reason.
This commit leaves filenames and other repository data as bytestrings,
and simply writes them to `sys.stderr.buffer` on Python 3 or
`sys.stderr` on Python 2 as-is, after `%` formatting with bytestring
literals. This avoids encoding issues of filenames altogether.
Other writing to stderr that does not involve repository data has been
left with "native" strings, i.e.
`sys.stderr.write("a string literal %s" % a_command_line_arg)`. These
will still fail on Python 3 if the user passes a non-UTF filename as a
command line argument or similar. This is acceptable IMHO - although
`hg-fast-export` may encounter invalid UTF8 in mercurial repositories,
it is not too much to impose that the user name their branch mapping
files etc with valid UTF8!
2020-02-19 10:53:09 -05:00
|
|
|
% repo.changelog.rev(h)
|
|
|
|
|
)
|
2019-11-13 15:47:05 -08:00
|
|
|
unnamed_heads=True
|
2020-07-31 10:30:53 +02:00
|
|
|
if not force and not ignore_unnamed_heads: return False
|
2007-03-13 16:31:57 +00:00
|
|
|
t[branch]=True
|
2020-07-31 10:30:53 +02:00
|
|
|
if unnamed_heads and not force and not ignore_unnamed_heads: return False
|
2007-03-06 17:00:25 +00:00
|
|
|
return True
|
|
|
|
|
|
2015-08-16 17:13:04 +02:00
|
|
|
def hg2git(repourl,m,marksfile,mappingfile,headsfile,tipfile,
|
|
|
|
|
authors={},branchesmap={},tagsmap={},
|
2020-07-31 10:30:53 +02:00
|
|
|
sob=False,force=False,ignore_unnamed_heads=False,hgtags=False,notes=False,encoding='',fn_encoding='',
|
2018-12-05 09:23:35 -08:00
|
|
|
plugins={}):
|
2016-04-03 15:48:03 +02:00
|
|
|
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)
|
|
|
|
|
|
2007-03-06 17:00:25 +00:00
|
|
|
_max=int(m)
|
|
|
|
|
|
2010-09-20 10:55:24 +01:00
|
|
|
old_marks=load_cache(marksfile,lambda s: int(s)-1)
|
2008-12-11 09:05:05 -05:00
|
|
|
mapping_cache=load_cache(mappingfile)
|
2007-03-06 17:00:25 +00:00
|
|
|
heads_cache=load_cache(headsfile)
|
|
|
|
|
state_cache=load_cache(tipfile)
|
|
|
|
|
|
2016-04-03 15:48:03 +02:00
|
|
|
if len(state_cache) != 0:
|
|
|
|
|
for (name, data) in [(marksfile, old_marks),
|
|
|
|
|
(mappingfile, mapping_cache),
|
2025-06-05 16:35:08 +02:00
|
|
|
(headsfile, heads_cache)]:
|
2016-04-03 15:48:03 +02:00
|
|
|
check_cache(name, data)
|
|
|
|
|
|
2007-03-06 17:00:25 +00:00
|
|
|
ui,repo=setup_repo(repourl)
|
|
|
|
|
|
2020-07-31 10:30:53 +02:00
|
|
|
if not verify_heads(ui,repo,heads_cache,force,ignore_unnamed_heads,branchesmap):
|
2007-03-08 11:21:21 +00:00
|
|
|
return 1
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2008-09-19 08:01:53 +02:00
|
|
|
try:
|
|
|
|
|
tip=repo.changelog.count()
|
|
|
|
|
except AttributeError:
|
|
|
|
|
tip=len(repo)
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2021-02-19 16:46:37 +01:00
|
|
|
min=int(state_cache.get(b'tip',0))
|
2007-03-06 17:00:25 +00:00
|
|
|
max=_max
|
2007-10-25 15:23:17 +02:00
|
|
|
if _max<0 or max>tip:
|
2007-03-06 17:00:25 +00:00
|
|
|
max=tip
|
|
|
|
|
|
2008-12-11 09:05:05 -05:00
|
|
|
for rev in range(0,max):
|
2023-03-09 18:31:02 -06:00
|
|
|
ctx=repo[rev]
|
|
|
|
|
if ctx.hidden():
|
Support Python 3
Port hg-fast-import to Python 2/3 polyglot code.
Since mercurial accepts and returns bytestrings for all repository data,
the approach I've taken here is to use bytestrings throughout the
hg-fast-import code. All strings pertaining to repository data are
bytestrings. This means the code is using the same string datatype for
this data on Python 3 as it did (and still does) on Python 2.
Repository data coming from subprocess calls to git, or read from files,
is also left as the bytestrings either returned from
subprocess.check_output or as read from the file in 'rb' mode.
Regexes and string literals that are used with repository data have
all had a b'' prefix added.
When repository data is used in error/warning messages, it is decoded
with the UTF8 codec for printing.
With this patch, hg-fast-export.py writes binary output to
sys.stdout.buffer on Python 3 - on Python 2 this doesn't exist and it
still uses sys.stdout.
The only strings that are left as "native" strings and not coerced to
bytestrings are filepaths passed in on the command line, and dictionary
keys for internal data structures used by hg-fast-import.py, that do
not originate in repository data.
Mapping files are read in 'rb' mode, and thus bytestrings are read from
them. When an encoding is given, their contents are decoded with that
encoding, but then immediately encoded again with UTF8 and they are
returned as the resulting bytestrings
Other necessary changes were:
- indexing byestrings with a single index returns an integer on Python.
These indexing operations have been replaced with a one-element
slice: x[0] -> x[0:1] or x[-1] -> [-1:] so at to return a bytestring.
- raw_hash.encode('hex_codec') replaced with binascii.hexlify(raw_hash)
- str(integer) -> b'%d' % integer
- 'string_escape' codec replaced with 'unicode_escape' (which was
backported to python 2.7). Strings decoded with this codec were then
immediately re-encoded with UTF8.
- Calls to map() intended to execute their contents immediately were
unwrapped or converted to list comprehensions, since map() is an
iterator and does not execute until iterated over.
hg-fast-export.sh has been modified to not require Python 2. Instead, if
PYTHON has not been defined, it checks python2, python, then python3,
and uses the first one that exists and can import the mercurial module.
2020-02-10 21:39:13 -05:00
|
|
|
continue
|
2023-03-09 18:31:02 -06:00
|
|
|
mapping_cache[ctx.hex()] = b"%d" % rev
|
2008-12-11 09:05:05 -05:00
|
|
|
|
2019-01-02 12:38:34 +01:00
|
|
|
if submodule_mappings:
|
2020-04-29 15:56:36 +02:00
|
|
|
# Make sure that all mercurial submodules are registered in the submodule-mappings file
|
2019-01-02 12:38:34 +01:00
|
|
|
for rev in range(0,max):
|
2023-03-09 19:02:39 -06:00
|
|
|
ctx=repo[rev]
|
2019-10-19 10:26:15 -07:00
|
|
|
if ctx.hidden():
|
|
|
|
|
continue
|
2019-01-02 12:38:34 +01:00
|
|
|
if ctx.substate:
|
|
|
|
|
for key in ctx.substate:
|
2020-04-29 15:56:36 +02:00
|
|
|
if ctx.substate[key][2]=='hg' and key not in submodule_mappings:
|
2019-01-02 12:38:34 +01:00
|
|
|
sys.stderr.write("Error: %s not found in submodule-mappings\n" % (key))
|
|
|
|
|
return 1
|
2008-12-11 09:05:05 -05:00
|
|
|
|
2007-03-07 11:06:34 +00:00
|
|
|
c=0
|
2007-10-26 17:11:57 +02:00
|
|
|
brmap={}
|
2007-03-06 17:00:25 +00:00
|
|
|
for rev in range(min,max):
|
2015-08-16 17:13:04 +02:00
|
|
|
c=export_commit(ui,repo,rev,old_marks,max,c,authors,branchesmap,
|
2018-12-05 09:24:56 -08:00
|
|
|
sob,brmap,hgtags,encoding,fn_encoding,
|
2018-12-05 09:23:35 -08:00
|
|
|
plugins)
|
2016-01-10 14:00:02 +01:00
|
|
|
if notes:
|
|
|
|
|
for rev in range(min,max):
|
|
|
|
|
c=export_note(ui,repo,rev,c,authors, encoding, rev == min and min != 0)
|
2007-03-06 17:00:25 +00:00
|
|
|
|
2021-02-19 16:46:37 +01:00
|
|
|
state_cache[b'tip']=max
|
|
|
|
|
state_cache[b'repo']=repourl
|
2007-03-06 17:00:25 +00:00
|
|
|
save_cache(tipfile,state_cache)
|
2008-12-11 09:05:05 -05:00
|
|
|
save_cache(mappingfile,mapping_cache)
|
|
|
|
|
|
2015-08-16 17:13:04 +02:00
|
|
|
c=export_tags(ui,repo,old_marks,mapping_cache,c,authors,tagsmap)
|
2008-12-11 09:05:05 -05:00
|
|
|
|
|
|
|
|
sys.stderr.write('Issued %d commands\n' % c)
|
2007-03-08 11:21:21 +00:00
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
2007-03-12 07:33:40 +00:00
|
|
|
def bail(parser,opt):
|
|
|
|
|
sys.stderr.write('Error: No %s option given\n' % opt)
|
|
|
|
|
parser.print_help()
|
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
|
|
parser=OptionParser()
|
|
|
|
|
|
2019-05-10 18:52:57 +02:00
|
|
|
parser.add_option("-n", "--no-auto-sanitize",action="store_false",
|
|
|
|
|
dest="auto_sanitize",default=True,
|
|
|
|
|
help="Do not perform built-in (broken in many cases) sanitizing of names")
|
2007-03-12 07:33:40 +00:00
|
|
|
parser.add_option("-m","--max",type="int",dest="max",
|
|
|
|
|
help="Maximum hg revision to import")
|
2008-12-11 09:05:05 -05:00
|
|
|
parser.add_option("--mapping",dest="mappingfile",
|
|
|
|
|
help="File to read last run's hg-to-git SHA1 mapping")
|
2007-03-12 07:33:40 +00:00
|
|
|
parser.add_option("--marks",dest="marksfile",
|
|
|
|
|
help="File to read git-fast-import's marks from")
|
|
|
|
|
parser.add_option("--heads",dest="headsfile",
|
|
|
|
|
help="File to read last run's git heads from")
|
|
|
|
|
parser.add_option("--status",dest="statusfile",
|
|
|
|
|
help="File to read status from")
|
|
|
|
|
parser.add_option("-r","--repo",dest="repourl",
|
|
|
|
|
help="URL of repo to import")
|
2007-03-12 08:00:18 +00:00
|
|
|
parser.add_option("-s",action="store_true",dest="sob",
|
|
|
|
|
default=False,help="Enable parsing Signed-off-by lines")
|
2014-03-15 00:54:45 -07:00
|
|
|
parser.add_option("--hgtags",action="store_true",dest="hgtags",
|
|
|
|
|
default=False,help="Enable exporting .hgtags files")
|
2007-03-12 08:54:30 +00:00
|
|
|
parser.add_option("-A","--authors",dest="authorfile",
|
|
|
|
|
help="Read authormap from AUTHORFILE")
|
2015-08-16 17:13:04 +02:00
|
|
|
parser.add_option("-B","--branches",dest="branchesfile",
|
|
|
|
|
help="Read branch map from BRANCHESFILE")
|
|
|
|
|
parser.add_option("-T","--tags",dest="tagsfile",
|
|
|
|
|
help="Read tags map from TAGSFILE")
|
2007-03-13 16:43:20 +00:00
|
|
|
parser.add_option("-f","--force",action="store_true",dest="force",
|
2020-07-31 10:30:53 +02:00
|
|
|
default=False,help="Ignore validation errors by force, implies --ignore-unnamed-heads")
|
|
|
|
|
parser.add_option("--ignore-unnamed-heads",action="store_true",dest="ignore_unnamed_heads",
|
|
|
|
|
default=False,help="Ignore unnamed head errors")
|
2008-09-18 21:10:16 +02:00
|
|
|
parser.add_option("-M","--default-branch",dest="default_branch",
|
|
|
|
|
help="Set the default branch")
|
2008-09-19 18:31:53 +02:00
|
|
|
parser.add_option("-o","--origin",dest="origin_name",
|
|
|
|
|
help="use <name> as namespace to track upstream")
|
2014-04-22 23:00:44 +02:00
|
|
|
parser.add_option("--hg-hash",action="store_true",dest="notes",
|
|
|
|
|
default=False,help="Annotate commits with the hg hash as git notes in the hg namespace")
|
2014-10-25 13:18:41 +03:00
|
|
|
parser.add_option("-e",dest="encoding",
|
|
|
|
|
help="Assume commit and author strings retrieved from Mercurial are encoded in <encoding>")
|
2015-11-03 16:12:46 +09:00
|
|
|
parser.add_option("--fe",dest="fn_encoding",
|
|
|
|
|
help="Assume file names from Mercurial are encoded in <filename_encoding>")
|
2017-09-30 14:51:24 +02:00
|
|
|
parser.add_option("--mappings-are-raw",dest="raw_mappings", default=False,
|
|
|
|
|
help="Assume mappings are raw <key>=<value> lines")
|
2018-06-17 21:09:59 +03:00
|
|
|
parser.add_option("--filter-contents",dest="filter_contents",
|
|
|
|
|
help="Pipe contents of each exported file through FILTER_CONTENTS <file-path> <hg-hash> <is-binary>")
|
2018-12-05 09:23:35 -08:00
|
|
|
parser.add_option("--plugin-path", type="string", dest="pluginpath",
|
|
|
|
|
help="Additional search path for plugins ")
|
|
|
|
|
parser.add_option("--plugin", action="append", type="string", dest="plugins",
|
|
|
|
|
help="Add a plugin with the given init string <name=init>")
|
2019-01-02 12:38:34 +01:00
|
|
|
parser.add_option("--subrepo-map", type="string", dest="subrepo_map",
|
|
|
|
|
help="Provide a mapping file between the subrepository name and the submodule name")
|
2007-03-12 07:33:40 +00:00
|
|
|
|
|
|
|
|
(options,args)=parser.parse_args()
|
|
|
|
|
|
2007-03-12 08:00:18 +00:00
|
|
|
m=-1
|
2019-05-10 18:52:57 +02:00
|
|
|
auto_sanitize = options.auto_sanitize
|
2007-03-12 07:33:40 +00:00
|
|
|
if options.max!=None: m=options.max
|
|
|
|
|
|
|
|
|
|
if options.marksfile==None: bail(parser,'--marks')
|
2008-12-11 09:05:05 -05:00
|
|
|
if options.mappingfile==None: bail(parser,'--mapping')
|
2007-03-19 09:05:51 +00:00
|
|
|
if options.headsfile==None: bail(parser,'--heads')
|
|
|
|
|
if options.statusfile==None: bail(parser,'--status')
|
|
|
|
|
if options.repourl==None: bail(parser,'--repo')
|
2007-03-12 07:33:40 +00:00
|
|
|
|
2019-01-02 12:38:34 +01:00
|
|
|
if options.subrepo_map:
|
|
|
|
|
if not os.path.exists(options.subrepo_map):
|
|
|
|
|
sys.stderr.write('Subrepo mapping file not found %s\n'
|
|
|
|
|
% options.subrepo_map)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
submodule_mappings=load_mapping('subrepo mappings',
|
|
|
|
|
options.subrepo_map,False)
|
|
|
|
|
|
2007-03-12 08:54:30 +00:00
|
|
|
a={}
|
|
|
|
|
if options.authorfile!=None:
|
2017-09-30 14:51:24 +02:00
|
|
|
a=load_mapping('authors', options.authorfile, options.raw_mappings)
|
2007-03-12 08:54:30 +00:00
|
|
|
|
2015-08-16 17:13:04 +02:00
|
|
|
b={}
|
|
|
|
|
if options.branchesfile!=None:
|
2017-09-30 14:51:24 +02:00
|
|
|
b=load_mapping('branches', options.branchesfile, options.raw_mappings)
|
2015-08-16 17:13:04 +02:00
|
|
|
|
|
|
|
|
t={}
|
|
|
|
|
if options.tagsfile!=None:
|
2019-10-12 21:09:14 +02:00
|
|
|
t=load_mapping('tags', options.tagsfile, options.raw_mappings)
|
2015-08-16 17:13:04 +02:00
|
|
|
|
2008-09-18 21:10:16 +02:00
|
|
|
if options.default_branch!=None:
|
|
|
|
|
set_default_branch(options.default_branch)
|
|
|
|
|
|
2008-09-19 18:31:53 +02:00
|
|
|
if options.origin_name!=None:
|
|
|
|
|
set_origin_name(options.origin_name)
|
|
|
|
|
|
2014-10-25 13:18:41 +03:00
|
|
|
encoding=''
|
|
|
|
|
if options.encoding!=None:
|
|
|
|
|
encoding=options.encoding
|
|
|
|
|
|
2015-11-03 16:12:46 +09:00
|
|
|
fn_encoding=encoding
|
|
|
|
|
if options.fn_encoding!=None:
|
|
|
|
|
fn_encoding=options.fn_encoding
|
|
|
|
|
|
2018-12-05 09:23:35 -08:00
|
|
|
plugins=[]
|
|
|
|
|
if options.plugins!=None:
|
|
|
|
|
plugins+=options.plugins
|
|
|
|
|
|
2018-06-17 21:09:59 +03:00
|
|
|
if options.filter_contents!=None:
|
2018-12-05 09:24:56 -08:00
|
|
|
plugins+=['shell_filter_file_contents='+options.filter_contents]
|
2018-06-17 21:09:59 +03:00
|
|
|
|
2018-12-05 09:23:35 -08:00
|
|
|
plugins_dict={}
|
|
|
|
|
plugins_dict['commit_message_filters']=[]
|
|
|
|
|
plugins_dict['file_data_filters']=[]
|
|
|
|
|
|
|
|
|
|
if plugins and options.pluginpath:
|
|
|
|
|
sys.stderr.write('Using additional plugin path: ' + options.pluginpath + '\n')
|
|
|
|
|
|
|
|
|
|
for plugin in plugins:
|
|
|
|
|
split = plugin.split('=')
|
|
|
|
|
name, opts = split[0], '='.join(split[1:])
|
|
|
|
|
i = pluginloader.get_plugin(name,options.pluginpath)
|
|
|
|
|
sys.stderr.write('Loaded plugin ' + i['name'] + ' from path: ' + i['path'] +' with opts: ' + opts + '\n')
|
|
|
|
|
plugin = pluginloader.load_plugin(i).build_filter(opts)
|
|
|
|
|
if hasattr(plugin,'file_data_filter') and callable(plugin.file_data_filter):
|
|
|
|
|
plugins_dict['file_data_filters'].append(plugin.file_data_filter)
|
|
|
|
|
if hasattr(plugin, 'commit_message_filter') and callable(plugin.commit_message_filter):
|
|
|
|
|
plugins_dict['commit_message_filters'].append(plugin.commit_message_filter)
|
|
|
|
|
|
2014-10-25 13:18:41 +03:00
|
|
|
sys.exit(hg2git(options.repourl,m,options.marksfile,options.mappingfile,
|
2015-08-16 17:13:04 +02:00
|
|
|
options.headsfile, options.statusfile,
|
|
|
|
|
authors=a,branchesmap=b,tagsmap=t,
|
2020-07-31 10:30:53 +02:00
|
|
|
sob=options.sob,force=options.force,
|
|
|
|
|
ignore_unnamed_heads=options.ignore_unnamed_heads,
|
|
|
|
|
hgtags=options.hgtags,
|
2018-12-05 09:24:56 -08:00
|
|
|
notes=options.notes,encoding=encoding,fn_encoding=fn_encoding,
|
2018-12-05 09:23:35 -08:00
|
|
|
plugins=plugins_dict))
|