Skip to content

Instantly share code, notes, and snippets.

@smgladkovskiy
Created May 30, 2019 20:06
Show Gist options
  • Save smgladkovskiy/04161dfb2d5c9ece437afa91b0ffb35e to your computer and use it in GitHub Desktop.
Save smgladkovskiy/04161dfb2d5c9ece437afa91b0ffb35e to your computer and use it in GitHub Desktop.
Pre-commit git hook to update TravisCI and CodeCov badges URL to aim current branch
#!/usr/bin/python2
"""
Referencing current branch in github README.md
This pre-commit hook updates the README.md file's
Travis and CodeCov badges with the current branch.
"""
import subprocess
# Hard-Coded for your repo (ToDo: get from remote?)
GITHUB_USER="whalekeepers"
REPO="whalekeeper"
print "Starting pre-commit hook..."
BRANCH=subprocess.check_output(["git",
"rev-parse",
"--abbrev-ref",
"HEAD"]).strip()
# TRAVIS CI
travis="<img src=\"https://travis-ci.org/" \
"{GITHUB_USER}/{REPO}.svg?" \
"branch={BRANCH}\"/>\n".format(BRANCH=BRANCH,
GITHUB_USER=GITHUB_USER,
REPO=REPO)
sentinal_str_travis="<img src=\"https://travis-ci.org/"
# CODECOV
codecov="<img src=\"https://codecov.io/gh/" \
"{GITHUB_USER}/{REPO}" \
"/branch/{BRANCH}/graph/badge.svg\"/>\n".format(BRANCH=BRANCH,
GITHUB_USER=GITHUB_USER,
REPO=REPO)
sentinal_str_codecov="<img src=\"https://codecov.io"
readmelines=open("README.md").readlines()
with open("README.md", "w") as fh:
for aline in readmelines:
if sentinal_str_travis in aline and travis != aline:
print "Replacing:\n\t{aline}\nwith:\n\t{travis}".format(
aline=aline,
travis=travis)
fh.write(travis)
elif sentinal_str_codecov in aline and codecov != aline:
print "Replacing:\n\t{aline}\nwith:\n\t{codecov}".format(
aline=aline,
codecov=codecov)
fh.write(codecov)
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