Skip to content

Commit

Permalink
use ansemjo/version.sh for version strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ansemjo committed Oct 30, 2018
1 parent 1be23be commit 1693958
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
setup.py export-subst
makefile export-subst
version.sh export-subst
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
archive/
__pycache__/
build/
dist/
pkg/
*.egg-info/
*.apk
*.deb
*.rpm
9 changes: 4 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ $(DESTDIR)/usr/share/doc/imapfetch/README.md : README.md
FORMATS = rpm deb apk

# package version
VERSION := 0.2.0-$Format:%h$
VERSION := $(shell [[ $(VERSION) = *-ormat:* ]] && git describe --always || echo $(VERSION))
VERSION := $(shell sh version.sh describe | sed s/-/./ )

.PHONY: fpm
fpm : $(FORMATS)
Expand All @@ -46,6 +45,6 @@ $(FORMATS) : install

FPM_IMAGE := registry.rz.semjonov.de/docker/fpm-builder:latest

.PHONY: docker-fpm
docker-fpm :
docker run --rm -v $$PWD:/build $(FPM_IMAGE) make fpm
.PHONY: docker-%
docker-% :
docker run --rm -v $$PWD:/build $(FPM_IMAGE) make $*
24 changes: 7 additions & 17 deletions setup.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
#!/usr/bin/env python3
#!/usr/bin/env python

from subprocess import check_output
from shlex import split
cmd = lambda s: check_output(split(s)).strip().decode()

from os.path import isdir
from subprocess import check_output as cmd
from setuptools import setup

# package name
# package metadata
name = "imapfetch"

# package version and commit hash
version = "0.2.0"
commit = "$Format:%h$"

# try to get the most specific version possible
if "Format:" not in commit:
version = version + "-" + commit

if isdir(".git"):
version = cmd(["git", "describe", "--always", "--long", "--dirty"]).strip().decode()

# author information
version = cmd('sh ./version.sh describe')
author = "Anton Semjonov"
email = "anton@semjonov.de"
github = f"https://github.com/ansemjo/{name}"
Expand Down
49 changes: 49 additions & 0 deletions version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

# Copyright (c) 2018 Anton Semjonov
# Licensed under the MIT License

# add 'VERSION export-subst' in .gitattributes and
# these strings will be substituted by git-archive
COMMIT='$Format:%H$'
REFS='$Format:%D$'

# constants
FALLBACK_VERSION='commit'
FALLBACK_COMMIT='unknown'
REVISION='.r'

# check if variable contains a subst value or still has the format string
hasval() { expr match "$1" '$Format' == 0 >/dev/null; }

# parse the %D reflist to get tag or branch
refparse() { REF="$1";
tag=$(echo "$REF" | sed -ne 's/.*tag: \([^,]*\).*/\1/p'); test -n "$tag" && echo "$tag" && return 0;
branch=$(echo "$REF" | sed -e 's/HEAD -> //' -e 's/, /\
/' | sed -ne '/^[a-z0-9._-]*$/p' | sed -n '1p'); test -n "$branch" && echo "$branch" && return 0;
return 1; }

# git functions to return commit and version in repository
hasgit() { test -d .git; }
gitcommit() { hasgit && git describe --always --abbrev=0 --match '^$' --dirty; }
gitversion() { hasgit \
&& { V=$(git describe 2>/dev/null) && echo "$V" | sed "s/-\([0-9]*\)-g.*/$REVISION\1/"; } \
|| { C=$(git rev-list --count HEAD) && printf '0.0.0%s%s' "$REVISION" "$C"; };
}

# wrappers
version() { hasval "$REFS" && refparse "$REFS" || gitversion || echo "$FALLBACK_VERSION"; }
commit() { hasval "$COMMIT" && echo "$COMMIT" || gitcommit || echo "$FALLBACK_COMMIT"; }

# ---------------------------------

case "$1" in
version) version ;;
commit) commit ;;
describe)
printf '%s-g%.7s\n' "$(version)" "$(commit)" ;;
help)
printf '%s [version|commit|describe]\n' "$0" ;;
*)
printf 'version : %s\ncommit : %s\n' "$(version)" "$(commit)" ;;
esac

0 comments on commit 1693958

Please sign in to comment.