Skip to content
Hiroshi Kikuchi edited this page Oct 5, 2015 · 1 revision
#! /bin/zsh

set -e

EXPECTED_REMOTE_URL='https://[email protected]/mixigitchallenge/git-challenge-bad-ignore.git'
EXPECTED_REMOTE_NAME='badignore'
CSV_FILES='users1.csv users2.csv users3.csv'
ADDED_FILE='users4.csv'
EXPECTED_ADDED_FILE_BRANCH='origin/file_to_add'

function check_csv_in_history() {
    (git remote | grep $EXPECTED_REMOTE_NAME &> /dev/null) || git remote add $EXPECTED_REMOTE_NAME $EXPECTED_REMOTE_URL
    git fetch $EXPECTED_REMOTE_NAME

    for sha1_pair in `paste -d':' <(git rev-list --reverse ${EXPECTED_REMOTE_NAME}/master) <(git rev-list --reverse master)`; do
        EXPECTED_SHA1=`echo $sha1_pair | awk -F':' '{ print $1 }'`
        ACTUAL_SHA1=`echo $sha1_pair | awk -F':' '{ print $2 }'`

        for csv_file in `echo $CSV_FILES`; do
            EXPECTED_CHECK_SUM=get_checksum(`git show ${EXPECTED_SHA1}:$csv_file`)
            ACTUAL_CHECK_SUM=get_checksum(`git show ${ACTUAL_SHA1}:$csv_file`)
            [[ $EXPECTED_CHECK_SUM == $ACTUAL_CHECK_SUM ]]
        done
    done
}

function check_added_file() {
    EXPECTED_SHA1=`git rev-parse ${EXPECTED_ADDED_FILE_BRANCH}`
    ACTUAL_SHA1=`git rev-parse master`
    git show ${ACTUAL_SHA1}:$ADDED_FILE &> /dev/null
    if [ $? -eq 0 ]; then
        EXPECTED_CHECK_SUM=get_checksum(`git show ${EXPECTED_SHA1}:$ADDED_FILE`)
        ACTUAL_CHECK_SUM=get_checksum(`git show ${ACTUAL_SHA1}:$ADDED_FILE`)
        [[ $EXPECTED_CHECK_SUM == $ACTUAL_CHECK_SUM ]]
    else
        return 1
    fi
}

function get_checksum() {
    CONTENT=$1
    if [ "$(uname)" == 'Darwin' ]; then
        return md5 -q -s $CONTENT
    else
        return `echo $CONTENT | md5sum`
    fi
}

check_csv_in_history
check_added_file
Clone this wiki locally