Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
Junio C Hamano | 8ac069a | 2005-05-09 22:57:58 -0700 | [diff] [blame] | 3 | # Copyright (c) 2005 Linus Torvalds |
| 4 | # |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 5 | # Resolve two trees. |
| 6 | # |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 7 | |
freku045@student.liu.se | 806f36d | 2005-12-13 23:30:31 +0100 | [diff] [blame] | 8 | USAGE='<head> <remote> <merge-message>' |
| 9 | . git-sh-setup |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 10 | |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 11 | dropheads() { |
Linus Torvalds | 6b38a40 | 2005-06-21 14:04:13 -0700 | [diff] [blame] | 12 | rm -f -- "$GIT_DIR/MERGE_HEAD" \ |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 13 | "$GIT_DIR/LAST_MERGE" || exit 1 |
| 14 | } |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 15 | |
Junio C Hamano | 165e160 | 2005-08-20 01:21:21 -0700 | [diff] [blame] | 16 | head=$(git-rev-parse --verify "$1"^0) && |
| 17 | merge=$(git-rev-parse --verify "$2"^0) && |
| 18 | merge_msg="$3" || usage |
| 19 | |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 20 | # |
| 21 | # The remote name is just used for the message, |
| 22 | # but we do want it. |
| 23 | # |
Linus Torvalds | 3ba513c | 2005-07-08 17:38:44 -0700 | [diff] [blame] | 24 | if [ -z "$head" -o -z "$merge" -o -z "$merge_msg" ]; then |
Junio C Hamano | 165e160 | 2005-08-20 01:21:21 -0700 | [diff] [blame] | 25 | usage |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 26 | fi |
| 27 | |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 28 | dropheads |
| 29 | echo $head > "$GIT_DIR"/ORIG_HEAD |
| 30 | echo $merge > "$GIT_DIR"/LAST_MERGE |
| 31 | |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 32 | common=$(git-merge-base $head $merge) |
| 33 | if [ -z "$common" ]; then |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 34 | die "Unable to find common commit between" $merge $head |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 35 | fi |
| 36 | |
Junio C Hamano | 86b13da | 2005-09-02 10:53:15 -0700 | [diff] [blame] | 37 | case "$common" in |
| 38 | "$merge") |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 39 | echo "Already up-to-date. Yeeah!" |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 40 | dropheads |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 41 | exit 0 |
Junio C Hamano | 86b13da | 2005-09-02 10:53:15 -0700 | [diff] [blame] | 42 | ;; |
| 43 | "$head") |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 44 | echo "Updating from $head to $merge." |
Linus Torvalds | 220a0b5 | 2005-06-05 22:07:31 -0700 | [diff] [blame] | 45 | git-read-tree -u -m $head $merge || exit 1 |
Junio C Hamano | bf7960e | 2005-09-27 18:14:27 -0700 | [diff] [blame] | 46 | git-update-ref HEAD "$merge" "$head" |
Linus Torvalds | 6b38a40 | 2005-06-21 14:04:13 -0700 | [diff] [blame] | 47 | git-diff-tree -p $head $merge | git-apply --stat |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 48 | dropheads |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 49 | exit 0 |
Junio C Hamano | 86b13da | 2005-09-02 10:53:15 -0700 | [diff] [blame] | 50 | ;; |
| 51 | esac |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 52 | |
Junio C Hamano | e3b59a4 | 2006-02-18 20:51:26 -0800 | [diff] [blame] | 53 | # We are going to make a new commit. |
| 54 | git var GIT_COMMITTER_IDENT >/dev/null || exit |
| 55 | |
Junio C Hamano | 9585e40 | 2005-08-23 21:08:59 -0700 | [diff] [blame] | 56 | # Find an optimum merge base if there are more than one candidates. |
| 57 | LF=' |
| 58 | ' |
| 59 | common=$(git-merge-base -a $head $merge) |
| 60 | case "$common" in |
| 61 | ?*"$LF"?*) |
| 62 | echo "Trying to find the optimum merge base." |
| 63 | G=.tmp-index$$ |
| 64 | best= |
| 65 | best_cnt=-1 |
| 66 | for c in $common |
| 67 | do |
| 68 | rm -f $G |
| 69 | GIT_INDEX_FILE=$G git-read-tree -m $c $head $merge \ |
| 70 | 2>/dev/null || continue |
| 71 | # Count the paths that are unmerged. |
| 72 | cnt=`GIT_INDEX_FILE=$G git-ls-files --unmerged | wc -l` |
| 73 | if test $best_cnt -le 0 -o $cnt -le $best_cnt |
| 74 | then |
| 75 | best=$c |
| 76 | best_cnt=$cnt |
| 77 | if test "$best_cnt" -eq 0 |
| 78 | then |
| 79 | # Cannot do any better than all trivial merge. |
| 80 | break |
| 81 | fi |
| 82 | fi |
| 83 | done |
| 84 | rm -f $G |
| 85 | common="$best" |
| 86 | esac |
| 87 | |
| 88 | echo "Trying to merge $merge into $head using $common." |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 89 | git-update-index --refresh 2>/dev/null |
Linus Torvalds | 220a0b5 | 2005-06-05 22:07:31 -0700 | [diff] [blame] | 90 | git-read-tree -u -m $common $head $merge || exit 1 |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 91 | result_tree=$(git-write-tree 2> /dev/null) |
| 92 | if [ $? -ne 0 ]; then |
| 93 | echo "Simple merge failed, trying Automatic merge" |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 94 | git-merge-index -o git-merge-one-file -a |
Linus Torvalds | e5b905c | 2005-06-06 19:37:25 -0700 | [diff] [blame] | 95 | if [ $? -ne 0 ]; then |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 96 | echo $merge > "$GIT_DIR"/MERGE_HEAD |
Linus Torvalds | b33e966 | 2005-07-08 10:57:21 -0700 | [diff] [blame] | 97 | die "Automatic merge failed, fix up by hand" |
Linus Torvalds | e5b905c | 2005-06-06 19:37:25 -0700 | [diff] [blame] | 98 | fi |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 99 | result_tree=$(git-write-tree) || exit 1 |
| 100 | fi |
Linus Torvalds | d5a72fd | 2005-05-05 16:07:56 -0700 | [diff] [blame] | 101 | result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge) |
Linus Torvalds | 67cc5c4 | 2005-05-05 11:43:30 -0700 | [diff] [blame] | 102 | echo "Committed merge $result_commit" |
Junio C Hamano | bf7960e | 2005-09-27 18:14:27 -0700 | [diff] [blame] | 103 | git-update-ref HEAD "$result_commit" "$head" |
Linus Torvalds | 9c06531 | 2005-06-08 13:33:15 -0700 | [diff] [blame] | 104 | git-diff-tree -p $head $result_commit | git-apply --stat |
Dan Holmsand | c591412 | 2005-06-20 20:52:38 +0200 | [diff] [blame] | 105 | dropheads |