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