#!/bin/sh

# Check for git and a git repo.
if ! git rev-parse --show-cdup > /dev/null
then
    return
fi

if [ -n "$1" ]
then

    for i in "$@"
    do

	dirty=""
	if [ -n "`git diff-index --name-only HEAD "$i" 2>/dev/null`" ]
	then
	    dirty="-dirty"
	    #echo dirty
	fi
	commit=`git log -n1 --format="%h" "$i"`

	if atag="`git describe --long $commit 2>/dev/null`"
	then
	    commit="$atag"
	    #echo $atag
	fi
	    

	datum=`TZ=GMT git log -n1 --date=format-local:'%F %T' --format="%cd" "$i"`
	echo $datum $commit$dirty "$i"
    done

else

    head=`git rev-parse --verify --short HEAD 2>/dev/null`
    #echo $head

    dirty=""
    if [ -n "`git diff-index --name-only HEAD 2>/dev/null`" ]
    then
	dirty="-dirty"
	#echo dirty
    fi

    commit=$head
    datum=`TZ=GMT git log -n1 --date=format-local:'%F %T' --format="%cd"`
    exact=""

    # does the git repo. have tags ?
    if atag="`git describe --long 2>/dev/null`"
    then

	# If we are at a tagged commit (like "v2.6.30-rc6")
	if etag="`git describe --exact-match --long 2>/dev/null`"
	then
	    atag="$etag"
	    exact=-e
	fi

	commit="$atag"
    fi

    echo $datum "$commit"$dirty

fi



