set -x
set -e
mkdir genetest
cd genetest
git init-db
cat - >test1 <<EOF
aaaaaaaa
BBBBBBBB
cccccccc
EOF
echo README>README
git add .
git commit -a -m "Initial checkin"

# Create step2 branch
git checkout -b step2
git mv test1 test2
git commit -a -m "Created step2"
echo dddddddd>>test2
echo "README 2">README
git commit -a -m "Added step2 changes"
git checkout master

# Create conglomerate branch
git checkout -b final

### HERE'S THE IMPORTANT SEQUENCE
# The fetch/pull sequence with the -a args makes certain that the original
# files from the branch above are left intact.  Then the diff index picks up
# those filse that have been "deleted" because of a rename, and they are added
# back.
git fetch -a . step2
git pull -a . step2
FILES=$(git-diff-index --name-only --diff-filter=D master)
git add $FILES
git commit -a -m "Created final"

git checkout master
cat - >test1 <<EOF
aaaaaaaa
bbbbbbbb
cccccccc
EOF
git commit -a -m "Fixed the B... line"
git checkout step2
git pull . master || true
mv -f test1 test2
echo dddddddd>>test2
MSG=$(cat .git/MERGE_MSG)
git commit -a -m "$MSG"

# Now check that all changes come through successfully, the interesting thing is that all the "parent" branches MUST be pulled at once.
git checkout final
git pull . master step2
cat README
cat test1
cat test2
echo SUCCEEDED



