Writing Git hooks in python/bash scripts [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionI have recently needed to write git hooks, for all commits to reference a particular ticket.
I was hoping for a place to start lea开发者_C百科rning. All the stuff in the pro git book is written in Ruby. Since Ruby is not my strong suit, can anybody share tutorials about git hooks written in other languages? (I'd particularly prefer Python or Bash scripts.)
Here is an example of using Python for a hook. In general the hooks are language agnostic. You use the script to do some work or to exit with a 0/other return code to alter the flow of a git process.
The examples that come with git are written in shell script; there are some basic ones in .git/hooks
of each repo and more advanced ones installed to /usr/share/doc/git-core/contrib/hooks
.
There's also more info on the various hooks available via $ man githooks
.
I found out that it's easy to write git hook on python. It's an example of post-receive hook on python. Provided example deploys master and develop branches in different folders (changes in master will be pushed to production website and changes in develop branch will be pushed to qa site)
#!/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)
精彩评论