git-resolve-scripton commit Introduce "reset type" flag to "git reset" (45d197a)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Linus Torvalds
   4#
   5# Resolve two trees.
   6#
   7. git-sh-setup-script || die "Not a git archive"
   8
   9usage () {
  10        die "git-resolve-script <head> <remote> <merge-message>"
  11}
  12
  13dropheads() {
  14        rm -f -- "$GIT_DIR/MERGE_HEAD" \
  15                "$GIT_DIR/LAST_MERGE" || exit 1
  16}
  17
  18head=$(git-rev-parse --verify "$1"^0) &&
  19merge=$(git-rev-parse --verify "$2"^0) &&
  20merge_msg="$3" || usage
  21
  22#
  23# The remote name is just used for the message,
  24# but we do want it.
  25#
  26if [ -z "$head" -o -z "$merge" -o -z "$merge_msg" ]; then
  27        usage
  28fi
  29
  30dropheads
  31echo $head > "$GIT_DIR"/ORIG_HEAD
  32echo $merge > "$GIT_DIR"/LAST_MERGE
  33
  34common=$(git-merge-base $head $merge)
  35if [ -z "$common" ]; then
  36        die "Unable to find common commit between" $merge $head
  37fi
  38
  39if [ "$common" == "$merge" ]; then
  40        echo "Already up-to-date. Yeeah!"
  41        dropheads
  42        exit 0
  43fi
  44if [ "$common" == "$head" ]; then
  45        echo "Updating from $head to $merge."
  46        git-read-tree -u -m $head $merge || exit 1
  47        echo $merge > "$GIT_DIR"/HEAD
  48        git-diff-tree -p $head $merge | git-apply --stat
  49        dropheads
  50        exit 0
  51fi
  52echo "Trying to merge $merge into $head"
  53git-read-tree -u -m $common $head $merge || exit 1
  54result_tree=$(git-write-tree  2> /dev/null)
  55if [ $? -ne 0 ]; then
  56        echo "Simple merge failed, trying Automatic merge"
  57        git-merge-cache -o git-merge-one-file-script -a
  58        if [ $? -ne 0 ]; then
  59                echo $merge > "$GIT_DIR"/MERGE_HEAD
  60                die "Automatic merge failed, fix up by hand"
  61        fi
  62        result_tree=$(git-write-tree) || exit 1
  63fi
  64result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
  65echo "Committed merge $result_commit"
  66echo $result_commit > "$GIT_DIR"/HEAD
  67git-diff-tree -p $head $result_commit | git-apply --stat
  68dropheads