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] ...'
16 # Returns true if the mode reflects a symlink
26 test -n "$remote_mode"
33 cleanup_temp_files
() {
34 if test "$1" = --save-backup ; then
35 mv -- "$BACKUP" "$path.orig"
36 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
47 printf " {%s}: " "$branch"
48 if test -z "$mode"; then
50 elif is_symlink
"$mode" ; then
51 echo "a symbolic link -> '$(cat "$file")'"
62 resolve_symlink_merge
() {
64 printf "Use (l)ocal or (r)emote, or (a)bort? "
68 git-checkout-index
-f --stage=2 -- "$path"
70 cleanup_temp_files
--save-backup
74 git-checkout-index
-f --stage=3 -- "$path"
76 cleanup_temp_files
--save-backup
86 resolve_deleted_merge
() {
89 printf "Use (m)odified or (d)eleted file, or (a)bort? "
91 printf "Use (c)reated or (d)eleted file, or (a)bort? "
97 cleanup_temp_files
--save-backup
101 git-rm
-- "$path" > /dev
/null
113 if test "$path" -nt "$BACKUP" ; then
117 echo "$path seems unchanged."
118 printf "Was the merge successful? [y/n] "
119 read answer
< /dev
/tty
121 y
*|Y
*) status
=0; break ;;
122 n
*|N
*) status
=1; break ;;
129 if test "$status" -eq 0; then
130 mv -- "$BACKUP" "$path.orig"
135 if test "$status" -eq 0; then
143 f
=`git-ls-files -u -- "$path"`
144 if test -z "$f" ; then
145 if test ! -f "$path" ; then
146 echo "$path: file not found"
148 echo "$path: file does not need merging"
153 BACKUP
="$path.BACKUP.$$"
154 LOCAL
="$path.LOCAL.$$"
155 REMOTE
="$path.REMOTE.$$"
158 mv -- "$path" "$BACKUP"
159 cp -- "$BACKUP" "$path"
161 base_mode
=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
162 local_mode
=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
163 remote_mode
=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
165 base_present
&& git cat-file blob
":1:$path" > "$BASE" 2>/dev
/null
166 local_present
&& git cat-file blob
":2:$path" > "$LOCAL" 2>/dev
/null
167 remote_present
&& git cat-file blob
":3:$path" > "$REMOTE" 2>/dev
/null
169 if test -z "$local_mode" -o -z "$remote_mode"; then
170 echo "Deleted merge conflict for '$path':"
171 describe_file
"$local_mode" "local" "$LOCAL"
172 describe_file
"$remote_mode" "remote" "$REMOTE"
173 resolve_deleted_merge
177 if is_symlink
"$local_mode" || is_symlink
"$remote_mode"; then
178 echo "Symbolic link merge conflict for '$path':"
179 describe_file
"$local_mode" "local" "$LOCAL"
180 describe_file
"$remote_mode" "remote" "$REMOTE"
181 resolve_symlink_merge
185 echo "Normal merge conflict for '$path':"
186 describe_file
"$local_mode" "local" "$LOCAL"
187 describe_file
"$remote_mode" "remote" "$REMOTE"
188 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
191 case "$merge_tool" in
193 if base_present
; then
194 (kdiff3
--auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
195 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
197 (kdiff3
--auto -L1 "$path (Local)" --L2 "$path (Remote)" \
198 -o "$path" -- "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
204 if base_present
; then
205 tkdiff
-a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
207 tkdiff
-o "$path" -- "$LOCAL" "$REMOTE"
214 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
220 gvimdiff
-f -- "$LOCAL" "$path" "$REMOTE"
226 if base_present
; then
227 xxdiff
-X --show-merged-pane \
228 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
229 -R 'Accel.Search: "Ctrl+F"' \
230 -R 'Accel.SearchForward: "Ctrl-G"' \
231 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
233 xxdiff
-X --show-merged-pane \
234 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
235 -R 'Accel.Search: "Ctrl+F"' \
236 -R 'Accel.SearchForward: "Ctrl-G"' \
237 --merged-file "$path" -- "$LOCAL" "$REMOTE"
244 if base_present
; then
245 opendiff
"$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" |
cat
247 opendiff
"$LOCAL" "$REMOTE" -merge "$path" |
cat
253 if base_present
; then
254 emacs
-f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$path"
256 emacs
-f emerge-files-command
"$LOCAL" "$REMOTE" "$path"
262 if test "$status" -ne 0; then
263 echo "merge of $path failed" 1>&2
264 mv -- "$BACKUP" "$path"
271 while case $# in 0) break ;; esac
277 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
299 if test -z "$merge_tool"; then
300 merge_tool
=`git-config merge.tool`
301 case "$merge_tool" in
302 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff |
"")
305 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
306 echo >&2 "Resetting to default..."
312 if test -z "$merge_tool" ; then
313 if test -n "$DISPLAY"; then
314 merge_tool_candidates
="kdiff3 tkdiff xxdiff meld gvimdiff"
315 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
316 merge_tool_candidates
="meld $merge_tool_candidates"
318 if test "$KDE_FULL_SESSION" = "true"; then
319 merge_tool_candidates
="kdiff3 $merge_tool_candidates"
322 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
323 merge_tool_candidates
="$merge_tool_candidates emerge"
325 if echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
326 merge_tool_candidates
="$merge_tool_candidates vimdiff"
328 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
329 echo "merge tool candidates: $merge_tool_candidates"
330 for i
in $merge_tool_candidates; do
331 if test $i = emerge
; then
336 if type $cmd > /dev
/null
2>&1; then
341 if test -z "$merge_tool" ; then
342 echo "No available merge resolution programs available."
347 case "$merge_tool" in
348 kdiff3|tkdiff|meld|xxdiff|vimdiff|gvimdiff|opendiff
)
349 if ! type "$merge_tool" > /dev
/null
2>&1; then
350 echo "The merge tool $merge_tool is not available"
355 if ! type "emacs" > /dev
/null
2>&1; then
356 echo "Emacs is not available"
361 echo "Unknown merge tool: $merge_tool"
366 if test $# -eq 0 ; then
367 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
368 if test -z "$files" ; then
369 echo "No files need merging"
372 echo Merging the files
: $files
373 git ls-files
-u |
sed -e 's/^[^ ]* //' |
sort -u |
while read i
376 merge_file
"$i" < /dev
/tty
> /dev
/tty
379 while test $# -gt 0; do