Git 1.7.12
[git/git.git] / git-mergetool--lib.sh
CommitLineData
02e51243 1#!/bin/sh
21d0ba7e
DA
2# git-mergetool--lib is a library for common merge tool functions
3diff_mode() {
4 test "$TOOL_MODE" = diff
5}
6
7merge_mode() {
8 test "$TOOL_MODE" = merge
9}
10
11translate_merge_tool_path () {
bc7a96a8 12 echo "$1"
21d0ba7e
DA
13}
14
15check_unchanged () {
240dc3e8
DA
16 if test "$MERGED" -nt "$BACKUP"
17 then
21d0ba7e
DA
18 status=0
19 else
240dc3e8
DA
20 while true
21 do
21d0ba7e
DA
22 echo "$MERGED seems unchanged."
23 printf "Was the merge successful? [y/n] "
e622f41d 24 read answer || return 1
21d0ba7e
DA
25 case "$answer" in
26 y*|Y*) status=0; break ;;
27 n*|N*) status=1; break ;;
28 esac
29 done
30 fi
31}
32
bc7a96a8
DA
33valid_tool_config () {
34 if test -n "$(get_merge_tool_cmd "$1")"
35 then
36 return 0
37 else
38 return 1
39 fi
40}
41
21d0ba7e 42valid_tool () {
bc7a96a8
DA
43 setup_tool "$1" || valid_tool_config "$1"
44}
45
46setup_tool () {
21d0ba7e 47 case "$1" in
bc7a96a8
DA
48 vim*|gvim*)
49 tool=vim
21d0ba7e
DA
50 ;;
51 *)
bc7a96a8 52 tool="$1"
21d0ba7e
DA
53 ;;
54 esac
bc7a96a8
DA
55 mergetools="$(git --exec-path)/mergetools"
56
57 # Load the default definitions
58 . "$mergetools/defaults"
59 if ! test -f "$mergetools/$tool"
60 then
61 return 1
62 fi
63
64 # Load the redefined functions
65 . "$mergetools/$tool"
66
67 if merge_mode && ! can_merge
68 then
69 echo "error: '$tool' can not be used to resolve merges" >&2
70 exit 1
71 elif diff_mode && ! can_diff
72 then
73 echo "error: '$tool' can only be used to resolve merges" >&2
74 exit 1
75 fi
76 return 0
21d0ba7e
DA
77}
78
79get_merge_tool_cmd () {
47d65924 80 # Prints the custom command for a merge tool
bc7a96a8 81 merge_tool="$1"
240dc3e8
DA
82 if diff_mode
83 then
47d65924 84 echo "$(git config difftool.$merge_tool.cmd ||
285c6cbf 85 git config mergetool.$merge_tool.cmd)"
47d65924
DA
86 else
87 echo "$(git config mergetool.$merge_tool.cmd)"
88 fi
21d0ba7e
DA
89}
90
bc7a96a8 91# Entry point for running tools
21d0ba7e 92run_merge_tool () {
f9ad901f
DA
93 # If GIT_PREFIX is empty then we cannot use it in tools
94 # that expect to be able to chdir() to its value.
95 GIT_PREFIX=${GIT_PREFIX:-.}
96 export GIT_PREFIX
97
47d65924 98 merge_tool_path="$(get_merge_tool_path "$1")" || exit
21d0ba7e
DA
99 base_present="$2"
100 status=0
101
bc7a96a8
DA
102 # Bring tool-specific functions into scope
103 setup_tool "$1"
104
105 if merge_mode
106 then
107 merge_cmd "$1"
108 else
109 diff_cmd "$1"
110 fi
21d0ba7e
DA
111 return $status
112}
113
109859e2 114list_merge_tool_candidates () {
240dc3e8
DA
115 if merge_mode
116 then
21d0ba7e
DA
117 tools="tortoisemerge"
118 else
119 tools="kompare"
120 fi
240dc3e8
DA
121 if test -n "$DISPLAY"
122 then
123 if test -n "$GNOME_DESKTOP_SESSION_ID"
124 then
21d0ba7e
DA
125 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
126 else
127 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
128 fi
ffe6dc08 129 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3"
21d0ba7e 130 fi
7b104229
RS
131 case "${VISUAL:-$EDITOR}" in
132 *vim*)
21d0ba7e 133 tools="$tools vimdiff emerge"
7b104229
RS
134 ;;
135 *)
21d0ba7e 136 tools="$tools emerge vimdiff"
7b104229
RS
137 ;;
138 esac
109859e2
JH
139}
140
141guess_merge_tool () {
142 list_merge_tool_candidates
21d0ba7e
DA
143 echo >&2 "merge tool candidates: $tools"
144
145 # Loop over each candidate and stop when a valid merge tool is found.
146 for i in $tools
147 do
148 merge_tool_path="$(translate_merge_tool_path "$i")"
240dc3e8
DA
149 if type "$merge_tool_path" >/dev/null 2>&1
150 then
47d65924
DA
151 echo "$i"
152 return 0
21d0ba7e
DA
153 fi
154 done
155
47d65924
DA
156 echo >&2 "No known merge resolution program available."
157 return 1
21d0ba7e
DA
158}
159
160get_configured_merge_tool () {
161 # Diff mode first tries diff.tool and falls back to merge.tool.
162 # Merge mode only checks merge.tool
240dc3e8
DA
163 if diff_mode
164 then
47d65924
DA
165 merge_tool=$(git config diff.tool || git config merge.tool)
166 else
167 merge_tool=$(git config merge.tool)
21d0ba7e 168 fi
240dc3e8
DA
169 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
170 then
21d0ba7e
DA
171 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
172 echo >&2 "Resetting to default..."
173 return 1
174 fi
47d65924 175 echo "$merge_tool"
21d0ba7e
DA
176}
177
178get_merge_tool_path () {
179 # A merge tool has been set, so verify that it's valid.
bc7a96a8 180 merge_tool="$1"
240dc3e8
DA
181 if ! valid_tool "$merge_tool"
182 then
21d0ba7e
DA
183 echo >&2 "Unknown merge tool $merge_tool"
184 exit 1
185 fi
240dc3e8
DA
186 if diff_mode
187 then
47d65924 188 merge_tool_path=$(git config difftool."$merge_tool".path ||
285c6cbf 189 git config mergetool."$merge_tool".path)
47d65924
DA
190 else
191 merge_tool_path=$(git config mergetool."$merge_tool".path)
21d0ba7e 192 fi
240dc3e8
DA
193 if test -z "$merge_tool_path"
194 then
47d65924 195 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
21d0ba7e 196 fi
47d65924 197 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
240dc3e8
DA
198 ! type "$merge_tool_path" >/dev/null 2>&1
199 then
47d65924 200 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
285c6cbf 201 "'$merge_tool_path'"
21d0ba7e
DA
202 exit 1
203 fi
204 echo "$merge_tool_path"
205}
206
207get_merge_tool () {
21d0ba7e 208 # Check if a merge tool has been configured
bc7a96a8 209 merge_tool="$(get_configured_merge_tool)"
21d0ba7e 210 # Try to guess an appropriate merge tool if no tool has been set.
240dc3e8
DA
211 if test -z "$merge_tool"
212 then
47d65924 213 merge_tool="$(guess_merge_tool)" || exit
21d0ba7e
DA
214 fi
215 echo "$merge_tool"
216}