#!/bin/bash
# git merge driver for debian/changelog and other as used in zendframework
# Inspired from https://gist.github.com/mezis/1605647
#
# Add this to .git/config:
#
# [merge "zen"]
# name = zen merge driver
# driver = ./debian/git-merge-zen.sh %O %A %B
#
# - Add this to .gitattributes:
#
# debian/changelog	merge=zen
# …

# From https://stackoverflow.com/questions/24302644/how-to-retrieve-branch-names-in-a-custom-git-merge-driver
# retrieve base branch name from a sym ref of HEAD
branch=$(git symbolic-ref HEAD) # e.g. refs/heads/master
# cut out "refs/heads"
destination="${branch#refs/heads/}"

O=$1
A=$2
B=$3

# Change the package’s @@name@@
temp1=$(mktemp /tmp/merge-zen.XXXX)
temp2=$(mktemp /tmp/merge-zen.XXXX)
sed "s/@@name@@/$destination/g;s/@@Name@@/${destination^}/g" $O > $temp1
sed "s/@@name@@/$destination/g;s/@@Name@@/${destination^}/g" $B > $temp2

# merge
merge $A $temp1 $temp2

# Clean up
rm $temp1
rm $temp2
