Thanks to my sweet little sibling Kitty, my post “Comment vérifier l’existence d’une branche ou d’un tag sur un repository distant sans le cloner” is now translated in english! All kudos to them!
When writing scripts to automate packaging and deployment of applications, we might need to make sure that a tag or branch exists before needlessly cloning a huge repository.
It turns out there is a git command to do just that. It’s
ls-remote. It’s aptly named as it lists (
ls) named references (branches, tags…) from a repository (
Let’s take a repository with a lot of branches and tags as an example: https://github.com/facebook/react.
ls-remote, I’ll get this:
Great. However it’s a wee bit too much since it also outputs pull-requests. To filter out everything but branches, we can use the
--heads argument. For tags, it’s, well,
Now, if we had a script or a CI job taking a branch or tag to deploy, it would be as simple as doing a
ls-remote output to make sure the input parameter exists:
In a bash script, we could then check the result of the last command to check whether the given version exists. Then, we could either clone the repository or throw an error.
Here is how it would look:
Now let’s use it with an unknown tag, then a correct one:
That being said, let’s remember that git checks that the branch exists before cloning since version 1.7.0.
But still, pretty nice.