使用 git-filter-repo 的默认参数编写 shell 脚本或 Python 代码

问题描述 投票:0回答:1

有时,我需要更改存储库中所有提交的作者/提交者电子邮件和名称(始终假设作者=提交者)。我需要为回购执行一次此操作,但我可能需要为多个回购执行此操作。我正在使用这个很好的答案:

如何修改Git中的多个提交来更改作者

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
    不同的参数,脚本应生成错误

我怎样才能做到这一点?

bash git shell command-line-arguments
1个回答
1
投票

您可以通过在回调中使用

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
块中添加默认交换。

旁注: 在您问题的示例中,您仅根据作者设置作者和提交者。也许它们在你的仓库中总是相同的,在这种情况下这并不重要。如果它们确实不同,您可能希望单独处理它们。

© www.soinside.com 2019 - 2024. All rights reserved.