#!/bin/sh
# Exercise the --author='user name <email@addr.dom>' option.

if test "$VERBOSE" = yes; then
  set -x
  vc-dwim --version
fi

. $srcdir/trap-setup

require_git_

framework_failure=0
mkdir -p $tmp && cd $tmp \
  || framework_failure=1

cat <<EOF > ChangeLog || framework_failure=1
2006-09-04  Jim Meyering  <jim@meyering.net>
EOF
git init > /dev/null 2>&1		\
  && git add .				\
  && git commit -m m . > /dev/null 2>&1	\
    || framework_failure=1

touch x || framework_failure=1
git add x || framework_failure=1
cat <<EOF >> ChangeLog || framework_failure=1

	* x: whatever...
EOF

if test $framework_failure = 1; then
  echo "$0: failure in testing framework" 1>&2
  (exit 1); exit 1
fi

set -e
fail=0

name_and_addr='joe blow <j@blow.org>'
vc-dwim --commit --author="$name_and_addr" > /dev/null
git log --max-count=1 > out
grep "^Author: $name_and_addr" out > /dev/null

##################################################
# Now ensure that with no --author=... option, the
# name in ChangeLog entry is used as the author.
cat <<EOF > t || framework_failure=1
2006-09-05  X Y  <z@example.org>

	* x: Done.

EOF
cat t ChangeLog > k
mv k ChangeLog
echo foo >> x

vc-dwim --commit > /dev/null
git log --max-count=1 > out
grep '^Author: X Y <z@example\.org>' out > /dev/null

##################################################
# Now ensure that when --author=... and ChangeLog
# disagree, vc-dwim objects.
cat <<EOF > t || framework_failure=1
2006-09-06  X Y  <z@example.org>

	* x: Done, again.

EOF
cat t ChangeLog > k
mv k ChangeLog
echo foo >> x

# Expect this to fail
vc-dwim --author='wrong.person@example.org' --commit 2> /dev/null || :

##################################################
# Finally, ensure that when --author=S matches the name/email
# in ChangeLog it works.
cat <<EOF > t || framework_failure=1
2006-09-07  X Y  <z@example.org>

	* x: Last.

EOF
cat t ChangeLog > k
mv k ChangeLog
echo foo >> x

# Expect this to succeed
vc-dwim --author='X Y <z@example.org>' --commit > /dev/null

Exit 0
