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