#!/bin/sh
# Running "vc-dwim ChangeLog" in a directory beneath the one containing
# an .hg or .git repository is a little tricky internally.
# It "just works" when CVS is the version control system.

. "${srcdir=.}/init.sh"; path_prepend_ ..
print_ver_ vc-dwim

require_cvs_

mkdir repo || framework_failure_
repo=$PWD/repo
cvs -f -Q -d $repo init 2> /dev/null || framework_failure_

mkdir d || framework_failure_
cd d
mkdir s || framework_failure_
touch s/f s/ChangeLog || framework_failure_
cvs -f -Q -d $repo import -m msg m M M0 || framework_failure_
cvs -f -Q -d $repo co m || framework_failure_
cd m || framework_failure_

fail=0

cat <<EOF > s/ChangeLog || fail=1
2006-09-04  Jim Meyering  <jim@meyering.net>

	* f: X.
EOF
echo bow > s/f || fail=1
cd s

# cvs can mistakenly report no diffs due to file system time stamp
# granularity.  If that happens, adjust mtimes manually.
if cvs diff ChangeLog > /dev/null; then
  # Attempt to use gnu touch.  If that fails, sleep.
  touch -d '2 seconds' f ChangeLog 2> /dev/null || sleep 2
fi

vc-dwim ChangeLog > out 2>&1 || fail=1

sed 's/RCS file:.*//;s/^[-+][-+][-+] f.*//' out > k; mv k out
cat <<\EOF > exp || fail=1
* f: X.
Index: f
===================================================================

retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 f


@@ -0,0 +1 @@
+bow
EOF

cmp out exp || fail=1
test $fail = 1 && diff out exp 2> /dev/null

Exit $fail
