Skip to content

Instantly share code, notes, and snippets.

@kallewoof
Created July 19, 2018 04:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kallewoof/10ce05193e738b42517b565a2f9b22e6 to your computer and use it in GitHub Desktop.
Save kallewoof/10ce05193e738b42517b565a2f9b22e6 to your computer and use it in GitHub Desktop.
Compile commits in a range from current head
#!/bin/bash
if [ $# -lt 1 ]; then
echo "syntax: $0 <commit count> [<make args>]"
echo "will attempt to run 'make' on each commit starting with the commit <commit count> commits ago all the way up to the current HEAD"
echo "e.g. compile-commits 10 -j10"
exit 1
fi
if [ ! -d ".git" ]; then
echo ".git not found or not a dir"
exit 2
fi
commit_count=$1
shift
# determine current branch
head=$(cat .git/HEAD)
branch=${head:16}
if [ "$branch" = "" ]; then
echo "could not determine branch from $head (.git/HEAD)"
exit 3
fi
# iterate commits - this iteration is in the opposite order (newest to oldest) of what we want
ordered_commits=""
for i in $(git log -$commit_count|grep "^commit [a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]$"); do
if [ "$i" = "commit" ]; then continue; fi
commit=$i
ordered_commits="$commit $ordered_commits"
done
report=""
rebase_head=$commit_count
let rebase_head++ # if we rebase the first commit, we need to do HEAD~(arg + 1)
left=""
done=""
for i in $(seq 1 $commit_count); do
left="$left "
clr="$clr "
done
clr="$clr \r"
clean_up=1
function trap_ctrlc()
{
if [ $clean_up ]; then
echo -n -e "\r$clr""Aborting..."
git checkout $branch &>/dev/null
echo ""
exit 2
fi
}
trap "trap_ctrlc" INT
for i in $ordered_commits; do
git checkout $i &> .gco-output
if [ $? -ne 0 ]; then echo "git checkout $i failed:"; echo "====="; cat .gco-output; rm .gco-output; git checkout $branch; exit 10; fi
rm .gco-output
echo -n -e "$i"
git show HEAD | head -n5 | tail -n1
echo -n -e "[$done$left]\r"
make "$@" &> .make-output
makeres=$?
echo -n -e "$clr"
if [ $makeres -ne 0 ]; then
prevreport=$report
report="$report$i: make failure\n"
echo "make FAILURE"
echo "====="
cat .make-output
rm .make-output
while [ 1 ]; do
echo -n -e "rebase (r), ignore (i), or return to $branch (b): "
read x
if [ "$x" = "r" ]; then
git checkout $branch
git rebase -i HEAD~$rebase_head
exit 12
elif [ "$x" = "i" ]; then
break
elif [ "$x" = "b" ]; then
git checkout $branch
if [ "$prevreport" != "" ]; then
echo -n -e "Results:\n$report"
fi
exit 11
fi
done
fi
rm .make-output
left=${left:1}
done="#$done"
done
git checkout $branch &> .gco-output
if [ $? -ne 0 ]; then echo "git checkout $i failed:"; echo "====="; cat .gco-output; rm .gco-output; exit 10; fi
rm .gco-output
if [ "$report" = "" ]; then
echo "All OK"
else
echo -n -e "Results:\n$report"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment