#!/bin/bash
#
# file-edit-delete.sh
#
# Test whether a file edit/delete merge conflict introduces subtree merge info.

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"

# 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'

# Check out the trunk.
$SVN switch --ignore-ancestry ^/trunk ||
    $SVN switch ^/trunk

# Create a file in a directory and commit it on the trunk.
mkdir dir
echo hey >dir/file.txt
$SVN add dir
$SVN commit -m 'Created dir/file.txt on trunk'

# Check out the branch and merge from trunk.
$SVN switch ^/branches/branch
$SVN merge --non-interactive ^/trunk
$SVN commit -m 'Merge from trunk'

# Edit the file on the branch and commit.
echo "on branch" >>dir/file.txt
$SVN commit -m "Edited file on branch"

# Delete the file on trunk and commit.
$SVN switch ^/trunk
$SVN rm dir/file.txt
$SVN commit -m "Removed file on trunk"

# Merge to branch and resolve in favor of keeping the edited version
$SVN switch ^/branches/branch
$SVN merge --non-interactive ^/trunk || true
$SVN resolve --accept=working dir/file.txt

$SVN propget svn:mergeinfo -R . >actual.out
echo '. - /trunk:2-6' >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
