1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5# Resolve two or more trees recorded in $GIT_DIR/FETCH_HEAD. 6# 7. git-sh-setup 8 9usage () { 10 die "usage: git octopus" 11} 12 13# Sanity check the heads early. 14whileread SHA1 REPO 15do 16test$(git-cat-file -t $SHA1)="commit"|| 17 die "$REPOgiven to octopus is not a commit" 18done<"$GIT_DIR/FETCH_HEAD" 19 20head=$(git-rev-parse --verify HEAD)||exit 21 22git-update-index --refresh|| 23 die "Your working tree is dirty." 24test"$(git-diff-index --cached "$head")"=""|| 25 die "Your working tree does not match HEAD." 26 27# MRC is the current "merge reference commit" 28# MRT is the current "merge result tree" 29 30MRC=$head PARENT="-p$head" 31MRT=$(git-write-tree) 32CNT=1;# counting our head 33NON_FF_MERGE=0 34whileread SHA1 REPO 35do 36 common=$(git-merge-base $MRC $SHA1)|| 37 die "Unable to find common commit with$SHA1from$REPO" 38 39iftest"$common"=$SHA1 40then 41echo"Already up-to-date:$REPO" 42continue 43fi 44 45 CNT=`expr$CNT+ 1` 46 PARENT="$PARENT-p$SHA1" 47 48iftest"$common,$NON_FF_MERGE"="$MRC,0" 49then 50# The first head being merged was a fast-forward. 51# Advance MRC to the head being merged, and use that 52# tree as the intermediate result of the merge. 53# We still need to count this as part of the parent set. 54 55echo"Fast forwarding to:$REPO" 56 git-read-tree -u -m$head $SHA1||exit 57 MRC=$SHA1 MRT=$(git-write-tree) 58continue 59fi 60 61 NON_FF_MERGE=1 62 63echo"Trying simple merge with$REPO" 64 git-read-tree -u -m$common $MRT $SHA1||exit 65 next=$(git-write-tree 2>/dev/null) 66iftest $? -ne0 67then 68echo"Simple merge did not work, trying automatic merge." 69 git-merge-index -o git-merge-one-file -a|| { 70 git-read-tree --reset"$head" 71 git-checkout-index -f -q -u -a 72 die "Automatic merge failed; should not be doing Octopus" 73} 74 next=$(git-write-tree 2>/dev/null) 75fi 76 MRC=$common 77 MRT=$next 78done<"$GIT_DIR/FETCH_HEAD" 79 80# Just to be careful in case the user feeds nonsense to us. 81case"$CNT"in 821) 83echo"No changes." 84exit0;; 85esac 86result_commit=$(git-fmt-merge-msg<"$GIT_DIR/FETCH_HEAD"| 87 git-commit-tree$MRT $PARENT) 88echo"Committed merge$result_commit" 89git-update-ref HEAD $result_commit $head 90git-diff-tree -p$head $result_commit| git-apply --stat