]> git.ipfire.org Git - thirdparty/git.git/blame - git-resolve.sh
Fix installation of templates on ancient systems.
[thirdparty/git.git] / git-resolve.sh
CommitLineData
67cc5c4e
LT
1#!/bin/sh
2#
8ac069ac
JH
3# Copyright (c) 2005 Linus Torvalds
4#
67cc5c4e
LT
5# Resolve two trees.
6#
b33e9666 7
806f36d4
FK
8USAGE='<head> <remote> <merge-message>'
9. git-sh-setup
67cc5c4e 10
c591412c 11dropheads() {
6b38a402 12 rm -f -- "$GIT_DIR/MERGE_HEAD" \
c591412c
DH
13 "$GIT_DIR/LAST_MERGE" || exit 1
14}
67cc5c4e 15
165e160e
JH
16head=$(git-rev-parse --verify "$1"^0) &&
17merge=$(git-rev-parse --verify "$2"^0) &&
5a6852fe 18merge_name="$2" &&
165e160e
JH
19merge_msg="$3" || usage
20
67cc5c4e
LT
21#
22# The remote name is just used for the message,
23# but we do want it.
24#
3ba513c3 25if [ -z "$head" -o -z "$merge" -o -z "$merge_msg" ]; then
165e160e 26 usage
67cc5c4e
LT
27fi
28
c591412c
DH
29dropheads
30echo $head > "$GIT_DIR"/ORIG_HEAD
31echo $merge > "$GIT_DIR"/LAST_MERGE
32
67cc5c4e
LT
33common=$(git-merge-base $head $merge)
34if [ -z "$common" ]; then
b33e9666 35 die "Unable to find common commit between" $merge $head
67cc5c4e
LT
36fi
37
86b13da4
JH
38case "$common" in
39"$merge")
67cc5c4e 40 echo "Already up-to-date. Yeeah!"
c591412c 41 dropheads
67cc5c4e 42 exit 0
86b13da4
JH
43 ;;
44"$head")
180b0d74 45 echo "Updating from $head to $merge"
220a0b52 46 git-read-tree -u -m $head $merge || exit 1
5a6852fe
SP
47 git-update-ref -m "resolve $merge_name: Fast forward" \
48 HEAD "$merge" "$head"
6b38a402 49 git-diff-tree -p $head $merge | git-apply --stat
c591412c 50 dropheads
67cc5c4e 51 exit 0
86b13da4
JH
52 ;;
53esac
9585e406 54
e3b59a44
JH
55# We are going to make a new commit.
56git var GIT_COMMITTER_IDENT >/dev/null || exit
57
9585e406
JH
58# Find an optimum merge base if there are more than one candidates.
59LF='
60'
61common=$(git-merge-base -a $head $merge)
62case "$common" in
63?*"$LF"?*)
64 echo "Trying to find the optimum merge base."
65 G=.tmp-index$$
66 best=
67 best_cnt=-1
68 for c in $common
69 do
70 rm -f $G
71 GIT_INDEX_FILE=$G git-read-tree -m $c $head $merge \
72 2>/dev/null || continue
73 # Count the paths that are unmerged.
74 cnt=`GIT_INDEX_FILE=$G git-ls-files --unmerged | wc -l`
75 if test $best_cnt -le 0 -o $cnt -le $best_cnt
76 then
77 best=$c
78 best_cnt=$cnt
79 if test "$best_cnt" -eq 0
80 then
81 # Cannot do any better than all trivial merge.
82 break
83 fi
84 fi
85 done
86 rm -f $G
87 common="$best"
88esac
89
90echo "Trying to merge $merge into $head using $common."
215a7ad1 91git-update-index --refresh 2>/dev/null
220a0b52 92git-read-tree -u -m $common $head $merge || exit 1
67cc5c4e
LT
93result_tree=$(git-write-tree 2> /dev/null)
94if [ $? -ne 0 ]; then
95 echo "Simple merge failed, trying Automatic merge"
215a7ad1 96 git-merge-index -o git-merge-one-file -a
e5b905c4 97 if [ $? -ne 0 ]; then
c591412c 98 echo $merge > "$GIT_DIR"/MERGE_HEAD
b33e9666 99 die "Automatic merge failed, fix up by hand"
e5b905c4 100 fi
67cc5c4e
LT
101 result_tree=$(git-write-tree) || exit 1
102fi
d5a72fd6 103result_commit=$(echo "$merge_msg" | git-commit-tree $result_tree -p $head -p $merge)
67cc5c4e 104echo "Committed merge $result_commit"
5a6852fe
SP
105git-update-ref -m "resolve $merge_name: In-index merge" \
106 HEAD "$result_commit" "$head"
9c065315 107git-diff-tree -p $head $result_commit | git-apply --stat
c591412c 108dropheads