Skip to content

Instantly share code, notes, and snippets.

@claudioluciano
Created August 9, 2023 14:42
Show Gist options
  • Save claudioluciano/33e5af5f5fb5ff731f93752ed7cb7382 to your computer and use it in GitHub Desktop.
Save claudioluciano/33e5af5f5fb5ff731f93752ed7cb7382 to your computer and use it in GitHub Desktop.
List all tags and increment the version in a repo with tags for packages
# This code will get the latest tag for a package and increment it by one.
# The function get_latest_tags will get all the tags for a package
# The function get_latest_tag_for_package will get the latest tag for a package
# Then the latest tag will be incremented by one using awk and sed
#!/bin/bash
get_latest_tags() {
git for-each-ref --sort=-taggerdate --format '%(refname:short)' refs/tags/*/**
}
get_latest_tag_for_package() {
package_name="$1"
latest_tags="$2"
package_latest_tag=$(echo "$latest_tags" | grep "^$package_name/" | sort -V | tail -n 1)
echo "$package_latest_tag"
}
# Get all latest tags
all_latest_tags=$(get_latest_tags)
for d in */; do
cd $d
# remove the last / from the directory name
tag_name=${d%?}
package=$tag_name
latest_tag_for_package=$(get_latest_tag_for_package "$package" "$all_latest_tags")
if [ -n "$latest_tag_for_package" ]; then
echo "Latest tag for $package: $latest_tag_for_package"
else
echo "No tag found for $package"
fi
# increment the latest tag
new_tag=$(echo $latest_tag_for_package | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')
# show the new tag
echo "New tag for $package: $new_tag"
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment