You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
3.8 KiB
167 lines
3.8 KiB
20 years ago
|
#!/bin/sh
|
||
20 years ago
|
#
|
||
|
# Copyright (c) 2005 Linus Torvalds
|
||
|
# Copyright (c) 2005 Junio C Hamano
|
||
|
#
|
||
19 years ago
|
. git-sh-setup || die "Not a git archive"
|
||
20 years ago
|
|
||
20 years ago
|
case "$0" in
|
||
19 years ago
|
*-revert* )
|
||
20 years ago
|
me=revert ;;
|
||
19 years ago
|
*-cherry-pick* )
|
||
20 years ago
|
me=cherry-pick ;;
|
||
19 years ago
|
* )
|
||
|
die "What are ou talking about?" ;;
|
||
20 years ago
|
esac
|
||
|
|
||
|
usage () {
|
||
|
case "$me" in
|
||
|
cherry-pick)
|
||
|
die "usage git $me [-n] [-r] <commit-ish>"
|
||
|
;;
|
||
|
revert)
|
||
|
die "usage git $me [-n] <commit-ish>"
|
||
|
;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
no_commit= replay=
|
||
|
while case "$#" in 0) break ;; esac
|
||
|
do
|
||
|
case "$1" in
|
||
|
-n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\
|
||
|
--no-commi|--no-commit)
|
||
|
no_commit=t
|
||
|
;;
|
||
|
-r|--r|--re|--rep|--repl|--repla|--replay)
|
||
|
replay=t
|
||
|
;;
|
||
|
-*)
|
||
|
usage
|
||
|
;;
|
||
|
*)
|
||
|
break
|
||
|
;;
|
||
|
esac
|
||
|
shift
|
||
|
done
|
||
|
|
||
|
test "$me,$replay" = "revert,t" && usage
|
||
|
|
||
|
case "$no_commit" in
|
||
|
t)
|
||
|
# We do not intend to commit immediately. We just want to
|
||
|
# merge the differences in.
|
||
|
head=$(git-write-tree) ||
|
||
|
die "Your index file is unmerged."
|
||
|
;;
|
||
20 years ago
|
*)
|
||
20 years ago
|
check_clean_tree || die "Cannot run $me from a dirty tree."
|
||
|
head=$(git-rev-parse --verify HEAD) ||
|
||
|
die "You do not have a valid HEAD"
|
||
|
;;
|
||
20 years ago
|
esac
|
||
|
|
||
20 years ago
|
rev=$(git-rev-parse --verify "$@") &&
|
||
20 years ago
|
commit=$(git-rev-parse --verify "$rev^0") ||
|
||
|
die "Not a single commit $@"
|
||
|
prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
|
||
|
die "Cannot run $me a root commit"
|
||
|
git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
|
||
|
die "Cannot run $me a multi-parent commit."
|
||
|
|
||
|
# "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
|
||
|
# reverse of it if we are revert.
|
||
|
|
||
|
case "$me" in
|
||
|
revert)
|
||
|
git-rev-list --pretty=oneline --max-count=1 $commit |
|
||
|
sed -e '
|
||
|
s/^[^ ]* /Revert "/
|
||
|
s/$/"/'
|
||
|
echo
|
||
|
echo "This reverts $commit commit."
|
||
|
test "$rev" = "$commit" ||
|
||
|
echo "(original 'git revert' arguments: $@)"
|
||
|
base=$commit next=$prev
|
||
|
;;
|
||
|
|
||
|
cherry-pick)
|
||
|
pick_author_script='
|
||
|
/^author /{
|
||
|
h
|
||
|
s/^author \([^<]*\) <[^>]*> .*$/\1/
|
||
|
s/'\''/'\''\'\'\''/g
|
||
|
s/.*/GIT_AUTHOR_NAME='\''&'\''/p
|
||
|
|
||
|
g
|
||
|
s/^author [^<]* <\([^>]*\)> .*$/\1/
|
||
|
s/'\''/'\''\'\'\''/g
|
||
|
s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
|
||
|
|
||
|
g
|
||
|
s/^author [^<]* <[^>]*> \(.*\)$/\1/
|
||
|
s/'\''/'\''\'\'\''/g
|
||
|
s/.*/GIT_AUTHOR_DATE='\''&'\''/p
|
||
|
|
||
|
q
|
||
|
}'
|
||
|
set_author_env=`git-cat-file commit "$commit" |
|
||
|
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'
|
||
|
case "$replay" in
|
||
|
'')
|
||
|
echo "(cherry picked from $commit commit)"
|
||
|
test "$rev" = "$commit" ||
|
||
|
echo "(original 'git cherry-pick' arguments: $@)"
|
||
|
;;
|
||
|
esac
|
||
|
base=$prev next=$commit
|
||
|
;;
|
||
|
|
||
|
esac >.msg
|
||
|
|
||
|
# 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 &&
|
||
|
result=$(git-write-tree 2>/dev/null) || {
|
||
|
echo >&2 "Simple $me fails; trying Automatic $me."
|
||
19 years ago
|
git-merge-index -o git-merge-one-file -a || {
|
||
20 years ago
|
echo >&2 "Automatic $me failed. After fixing it up,"
|
||
|
echo >&2 "you can use \"git commit -F .msg\""
|
||
|
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
|
||
|
}
|
||
|
echo >&2 "Finished one $me."
|
||
|
|
||
|
# If we are cherry-pick, and if the merge did not result in
|
||
|
# hand-editing, we will hit this commit and inherit the original
|
||
|
# author date and name.
|
||
|
# If we are revert, or if our cherry-pick results in a hand merge,
|
||
|
# we had better say that the current user is responsible for that.
|
||
|
|
||
|
case "$no_commit" in
|
||
|
'')
|
||
19 years ago
|
git-commit -n -F .msg
|
||
20 years ago
|
rm -f .msg
|
||
|
;;
|
||
|
esac
|