1748315bc2b45db2b078a3ff293f779d18f7026d
1#!/bin/sh
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 () {
12 echo "$1"
13}
14
15check_unchanged () {
16 if test "$MERGED" -nt "$BACKUP"
17 then
18 status=0
19 else
20 while true
21 do
22 echo "$MERGED seems unchanged."
23 printf "Was the merge successful? [y/n] "
24 read answer || return 1
25 case "$answer" in
26 y*|Y*) status=0; break ;;
27 n*|N*) status=1; break ;;
28 esac
29 done
30 fi
31}
32
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
42valid_tool () {
43 setup_tool "$1" || valid_tool_config "$1"
44}
45
46setup_tool () {
47 case "$1" in
48 vim*|gvim*)
49 tool=vim
50 ;;
51 *)
52 tool="$1"
53 ;;
54 esac
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
77}
78
79get_merge_tool_cmd () {
80 # Prints the custom command for a merge tool
81 merge_tool="$1"
82 if diff_mode
83 then
84 echo "$(git config difftool.$merge_tool.cmd ||
85 git config mergetool.$merge_tool.cmd)"
86 else
87 echo "$(git config mergetool.$merge_tool.cmd)"
88 fi
89}
90
91# Entry point for running tools
92run_merge_tool () {
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
98 merge_tool_path="$(get_merge_tool_path "$1")" || exit
99 base_present="$2"
100 status=0
101
102 # Bring tool-specific functions into scope
103 setup_tool "$1"
104
105 if merge_mode
106 then
107 run_merge_cmd "$1"
108 else
109 run_diff_cmd "$1"
110 fi
111 return $status
112}
113
114# Run a either a configured or built-in diff tool
115run_diff_cmd () {
116 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
117 if test -n "$merge_tool_cmd"
118 then
119 ( eval $merge_tool_cmd )
120 status=$?
121 return $status
122 else
123 diff_cmd "$1"
124 fi
125}
126
127# Run a either a configured or built-in merge tool
128run_merge_cmd () {
129 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
130 if test -n "$merge_tool_cmd"
131 then
132 trust_exit_code="$(git config --bool \
133 mergetool."$1".trustExitCode || echo false)"
134 if test "$trust_exit_code" = "false"
135 then
136 touch "$BACKUP"
137 ( eval $merge_tool_cmd )
138 status=$?
139 check_unchanged
140 else
141 ( eval $merge_tool_cmd )
142 status=$?
143 fi
144 return $status
145 else
146 merge_cmd "$1"
147 fi
148}
149
150list_merge_tool_candidates () {
151 if merge_mode
152 then
153 tools="tortoisemerge"
154 else
155 tools="kompare"
156 fi
157 if test -n "$DISPLAY"
158 then
159 if test -n "$GNOME_DESKTOP_SESSION_ID"
160 then
161 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
162 else
163 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
164 fi
165 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
166 fi
167 case "${VISUAL:-$EDITOR}" in
168 *vim*)
169 tools="$tools vimdiff emerge"
170 ;;
171 *)
172 tools="$tools emerge vimdiff"
173 ;;
174 esac
175}
176
177show_tool_help () {
178 list_merge_tool_candidates
179 unavailable= available= LF='
180'
181 for i in $tools
182 do
183 merge_tool_path=$(translate_merge_tool_path "$i")
184 if type "$merge_tool_path" >/dev/null 2>&1
185 then
186 available="$available$i$LF"
187 else
188 unavailable="$unavailable$i$LF"
189 fi
190 done
191 if test -n "$available"
192 then
193 echo "'git mergetool --tool=<tool>' may be set to one of the following:"
194 echo "$available" | sort | sed -e 's/^/ /'
195 else
196 echo "No suitable tool for 'git mergetool --tool=<tool>' found."
197 fi
198 if test -n "$unavailable"
199 then
200 echo
201 echo 'The following tools are valid, but not currently available:'
202 echo "$unavailable" | sort | sed -e 's/^/ /'
203 fi
204 if test -n "$unavailable$available"
205 then
206 echo
207 echo "Some of the tools listed above only work in a windowed"
208 echo "environment. If run in a terminal-only session, they will fail."
209 fi
210 exit 0
211}
212
213guess_merge_tool () {
214 list_merge_tool_candidates
215 echo >&2 "merge tool candidates: $tools"
216
217 # Loop over each candidate and stop when a valid merge tool is found.
218 for i in $tools
219 do
220 merge_tool_path="$(translate_merge_tool_path "$i")"
221 if type "$merge_tool_path" >/dev/null 2>&1
222 then
223 echo "$i"
224 return 0
225 fi
226 done
227
228 echo >&2 "No known merge resolution program available."
229 return 1
230}
231
232get_configured_merge_tool () {
233 # Diff mode first tries diff.tool and falls back to merge.tool.
234 # Merge mode only checks merge.tool
235 if diff_mode
236 then
237 merge_tool=$(git config diff.tool || git config merge.tool)
238 else
239 merge_tool=$(git config merge.tool)
240 fi
241 if test -n "$merge_tool" && ! valid_tool "$merge_tool"
242 then
243 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
244 echo >&2 "Resetting to default..."
245 return 1
246 fi
247 echo "$merge_tool"
248}
249
250get_merge_tool_path () {
251 # A merge tool has been set, so verify that it's valid.
252 merge_tool="$1"
253 if ! valid_tool "$merge_tool"
254 then
255 echo >&2 "Unknown merge tool $merge_tool"
256 exit 1
257 fi
258 if diff_mode
259 then
260 merge_tool_path=$(git config difftool."$merge_tool".path ||
261 git config mergetool."$merge_tool".path)
262 else
263 merge_tool_path=$(git config mergetool."$merge_tool".path)
264 fi
265 if test -z "$merge_tool_path"
266 then
267 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
268 fi
269 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
270 ! type "$merge_tool_path" >/dev/null 2>&1
271 then
272 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
273 "'$merge_tool_path'"
274 exit 1
275 fi
276 echo "$merge_tool_path"
277}
278
279get_merge_tool () {
280 # Check if a merge tool has been configured
281 merge_tool="$(get_configured_merge_tool)"
282 # Try to guess an appropriate merge tool if no tool has been set.
283 if test -z "$merge_tool"
284 then
285 merge_tool="$(guess_merge_tool)" || exit
286 fi
287 echo "$merge_tool"
288}