Identifying the Earliest Tag Containing A Commit Using the Git CLI
I'm reviewing some security reports for Laminas, and needed to identify the earliest branch to patch. The GitHub UI has become so incredibly slow, and requires so many different clicks to get at the information that I decided to see if I could do it from the command line. As such, I learned a couple of git commands I'd not used previously.
We pin to explicit PHP minor versions in our composer.json files. As such, I needed to determine when the line with the PHP dependency was changed such that it's minimum PHP version matches the current earliest supported PHP version (8.2 at the time of writing).
I identified the line number using grep and assigned it to a variable:
php_constraint_lineno=$(grep -E -n '"php":\s*".*?\|' composer.json | cut -d':' -f 1)
Once I had the line number, I could review the git history. The "-L" flag to git log allows you to provide a line range and a file:
git log -L "${php_constraint_lineno},${php_constraint_lineno}:composer.json"
This opens up a pager, and from there I can see the various changes to that line, along with commit hash, author, date, message, and diff.
The final piece was determining the first tag that has that commit:
git describe --contains <commit>
This is so much easier and faster!