Skip to content

Instantly share code, notes, and snippets.

@benjamine
Last active August 1, 2017 17:37
Show Gist options
  • Save benjamine/8348022 to your computer and use it in GitHub Desktop.
Save benjamine/8348022 to your computer and use it in GitHub Desktop.
move a git tag remotely using github API (avoiding any clone or fetch)
#!/bin/sh
#
# moves a tag in a github repo
#
# Usage: github-tag <tag-name> <ref> <apirepourl>
#
# Examples:
# github-tag env-staging heads/release https://usertoken@api.github.com/repos/user/reponame
# github-tag env-production tags/env-staging https://usertoken@api.github.com/repos/user/reponame
if [ $# -lt 3 ]
then
echo "usage: github-tag <tag-name> <ref> <apirepourl>
examples:
github-tag env-staging heads/release https://usertoken@api.github.com/repos/user/reponame
github-tag env-production tags/env-staging https://usertoken@api.github.com/repos/user/reponame"
exit 1
fi
echo "moving tag $1"
current_sha=$(curl -fs $3/git/refs/tags/$1 | awk '/sha/ { print $2 }' | sed s/[^a-z0-9]//g)
if [ $? -ne 0 ]
then
echo ' error getting tag $1'
exit $?
fi
echo " from $current_sha"
sha=$(curl -fs $3/git/refs/$2 | awk '/sha/ { print $2 }' | sed s/[^a-z0-9]//g)
if [ $? -ne 0 ]
then
echo ' error getting ref $2'
exit $?
fi
echo " to $sha"
curl -fs -X PATCH -H "Content-Type: application/json" -d '{"sha":"'$sha'","force":"true"}' $3/git/refs/tags/$1
if [ $? -ne 0 ]
then
echo ' error moving tag'
exit $?
else
echo ' done!'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment