#!/usr/bin/bash function help() { cat </dev/null # If the commit was successful, tag it (overwriting any previous tag) if [[ $? == 0 ]]; then status="*" git tag -f "git-jump-$head_sha" >/dev/null fi echo "Previous HEAD was $head_sha$status, $(git_commit_subject $head_sha)" } # Restore previously-saved changes function restore() { local status="" # Save current changes before restoring save # Attempt to restore saved changes for specified commit git checkout "git-jump-$1" 2>/dev/null if [[ $? == 0 ]]; then # If the restore was successful, figure out exactly what was saved, check # out the original commit, then restore the saved changes on top of it status="*" local patch="$(git format-patch HEAD^ --stdout)" git checkout HEAD^ 2>/dev/null echo "$patch" | git apply - else # Otherwise, just restore the original commit git checkout "$1" 2>/dev/null fi echo "HEAD is now $1$status, $(git_commit_subject $1)" } # Jump to next commit function next() { local next_sha=$(git_next_sha) if [[ "$next_sha" == "$(git_head_sha)" ]]; then # Abort if no more commits echo "Already at last commit in $git_branch. Congratulations!" else # Checkout branch by name if at its HEAD if [[ "$next_sha" == "$(git_branch_sha)" ]]; then next_sha="$git_branch" fi echo "Jumping ahead to next commit." restore $next_sha fi } # Jump to previous commit function prev() { local prev_sha=$(git_prev_sha) if [[ "$prev_sha" == "$(git_head_sha)" ]]; then # Abort if no more commits echo "Already at first commit in $git_branch." else echo "Jumping back to previous commit." restore $prev_sha fi } # Show help if requested if [[ "$1" == "--help" || "$1" == "-h" ]]; then help exit fi # Check if branch is valid git rev-parse "$git_branch" &>/dev/null if [[ $? != 0 ]]; then echo "Error: Branch \"$git_branch\" does not appear to be valid." echo "Try $(basename "$0") --help for more information." exit 1 fi # Handle CLI arguments if [[ "$1" == "next" ]]; then next elif [[ "$1" == "prev" ]]; then prev else usage exit 1 fi