Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env bash
- # Author: Thomas DEBESSE <dev@illwieckz.net>
- # License: MIT
- set -e
- set -u
- delta='false'
- case "${1:-}" in
- '-d'|'--delta')
- delta='true'
- shift
- ;;
- esac
- ref="${1:-HEAD}"
- version="$(git describe --tags --match 'v[0-9]*' "${ref}" 2>/dev/null || true)"
- if [ -n "${version}" ]
- then
- if "${delta}"
- then
- tag="$(git describe --tags --abbrev=0 --match 'v[0-9]*')"
- if [ "${tag}" = "${version}" ]
- then
- version="$(git tag --contains "${ref}" 2>/dev/null | egrep '^v[0-9].*' | head -n1)"
- fi
- fi
- else
- echo 'warning: no tag found' >&2
- tag='0'
- count="$(git rev-list --count "${ref}" 2>/dev/null || true)"
- count="${count:-0}"
- id="$(git rev-list --abbrev-commit --abbrev=9 -n1 "${ref}" 2>/dev/null || true)"
- if [ -n "${id}" ]
- then
- id="g${id}"
- else
- echo 'warning: no commit found' >&2
- id="$(date --utc '+%Y%m%d-%H%M%S')"
- fi
- version="${tag}-${count}-${id}"
- fi
- if ! git status >/dev/null 2>&1
- then
- if [ "${ref}" != 'HEAD' ]
- then
- echo 'error: not a git repository' >&2
- exit 1
- fi
- echo 'warning: not a git repository' >&2
- version+='0'
- elif ! git diff --quiet 2>/dev/null
- then
- echo 'warning: uncommited changes' >&2
- version+='-dirty'
- elif ! git ls-files --other --directory --exclude-standard 2>/dev/null | sed q1 >/dev/null
- then
- echo 'warning: untracked files' >&2
- version+='-dirty'
- fi
- echo "${version}"
Add Comment
Please, Sign In to add comment