contrib / examples / git-revert.shon commit Merge branch 'master' of git://repo.or.cz/git-gui (f4198c9)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Linus Torvalds
   4# Copyright (c) 2005 Junio C Hamano
   5#
   6
   7case "$0" in
   8*-revert* )
   9        test -t 0 && edit=-e
  10        replay=
  11        me=revert
  12        USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
  13*-cherry-pick* )
  14        replay=t
  15        edit=
  16        me=cherry-pick
  17        USAGE='[--edit] [-n] [-r] [-x] <commit-ish>'  ;;
  18* )
  19        echo >&2 "What are you talking about?"
  20        exit 1 ;;
  21esac
  22
  23SUBDIRECTORY_OK=Yes ;# we will cd up
  24. git-sh-setup
  25require_work_tree
  26cd_to_toplevel
  27
  28no_commit=
  29while case "$#" in 0) break ;; esac
  30do
  31        case "$1" in
  32        -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\
  33            --no-commi|--no-commit)
  34                no_commit=t
  35                ;;
  36        -e|--e|--ed|--edi|--edit)
  37                edit=-e
  38                ;;
  39        --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
  40                edit=
  41                ;;
  42        -r)
  43                : no-op ;;
  44        -x|--i-really-want-to-expose-my-private-commit-object-name)
  45                replay=
  46                ;;
  47        -*)
  48                usage
  49                ;;
  50        *)
  51                break
  52                ;;
  53        esac
  54        shift
  55done
  56
  57set_reflog_action "$me"
  58
  59test "$me,$replay" = "revert,t" && usage
  60
  61case "$no_commit" in
  62t)
  63        # We do not intend to commit immediately.  We just want to
  64        # merge the differences in.
  65        head=$(git-write-tree) ||
  66                die "Your index file is unmerged."
  67        ;;
  68*)
  69        head=$(git-rev-parse --verify HEAD) ||
  70                die "You do not have a valid HEAD"
  71        files=$(git-diff-index --cached --name-only $head) || exit
  72        if [ "$files" ]; then
  73                die "Dirty index: cannot $me (dirty: $files)"
  74        fi
  75        ;;
  76esac
  77
  78rev=$(git-rev-parse --verify "$@") &&
  79commit=$(git-rev-parse --verify "$rev^0") ||
  80        die "Not a single commit $@"
  81prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
  82        die "Cannot run $me a root commit"
  83git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
  84        die "Cannot run $me a multi-parent commit."
  85
  86encoding=$(git config i18n.commitencoding || echo UTF-8)
  87
  88# "commit" is an existing commit.  We would want to apply
  89# the difference it introduces since its first parent "prev"
  90# on top of the current HEAD if we are cherry-pick.  Or the
  91# reverse of it if we are revert.
  92
  93case "$me" in
  94revert)
  95        git show -s --pretty=oneline --encoding="$encoding" $commit |
  96        sed -e '
  97                s/^[^ ]* /Revert "/
  98                s/$/"/
  99        '
 100        echo
 101        echo "This reverts commit $commit."
 102        test "$rev" = "$commit" ||
 103        echo "(original 'git revert' arguments: $@)"
 104        base=$commit next=$prev
 105        ;;
 106
 107cherry-pick)
 108        pick_author_script='
 109        /^author /{
 110                s/'\''/'\''\\'\'\''/g
 111                h
 112                s/^author \([^<]*\) <[^>]*> .*$/\1/
 113                s/'\''/'\''\'\'\''/g
 114                s/.*/GIT_AUTHOR_NAME='\''&'\''/p
 115
 116                g
 117                s/^author [^<]* <\([^>]*\)> .*$/\1/
 118                s/'\''/'\''\'\'\''/g
 119                s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
 120
 121                g
 122                s/^author [^<]* <[^>]*> \(.*\)$/\1/
 123                s/'\''/'\''\'\'\''/g
 124                s/.*/GIT_AUTHOR_DATE='\''&'\''/p
 125
 126                q
 127        }'
 128
 129        logmsg=`git show -s --pretty=raw --encoding="$encoding" "$commit"`
 130        set_author_env=`echo "$logmsg" |
 131        LANG=C LC_ALL=C sed -ne "$pick_author_script"`
 132        eval "$set_author_env"
 133        export GIT_AUTHOR_NAME
 134        export GIT_AUTHOR_EMAIL
 135        export GIT_AUTHOR_DATE
 136
 137        echo "$logmsg" |
 138        sed -e '1,/^$/d' -e 's/^    //'
 139        case "$replay" in
 140        '')
 141                echo "(cherry picked from commit $commit)"
 142                test "$rev" = "$commit" ||
 143                echo "(original 'git cherry-pick' arguments: $@)"
 144                ;;
 145        esac
 146        base=$prev next=$commit
 147        ;;
 148
 149esac >.msg
 150
 151eval GITHEAD_$head=HEAD
 152eval GITHEAD_$next='`git show -s \
 153        --pretty=oneline --encoding="$encoding" "$commit" |
 154        sed -e "s/^[^ ]* //"`'
 155export GITHEAD_$head GITHEAD_$next
 156
 157# This three way merge is an interesting one.  We are at
 158# $head, and would want to apply the change between $commit
 159# and $prev on top of us (when reverting), or the change between
 160# $prev and $commit on top of us (when cherry-picking or replaying).
 161
 162git-merge-recursive $base -- $head $next &&
 163result=$(git-write-tree 2>/dev/null) || {
 164        mv -f .msg "$GIT_DIR/MERGE_MSG"
 165        {
 166            echo '
 167Conflicts:
 168'
 169                git ls-files --unmerged |
 170                sed -e 's/^[^   ]*      /       /' |
 171                uniq
 172        } >>"$GIT_DIR/MERGE_MSG"
 173        echo >&2 "Automatic $me failed.  After resolving the conflicts,"
 174        echo >&2 "mark the corrected paths with 'git-add <paths>'"
 175        echo >&2 "and commit the result."
 176        case "$me" in
 177        cherry-pick)
 178                echo >&2 "You may choose to use the following when making"
 179                echo >&2 "the commit:"
 180                echo >&2 "$set_author_env"
 181        esac
 182        exit 1
 183}
 184echo >&2 "Finished one $me."
 185
 186# If we are cherry-pick, and if the merge did not result in
 187# hand-editing, we will hit this commit and inherit the original
 188# author date and name.
 189# If we are revert, or if our cherry-pick results in a hand merge,
 190# we had better say that the current user is responsible for that.
 191
 192case "$no_commit" in
 193'')
 194        git-commit -n -F .msg $edit
 195        rm -f .msg
 196        ;;
 197esac