有时,我需要更改存储库中所有提交的作者/提交者电子邮件和名称(始终假设作者=提交者)。我需要为回购执行一次此操作,但我可能需要为多个回购执行此操作。我正在使用这个很好的答案:
git filter-repo --commit-callback '
if commit.author_email == b"incorrect@email":
commit.author_email = b"correct@email"
commit.author_name = b"Correct Name"
commit.committer_email = b"correct@email"
commit.committer_name = b"Correct Name"
'
但是,每次我想为新存储库运行此命令时,我都需要手动更改上面命令中的
incorrect@email
、correct@email
和Correct Name
。使用 shell 脚本或者 Python CLI 应用程序会更容易,它接受参数 old_email
、new_email
和 author
。
old_email
、new_email
或 author
不同的参数,脚本应生成错误我怎样才能做到这一点?
您可以通过在回调中使用
elif
和 else
一次性完成这一切,如 git-filter-repo
文档示例中所示,如下所示:
--------------------------------------------------
git-filter-repo --filename-callback '
if commit.author_email == b"incorrect1@email"
# change it
elif commit.author_email == b"incorrect2@email"
# change it
else:
# put default here
'
--------------------------------------------------
在
elif
块中添加额外交换,并在 else
块中添加默认交换。
旁注: 在您问题的示例中,您仅根据作者设置作者和提交者。也许它们在你的仓库中总是相同的,在这种情况下这并不重要。如果它们确实不同,您可能希望单独处理它们。