Merge branch 'kb/maint-rebase-autosquash'
[git/kirr.git] / git-mergetool.sh
blob2f8dc441c6d575323f2fad920ecdb7b20e1445e6
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] [-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
17 require_work_tree
19 # Returns true if the mode reflects a symlink
20 is_symlink () {
21 test "$1" = 120000
24 local_present () {
25 test -n "$local_mode"
28 remote_present () {
29 test -n "$remote_mode"
32 base_present () {
33 test -n "$base_mode"
36 cleanup_temp_files () {
37 if test "$1" = --save-backup ; then
38 mv -- "$BACKUP" "$MERGED.orig"
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
40 else
41 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
45 describe_file () {
46 mode="$1"
47 branch="$2"
48 file="$3"
50 printf " {%s}: " "$branch"
51 if test -z "$mode"; then
52 echo "deleted"
53 elif is_symlink "$mode" ; then
54 echo "a symbolic link -> '$(cat "$file")'"
55 else
56 if base_present; then
57 echo "modified"
58 else
59 echo "created"
65 resolve_symlink_merge () {
66 while true; do
67 printf "Use (l)ocal or (r)emote, or (a)bort? "
68 read ans
69 case "$ans" in
70 [lL]*)
71 git checkout-index -f --stage=2 -- "$MERGED"
72 git add -- "$MERGED"
73 cleanup_temp_files --save-backup
74 return 0
76 [rR]*)
77 git checkout-index -f --stage=3 -- "$MERGED"
78 git add -- "$MERGED"
79 cleanup_temp_files --save-backup
80 return 0
82 [aA]*)
83 return 1
85 esac
86 done
89 resolve_deleted_merge () {
90 while true; do
91 if base_present; then
92 printf "Use (m)odified or (d)eleted file, or (a)bort? "
93 else
94 printf "Use (c)reated or (d)eleted file, or (a)bort? "
96 read ans
97 case "$ans" in
98 [mMcC]*)
99 git add -- "$MERGED"
100 cleanup_temp_files --save-backup
101 return 0
103 [dD]*)
104 git rm -- "$MERGED" > /dev/null
105 cleanup_temp_files
106 return 0
108 [aA]*)
109 return 1
111 esac
112 done
115 checkout_staged_file () {
116 tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^ ]*\) ')
118 if test $? -eq 0 -a -n "$tmpfile" ; then
119 mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
123 merge_file () {
124 MERGED="$1"
126 f=$(git ls-files -u -- "$MERGED")
127 if test -z "$f" ; then
128 if test ! -f "$MERGED" ; then
129 echo "$MERGED: file not found"
130 else
131 echo "$MERGED: file does not need merging"
133 return 1
136 ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
137 BACKUP="./$MERGED.BACKUP.$ext"
138 LOCAL="./$MERGED.LOCAL.$ext"
139 REMOTE="./$MERGED.REMOTE.$ext"
140 BASE="./$MERGED.BASE.$ext"
142 mv -- "$MERGED" "$BACKUP"
143 cp -- "$BACKUP" "$MERGED"
145 base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
146 local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
147 remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
149 base_present && checkout_staged_file 1 "$MERGED" "$BASE"
150 local_present && checkout_staged_file 2 "$MERGED" "$LOCAL"
151 remote_present && checkout_staged_file 3 "$MERGED" "$REMOTE"
153 if test -z "$local_mode" -o -z "$remote_mode"; then
154 echo "Deleted merge conflict for '$MERGED':"
155 describe_file "$local_mode" "local" "$LOCAL"
156 describe_file "$remote_mode" "remote" "$REMOTE"
157 resolve_deleted_merge
158 return
161 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
162 echo "Symbolic link merge conflict for '$MERGED':"
163 describe_file "$local_mode" "local" "$LOCAL"
164 describe_file "$remote_mode" "remote" "$REMOTE"
165 resolve_symlink_merge
166 return
169 echo "Normal merge conflict for '$MERGED':"
170 describe_file "$local_mode" "local" "$LOCAL"
171 describe_file "$remote_mode" "remote" "$REMOTE"
172 if "$prompt" = true; then
173 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
174 read ans
177 if base_present; then
178 present=true
179 else
180 present=false
183 if ! run_merge_tool "$merge_tool" "$present"; then
184 echo "merge of $MERGED failed" 1>&2
185 mv -- "$BACKUP" "$MERGED"
187 if test "$merge_keep_temporaries" = "false"; then
188 cleanup_temp_files
191 return 1
194 if test "$merge_keep_backup" = "true"; then
195 mv -- "$BACKUP" "$MERGED.orig"
196 else
197 rm -- "$BACKUP"
200 git add -- "$MERGED"
201 cleanup_temp_files
202 return 0
205 prompt=$(git config --bool mergetool.prompt || echo true)
207 while test $# != 0
209 case "$1" in
210 -t|--tool*)
211 case "$#,$1" in
212 *,*=*)
213 merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
215 1,*)
216 usage ;;
218 merge_tool="$2"
219 shift ;;
220 esac
222 -y|--no-prompt)
223 prompt=false
225 --prompt)
226 prompt=true
229 shift
230 break
233 usage
236 break
238 esac
239 shift
240 done
242 prompt_after_failed_merge() {
243 while true; do
244 printf "Continue merging other unresolved paths (y/n) ? "
245 read ans
246 case "$ans" in
248 [yY]*)
249 return 0
252 [nN]*)
253 return 1
255 esac
256 done
259 if test -z "$merge_tool"; then
260 merge_tool=$(get_merge_tool "$merge_tool") || exit
262 merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
263 merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
265 last_status=0
266 rollup_status=0
267 rerere=false
269 files_to_merge() {
270 if test "$rerere" = true
271 then
272 git rerere status
273 else
274 git ls-files -u | sed -e 's/^[^ ]* //' | sort -u
279 if test $# -eq 0 ; then
280 cd_to_toplevel
282 if test -e "$GIT_DIR/MERGE_RR"
283 then
284 rerere=true
287 files=$(files_to_merge)
288 if test -z "$files" ; then
289 echo "No files need merging"
290 exit 0
293 # Save original stdin
294 exec 3<&0
296 printf "Merging:\n"
297 printf "$files\n"
299 files_to_merge |
300 while IFS= read i
302 if test $last_status -ne 0; then
303 prompt_after_failed_merge <&3 || exit 1
305 printf "\n"
306 merge_file "$i" <&3
307 last_status=$?
308 if test $last_status -ne 0; then
309 rollup_status=1
311 done
312 else
313 while test $# -gt 0; do
314 if test $last_status -ne 0; then
315 prompt_after_failed_merge || exit 1
317 printf "\n"
318 merge_file "$1"
319 last_status=$?
320 if test $last_status -ne 0; then
321 rollup_status=1
323 shift
324 done
327 exit $rollup_status