火曜日, 2月 02, 2021

コミットログ(コミッター、コミッターのメールアドレス)を書き換える方法【改】

こちらの過去記事に従って、コミットログ(コミッター、コミッターのメールアドレス)を変えようとしたら以下のエラーが表示されたので、修正することにしました。

コミットログ(コミッター、コミッターのメールアドレス)を書き換える方法

表示されたエラーメッセージ

WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites.  Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead.  See the
filter-branch manual page for more details; to squelch this warning,
set FILTER_BRANCH_SQUELCH_WARNING=1.

なんでも git-filter-branch は不具合や実行速度が遅いので、今は Python で書かれた git-filter-repo を使うように勧められているようです。

こちらに git-filter-branch でのコマンドを git-filter-repo に書き換えるチートシートが用意されているのでこちらを見ながら、前回のブログ記事のコマンドを書き直すことにしました。

git-filter-repo/converting-from-filter-branch.md at main · newren/git-filter-repo

自分のやりたいことは間違ったメールアドレスを参照して間違ったメールアドレスのユーザー名、メールアドレスを変更することです。

従ってこちらのリンク先の git-filter-branch の例を git-filter-repo の例に書き換えます。

git-filter-repo/converting-from-filter-branch.md at main · newren/git-filter-repo - Changing author/committer(/tagger?) information

まずは git-filter-repo をインストールします。

インストール方法は、git-filter-repo のレポジトリに書いてありますが、パッケージツールを使ってインストールします。

サポートされているパッケージツールについては、git-filter-repo のレポジトリを参考にしてみて下さい。

git-filter-repo/INSTALL.md at main · newren/git-filter-repo

自分は、Mac の homebrew を使っているので、brew コマンドでインストールします。

$ brew install git-filter-repo

その後 .mailmap というファイル名でファイルを作り、間違ったメールアドレスと正しいユーザー名正しいメールアドレスを入力しておきます。

correct_name <correct_email@example.com> <wrong_email@example.com>

その状態で以下のコマンドを実行します。

$ git filter-repo --use-mailmap

こちらで上手くいくと思ったのですが、上手く変更されませんでした。

メールアドレスではなく、ユーザー名だと上手く行きました。

correct_name <correct_email@example.com> wrong_name

調べてみると git-filter-branch と似たような形式で変更できるコマンドが見つかりました。

How to change the author and committer name and e-mail of multiple commits in Git?

こちらを参考に変更してみます。

$ git filter-repo --commit-callback '

old_email = b"wrong@example.com"
correct_name = b"correct"
correct_email = b"correct@example.com"

if commit.committer_email == old_email :
  commit.committer_name = correct_name
  commit.committer_email = correct_email

if commit.author_email == old_email :
  commit.author_name = correct_name
  commit.author_email = correct_email
'

しかし、こちらでも上手く行かなかったので、email ではなく name を参照して変更することにしました。

$ git filter-repo --commit-callback '

old_name = b"wrong"
correct_name = b"correct"
correct_email = b"correct@example.com"

if commit.committer_name == old_name :
commit.committer_name = correct_name
commit.committer_email = correct_email

if commit.author_name == old_name :
commit.author_name = correct_name
commit.author_email = correct_email
'

すると、この方法では期待通りの結果を得ることが出来ました。

email では上手く変更が出来ず、name だと上手く変更が出来る理由は分かりませんでしたが、得たい結果が得られたのでヨシとします。

以上でおしまいです。

git-filter-branch を使うよりも .mailmap を使う場合はずっと簡単になります。

エラーメッセージも出ないし、精神的な負担も軽くすることができますね!

皆さんも試してみてください。

0 件のコメント:

コメントを投稿