代码之家  ›  专栏  ›  技术社区  ›  Hi-Angel

git将“审阅者”追加到提交

  •  2
  • Hi-Angel  · 技术社区  · 6 年前

    Reviewed-by: user<mail> .

    Invalid line: 10: Reviewed-by: User <mail>

    GIT_EDITOR='git interpret-trailers --trailer "Reviewed-by: User <mail>" --in-place' git rebase -i HEAD~8
    

    我也问过IRC,但没有结果。

    欢迎提出任何其他建议。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Hi-Angel    4 年前

    git rebase 解决方案

    我制定了以下解决方案。它看起来并不完美,但它确实发挥了作用,在我的健全性测试中,它不会失败,例如,对于只有一个双引号的提交消息:

    git rebase HEAD~2 -x 'git commit --amend -m"$(git log --format=%B -n1)$(echo -ne \\nReviewed-by: User \<mail\>.)"'
    

    让我们把命令分解一下:

    1. git rebase HEAD~2 -x … (其中n=2) ,您想附加到 Reviewed-by
    2. git commit --amend -m… 修正提交,并将其消息替换为下面的消息。
    3. git log --format=%B -n1
    4. echo -ne \\nReviewed-by: User \<mail\>. 这个有点棘手。最明显的是它增加了 审核人 echo 这里添加新行,然后添加文本。

    您可以通过从命令历史记录中获取它来使用它,也可以将它包装到bash/zsh函数中,例如:

    # adds reviwed-by to n commits
    function git_rb() {
        # export arguments, otherwise they're not visible to inline shell executions
        export who=$1
        export mail=$2
        export n=$3
        git rebase HEAD~$n -x 'git commit --amend -m"$(git log --format=%B -n1)$(echo -e \\nReviewed-by: ${who} \<${mail}\>.)"'
    }
    

    $ git_rb "Holy Moly" "Holy@example.com" 5
    

    git filter-branch 解决方案

    一个有限的解决方案 this article . 结果,你可以用 git筛选器分支 做同样的事。它会打印出一个警告,尽管它会破坏历史,并建议使用 git filter-repo 相反。但这是一个独立于git的项目,我没有测试它。从阅读“gotchas”我得到了一个印象,那就是修改提交消息 很好。但是我发现这个方法有一个问题:你不能使用 HEAD~n 符号,它会随着 You must specify a ref to rewrite 错误。相反,你必须使用这样的符号 master..HEAD

    git filter-branch -f --msg-filter 'cat && echo -e "\nReviewed-by: User<Holy Moly>"' master..HEAD