#!/bin/sh -ex

#
# inheritable mergeinfo failure
#
# This is reduced from some real-world use on an enormous repository.
# 

#### common variables
WORK=$(mktemp -d)
REPO=$WORK/repo
WC=$WORK/wc
MWC=$WORK/mwc
RURI=file:////$REPO

#### cleanup helper
trap "echo rm -rf $WORK" EXIT

#### set up generic repository
svnadmin create $REPO
svn -q mkdir -m 'init trunk' $RURI/trunk
svn -q mkdir -m 'init branches' $RURI/branches

#### set up some content

# directories
svn -q mkdir -m 'init content' $RURI/trunk/product
svn -q mkdir -m 'init content' $RURI/trunk/product/thing
svn -q mkdir -m 'init content' $RURI/trunk/product/frob

# create file trunk/product/thing/code.py
mkdir $WC; cd $WC
   svn -q co $RURI/trunk/product/thing
   cd thing
   date +%s.%N > code.py
   svn add code.py
   svn commit -m 'create file' code.py
cd ..; rm -rf $WC

# create file trunk/product/frob/other.py
# note that it's in a *different* directory,
#  but with a common ancestor
mkdir $WC; cd $WC
   svn -q co $RURI/trunk/product/frob
   cd frob
   date +%s.%N > other.py
   svn add other.py
   svn commit -m 'create file' other.py
cd ..; rm -rf $WC

#### Make a full branch.

svn copy -m 'working branch' $RURI/trunk $RURI/branches/troublemaker1

#### Make a simple change on trunk.
# change trunk/product/thing/code.py
mkdir $WC; cd $WC
   svn -q co $RURI/trunk/product/thing
   cd thing
   date +%s.%N > code.py
   svn commit -m 'change file once' code.py
cd ..; rm -rf $WC
CHNO=$(svnlook youngest $REPO)

#### Pull that change on to the branch using a sparse checkout
# (we actually have a pullup_one_change script that automates this;
#  when developers actually do repository-wide atomic commits for
#  consistency, it's expensive to check out the whole branch just
#  to apply a the change -- sparse checkouts make a huge difference.
#  Of course, in the example we're only bothering to change one file.)

mkdir $WC; cd $WC
 svn -q co --depth=empty $RURI/branches/troublemaker1
 cd troublemaker1; svn -q update --depth=empty product
   cd product;     svn -q update --depth=empty thing
    cd thing;      svn -q update --depth=files code.py
    cd ..
   cd ..
  svn merge --depth=infinity -c $CHNO $RURI/trunk .
  svn commit -m "big merge $CHNO"
cd .. ; rm -rf $WC

#### Make a change to a different file, in a different dir with a
####  common ancestor
# change trunk/product/frob/other.py 
mkdir $WC; cd $WC
   svn -q co $RURI/trunk/product/frob
   cd frob
   date +%s.%N > other.py
   svn commit -m 'change other file' other.py
cd ..; rm -rf $WC
CHNO=$(svnlook youngest $REPO)

#### Pull that change on to the same branch, as a change of narrower
####   scope so it doesn't need a big checkout

mkdir $WC
 cd $WC
 svn -q co $RURI/branches/troublemaker1/product/frob
 cd frob
 svn merge -c $CHNO $RURI/trunk/product/frob
 ############# blows up here! ###################
 ##    --- Merging r11 into '.':
 ##    U    other.py
 ##    svn: Mergeinfo for '/trunk/product/frob' maps to an empty revision range


 svn commit -m "small merge $CHNO"
 cd ..
rm -rf $WC


# diags
svn proplist -vR $RURI/trunk
svn pg -R svn:mergeinfo $RURI
svn log -v -g $RURI/branches/troublemaker1/product/thing/code.py



