#!/bin/bash

# TheBugGenie post-receive hook for git, for direct access (git and tbg on same machine)
# To use, this needs to be marked executable, and copied into your git repo,
# user@server:/path/to/repo/.git/hooks $
#   cp /tbg_install/modules/vcs_integration/hooks/git/tbg-post-receive post-receive
#   chmod +x ./post-receive

#>>>>> User config

# projectid comes from Project Settings -> VCS Integration
projectid=1

# this is the path to the installed thebuggenie.
tbg_cli_path=/home/...

#<<<<< End of user config

update_tbg()
{
    oldhash=$1
    newhash=$2
    refname=$3

    # Not working? uncomment the echos and see what's not arriving properly
    #echo "Attempting to update tbg with oldhash: $oldhash newhash: $newhash refname: $refname"

    # Ignore tag pushes
    if [[ "$refname" == "refs/tags/"* ]]; then
        return
    # If no previous hash is defined then we assume this is the first commit, so process all previous commits
    elif [[ "$oldhash" == "0000000000000000000000000000000000000000" ]]; then
        commitlist=`git rev-list ${newhash}`
    else
        commitlist=`git rev-list ${oldhash}..${newhash}`
    fi

    # Loop through all commits
    echo "$commitlist" | while read -r commithash
    do
        #echo "Update tbg with commit: $commithash"

        # Retrieve commit specific information.
        parrent=`git show --no-patch --format="%P" $commithash`
        name=`git show --no-patch --format="%an <%ae>" $commithash`
        log=`git show --no-patch --format="%s %b" $commithash`
        time=`git show --no-patch --format="%ct" $commithash`
        changedfiles=`git diff-tree --name-status -r $commithash --no-commit-id`

        #echo "updating with name: $name"
        #echo "updating with log: $log"
        #echo "updating with time: $time"
        #echo "updating files: $changedfiles"

        # Report commit to TBG
        cd $tbg_cli_path
        ./tbg_cli vcs_integration:report_commit $projectid "$name" $commithash "$log" "$changedfiles" $parrent $time $refname
        cd - > /dev/null # back to git repo folder
    done
}

if [ -n "$1" -a -n "$2" ]; then
    # we seem to be in command line mode...
    update_tbg $1 $2
else
    while read oldhash newhash refname
    do
        # we seem to be operating as a git post-receive hook
        update_tbg $oldhash $newhash $refname
    done
fi


