git-sh-setup: move GIT_DIR initialization into a function
[git/git.git] / git-mergetool.sh
CommitLineData
c4b4a5af
TT
1#!/bin/sh
2#
3# This program resolves merge conflicts in git
4#
5# Copyright (c) 2006 Theodore Y. Ts'o
6#
7# This file is licensed under the GPL v2, or a later version
2571ac67 8# at the discretion of Junio C Hamano.
c4b4a5af
TT
9#
10
109859e2 11USAGE='[--tool=tool] [--tool-help] [-y|--no-prompt|--prompt] [file to merge] ...'
c4b4a5af 12SUBDIRECTORY_OK=Yes
8f321a39 13OPTIONS_SPEC=
21d0ba7e 14TOOL_MODE=merge
c4b4a5af 15. git-sh-setup
21d0ba7e 16. git-mergetool--lib
c4b4a5af
TT
17require_work_tree
18
19# Returns true if the mode reflects a symlink
262c981e 20is_symlink () {
f8750a0e 21 test "$1" = 120000
c4b4a5af
TT
22}
23
ff7f089e 24is_submodule () {
f8750a0e 25 test "$1" = 160000
ff7f089e
JM
26}
27
262c981e 28local_present () {
f8750a0e 29 test -n "$local_mode"
c4b4a5af
TT
30}
31
262c981e 32remote_present () {
f8750a0e 33 test -n "$remote_mode"
c4b4a5af
TT
34}
35
262c981e 36base_present () {
f8750a0e 37 test -n "$base_mode"
c4b4a5af
TT
38}
39
40cleanup_temp_files () {
f8750a0e
JH
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"
48 fi
c4b4a5af
TT
49}
50
262c981e 51describe_file () {
f8750a0e
JH
52 mode="$1"
53 branch="$2"
54 file="$3"
55
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"
c4b4a5af 69 else
f8750a0e 70 echo "created file"
c4b4a5af 71 fi
c4b4a5af
TT
72}
73
c4b4a5af 74resolve_symlink_merge () {
f8750a0e
JH
75 while true
76 do
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
85 ;;
86 [rR]*)
87 git checkout-index -f --stage=3 -- "$MERGED"
88 git add -- "$MERGED"
89 cleanup_temp_files --save-backup
90 return 0
91 ;;
92 [aA]*)
93 return 1
94 ;;
95 esac
c4b4a5af
TT
96 done
97}
98
99resolve_deleted_merge () {
f8750a0e
JH
100 while true
101 do
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? "
107 fi
108 read ans || return 1
109 case "$ans" in
110 [mMcC]*)
111 git add -- "$MERGED"
112 cleanup_temp_files --save-backup
113 return 0
114 ;;
115 [dD]*)
116 git rm -- "$MERGED" > /dev/null
117 cleanup_temp_files
118 return 0
119 ;;
120 [aA]*)
121 return 1
122 ;;
123 esac
c4b4a5af
TT
124 done
125}
126
ff7f089e 127resolve_submodule_merge () {
f8750a0e
JH
128 while true
129 do
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
145 fi
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"
152 fi
153 return 0
154 ;;
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"
166 fi
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"
177 fi
ff7f089e 178 cleanup_temp_files --save-backup
f8750a0e
JH
179 return 0
180 ;;
181 [aA]*)
182 return 1
183 ;;
184 esac
ff7f089e
JM
185 done
186}
187
188stage_submodule () {
f8750a0e
JH
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
197 )
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
ff7f089e
JM
201}
202
0ec7b6c2 203checkout_staged_file () {
f8750a0e
JH
204 tmpfile=$(expr \
205 "$(git checkout-index --temp --stage="$1" "$2" 2>/dev/null)" \
206 : '\([^ ]*\) ')
207
208 if test $? -eq 0 -a -n "$tmpfile"
209 then
210 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
211 else
212 >"$3"
213 fi
0ec7b6c2
CB
214}
215
c4b4a5af 216merge_file () {
f8750a0e 217 MERGED="$1"
c4b4a5af 218
f8750a0e
JH
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"
227 fi
228 return 1
ce5b6d75 229 fi
c4b4a5af 230
9c66cd3b
DA
231 if BASE=$(expr "$MERGED" : '\(.*\)\.[^/]*$')
232 then
233 ext=$(expr "$MERGED" : '.*\(\.[^/]*\)$')
234 else
235 BASE=$MERGED
236 ext=
237 fi
238 BACKUP="./${BASE}_BACKUP_$$$ext"
239 LOCAL="./${BASE}_LOCAL_$$$ext"
240 REMOTE="./${BASE}_REMOTE_$$$ext"
241 BASE="./${BASE}_BASE_$$$ext"
f8750a0e
JH
242
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;}')
246
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
256 fi
257
258 mv -- "$MERGED" "$BACKUP"
259 cp -- "$BACKUP" "$MERGED"
260
261 checkout_staged_file 1 "$MERGED" "$BASE"
262 checkout_staged_file 2 "$MERGED" "$LOCAL"
263 checkout_staged_file 3 "$MERGED" "$REMOTE"
264
265 if test -z "$local_mode" -o -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
272 fi
273
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
281 fi
282
283 echo "Normal merge conflict for '$MERGED':"
c4b4a5af
TT
284 describe_file "$local_mode" "local" "$LOCAL"
285 describe_file "$remote_mode" "remote" "$REMOTE"
f8750a0e
JH
286 if "$prompt" = true
287 then
288 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
289 read ans || return 1
162eba8b
CB
290 fi
291
f8750a0e
JH
292 if base_present
293 then
294 present=true
295 else
296 present=false
297 fi
298
299 if ! run_merge_tool "$merge_tool" "$present"
300 then
301 echo "merge of $MERGED failed" 1>&2
302 mv -- "$BACKUP" "$MERGED"
303
304 if test "$merge_keep_temporaries" = "false"
305 then
306 cleanup_temp_files
307 fi
44c36d1c 308
f8750a0e
JH
309 return 1
310 fi
44c36d1c 311
f8750a0e
JH
312 if test "$merge_keep_backup" = "true"
313 then
314 mv -- "$BACKUP" "$MERGED.orig"
315 else
316 rm -- "$BACKUP"
317 fi
318
319 git add -- "$MERGED"
320 cleanup_temp_files
321 return 0
c4b4a5af
TT
322}
323
682b451f
CB
324prompt=$(git config --bool mergetool.prompt || echo true)
325
822f7c73 326while test $# != 0
c4b4a5af 327do
f8750a0e 328 case "$1" in
109859e2
JH
329 --tool-help)
330 show_tool_help
331 ;;
c4b4a5af 332 -t|--tool*)
f8750a0e 333 case "$#,$1" in
c4b4a5af 334 *,*=*)
f8750a0e
JH
335 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
336 ;;
c4b4a5af 337 1,*)
f8750a0e 338 usage ;;
c4b4a5af 339 *)
f8750a0e
JH
340 merge_tool="$2"
341 shift ;;
342 esac
343 ;;
682b451f 344 -y|--no-prompt)
f8750a0e
JH
345 prompt=false
346 ;;
682b451f 347 --prompt)
f8750a0e
JH
348 prompt=true
349 ;;
c4b4a5af 350 --)
f8750a0e
JH
351 shift
352 break
353 ;;
c4b4a5af 354 -*)
f8750a0e 355 usage
b0169d84 356 ;;
f8750a0e
JH
357 *)
358 break
b0169d84
CB
359 ;;
360 esac
f8750a0e
JH
361 shift
362done
363
364prompt_after_failed_merge () {
365 while true
366 do
367 printf "Continue merging other unresolved paths (y/n) ? "
368 read ans || return 1
369 case "$ans" in
370 [yY]*)
371 return 0
372 ;;
373 [nN]*)
374 return 1
375 ;;
376 esac
377 done
b0169d84 378}
e3fa2c76 379
f8750a0e
JH
380if test -z "$merge_tool"
381then
382 merge_tool=$(get_merge_tool "$merge_tool") || exit
47d65924 383fi
70af4e9b 384merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
21d0ba7e 385merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
c4b4a5af 386
b0169d84
CB
387last_status=0
388rollup_status=0
3e8e691a 389files=
c4b4a5af 390
f8750a0e
JH
391if test $# -eq 0
392then
393 cd_to_toplevel
bb0a484e 394
f8750a0e
JH
395 if test -e "$GIT_DIR/MERGE_RR"
396 then
397 files=$(git rerere remaining)
398 else
399 files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u)
400 fi
3e8e691a 401else
f8750a0e 402 files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
3e8e691a 403fi
af314714 404
f8750a0e
JH
405if test -z "$files"
406then
407 echo "No files need merging"
408 exit 0
3e8e691a 409fi
af314714 410
3e8e691a 411printf "Merging:\n"
59cf706b 412printf "%s\n" "$files"
bb0a484e 413
3e8e691a 414IFS='
6d9990a9
JH
415'
416for i in $files
3e8e691a 417do
f8750a0e
JH
418 if test $last_status -ne 0
419 then
420 prompt_after_failed_merge || exit 1
421 fi
422 printf "\n"
423 merge_file "$i"
424 last_status=$?
425 if test $last_status -ne 0
426 then
427 rollup_status=1
428 fi
3e8e691a 429done
b0169d84
CB
430
431exit $rollup_status