Ian*_*ell 58 git pre-commit pre-commit-hook githooks
我想使用Git中的预提交或后提交挂钩将自动生成的文件添加到同一提交中,这取决于在该提交中修改的文件.我该怎么做?
我已经尝试过这个作为预先提交的钩子,但没有运气:
#!/bin/sh
files=`git diff --cached --name-status`
re="<files of importance>"
if [[ $files =~ $re ]]
then
echo "Creating files"
exec bundle exec create_my_files
exec git add my_files
exec git commit --amend -C HEAD
fi
Run Code Online (Sandbox Code Playgroud)
这会成功将它们添加到存储库,但不会将它们添加到提交中.我也尝试在post-commit钩子中使用最后两个exec行以及pre-commit检查,但也没有好处.
小智 41
由于git add在预提交中对我来说也不起作用,因此我遵循了标记使用.commit文件并将进程分为提交前和提交后的想法.
以下是一些易于理解的代码
在预提交中:
- 触摸文件.commit或其他内容.(务必将其添加到.gitignore)
#!/bin/sh
echo
touch .commit
exit
Run Code Online (Sandbox Code Playgroud)
在提交后:
如果.commit存在,你知道刚刚发生了提交但是提交尚未运行.所以,你可以在这里进行代码生成.另外,测试.commit以及它是否存在:
- 添加文件
- commit --amend -C HEAD --no-verify(避免循环)
- 删除.commit文件
#!/bin/sh
echo
if [ -a .commit ]
then
rm .commit
git add yourfile
git commit --amend -C HEAD --no-verify
fi
exit
Run Code Online (Sandbox Code Playgroud)
希望这使得那些没有什么bash知识的人更容易遵循马克的想法.
Jim*_*vin 32
使用预提交挂钩可以做你想做的事.我们为heroku部署做了类似的事情(将coffeescript编译为javascript).您的脚本无法正常工作的原因是您未exec正确使用该命令.
从手册页:
exec builtin用于使用新命令替换当前运行的shell进程映像.成功完成后,exec永远不会返回.exec不能在管道内使用.
只有第一个exec命令正在运行.之后,您的脚本基本上终止了.
尝试这样的东西(作为预提交钩子):
#!/bin/sh
files=`git diff --cached --name-status`
re="<files of importance>"
if [[ $files =~ $re ]]
then
echo "Creating files"
bundle exec create_my_files
git add my_files
fi
Run Code Online (Sandbox Code Playgroud)
小智 10
您可以使用前提交和后提交脚本的组合.
在预提交中:
在提交后:
如果.commit存在,你知道刚刚发生了提交但是提交尚未运行.所以,你可以在这里进行代码生成.另外,测试.commit以及它是否存在:
这大致是我用来在Metastore生成的存储库中存储.metadata文件的过程.
如果有人知道更好的方式,我会全神贯注,但它现在似乎有效.
您可以使用update-index:
git update-index --add my_files
#!/bin/sh
#
# .git/hooks/pre-commit
#
git add file.xyz
Run Code Online (Sandbox Code Playgroud)
这对我来说很好.它将成为当前提交的一部分.
git version 1.7.12.4 (Apple Git-37)
| 归档时间: |
|
| 查看次数: |
24634 次 |
| 最近记录: |