#!/bin/sh

# This file holds logic that is used in many tests.
# It can be called in a test like this:
#  $ . "$TESTDIR/testutil"

# our test suite relies on being able to create symlinks, even on
# Windows
export MSYS=winsymlinks:nativestrict

# Always trust root - which may own the repository we're working off
echo "[trusted]" >> $HGRCPATH
echo "users=root" >> $HGRCPATH

# silence some output related to templates
mkdir -p $TESTTMP/gittemplates
export GIT_TEMPLATE_DIR=$TESTTMP/gittemplates

# Activate extensions
echo "[extensions]" >> $HGRCPATH
echo "hggit=$(echo $(dirname $TESTDIR))/hggit" >> $HGRCPATH
echo 'mq=' >> $HGRCPATH

# Enable git subrepository
echo '[subrepos]' >> $HGRCPATH
echo 'git:allowed = yes' >> $HGRCPATH

# silence warning from recent git
cat >> $TESTTMP/.gitconfig <<EOF
[init]
defaultBranch = master
[protocol.file]
allow = always
EOF

# Standard checks for external dependencies
# We use the git command-line client and dulwich in pretty much all the tests.
# Thus, to avoid repetitively declaring that requirement in almost every test,
# we just call the checks in all tests that include this library.
"$TESTDIR/hghave" dulwich || exit 80
"$TESTDIR/hghave" git || exit 80

GIT_AUTHOR_NAME='test'; export GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL='test@example.org'; export GIT_AUTHOR_EMAIL
GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0000"; export GIT_AUTHOR_DATE
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; export GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"; export GIT_COMMITTER_EMAIL
GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"; export GIT_COMMITTER_DATE

# Functions to commit and tag in Mercurial and Git in a predictable manner
count=10

fn_get_date() {
    h=$(expr $count / 60 / 60)
    m=$(expr $count / 60 % 60)
    s=$(expr $count % 60)
    printf "2007-01-01 %02d:%02d:%02d +0000" $h $m $s
}

fn_git_commit() {
    GIT_AUTHOR_DATE="$(fn_get_date)"
    GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
    git commit "$@" >/dev/null || echo "git commit error"
    count=`expr $count + 1`
}

fn_git_rebase() {
    GIT_AUTHOR_DATE="$(fn_get_date)"
    GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
    git rebase --quiet "$@" >/dev/null || echo "git rebase error"
    count=`expr $count + 1`
}

fn_hg_commit() {
    HGDATE="$(fn_get_date)"
    hg commit -d "$HGDATE" "$@" >/dev/null || echo "hg commit error"
    count=`expr $count + 1`
}

fn_hg_commitextra() {
    HGDATE="$(fn_get_date)"
    hg --config extensions.commitextra=$TESTDIR/testlib/ext-commit-extra.py \
       commitextra -d "$HGDATE" "$@" >/dev/null || echo "hg commit error"
    count=`expr $count + 1`
}

fn_git_tag() {
    GIT_AUTHOR_DATE="$(fn_get_date)"
    GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
    git tag "$@" >/dev/null || echo "git tag error"
    count=`expr $count + 1`
}

fn_hg_tag() {
    HGDATE="$(fn_get_date)"
    hg tag -d "$HGDATE" "$@" >/dev/null || echo "hg tag error"
    count=`expr $count + 1`
}

fn_touch_escaped() {
    python - "$@" <<EOF
import os, sys
for p in sys.argv[1:]:
  p = p.encode('ascii').decode('unicode_escape').encode('utf-8')
  if b'/' in p and not os.path.exists(os.path.dirname(p)):
    os.makedirs(os.path.dirname(p))
  open(p, 'w')
EOF
}
