fast-import: fix compilation on MinGW
[git/platforms.git] / git-mergetool.sh
blob54f6d58a847ca3788a573309bdf7772bd6d85a1b
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] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 . git-sh-setup
14 require_work_tree
16 KDIFF3=kdiff3
17 KDIFF3SEPARATOR=--
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" "$path.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 -- "$path"
72 git add -- "$path"
73 cleanup_temp_files --save-backup
74 return
76 [rR]*)
77 git checkout-index -f --stage=3 -- "$path"
78 git add -- "$path"
79 cleanup_temp_files --save-backup
80 return
82 [aA]*)
83 exit 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 -- "$path"
100 cleanup_temp_files --save-backup
101 return
103 [dD]*)
104 git rm -- "$path" > /dev/null
105 cleanup_temp_files
106 return
108 [aA]*)
109 exit 1
111 esac
112 done
115 check_unchanged () {
116 if test "$path" -nt "$BACKUP" ; then
117 status=0;
118 else
119 while true; do
120 echo "$path seems unchanged."
121 printf "Was the merge successful? [y/n] "
122 read answer < /dev/tty
123 case "$answer" in
124 y*|Y*) status=0; break ;;
125 n*|N*) status=1; break ;;
126 esac
127 done
131 save_backup () {
132 if test "$status" -eq 0; then
133 mv -- "$BACKUP" "$path.orig"
137 remove_backup () {
138 if test "$status" -eq 0; then
139 rm "$BACKUP"
143 merge_file () {
144 path="$1"
146 f=`git ls-files -u -- "$path"`
147 if test -z "$f" ; then
148 if test ! -f "$path" ; then
149 echo "$path: file not found"
150 else
151 echo "$path: file does not need merging"
153 exit 1
156 BACKUP="$path.BACKUP.$$"
157 LOCAL="$path.LOCAL.$$"
158 REMOTE="$path.REMOTE.$$"
159 BASE="$path.BASE.$$"
161 mv -- "$path" "$BACKUP"
162 cp -- "$BACKUP" "$path"
164 base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
165 local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
166 remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
168 base_present && git cat-file blob ":1:$path" > "$BASE" 2>/dev/null
169 local_present && git cat-file blob ":2:$path" > "$LOCAL" 2>/dev/null
170 remote_present && git cat-file blob ":3:$path" > "$REMOTE" 2>/dev/null
172 if test -z "$local_mode" -o -z "$remote_mode"; then
173 echo "Deleted merge conflict for '$path':"
174 describe_file "$local_mode" "local" "$LOCAL"
175 describe_file "$remote_mode" "remote" "$REMOTE"
176 resolve_deleted_merge
177 return
180 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
181 echo "Symbolic link merge conflict for '$path':"
182 describe_file "$local_mode" "local" "$LOCAL"
183 describe_file "$remote_mode" "remote" "$REMOTE"
184 resolve_symlink_merge
185 return
188 echo "Normal merge conflict for '$path':"
189 describe_file "$local_mode" "local" "$LOCAL"
190 describe_file "$remote_mode" "remote" "$REMOTE"
191 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
192 read ans
194 case "$merge_tool" in
195 kdiff3)
196 if base_present ; then
197 ("$KDIFF3" --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
198 -o "$path" $KDIFF3SEPERATOR "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
199 else
200 ("$KDIFF3" --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
201 -o "$path" $KDIFF3SEPERATOR "$LOCAL" "$REMOTE" > /dev/null 2>&1)
203 status=$?
204 remove_backup
206 tkdiff)
207 if base_present ; then
208 tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
209 else
210 tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
212 status=$?
213 save_backup
215 meld|vimdiff)
216 touch "$BACKUP"
217 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
218 check_unchanged
219 save_backup
221 gvimdiff)
222 touch "$BACKUP"
223 gvimdiff -f -- "$LOCAL" "$path" "$REMOTE"
224 check_unchanged
225 save_backup
227 xxdiff)
228 touch "$BACKUP"
229 if base_present ; then
230 xxdiff -X --show-merged-pane \
231 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
232 -R 'Accel.Search: "Ctrl+F"' \
233 -R 'Accel.SearchForward: "Ctrl-G"' \
234 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
235 else
236 xxdiff -X --show-merged-pane \
237 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
238 -R 'Accel.Search: "Ctrl+F"' \
239 -R 'Accel.SearchForward: "Ctrl-G"' \
240 --merged-file "$path" -- "$LOCAL" "$REMOTE"
242 check_unchanged
243 save_backup
245 opendiff)
246 touch "$BACKUP"
247 if base_present; then
248 opendiff "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat
249 else
250 opendiff "$LOCAL" "$REMOTE" -merge "$path" | cat
252 check_unchanged
253 save_backup
255 emerge)
256 if base_present ; then
257 emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path"
258 else
259 emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
261 status=$?
262 save_backup
264 esac
265 if test "$status" -ne 0; then
266 echo "merge of $path failed" 1>&2
267 mv -- "$BACKUP" "$path"
268 exit 1
270 git add -- "$path"
271 cleanup_temp_files
274 while case $# in 0) break ;; esac
276 case "$1" in
277 -t|--tool*)
278 case "$#,$1" in
279 *,*=*)
280 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
282 1,*)
283 usage ;;
285 merge_tool="$2"
286 shift ;;
287 esac
290 break
293 usage
296 break
298 esac
299 shift
300 done
302 if test -z "$merge_tool"; then
303 merge_tool=`git config merge.tool`
304 case "$merge_tool" in
305 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | "")
306 ;; # happy
308 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
309 echo >&2 "Resetting to default..."
310 unset merge_tool
312 esac
315 if test -z "$merge_tool" ; then
316 if test -n "$DISPLAY"; then
317 merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
318 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
319 merge_tool_candidates="meld $merge_tool_candidates"
321 if test "$KDE_FULL_SESSION" = "true"; then
322 merge_tool_candidates="kdiff3 $merge_tool_candidates"
325 regentry="$(REG QUERY 'HKEY_LOCAL_MACHINE\SOFTWARE\KDiff3\diff-ext' 2>/dev/null)" && {
326 KDIFF3=$(echo "$regentry" | grep diffcommand | awk -F 'REG_SZ' '{ print $2 }' | sed -e 's/^ *//'| sed -e 's@\\@/@g')
327 KDIFF3SEPARATOR=
328 merge_tool_candidates="$merge_tool_candidates kdiff3"
330 if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
331 merge_tool_candidates="$merge_tool_candidates emerge"
333 if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
334 merge_tool_candidates="$merge_tool_candidates vimdiff"
336 merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
337 echo "merge tool candidates: $merge_tool_candidates"
338 for i in $merge_tool_candidates; do
339 if test $i = emerge ; then
340 cmd=emacs
341 elif test $i = kdiff3 ; then
342 cmd="$KDIFF3"
343 else
344 cmd=$i
346 if type "$cmd" > /dev/null 2>&1; then
347 merge_tool=$i
348 break
350 done
351 if test -z "$merge_tool" ; then
352 echo "No available merge resolution programs available."
353 exit 1
357 case "$merge_tool" in
358 kdiff3)
359 if ! type "$KDIFF3" > /dev/null 2>&1; then
360 echo "The merge tool $merge_tool is not available"
361 exit 1
364 tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff)
365 if ! type "$merge_tool" > /dev/null 2>&1; then
366 echo "The merge tool $merge_tool is not available"
367 exit 1
370 emerge)
371 if ! type "emacs" > /dev/null 2>&1; then
372 echo "Emacs is not available"
373 exit 1
377 echo "Unknown merge tool: $merge_tool"
378 exit 1
380 esac
382 if test $# -eq 0 ; then
383 files=`git ls-files -u | sed -e 's/^[^ ]* //' | /usr/bin/sort -u`
384 if test -z "$files" ; then
385 echo "No files need merging"
386 exit 0
388 echo Merging the files: $files
389 git ls-files -u | sed -e 's/^[^ ]* //' | /usr/bin/sort -u | while read i
391 printf "\n"
392 merge_file "$i" < /dev/tty > /dev/tty
393 done
394 else
395 while test $# -gt 0; do
396 printf "\n"
397 merge_file "$1"
398 shift
399 done
401 exit 0