89e967abece8329cb6a79140f6ca1a6a63bf1d07
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5# Resolve two or more trees.
   6#
   7
   8. git-sh-i18n
   9
  10LF='
  11'
  12
  13die () {
  14    echo >&2 "$*"
  15    exit 1
  16}
  17
  18# The first parameters up to -- are merge bases; the rest are heads.
  19bases= head= remotes= sep_seen=
  20for arg
  21do
  22        case ",$sep_seen,$head,$arg," in
  23        *,--,)
  24                sep_seen=yes
  25                ;;
  26        ,yes,,*)
  27                head=$arg
  28                ;;
  29        ,yes,*)
  30                remotes="$remotes$arg "
  31                ;;
  32        *)
  33                bases="$bases$arg "
  34                ;;
  35        esac
  36done
  37
  38# Reject if this is not an Octopus -- resolve should be used instead.
  39case "$remotes" in
  40?*' '?*)
  41        ;;
  42*)
  43        exit 2 ;;
  44esac
  45
  46# MRC is the current "merge reference commit"
  47# MRT is the current "merge result tree"
  48
  49if ! git diff-index --quiet --cached HEAD --
  50then
  51    gettextln "Error: Your local changes to the following files would be overwritten by merge"
  52    git diff-index --cached --name-only HEAD -- | sed -e 's/^/    /'
  53    exit 2
  54fi
  55MRC=$(git rev-parse --verify -q $head)
  56MRT=$(git write-tree)
  57NON_FF_MERGE=0
  58OCTOPUS_FAILURE=0
  59for SHA1 in $remotes
  60do
  61        case "$OCTOPUS_FAILURE" in
  62        1)
  63                # We allow only last one to have a hand-resolvable
  64                # conflicts.  Last round failed and we still had
  65                # a head to merge.
  66                gettextln "Automated merge did not work."
  67                gettextln "Should not be doing an Octopus."
  68                exit 2
  69        esac
  70
  71        eval pretty_name=\${GITHEAD_$SHA1:-$SHA1}
  72        if test "$SHA1" = "$pretty_name"
  73        then
  74                SHA1_UP="$(echo "$SHA1" | tr a-z A-Z)"
  75                eval pretty_name=\${GITHEAD_$SHA1_UP:-$pretty_name}
  76        fi
  77        common=$(git merge-base --all $SHA1 $MRC) ||
  78                die "$(eval_gettext "Unable to find common commit with \$pretty_name")"
  79
  80        case "$LF$common$LF" in
  81        *"$LF$SHA1$LF"*)
  82                eval_gettextln "Already up-to-date with \$pretty_name"
  83                continue
  84                ;;
  85        esac
  86
  87        if test "$common,$NON_FF_MERGE" = "$MRC,0"
  88        then
  89                # The first head being merged was a fast-forward.
  90                # Advance MRC to the head being merged, and use that
  91                # tree as the intermediate result of the merge.
  92                # We still need to count this as part of the parent set.
  93
  94                eval_gettextln "Fast-forwarding to: \$pretty_name"
  95                git read-tree -u -m $head $SHA1 || exit
  96                MRC=$SHA1 MRT=$(git write-tree)
  97                continue
  98        fi
  99
 100        NON_FF_MERGE=1
 101
 102        eval_gettextln "Trying simple merge with \$pretty_name"
 103        git read-tree -u -m --aggressive  $common $MRT $SHA1 || exit 2
 104        next=$(git write-tree 2>/dev/null)
 105        if test $? -ne 0
 106        then
 107                gettextln "Simple merge did not work, trying automatic merge."
 108                git-merge-index -o git-merge-one-file -a ||
 109                OCTOPUS_FAILURE=1
 110                next=$(git write-tree 2>/dev/null)
 111        fi
 112
 113        MRC="$MRC $SHA1"
 114        MRT=$next
 115done
 116
 117exit "$OCTOPUS_FAILURE"