2005-08-10 09:30:17 +04:00
|
|
|
#!/bin/sh
|
2005-08-28 10:53:27 +04:00
|
|
|
#
|
|
|
|
# Copyright (c) 2005 Linus Torvalds
|
|
|
|
# Copyright (c) 2005 Junio C Hamano
|
|
|
|
#
|
2005-08-10 09:30:17 +04:00
|
|
|
|
2005-08-28 10:53:27 +04:00
|
|
|
case "$0" in
|
2005-09-08 04:26:23 +04:00
|
|
|
*-revert* )
|
2005-11-24 03:56:55 +03:00
|
|
|
test -t 0 && edit=-e
|
2006-10-10 06:19:45 +04:00
|
|
|
replay=
|
2005-12-14 01:30:31 +03:00
|
|
|
me=revert
|
|
|
|
USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
|
2005-09-08 04:26:23 +04:00
|
|
|
*-cherry-pick* )
|
2006-10-10 06:19:45 +04:00
|
|
|
replay=t
|
2005-11-24 03:56:55 +03:00
|
|
|
edit=
|
2005-12-14 01:30:31 +03:00
|
|
|
me=cherry-pick
|
2006-10-06 04:54:14 +04:00
|
|
|
USAGE='[--edit] [-n] [-r] [-x] <commit-ish>' ;;
|
2005-09-08 04:26:23 +04:00
|
|
|
* )
|
2005-11-24 03:56:55 +03:00
|
|
|
die "What are you talking about?" ;;
|
2005-08-28 10:53:27 +04:00
|
|
|
esac
|
2005-12-14 01:30:31 +03:00
|
|
|
. git-sh-setup
|
2005-08-28 10:53:27 +04:00
|
|
|
|
2006-10-10 06:19:45 +04:00
|
|
|
no_commit=
|
2005-08-28 10:53:27 +04:00
|
|
|
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
|
|
|
|
;;
|
2005-11-24 03:56:55 +03:00
|
|
|
-e|--e|--ed|--edi|--edit)
|
|
|
|
edit=-e
|
|
|
|
;;
|
2005-12-09 02:28:05 +03:00
|
|
|
--n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
|
2005-11-24 03:56:55 +03:00
|
|
|
edit=
|
|
|
|
;;
|
2006-10-06 04:54:14 +04:00
|
|
|
-r)
|
|
|
|
: no-op ;;
|
|
|
|
-x|--i-really-want-to-expose-my-private-commit-object-name)
|
|
|
|
replay=
|
2005-08-28 10:53:27 +04:00
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
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."
|
|
|
|
;;
|
2005-08-10 09:30:17 +04:00
|
|
|
*)
|
2005-08-28 10:53:27 +04:00
|
|
|
head=$(git-rev-parse --verify HEAD) ||
|
|
|
|
die "You do not have a valid HEAD"
|
2005-09-25 09:52:32 +04:00
|
|
|
files=$(git-diff-index --cached --name-only $head) || exit
|
|
|
|
if [ "$files" ]; then
|
|
|
|
die "Dirty index: cannot $me (dirty: $files)"
|
|
|
|
fi
|
2005-08-28 10:53:27 +04:00
|
|
|
;;
|
2005-08-10 09:30:17 +04:00
|
|
|
esac
|
|
|
|
|
2005-08-25 01:31:36 +04:00
|
|
|
rev=$(git-rev-parse --verify "$@") &&
|
2005-08-28 10:53:27 +04:00
|
|
|
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
|
2006-07-13 09:51:25 +04:00
|
|
|
echo "This reverts commit $commit."
|
2005-08-28 10:53:27 +04:00
|
|
|
test "$rev" = "$commit" ||
|
|
|
|
echo "(original 'git revert' arguments: $@)"
|
|
|
|
base=$commit next=$prev
|
|
|
|
;;
|
|
|
|
|
|
|
|
cherry-pick)
|
|
|
|
pick_author_script='
|
|
|
|
/^author /{
|
2006-01-08 07:45:11 +03:00
|
|
|
s/'\''/'\''\\'\'\''/g
|
2005-08-28 10:53:27 +04:00
|
|
|
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" |
|
2005-11-11 10:34:08 +03:00
|
|
|
LANG=C LC_ALL=C sed -ne "$pick_author_script"`
|
2005-08-28 10:53:27 +04:00
|
|
|
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
|
|
|
|
'')
|
2006-10-06 04:54:14 +04:00
|
|
|
echo "(cherry picked from commit $commit)"
|
2005-08-28 10:53:27 +04:00
|
|
|
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."
|
2006-05-10 06:22:25 +04:00
|
|
|
git-read-tree -m -u --aggressive $base $head $next &&
|
2005-08-28 10:53:27 +04:00
|
|
|
result=$(git-write-tree 2>/dev/null) || {
|
|
|
|
echo >&2 "Simple $me fails; trying Automatic $me."
|
2005-09-08 04:26:23 +04:00
|
|
|
git-merge-index -o git-merge-one-file -a || {
|
2006-10-13 01:52:42 +04:00
|
|
|
mv -f .msg "$GIT_DIR/MERGE_MSG"
|
|
|
|
{
|
|
|
|
echo '
|
|
|
|
Conflicts:
|
|
|
|
'
|
|
|
|
git ls-files --unmerged |
|
|
|
|
sed -e 's/^[^ ]* / /' |
|
|
|
|
uniq
|
|
|
|
} >>"$GIT_DIR/MERGE_MSG"
|
2006-02-21 11:28:04 +03:00
|
|
|
echo >&2 "Automatic $me failed. After resolving the conflicts,"
|
|
|
|
echo >&2 "mark the corrected paths with 'git-update-index <paths>'"
|
2006-10-13 01:52:42 +04:00
|
|
|
echo >&2 "and commit the result."
|
2005-08-28 10:53:27 +04:00
|
|
|
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
|
|
|
|
'')
|
2005-11-24 03:56:55 +03:00
|
|
|
git-commit -n -F .msg $edit
|
2005-08-28 10:53:27 +04:00
|
|
|
rm -f .msg
|
|
|
|
;;
|
|
|
|
esac
|