简体   繁体   English

Git挂钩-提交后自动更新文件md5sum

[英]Git hooks - update file md5sum automatically upon commit

One of the scripts in my git repository is distributed to many users. 我的git存储库中的脚本之一已分发给许多用户。 When they run this script I would it to look into my repository and see if any change is made to this script and if so suggest the users to update the script from the repository. 当他们运行此脚本时,我希望它可以查看我的存储库并查看是否对该脚本进行了任何更改,如果是,则建议用户从存储库中更新脚本。 The users know nothing about git and wish to understand as little as possible in the system implementation. 用户对git一无所知,并希望在系统实现中尽可能少地了解。

I would like to automatically update a this script's md5sum every time I commit it, without the need to bother and do it manually. 我想在每次提交该脚本时自动更新它的md5sum,而无需费心并手动进行。 Here is the pre-commit hook I wrote 这是我写的预提交钩子

#!/bin/sh

echo -n "# "  > test.txt.tmp
linesNumber=`wc -l test.txt | awk '{print $1}'`
tail -n $(($linesNumber - 1)) test.txt | md5sum | awk '{print $1}' >> test.txt.tmp
tail -n $(($linesNumber - 1)) test.txt >> test.txt.tmp
mv test.txt.tmp test.txt
#git push
#git commit --no-verify --message "update file md5sum"

The md5sum works fine, the problem is that after the commit my script is again shows as modified. md5sum工作正常,问题在于提交后我的脚本再次显示为已修改。 I tried to commit my changes without running the pre-commit hook (--no-verify) but git says I can't commit because my branch is ahead of 'origin/master' by 1 commit, I tried to push my changes before committing the md5sum but it never finished the operation. 我试图在不运行预提交钩子的情况下提交更改(--no-verify),但是git表示我无法提交,因为我的分支比'origin / master'提前1次提交,因此我尝试在更改之前推送更改提交md5sum,但从未完成操作。

What is the right way to achieve this type of functionality? 实现此类功能的正确方法是什么?

Your pre-commit hook does not git add the updated test.txt file. 您的预提交钩子不会git add更新的test.txt文件。

Note that the version of the file in the working directory is not necessarily the version that would be committed: 请注意,工作目录中文件的版本不一定是要提交的版本:

echo foo > test.txt
git add test.txt
echo bar > test.txt
git commit -m 'this commit has "foo" in test.txt'

To see, in a pre-commit hook, the version of the file in the index—ie, what will be committed if the pre-commit hook allows the commit to proceed, without doing any git add step—you can use git show :0:test.txt . 要查看预提交钩子中索引中文件的版本(即,如果预提交钩子允许进行提交而不执行任何git add步骤,将执行什么操作),可以使用git show :0:test.txt The magic :0: prefix is gitrevisions syntax for "version staged for commit". 神奇的:0:前缀是gitrevisions语法,用于“为提交而进行的版本转换”。 (You can abbreviate this further as just :test.txt , although I find the :0: a bit more obvious as a revspec; I've seen real files really named :README for instance. :-) ) Note that this bypasses any smudge filter(s) though. (您可以将其进一步缩写为:test.txt ,尽管我发现:0:作为revspec更为明显;例如,我已经看到了真正名为:README真实文件。:-))不过会弄脏过滤器。

(Hooks that want to test the entire current index contents, but preserve working directory state as well, can use git stash save --keep-index . See How do I properly git stash/pop in pre-commit hooks to get a clean working tree for tests for advice, and a caveat, on doing this.) (要测试整个当前索引内容但仍保留工作目录状态的git stash save --keep-index ,可以使用git stash save --keep-index 。请参阅如何在预提交的钩子中正确git stash / pop以获得干净的工作进行测试以获取建议的 ,并对此进行警告。)

In my current version of git (1.8.5.4), if I modify a file and git add the result in a pre-commit hook, the contents I add ed are what goes into the commit. 在当前版本的git(1.8.5.4)中,如果我修改文件并将git add到预提交钩子中,那么我add的内容即为提交内容。 (I do have some vague recollection of an earlier version of git saving the index before the pre-commit hook runs, so that changes made to the index did not make it into the commit, but I might just be mis-remembering.) (我确实对git的较早版本进行了模糊的回忆,以在预提交挂钩运行之前保存索引,以便对索引所做的更改不会使其进入提交,但我可能只是记错了。)

Aside: It's generally not a great idea to modify the commit-contents in a pre-commit hook. 另外:在预提交钩子中修改提交内容通常不是一个好主意。 They're really just meant to verify that the commit is ready to go. 它们实际上只是用来验证提交已准备就绪。 But if you're determined to modify the contents, git add (or git update-index but add is much simpler) will do it. 但是,如果您决定修改内容,则可以使用git add (或git update-index但add更为简单)来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM