在python/bash脚本中编写Git钩子

myu*_*uf3 26 git githooks

我最近需要编写git hooks,用于引用特定票证的所有提交.

我希望有一个地方可以开始学习.pro git书中的所有内容都是用Ruby编写的.由于Ruby不是我强大的西装,任何人都可以分享有关用其他语言编写的git hook的教程吗?(我特别喜欢Python或Bash脚本.)

Pet*_*ron 21

是一个使用Python作为钩子的例子.通常,钩子是语言不可知的.您可以使用该脚本执行某些操作或使用0 /其他返回代码退出以更改git进程的流程.


int*_*ted 8

git附带的例子是用shell脚本编写的; .git/hooks每个仓库中都有一些基本的,并且安装了更高级的仓库/usr/share/doc/git-core/contrib/hooks.

还有更多关于各种钩子的信息$ man githooks.


Rom*_*nov 5

我发现在python上编写git hook很容易.这是python上的post-receive hook的一个例子.提供的示例在不同文件夹中部署master和develop 分支(master中的更改将被推送到生产网站,develop分支中的更改将被推送到qa站点)

#!/usr/bin/env python                                                                    
# -*- coding: UTF-8 -*-                                                                  
#post-receive                                                                            

import sys                                                                               
import subprocess                                                                        

# 1. Read STDIN (Format: "from_commit to_commit branch_name")                            
(old, new, branch) = sys.stdin.read().split()                                            

# 2. Only deploy if master branch was pushed                                             
if branch == 'refs/heads/master':                                                        
    subprocess.call('date >> ~/prod-deployment.log', shell=True)                         
    subprocess.call('GIT_WORK_TREE=/home/ft/app.prod git checkout master -f', shell=True)
    subprocess.call('cd ../../app.prod;bower update', shell=True)                        

#3. Only deploy if develop branch was pushed                                             
if branch == 'refs/heads/develop':                                                       
    subprocess.call('date >> ~/dev-deployment.log', shell=True)                          
    subprocess.call('GIT_WORK_TREE=/home/ft/app.dev git checkout develop -f', shell=True)
    subprocess.call('cd ../../app.dev;bower update', shell=True)                         
Run Code Online (Sandbox Code Playgroud)