Skip to content

Instantly share code, notes, and snippets.

@nickebbutt
Created September 29, 2021 11:25
Show Gist options
  • Save nickebbutt/72337c7e7bd3988eec8cf6fc816a726d to your computer and use it in GitHub Desktop.
Save nickebbutt/72337c7e7bd3988eec8cf6fc816a726d to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ "$#" -ne 2 ] ; then
echo "Usage: `basename -- $0` date tagFilter"
echo "eg: `basename -- $0` 20201225 TAG_SUBSTRING_TO_MATCH"
exit 1
fi
DATE=$1
SEARCH=$2
YEAR=${DATE:0:4}
MONTH=${DATE:4:2}
DAYS=${DATE:6:2}
git for-each-ref --sort=creatordate --format '%(refname) CreateDate=%(creatordate)' refs/tags |
grep ${SEARCH} |
awk -F'CreateDate=' -v YEAR=${YEAR} -v MONTH=${MONTH} -v DAYS=${DAYS} '
BEGIN{
m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|")
for(o=1;o<=m;o++){
months[d[o]]=sprintf("%02d",o)
}
before = mktime(YEAR " " MONTH " " DAYS " 00 00 00")
}
{
split($2,time," ")
date = (time[5] " " months[time[2]] " " time[3] " 00 00 00 ")
timestamp = mktime(date)
if ( timestamp < before ) {
split($1,tagElements,"/")
print tagElements[3]
}
}
' | tail -1l
@nickebbutt
Copy link
Author

Find the last tag in a git repo which precedes a date and matches a search string
Needs to be run in the same directory as the repo (or amend to take git repo dir as an extra argument)

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