Skip to content

Instantly share code, notes, and snippets.

@hayyp
Last active October 30, 2020 13:55
Show Gist options
  • Save hayyp/d365af1b3a16a2a72b3e8a3cba2f10cf to your computer and use it in GitHub Desktop.
Save hayyp/d365af1b3a16a2a72b3e8a3cba2f10cf to your computer and use it in GitHub Desktop.
Read git commits
#!/usr/bin/env bash
# Consider creating a soft link for the script under /usr/local/bin
# ln -s ~/path/to/script /usr/local/bin/gitreader
# And perhaps give it a better name
if [[ -z $1 || $1 == "help" ]]
then
echo "
gitreader first to checkout the first commit
gitreader next to checkout the next commit
gitreader last to checkout the last commit
gitreader commit NUM to checkout commit #NUM
gitreader progress to view current progress
gitreader calibrate to set progress to the current commit
use it after a git checkout
gitreader previously to jump back to the position bookmarked by gitreader
use it after interrupted by git commands like git checkout
gitreader reset to reset gitreader for the current repository
use it when you want to start afresh
Enjoy!
"
else
# get the name of this git repo
LOC="$(git rev-parse --show-toplevel)"
if [[ $? -ne 0 ]]; then exit; fi
NAME="$(basename $LOC)"
DIR="${HOME}/.config/gitreader/${NAME}"
HST="${DIR}/hst.txt" # entire history
PRG="${DIR}/prg.txt" # progress
if [ ! -e "$HST" ]
then
mkdir -p "$DIR"
touch "$HST"
git log --all --reverse --pretty=%H > $HST
fi
if [ ! -e "$PRG" ]
then
git rev-list HEAD --count > $PRG
fi
CNT=$(wc -l $HST | awk '{print $1}') # number of commits
if [[ $1 == "first" ]]
then
echo 1 > $PRG
head -1 $HST | xargs git checkout
echo "Currently commit #$(cat $PRG)"
fi
if [[ $1 == "next" ]]
then
CUR="$(cat $PRG)"
NEXT=$(($CUR + 1))
if [[ $NEXT -gt $CNT ]]
then
echo "Already the latest commit"
exit
fi
echo $NEXT > $PRG
awk "NR==${NEXT}" $HST | xargs git checkout
echo "Currently commit #$(cat $PRG)"
fi
if [[ $1 == "last" ]]
then
CUR="$(cat $PRG)"
LAST=$(($CUR - 1))
if [[ $LAST -lt 1 ]]
then
echo "Already the first commit!"
exit
fi
echo $LAST > $PRG
awk "NR==${LAST}" $HST | xargs git checkout
echo "Currently commit #$(cat $PRG)"
fi
if [[ $1 == "progress" ]]
then
echo "Currently $(cat $PRG)/$CNT"
fi
if [[ $1 == "calibrate" ]]
then
git rev-list HEAD --count > $PRG
fi
if [[ $1 == "previously" ]]
then
CUR="$(cat $PRG)"
awk "NR==${CUR}" $HST | xargs git checkout
echo "Currently commit #$(cat $PRG)"
fi
if [[ $1 == "commit" ]]
then
if [[ -z $2 ]]
then
echo "Usage: gitreader commit NUM"
exit
fi
awk "NR==$2" $HST | xargs git checkout
echo $2 > $PRG
echo "Currently commit #$(cat $PRG)"
fi
if [[ $1 == "reset" ]]
then
rm $HST $PRG
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment