Created
January 29, 2016 01:05
-
-
Save jzempel/9d9a41dc8df6a8636e01 to your computer and use it in GitHub Desktop.
Custom git command for tagging in sync with package versions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
pkgtag | |
~~~~~~ | |
Execute git-tag based on package versions. | |
""" | |
from __future__ import print_function | |
from argparse import ArgumentParser | |
from json import load | |
from os.path import basename, isfile, splitext | |
from subprocess import CalledProcessError, call, check_output | |
from sys import argv | |
PACKAGES = ["bower.json", "component.json", "package.json"] | |
def get_arguments(): | |
"""Get command-line arguments. | |
""" | |
prog = basename(argv[0]) | |
parser = ArgumentParser(prog=prog) | |
parser.add_argument("version", metavar="<version>", nargs='?') | |
return parser.parse_args() | |
def get_manager(package): | |
"""Get the manager name for the given package. | |
:param package: Package file name. | |
""" | |
if package == "package.json": | |
ret_val = "npm" | |
else: | |
ret_val = splitext(package)[0] | |
return ret_val | |
def get_version(package): | |
"""Get the version for the given package. | |
:param package: Package file name. | |
""" | |
ret_val = None | |
if isfile(package): | |
with open(package) as file: | |
json = load(file) | |
ret_val = json.get("version") | |
return ret_val | |
def tag_read(): | |
"""Read tag information. | |
""" | |
try: | |
tag = check_output(["git", "describe", "--tags", "--abbrev=0"]).strip() | |
except: | |
tag = '<empty>' | |
print("git@{}".format(tag)) | |
for package in PACKAGES: | |
version = get_version(package) | |
if version: | |
manager = get_manager(package) | |
if version == tag: | |
print("{}@{}".format(manager, version)) | |
else: | |
print("{}@{} (version mismatch)".format(manager, version)) | |
def tag_write(tag): | |
"""Write tag version. | |
:param tag: Tag version. | |
""" | |
status = check_output(["git", "status", "--porcelain"]) | |
if status == '': | |
for package in PACKAGES: | |
version = get_version(package) | |
if version and (version != tag): | |
manager = get_manager(package) | |
print("error: version mismatch {}@{}".format(manager, version)) | |
break | |
else: | |
call(["git", "tag", tag]) | |
else: | |
print("error: uncommitted changes") | |
if __name__ == "__main__": | |
args = get_arguments() | |
try: | |
if args.version: | |
tag_write(args.version) | |
else: | |
tag_read() | |
except CalledProcessError: | |
pass |
@alanhogan, I think there are a few ways you could go. But I use git config --global alias.pkgtag '!/usr/bin/pkgtag.py'
(see my dotfile). You might need to chmod +x pkgtag.py
. From there, you can use git pkgtag
to tag in-sync with your package version.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @jzempel, what’s the suggested usage / installation here?