Code

checkout: report where the new HEAD is upon detaching HEAD
[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 Hammano.
9 #
11 USAGE='[--tool=tool] [file to merge] ...'
12 SUBDIRECTORY_OK=Yes
13 . git-sh-setup
14 require_work_tree
16 # Returns true if the mode reflects a symlink
17 function is_symlink () {
18     test "$1" = 120000
19 }
21 function local_present () {
22     test -n "$local_mode"
23 }
25 function remote_present () {
26     test -n "$remote_mode"
27 }
29 function base_present () {
30     test -n "$base_mode"
31 }
33 cleanup_temp_files () {
34     if test "$1" = --save-backup ; then
35         mv -- "$BACKUP" "$path.orig"
36         rm -f -- "$LOCAL" "$REMOTE" "$BASE"
37     else
38         rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
39     fi
40 }
42 function describe_file () {
43     mode="$1"
44     branch="$2"
45     file="$3"
47     echo -n "    "
48     if test -z "$mode"; then
49         echo -n "'$path' was deleted"
50     elif is_symlink "$mode" ; then
51         echo -n "'$path' is a symlink containing '"
52         cat "$file"
53         echo -n "'"
54     else
55         if base_present; then
56             echo -n "'$path' was created"
57         else
58             echo -n "'$path' was modified"
59         fi
60     fi
61     echo " in the $branch branch"
62 }
65 resolve_symlink_merge () {
66     while /bin/true; do
67         echo -n "Use (r)emote or (l)ocal, 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
75                 ;;
76            [rR]*)
77                 git-checkout-index -f --stage=3 -- "$path"
78                 git-add -- "$path"
79                 cleanup_temp_files --save-backup
80                 return
81                 ;;
82             [qQ]*)
83                 exit 1
84                 ;;
85             esac
86         done
87 }
89 resolve_deleted_merge () {
90     while /bin/true; do
91         echo -n "Use (m)odified or (d)eleted file, or (a)bort? "
92         read ans
93         case "$ans" in
94             [mM]*)
95                 git-add -- "$path"
96                 cleanup_temp_files --save-backup
97                 return
98                 ;;
99            [dD]*)
100                 git-rm -- "$path"
101                 cleanup_temp_files
102                 return
103                 ;;
104             [qQ]*)
105                 exit 1
106                 ;;
107             esac
108         done
111 merge_file () {
112     path="$1"
114     if test ! -f "$path" ; then
115         echo "$path: file not found"
116         exit 1
117     fi
119     f=`git-ls-files -u -- "$path"`
120     if test -z "$f" ; then
121         echo "$path: file does not need merging"
122         exit 1
123     fi
125     BACKUP="$path.BACKUP.$$"
126     LOCAL="$path.LOCAL.$$"
127     REMOTE="$path.REMOTE.$$"
128     BASE="$path.BASE.$$"
130     mv -- "$path" "$BACKUP"
131     cp -- "$BACKUP" "$path"
133     base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
134     local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
135     remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
137     base_present   && git cat-file blob ":1:$path" > "$BASE" 2>/dev/null
138     local_present  && git cat-file blob ":2:$path" > "$LOCAL" 2>/dev/null
139     remote_present && git cat-file blob ":3:$path" > "$REMOTE" 2>/dev/null
141     if test -z "$local_mode" -o -z "$remote_mode"; then
142         echo "Deleted merge conflict for $path:"
143         describe_file "$local_mode" "local" "$LOCAL"
144         describe_file "$remote_mode" "remote" "$REMOTE"
145         resolve_deleted_merge
146         return
147     fi
149     if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
150         echo "Symlink merge conflict for $path:"
151         describe_file "$local_mode" "local" "$LOCAL"
152         describe_file "$remote_mode" "remote" "$REMOTE"
153         resolve_symlink_merge
154         return
155     fi
157     echo "Normal merge conflict for $path:"
158     describe_file "$local_mode" "local" "$LOCAL"
159     describe_file "$remote_mode" "remote" "$REMOTE"
160     echo -n "Hit return to start merge resolution tool ($merge_tool): "
161     read ans
163     case "$merge_tool" in
164         kdiff3)
165             if base_present ; then
166                 (kdiff3 --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
167                     -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
168             else
169                 (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
170                     -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
171             fi
172             status=$?
173             if test "$status" -eq 0; then
174                 rm "$BACKUP"
175             fi
176             ;;
177         tkdiff)
178             if base_present ; then
179                 tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
180             else
181                 tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
182             fi
183             status=$?
184             if test "$status" -eq 0; then
185                 mv -- "$BACKUP" "$path.orig"
186             fi
187             ;;
188         meld|vimdiff)
189             touch "$BACKUP"
190             $merge_tool -- "$LOCAL" "$path" "$REMOTE"
191             if test "$path" -nt "$BACKUP" ; then
192                 status=0;
193             else
194                 while true; do
195                     echo "$path seems unchanged."
196                     echo -n "Was the merge successful? [y/n] "
197                     read answer < /dev/tty
198                     case "$answer" in
199                         y*|Y*) status=0; break ;;
200                         n*|N*) status=1; break ;;
201                     esac
202                 done
203             fi
204             if test "$status" -eq 0; then
205                 mv -- "$BACKUP" "$path.orig"
206             fi
207             ;;
208         xxdiff)
209             touch "$BACKUP"
210             if base_present ; then
211                 xxdiff -X --show-merged-pane \
212                     -R 'Accel.SaveAsMerged: "Ctrl-S"' \
213                     -R 'Accel.Search: "Ctrl+F"' \
214                     -R 'Accel.SearchForward: "Ctrl-G"' \
215                     --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
216             else
217                 xxdiff -X --show-merged-pane \
218                     -R 'Accel.SaveAsMerged: "Ctrl-S"' \
219                     -R 'Accel.Search: "Ctrl+F"' \
220                     -R 'Accel.SearchForward: "Ctrl-G"' \
221                     --merged-file "$path" -- "$LOCAL" "$REMOTE"
222             fi
223             if test "$path" -nt "$BACKUP" ; then
224                 status=0;
225             else
226                 while true; do
227                     echo "$path seems unchanged."
228                     echo -n "Was the merge successful? [y/n] "
229                     read answer < /dev/tty
230                     case "$answer" in
231                         y*|Y*) status=0; break ;;
232                         n*|N*) status=1; break ;;
233                     esac
234                 done
235             fi
236             if test "$status" -eq 0; then
237                 mv -- "$BACKUP" "$path.orig"
238             fi
239             ;;
240         emerge)
241             if base_present ; then
242                 emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path"
243             else
244                 emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
245             fi
246             status=$?
247             if test "$status" -eq 0; then
248                 mv -- "$BACKUP" "$path.orig"
249             fi
250             ;;
251     esac
252     if test "$status" -ne 0; then
253         echo "merge of $path failed" 1>&2
254         mv -- "$BACKUP" "$path"
255         exit 1
256     fi
257     git add -- "$path"
258     cleanup_temp_files
261 while case $# in 0) break ;; esac
262 do
263     case "$1" in
264         -t|--tool*)
265             case "$#,$1" in
266                 *,*=*)
267                     merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
268                     ;;
269                 1,*)
270                     usage ;;
271                 *)
272                     merge_tool="$2"
273                     shift ;;
274             esac
275             ;;
276         --)
277             break
278             ;;
279         -*)
280             usage
281             ;;
282         *)
283             break
284             ;;
285     esac
286     shift
287 done
289 if test -z "$merge_tool"; then
290     merge_tool=`git-config merge.tool`
291     case "$merge_tool" in
292         kdiff3 | tkdiff | xxdiff | meld | emerge | vimdiff)
293             ;; # happy
294         *)
295             echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
296             echo >&2 "Resetting to default..."
297             unset merge_tool
298             ;;
299     esac
300 fi
302 if test -z "$merge_tool" ; then
303     if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
304         merge_tool="kdiff3";
305     elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
306         merge_tool=tkdiff
307     elif type xxdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
308         merge_tool=xxdiff
309     elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
310         merge_tool=meld
311     elif type emacs >/dev/null 2>&1; then
312         merge_tool=emerge
313     elif type vimdiff >/dev/null 2>&1; then
314         merge_tool=vimdiff
315     else
316         echo "No available merge resolution programs available."
317         exit 1
318     fi
319 fi
321 case "$merge_tool" in
322     kdiff3|tkdiff|meld|xxdiff|vimdiff)
323         if ! type "$merge_tool" > /dev/null 2>&1; then
324             echo "The merge tool $merge_tool is not available"
325             exit 1
326         fi
327         ;;
328     emerge)
329         if ! type "emacs" > /dev/null 2>&1; then
330             echo "Emacs is not available"
331             exit 1
332         fi
333         ;;
334     *)
335         echo "Unknown merge tool: $merge_tool"
336         exit 1
337         ;;
338 esac
340 if test $# -eq 0 ; then
341         files=`git ls-files -u | sed -e 's/^[^  ]*      //' | sort -u`
342         if test -z "$files" ; then
343                 echo "No files need merging"
344                 exit 0
345         fi
346         echo Merging the files: $files
347         git ls-files -u | sed -e 's/^[^ ]*      //' | sort -u | while read i
348         do
349                 echo ""
350                 merge_file "$i" < /dev/tty > /dev/tty
351         done
352 else
353         while test $# -gt 0; do
354                 echo ""
355                 merge_file "$1"
356                 shift
357         done
358 fi
359 exit 0