Skip to content

Instantly share code, notes, and snippets.

@MatthewScholefield
Last active August 20, 2018 19:04
Show Gist options
  • Save MatthewScholefield/b00ef821185a9eb1a35297a764a8616f to your computer and use it in GitHub Desktop.
Save MatthewScholefield/b00ef821185a9eb1a35297a764a8616f to your computer and use it in GitHub Desktop.
Generate git tags from version in setup.py over git history. Tags: PyPI, Bash, Git Tag
#!/usr/bin/env bash
n=0
while git cat-file -t HEAD~$n &>/dev/null; do
ver="$(git show "HEAD~${n}:setup.py" | grep -oP "(?<=version=\')[0-9\.]+(?=\')")"
[ -z "$ver" ] || git tag -fa "_generated-v$ver" -m "v$ver" "HEAD~$n"; n=$((n+1))
done
for i in $(git tag | grep -F '_generated-'); do
tag_name=${i/_generated-/}
correct_sha=$(git rev-list -1 "$i")
if [ "$correct_sha" != "$(git rev-list -1 "$tag_name" 2>/dev/null)" ]; then
git tag -fa "$tag_name" -m "$tag_name" "$correct_sha"
fi
git tag -d "_generated-$tag_name"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment