From bc3719402197835fb064a141a4bbe24243110cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 28 Jan 2018 22:16:33 +0100 Subject: [PATCH] Added pre-commit hook to reference current branch in README.md --- README.md | 3 ++- pre-commit | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 pre-commit diff --git a/README.md b/README.md index 432b9955..4362f88a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8266 based smart switch It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries. [![version](https://badge.fury.io/gh/xoseperez%2Fespurna.svg)](CHANGELOG.md) -[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=master)](https://travis-ci.org/xoseperez/espurna) +![branch](https://img.shields.io/badge/branch-dev-orange.svg) +[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna) [![license](https://img.shields.io/github/license/xoseperez/espurna.svg)](LICENSE) [![donate](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=xose%2eperez%40gmail%2ecom&lc=US&no_note=0¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHostedGuest) [![twitter](https://img.shields.io/twitter/follow/xoseperez.svg?style=social)](https://twitter.com/intent/follow?screen_name=xoseperez) diff --git a/pre-commit b/pre-commit new file mode 100755 index 00000000..caecaede --- /dev/null +++ b/pre-commit @@ -0,0 +1,52 @@ +#!/usr/bin/python +""" + +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. Based on [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 + +Copy this file to .git/hooks/ + +""" + +import os +import sys +import re +import subprocess + +README = os.path.dirname(os.path.realpath(__file__)) + "/../../README.md" +remote = subprocess.check_output(["git", "remote", "-v"]).strip().split('\n')[0] +parts = re.split('[/\.: ]', remote) +REPO = parts[ len(parts) - 3] +USER = parts[ len(parts) - 4] +BRANCH=subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip() + +branch_mark = "![branch]" +branch = "![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)\n".format( + BRANCH=BRANCH + ) + +travis_mark = "![travis]" +travis = "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \ + "(https://travis-ci.org/{USER}/{REPO})\n".format( + USER=USER, + REPO=REPO, + BRANCH=BRANCH + ) + +lines = open(README).readlines() +with open(README, "w") as fh: + for line in lines: + if travis_mark in line: + fh.write(travis) + elif branch_mark in line: + fh.write(branch) + else: + fh.write(line) + +subprocess.check_output(["git", "add", README ])