Mercurial から Git へ引越し。

母艦(という言い方もあまりしなくなってきたみたいですが)を切り替えるのにあわせて、これまでのメイン PC でやってきた各種設定や Python スクリプトをコピーして使おうとしたのですが、環境が違うと手直しも必要になってきます。

これまではバージョン管理はメイン PC の、たとえば c:\Apps\bin にリポジトリを作って、そこにコミットしたりしてたのですが、新規の PC で手直ししたりするとその変更をメイン側に反映するのも結構手間です。さらに、その結果としてメイン側でちょこっと変えて、それを新規側に戻して、とかになってくると、やっぱりリポジトリはサーバにおいたほうがいいかな、と。ちょー今更ですが。

実はかなり前になりますが Bitbucket にアカウントを作って、そちらをリポジトリにしようかなとしていたことがあります。なんだかんだでちょっとお試しでいじって放置してました。ところが Bitbucket は2010年に Atlassian に買収され、2020年6月で Mercurial のすべてのサポートを終了し、Mercurial リポジトリを削除し、Git のみにするということになっていたようです。そしてアカウントもいつのまにか Atlassian に移行していました。

一方、実は GitHub にもアカウントを作っていまして、こちらのほうはちょっとだけ使ったのですがやっぱり放置していました。Mercurial のローカルリポジトリで当面の用は足りていたのと、Python をメインで使うんだから VCS も Python 系の Mercurial がいいし、むしろ Git のみのために Perl をインストールするのがなんとなく嫌だったりしたもので。

といいつつ、やっぱり Visual Studio は Git を組み込んでそのまま VCS が使えたり、VSCode も Git リポジトリを見に行ったり、兎にも角にも Git がないと始まらない世の中になってきていたりするので、そろそろ Mercurial じゃなきゃ嫌だ!みたいなことやってると面倒だったりするんだろうなということで、すぱっと Git に切り替えて、リポジトリも GitHub を利用させてもらえばいろいろスッキリするじゃん、と。

ということで、移行することにしたのですが、たぶんステップとしては2つ。

  1. ローカルの hg リポジトリを git リポジトリに変換
  2. ローカルの git リポジトリを GitHub にインポート
手順については、9.2 Gitとその他のシステムの連携 - Git へ移行する にまとめられているので、これに沿ってやっていきます。

ローカルの hg リポジトリを git リポジトリに変換

まずは懸案の /Apps/bin から取り掛かります。

hg-fast-export をクローンします。
$ git clone http://repo.or.cz/r/fast-export.git /Apps/fast-export
説明では '/tmp/fast-export' となっていますが、ここではわかりやすいところとして '/Apps/fast-export' を指定します。クローン後に中身を見ると、 hg-fast-export.sh と hg-fast-export.py があります。この hg-fast-export.py は hg-fast-export.sh から呼び出されるため、sh 系のシェルで実行する必要があります。そのため、此処から先は MSYS2 で作業を行います。

次に、コマンドラインから Mercurial を使えるようにします。TortoiseHG はインストールしてありますが、ここでは hg-fast-export.sh を実行するので、MSYS2 に Python3 をインストールし、さらに Mercurial もインストールして hg コマンドを使えるようにしておきます。
$ pacman -S python mercurial
そうしたら次に author の正規化を行います。具体的には、コミットしたユーザ名を "ユーザ名" <メールアドレス> の形式に修正します。
$ cd /Apps/bin
$ hg log | grep user: | sort | uniq | sed 's/user: *//' > ../authors
ここでは authors をみると全部同じ名前だったけれどメールアドレスが入っていなかったので(なんたってローカルリポジトリですから……)
bob=Bob Jones <bob@company.com>
の例にあるように '=' に続けて正しいユーザ名形式で記述しておきます。

次は Mercurial リポジトリから Git リポジトリへの変換の準備です。
$ cd /c/Apps
$ git init converted
$ cd converted
$ ../fast-export/hg-fast-export.sh -r ../bin -A ../authors
ここでもしかしたら以下のようなエラーが出るかもしれません。
/c/Apps/converted% ../fast-export/hg-fast-export.sh -r ../bin -A ../authors
Error: The option core.ignoreCase is set to true in the git
repository. This will produce empty changesets for renames that just
change the case of the file name.
Use --force to skip this check or change the option with
git config core.ignoreCase false
/c/Apps/converted% 

なぜか core.ignoreCase が true になってるよ、ということですので、最後の行にあるように
/c/Apps/converted% git config core.ignoreCase false

で設定を追加しておきます。

