Skip to content

Instantly share code, notes, and snippets.

@jeroenvermeulen
Created March 3, 2018 20:53
Show Gist options
  • Save jeroenvermeulen/8971228f56c7f0d7bd72cbcc9802b7e7 to your computer and use it in GitHub Desktop.
Save jeroenvermeulen/8971228f56c7f0d7bd72cbcc9802b7e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
File: .git/hooks/pre-commit
Referencing current branch in github README.md[1]
This pre-commit hook[2] updates the README.md file's
Travis badge with the current branch. Gist at[4].
[1] http://stackoverflow.com/questions/18673694/referencing-current-branch-in-github-readme-md
[2] http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
[3] https://docs.travis-ci.com/user/status-images/
[4] https://gist.github.com/dandye/dfe0870a6a1151c89ed9
"""
import subprocess
# Hard-Coded for your repo (ToDo: get from remote?)
GITHUB_USER="magehost"
REPO="magento-malware-scanner"
print "Starting pre-commit hook..."
BRANCH=subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip()
# [![Build Status](https://travis-ci.org/user/repo.png?branch=branchname)](https://travis-ci.org/user/repo?branch=branchname)
# Output String with Variable substitution
travis="[![Build Status]" \
"(https://travis-ci.org/{GITHUB_USER}/{REPO}.png?branch={BRANCH})" \
"]" \
"(https://travis-ci.org/{GITHUB_USER}/{REPO}?branch={BRANCH})" \
"\n" \
.format(BRANCH=BRANCH, GITHUB_USER=GITHUB_USER, REPO=REPO)
sentinal_str="[![Build Status]"
readmelines=open("README.md").readlines()
with open("README.md", "w") as fh:
for aline in readmelines:
if sentinal_str in aline and travis != aline:
print "Replacing:\n\t{aline}\nwith:\n\t{travis}".format(
aline=aline,
travis=travis)
fh.write(travis)
else:
fh.write(aline)
subprocess.check_output(["git", "add", "README.md" ])
print "pre-commit hook complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment