This article introduces git pre-push hook.

Problem

In Lean project, we use a modified version of Google’s C++ style checker. I want to automatically run the checker over the changed files before I push commits to git repositories. This is useful because it prevents me from accidentally pushing the commits which contain style problems.

Solution: git pre-push hook

Since git 1.8.2, git introduced pre-push hook which is executed before actual push operation is performed. The following two steps solve the problem.

  • Create <PROJECT_ROOT>/.git/hooks/pre-push file with the following contents:
#!/usr/bin/env bash
IFS=' '
DIR="$( cd "$( dirname "$0" )" && pwd )"
CHECKER=$DIR/../../src/cmake/Modules/cpplint.py
while read local_ref local_sha remote_ref remote_sha;
do
CHANGED_FILES=`git diff --name-only $local_sha $remote_sha | grep '\(cpp\|h\)$'`
if [ ! -z "$CHANGED_FILES" -a "$CHANGED_FILES" != " " ]; then
echo $CHANGED_FILES | xargs $CHECKER
RET=$?
if [ $RET -ne 0 ]; then
echo "There is error(s) from style-check. Please fix them before push to the repo."
exit $RET
fi
fi
done
exit 0
  • Give “exec” permission to the file
chmod +x <PROJECT_ROOT>/.git/hooks/pre-push

Note that you need to change CHECKER variable if you want to use other checkers.

git pre-push hook的更多相关文章

  1. git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题

    在同步本地文件到线上仓库的时候 报错 pre -commit hook failed (add --no-verify to bypass) 当你在终端输入git commit -m "xx ...

  2. 解决git pull/push每次都需要输入密码问题 和 HttpRequestException encountered

    如果我们git clone的下载代码的时候是连接的https://而不是git@git (ssh)的形式,当我们操作git pull/push到远程的时候,总是提示我们输入账号和密码才能操作成功,频繁 ...

  3. git server side hook 试用

    git 的hook 是一个很方便的功能,我们可以使用hook 做好多处理,比如client side hook 进行 提交格式校验,server side 进行ci/cd 处理 测试使用docker- ...

  4. git问题--Push rejected: Push to origin/master was rejected

    解决git问题 Push rejected: Push to origin/master was rejected 意思是git拒绝合并两个不相干的东西 此时你需要在打开Git Bash,然后进入相应 ...

  5. git一键push至github脚本

    ######################################################################### # File Name: push.sh # Aut ...

  6. 使用 expect 重启失败的 git pull/push 操作

    问题的提出 最近使用 github 上传.下载项目代码时,经常会卡很久,有时候在命令行打了 git push 然后就去上厕所了,结果等我回来的时候,发现 push 早已经失败了,还得重新提交一下.如果 ...

  7. SourceTree/git解决pre-commit hook failed的问题

    一. git commit -m 'xxx' 出现问题 今天在上传项目的时候在commit阶段遇到一个问题,无论是在Sourcetree上传还是用命令git commit -m 'xxx'都报了一下错 ...

  8. git remote: error: hook declined to update

    提交一个项目,push的时候,报错: remote: error: File xxx.rar is MB; this exceeds Git@OSC's file size limit of 100 ...

  9. 使用Git的Push出现rejected - non-fast-forward错误

    通过查阅资料,发现是文件冲突问题,即本地和远程的Repository中的文件出现了冲突所致,重新检查了一下,发现是在建立Repository时,添加了ReadMe.txt文件,导致和本地得项目分支不一 ...

  10. 配置git同时push到两个远端库的简单方法

    最近在写一个开源的论坛系统,在发布代码时选择了github和coding这两个平台,我手懒,不想敲两次git push了,所以说突然有了一个很奇怪的需求:用一条git push同时push到两个远端代 ...

随机推荐

  1. python:面向对象初级

    面向对象编程类的概念 : 具有相同属性和技能的一类事物 人类 抽象对象 : 就是对一个类的具体的描述 具体的人 具体 使用面向对象的好处: 使得代码之间的角色关系更加明确 增强了代码的可扩展性 规范了 ...

  2. android全屏/沉浸式状态栏下,各种键盘挡住输入框解决办法

    https://blog.csdn.net/smileiam/article/details/69055963

  3. Unity 缓冲池概念

    在Unity 中碰到一些重复使用的游戏物体时,我们可以使用缓冲池来进行操作,即重复利用

  4. Mock测试接口

    Mock使用场景: (1)创建所需的DB数据可能需要很长时间,如:调用别的接口,模拟很多数据,确保发布版本接口可用 (2)调用第三方API接口,测试很慢, (3)编写满足所有外部依赖的测试可能很复杂, ...

  5. 一个小白用 PhotoView 引起的学习记录

    缘由(可跳过) 作为一个开发小白,有着各种各样想实现的功能, 最近想实现一个图片查看的功能(有放大,缩小等功能),发现了 PhotoView 这个开源框架, 用了它,腰不疼,腿不酸了 ... 添加依赖 ...

  6. Windows系统配置

    1.常用设置 (1)设置虚拟内存 右击:计算机-属性-高级系统设置-高级选项卡-性能组框设置按钮-高级选项卡-虚拟内存组框更改按钮-按需求设置 (2)设置休眠文件 改变休眠文件大小:powercfg ...

  7. 软链接ln -s 总结

    ln -s 软链接知识总结 1.软连建立:ln  -s  源文件 软链接文件 2.误区:软链接是创建的,就意味着软链接文件不可以在创建之前存在 3.类比:win快捷方式 4.删除:rm就可以,但源文件 ...

  8. 关于getClass()的一些自己的见解

    通过观看视频讲解,我得知,Class 老师讲解的是 这个是类的类,即是管理类的类.当时不是很理解,随后查询了相关资料后,以及真切的运行了几行代码后,才有一些深层次的体会, 在这里想要跟大家分享一下.如 ...

  9. C#验证ip地址的代码

    如下的代码是关于C#验证ip地址的代码. public Boolean CheckIPValid(String strIP) { char chrFullStop = '.'; string[] ar ...

  10. vue简单的导航栏

    <style> img{ display:block; margin:0 auto; width:500px; height:500px; } #app li{ list-style: n ...