Code

Merge branch 'jc/clean-fix'
[git.git] / git-mergetool.sh
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
8 # at the discretion of Junio C Hamano.
9 #
11 USAGE='[--tool=tool] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 OPTIONS_SPEC=
14 . git-sh-setup
15 require_work_tree
16 prefix=$(git rev-parse --show-prefix)
18 # Returns true if the mode reflects a symlink
19 is_symlink () {
20     test "$1" = 120000
21 }
23 local_present () {
24     test -n "$local_mode"
25 }
27 remote_present () {
28     test -n "$remote_mode"
29 }
31 base_present () {
32     test -n "$base_mode"
33 }
35 cleanup_temp_files () {
36     if test "$1" = --save-backup ; then
37         mv -- "$BACKUP" "$path.orig"
38         rm -f -- "$LOCAL" "$REMOTE" "$BASE"
39     else
40         rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
41     fi
42 }
44 describe_file () {
45     mode="$1"
46     branch="$2"
47     file="$3"
49     printf "  {%s}: " "$branch"
50     if test -z "$mode"; then
51         echo "deleted"
52     elif is_symlink "$mode" ; then
53         echo "a symbolic link -> '$(cat "$file")'"
54     else
55         if base_present; then
56             echo "modified"
57         else
58             echo "created"
59         fi
60     fi
61 }
64 resolve_symlink_merge () {
65     while true; do
66         printf "Use (l)ocal or (r)emote, or (a)bort? "
67         read ans
68         case "$ans" in
69             [lL]*)
70                 git checkout-index -f --stage=2 -- "$path"
71                 git add -- "$path"
72                 cleanup_temp_files --save-backup
73                 return
74                 ;;
75             [rR]*)
76                 git checkout-index -f --stage=3 -- "$path"
77                 git add -- "$path"
78                 cleanup_temp_files --save-backup
79                 return
80                 ;;
81             [aA]*)
82                 exit 1
83                 ;;
84             esac
85         done
86 }
88 resolve_deleted_merge () {
89     while true; do
90         if base_present; then
91             printf "Use (m)odified or (d)eleted file, or (a)bort? "
92         else
93             printf "Use (c)reated or (d)eleted file, or (a)bort? "
94         fi
95         read ans
96         case "$ans" in
97             [mMcC]*)
98                 git add -- "$path"
99                 cleanup_temp_files --save-backup
100                 return
101                 ;;
102             [dD]*)
103                 git rm -- "$path" > /dev/null
104                 cleanup_temp_files
105                 return
106                 ;;
107             [aA]*)
108                 exit 1
109                 ;;
110             esac
111         done
114 check_unchanged () {
115     if test "$path" -nt "$BACKUP" ; then
116         status=0;
117     else
118         while true; do
119             echo "$path seems unchanged."
120             printf "Was the merge successful? [y/n] "
121             read answer < /dev/tty
122             case "$answer" in
123                 y*|Y*) status=0; break ;;
124                 n*|N*) status=1; break ;;
125             esac
126         done
127     fi
130 save_backup () {
131     if test "$status" -eq 0; then
132         mv -- "$BACKUP" "$path.orig"
133     fi
136 remove_backup () {
137     if test "$status" -eq 0; then
138         rm "$BACKUP"
139     fi
142 merge_file () {
143     path="$1"
145     f=`git ls-files -u -- "$path"`
146     if test -z "$f" ; then
147         if test ! -f "$path" ; then
148             echo "$path: file not found"
149         else
150             echo "$path: file does not need merging"
151         fi
152         exit 1
153     fi
155     BACKUP="$path.BACKUP.$$"
156     LOCAL="$path.LOCAL.$$"
157     REMOTE="$path.REMOTE.$$"
158     BASE="$path.BASE.$$"
160     mv -- "$path" "$BACKUP"
161     cp -- "$BACKUP" "$path"
163     base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
164     local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
165     remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
167     base_present   && git cat-file blob ":1:$prefix$path" >"$BASE" 2>/dev/null
168     local_present  && git cat-file blob ":2:$prefix$path" >"$LOCAL" 2>/dev/null
169     remote_present && git cat-file blob ":3:$prefix$path" >"$REMOTE" 2>/dev/null
171     if test -z "$local_mode" -o -z "$remote_mode"; then
172         echo "Deleted merge conflict for '$path':"
173         describe_file "$local_mode" "local" "$LOCAL"
174         describe_file "$remote_mode" "remote" "$REMOTE"
175         resolve_deleted_merge
176         return
177     fi
179     if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
180         echo "Symbolic link merge conflict for '$path':"
181         describe_file "$local_mode" "local" "$LOCAL"
182         describe_file "$remote_mode" "remote" "$REMOTE"
183         resolve_symlink_merge
184         return
185     fi
187     echo "Normal merge conflict for '$path':"
188     describe_file "$local_mode" "local" "$LOCAL"
189     describe_file "$remote_mode" "remote" "$REMOTE"
190     printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
191     read ans
193     case "$merge_tool" in
194         kdiff3)
195             if base_present ; then
196                 ("$merge_tool_path" --auto --L1 "$path (Base)" --L2 "$path (Local)" --L3 "$path (Remote)" \
197                     -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
198             else
199                 ("$merge_tool_path" --auto --L1 "$path (Local)" --L2 "$path (Remote)" \
200                     -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
201             fi
202             status=$?
203             remove_backup
204             ;;
205         tkdiff)
206             if base_present ; then
207                 "$merge_tool_path" -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
208             else
209                 "$merge_tool_path" -o "$path" -- "$LOCAL" "$REMOTE"
210             fi
211             status=$?
212             save_backup
213             ;;
214         meld|vimdiff)
215             touch "$BACKUP"
216             "$merge_tool_path" -- "$LOCAL" "$path" "$REMOTE"
217             check_unchanged
218             save_backup
219             ;;
220         gvimdiff)
221                 touch "$BACKUP"
222                 "$merge_tool_path" -f -- "$LOCAL" "$path" "$REMOTE"
223                 check_unchanged
224                 save_backup
225                 ;;
226         xxdiff)
227             touch "$BACKUP"
228             if base_present ; then
229                 "$merge_tool_path" -X --show-merged-pane \
230                     -R 'Accel.SaveAsMerged: "Ctrl-S"' \
231                     -R 'Accel.Search: "Ctrl+F"' \
232                     -R 'Accel.SearchForward: "Ctrl-G"' \
233                     --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
234             else
235                 "$merge_tool_path" -X --show-merged-pane \
236                     -R 'Accel.SaveAsMerged: "Ctrl-S"' \
237                     -R 'Accel.Search: "Ctrl+F"' \
238                     -R 'Accel.SearchForward: "Ctrl-G"' \
239                     --merged-file "$path" -- "$LOCAL" "$REMOTE"
240             fi
241             check_unchanged
242             save_backup
243             ;;
244         opendiff)
245             touch "$BACKUP"
246             if base_present; then
247                 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" | cat
248             else
249                 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$path" | cat
250             fi
251             check_unchanged
252             save_backup
253             ;;
254         ecmerge)
255             touch "$BACKUP"
256             if base_present; then
257                 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --mode=merge3 --to="$path"
258             else
259                 "$merge_tool_path" "$LOCAL" "$REMOTE" --mode=merge2 --to="$path"
260             fi
261             check_unchanged
262             save_backup
263             ;;
264         emerge)
265             if base_present ; then
266                 "$merge_tool_path" -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$(basename "$path")"
267             else
268                 "$merge_tool_path" -f emerge-files-command "$LOCAL" "$REMOTE" "$(basename "$path")"
269             fi
270             status=$?
271             save_backup
272             ;;
273     esac
274     if test "$status" -ne 0; then
275         echo "merge of $path failed" 1>&2
276         mv -- "$BACKUP" "$path"
277         exit 1
278     fi
279     git add -- "$path"
280     cleanup_temp_files
283 while test $# != 0
284 do
285     case "$1" in
286         -t|--tool*)
287             case "$#,$1" in
288                 *,*=*)
289                     merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
290                     ;;
291                 1,*)
292                     usage ;;
293                 *)
294                     merge_tool="$2"
295                     shift ;;
296             esac
297             ;;
298         --)
299             break
300             ;;
301         -*)
302             usage
303             ;;
304         *)
305             break
306             ;;
307     esac
308     shift
309 done
311 valid_tool() {
312         case "$1" in
313                 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge)
314                         ;; # happy
315                 *)
316                         return 1
317                         ;;
318         esac
321 init_merge_tool_path() {
322         merge_tool_path=`git config mergetool.$1.path`
323         if test -z "$merge_tool_path" ; then
324                 case "$1" in
325                         emerge)
326                                 merge_tool_path=emacs
327                                 ;;
328                         *)
329                                 merge_tool_path=$1
330                                 ;;
331                 esac
332         fi
336 if test -z "$merge_tool"; then
337     merge_tool=`git config merge.tool`
338     if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
339             echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
340             echo >&2 "Resetting to default..."
341             unset merge_tool
342     fi
343 fi
345 if test -z "$merge_tool" ; then
346     if test -n "$DISPLAY"; then
347         merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
348         if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
349             merge_tool_candidates="meld $merge_tool_candidates"
350         fi
351         if test "$KDE_FULL_SESSION" = "true"; then
352             merge_tool_candidates="kdiff3 $merge_tool_candidates"
353         fi
354     fi
355     if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
356         merge_tool_candidates="$merge_tool_candidates emerge"
357     fi
358     if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
359         merge_tool_candidates="$merge_tool_candidates vimdiff"
360     fi
361     merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
362     echo "merge tool candidates: $merge_tool_candidates"
363     for i in $merge_tool_candidates; do
364         init_merge_tool_path $i
365         if type "$merge_tool_path" > /dev/null 2>&1; then
366             merge_tool=$i
367             break
368         fi
369     done
370     if test -z "$merge_tool" ; then
371         echo "No known merge resolution program available."
372         exit 1
373     fi
374 else
375     if ! valid_tool "$merge_tool"; then
376         echo >&2 "Unknown merge_tool $merge_tool"
377         exit 1
378     fi
380     init_merge_tool_path "$merge_tool"
382     if ! type "$merge_tool_path" > /dev/null 2>&1; then
383         echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
384         exit 1
385     fi
386 fi
389 if test $# -eq 0 ; then
390         files=`git ls-files -u | sed -e 's/^[^  ]*      //' | sort -u`
391         if test -z "$files" ; then
392                 echo "No files need merging"
393                 exit 0
394         fi
395         echo Merging the files: $files
396         git ls-files -u | sed -e 's/^[^ ]*      //' | sort -u | while read i
397         do
398                 printf "\n"
399                 merge_file "$i" < /dev/tty > /dev/tty
400         done
401 else
402         while test $# -gt 0; do
403                 printf "\n"
404                 merge_file "$1"
405                 shift
406         done
407 fi
408 exit 0