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