#!/usr/bin/env bash
#
# Copyright (c) Linus Torvalds, 2005
#
# This is the per-file merge script, called with
#
#   $1 - original file SHA1 (or empty)
#   $2 - file in branch1 SHA1 (or empty)
#   $3 - file in branch2 SHA1 (or empty)
#   $4 - pathname in repository
#   $5 - original permissions
#   $6 - new permissions in branch1
#   $7 - new permissions in branch2
#
# We are designed to merge $3 _to_ $2, so we will give it
# a preference.
#
#
# Handle some trivial cases.. The _really_ trivial cases have
# been handled already by read-tree, but that one doesn't
# do any merges that migth change the tree layout
#

. cg-Xlib

# if the directory is newly added in a branch, it might not exist
# in the current tree
dir=$(dirname "$4")
mkdir -p "$dir"


error () {
	echo "MERGE ERROR: $@" >&2
}


case "${1:-.}${2:-.}${3:-.}" in
#
# deleted in both
#
"$1..")
	error "$4 is removed in both branches"
	error "This is a potential rename conflict"
	exit 1
	;;
#
# deleted in one and unchanged in the other
#
"$1.$1" | "$1$1.")
	#echo "Removing $4"
	rm -f -- "$4"; update-cache --remove -- "$4"
	exit 0
	;;

#
# added in one
#
".$2." | "..$3" )
	#echo "Adding $4 with perm $6$7"
	cat-file blob "${2:-$3}" >"$4"
	chmod "${6:-$7}" "$4"
	update-cache --add -- $4
	exit 0
	;;
#
# Added in both (check for same permissions)
#
".$2$2")
	#echo "Adding $4 with perm $6"
	cat-file blob "${2:-$3}" >"$4"
	chmod "${6:-$7}" "$4"
	update-cache --add -- $4
	if [ "$6" != "$7" ]; then
		error "Added in both branches, permissions conflict $6->$7"
		exit 1
	fi
	exit 0
	;;

#
# Modified in both, but differently ;(
#
"$1$2$3")
	echo "... Auto-merging $4"
	orig=$(unpack-file $1)
	src1=$(unpack-file $2)
	src2=$(unpack-file $3)
	ret=0
	if [ "$6" != "$7" ]; then
		error "Permissions conflict: $5->$6 here but merging $7"
		ret=1
	fi
	chmod "$6" "$src2"
	if ! merge "$src2" "$orig" "$src1"; then
		error "Auto-merge failed"
		mv -- "$src2" "$4"
		ret=1

	elif ! mv -- "$src2" "$4" || ! update-cache --add -- "$4"; then
		error "Choosing $src2 -> $4 failed"
		ret=1
	fi
	rm "$orig" "$src1"
	exit $ret
	;;

*)
	error "Not handling case $1 -> $2 -> $3"
	;;
esac
exit 1
