#!/bin/bash
#
# Reproduce an issue in Subversion 1.8.11 where files in a tree
# conflict can have svn:mergeinfo properties added to them during a
# merge.

set -e

SERVER_DIR=server
CLIENT_DIR=client

if [ $# != 1 ]; then
    echo "usage: $0 <path to svn>"
    exit
fi

SVN=$1
SVNADMIN="$(dirname $SVN)/svnadmin"

SERVER_URL="file:///$PWD/$SERVER_DIR"

createAndCommitSubdirWithFile ()
{
    mkdir dir
    touch dir/file.txt
    $SVN add dir
    $SVN commit -m "$1"
    $SVN update  # Update . to committed rev
}

# Create the repo and enter it.
$SVNADMIN create $SERVER_DIR
$SVN checkout $SERVER_URL $CLIENT_DIR
cd $CLIENT_DIR

# Create the trunk/branches structure
mkdir trunk
mkdir branches
$SVN add trunk branches
$SVN commit -m 'Create initial structure'

# Make a branch from the trunk.
$SVN copy ^/trunk ^/branches/branch -m 'Create branch from trunk'

# Commit a subdir with a file in it in the trunk and commit it.
# NB: If testing version 1.6 or earlier, remove "--ignore-ancestry".
$SVN switch --ignore-ancestry ^/trunk ||
    $SVN switch ^/trunk

createAndCommitSubdirWithFile 'Committed on trunk'

# Commit a subdir with a file in it in the branch and commit it.
$SVN switch ^/branches/branch
createAndCommitSubdirWithFile 'Committed on branch'

# Merge the trunk.
$SVN merge --non-interactive ^/trunk || true

# Resolve the tree conflict.
$SVN resolve --accept=working dir ||
    $SVN resolved dir

# See if the svn:mergeinfo properties are what we expect.
$SVN propget svn:mergeinfo -R . >actual.out
echo '. - /trunk:2-4' >expected.out

$SVN --version | head -1

if diff -q actual.out expected.out; then
    echo Success
else
    echo 'Test failed!'
    echo 'Expected output:'
    cat expected.out
    echo 'Actual output:'
    cat actual.out
    exit 1
fi
