git-octopus.shon commit Use Daniel's read-tree in the merge strategy 'resolve'. (2276aa6)
   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 || die "Not a git archive"
   8
   9usage () {
  10    die "usage: git octopus"
  11}
  12
  13# Sanity check the heads early.
  14while read SHA1 REPO
  15do
  16        test $(git-cat-file -t $SHA1) = "commit" ||
  17                die "$REPO given 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 MSG= PARENT="-p $head"
  31MRT=$(git-write-tree)
  32CNT=1 ;# counting our head
  33NON_FF_MERGE=0
  34while read SHA1 REPO
  35do
  36        common=$(git-merge-base $MRC $SHA1) ||
  37                die "Unable to find common commit with $SHA1 from $REPO"
  38
  39        if test "$common" = $SHA1
  40        then
  41                echo "Already up-to-date: $REPO"
  42                continue
  43        fi
  44
  45        CNT=`expr $CNT + 1`
  46        PARENT="$PARENT -p $SHA1"
  47        MSG="$MSG
  48        $REPO"
  49
  50        if test "$common,$NON_FF_MERGE" = "$MRC,0"
  51        then
  52                # The first head being merged was a fast-forward.
  53                # Advance MRC to the head being merged, and use that
  54                # tree as the intermediate result of the merge.
  55                # We still need to count this as part of the parent set.
  56
  57                echo "Fast forwarding to: $REPO"
  58                git-read-tree -u -m $head $SHA1 || exit
  59                MRC=$SHA1 MRT=$(git-write-tree)
  60                continue
  61        fi
  62
  63        NON_FF_MERGE=1
  64
  65        echo "Trying simple merge with $REPO"
  66        git-read-tree -u -m $common $MRT $SHA1 || exit
  67        next=$(git-write-tree 2>/dev/null)
  68        if test $? -ne 0
  69        then
  70                echo "Simple merge did not work, trying automatic merge."
  71                git-merge-index -o git-merge-one-file -a || {
  72                git-read-tree --reset "$head"
  73                git-checkout-index -f -q -u -a
  74                die "Automatic merge failed; should not be doing Octopus"
  75                }
  76                next=$(git-write-tree 2>/dev/null)
  77        fi
  78        MRC=$common
  79        MRT=$next
  80done <"$GIT_DIR/FETCH_HEAD"
  81
  82# Just to be careful in case the user feeds nonsense to us.
  83case "$CNT" in
  841)
  85        echo "No changes."
  86        exit 0 ;;
  872)
  88        echo "Not an Octopus; making an ordinary commit."
  89        MSG="Merge "`expr "$MSG" : '.   \(.*\)'` ; # remove LF and TAB
  90        ;;
  91*)
  92        # In an octopus, the original head is just one of the equals,
  93        # so we should list it as such.
  94        HEAD_LINK=`readlink "$GIT_DIR/HEAD"`
  95        MSG="Octopus merge of the following:
  96
  97        $HEAD_LINK from .$MSG"
  98        ;;
  99esac
 100result_commit=$(echo "$MSG" | git-commit-tree $MRT $PARENT)
 101echo "Committed merge $result_commit"
 102echo $result_commit >"$GIT_DIR"/HEAD
 103git-diff-tree -p $head $result_commit | git-apply --stat