refs: move head_ref{,_submodule} to the common code
[git.git] / git-mergetool--lib.sh
blob54ac8e484674710ca21ea0038d07dcbefbe2fb1e
1 # git-mergetool--lib is a shell library for common merge tool functions
3 : ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
5 IFS='
8 mode_ok () {
9 if diff_mode
10 then
11 can_diff
12 elif merge_mode
13 then
14 can_merge
15 else
16 false
20 is_available () {
21 merge_tool_path=$(translate_merge_tool_path "$1") &&
22 type "$merge_tool_path" >/dev/null 2>&1
25 list_config_tools () {
26 section=$1
27 line_prefix=${2:-}
29 git config --get-regexp $section'\..*\.cmd' |
30 while read -r key value
32 toolname=${key#$section.}
33 toolname=${toolname%.cmd}
35 printf "%s%s\n" "$line_prefix" "$toolname"
36 done
39 show_tool_names () {
40 condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
41 not_found_msg=${4:-}
42 extra_content=${5:-}
44 shown_any=
45 ( cd "$MERGE_TOOLS_DIR" && ls ) | {
46 while read toolname
48 if setup_tool "$toolname" 2>/dev/null &&
49 (eval "$condition" "$toolname")
50 then
51 if test -n "$preamble"
52 then
53 printf "%s\n" "$preamble"
54 preamble=
56 shown_any=yes
57 printf "%s%s\n" "$per_line_prefix" "$toolname"
59 done
61 if test -n "$extra_content"
62 then
63 if test -n "$preamble"
64 then
65 # Note: no '\n' here since we don't want a
66 # blank line if there is no initial content.
67 printf "%s" "$preamble"
68 preamble=
70 shown_any=yes
71 printf "\n%s\n" "$extra_content"
74 if test -n "$preamble" && test -n "$not_found_msg"
75 then
76 printf "%s\n" "$not_found_msg"
79 test -n "$shown_any"
83 diff_mode() {
84 test "$TOOL_MODE" = diff
87 merge_mode() {
88 test "$TOOL_MODE" = merge
91 translate_merge_tool_path () {
92 echo "$1"
95 check_unchanged () {
96 if test "$MERGED" -nt "$BACKUP"
97 then
98 return 0
99 else
100 while true
102 echo "$MERGED seems unchanged."
103 printf "Was the merge successful? [y/n] "
104 read answer || return 1
105 case "$answer" in
106 y*|Y*) return 0 ;;
107 n*|N*) return 1 ;;
108 esac
109 done
113 valid_tool () {
114 setup_tool "$1" && return 0
115 cmd=$(get_merge_tool_cmd "$1")
116 test -n "$cmd"
119 setup_user_tool () {
120 merge_tool_cmd=$(get_merge_tool_cmd "$tool")
121 test -n "$merge_tool_cmd" || return 1
123 diff_cmd () {
124 ( eval $merge_tool_cmd )
127 merge_cmd () {
128 trust_exit_code=$(git config --bool \
129 "mergetool.$1.trustExitCode" || echo false)
130 if test "$trust_exit_code" = "false"
131 then
132 touch "$BACKUP"
133 ( eval $merge_tool_cmd )
134 check_unchanged
135 else
136 ( eval $merge_tool_cmd )
141 setup_tool () {
142 tool="$1"
144 # Fallback definitions, to be overridden by tools.
145 can_merge () {
146 return 0
149 can_diff () {
150 return 0
153 diff_cmd () {
154 return 1
157 merge_cmd () {
158 return 1
161 translate_merge_tool_path () {
162 echo "$1"
165 if ! test -f "$MERGE_TOOLS_DIR/$tool"
166 then
167 setup_user_tool
168 return $?
171 # Load the redefined functions
172 . "$MERGE_TOOLS_DIR/$tool"
173 # Now let the user override the default command for the tool. If
174 # they have not done so then this will return 1 which we ignore.
175 setup_user_tool
177 if merge_mode && ! can_merge
178 then
179 echo "error: '$tool' can not be used to resolve merges" >&2
180 return 1
181 elif diff_mode && ! can_diff
182 then
183 echo "error: '$tool' can only be used to resolve merges" >&2
184 return 1
186 return 0
189 get_merge_tool_cmd () {
190 merge_tool="$1"
191 if diff_mode
192 then
193 git config "difftool.$merge_tool.cmd" ||
194 git config "mergetool.$merge_tool.cmd"
195 else
196 git config "mergetool.$merge_tool.cmd"
200 # Entry point for running tools
201 run_merge_tool () {
202 # If GIT_PREFIX is empty then we cannot use it in tools
203 # that expect to be able to chdir() to its value.
204 GIT_PREFIX=${GIT_PREFIX:-.}
205 export GIT_PREFIX
207 merge_tool_path=$(get_merge_tool_path "$1") || exit
208 base_present="$2"
210 # Bring tool-specific functions into scope
211 setup_tool "$1" || return 1
213 if merge_mode
214 then
215 run_merge_cmd "$1"
216 else
217 run_diff_cmd "$1"
221 # Run a either a configured or built-in diff tool
222 run_diff_cmd () {
223 diff_cmd "$1"
226 # Run a either a configured or built-in merge tool
227 run_merge_cmd () {
228 merge_cmd "$1"
231 list_merge_tool_candidates () {
232 if merge_mode
233 then
234 tools="tortoisemerge"
235 else
236 tools="kompare"
238 if test -n "$DISPLAY"
239 then
240 if test -n "$GNOME_DESKTOP_SESSION_ID"
241 then
242 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
243 else
244 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
246 tools="$tools gvimdiff diffuse diffmerge ecmerge"
247 tools="$tools p4merge araxis bc codecompare"
249 case "${VISUAL:-$EDITOR}" in
250 *vim*)
251 tools="$tools vimdiff emerge"
254 tools="$tools emerge vimdiff"
256 esac
259 show_tool_help () {
260 tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
262 tab=' '
263 LF='
265 any_shown=no
267 cmd_name=${TOOL_MODE}tool
268 config_tools=$({
269 diff_mode && list_config_tools difftool "$tab$tab"
270 list_config_tools mergetool "$tab$tab"
271 } | sort)
272 extra_content=
273 if test -n "$config_tools"
274 then
275 extra_content="${tab}user-defined:${LF}$config_tools"
278 show_tool_names 'mode_ok && is_available' "$tab$tab" \
279 "$tool_opt may be set to one of the following:" \
280 "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
281 "$extra_content" &&
282 any_shown=yes
284 show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
285 "${LF}The following tools are valid, but not currently available:" &&
286 any_shown=yes
288 if test "$any_shown" = yes
289 then
290 echo
291 echo "Some of the tools listed above only work in a windowed"
292 echo "environment. If run in a terminal-only session, they will fail."
294 exit 0
297 guess_merge_tool () {
298 list_merge_tool_candidates
299 cat >&2 <<-EOF
301 This message is displayed because '$TOOL_MODE.tool' is not configured.
302 See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
303 'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
304 $tools
307 # Loop over each candidate and stop when a valid merge tool is found.
308 IFS=' '
309 for tool in $tools
311 is_available "$tool" && echo "$tool" && return 0
312 done
314 echo >&2 "No known ${TOOL_MODE} tool is available."
315 return 1
318 get_configured_merge_tool () {
319 # Diff mode first tries diff.tool and falls back to merge.tool.
320 # Merge mode only checks merge.tool
321 if diff_mode
322 then
323 merge_tool=$(git config diff.tool || git config merge.tool)
324 else
325 merge_tool=$(git config merge.tool)
327 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
328 then
329 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
330 echo >&2 "Resetting to default..."
331 return 1
333 echo "$merge_tool"
336 get_merge_tool_path () {
337 # A merge tool has been set, so verify that it's valid.
338 merge_tool="$1"
339 if ! valid_tool "$merge_tool"
340 then
341 echo >&2 "Unknown merge tool $merge_tool"
342 exit 1
344 if diff_mode
345 then
346 merge_tool_path=$(git config difftool."$merge_tool".path ||
347 git config mergetool."$merge_tool".path)
348 else
349 merge_tool_path=$(git config mergetool."$merge_tool".path)
351 if test -z "$merge_tool_path"
352 then
353 merge_tool_path=$(translate_merge_tool_path "$merge_tool")
355 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
356 ! type "$merge_tool_path" >/dev/null 2>&1
357 then
358 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
359 "'$merge_tool_path'"
360 exit 1
362 echo "$merge_tool_path"
365 get_merge_tool () {
366 # Check if a merge tool has been configured
367 merge_tool=$(get_configured_merge_tool)
368 # Try to guess an appropriate merge tool if no tool has been set.
369 if test -z "$merge_tool"
370 then
371 merge_tool=$(guess_merge_tool) || exit
373 echo "$merge_tool"