git-mergetool.shon commit fetch and pull: learn --progress (9839018)
   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 Hamano.
   9#
  10
  11USAGE='[--tool=tool] [-y|--no-prompt|--prompt] [file to merge] ...'
  12SUBDIRECTORY_OK=Yes
  13OPTIONS_SPEC=
  14TOOL_MODE=merge
  15. git-sh-setup
  16. git-mergetool--lib
  17require_work_tree
  18
  19# Returns true if the mode reflects a symlink
  20is_symlink () {
  21    test "$1" = 120000
  22}
  23
  24local_present () {
  25    test -n "$local_mode"
  26}
  27
  28remote_present () {
  29    test -n "$remote_mode"
  30}
  31
  32base_present () {
  33    test -n "$base_mode"
  34}
  35
  36cleanup_temp_files () {
  37    if test "$1" = --save-backup ; then
  38        mv -- "$BACKUP" "$MERGED.orig"
  39        rm -f -- "$LOCAL" "$REMOTE" "$BASE"
  40    else
  41        rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
  42    fi
  43}
  44
  45describe_file () {
  46    mode="$1"
  47    branch="$2"
  48    file="$3"
  49
  50    printf "  {%s}: " "$branch"
  51    if test -z "$mode"; then
  52        echo "deleted"
  53    elif is_symlink "$mode" ; then
  54        echo "a symbolic link -> '$(cat "$file")'"
  55    else
  56        if base_present; then
  57            echo "modified"
  58        else
  59            echo "created"
  60        fi
  61    fi
  62}
  63
  64
  65resolve_symlink_merge () {
  66    while true; do
  67        printf "Use (l)ocal or (r)emote, or (a)bort? "
  68        read ans
  69        case "$ans" in
  70            [lL]*)
  71                git checkout-index -f --stage=2 -- "$MERGED"
  72                git add -- "$MERGED"
  73                cleanup_temp_files --save-backup
  74                return 0
  75                ;;
  76            [rR]*)
  77                git checkout-index -f --stage=3 -- "$MERGED"
  78                git add -- "$MERGED"
  79                cleanup_temp_files --save-backup
  80                return 0
  81                ;;
  82            [aA]*)
  83                return 1
  84                ;;
  85            esac
  86        done
  87}
  88
  89resolve_deleted_merge () {
  90    while true; do
  91        if base_present; then
  92            printf "Use (m)odified or (d)eleted file, or (a)bort? "
  93        else
  94            printf "Use (c)reated or (d)eleted file, or (a)bort? "
  95        fi
  96        read ans
  97        case "$ans" in
  98            [mMcC]*)
  99                git add -- "$MERGED"
 100                cleanup_temp_files --save-backup
 101                return 0
 102                ;;
 103            [dD]*)
 104                git rm -- "$MERGED" > /dev/null
 105                cleanup_temp_files
 106                return 0
 107                ;;
 108            [aA]*)
 109                return 1
 110                ;;
 111            esac
 112        done
 113}
 114
 115checkout_staged_file () {
 116    tmpfile=$(expr "$(git checkout-index --temp --stage="$1" "$2")" : '\([^     ]*\)    ')
 117
 118    if test $? -eq 0 -a -n "$tmpfile" ; then
 119        mv -- "$(git rev-parse --show-cdup)$tmpfile" "$3"
 120    fi
 121}
 122
 123merge_file () {
 124    MERGED="$1"
 125
 126    f=$(git ls-files -u -- "$MERGED")
 127    if test -z "$f" ; then
 128        if test ! -f "$MERGED" ; then
 129            echo "$MERGED: file not found"
 130        else
 131            echo "$MERGED: file does not need merging"
 132        fi
 133        return 1
 134    fi
 135
 136    ext="$$$(expr "$MERGED" : '.*\(\.[^/]*\)$')"
 137    BACKUP="./$MERGED.BACKUP.$ext"
 138    LOCAL="./$MERGED.LOCAL.$ext"
 139    REMOTE="./$MERGED.REMOTE.$ext"
 140    BASE="./$MERGED.BASE.$ext"
 141
 142    mv -- "$MERGED" "$BACKUP"
 143    cp -- "$BACKUP" "$MERGED"
 144
 145    base_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==1) print $1;}')
 146    local_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==2) print $1;}')
 147    remote_mode=$(git ls-files -u -- "$MERGED" | awk '{if ($3==3) print $1;}')
 148
 149    base_present   && checkout_staged_file 1 "$MERGED" "$BASE"
 150    local_present  && checkout_staged_file 2 "$MERGED" "$LOCAL"
 151    remote_present && checkout_staged_file 3 "$MERGED" "$REMOTE"
 152
 153    if test -z "$local_mode" -o -z "$remote_mode"; then
 154        echo "Deleted merge conflict for '$MERGED':"
 155        describe_file "$local_mode" "local" "$LOCAL"
 156        describe_file "$remote_mode" "remote" "$REMOTE"
 157        resolve_deleted_merge
 158        return
 159    fi
 160
 161    if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
 162        echo "Symbolic link merge conflict for '$MERGED':"
 163        describe_file "$local_mode" "local" "$LOCAL"
 164        describe_file "$remote_mode" "remote" "$REMOTE"
 165        resolve_symlink_merge
 166        return
 167    fi
 168
 169    echo "Normal merge conflict for '$MERGED':"
 170    describe_file "$local_mode" "local" "$LOCAL"
 171    describe_file "$remote_mode" "remote" "$REMOTE"
 172    if "$prompt" = true; then
 173        printf "Hit return to start merge resolution tool (%s): " "$merge_tool"
 174        read ans
 175    fi
 176
 177    if base_present; then
 178            present=true
 179    else
 180            present=false
 181    fi
 182
 183    if ! run_merge_tool "$merge_tool" "$present"; then
 184        echo "merge of $MERGED failed" 1>&2
 185        mv -- "$BACKUP" "$MERGED"
 186
 187        if test "$merge_keep_temporaries" = "false"; then
 188            cleanup_temp_files
 189        fi
 190
 191        return 1
 192    fi
 193
 194    if test "$merge_keep_backup" = "true"; then
 195        mv -- "$BACKUP" "$MERGED.orig"
 196    else
 197        rm -- "$BACKUP"
 198    fi
 199
 200    git add -- "$MERGED"
 201    cleanup_temp_files
 202    return 0
 203}
 204
 205prompt=$(git config --bool mergetool.prompt || echo true)
 206
 207while test $# != 0
 208do
 209    case "$1" in
 210        -t|--tool*)
 211            case "$#,$1" in
 212                *,*=*)
 213                    merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
 214                    ;;
 215                1,*)
 216                    usage ;;
 217                *)
 218                    merge_tool="$2"
 219                    shift ;;
 220            esac
 221            ;;
 222        -y|--no-prompt)
 223            prompt=false
 224            ;;
 225        --prompt)
 226            prompt=true
 227            ;;
 228        --)
 229            shift
 230            break
 231            ;;
 232        -*)
 233            usage
 234            ;;
 235        *)
 236            break
 237            ;;
 238    esac
 239    shift
 240done
 241
 242prompt_after_failed_merge() {
 243    while true; do
 244        printf "Continue merging other unresolved paths (y/n) ? "
 245        read ans
 246        case "$ans" in
 247
 248            [yY]*)
 249                return 0
 250                ;;
 251
 252            [nN]*)
 253                return 1
 254                ;;
 255        esac
 256    done
 257}
 258
 259if test -z "$merge_tool"; then
 260    merge_tool=$(get_merge_tool "$merge_tool") || exit
 261fi
 262merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
 263merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
 264
 265last_status=0
 266rollup_status=0
 267
 268if test $# -eq 0 ; then
 269    files=$(git ls-files -u | sed -e 's/^[^     ]*      //' | sort -u)
 270    if test -z "$files" ; then
 271        echo "No files need merging"
 272        exit 0
 273    fi
 274    echo Merging the files: "$files"
 275    git ls-files -u |
 276    sed -e 's/^[^       ]*      //' |
 277    sort -u |
 278    while IFS= read i
 279    do
 280        if test $last_status -ne 0; then
 281            prompt_after_failed_merge < /dev/tty || exit 1
 282        fi
 283        printf "\n"
 284        merge_file "$i" < /dev/tty > /dev/tty
 285        last_status=$?
 286        if test $last_status -ne 0; then
 287            rollup_status=1
 288        fi
 289    done
 290else
 291    while test $# -gt 0; do
 292        if test $last_status -ne 0; then
 293            prompt_after_failed_merge || exit 1
 294        fi
 295        printf "\n"
 296        merge_file "$1"
 297        last_status=$?
 298        if test $last_status -ne 0; then
 299            rollup_status=1
 300        fi
 301        shift
 302    done
 303fi
 304
 305exit $rollup_status