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 Hammano.
11 USAGE='[--tool=tool] [file to merge] ...'
16 # Returns true if the mode reflects a symlink
17 function is_symlink () {
21 function local_present () {
25 function remote_present () {
26 test -n "$remote_mode"
29 function base_present () {
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"
42 function describe_file () {
48 if test -z "$mode"; then
49 printf "'%s' was deleted" "$path"
50 elif is_symlink "$mode" ; then
51 printf "'%s' is a symlink containing '%s'" "$path" "$file"
54 printf "'%s' was created" "$path"
56 printf "'%s' was modified" "$path"
59 echo " in the $branch branch"
63 resolve_symlink_merge () {
65 printf "Use (r)emote or (l)ocal, or (a)bort? "
69 git-checkout-index -f --stage=2 -- "$path"
71 cleanup_temp_files --save-backup
75 git-checkout-index -f --stage=3 -- "$path"
77 cleanup_temp_files --save-backup
87 resolve_deleted_merge () {
89 printf "Use (m)odified or (d)eleted file, or (a)bort? "
94 cleanup_temp_files --save-backup
112 if test ! -f "$path" ; then
113 echo "$path: file not found"
117 f=`git-ls-files -u -- "$path"`
118 if test -z "$f" ; then
119 echo "$path: file does not need merging"
123 BACKUP="$path.BACKUP.$$"
124 LOCAL="$path.LOCAL.$$"
125 REMOTE="$path.REMOTE.$$"
128 mv -- "$path" "$BACKUP"
129 cp -- "$BACKUP" "$path"
131 base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
132 local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
133 remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
135 base_present && git cat-file blob ":1:$path" > "$BASE" 2>/dev/null
136 local_present && git cat-file blob ":2:$path" > "$LOCAL" 2>/dev/null
137 remote_present && git cat-file blob ":3:$path" > "$REMOTE" 2>/dev/null
139 if test -z "$local_mode" -o -z "$remote_mode"; then
140 echo "Deleted merge conflict for $path:"
141 describe_file "$local_mode" "local" "$LOCAL"
142 describe_file "$remote_mode" "remote" "$REMOTE"
143 resolve_deleted_merge
147 if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
148 echo "Symlink merge conflict for $path:"
149 describe_file "$local_mode" "local" "$LOCAL"
150 describe_file "$remote_mode" "remote" "$REMOTE"
151 resolve_symlink_merge
155 echo "Normal merge conflict for $path:"
156 describe_file "$local_mode" "local" "$LOCAL"
157 describe_file "$remote_mode" "remote" "$REMOTE"
158 printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
161 case "$merge_tool" in
163 if base_present ; then
164 (kdiff3 --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
165 -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
167 (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
168 -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
171 if test "$status" -eq 0; then
176 if base_present ; then
177 tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
179 tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
182 if test "$status" -eq 0; then
183 mv -- "$BACKUP" "$path.orig"
188 $merge_tool -- "$LOCAL" "$path" "$REMOTE"
189 if test "$path" -nt "$BACKUP" ; then
193 echo "$path seems unchanged."
194 printf "Was the merge successful? [y/n] "
195 read answer < /dev/tty
197 y*|Y*) status=0; break ;;
198 n*|N*) status=1; break ;;
202 if test "$status" -eq 0; then
203 mv -- "$BACKUP" "$path.orig"
208 if base_present ; then
209 xxdiff -X --show-merged-pane \
210 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
211 -R 'Accel.Search: "Ctrl+F"' \
212 -R 'Accel.SearchForward: "Ctrl-G"' \
213 --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
215 xxdiff -X --show-merged-pane \
216 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
217 -R 'Accel.Search: "Ctrl+F"' \
218 -R 'Accel.SearchForward: "Ctrl-G"' \
219 --merged-file "$path" -- "$LOCAL" "$REMOTE"
221 if test "$path" -nt "$BACKUP" ; then
225 echo "$path seems unchanged."
226 printf "Was the merge successful? [y/n] "
227 read answer < /dev/tty
229 y*|Y*) status=0; break ;;
230 n*|N*) status=1; break ;;
234 if test "$status" -eq 0; then
235 mv -- "$BACKUP" "$path.orig"
239 if base_present ; then
240 emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path"
242 emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
245 if test "$status" -eq 0; then
246 mv -- "$BACKUP" "$path.orig"
250 if test "$status" -ne 0; then
251 echo "merge of $path failed" 1>&2
252 mv -- "$BACKUP" "$path"
259 while case $# in 0) break ;; esac
265 merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
287 if test -z "$merge_tool"; then
288 merge_tool=`git-config merge.tool`
289 case "$merge_tool" in
290 kdiff3 | tkdiff | xxdiff | meld | emerge | vimdiff)
293 echo >&2 "git config option merge.tool set to unknown tool: $merge_tool"
294 echo >&2 "Resetting to default..."
300 if test -z "$merge_tool" ; then
301 if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
303 elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
305 elif type xxdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
307 elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
309 elif type emacs >/dev/null 2>&1; then
311 elif type vimdiff >/dev/null 2>&1; then
314 echo "No available merge resolution programs available."
319 case "$merge_tool" in
320 kdiff3|tkdiff|meld|xxdiff|vimdiff)
321 if ! type "$merge_tool" > /dev/null 2>&1; then
322 echo "The merge tool $merge_tool is not available"
327 if ! type "emacs" > /dev/null 2>&1; then
328 echo "Emacs is not available"
333 echo "Unknown merge tool: $merge_tool"
338 if test $# -eq 0 ; then
339 files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
340 if test -z "$files" ; then
341 echo "No files need merging"
344 echo Merging the files: $files
345 git ls-files -u | sed -e 's/^[^ ]* //' | sort -u | while read i
348 merge_file "$i" < /dev/tty > /dev/tty
351 while test $# -gt 0; do