From ad965315875791413b8128f4d78f3e106bca7985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Nu=C3=9Fm=C3=BCller?= Date: Thu, 10 Jul 2025 08:43:10 +0200 Subject: [PATCH 1/2] Fix TypeError when using the --origin option Encode the `name` parameter to bytes (using the utf8 codec). This fixes the `TypeError` in subsequent concatenations in `get_branch`: ``` Traceback (most recent call last): # stack omitted for brevity File "C:\Dev\git-migration\fast-export\hg2git.py", line 73, in get_branch return origin_name + b'/' + name TypeError: can only concatenate str (not "bytes") to str ``` The conversion is done unconditionally since the passed parameter is currently always of type `str`. --- hg2git.py | 2 +- t/set_origin.expected | 42 ++++++++++++++++++++++++++++++ t/set_origin.t | 59 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 t/set_origin.expected create mode 100755 t/set_origin.t diff --git a/hg2git.py b/hg2git.py index 6b3fc29..0e51e64 100755 --- a/hg2git.py +++ b/hg2git.py @@ -27,7 +27,7 @@ def set_default_branch(name): def set_origin_name(name): global origin_name - origin_name = name + origin_name = name.encode('utf8') def setup_repo(url): try: diff --git a/t/set_origin.expected b/t/set_origin.expected new file mode 100644 index 0000000..7aa7370 --- /dev/null +++ b/t/set_origin.expected @@ -0,0 +1,42 @@ +blob +mark :1 +data 5 +zero + +reset refs/heads/prefix/master +commit refs/heads/prefix/master +mark :2 +author H G Wells 1679014800 +0000 +committer H G Wells 1679014800 +0000 +data 5 +zero +M 100644 :1 content + +blob +mark :3 +data 8 +branch1 + +commit refs/heads/prefix/branch1 +mark :4 +author H G Wells 1679018400 +0000 +committer H G Wells 1679018400 +0000 +data 29 +Added file in branch branch1 +from :2 +M 100644 :3 b8486c4feca589a4237a1ee428322d7109ede12e + +blob +mark :5 +data 8 +branch2 + +commit refs/heads/prefix/branch2 +mark :6 +author H G Wells 1679022000 +0000 +committer H G Wells 1679022000 +0000 +data 29 +Added file in branch branch2 +from :4 +M 100644 :5 fe786baee0d76603092c25609f2967b9c28a2cf2 + diff --git a/t/set_origin.t b/t/set_origin.t new file mode 100755 index 0000000..d92b3dc --- /dev/null +++ b/t/set_origin.t @@ -0,0 +1,59 @@ +#!/bin/bash +# +# Copyright (c) 2023 Felipe Contreras +# Copyright (c) 2025 Günther Nußmüller +# + +test_description='Set origin tests' + +. "${SHARNESS_TEST_SRCDIR-$(dirname "$0")/sharness}"/sharness.sh || exit 1 + +check() { + git -C "$1" fast-export --all > actual + test_cmp "$SHARNESS_TEST_DIRECTORY"/set_origin.expected actual +} + +git_clone() { + ( + git init -q "$2" && + cd "$2" && + git config core.ignoreCase false && + hg-fast-export.sh --repo "../$1" --origin "$3" + ) +} + +setup() { + cat > "$HOME"/.hgrc <<-EOF + [ui] + username = H G Wells + EOF +} + +make-branch() { + hg branch "$1" + FILE=$(echo "$1" | sha1sum | cut -d " " -f 1) + echo "$1" > $FILE + hg add $FILE + hg commit -d "2023-03-17 $2:00Z" -m "Added file in branch $1" +} + +setup + +test_expect_success 'basic' ' + test_when_finished "rm -rf hgrepo gitrepo" && + + ( + hg init hgrepo && + cd hgrepo && + echo zero > content && + hg add content && + hg commit -m zero -d "2023-03-17 01:00Z" && + make-branch branch1 02 && + make-branch branch2 03 + ) && + + git_clone hgrepo gitrepo prefix && + check gitrepo +' + +test_done From de5c8d9d9724e3ec48fae034f86bf33d4c9f4f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Nu=C3=9Fm=C3=BCller?= Date: Wed, 16 Jul 2025 14:44:43 +0200 Subject: [PATCH 2/2] Remove redundant type check in set_default_branch The passed `name` parameter is always of type `str` in Python 3 hence the type check is redundant and no longer needed. --- hg2git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hg2git.py b/hg2git.py index 0e51e64..73b475c 100755 --- a/hg2git.py +++ b/hg2git.py @@ -23,7 +23,7 @@ user_clean_re=re.compile(b'^["]([^"]+)["]$') def set_default_branch(name): global cfg_master - cfg_master = name.encode('utf8') if not isinstance(name, bytes) else name + cfg_master = name.encode('utf8') def set_origin_name(name): global origin_name