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 prefix
=$
(git rev-parse
--show-prefix)
18 # Returns true if the mode reflects a symlink
28 test -n "$remote_mode"
35 cleanup_temp_files
() {
36 if test "$1" = --save-backup ; then
37 mv -- "$BACKUP" "$path.orig"
38 rm -f -- "$LOCAL" "$REMOTE" "$BASE"
40 rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
49 printf " {%s}: " "$branch"
50 if test -z "$mode"; then
52 elif is_symlink
"$mode" ; then
53 echo "a symbolic link -> '$(cat "$file")'"
64 resolve_symlink_merge
() {
66 printf "Use (l)ocal or (r)emote, or (a)bort? "
70 git checkout-index
-f --stage=2 -- "$path"
72 cleanup_temp_files
--save-backup
76 git checkout-index
-f --stage=3 -- "$path"
78 cleanup_temp_files
--save-backup
88 resolve_deleted_merge
() {
91 printf "Use (m)odified or (d)eleted file, or (a)bort? "
93 printf "Use (c)reated or (d)eleted file, or (a)bort? "
99 cleanup_temp_files
--save-backup
103 git
rm -- "$path" > /dev
/null
115 if test "$path" -nt "$BACKUP" ; then
119 echo "$path seems unchanged."
120 printf "Was the merge successful? [y/n] "
121 read answer
< /dev
/tty
123 y
*|Y
*) status
=0; break ;;
124 n
*|N
*) status
=1; break ;;
131 if test "$status" -eq 0; then
132 mv -- "$BACKUP" "$path.orig"
137 if test "$status" -eq 0; then
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"
150 echo "$path: file does not need merging"
155 ext
="$$$(expr "$path" : '.*\(\.[^/]*\)$')"
156 BACKUP
="$path.BACKUP.$ext"
157 LOCAL
="$path.LOCAL.$ext"
158 REMOTE
="$path.REMOTE.$ext"
159 BASE
="$path.BASE.$ext"
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:$prefix$path" >"$BASE" 2>/dev
/null
169 local_present
&& git cat-file blob
":2:$prefix$path" >"$LOCAL" 2>/dev
/null
170 remote_present
&& git cat-file blob
":3:$prefix$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 ("$merge_tool_path" --auto --L1 "$path (Base)" --L2 "$path (Local)" --L3 "$path (Remote)" \
198 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
200 ("$merge_tool_path" --auto --L1 "$path (Local)" --L2 "$path (Remote)" \
201 -o "$path" -- "$LOCAL" "$REMOTE" > /dev
/null
2>&1)
207 if base_present
; then
208 "$merge_tool_path" -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
210 "$merge_tool_path" -o "$path" -- "$LOCAL" "$REMOTE"
217 "$merge_tool_path" -- "$LOCAL" "$path" "$REMOTE"
223 "$merge_tool_path" -f -- "$LOCAL" "$path" "$REMOTE"
229 if base_present
; then
230 "$merge_tool_path" -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 "$merge_tool_path" -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 "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$path" |
cat
250 "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$path" |
cat
257 if base_present
; then
258 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --mode=merge3
--to="$path"
260 "$merge_tool_path" "$LOCAL" "$REMOTE" --mode=merge2
--to="$path"
266 if base_present
; then
267 "$merge_tool_path" -f emerge-files-with-ancestor-command
"$LOCAL" "$REMOTE" "$BASE" "$(basename "$path")"
269 "$merge_tool_path" -f emerge-files-command
"$LOCAL" "$REMOTE" "$(basename "$path")"
275 if test "$status" -ne 0; then
276 echo "merge of $path failed" 1>&2
277 mv -- "$BACKUP" "$path"
290 merge_tool
=`expr "z$1" : 'z-[^=]*=\(.*\)'`
314 kdiff3 | tkdiff | xxdiff | meld | opendiff | emerge | vimdiff | gvimdiff | ecmerge
)
322 init_merge_tool_path
() {
323 merge_tool_path
=`git config mergetool.$1.path`
324 if test -z "$merge_tool_path" ; then
327 merge_tool_path
=emacs
337 if test -z "$merge_tool"; then
338 merge_tool
=`git config merge.tool`
339 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
340 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
341 echo >&2 "Resetting to default..."
346 if test -z "$merge_tool" ; then
347 if test -n "$DISPLAY"; then
348 merge_tool_candidates
="kdiff3 tkdiff xxdiff meld gvimdiff"
349 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
350 merge_tool_candidates
="meld $merge_tool_candidates"
352 if test "$KDE_FULL_SESSION" = "true"; then
353 merge_tool_candidates
="kdiff3 $merge_tool_candidates"
356 if echo "${VISUAL:-$EDITOR}" |
grep 'emacs' > /dev
/null
2>&1; then
357 merge_tool_candidates
="$merge_tool_candidates emerge"
359 if echo "${VISUAL:-$EDITOR}" |
grep 'vim' > /dev
/null
2>&1; then
360 merge_tool_candidates
="$merge_tool_candidates vimdiff"
362 merge_tool_candidates
="$merge_tool_candidates opendiff emerge vimdiff"
363 echo "merge tool candidates: $merge_tool_candidates"
364 for i
in $merge_tool_candidates; do
365 init_merge_tool_path
$i
366 if type "$merge_tool_path" > /dev
/null
2>&1; then
371 if test -z "$merge_tool" ; then
372 echo "No known merge resolution program available."
376 if ! valid_tool
"$merge_tool"; then
377 echo >&2 "Unknown merge_tool $merge_tool"
381 init_merge_tool_path
"$merge_tool"
383 if ! type "$merge_tool_path" > /dev
/null
2>&1; then
384 echo "The merge tool $merge_tool is not available as '$merge_tool_path'"
390 if test $# -eq 0 ; then
391 files
=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
392 if test -z "$files" ; then
393 echo "No files need merging"
396 echo Merging the files
: $files
397 git ls-files
-u |
sed -e 's/^[^ ]* //' |
sort -u |
while read i
400 merge_file
"$i" < /dev
/tty
> /dev
/tty
403 while test $# -gt 0; do