I have a shallow Git repository that was created as follows:
mkdir repo
cd repo
git init
git remote add origin $URL
git fetch --depth 1 origin $SHA
As part of my build process, I would like to use git describe --tags to describe the revision relative to its closest ancestor tag. Since I just fetched the specific revision I needed, it can't do this, because it doesn't know about any of my commit's ancestors.
So, I had a thought to write a simple bash script to deepen the history as needed:
GIT_HEAD=$(git rev-parse HEAD)
until git describe --tags
do
git fetch --deepen 100 origin $GIT_HEAD
done
This doesn't work, because, as the documentation for git-fetch says:
--depth= Limit fetching to the specified number of commits from the tip of each remote branch history. If fetching to a shallow repository created by git clone with --depth= option (see git- clone(1)), deepen or shorten the history to the specified number of commits. Tags for the deepened commits are not fetched.
I then tried to use git fetch --tags to fetch the list of tags, and that works, but it also fetches the commit data for each tag. The repository that I'm working with has a large history and lots of tags, and this causes a lot of disk/network/time usage (that's why I'm using a shallow clone to begin with!).
Is there a way to make Git fetch just the SHAs for the tags, so I can match them against my repository's revision list when trying to deepen the history? Alternatively, is there a way I can do a shallow fetch of the repository's history while also getting the tags associated with that depth?