Two objects behind one name#
An annotated tag is an object of its own, holding the tagger, date, message and an optional signature; a lightweight tag is only a name for an object, usually a commit. Git's documentation says annotated tags "are meant for release". git tag So a release tag usually has its own ID, and any comparison with a commit ID needs one more step.
This fixture creates a commit, an annotated tag and a lightweight tag on it, and pushes them (Git 2.43.0):
$ git rev-parse HEAD
1acc584ce30d3f0579c1a1d9a4f6a675f470109d
$ git rev-parse v1.2.0
474e08949dfd39983d3a8b3b66e6c321d2f82f13
$ git cat-file -t v1.2.0
tag
$ git rev-parse v1.2.0^{commit}
1acc584ce30d3f0579c1a1d9a4f6a675f470109d
$ git rev-parse v1.2.0-light
1acc584ce30d3f0579c1a1d9a4f6a675f470109d
$ git ls-remote --tags origin
474e08949dfd39983d3a8b3b66e6c321d2f82f13 refs/tags/v1.2.0
1acc584ce30d3f0579c1a1d9a4f6a675f470109d refs/tags/v1.2.0^{}
1acc584ce30d3f0579c1a1d9a4f6a675f470109d refs/tags/v1.2.0-light
A release check that compares git rev-parse v1.2.0 with the tested SHA reports a mismatch on a correct release. <rev>^{<type>} dereferences the object recursively "until an object of type ^{} peels until it reaches an object that is not a tag. gitrevisions ls-remote shows a second line, with ^{} appended, whose ID is "the name of the object the tag points at". git ls-remote
The same on GitHub#
GitHub's refs API returns the object the ref names, which for an annotated tag is the tag object, and the tags endpoint reads that object. Git references API ยท Git tags API Against the public Git repository, the commits endpoint returned the peeled commit in one call:
$ gh api repos/git/git/git/ref/tags/v2.43.0 --jq .object
{"sha":"c089584ac8dedc3aa7c2c404839bc098050298a2","type":"tag","url":"https://api.github.com/repos/git/git/git/tags/c089584ac8dedc3aa7c2c404839bc098050298a2"}
$ gh api repos/git/git/git/tags/c089584ac8dedc3aa7c2c404839bc098050298a2 --jq .object
{"sha":"564d0252ca632e0264ed670534a51d18a689ef5d","type":"commit","url":"https://api.github.com/repos/git/git/git/commits/564d0252ca632e0264ed670534a51d18a689ef5d"}
$ gh api repos/git/git/commits/v2.43.0 --jq .sha
564d0252ca632e0264ed670534a51d18a689ef5d
"type":"tag" is the signal. A script that reads .object.sha without checking the type takes a tag object for a commit.
Decide what a mismatch means#
| After peeling | Meaning | Next step |
|---|---|---|
| Peeled commit equals the tested SHA | The tag names the tested commit | None |
| Peeled commit differs | The tag is on another commit | Find who created or moved the tag, and compare trees |
git cat-file -t says commit | A lightweight tag; no peeling needed | Compare directly |
A peeled match covers the commit's source only. The artifact built from it is a separate question, answered by its digest or checksum.
Worked example#
This example is fictional. Ben's deploy gate compares the tag with the commit whose checks passed and blocks release 3.2.0 for "tag points to an untested commit". git cat-file -t v3.2.0 prints tag, and git rev-parse 'v3.2.0^{commit}' returns the tested SHA. The gate had compared a tag object with a commit since the team switched to annotated tags. Ben changes the gate to fetch with --prune-tags, so a tag deleted on the remote is deleted locally too, peel refs/tags/v3.2.0, and adds a test with an annotated tag. A local peel reads the local tag, so the gate fetches first: a tag moved on the remote after the last fetch would otherwise pass.
To keep the tag from moving after the check, read protect release tags. For how a release creates its tag, read GitHub release target commit.