#!/bin/bash

show_usage () {
  echo "Usage: `basename $0` [START [END]]"
  echo
  echo "Steps through the commit history from START to END,"
  echo "then returns to the branch or commit from before execution."
  echo
  echo "START defaults to the root commit (beginning of history)."
  echo "END defaults to current branch/commit."
}

initial_branch=$(git symbolic-ref --short -q HEAD)
initial_commit=$(git rev-parse HEAD)
reset_to=${initial_branch:-${initial_commit:-"master"}}

if [[ ( $1 == "--help" ) || $1 == "-h" ]]; then
  show_usage
  exit 0
fi

if [ $# -eq 0 ]; then
  end=$reset_to
elif [ $# -eq 1 ]; then
  start="^$1^"
  end=$reset_to
elif [ $# -eq 2 ]; then
  start="^$1^"
  end=$2
else
  show_usage
  exit 1
fi

for commit in $(git rev-list $end $start --reverse); do
  git checkout $commit
  read
done

git checkout $reset_to