如何使用挂钩在提交时更改作者电子邮件

Ale*_*oux 4 git hook commit

问题说明了一切.

我希望通过使用我自己的别名来提交真实的电子邮件地址来为我的用户提供一些隐私.有没有可以帮我这样做的钩子?

Abi*_*ern 7

您可以让他们更改user.email本地存储库中的属性.

git config user.email email@server.com
Run Code Online (Sandbox Code Playgroud)


tor*_*rek 6

无论发生什么情况,如果您在某些提交中更改了某些内容\xe2\x80\x94,甚至是用户名中的单个字符和/或电子邮件\xe2\x80\x94,您都会得到一个新的、不同的提交。(这是因为提交 ID 由提交内容的加密校验和组成。所以,好吧,如果您可以破解密码,您可以提出两个具有相同 ID 的不同提交。但是您\'我完全破坏了 git,并且可能还赢得了图灵奖或诺贝尔奖。:-) )

\n\n

您在另一个答案的评论中链接到的“工作挂钩”可能是您最好的选择。这个问题归结为一件事:您无法对已发布的提交进行任何更改(不会给该出版物的使用者造成问题),但您可以对私有提交进行任何您想要的更改,只要您“知道什么”你正在做的”。在每次提交的基础上使用git commit --amend --author "$user <$email>" -C HEAD,当它们进入您的存储库副本时,可以保证您将未发布的提交替换为新的、略有不同的未发布的提交。(我假设您已将其放入提交后挂钩中。)

\n\n

我不确定你对哪一部分不满意,也许是[ -n "$richo_git_rewrite" ] && exit 0?这是一种相当聪明的检测递归的方法。另一种方法是跳过递归检测,而是将提交中的现有用户和电子邮件与所需的用户和电子邮件进行比较。

\n\n

这是一个执行此操作的脚本(除了我使用环境变量 SWITTCHY=true 来进行测试):

\n\n
#! /bin/sh\n# first, pick which git config variables to get\nif ${SWITCHY-false}; then\n    config=user.work\nelse\n    config=user\nfi\n# next, find out if they\'re set\ncan_rewrite=false\ntarget_author=$(git config --get $config.name) &&\n    target_email=$(git config --get $config.email) &&\n    can_rewrite=true\n# If they ARE set, we can "rewrite" (replace) the commit;\n# if not, we can\'t.  Just because we can, though, does not\n# mean we should.  Find out if the current author and email\n# differ from the desired ones.\nif $can_rewrite; then\n    current_author=$(git log --pretty=format:%an HEAD -n 1)\n    current_email=$(git log --pretty=format:%ae HEAD -n 1)\n    if [ "$current_author" != "$target_author" -o \\\n          "$current_email" != "$target_email" ]; then\n        # may want --allow-empty here, if you\'re allowing empty commits\n        # at all, otherwise empty ones don\'t get the rewrite done\n        git commit --amend --author "$target_author <$target_email>" -C HEAD\n    fi\nfi\n
Run Code Online (Sandbox Code Playgroud)\n\n

注意:您需要在 中设置相同的挂钩hooks/post-merge,才能与更新的姓名和电子邮件进行修改合并。当然,您可以稍微简化一下挂钩(不需要实际的 can_rewrite 变量,只需使用&&s 执行两个 git config get ops 并继续使用另一个&&)。除了 user vs user.work 之外,也许还有 user vs user.ModeA vs user.ModeB 等等,您可以将其驱动到任何您喜欢的测试(环境变量、是否存在命令、 ETC)。


好的,根据评论,这是一个预提交挂钩。不幸的是它不能与git merge(预提交挂钩运行并抱怨,然后合并提交进入)。

\n\n

#! /bin/sh\nfatal()\n{\n    echo "$@" 1>&2\n    exit 1\n}\n# pick which git config variables to get\nif ${SWITCHY-false}; then\n    config=user.work\nelse\n    config=user\nfi\n# tn, te = target author name/email\n# an, ae = what git will use for author name/email\ntn=$(git config --get $config.name) || exit 0\nte=$(git config --get $config.email) || exit 0\n\nan=${GIT_AUTHOR_NAME-$(git config --get user.name)} ||\n    fatal "no author name set"\nae=${GIT_AUTHOR_EMAIL-$(git config --get user.email)} ||\n    fatal "no author email set"\n[ "$an" = "$tn" -a "$ae" = "$te" ] ||\n    fatal "git will use author $an <$ae>\nbut you want them as $tn <$te>\nfix your environment variables and try again"\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可以将此预提交挂钩与合并后重写提交挂钩结合起来(eww :-))。我没有尝试冲突的提交,但大概预提交挂钩会捕获需要您自己进行提交的冲突合并。

\n\n

(还值得注意的是,这个预提交挂钩不会查看工作树或索引,因此它没有通常的“检查工作树但提交索引”缺陷。)

\n