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] ...'
19 # Returns true if the mode reflects a symlink
29 test -n "$remote_mode"
36 cleanup_temp_files
() {
37 if test "$1" = --save-backup ; then
38 mv -- "$BACKUP" "$path.orig"
39 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
41 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
50 printf " {%s}: " "$branch"
51 if test -z "$mode"; then
53 elif is_symlink
"$mode" ; then
54 echo "a symbolic link -> '$(cat "$file")'"
65 resolve_symlink_merge
() {
67 printf "Use (l)ocal or (r)emote, or (a)bort? "
71 git checkout-index
-f --stage=2 -- "$path"
73 cleanup_temp_files
--save-backup
77 git checkout-index
-f --stage=3 -- "$path"
79 cleanup_temp_files
--save-backup
89 resolve_deleted_merge
() {
92 printf "Use (m)odified or (d)eleted file, or (a)bort? "
94 printf "Use (c)reated or (d)eleted file, or (a)bort? "
100 cleanup_temp_files
--save-backup
104 git
rm -- "$path" > /dev
/null
116 if test "$path" -nt "$BACKUP" ; then
120 echo "$path seems unchanged."
121 printf "Was the merge successful? [y/n] "
122 read answer
< /dev
/tty
124 y
*|Y
*) status
=0; break ;;
125 n
*|N
*) status
=1; break ;;
132 if test "$status" -eq 0; then
133 mv -- "$BACKUP" "$path.orig"
138 if test "$status" -eq 0; then
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"
151 echo "$path: file does not need merging"
156 BACKUP
="$path.BACKUP.$$"
157 LOCAL
="$path.LOCAL.$$"
158 REMOTE
="$path.REMOTE.$$"
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
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
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"
194 case "$merge_tool" in
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)
200 ("$KDIFF3" --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
201 -o "$path" $KDIFF3SEPERATOR "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
207 if base_present
; then
208 tkdiff
-a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
210 tkdiff
-o "$path" -- "$LOCAL" "$REMOTE"
217 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
223 gvimdiff
-f -- "$LOCAL" "$path" "$REMOTE"
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"
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"
247 if base_present
; then
248 opendiff
"$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" |
cat
250 opendiff
"$LOCAL" "$REMOTE" -merge "$path" |
cat
256 if base_present
; then
257 emacs
-f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$path"
259 emacs
-f emerge-files-command
"$LOCAL" "$REMOTE" "$path"
265 if test "$status" -ne 0; then
266 echo "merge of $path failed" 1>&2
267 mv -- "$BACKUP" "$path"
274 while case $# in 0) break ;; esac
280 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
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 |
"")
308 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
309 echo >&2 "Resetting to default..."
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')
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
341 elif test $i = kdiff3
; then
346 if type "$cmd" > /dev
/null
2>&1; then
351 if test -z "$merge_tool" ; then
352 echo "No available merge resolution programs available."
357 case "$merge_tool" in
359 if ! type "$KDIFF3" > /dev
/null
2>&1; then
360 echo "The merge tool $merge_tool is not available"
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"
371 if ! type "emacs" > /dev
/null
2>&1; then
372 echo "Emacs is not available"
377 echo "Unknown merge tool: $merge_tool"
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"
388 echo Merging the files
: $files
389 git ls-files
-u |
sed -e 's/^[^ ]* //' |
/usr
/bin
/sort -u |
while read i
392 merge_file
"$i" < /dev
/tty
> /dev
/tty
395 while test $# -gt 0; do