Skip to content

Instantly share code, notes, and snippets.

@GrayedFox
Last active August 13, 2018 16:52
Show Gist options
  • Save GrayedFox/de2bf1dd13a124557f6433b7d6dabb62 to your computer and use it in GitHub Desktop.
Save GrayedFox/de2bf1dd13a124557f6433b7d6dabb62 to your computer and use it in GitHub Desktop.
Bash function to clone the latest tagged version of a repo
#!/usr/bin/env bash
LATEST_TAG="$(curl --silent https://api.github.com/repos/$1/$2/tags | grep -Po '"name": "\K.*?(?=")' | head -1)"
echo "Cloning $LATEST_TAG"
if [ "$3" = "-https" ]; then
git clone https://github.com/$1/$2.git --branch $LATEST_TAG
else
git clone git@github.com:$1/$2.git --branch $LATEST_TAG
fi
# Usage (defaults to cloning with SSH):
# git clone-latest asdf-vm asdf
# git clone-latest asdf-vm asdf https
# Notes:
# This will result in cloning the repository and detaching the head, as per the docs.
# The latest tag is not necessarily the latest release: this command simply takes the top most entry of the
# JSON response. If wanting to check the order and see all available tags do:
#
# curl --silent https://api.github.com/repos/$USER/$REPO/tags | grep -Po '"name": "\K.*?(?=")'
@GrayedFox
Copy link
Author

Note: this will result in cloning the repository and detaching the head, as per the docs.

@GrayedFox
Copy link
Author

GrayedFox commented Jul 17, 2018

Important: the latest tag is not necessarily the latest release!

This command simply takes the top most entry of the the JSON response. If wanting to check the order and see all available tags do:

curl --silent https://api.github.com/repos/$USER/$REPO/tags | grep -Po '"name": "\K.*?(?=")'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment