]> git.ipfire.org Git - thirdparty/git.git/blame - git-resolve-script
Try to find the optimum merge base while resolving.
[thirdparty/git.git] / git-resolve-script
CommitLineData
67cc5c4e
LT
1#!/bin/sh
2#
8ac069ac
JH
3# Copyright (c) 2005 Linus Torvalds
4#
67cc5c4e
LT
5# Resolve two trees.
6#
b33e9666
LT
7. git-sh-setup-script || die "Not a git archive"
8
165e160e
JH
9usage () {
10 die "git-resolve-script <head> <remote> <merge-message>"
11}
67cc5c4e 12
c591412c 13dropheads() {
6b38a402 14 rm -f -- "$GIT_DIR/MERGE_HEAD" \
c591412c
DH
15 "$GIT_DIR/LAST_MERGE" || exit 1
16}
67cc5c4e 17
165e160e
JH
18head=$(git-rev-parse --verify "$1"^0) &&
19merge=$(git-rev-parse --verify "$2"^0) &&
20merge_msg="$3" || usage
21
67cc5c4e
LT
22#
23# The remote name is just used for the message,
24# but we do want it.
25#
3ba513c3 26if [ -z "$head" -o -z "$merge" -o -z "$merge_msg" ]; then
165e160e 27 usage
67cc5c4e
LT
28fi
29
c591412c
DH
30dropheads
31echo $head > "$GIT_DIR"/ORIG_HEAD
32echo $merge > "$GIT_DIR"/LAST_MERGE
33
67cc5c4e
LT
34common=$(git-merge-base $head $merge)
35if [ -z "$common" ]; then
b33e9666 36 die "Unable to find common commit between" $merge $head
67cc5c4e
LT
37fi
38
39if [ "$common" == "$merge" ]; then
40 echo "Already up-to-date. Yeeah!"
c591412c 41 dropheads
67cc5c4e
LT
42 exit 0
43fi
44if [ "$common" == "$head" ]; then
45 echo "Updating from $head to $merge."
220a0b52 46 git-read-tree -u -m $head $merge || exit 1
8ac069ac 47 echo $merge > "$GIT_DIR"/HEAD
6b38a402 48 git-diff-tree -p $head $merge | git-apply --stat
c591412c 49 dropheads
67cc5c4e
LT
50 exit 0
51fi
9585e406
JH
52
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."
86git-update-cache --refresh 2>/dev/null
220a0b52 87git-read-tree -u -m $common $head $merge || exit 1
67cc5c4e
LT
88result_tree=$(git-write-tree 2> /dev/null)
89if [ $? -ne 0 ]; then
90 echo "Simple merge failed, trying Automatic merge"
e5b905c4
LT
91 git-merge-cache -o git-merge-one-file-script -a
92 if [ $? -ne 0 ]; then
c591412c 93 echo $merge > "$GIT_DIR"/MERGE_HEAD
b33e9666 94 die "Automatic merge failed, fix up by hand"
e5b905c4 95 fi
67cc5c4e
LT
96 result_tree=$(git-write-tree) || exit 1
97fi
d5a72fd6 98result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
67cc5c4e 99echo "Committed merge $result_commit"
8ac069ac 100echo $result_commit > "$GIT_DIR"/HEAD
9c065315 101git-diff-tree -p $head $result_commit | git-apply --stat
c591412c 102dropheads