Created
December 16, 2022 22:12
-
-
Save amedeedaboville/64a2bc1e9dcec2f7f7d088450b5bbee2 to your computer and use it in GitHub Desktop.
Repotracer: compute stats command over the last commit of every day in a repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eou pipefail | |
# A script that runs a command on every day of the last year over the commmit | |
# history of a git repository. The command is passed as the first argument to | |
# the script. The script will run the command in the directory of the commit | |
# that is being processed. The script will also pass the date of the commit as | |
# the second argument to the command. | |
function usage { | |
echo "Usage: $0 <command>" | |
echo "Runs a command on every day of the last year over the commit history" | |
echo "of a git repository. The command is passed as the first argument to" | |
echo "the script. The script will run the command in the directory of the" | |
echo "commit that is being processed. The script will also pass the date of" | |
echo "the commit as the second argument to the command." | |
} | |
if [ $# -ne 1 ]; then | |
usage | |
exit 1 | |
fi | |
outfile=`pwd`/output.txt | |
echo $outfile | |
##Either use the date command or gdate command depending on the OS | |
if date -v -1d > /dev/null 2>&1; then | |
dateDelta() { date -v -"$1d" +%F; } | |
else | |
dateDelta() { date --date="$1 day ago"; } | |
fi | |
cmd=$1 | |
oldcommit="" | |
commit="" | |
runCommandOnDate() { | |
date=$1 | |
# Get the commit hash for the day | |
oldcommit=$commit | |
commit=$(git rev-list -n 1 --before="$date" master) | |
# if the commit is the same as the previous day, reuse the output | |
if [ "$commit" == "$oldcommit" ]; then | |
output=$(echo $raw_output | jq -c --arg date $date '{date: $date} + .') | |
continue | |
fi | |
# Checkout the commit, redirecting stderr to /dev/null to avoid printing | |
git checkout $commit > /dev/null 2>&1 | |
raw_output=$(eval $cmd) | |
output=$(echo $raw_output | jq -c --arg date $date '{date: $date} + .') | |
} | |
for i in {365..0} | |
do | |
echo -ne "$i days remaining\r" | |
runCommandOnDate $(dateDelta $i) | |
echo $output >> "$outfile" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment