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. selenium 目录结构解释

    common目录         定义了通用的异常类 webdriver目录 android.backberry.chrome.edge.firefox.ie.opera.phantomjs.safa ...

  2. 基于STM8的GPIO操作---STM8-第一章

    1. 综诉 也许单片机在你看来是一件不太容易的事,但据我所知,单片机,无非就是控制它的GPIO口,所以可以看出,学会如何操作控制GPIO口对使用单片机来说是很重要的一件事. 在装载STM8的单片机中, ...

  3. note10 元组

    元组 Tuple +元组即不可变(immutable)列表 除了可改变列表内容的方法外,其他方法均适用于元组 因此,索引.切片.len().print等均可用 但是,appeng.extend.del ...

  4. Windows和MacOS的比较——不断完善和补充,欢迎吐槽

    1. 鼠标滚轮的方向不一样,Windows上滚轮朝下,页面滚动条也会朝下.而Mac上则相反. 2. Windows上有Home和End键,经常可以Ctrl+Home,Ctrl+End,Ctrl+Shi ...

  5. C语言排序算法学习笔记——选择类排序

    选择排序:每一趟(例如第i趟)在后面n-i+1(i=1,2,3,……,n-1)个待排序元素中选取关键字最小的元素,作为有序子序列的第i个元素,直到n-1趟做完,待排序元素只剩下1个,就不用再选了. 简 ...

  6. VS2017无法进入安装界面问题的解决方法

    VS2017无法进入安装界面问题的解决方法 打开C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe也没有 ...

  7. Java ---- 链表逆序

    public class LinkedListRevert { public static void main(String[] args) { Node next3 = new Node(4,nul ...

  8. Docker笔记——jdk镜像制作

    openjdk镜像依赖如下: openjdk:8-jdk -> buildpack-deps:jessie-scm -> buildpack-deps:jessie-curl -> ...

  9. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  10. MySQL表数据的增删改查

    1.增 INSERT INTO tb VALUES(v1,v2,v3...); INSERT INTO tb (field1,field2...) VALUES(v1,v2...); INSERT I ...