add replay and log to the usage string of git-bisect
[gitweb.git] / git-revert.sh
index dfd914cf561c10f92ef2f3207446800de70c4834..49f00321b28833c24ebb78ea2104f34091d43017 100755 (executable)
@@ -3,29 +3,29 @@
 # Copyright (c) 2005 Linus Torvalds
 # Copyright (c) 2005 Junio C Hamano
 #
-. git-sh-setup || die "Not a git archive"
 
 case "$0" in
 *-revert* )
-       me=revert ;;
+       test -t 0 && edit=-e
+       replay=
+       me=revert
+       USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
 *-cherry-pick* )
-       me=cherry-pick ;;
+       replay=t
+       edit=
+       me=cherry-pick
+       USAGE='[--edit] [-n] [-r] [-x] <commit-ish>'  ;;
 * )
-       die "What are ou talking about?" ;;
+       echo >&2 "What are you talking about?"
+       exit 1 ;;
 esac
 
-usage () {
-       case "$me" in
-       cherry-pick)
-               die "usage git $me [-n] [-r] <commit-ish>"
-               ;;
-       revert)
-               die "usage git $me [-n] <commit-ish>"
-               ;;
-       esac
-}
+SUBDIRECTORY_OK=Yes ;# we will cd up
+. git-sh-setup
+require_work_tree
+cd_to_toplevel
 
-no_commit= replay=
+no_commit=
 while case "$#" in 0) break ;; esac
 do
        case "$1" in
@@ -33,8 +33,16 @@ do
            --no-commi|--no-commit)
                no_commit=t
                ;;
-       -r|--r|--re|--rep|--repl|--repla|--replay)
-               replay=t
+       -e|--e|--ed|--edi|--edit)
+               edit=-e
+               ;;
+       --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
+               edit=
+               ;;
+       -r)
+               : no-op ;;
+       -x|--i-really-want-to-expose-my-private-commit-object-name)
+               replay=
                ;;
        -*)
                usage
@@ -46,6 +54,8 @@ do
        shift
 done
 
+set_reflog_action "$me"
+
 test "$me,$replay" = "revert,t" && usage
 
 case "$no_commit" in
@@ -73,6 +83,8 @@ prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
        die "Cannot run $me a multi-parent commit."
 
+encoding=$(git config i18n.commitencoding || echo UTF-8)
+
 # "commit" is an existing commit.  We would want to apply
 # the difference it introduces since its first parent "prev"
 # on top of the current HEAD if we are cherry-pick.  Or the
@@ -80,12 +92,13 @@ git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
 
 case "$me" in
 revert)
-       git-rev-list --pretty=oneline --max-count=1 $commit |
+       git show -s --pretty=oneline --encoding="$encoding" $commit |
        sed -e '
                s/^[^ ]* /Revert "/
-               s/$/"/'
+               s/$/"/
+       '
        echo
-       echo "This reverts $commit commit."
+       echo "This reverts commit $commit."
        test "$rev" = "$commit" ||
        echo "(original 'git revert' arguments: $@)"
        base=$commit next=$prev
@@ -94,6 +107,7 @@ revert)
 cherry-pick)
        pick_author_script='
        /^author /{
+               s/'\''/'\''\\'\'\''/g
                h
                s/^author \([^<]*\) <[^>]*> .*$/\1/
                s/'\''/'\''\'\'\''/g
@@ -111,17 +125,20 @@ cherry-pick)
 
                q
        }'
-       set_author_env=`git-cat-file commit "$commit" |
-       sed -ne "$pick_author_script"`
+
+       logmsg=`git show -s --pretty=raw --encoding="$encoding" "$commit"`
+       set_author_env=`echo "$logmsg" |
+       LANG=C LC_ALL=C sed -ne "$pick_author_script"`
        eval "$set_author_env"
        export GIT_AUTHOR_NAME
        export GIT_AUTHOR_EMAIL
        export GIT_AUTHOR_DATE
 
-       git-cat-file commit $commit | sed -e '1,/^$/d'
+       echo "$logmsg" |
+       sed -e '1,/^$/d' -e 's/^    //'
        case "$replay" in
        '')
-               echo "(cherry picked from $commit commit)"
+               echo "(cherry picked from commit $commit)"
                test "$rev" = "$commit" ||
                echo "(original 'git cherry-pick' arguments: $@)"
                ;;
@@ -131,27 +148,38 @@ cherry-pick)
 
 esac >.msg
 
+eval GITHEAD_$head=HEAD
+eval GITHEAD_$next='`git show -s \
+       --pretty=oneline --encoding="$encoding" "$commit" |
+       sed -e "s/^[^ ]* //"`'
+export GITHEAD_$head GITHEAD_$next
+
 # This three way merge is an interesting one.  We are at
 # $head, and would want to apply the change between $commit
 # and $prev on top of us (when reverting), or the change between
 # $prev and $commit on top of us (when cherry-picking or replaying).
 
-echo >&2 "First trying simple merge strategy to $me."
-git-read-tree -m -u $base $head $next &&
+git-merge-recursive $base -- $head $next &&
 result=$(git-write-tree 2>/dev/null) || {
-    echo >&2 "Simple $me fails; trying Automatic $me."
-    git-merge-index -o git-merge-one-file -a || {
-           echo >&2 "Automatic $me failed.  After fixing it up,"
-           echo >&2 "you can use \"git commit -F .msg\""
-           case "$me" in
-           cherry-pick)
+       mv -f .msg "$GIT_DIR/MERGE_MSG"
+       {
+           echo '
+Conflicts:
+'
+               git ls-files --unmerged |
+               sed -e 's/^[^   ]*      /       /' |
+               uniq
+       } >>"$GIT_DIR/MERGE_MSG"
+       echo >&2 "Automatic $me failed.  After resolving the conflicts,"
+       echo >&2 "mark the corrected paths with 'git-add <paths>'"
+       echo >&2 "and commit the result."
+       case "$me" in
+       cherry-pick)
                echo >&2 "You may choose to use the following when making"
                echo >&2 "the commit:"
                echo >&2 "$set_author_env"
-           esac
-           exit 1
-    }
-    result=$(git-write-tree) || exit
+       esac
+       exit 1
 }
 echo >&2 "Finished one $me."
 
@@ -163,7 +191,7 @@ echo >&2 "Finished one $me."
 
 case "$no_commit" in
 '')
-       git-commit -n -F .msg
+       git-commit -n -F .msg $edit
        rm -f .msg
        ;;
 esac