いよいよコンバートです。
/c/Apps/converted% ../fast-export/hg-fast-export.sh -r ../bin -A ../authors
Invalid file format in [../authors], line 1
Loaded 0 authors
master: Exporting full revision 1/144 with 32/0/0 added/changed/removed files
master: Exporting simple delta revision 2/144 with 0/1/0 added/changed/removed files
master: Exporting simple delta revision 3/144 with 0/1/0 added/changed/removed files
[...]
master: Exporting simple delta revision 143/144 with 0/2/0 added/changed/removed files
master: Exporting simple delta revision 144/144 with 0/2/0 added/changed/removed files
Issued 144 commands
fast-import statistics:
---------------------------------------------------------------------
Alloc'd objects:       5000
Total objects:          490 (         0 duplicates                  )
      blobs  :          202 (         0 duplicates        160 deltas of        200 attempts)
      trees  :          144 (         0 duplicates        141 deltas of        141 attempts)
      commits:          144 (         0 duplicates          0 deltas of          0 attempts)
      tags   :            0 (         0 duplicates          0 deltas of          0 attempts)
Total branches:           1 (         1 loads     )
      marks:           1024 (       144 unique    )
      atoms:             45
Memory total:          2493 KiB
       pools:          2141 KiB
     objects:           351 KiB


---------------------------------------------------------------------
pack_report: getpagesize()            =      65536
pack_report: core.packedGitWindowSize = 1073741824
pack_report: core.packedGitLimit      = 35184372088832
pack_report: pack_used_ctr            =          2
pack_report: pack_mmap_calls          =          1
pack_report: pack_open_windows        =          1 /          1
pack_report: pack_mapped              =     277981 /     277981
---------------------------------------------------------------------
なんか最初にエラーが出てます。どうもauthorsファイルがダメって言ってるようですが。このままだとユーザ名が正しくありません。

ということで別の方法を取ります。
'/c/Apps/bin/.hg/hgrc' を開き、
[extensions]
hgext.convert=

を追加します。そして、
/c/Apps/bin% hg convert --authors ../authors . ../bak
initializing destination ../bak repository
scanning source...
sorting...
converting...
143 initial import
[...]
writing author map file C:\Apps\bak\.hg\authormap

/c/Apps/bin% 

としてやれば、'../bak' にリポジトリを作り直してくれます。
'../convert' をいったん削除してから、再度コンバートしてみます。
/c/Apps% rm -rf converted
/c/Apps% git init converted
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /c/Apps/converted/.git/
/c/Apps% cd converted
/c/Apps/converted% git config core.ignoreCase false
/c/Apps/converted% ../fast-export/hg-fast-export.sh -r ../bak
master: Exporting full revision 1/144 with 32/0/0 added/changed/removed files
master: Exporting simple delta revision 2/144 with 0/1/0 added/changed/removed files
master: Exporting simple delta revision 3/144 with 0/1/0 added/changed/removed files
master: Exporting simple delta revision 4/144 with 1/0/0 added/changed/removed files
master: Exporting simple delta revision 5/144 with 0/1/0 added/changed/removed files
[...]
master: Exporting simple delta revision 144/144 with 1/2/1 added/changed/removed files
Issued 144 commands
fast-import statistics:
---------------------------------------------------------------------
Alloc'd objects:       5000
Total objects:          490 (         1 duplicates                  )
      blobs  :          202 (         1 duplicates        160 deltas of        200 attempts)
      trees  :          144 (         0 duplicates        141 deltas of        141 attempts)
      commits:          144 (         0 duplicates          0 deltas of          0 attempts)
      tags   :            0 (         0 duplicates          0 deltas of          0 attempts)
Total branches:           1 (         1 loads     )
      marks:           1024 (       144 unique    )
      atoms:             46
Memory total:          2493 KiB
       pools:          2141 KiB
     objects:           351 KiB
---------------------------------------------------------------------
pack_report: getpagesize()            =      65536
pack_report: core.packedGitWindowSize = 1073741824
pack_report: core.packedGitLimit      = 35184372088832
pack_report: pack_used_ctr            =          2
pack_report: pack_mmap_calls          =          1
pack_report: pack_open_windows        =          1 /          1
pack_report: pack_mapped              =     278859 /     278859
---------------------------------------------------------------------
/c/Apps/converted% git shortlog -sn
   144  Chiyosuke Fujii
/c/Apps/converted%

以上でローカル側での作業は終わりで、あとはできあがったリポジトリを GitHub にプッシュするだけです。
$ git remote add origin git@my-git-server:myrepository.git
$ git push origin --all
嘘でした。ここから苦難の道が始まりました。


0 件のコメント:

コメントを投稿

AstroNvimでtelescope.nvimがエラーを吐いたとき。

Windowsの環境でAstroNvimをインストールして、Find Fileしたらtelescopeがエラーを吐いてきました。 メッセージは、 Failed to run `config` for telescope.nvim ...a/lazy/telescope.nvim/...