Browse Source

Merge pull request #806 from mcspr/git-commit-hook

Reworked git pre-commit hook
rfm69
Xose Pérez 6 years ago
committed by GitHub
parent
commit
30c1513fe2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 60 deletions
  1. +1
    -1
      README.md
  2. +105
    -59
      pre-commit

+ 1
- 1
README.md View File

@ -4,7 +4,7 @@ ESPurna ("spark" in Catalan) is a custom firmware for ESP8285/ESP8266 based smar
It uses the Arduino Core for ESP8266 framework and a number of 3rd party libraries.
[![version](https://img.shields.io/badge/version-1.12.6a-brightgreen.svg)](CHANGELOG.md)
![branch](https://img.shields.io/badge/branch-dev-orange.svg)
[![branch](https://img.shields.io/badge/branch-dev-orange.svg)](https://github.com/xoseperez/espurna/tree/dev/)
[![travis](https://travis-ci.org/xoseperez/espurna.svg?branch=dev)](https://travis-ci.org/xoseperez/espurna)
[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/dev.svg)](https://www.codacy.com/app/xoseperez/espurna/dashboard)
[![license](https://img.shields.io/github/license/xoseperez/espurna.svg)](LICENSE)


+ 105
- 59
pre-commit View File

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
"""
Referencing current branch in github README.md [1]
@ -17,61 +17,107 @@ Copy this file to .git/hooks/
import os
import sys
import re
import subprocess
BASE = os.path.dirname(os.path.realpath(__file__)) + "/../../"
README = BASE + "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()
def getVersion():
file_name = BASE + "code/espurna/config/version.h"
lines = open(file_name).readlines()
for line in lines:
if "APP_VERSION" in line:
parts = line.split('"')
return parts[1]
return "unknown"
VERSION = getVersion()
version = "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n".format(
VERSION = VERSION
)
branch = "![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)\n".format(
BRANCH = BRANCH
)
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
)
codacy = "[![codacy](https://img.shields.io/codacy/grade/{HASH}/{BRANCH}.svg)]" \
"(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n".format(
HASH = "c9496e25cf07434cba786b462cb15f49",
USER = USER,
REPO = REPO,
BRANCH = BRANCH
)
lines = open(README).readlines()
with open(README, "w") as fh:
for line in lines:
if "![travis]" in line:
fh.write(travis)
elif "![version]" in line:
fh.write(version)
elif "![branch]" in line:
fh.write(branch)
elif "![codacy]" in line:
fh.write(codacy)
else:
fh.write(line)
subprocess.check_output(["git", "add", README ])
from subprocess import call, check_output
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
from fileinput import FileInput
# https://github.com/python/cpython/commit/6cb7b659#diff-78790b53ff259619377058acd4f74672
if sys.version_info[0] < 3:
class FileInputCtx(FileInput):
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
FileInput = FileInputCtx
def run(cmd, cwd=None):
out = check_output(cmd, cwd=cwd)
out = out.decode("latin1").strip()
return out
def parse_h_string(define, r_quotes=re.compile("\"(.*)\"")):
string = r_quotes.search(define).group(1)
return string
def git_parse_remote(cwd=None, remote="origin"):
remote_url = run([
"git", "config", "--local",
"--get", "remote.{}.url".format(remote)], cwd)
if remote_url.startswith("git"):
_, _, repo = remote_url.partition(":")
path = repo.replace(".git", "")
elif remote_url.startswith("https"):
parsed = urlparse(remote_url)
path = parsed.path[1:]
return path.split("/")
def git_branch(cwd=None):
return run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd)
def espurna_get_version(base, version_h="code/espurna/config/version.h"):
version = "unknown"
path = os.path.join(base, version_h)
with open(path, "r") as version_f:
for line in version_f:
if line.startswith("#define") and "APP_VERSION" in line:
version = parse_h_string(line)
break
return version
TEMPLATES = {
"![travis]": "[![travis](https://travis-ci.org/{USER}/{REPO}.svg?branch={BRANCH})]" \
"(https://travis-ci.org/{USER}/{REPO})\n",
"![version]": "[![version](https://img.shields.io/badge/version-{VERSION}-brightgreen.svg)](CHANGELOG.md)\n",
"![branch]": "[![branch](https://img.shields.io/badge/branch-{BRANCH}-orange.svg)]" \
"(https://github.org/{USER}/{REPO}/tree/{BRANCH}/)\n",
"![codacy]": "[![codacy](https://img.shields.io/codacy/grade/c9496e25cf07434cba786b462cb15f49/{BRANCH}.svg)]" \
"(https://www.codacy.com/app/{USER}/{REPO}/dashboard)\n"
}
README = "README.md"
if __name__ == "__main__":
base = os.getcwd()
user, repo = git_parse_remote()
fmt = {
"USER": user,
"REPO": repo,
"BRANCH": git_branch(),
"VERSION": espurna_get_version(base)
}
templates = [
(k, tmpl.format(**fmt))
for k, tmpl in TEMPLATES.items()
]
def fmt_line(line):
for match, tmpl in templates:
if match in line:
return tmpl
return line
path = os.path.join(base, README)
with FileInput(path, inplace=True) as readme:
for line in readme:
sys.stdout.write(fmt_line(line))
sys.exit(call(["git", "add", README]))

Loading…
Cancel
Save