Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Gary Bernhardt December 08, 2013
executable file 86 lines (73 sloc) 1.812 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#!/bin/bash
#
# This script runs a given command over a range of Git revisions. Note that it
# will check past revisions out! Exercise caution if there are important
# untracked files in your working tree.
#
# This came from Gary Bernhardt's dotfiles:
# https://github.com/garybernhardt/dotfiles
#
# Example usage:
# $ run-command-on-git-revisions origin/master master 'python runtests.py'

set -e

if [[ $1 == -v ]]; then
verbose=1
    shift
fi

start_ref=$1
end_ref=$2
test_command=$3

main() {
    abort_if_dirty_repo
    enforce_usage
    run_tests
}

abort_if_dirty_repo() {
    set +e
    git diff-index --quiet --cached HEAD
    if [[ $? -ne 0 ]]; then
echo "You have staged but not committed changes that would be lost! Aborting."
        exit 1
    fi
git diff-files --quiet
    if [[ $? -ne 0 ]]; then
echo "You have unstaged changes that would be lost! Aborting."
        exit 1
    fi
untracked=$(git ls-files --exclude-standard --others)
    if [ -n "$untracked" ]; then
echo "You have untracked files that could be overwritten! Aborting."
        exit 1
    fi
set -e
}

enforce_usage() {
    if [ -z "$test_command" ]; then
usage
        exit $E_BADARGS
    fi
}

usage() {
    echo "usage: `basename $0` start_ref end_ref test_command"
}

run_tests() {
    revs=`log_command git rev-list --reverse ${start_ref}..${end_ref}`

    for rev in $revs; do
debug "Checking out: $(git log --oneline -1 $rev)"
        log_command git checkout --quiet $rev
        log_command $test_command
        log_command git reset --hard
    done
log_command git checkout --quiet $end_ref
    debug "OK for all revisions!"
}

log_command() {
    debug "=> $*"
    eval "$@"
}

debug() {
    if [ $verbose ]; then
echo $* >&2
    fi
}

main
Something went wrong with that request. Please try again.