blob: 926307005493b7c8c4253b1adff34bdb4bc57b77 [file] [log] [blame]
Linus Torvalds67cc5c42005-05-05 11:43:30 -07001#!/bin/sh
2#
Junio C Hamano8ac069a2005-05-09 22:57:58 -07003# Copyright (c) 2005 Linus Torvalds
4#
Linus Torvalds67cc5c42005-05-05 11:43:30 -07005# Resolve two trees.
6#
Linus Torvaldsb33e9662005-07-08 10:57:21 -07007
freku045@student.liu.se806f36d2005-12-13 23:30:31 +01008USAGE='<head> <remote> <merge-message>'
9. git-sh-setup
Linus Torvalds67cc5c42005-05-05 11:43:30 -070010
Dan Holmsandc5914122005-06-20 20:52:38 +020011dropheads() {
Linus Torvalds6b38a402005-06-21 14:04:13 -070012 rm -f -- "$GIT_DIR/MERGE_HEAD" \
Dan Holmsandc5914122005-06-20 20:52:38 +020013 "$GIT_DIR/LAST_MERGE" || exit 1
14}
Linus Torvalds67cc5c42005-05-05 11:43:30 -070015
Junio C Hamano165e1602005-08-20 01:21:21 -070016head=$(git-rev-parse --verify "$1"^0) &&
17merge=$(git-rev-parse --verify "$2"^0) &&
18merge_msg="$3" || usage
19
Linus Torvalds67cc5c42005-05-05 11:43:30 -070020#
21# The remote name is just used for the message,
22# but we do want it.
23#
Linus Torvalds3ba513c2005-07-08 17:38:44 -070024if [ -z "$head" -o -z "$merge" -o -z "$merge_msg" ]; then
Junio C Hamano165e1602005-08-20 01:21:21 -070025 usage
Linus Torvalds67cc5c42005-05-05 11:43:30 -070026fi
27
Dan Holmsandc5914122005-06-20 20:52:38 +020028dropheads
29echo $head > "$GIT_DIR"/ORIG_HEAD
30echo $merge > "$GIT_DIR"/LAST_MERGE
31
Linus Torvalds67cc5c42005-05-05 11:43:30 -070032common=$(git-merge-base $head $merge)
33if [ -z "$common" ]; then
Linus Torvaldsb33e9662005-07-08 10:57:21 -070034 die "Unable to find common commit between" $merge $head
Linus Torvalds67cc5c42005-05-05 11:43:30 -070035fi
36
Junio C Hamano86b13da2005-09-02 10:53:15 -070037case "$common" in
38"$merge")
Linus Torvalds67cc5c42005-05-05 11:43:30 -070039 echo "Already up-to-date. Yeeah!"
Dan Holmsandc5914122005-06-20 20:52:38 +020040 dropheads
Linus Torvalds67cc5c42005-05-05 11:43:30 -070041 exit 0
Junio C Hamano86b13da2005-09-02 10:53:15 -070042 ;;
43"$head")
Linus Torvalds67cc5c42005-05-05 11:43:30 -070044 echo "Updating from $head to $merge."
Linus Torvalds220a0b52005-06-05 22:07:31 -070045 git-read-tree -u -m $head $merge || exit 1
Junio C Hamanobf7960e2005-09-27 18:14:27 -070046 git-update-ref HEAD "$merge" "$head"
Linus Torvalds6b38a402005-06-21 14:04:13 -070047 git-diff-tree -p $head $merge | git-apply --stat
Dan Holmsandc5914122005-06-20 20:52:38 +020048 dropheads
Linus Torvalds67cc5c42005-05-05 11:43:30 -070049 exit 0
Junio C Hamano86b13da2005-09-02 10:53:15 -070050 ;;
51esac
Junio C Hamano9585e402005-08-23 21:08:59 -070052
53# Find an optimum merge base if there are more than one candidates.
54LF='
55'
56common=$(git-merge-base -a $head $merge)
57case "$common" in
58?*"$LF"?*)
59 echo "Trying to find the optimum merge base."
60 G=.tmp-index$$
61 best=
62 best_cnt=-1
63 for c in $common
64 do
65 rm -f $G
66 GIT_INDEX_FILE=$G git-read-tree -m $c $head $merge \
67 2>/dev/null || continue
68 # Count the paths that are unmerged.
69 cnt=`GIT_INDEX_FILE=$G git-ls-files --unmerged | wc -l`
70 if test $best_cnt -le 0 -o $cnt -le $best_cnt
71 then
72 best=$c
73 best_cnt=$cnt
74 if test "$best_cnt" -eq 0
75 then
76 # Cannot do any better than all trivial merge.
77 break
78 fi
79 fi
80 done
81 rm -f $G
82 common="$best"
83esac
84
85echo "Trying to merge $merge into $head using $common."
Junio C Hamano215a7ad2005-09-07 17:26:23 -070086git-update-index --refresh 2>/dev/null
Linus Torvalds220a0b52005-06-05 22:07:31 -070087git-read-tree -u -m $common $head $merge || exit 1
Linus Torvalds67cc5c42005-05-05 11:43:30 -070088result_tree=$(git-write-tree 2> /dev/null)
89if [ $? -ne 0 ]; then
90 echo "Simple merge failed, trying Automatic merge"
Junio C Hamano215a7ad2005-09-07 17:26:23 -070091 git-merge-index -o git-merge-one-file -a
Linus Torvaldse5b905c2005-06-06 19:37:25 -070092 if [ $? -ne 0 ]; then
Dan Holmsandc5914122005-06-20 20:52:38 +020093 echo $merge > "$GIT_DIR"/MERGE_HEAD
Linus Torvaldsb33e9662005-07-08 10:57:21 -070094 die "Automatic merge failed, fix up by hand"
Linus Torvaldse5b905c2005-06-06 19:37:25 -070095 fi
Linus Torvalds67cc5c42005-05-05 11:43:30 -070096 result_tree=$(git-write-tree) || exit 1
97fi
Linus Torvaldsd5a72fd2005-05-05 16:07:56 -070098result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
Linus Torvalds67cc5c42005-05-05 11:43:30 -070099echo "Committed merge $result_commit"
Junio C Hamanobf7960e2005-09-27 18:14:27 -0700100git-update-ref HEAD "$result_commit" "$head"
Linus Torvalds9c065312005-06-08 13:33:15 -0700101git-diff-tree -p $head $result_commit | git-apply --stat
Dan Holmsandc5914122005-06-20 20:52:38 +0200102dropheads