Merge branch 'br/imap-send-simplify-tunnel-child-process' into jch
[git/jrn.git] / git-mergetool.sh
blobd32b6634919464ccf15ded7b28858e27b84f513f
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 OPTIONS_SPEC=
14 TOOL_MODE=merge
15 . git-sh-setup
16 . git-mergetool--lib
18 # Returns true if the mode reflects a symlink
19 is_symlink () {
20 test "$1" = 120000
23 is_submodule () {
24 test "$1" = 160000
27 local_present () {
28 test -n "$local_mode"
31 remote_present () {
32 test -n "$remote_mode"
35 base_present () {
36 test -n "$base_mode"
39 cleanup_temp_files () {
40 if test "$1" = --save-backup
41 then
42 rm -rf -- "$MERGED.orig"
43 test -e "$BACKUP" && mv -- "$BACKUP" "$MERGED.orig"
44 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
45 else
46 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
50 describe_file () {
51 mode="$1"
52 branch="$2"
53 file="$3"
55 printf " {%s}: " "$branch"
56 if test -z "$mode"
57 then
58 echo "deleted"
59 elif is_symlink "$mode"
60 then
61 echo "a symbolic link -> '$(cat "$file")'"
62 elif is_submodule "$mode"
63 then
64 echo "submodule commit $file"
65 elif base_present
66 then
67 echo "modified file"
68 else
69 echo "created file"
73 resolve_symlink_merge () {
74 while true
76 printf "Use (l)ocal or (r)emote, or (a)bort? "
77 read ans || return 1
78 case "$ans" in
79 [lL]*)
80 git checkout-index -f --stage=2 -- "$MERGED"
81 git add -- "$MERGED"
82 cleanup_temp_files --save-backup
83 return 0
85 [rR]*)
86 git checkout-index -f --stage=3 -- "$MERGED"
87 git add -- "$MERGED"
88 cleanup_temp_files --save-backup
89 return 0
91 [aA]*)
92 return 1
94 esac
95 done
98 resolve_deleted_merge () {
99 while true
101 if base_present
102 then
103 printf "Use (m)odified or (d)eleted file, or (a)bort? "
104 else
105 printf "Use (c)reated or (d)eleted file, or (a)bort? "
107 read ans || return 1
108 case "$ans" in
109 [mMcC]*)
110 git add -- "$MERGED"
111 cleanup_temp_files --save-backup
112 return 0
114 [dD]*)
115 git rm -- "$MERGED" > /dev/null
116 cleanup_temp_files
117 return 0
119 [aA]*)
120 return 1
122 esac
123 done
126 resolve_submodule_merge () {
127 while true
129 printf "Use (l)ocal or (r)emote, or (a)bort? "
130 read ans || return 1
131 case "$ans" in
132 [lL]*)
133 if ! local_present
134 then
135 if test -n "$(git ls-tree HEAD -- "$MERGED")"
136 then
137 # Local isn't present, but it's a subdirectory
138 git ls-tree --full-name -r HEAD -- "$MERGED" |
139 git update-index --index-info || exit $?
140 else
141 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
142 git update-index --force-remove "$MERGED"
143 cleanup_temp_files --save-backup
145 elif is_submodule "$local_mode"
146 then
147 stage_submodule "$MERGED" "$local_sha1"
148 else
149 git checkout-index -f --stage=2 -- "$MERGED"
150 git add -- "$MERGED"
152 return 0
154 [rR]*)
155 if ! remote_present
156 then
157 if test -n "$(git ls-tree MERGE_HEAD -- "$MERGED")"
158 then
159 # Remote isn't present, but it's a subdirectory
160 git ls-tree --full-name -r MERGE_HEAD -- "$MERGED" |
161 git update-index --index-info || exit $?
162 else
163 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
164 git update-index --force-remove "$MERGED"
166 elif is_submodule "$remote_mode"
167 then
168 ! is_submodule "$local_mode" &&
169 test -e "$MERGED" &&
170 mv -- "$MERGED" "$BACKUP"
171 stage_submodule "$MERGED" "$remote_sha1"
172 else
173 test -e "$MERGED" && mv -- "$MERGED" "$BACKUP"
174 git checkout-index -f --stage=3 -- "$MERGED"
175 git add -- "$MERGED"
177 cleanup_temp_files --save-backup
178 return 0
180 [aA]*)
181 return 1
183 esac
184 done
187 stage_submodule () {
188 path="$1"
189 submodule_sha1="$2"
190 mkdir -p "$path" ||
191 die "fatal: unable to create directory for module at $path"
192 # Find $path relative to work tree
193 work_tree_root=$(cd_to_toplevel && pwd)
194 work_rel_path=$(cd "$path" &&
195 GIT_WORK_TREE="${work_tree_root}" git rev-parse --show-prefix
197 test -n "$work_rel_path" ||
198 die "fatal: unable to get path of module $path relative to work tree"
199 git update-index --add --replace --cacheinfo 160000 "$submodule_sha1" "${work_rel_path%/}" || die
202 checkout_staged_file () {
203 tmpfile=$(expr \
204 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
205 : '\([^ ]*\) ')
207 if test $? -eq 0 && test -n "$tmpfile"
208 then
209 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
210 else
211 >"$3"
215 merge_file () {
216 MERGED="$1"
218 f=$(git ls-files -u -- "$MERGED")
219 if test -z "$f"
220 then
221 if test ! -f "$MERGED"
222 then
223 echo "$MERGED: file not found"
224 else
225 echo "$MERGED: file does not need merging"
227 return 1
230 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
231 BACKUP="./$MERGED.BACKUP.$ext"
232 LOCAL="./$MERGED.LOCAL.$ext"
233 REMOTE="./$MERGED.REMOTE.$ext"
234 BASE="./$MERGED.BASE.$ext"
236 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
237 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
238 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
240 if is_submodule "$local_mode" || is_submodule "$remote_mode"
241 then
242 echo "Submodule merge conflict for '$MERGED':"
243 local_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $2;}')
244 remote_sha1=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $2;}')
245 describe_file "$local_mode" "local" "$local_sha1"
246 describe_file "$remote_mode" "remote" "$remote_sha1"
247 resolve_submodule_merge
248 return
251 mv -- "$MERGED" "$BACKUP"
252 cp -- "$BACKUP" "$MERGED"
254 checkout_staged_file 1 "$MERGED" "$BASE"
255 checkout_staged_file 2 "$MERGED" "$LOCAL"
256 checkout_staged_file 3 "$MERGED" "$REMOTE"
258 if test -z "$local_mode" || test -z "$remote_mode"
259 then
260 echo "Deleted merge conflict for '$MERGED':"
261 describe_file "$local_mode" "local" "$LOCAL"
262 describe_file "$remote_mode" "remote" "$REMOTE"
263 resolve_deleted_merge
264 return
267 if is_symlink "$local_mode" || is_symlink "$remote_mode"
268 then
269 echo "Symbolic link merge conflict for '$MERGED':"
270 describe_file "$local_mode" "local" "$LOCAL"
271 describe_file "$remote_mode" "remote" "$REMOTE"
272 resolve_symlink_merge
273 return
276 echo "Normal merge conflict for '$MERGED':"
277 describe_file "$local_mode" "local" "$LOCAL"
278 describe_file "$remote_mode" "remote" "$REMOTE"
279 if test "$guessed_merge_tool" = true || test "$prompt" = true
280 then
281 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
282 read ans || return 1
285 if base_present
286 then
287 present=true
288 else
289 present=false
292 if ! run_merge_tool "$merge_tool" "$present"
293 then
294 echo "merge of $MERGED failed" 1>&2
295 mv -- "$BACKUP" "$MERGED"
297 if test "$merge_keep_temporaries" = "false"
298 then
299 cleanup_temp_files
302 return 1
305 if test "$merge_keep_backup" = "true"
306 then
307 mv -- "$BACKUP" "$MERGED.orig"
308 else
309 rm -- "$BACKUP"
312 git add -- "$MERGED"
313 cleanup_temp_files
314 return 0
317 prompt=$(git config --bool mergetool.prompt)
318 guessed_merge_tool=false
320 while test $# != 0
322 case "$1" in
323 --tool-help*)
324 case "$#,$1" in
325 1,*=*)
326 TOOL_MODE=$(expr "z$1" : 'z-[^=]*=\(.*\)')
328 1,--tool-help)
331 usage
333 esac
334 show_tool_help
336 -t|--tool*)
337 case "$#,$1" in
338 *,*=*)
339 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
341 1,*)
342 usage ;;
344 merge_tool="$2"
345 shift ;;
346 esac
348 -y|--no-prompt)
349 prompt=false
351 --prompt)
352 prompt=true
355 shift
356 break
359 usage
362 break
364 esac
365 shift
366 done
368 prompt_after_failed_merge () {
369 while true
371 printf "Continue merging other unresolved paths (y/n) ? "
372 read ans || return 1
373 case "$ans" in
374 [yY]*)
375 return 0
377 [nN]*)
378 return 1
380 esac
381 done
384 require_work_tree
386 if test -z "$merge_tool"
387 then
388 # Check if a merge tool has been configured
389 merge_tool=$(get_configured_merge_tool)
390 # Try to guess an appropriate merge tool if no tool has been set.
391 if test -z "$merge_tool"
392 then
393 merge_tool=$(guess_merge_tool) || exit
394 guessed_merge_tool=true
397 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
398 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
400 last_status=0
401 rollup_status=0
402 files=
404 if test $# -eq 0
405 then
406 cd_to_toplevel
408 if test -e "$GIT_DIR/MERGE_RR"
409 then
410 files=$(git rerere remaining)
411 else
412 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
414 else
415 files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
418 if test -z "$files"
419 then
420 echo "No files need merging"
421 exit 0
424 printf "Merging:\n"
425 printf "%s\n" "$files"
427 IFS='
429 for i in $files
431 if test $last_status -ne 0
432 then
433 prompt_after_failed_merge || exit 1
435 printf "\n"
436 merge_file "$i"
437 last_status=$?
438 if test $last_status -ne 0
439 then
440 rollup_status=1
442 done
444 exit $rollup_status