Skip to content

Instantly share code, notes, and snippets.

@b0o
Last active August 16, 2021 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0o/bd3d3428469f06a8547293a8d61c94ed to your computer and use it in GitHub Desktop.
Save b0o/bd3d3428469f06a8547293a8d61c94ed to your computer and use it in GitHub Desktop.
#compdef git-tag-version
# zsh completion for git-tag-version
_git-tag-version () {
_arguments -S -s "-h[usage]" "1:version-name" "2:from:__git_commits" "3:to:__git_commits"
}
#!/usr/bin/env bash
#
# git helper for creating version tags with the message body containing the log of
# commits since the last tag
#
# (c) 2021 Maddison Hellstrom <github.com/b0o>, MIT License
set -eof pipefail
function main() {
if [[ $1 =~ (-h|--help) ]]; then
echo "usage: $(basename "$0") [version-name] [from] [to]" >&2
return 1
fi
git status > /dev/null
local v
v="${1:-$(git show --quiet --format=%s HEAD)}"
local from="${2:+^$2}"
from="${from#\^-}"
from="${from#\^\=}"
from="${from:-$(
cat \
<(git rev-list --max-parents=0 HEAD | head -1) \
<(git tag --list --sort=v:refname --format="%(object)" | xargs --no-run-if-empty printf '^%s\n') \
| tail -1
)}"
local to="${3:-}"
to="${to#-}"
to="${to:-HEAD}"
if [[ -n $(git tag --list --points-at="$to") ]]; then
echo "error: already a tag: $to"
return 1
fi
if git merge-base --is-ancestor "$to" "${from/\^/}"; then
echo "error: $to is not an ancestor of $from"
return 1
fi
local msg
msg="$(git log --format="%cs %h %s" "${from}" "${to}^")"
local comment
mapfile -t comment << EOF
version: $v
from: $from
to: $to ($(git log --format=format:%s --max-count=1 "${to/\^/}"))
$(git log --format=short --decorate "$from" "$to" | paste -d' ' - <(echo "<--- TARGET ($v)"))
--
$(git tag --points-at="${from/\^/}" | head -1 | xargs --no-run-if-empty git log --max-count=1 --format=short --decorate)
EOF
git tag --edit -sm "$v" -m "$msg$(echo; echo; printf '%s\n' "${comment[@]}" | sed 's/^/# /; s/\s*$//')" "$v" "$to"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment