ソフラボの技術ブログ

仕事で使ったプログラミング、サーバー周りで役に立つこと、Webサービス開発に必要な技術情報、モバイル情報を書いてます。わかりやすく見やすくをモットーにしています。

MecurialをGitに変換する

愛用しているバージョン管理サービスのbitbucketでMercurialの提供を終了し、Gitに完全移行するということで、既存のMercurialリポジトリをGitに移行してみました。

記事を公開するまでの期間が長かったため、抜けや間違い等があるかもしれないので、参考程度にしてみてください。

動作環境

Mac
Homebrew

環境構築

MecurialをGitに変換するための環境を構築します。

Pythonの確認

$ python --version
Python 2.7.10

# インストールされてなければ
$ brew install python2

pipの確認(Pythonのパッケージ管理)

# インストール確認
$ pip
-bash: pip: command not found

# curlでpipを取得
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1764k  100 1764k    0     0  3484k      0 --:--:-- --:--:-- --:--:-- 3488k

# pipをインストール
$ python get-pip.py --user
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
  Using cached pip-20.0.2-py2.py3-none-any.whl (1.4 MB)
Collecting wheel
  Using cached wheel-0.33.6-py2.py3-none-any.whl (21 kB)
Installing collected packages: pip, wheel
  WARNING: The scripts pip, pip2 and pip2.7 are installed in '/Users/xxxxx/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script wheel is installed in '/Users/xxxxx/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.0.2 wheel-0.33.6

# 環境変数を設定
$ vi ~/.bash_profile
export PATH=$PATH:$HOME/Library/Python/2.7/bin

# 環境変数の有効化
$ source ~/.bash_profile

PythonにMecurialをインストール

$ pip install mercurial
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting mercurial
  Downloading mercurial-5.2.2.tar.gz (7.3 MB)
     |████████████████████████████████| 7.3 MB 2.3 MB/s 
Building wheels for collected packages: mercurial
  Building wheel for mercurial (setup.py) ... done
  Created wheel for mercurial: filename=mercurial-5.2.2-cp27-cp27m-macosx_10_14_intel.whl size=2728813 sha256=f0b70639c66146acc92e93b5d1325c68d5ab60eee4f62208d43ba4ad1f47821f
  Stored in directory: /Users/xxxxx/Library/Caches/pip/wheels/64/d4/95/33110d3cbdbafbd02d90913d03b12064730908962c08990d68
Successfully built mercurial
Installing collected packages: mercurial
Successfully installed mercurial-5.2.2

作業ディレクトリの作成

任意の場所に作業用ディレクトリを作成します。

MecurialをGitに変換する

変換ツールをダウンロードする

$ git clone https://github.com/frej/fast-export.git

変換

# Mecurialリポジトリに移動
$ cd HgRepo

# コミットログの登録ユーザー名を一覧に出力
$ hg log | grep user: | sort | uniq | sed 's/user: *//' > ../authors

# ユーザー名を変換でできる形式に変更
$ vi ../authors
# Mecurialでのユーザー一覧
User1
Yamada Taro
# 上記を以下の形式に書き換えて保存
"User1"="User1 <user1@gmail.com>"
"Yamada Taro"="Yamada Taro <yamada_taro@yahoo.co.jp>"

# Mecurialリポジトリ内でGitリポジトリを作成
$ git init

# 変換時にエラーが出るので設定を変更しておく
$ git config core.ignoreCase false

# 変換を実行する
$ ../fast-export/hg-fast-export.sh -r . -A ../authors

変換したソースをリモートにプッシュする

# リモート設定
$ git remote add origin https://xxxxx@bitbucket.org/xxxxx/GitRepo.git

# プッシュ
$ git push origin master

# タグを一括でプッシュ
$ git push origin master --tags