Merge branch 'da/mergetool-tool-help'
[alt-git.git] / git-mergetool.sh
blobec644d542d9754fa09ac7fb1b0f1f932a9bedbed
1 #!/bin/sh
3 # This program resolves merge conflicts in git
5 # Copyright (c) 2006 Theodore Y. Ts'o
7 # This file is licensed under the GPL v2, or a later version
8 # at the discretion of Junio C Hamano.
11 USAGE='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 NONGIT_OK=Yes
14 OPTIONS_SPEC=
15 TOOL_MODE=merge
16 . git-sh-setup
17 . git-mergetool--lib
19 # Returns true if the mode reflects a symlink
20 is_symlink () {
21 test "$1" = 120000
24 is_submodule () {
25 test "$1" = 160000
28 local_present () {
29 test -n "$local_mode"
32 remote_present () {
33 test -n "$remote_mode"
36 base_present () {
37 test -n "$base_mode"
40 cleanup_temp_files () {
41 if test "$1" = --save-backup
42 then
43 rm -rf -- "$MERGED.orig"
44 test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig"
45 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
46 else
47 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
51 describe_file () {
52 mode="$1"
53 branch="$2"
54 file="$3"
56 printf " {%s}: " "$branch"
57 if test -z "$mode"
58 then
59 echo "deleted"
60 elif is_symlink "$mode"
61 then
62 echo "a symbolic link -> '$(cat "$file")'"
63 elif is_submodule "$mode"
64 then
65 echo "submodule commit $file"
66 elif base_present
67 then
68 echo "modified file"
69 else
70 echo "created file"
74 resolve_symlink_merge () {
75 while true
77 printf "Use (l)ocal or (r)emote, or (a)bort? "
78 read ans || return 1
79 case "$ans" in
80 [lL]*)
81 git checkout-index -f --stage=2 -- "$MERGED"
82 git add -- "$MERGED"
83 cleanup_temp_files --save-backup
84 return 0
86 [rR]*)
87 git checkout-index -f --stage=3 -- "$MERGED"
88 git add -- "$MERGED"
89 cleanup_temp_files --save-backup
90 return 0
92 [aA]*)
93 return 1
95 esac
96 done
99 resolve_deleted_merge () {
100 while true
102 if base_present
103 then
104 printf "Use (m)odified or (d)eleted file, or (a)bort? "
105 else
106 printf "Use (c)reated or (d)eleted file, or (a)bort? "
108 read ans || return 1
109 case "$ans" in
110 [mMcC]*)
111 git add -- "$MERGED"
112 cleanup_temp_files --save-backup
113 return 0
115 [dD]*)
116 git rm -- "$MERGED" > /dev/null
117 cleanup_temp_files
118 return 0
120 [aA]*)
121 return 1
123 esac
124 done
127 resolve_submodule_merge () {
128 while true
130 printf "Use (l)ocal or (r)emote, or (a)bort? "
131 read ans || return 1
132 case "$ans" in
133 [lL]*)
134 if ! local_present
135 then
136 if test -n "$(git ls-tree HEAD -- "$MERGED")"
137 then
138 # Local isn't present, but it's a subdirectory
139 git ls-tree --full-name -r HEAD -- "$MERGED" |
140 git update-index --index-info || exit $?
141 else
142 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
143 git update-index --force-remove "$MERGED"
144 cleanup_temp_files --save-backup
146 elif is_submodule "$local_mode"
147 then
148 stage_submodule "$MERGED" "$local_sha1"
149 else
150 git checkout-index -f --stage=2 -- "$MERGED"
151 git add -- "$MERGED"
153 return 0
155 [rR]*)
156 if ! remote_present
157 then
158 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"
159 then
160 # Remote isn't present, but it's a subdirectory
161 git ls-tree --full-name -r MERGE_HEAD -- "$MERGED" |
162 git update-index --index-info || exit $?
163 else
164 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
165 git update-index --force-remove "$MERGED"
167 elif is_submodule "$remote_mode"
168 then
169 ! is_submodule "$local_mode" &&
170 test -e "$MERGED" &&
171 mv -- "$MERGED" "$BACKUP"
172 stage_submodule "$MERGED" "$remote_sha1"
173 else
174 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
175 git checkout-index -f --stage=3 -- "$MERGED"
176 git add -- "$MERGED"
178 cleanup_temp_files --save-backup
179 return 0
181 [aA]*)
182 return 1
184 esac
185 done
188 stage_submodule () {
189 path="$1"
190 submodule_sha1="$2"
191 mkdir -p "$path" ||
192 die "fatal: unable to create directory for module at $path"
193 # Find $path relative to work tree
194 work_tree_root=$(cd_to_toplevel && pwd)
195 work_rel_path=$(cd "$path" &&
196 GIT_WORK_TREE="${work_tree_root}" git rev-parse --show-prefix
198 test -n "$work_rel_path" ||
199 die "fatal: unable to get path of module $path relative to work tree"
200 git update-index --add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
203 checkout_staged_file () {
204 tmpfile=$(expr \
205 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
206 : '\([^ ]*\) ')
208 if test $? -eq 0 && test -n "$tmpfile"
209 then
210 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
211 else
212 >"$3"
216 merge_file () {
217 MERGED="$1"
219 f=$(git ls-files -u -- "$MERGED")
220 if test -z "$f"
221 then
222 if test ! -f "$MERGED"
223 then
224 echo "$MERGED: file not found"
225 else
226 echo "$MERGED: file does not need merging"
228 return 1
231 if BASE=$(expr "$MERGED" : '\(.*\)\.[^/]*$')
232 then
233 ext=$(expr "$MERGED" : '.*\(\.[^/]*\)$')
234 else
235 BASE=$MERGED
236 ext=
238 BACKUP="./${BASE}_BACKUP_$$$ext"
239 LOCAL="./${BASE}_LOCAL_$$$ext"
240 REMOTE="./${BASE}_REMOTE_$$$ext"
241 BASE="./${BASE}_BASE_$$$ext"
243 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
244 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
245 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
247 if is_submodule "$local_mode" || is_submodule "$remote_mode"
248 then
249 echo "Submodule merge conflict for '$MERGED':"
250 local_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $2;}')
251 remote_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $2;}')
252 describe_file "$local_mode" "local" "$local_sha1"
253 describe_file "$remote_mode" "remote" "$remote_sha1"
254 resolve_submodule_merge
255 return
258 mv -- "$MERGED" "$BACKUP"
259 cp -- "$BACKUP" "$MERGED"
261 checkout_staged_file 1 "$MERGED" "$BASE"
262 checkout_staged_file 2 "$MERGED" "$LOCAL"
263 checkout_staged_file 3 "$MERGED" "$REMOTE"
265 if test -z "$local_mode" || test -z "$remote_mode"
266 then
267 echo "Deleted merge conflict for '$MERGED':"
268 describe_file "$local_mode" "local" "$LOCAL"
269 describe_file "$remote_mode" "remote" "$REMOTE"
270 resolve_deleted_merge
271 return
274 if is_symlink "$local_mode" || is_symlink "$remote_mode"
275 then
276 echo "Symbolic link merge conflict for '$MERGED':"
277 describe_file "$local_mode" "local" "$LOCAL"
278 describe_file "$remote_mode" "remote" "$REMOTE"
279 resolve_symlink_merge
280 return
283 echo "Normal merge conflict for '$MERGED':"
284 describe_file "$local_mode" "local" "$LOCAL"
285 describe_file "$remote_mode" "remote" "$REMOTE"
286 if test "$guessed_merge_tool" = true || test "$prompt" = true
287 then
288 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
289 read ans || return 1
292 if base_present
293 then
294 present=true
295 else
296 present=false
299 if ! run_merge_tool "$merge_tool" "$present"
300 then
301 echo "merge of $MERGED failed" 1>&2
302 mv -- "$BACKUP" "$MERGED"
304 if test "$merge_keep_temporaries" = "false"
305 then
306 cleanup_temp_files
309 return 1
312 if test "$merge_keep_backup" = "true"
313 then
314 mv -- "$BACKUP" "$MERGED.orig"
315 else
316 rm -- "$BACKUP"
319 git add -- "$MERGED"
320 cleanup_temp_files
321 return 0
324 prompt=$(git config --bool mergetool.prompt)
325 guessed_merge_tool=false
327 while test $# != 0
329 case "$1" in
330 --tool-help=*)
331 TOOL_MODE=${1#--tool-help=}
332 show_tool_help
334 --tool-help)
335 show_tool_help
337 -t|--tool*)
338 case "$#,$1" in
339 *,*=*)
340 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
342 1,*)
343 usage ;;
345 merge_tool="$2"
346 shift ;;
347 esac
349 -y|--no-prompt)
350 prompt=false
352 --prompt)
353 prompt=true
356 shift
357 break
360 usage
363 break
365 esac
366 shift
367 done
369 prompt_after_failed_merge () {
370 while true
372 printf "Continue merging other unresolved paths (y/n) ? "
373 read ans || return 1
374 case "$ans" in
375 [yY]*)
376 return 0
378 [nN]*)
379 return 1
381 esac
382 done
385 git_dir_init
386 require_work_tree
388 if test -z "$merge_tool"
389 then
390 # Check if a merge tool has been configured
391 merge_tool=$(get_configured_merge_tool)
392 # Try to guess an appropriate merge tool if no tool has been set.
393 if test -z "$merge_tool"
394 then
395 merge_tool=$(guess_merge_tool) || exit
396 guessed_merge_tool=true
399 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
400 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
402 last_status=0
403 rollup_status=0
404 files=
406 if test $# -eq 0
407 then
408 cd_to_toplevel
410 if test -e "$GIT_DIR/MERGE_RR"
411 then
412 files=$(git rerere remaining)
413 else
414 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
416 else
417 files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
420 if test -z "$files"
421 then
422 echo "No files need merging"
423 exit 0
426 printf "Merging:\n"
427 printf "%s\n" "$files"
429 IFS='
431 for i in $files
433 if test $last_status -ne 0
434 then
435 prompt_after_failed_merge || exit 1
437 printf "\n"
438 merge_file "$i"
439 last_status=$?
440 if test $last_status -ne 0
441 then
442 rollup_status=1
444 done
446 exit $rollup_status