Skip to content

Instantly share code, notes, and snippets.

@xslim
Created September 21, 2012 10:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xslim/3760898 to your computer and use it in GitHub Desktop.
Save xslim/3760898 to your computer and use it in GitHub Desktop.
git commit hook for Harvest integration

Installation

  • Install hcl https://github.com/zenhob/hcl
  • cd MyProject
  • curl -L https://gist.github.com/raw/3760898/commit-msg.sh > .git/hooks/commit-msg
  • chmod +x .git/hooks/commit-msg
  • hcl tasks
  • Note ProjectID & TaskID
  • git config hooks.harvesttask "ProjectID TaskID"
  • Example: git config hooks.harvesttask "2551232 1441578"
#!/bin/sh
#git rev-list -n 1 --timestamp HEAD -- Gemfile
COMMIT_MSG=`tail -n 1 $1`
stamp2date (){
#date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
date -j -u -f "%s" $1 "+%Y-%m-%d %T"
}
timestampDiff (){
case $1 in
-s) sec=1; shift;;
-m) sec=60; shift;;
-h) sec=3600; shift;;
-d) sec=86400; shift;;
*) sec=86400;;
esac
dte1=$1 #$(date2stamp $1)
dte2=$2
diffSec=$((dte2-dte1))
if ((diffSec < 0)); then abs=-1; else abs=1; fi
echo $((diffSec/sec*abs))
}
sec2hm()
{
local S=${1}
((h=S/3600))
((m=S%3600/60))
((s=S%60))
printf "%d:%d\n" $h $m
}
trackTime()
{
HARVEST_TASK=$(git config hooks.harvesttask)
test "" != "${HARVEST_TASK}" || {
echo >&2 "ERROR: Set harvest Project & Task IDs. Use hcl tasks to see.\nExample: git config hooks.harvesttask \"2551232 1441578\""
exit 1
}
#echo "${HARVEST_TASK} +$1 ${COMMIT_MSG}"
# Push info to Harvest
hcl start ${HARVEST_TASK} +$1 "${COMMIT_MSG}"
hcl stop
}
# This will work only for >1 files
test "$(git diff-index --name-only HEAD | wc -l)" -gt 1 && {
TIMES=`git diff-index --name-only HEAD | xargs stat -f "%m" | sort | sed -n -e '1p;$p' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'`
} || {
TIMES=`git diff-index --name-only HEAD | xargs stat -f "%B%n%m" | sort | sed -n -e '1p;$p' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g'`
}
IFS=' ' read -ra TIME <<< "$TIMES"
TIMESPENT_SEC=`timestampDiff -s $TIMES`
TIMESPENT=`sec2hm $TIMESPENT_SEC`
test $TIMESPENT_SEC -gt 0 && {
echo "Commit files in: `stamp2date ${TIME[0]}` - `stamp2date ${TIME[1]}`, Time spent: ${TIMESPENT} hours"
} || {
echo "No time to track :("
}
# Tests here
# Commit message test
# Any commit message must contain the jira version number, or be a merge commit
#test "" != "$(grep -E '\[ZTVIOS-[[:digit:]]+\] ' "$1")" || \
#test "" != "$(grep -E 'Merge branch' "$1")" || {
# echo >&2 "ERROR: Commit message must contain Jira issue number like: '[ZTVIOS-1234] '."
# exit 1
#}
exec < /dev/tty
read -t 100 -e -p "Enter time spent on task, hours: [${TIMESPENT}] " NEW_TIMESPENT
test "${NEW_TIMESPENT}" != "" && {
TIMESPENT=$NEW_TIMESPENT
TIMESPENT_SEC=1
echo "New time spent: ${TIMESPENT}"
}
test $TIMESPENT_SEC -gt 0 && {
# Time test
test $(timestampDiff -h $TIMES) -lt 9 && {
trackTime ${TIMESPENT}
} || {
echo >&2 "ERROR: Are you working on commit more than 8 hours?. Maybe commit more atomicly!"
test "" != "$(grep -E '#track' "$1")" && {
trackTime ${TIMESPENT}
} || {
exit 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment