1#!/bin/sh
2#
3# use "$1" or something in a real script, this
4# just hard-codes it.
5#
6merge_repo=$1
7merge_name=${2:-HEAD}
8
9rm -f .git/MERGE_HEAD .git/ORIG_HEAD
10cp .git/HEAD .git/ORIG_HEAD
11
12echo "Getting object database"
13rsync -avz --ignore-existing $merge_repo/objects/. ${SHA1_FILE_DIRECTORY:-.git/objects}/.
14
15echo "Getting remote $merge_name"
16rsync -L $merge_repo/$merge_name .git/MERGE_HEAD || exit 1
17
18head=$(cat .git/HEAD)
19merge_head=$(cat .git/MERGE_HEAD)
20common=$(git-merge-base $head $merge_head)
21if [ -z "$common" ]; then
22 echo "Unable to find common commit between" $merge_head $head
23 exit 1
24fi
25
26# Get the trees associated with those commits
27common_tree=$(git-cat-file commit $common | sed 's/tree //;q')
28head_tree=$(git-cat-file commit $head | sed 's/tree //;q')
29merge_tree=$(git-cat-file commit $merge_head | sed 's/tree //;q')
30
31if [ "$common" == "$merge_head" ]; then
32 echo "Already up-to-date. Yeeah!"
33 exit 0
34fi
35if [ "$common" == "$head" ]; then
36 echo "Updating from $head to $merge_head."
37 echo "Destroying all noncommitted data!"
38 echo "Kill me within 3 seconds.."
39 sleep 3
40 git-read-tree -m $merge_tree && git-checkout-cache -f -a && git-update-cache --refresh
41 echo $merge_head > .git/HEAD
42 exit 0
43fi
44echo "Trying to merge $merge_head into $head"
45git-read-tree -m $common_tree $head_tree $merge_tree
46merge_msg="Merge of $merge_repo"
47result_tree=$(git-write-tree 2> /dev/null)
48if [ $? -ne 0 ]; then
49 echo "Simple merge failed, trying Automatic merge"
50 git-merge-cache git-merge-one-file-script -a
51 merge_msg="Automatic merge of $merge_repo"
52 result_tree=$(git-write-tree) || exit 1
53fi
54result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge_head)
55echo "Committed merge $result_commit"
56echo $result_commit > .git/HEAD
57git-checkout-cache -f -a && git-update-cache --refresh
58git-diff-tree -p ORIG_HEAD HEAD | diffstat -p1