Skip to content

Instantly share code, notes, and snippets.

@lbland94
Last active November 16, 2018 23:05
Show Gist options
  • Select an option

  • Save lbland94/671ecee07e6f402d05d0cddf73094721 to your computer and use it in GitHub Desktop.

Select an option

Save lbland94/671ecee07e6f402d05d0cddf73094721 to your computer and use it in GitHub Desktop.
Program to add git tags based on changes to npm package.json version
#!/bin/bash
pattern=:V:
test=0
confirm=0
function print_help() {
echo ""
echo "Usage: git-tag-npm-version <arguments>"
echo ""
echo "-p <pattern>"
echo " :V: will be replaced by the version in the package.json"
echo ""
echo "example:"
echo ""
echo "$ git-tag-npm-version -p v:V:"
echo "> v1.0.0"
echo "> v1.0.1"
echo ""
echo "-y"
echo " Automatically confirms all branch names"
echo ""
echo "-t"
echo " Test mode; prints all tags without tagging"
echo ""
exit 0
}
while getopts ":p:hty" opt; do
case $opt in
p)
pattern=$OPTARG
if [[ !( $pattern =~ \:V\: ) ]]; then
echo "Pattern must contain :V:" >&2
exit
fi
;;
t)
test=1
;;
y)
confirm=1
;;
h)
print_help
;;
\?)
echo "bad argument" >&2
exit
;;
esac
done
{ git log -L3,3:package.json | grep -E '^commit|^\+ ' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n+/ /g' | tr -d '",' | tr -s ' ' | cut -f2,4 -d' ' | awk '$2!=""' | while read line; do
commit=$(echo $line | cut -f1 -d' ')
version=$(echo $line | cut -f2 -d' ')
version=$(echo "$pattern" | sed "s/:V:/$version/g")
if (( $test == 0 )); then
if (( $confirm == 0 )); then
read -u 3 -r -p "Are you sure you want to add tag $version? [c/y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
git tag $version $commit
;;
[cC])
exit
;;
*)
echo "version $version not tagged"
;;
esac
elif (( $confirm == 1 )); then
git tag $version $commit
fi
elif (( $test == 1 )); then
echo "$version"
fi
done } 3<&0
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment