2007-06-25 08:11:14 +08:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Copyright (c) 2006 Johannes E. Schindelin
|
|
|
|
|
|
|
|
# SHORT DESCRIPTION
|
|
|
|
#
|
|
|
|
# This script makes it easy to fix up commits in the middle of a series,
|
|
|
|
# and rearrange commits.
|
|
|
|
#
|
|
|
|
# The original idea comes from Eric W. Biederman, in
|
|
|
|
# http://article.gmane.org/gmane.comp.version-control.git/22407
|
|
|
|
|
2008-07-12 23:48:20 +08:00
|
|
|
OPTIONS_KEEPDASHDASH=
|
|
|
|
OPTIONS_SPEC="\
|
|
|
|
git-rebase [-i] [options] [--] <upstream> [<branch>]
|
|
|
|
git-rebase [-i] (--continue | --abort | --skip)
|
|
|
|
--
|
|
|
|
Available options are
|
|
|
|
v,verbose display a diffstat of what changed upstream
|
|
|
|
onto= rebase onto given branch instead of upstream
|
|
|
|
p,preserve-merges try to recreate merges instead of ignoring them
|
|
|
|
s,strategy= use the given merge strategy
|
2010-03-25 04:34:04 +08:00
|
|
|
no-ff cherry-pick all commits, even if unchanged
|
2008-07-12 23:48:20 +08:00
|
|
|
m,merge always used (no-op)
|
|
|
|
i,interactive always used (no-op)
|
|
|
|
Actions:
|
|
|
|
continue continue rebasing process
|
|
|
|
abort abort rebasing process and restore original branch
|
|
|
|
skip skip current patch and continue rebasing process
|
2008-10-06 13:14:29 +08:00
|
|
|
no-verify override pre-rebase hook from stopping the operation
|
2010-11-23 04:21:01 +08:00
|
|
|
verify allow pre-rebase hook to run
|
2009-01-03 06:28:28 +08:00
|
|
|
root rebase all reachable commmits up to the root(s)
|
2009-12-08 11:13:14 +08:00
|
|
|
autosquash move commits that begin with squash!/fixup! under -i
|
2008-07-12 23:48:20 +08:00
|
|
|
"
|
2007-06-25 08:11:14 +08:00
|
|
|
|
|
|
|
. git-sh-setup
|
|
|
|
require_work_tree
|
|
|
|
|
2008-07-16 09:33:44 +08:00
|
|
|
DOTEST="$GIT_DIR/rebase-merge"
|
2010-01-14 13:54:45 +08:00
|
|
|
|
|
|
|
# The file containing rebase commands, comments, and empty lines.
|
|
|
|
# This file is created by "git rebase -i" then edited by the user. As
|
|
|
|
# the lines are processed, they are removed from the front of this
|
|
|
|
# file and written to the tail of $DONE.
|
2007-07-29 07:44:40 +08:00
|
|
|
TODO="$DOTEST"/git-rebase-todo
|
2010-01-14 13:54:45 +08:00
|
|
|
|
|
|
|
# The rebase command lines that have already been processed. A line
|
|
|
|
# is moved here when it is first handled, before any associated user
|
|
|
|
# actions.
|
2007-06-25 08:11:14 +08:00
|
|
|
DONE="$DOTEST"/done
|
2010-01-14 13:54:45 +08:00
|
|
|
|
|
|
|
# The commit message that is planned to be used for any changes that
|
|
|
|
# need to be committed following a user interaction.
|
2007-07-22 01:09:41 +08:00
|
|
|
MSG="$DOTEST"/message
|
2010-01-14 13:54:45 +08:00
|
|
|
|
|
|
|
# The file into which is accumulated the suggested commit message for
|
|
|
|
# squash/fixup commands. When the first of a series of squash/fixups
|
|
|
|
# is seen, the file is created and the commit message from the
|
|
|
|
# previous commit and from the first squash/fixup commit are written
|
|
|
|
# to it. The commit message for each subsequent squash/fixup commit
|
|
|
|
# is appended to the file as it is processed.
|
|
|
|
#
|
|
|
|
# The first line of the file is of the form
|
|
|
|
# # This is a combination of $COUNT commits.
|
|
|
|
# where $COUNT is the number of commits whose messages have been
|
|
|
|
# written to the file so far (including the initial "pick" commit).
|
|
|
|
# Each time that a commit message is processed, this line is read and
|
|
|
|
# updated. It is deleted just before the combined commit is made.
|
2007-07-22 01:09:41 +08:00
|
|
|
SQUASH_MSG="$DOTEST"/message-squash
|
2010-01-14 13:54:45 +08:00
|
|
|
|
2010-01-14 13:54:55 +08:00
|
|
|
# If the current series of squash/fixups has not yet included a squash
|
|
|
|
# command, then this file exists and holds the commit message of the
|
|
|
|
# original "pick" commit. (If the series ends without a "squash"
|
|
|
|
# command, then this can be used as the commit message of the combined
|
|
|
|
# commit without opening the editor.)
|
|
|
|
FIXUP_MSG="$DOTEST"/message-fixup
|
|
|
|
|
2010-01-14 13:54:45 +08:00
|
|
|
# $REWRITTEN is the name of a directory containing files for each
|
|
|
|
# commit that is reachable by at least one merge base of $HEAD and
|
|
|
|
# $UPSTREAM. They are not necessarily rewritten, but their children
|
|
|
|
# might be. This ensures that commits on merged, but otherwise
|
|
|
|
# unrelated side branches are left alone. (Think "X" in the man page's
|
|
|
|
# example.)
|
2007-06-26 01:59:43 +08:00
|
|
|
REWRITTEN="$DOTEST"/rewritten
|
2010-01-14 13:54:45 +08:00
|
|
|
|
2008-10-06 12:26:52 +08:00
|
|
|
DROPPED="$DOTEST"/dropped
|
2010-01-14 13:54:46 +08:00
|
|
|
|
|
|
|
# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
|
|
|
|
# GIT_AUTHOR_DATE that will be used for the commit that is currently
|
|
|
|
# being rebased.
|
|
|
|
AUTHOR_SCRIPT="$DOTEST"/author-script
|
|
|
|
|
2010-01-14 13:54:47 +08:00
|
|
|
# When an "edit" rebase command is being processed, the SHA1 of the
|
|
|
|
# commit to be edited is recorded in this file. When "git rebase
|
|
|
|
# --continue" is executed, if there are any staged changes then they
|
|
|
|
# will be amended to the HEAD commit, but only provided the HEAD
|
|
|
|
# commit is still the commit to be edited. When any other rebase
|
|
|
|
# command is processed, this file is deleted.
|
|
|
|
AMEND="$DOTEST"/amend
|
|
|
|
|
2010-03-13 01:04:30 +08:00
|
|
|
# For the post-rewrite hook, we make a list of rewritten commits and
|
|
|
|
# their new sha1s. The rewritten-pending list keeps the sha1s of
|
|
|
|
# commits that have been processed, but not committed yet,
|
|
|
|
# e.g. because they are waiting for a 'squash' command.
|
|
|
|
REWRITTEN_LIST="$DOTEST"/rewritten-list
|
|
|
|
REWRITTEN_PENDING="$DOTEST"/rewritten-pending
|
|
|
|
|
2007-06-26 01:59:43 +08:00
|
|
|
PRESERVE_MERGES=
|
2007-06-25 08:11:14 +08:00
|
|
|
STRATEGY=
|
2008-07-12 23:48:20 +08:00
|
|
|
ONTO=
|
2007-06-25 08:11:14 +08:00
|
|
|
VERBOSE=
|
2008-10-06 13:14:29 +08:00
|
|
|
OK_TO_SKIP_PRE_REBASE=
|
2009-01-03 06:28:28 +08:00
|
|
|
REBASE_ROOT=
|
2009-12-08 11:13:14 +08:00
|
|
|
AUTOSQUASH=
|
2010-07-14 19:59:57 +08:00
|
|
|
test "$(git config --bool rebase.autosquash)" = "true" && AUTOSQUASH=t
|
2010-03-25 04:34:04 +08:00
|
|
|
NEVER_FF=
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2010-08-11 16:37:51 +08:00
|
|
|
GIT_CHERRY_PICK_HELP="\
|
|
|
|
hint: after resolving the conflicts, mark the corrected paths
|
|
|
|
hint: with 'git add <paths>' and run 'git rebase --continue'"
|
2007-11-28 16:06:36 +08:00
|
|
|
export GIT_CHERRY_PICK_HELP
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
warn () {
|
2010-07-23 03:15:11 +08:00
|
|
|
printf '%s\n' "$*" >&2
|
2007-06-25 08:11:14 +08:00
|
|
|
}
|
|
|
|
|
2007-07-24 06:45:49 +08:00
|
|
|
output () {
|
|
|
|
case "$VERBOSE" in
|
|
|
|
'')
|
2007-09-26 09:30:13 +08:00
|
|
|
output=$("$@" 2>&1 )
|
2007-07-24 06:45:49 +08:00
|
|
|
status=$?
|
2007-09-26 09:30:13 +08:00
|
|
|
test $status != 0 && printf "%s\n" "$output"
|
2007-07-24 06:45:49 +08:00
|
|
|
return $status
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-07-24 06:45:49 +08:00
|
|
|
*)
|
|
|
|
"$@"
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-07-24 06:45:49 +08:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2010-01-14 13:54:51 +08:00
|
|
|
# Output the commit message for the specified commit.
|
|
|
|
commit_message () {
|
|
|
|
git cat-file commit "$1" | sed "1,/^$/d"
|
|
|
|
}
|
|
|
|
|
2008-10-06 13:14:24 +08:00
|
|
|
run_pre_rebase_hook () {
|
2008-10-06 13:14:29 +08:00
|
|
|
if test -z "$OK_TO_SKIP_PRE_REBASE" &&
|
|
|
|
test -x "$GIT_DIR/hooks/pre-rebase"
|
2008-10-06 13:14:24 +08:00
|
|
|
then
|
|
|
|
"$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
|
|
|
|
echo >&2 "The pre-rebase hook refused to rebase."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
|
|
|
|
ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
|
|
|
|
|
|
|
|
comment_for_reflog () {
|
|
|
|
case "$ORIG_REFLOG_ACTION" in
|
|
|
|
''|rebase*)
|
|
|
|
GIT_REFLOG_ACTION="rebase -i ($1)"
|
|
|
|
export GIT_REFLOG_ACTION
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-06-25 08:11:14 +08:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2008-01-15 12:01:21 +08:00
|
|
|
last_count=
|
2007-06-25 08:11:14 +08:00
|
|
|
mark_action_done () {
|
|
|
|
sed -e 1q < "$TODO" >> "$DONE"
|
|
|
|
sed -e 1d < "$TODO" >> "$TODO".new
|
|
|
|
mv -f "$TODO".new "$TODO"
|
2009-11-24 07:56:32 +08:00
|
|
|
count=$(sane_grep -c '^[^#]' < "$DONE")
|
|
|
|
total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
|
2008-01-15 12:01:21 +08:00
|
|
|
if test "$last_count" != "$count"
|
|
|
|
then
|
|
|
|
last_count=$count
|
|
|
|
printf "Rebasing (%d/%d)\r" $count $total
|
|
|
|
test -z "$VERBOSE" || echo
|
|
|
|
fi
|
2007-06-25 08:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
make_patch () {
|
2008-12-23 05:16:51 +08:00
|
|
|
sha1_and_parents="$(git rev-list --parents -1 "$1")"
|
|
|
|
case "$sha1_and_parents" in
|
|
|
|
?*' '?*' '?*)
|
|
|
|
git diff --cc $sha1_and_parents
|
|
|
|
;;
|
|
|
|
?*' '?*)
|
|
|
|
git diff-tree -p "$1^!"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Root commit"
|
|
|
|
;;
|
|
|
|
esac > "$DOTEST"/patch
|
2010-01-14 13:54:44 +08:00
|
|
|
test -f "$MSG" ||
|
2010-01-14 13:54:51 +08:00
|
|
|
commit_message "$1" > "$MSG"
|
2010-01-14 13:54:46 +08:00
|
|
|
test -f "$AUTHOR_SCRIPT" ||
|
|
|
|
get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
|
2007-06-25 08:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
die_with_patch () {
|
2010-03-13 01:04:30 +08:00
|
|
|
echo "$1" > "$DOTEST"/stopped-sha
|
2007-06-25 08:11:14 +08:00
|
|
|
make_patch "$1"
|
2007-11-22 19:18:10 +08:00
|
|
|
git rerere
|
2007-06-25 08:11:14 +08:00
|
|
|
die "$2"
|
|
|
|
}
|
|
|
|
|
2007-06-26 01:56:55 +08:00
|
|
|
die_abort () {
|
|
|
|
rm -rf "$DOTEST"
|
|
|
|
die "$1"
|
|
|
|
}
|
|
|
|
|
2007-09-25 23:42:51 +08:00
|
|
|
has_action () {
|
2009-11-24 07:56:32 +08:00
|
|
|
sane_grep '^[^#]' "$1" >/dev/null
|
2007-09-25 23:42:51 +08:00
|
|
|
}
|
|
|
|
|
2010-01-14 13:54:53 +08:00
|
|
|
# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
|
|
|
|
# GIT_AUTHOR_DATE exported from the current environment.
|
|
|
|
do_with_author () {
|
2010-01-23 05:09:24 +08:00
|
|
|
(
|
|
|
|
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
|
|
|
|
"$@"
|
|
|
|
)
|
2010-01-14 13:54:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
pick_one () {
|
2010-03-07 04:34:46 +08:00
|
|
|
ff=--ff
|
|
|
|
case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
|
2010-04-04 03:28:44 +08:00
|
|
|
case "$NEVER_FF" in '') ;; ?*) ff= ;; esac
|
2007-07-24 06:45:49 +08:00
|
|
|
output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
|
2007-06-26 01:59:43 +08:00
|
|
|
test -d "$REWRITTEN" &&
|
|
|
|
pick_one_preserving_merges "$@" && return
|
2010-01-14 13:54:43 +08:00
|
|
|
if test -n "$REBASE_ROOT"
|
2009-01-03 06:28:28 +08:00
|
|
|
then
|
|
|
|
output git cherry-pick "$@"
|
|
|
|
return
|
|
|
|
fi
|
2010-03-07 04:34:46 +08:00
|
|
|
output git cherry-pick $ff "$@"
|
2007-06-25 08:11:14 +08:00
|
|
|
}
|
|
|
|
|
2007-06-26 01:59:43 +08:00
|
|
|
pick_one_preserving_merges () {
|
2008-08-14 05:41:23 +08:00
|
|
|
fast_forward=t
|
|
|
|
case "$1" in
|
|
|
|
-n)
|
|
|
|
fast_forward=f
|
|
|
|
sha1=$2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
sha1=$1
|
|
|
|
;;
|
|
|
|
esac
|
2007-06-26 01:59:43 +08:00
|
|
|
sha1=$(git rev-parse $sha1)
|
|
|
|
|
2007-07-24 10:18:28 +08:00
|
|
|
if test -f "$DOTEST"/current-commit
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
2008-10-23 02:59:30 +08:00
|
|
|
if test "$fast_forward" = t
|
2008-10-15 15:44:36 +08:00
|
|
|
then
|
2010-07-06 13:16:15 +08:00
|
|
|
while read current_commit
|
2008-10-15 15:44:36 +08:00
|
|
|
do
|
|
|
|
git rev-parse HEAD > "$REWRITTEN"/$current_commit
|
2010-07-06 13:16:15 +08:00
|
|
|
done <"$DOTEST"/current-commit
|
2008-10-15 15:44:36 +08:00
|
|
|
rm "$DOTEST"/current-commit ||
|
|
|
|
die "Cannot write current commit's replacement sha1"
|
|
|
|
fi
|
2007-06-26 01:59:43 +08:00
|
|
|
fi
|
|
|
|
|
2008-10-15 15:44:36 +08:00
|
|
|
echo $sha1 >> "$DOTEST"/current-commit
|
2008-08-14 05:41:24 +08:00
|
|
|
|
2007-06-26 01:59:43 +08:00
|
|
|
# rewrite parents; if none were rewritten, we can fast-forward.
|
|
|
|
new_parents=
|
2009-01-03 06:28:28 +08:00
|
|
|
pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
|
|
|
|
if test "$pend" = " "
|
|
|
|
then
|
|
|
|
pend=" root"
|
|
|
|
fi
|
2008-10-06 12:26:52 +08:00
|
|
|
while [ "$pend" != "" ]
|
2007-06-26 01:59:43 +08:00
|
|
|
do
|
2008-10-06 12:26:52 +08:00
|
|
|
p=$(expr "$pend" : ' \([^ ]*\)')
|
|
|
|
pend="${pend# $p}"
|
|
|
|
|
2007-07-24 10:18:28 +08:00
|
|
|
if test -f "$REWRITTEN"/$p
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
|
|
|
new_p=$(cat "$REWRITTEN"/$p)
|
2008-10-15 15:44:40 +08:00
|
|
|
|
|
|
|
# If the todo reordered commits, and our parent is marked for
|
|
|
|
# rewriting, but hasn't been gotten to yet, assume the user meant to
|
|
|
|
# drop it on top of the current HEAD
|
|
|
|
if test -z "$new_p"
|
|
|
|
then
|
|
|
|
new_p=$(git rev-parse HEAD)
|
|
|
|
fi
|
|
|
|
|
2007-06-26 01:59:43 +08:00
|
|
|
test $p != $new_p && fast_forward=f
|
|
|
|
case "$new_parents" in
|
|
|
|
*$new_p*)
|
|
|
|
;; # do nothing; that parent is already there
|
|
|
|
*)
|
|
|
|
new_parents="$new_parents $new_p"
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-06-26 01:59:43 +08:00
|
|
|
esac
|
2008-07-16 09:51:49 +08:00
|
|
|
else
|
2008-10-06 12:26:52 +08:00
|
|
|
if test -f "$DROPPED"/$p
|
|
|
|
then
|
|
|
|
fast_forward=f
|
2009-01-03 06:28:28 +08:00
|
|
|
replacement="$(cat "$DROPPED"/$p)"
|
|
|
|
test -z "$replacement" && replacement=root
|
|
|
|
pend=" $replacement$pend"
|
2008-10-06 12:26:52 +08:00
|
|
|
else
|
|
|
|
new_parents="$new_parents $p"
|
|
|
|
fi
|
2007-06-26 01:59:43 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
case $fast_forward in
|
|
|
|
t)
|
2009-10-24 16:31:32 +08:00
|
|
|
output warn "Fast-forward to $sha1"
|
2008-08-14 05:41:23 +08:00
|
|
|
output git reset --hard $sha1 ||
|
2009-10-24 16:31:32 +08:00
|
|
|
die "Cannot fast-forward to $sha1"
|
2007-06-26 01:59:43 +08:00
|
|
|
;;
|
|
|
|
f)
|
2007-09-25 23:42:51 +08:00
|
|
|
first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
|
2008-10-15 15:44:37 +08:00
|
|
|
|
|
|
|
if [ "$1" != "-n" ]
|
|
|
|
then
|
|
|
|
# detach HEAD to current parent
|
|
|
|
output git checkout $first_parent 2> /dev/null ||
|
|
|
|
die "Cannot move HEAD to $first_parent"
|
|
|
|
fi
|
2007-06-26 01:59:43 +08:00
|
|
|
|
|
|
|
case "$new_parents" in
|
2007-09-25 23:42:51 +08:00
|
|
|
' '*' '*)
|
2008-10-15 15:44:37 +08:00
|
|
|
test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
|
|
|
|
|
2007-06-26 01:59:43 +08:00
|
|
|
# redo merge
|
|
|
|
author_script=$(get_author_ident_from_commit $sha1)
|
|
|
|
eval "$author_script"
|
2010-01-14 13:54:51 +08:00
|
|
|
msg="$(commit_message $sha1)"
|
2007-10-31 10:20:32 +08:00
|
|
|
# No point in merging the first parent, that's HEAD
|
|
|
|
new_parents=${new_parents# $first_parent}
|
2010-01-14 13:54:53 +08:00
|
|
|
if ! do_with_author output \
|
|
|
|
git merge $STRATEGY -m "$msg" $new_parents
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
2007-09-25 23:42:51 +08:00
|
|
|
printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
|
2008-12-15 18:05:31 +08:00
|
|
|
die_with_patch $sha1 "Error redoing merge $sha1"
|
2007-06-26 01:59:43 +08:00
|
|
|
fi
|
2010-03-13 01:04:30 +08:00
|
|
|
echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST"
|
2007-06-26 01:59:43 +08:00
|
|
|
;;
|
|
|
|
*)
|
2007-10-31 10:20:31 +08:00
|
|
|
output git cherry-pick "$@" ||
|
2007-06-26 01:59:43 +08:00
|
|
|
die_with_patch $sha1 "Could not pick $sha1"
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-06-26 01:59:43 +08:00
|
|
|
esac
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-06-26 01:59:43 +08:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2007-07-22 01:09:41 +08:00
|
|
|
nth_string () {
|
|
|
|
case "$1" in
|
|
|
|
*1[0-9]|*[04-9]) echo "$1"th;;
|
|
|
|
*1) echo "$1"st;;
|
|
|
|
*2) echo "$1"nd;;
|
|
|
|
*3) echo "$1"rd;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2010-01-14 13:54:55 +08:00
|
|
|
update_squash_messages () {
|
2007-07-24 10:18:28 +08:00
|
|
|
if test -f "$SQUASH_MSG"; then
|
2010-01-14 13:54:54 +08:00
|
|
|
mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
|
2010-01-14 13:54:50 +08:00
|
|
|
COUNT=$(($(sed -n \
|
|
|
|
-e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
|
2010-01-14 13:54:54 +08:00
|
|
|
-e "q" < "$SQUASH_MSG".bak)+1))
|
|
|
|
{
|
|
|
|
echo "# This is a combination of $COUNT commits."
|
|
|
|
sed -e 1d -e '2,/^./{
|
|
|
|
/^$/d
|
|
|
|
}' <"$SQUASH_MSG".bak
|
2010-01-27 08:29:30 +08:00
|
|
|
} >"$SQUASH_MSG"
|
2007-07-22 01:09:41 +08:00
|
|
|
else
|
2010-01-14 13:54:55 +08:00
|
|
|
commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
|
2007-07-22 01:09:41 +08:00
|
|
|
COUNT=2
|
2010-01-14 13:54:54 +08:00
|
|
|
{
|
|
|
|
echo "# This is a combination of 2 commits."
|
|
|
|
echo "# The first commit's message is:"
|
|
|
|
echo
|
2010-01-14 13:54:55 +08:00
|
|
|
cat "$FIXUP_MSG"
|
2010-01-27 08:29:30 +08:00
|
|
|
} >"$SQUASH_MSG"
|
2007-07-22 01:09:41 +08:00
|
|
|
fi
|
2009-12-07 17:20:59 +08:00
|
|
|
case $1 in
|
|
|
|
squash)
|
2010-01-14 13:54:55 +08:00
|
|
|
rm -f "$FIXUP_MSG"
|
2009-12-07 17:20:59 +08:00
|
|
|
echo
|
|
|
|
echo "# This is the $(nth_string $COUNT) commit message:"
|
|
|
|
echo
|
2010-01-14 13:54:51 +08:00
|
|
|
commit_message $2
|
2009-12-07 17:20:59 +08:00
|
|
|
;;
|
|
|
|
fixup)
|
|
|
|
echo
|
|
|
|
echo "# The $(nth_string $COUNT) commit message will be skipped:"
|
|
|
|
echo
|
2010-01-14 13:54:51 +08:00
|
|
|
commit_message $2 | sed -e 's/^/# /'
|
2009-12-07 17:20:59 +08:00
|
|
|
;;
|
2010-01-27 08:29:30 +08:00
|
|
|
esac >>"$SQUASH_MSG"
|
2007-07-22 01:09:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
peek_next_command () {
|
2010-01-27 07:08:31 +08:00
|
|
|
sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
|
2007-07-22 01:09:41 +08:00
|
|
|
}
|
|
|
|
|
2010-01-14 13:54:57 +08:00
|
|
|
# A squash/fixup has failed. Prepare the long version of the squash
|
|
|
|
# commit message, then die_with_patch. This code path requires the
|
|
|
|
# user to edit the combined commit message for all commits that have
|
|
|
|
# been squashed/fixedup so far. So also erase the old squash
|
|
|
|
# messages, effectively causing the combined commit to be used as the
|
|
|
|
# new basis for any further squash/fixups. Args: sha1 rest
|
|
|
|
die_failed_squash() {
|
|
|
|
mv "$SQUASH_MSG" "$MSG" || exit
|
|
|
|
rm -f "$FIXUP_MSG"
|
|
|
|
cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
|
|
|
|
warn
|
|
|
|
warn "Could not apply $1... $2"
|
|
|
|
die_with_patch $1 ""
|
|
|
|
}
|
|
|
|
|
2010-03-13 01:04:30 +08:00
|
|
|
flush_rewritten_pending() {
|
|
|
|
test -s "$REWRITTEN_PENDING" || return
|
|
|
|
newsha1="$(git rev-parse HEAD^0)"
|
|
|
|
sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST"
|
|
|
|
rm -f "$REWRITTEN_PENDING"
|
|
|
|
}
|
|
|
|
|
|
|
|
record_in_rewritten() {
|
|
|
|
oldsha1="$(git rev-parse $1)"
|
|
|
|
echo "$oldsha1" >> "$REWRITTEN_PENDING"
|
|
|
|
|
|
|
|
case "$(peek_next_command)" in
|
2010-07-06 13:16:15 +08:00
|
|
|
squash|s|fixup|f)
|
2010-03-13 01:04:30 +08:00
|
|
|
;;
|
2010-07-06 13:16:15 +08:00
|
|
|
*)
|
2010-03-13 01:04:30 +08:00
|
|
|
flush_rewritten_pending
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
do_next () {
|
2010-01-14 13:54:47 +08:00
|
|
|
rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
|
2010-07-06 14:08:36 +08:00
|
|
|
read -r command sha1 rest < "$TODO"
|
2007-06-25 08:11:14 +08:00
|
|
|
case "$command" in
|
2008-10-10 19:42:12 +08:00
|
|
|
'#'*|''|noop)
|
2007-06-25 08:11:14 +08:00
|
|
|
mark_action_done
|
|
|
|
;;
|
2007-09-29 10:32:11 +08:00
|
|
|
pick|p)
|
2007-06-25 08:11:14 +08:00
|
|
|
comment_for_reflog pick
|
|
|
|
|
|
|
|
mark_action_done
|
|
|
|
pick_one $sha1 ||
|
|
|
|
die_with_patch $sha1 "Could not apply $sha1... $rest"
|
2010-03-13 01:04:30 +08:00
|
|
|
record_in_rewritten $sha1
|
2007-06-25 08:11:14 +08:00
|
|
|
;;
|
2009-10-07 14:13:23 +08:00
|
|
|
reword|r)
|
|
|
|
comment_for_reflog reword
|
|
|
|
|
|
|
|
mark_action_done
|
|
|
|
pick_one $sha1 ||
|
|
|
|
die_with_patch $sha1 "Could not apply $sha1... $rest"
|
2010-03-13 01:04:28 +08:00
|
|
|
git commit --amend --no-post-rewrite
|
2010-03-13 01:04:30 +08:00
|
|
|
record_in_rewritten $sha1
|
2009-10-07 14:13:23 +08:00
|
|
|
;;
|
2007-09-29 10:32:11 +08:00
|
|
|
edit|e)
|
2007-06-25 08:11:14 +08:00
|
|
|
comment_for_reflog edit
|
|
|
|
|
|
|
|
mark_action_done
|
|
|
|
pick_one $sha1 ||
|
|
|
|
die_with_patch $sha1 "Could not apply $sha1... $rest"
|
2010-03-29 03:36:00 +08:00
|
|
|
echo "$sha1" > "$DOTEST"/stopped-sha
|
2007-06-25 08:11:14 +08:00
|
|
|
make_patch $sha1
|
2010-01-14 13:54:47 +08:00
|
|
|
git rev-parse --verify HEAD > "$AMEND"
|
2008-07-23 15:46:35 +08:00
|
|
|
warn "Stopped at $sha1... $rest"
|
2007-06-25 08:11:14 +08:00
|
|
|
warn "You can amend the commit now, with"
|
|
|
|
warn
|
|
|
|
warn " git commit --amend"
|
|
|
|
warn
|
2008-02-27 20:50:22 +08:00
|
|
|
warn "Once you are satisfied with your changes, run"
|
|
|
|
warn
|
|
|
|
warn " git rebase --continue"
|
|
|
|
warn
|
2007-06-25 08:11:14 +08:00
|
|
|
exit 0
|
|
|
|
;;
|
2009-12-07 17:20:59 +08:00
|
|
|
squash|s|fixup|f)
|
|
|
|
case "$command" in
|
|
|
|
squash|s)
|
|
|
|
squash_style=squash
|
|
|
|
;;
|
|
|
|
fixup|f)
|
|
|
|
squash_style=fixup
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
comment_for_reflog $squash_style
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2009-01-13 07:09:36 +08:00
|
|
|
test -f "$DONE" && has_action "$DONE" ||
|
2009-12-07 17:20:59 +08:00
|
|
|
die "Cannot '$squash_style' without a previous commit"
|
2007-06-25 08:11:14 +08:00
|
|
|
|
|
|
|
mark_action_done
|
2010-01-14 13:54:55 +08:00
|
|
|
update_squash_messages $squash_style $sha1
|
2008-10-03 08:08:21 +08:00
|
|
|
author_script=$(get_author_ident_from_commit HEAD)
|
2010-01-14 13:54:52 +08:00
|
|
|
echo "$author_script" > "$AUTHOR_SCRIPT"
|
|
|
|
eval "$author_script"
|
2008-10-03 08:08:21 +08:00
|
|
|
output git reset --soft HEAD^
|
2010-01-14 13:54:57 +08:00
|
|
|
pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
|
2007-07-22 01:09:41 +08:00
|
|
|
case "$(peek_next_command)" in
|
2009-12-07 17:20:59 +08:00
|
|
|
squash|s|fixup|f)
|
2010-01-14 13:54:55 +08:00
|
|
|
# This is an intermediate commit; its message will only be
|
|
|
|
# used in case of trouble. So use the long version:
|
2010-01-14 13:54:57 +08:00
|
|
|
do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
|
|
|
|
die_failed_squash $sha1 "$rest"
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-07-22 01:09:41 +08:00
|
|
|
*)
|
2010-01-14 13:54:55 +08:00
|
|
|
# This is the final command of this squash/fixup group
|
2010-01-14 13:54:57 +08:00
|
|
|
if test -f "$FIXUP_MSG"
|
2010-01-14 13:54:55 +08:00
|
|
|
then
|
2010-01-14 13:54:57 +08:00
|
|
|
do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
|
|
|
|
die_failed_squash $sha1 "$rest"
|
|
|
|
else
|
|
|
|
cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
|
|
|
|
rm -f "$GIT_DIR"/MERGE_MSG
|
|
|
|
do_with_author git commit --no-verify -e ||
|
|
|
|
die_failed_squash $sha1 "$rest"
|
2010-01-14 13:54:55 +08:00
|
|
|
fi
|
2010-01-14 13:54:57 +08:00
|
|
|
rm -f "$SQUASH_MSG" "$FIXUP_MSG"
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-07-22 01:09:41 +08:00
|
|
|
esac
|
2010-03-13 01:04:30 +08:00
|
|
|
record_in_rewritten $sha1
|
2007-06-25 08:11:14 +08:00
|
|
|
;;
|
2010-08-10 23:17:51 +08:00
|
|
|
x|"exec")
|
|
|
|
read -r command rest < "$TODO"
|
|
|
|
mark_action_done
|
|
|
|
printf 'Executing: %s\n' "$rest"
|
|
|
|
# "exec" command doesn't take a sha1 in the todo-list.
|
|
|
|
# => can't just use $sha1 here.
|
|
|
|
git rev-parse --verify HEAD > "$DOTEST"/stopped-sha
|
|
|
|
${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
|
|
|
|
status=$?
|
|
|
|
if test "$status" -ne 0
|
|
|
|
then
|
|
|
|
warn "Execution failed: $rest"
|
|
|
|
warn "You can fix the problem, and then run"
|
|
|
|
warn
|
|
|
|
warn " git rebase --continue"
|
|
|
|
warn
|
|
|
|
exit "$status"
|
|
|
|
fi
|
|
|
|
# Run in subshell because require_clean_work_tree can die.
|
2010-10-19 22:39:28 +08:00
|
|
|
if ! (require_clean_work_tree "rebase")
|
2010-08-10 23:17:51 +08:00
|
|
|
then
|
|
|
|
warn "Commit or stash your changes, and then run"
|
|
|
|
warn
|
|
|
|
warn " git rebase --continue"
|
|
|
|
warn
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
2007-06-25 08:11:14 +08:00
|
|
|
*)
|
|
|
|
warn "Unknown command: $command $sha1 $rest"
|
2009-10-27 22:58:14 +08:00
|
|
|
if git rev-parse --verify -q "$sha1" >/dev/null
|
|
|
|
then
|
|
|
|
die_with_patch $sha1 "Please fix this in the file $TODO."
|
|
|
|
else
|
|
|
|
die "Please fix this in the file $TODO."
|
|
|
|
fi
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-06-25 08:11:14 +08:00
|
|
|
esac
|
|
|
|
test -s "$TODO" && return
|
|
|
|
|
2007-06-26 01:58:28 +08:00
|
|
|
comment_for_reflog finish &&
|
|
|
|
HEADNAME=$(cat "$DOTEST"/head-name) &&
|
|
|
|
OLDHEAD=$(cat "$DOTEST"/head) &&
|
|
|
|
SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
|
2008-10-15 15:44:35 +08:00
|
|
|
NEWHEAD=$(git rev-parse HEAD) &&
|
2007-09-25 23:43:15 +08:00
|
|
|
case $HEADNAME in
|
|
|
|
refs/*)
|
2009-06-12 05:14:00 +08:00
|
|
|
message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
|
2007-09-25 23:43:15 +08:00
|
|
|
git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
|
|
|
|
git symbolic-ref HEAD $HEADNAME
|
|
|
|
;;
|
|
|
|
esac && {
|
2007-07-08 10:02:13 +08:00
|
|
|
test ! -f "$DOTEST"/verbose ||
|
2007-10-09 20:59:43 +08:00
|
|
|
git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
|
2007-07-08 10:02:13 +08:00
|
|
|
} &&
|
2010-03-13 01:04:33 +08:00
|
|
|
{
|
2010-04-18 20:01:45 +08:00
|
|
|
test -s "$REWRITTEN_LIST" &&
|
2010-03-13 01:04:33 +08:00
|
|
|
git notes copy --for-rewrite=rebase < "$REWRITTEN_LIST" ||
|
|
|
|
true # we don't care if this copying failed
|
|
|
|
} &&
|
2010-03-13 01:04:30 +08:00
|
|
|
if test -x "$GIT_DIR"/hooks/post-rewrite &&
|
|
|
|
test -s "$REWRITTEN_LIST"; then
|
|
|
|
"$GIT_DIR"/hooks/post-rewrite rebase < "$REWRITTEN_LIST"
|
|
|
|
true # we don't care if this hook failed
|
|
|
|
fi &&
|
2007-06-25 08:11:14 +08:00
|
|
|
rm -rf "$DOTEST" &&
|
2007-09-25 23:43:15 +08:00
|
|
|
git gc --auto &&
|
2007-06-25 08:11:14 +08:00
|
|
|
warn "Successfully rebased and updated $HEADNAME."
|
|
|
|
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
|
|
|
do_rest () {
|
|
|
|
while :
|
|
|
|
do
|
|
|
|
do_next
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-03-03 17:55:31 +08:00
|
|
|
# skip picking commits whose parents are unchanged
|
|
|
|
skip_unnecessary_picks () {
|
|
|
|
fd=3
|
git-rebase--interactive.sh: rework skip_unnecessary_picks
Commit cd035b1c introduced the exec command to interactive rebase. In
doing so, it modified the way that skip_unnecessary_picks iterates through
the list of rebase commands so that it avoided collapsing multiple spaces
into a single space. This is necessary for example if the argument to the
exec command contains a path with multiple spaces in it.
The way it did this was by reading each line of rebase commands into a
single variable, and then breaking the individual components out using
echo, sed, and cut. It used the individual broken-out components for
decision making, and was still able to write the original line to the
output file from the variable it had saved it in. But, since we only
really need to look at anything other than the first element of the line
when a 'pick' command is encountered, and even that is only necessary when
we are still searching for "unnecessary" picks, and since newer rebase
commands like 'exec' may not even require a sha1 field, let's make our read
statement parse its input into a "command" variable, and a "rest" variable,
and then only break out the sha1 from $rest, and call git-rev-parse, when
absolutely necessary.
I think this future proofs this subroutine, avoids calling git-rev-parse
unnecessarily, and possibly with bogus arguments, and still accomplishes
the goal of not mangling the $rest of the rebase command.
Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-08-14 04:47:34 +08:00
|
|
|
while read -r command rest
|
2009-03-03 17:55:31 +08:00
|
|
|
do
|
|
|
|
# fd=3 means we skip the command
|
git-rebase--interactive.sh: rework skip_unnecessary_picks
Commit cd035b1c introduced the exec command to interactive rebase. In
doing so, it modified the way that skip_unnecessary_picks iterates through
the list of rebase commands so that it avoided collapsing multiple spaces
into a single space. This is necessary for example if the argument to the
exec command contains a path with multiple spaces in it.
The way it did this was by reading each line of rebase commands into a
single variable, and then breaking the individual components out using
echo, sed, and cut. It used the individual broken-out components for
decision making, and was still able to write the original line to the
output file from the variable it had saved it in. But, since we only
really need to look at anything other than the first element of the line
when a 'pick' command is encountered, and even that is only necessary when
we are still searching for "unnecessary" picks, and since newer rebase
commands like 'exec' may not even require a sha1 field, let's make our read
statement parse its input into a "command" variable, and a "rest" variable,
and then only break out the sha1 from $rest, and call git-rev-parse, when
absolutely necessary.
I think this future proofs this subroutine, avoids calling git-rev-parse
unnecessarily, and possibly with bogus arguments, and still accomplishes
the goal of not mangling the $rest of the rebase command.
Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-08-14 04:47:34 +08:00
|
|
|
case "$fd,$command" in
|
|
|
|
3,pick|3,p)
|
2009-03-03 17:55:31 +08:00
|
|
|
# pick a commit whose parent is current $ONTO -> skip
|
2010-09-18 05:42:51 +08:00
|
|
|
sha1=${rest%% *}
|
git-rebase--interactive.sh: rework skip_unnecessary_picks
Commit cd035b1c introduced the exec command to interactive rebase. In
doing so, it modified the way that skip_unnecessary_picks iterates through
the list of rebase commands so that it avoided collapsing multiple spaces
into a single space. This is necessary for example if the argument to the
exec command contains a path with multiple spaces in it.
The way it did this was by reading each line of rebase commands into a
single variable, and then breaking the individual components out using
echo, sed, and cut. It used the individual broken-out components for
decision making, and was still able to write the original line to the
output file from the variable it had saved it in. But, since we only
really need to look at anything other than the first element of the line
when a 'pick' command is encountered, and even that is only necessary when
we are still searching for "unnecessary" picks, and since newer rebase
commands like 'exec' may not even require a sha1 field, let's make our read
statement parse its input into a "command" variable, and a "rest" variable,
and then only break out the sha1 from $rest, and call git-rev-parse, when
absolutely necessary.
I think this future proofs this subroutine, avoids calling git-rev-parse
unnecessarily, and possibly with bogus arguments, and still accomplishes
the goal of not mangling the $rest of the rebase command.
Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-08-14 04:47:34 +08:00
|
|
|
case "$(git rev-parse --verify --quiet "$sha1"^)" in
|
|
|
|
"$ONTO"*)
|
|
|
|
ONTO=$sha1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
fd=1
|
|
|
|
;;
|
|
|
|
esac
|
2009-03-03 17:55:31 +08:00
|
|
|
;;
|
git-rebase--interactive.sh: rework skip_unnecessary_picks
Commit cd035b1c introduced the exec command to interactive rebase. In
doing so, it modified the way that skip_unnecessary_picks iterates through
the list of rebase commands so that it avoided collapsing multiple spaces
into a single space. This is necessary for example if the argument to the
exec command contains a path with multiple spaces in it.
The way it did this was by reading each line of rebase commands into a
single variable, and then breaking the individual components out using
echo, sed, and cut. It used the individual broken-out components for
decision making, and was still able to write the original line to the
output file from the variable it had saved it in. But, since we only
really need to look at anything other than the first element of the line
when a 'pick' command is encountered, and even that is only necessary when
we are still searching for "unnecessary" picks, and since newer rebase
commands like 'exec' may not even require a sha1 field, let's make our read
statement parse its input into a "command" variable, and a "rest" variable,
and then only break out the sha1 from $rest, and call git-rev-parse, when
absolutely necessary.
I think this future proofs this subroutine, avoids calling git-rev-parse
unnecessarily, and possibly with bogus arguments, and still accomplishes
the goal of not mangling the $rest of the rebase command.
Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-08-14 04:47:34 +08:00
|
|
|
3,#*|3,)
|
2009-03-03 17:55:31 +08:00
|
|
|
# copy comments
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
fd=1
|
|
|
|
;;
|
|
|
|
esac
|
2010-08-14 04:47:35 +08:00
|
|
|
printf '%s\n' "$command${rest:+ }$rest" >&$fd
|
2009-03-03 17:55:31 +08:00
|
|
|
done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
|
2010-03-13 01:04:30 +08:00
|
|
|
mv -f "$TODO".new "$TODO" &&
|
|
|
|
case "$(peek_next_command)" in
|
|
|
|
squash|s|fixup|f)
|
|
|
|
record_in_rewritten "$ONTO"
|
|
|
|
;;
|
|
|
|
esac ||
|
2009-03-03 17:55:31 +08:00
|
|
|
die "Could not skip unnecessary pick commands"
|
|
|
|
}
|
|
|
|
|
2008-07-12 23:48:20 +08:00
|
|
|
# check if no other options are set
|
|
|
|
is_standalone () {
|
|
|
|
test $# -eq 2 -a "$2" = '--' &&
|
|
|
|
test -z "$ONTO" &&
|
|
|
|
test -z "$PRESERVE_MERGES" &&
|
|
|
|
test -z "$STRATEGY" &&
|
|
|
|
test -z "$VERBOSE"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_saved_options () {
|
|
|
|
test -d "$REWRITTEN" && PRESERVE_MERGES=t
|
|
|
|
test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
|
|
|
|
test -f "$DOTEST"/verbose && VERBOSE=t
|
2009-01-26 17:05:22 +08:00
|
|
|
test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
|
2008-07-12 23:48:20 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 11:13:14 +08:00
|
|
|
# Rearrange the todo list that has both "pick sha1 msg" and
|
|
|
|
# "pick sha1 fixup!/squash! msg" appears in it so that the latter
|
|
|
|
# comes immediately after the former, and change "pick" to
|
|
|
|
# "fixup"/"squash".
|
|
|
|
rearrange_squash () {
|
2010-11-05 06:36:32 +08:00
|
|
|
# extract fixup!/squash! lines and resolve any referenced sha1's
|
|
|
|
while read -r pick sha1 message
|
|
|
|
do
|
|
|
|
case "$message" in
|
|
|
|
"squash! "*|"fixup! "*)
|
|
|
|
action="${message%%!*}"
|
|
|
|
rest="${message#*! }"
|
|
|
|
echo "$sha1 $action $rest"
|
|
|
|
# if it's a single word, try to resolve to a full sha1 and
|
|
|
|
# emit a second copy. This allows us to match on both message
|
|
|
|
# and on sha1 prefix
|
|
|
|
if test "${rest#* }" = "$rest"; then
|
|
|
|
fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
|
|
|
|
if test -n "$fullsha"; then
|
|
|
|
# prefix the action to uniquely identify this line as
|
|
|
|
# intended for full sha1 match
|
|
|
|
echo "$sha1 +$action $fullsha"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
esac
|
|
|
|
done >"$1.sq" <"$1"
|
2009-12-08 11:13:14 +08:00
|
|
|
test -s "$1.sq" || return
|
|
|
|
|
|
|
|
used=
|
2010-07-06 14:08:36 +08:00
|
|
|
while read -r pick sha1 message
|
2009-12-08 11:13:14 +08:00
|
|
|
do
|
|
|
|
case " $used" in
|
|
|
|
*" $sha1 "*) continue ;;
|
|
|
|
esac
|
2010-07-23 03:15:11 +08:00
|
|
|
printf '%s\n' "$pick $sha1 $message"
|
2010-11-05 06:36:31 +08:00
|
|
|
used="$used$sha1 "
|
2010-07-06 14:08:36 +08:00
|
|
|
while read -r squash action msg
|
2009-12-08 11:13:14 +08:00
|
|
|
do
|
2010-11-05 06:36:31 +08:00
|
|
|
case " $used" in
|
|
|
|
*" $squash "*) continue ;;
|
|
|
|
esac
|
2010-11-05 06:36:32 +08:00
|
|
|
emit=0
|
|
|
|
case "$action" in
|
|
|
|
+*)
|
|
|
|
action="${action#+}"
|
|
|
|
# full sha1 prefix test
|
|
|
|
case "$msg" in "$sha1"*) emit=1;; esac ;;
|
|
|
|
*)
|
|
|
|
# message prefix test
|
|
|
|
case "$message" in "$msg"*) emit=1;; esac ;;
|
|
|
|
esac
|
|
|
|
if test $emit = 1; then
|
2010-07-23 03:15:11 +08:00
|
|
|
printf '%s\n' "$action $squash $action! $msg"
|
2009-12-08 11:13:14 +08:00
|
|
|
used="$used$squash "
|
2010-11-05 06:36:32 +08:00
|
|
|
fi
|
2009-12-08 11:13:14 +08:00
|
|
|
done <"$1.sq"
|
|
|
|
done >"$1.rearranged" <"$1"
|
|
|
|
cat "$1.rearranged" >"$1"
|
|
|
|
rm -f "$1.sq" "$1.rearranged"
|
|
|
|
}
|
|
|
|
|
2010-01-07 19:05:09 +08:00
|
|
|
LF='
|
|
|
|
'
|
|
|
|
parse_onto () {
|
|
|
|
case "$1" in
|
|
|
|
*...*)
|
|
|
|
if left=${1%...*} right=${1#*...} &&
|
|
|
|
onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
|
|
|
|
then
|
|
|
|
case "$onto" in
|
|
|
|
?*"$LF"?* | '')
|
|
|
|
exit 1 ;;
|
|
|
|
esac
|
|
|
|
echo "$onto"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
esac
|
|
|
|
git rev-parse --verify "$1^0"
|
|
|
|
}
|
|
|
|
|
2007-09-24 04:42:08 +08:00
|
|
|
while test $# != 0
|
2007-06-25 08:11:14 +08:00
|
|
|
do
|
|
|
|
case "$1" in
|
2008-10-06 13:14:29 +08:00
|
|
|
--no-verify)
|
|
|
|
OK_TO_SKIP_PRE_REBASE=yes
|
|
|
|
;;
|
|
|
|
--verify)
|
2010-11-23 04:21:01 +08:00
|
|
|
OK_TO_SKIP_PRE_REBASE=
|
2008-10-06 13:14:29 +08:00
|
|
|
;;
|
2007-06-25 08:11:14 +08:00
|
|
|
--continue)
|
2008-07-12 23:48:20 +08:00
|
|
|
is_standalone "$@" || usage
|
|
|
|
get_saved_options
|
2007-06-25 08:11:14 +08:00
|
|
|
comment_for_reflog continue
|
|
|
|
|
|
|
|
test -d "$DOTEST" || die "No interactive rebase running"
|
|
|
|
|
2007-12-31 04:51:42 +08:00
|
|
|
# Sanity check
|
|
|
|
git rev-parse --verify HEAD >/dev/null ||
|
|
|
|
die "Cannot read HEAD"
|
2008-05-15 01:03:59 +08:00
|
|
|
git update-index --ignore-submodules --refresh &&
|
|
|
|
git diff-files --quiet --ignore-submodules ||
|
2007-12-31 04:51:42 +08:00
|
|
|
die "Working tree is dirty"
|
|
|
|
|
|
|
|
# do we have anything to commit?
|
2008-05-15 01:03:59 +08:00
|
|
|
if git diff-index --cached --quiet --ignore-submodules HEAD --
|
2007-12-20 15:12:12 +08:00
|
|
|
then
|
2007-12-31 04:51:42 +08:00
|
|
|
: Nothing to commit -- skip this
|
|
|
|
else
|
2010-01-14 13:54:46 +08:00
|
|
|
. "$AUTHOR_SCRIPT" ||
|
2007-12-31 04:51:42 +08:00
|
|
|
die "Cannot find the author identity"
|
2008-09-09 04:42:48 +08:00
|
|
|
amend=
|
2010-01-14 13:54:47 +08:00
|
|
|
if test -f "$AMEND"
|
2007-12-31 04:51:42 +08:00
|
|
|
then
|
2008-09-09 04:42:48 +08:00
|
|
|
amend=$(git rev-parse --verify HEAD)
|
2010-01-14 13:54:47 +08:00
|
|
|
test "$amend" = $(cat "$AMEND") ||
|
2008-09-09 20:05:26 +08:00
|
|
|
die "\
|
|
|
|
You have uncommitted changes in your working tree. Please, commit them
|
|
|
|
first and then run 'git rebase --continue' again."
|
2007-12-31 04:51:42 +08:00
|
|
|
git reset --soft HEAD^ ||
|
|
|
|
die "Cannot rewind the HEAD"
|
|
|
|
fi
|
2010-01-14 13:54:53 +08:00
|
|
|
do_with_author git commit --no-verify -F "$MSG" -e || {
|
2008-09-09 04:42:48 +08:00
|
|
|
test -n "$amend" && git reset --soft $amend
|
|
|
|
die "Could not commit staged changes."
|
|
|
|
}
|
2007-12-20 15:12:12 +08:00
|
|
|
fi
|
2007-07-08 10:01:29 +08:00
|
|
|
|
2010-03-29 03:36:00 +08:00
|
|
|
record_in_rewritten "$(cat "$DOTEST"/stopped-sha)"
|
|
|
|
|
2010-10-19 22:39:28 +08:00
|
|
|
require_clean_work_tree "rebase"
|
2007-06-25 08:11:14 +08:00
|
|
|
do_rest
|
|
|
|
;;
|
|
|
|
--abort)
|
2008-07-12 23:48:20 +08:00
|
|
|
is_standalone "$@" || usage
|
|
|
|
get_saved_options
|
2007-06-25 08:11:14 +08:00
|
|
|
comment_for_reflog abort
|
|
|
|
|
2007-11-22 19:18:10 +08:00
|
|
|
git rerere clear
|
2007-06-25 08:11:14 +08:00
|
|
|
test -d "$DOTEST" || die "No interactive rebase running"
|
|
|
|
|
|
|
|
HEADNAME=$(cat "$DOTEST"/head-name)
|
|
|
|
HEAD=$(cat "$DOTEST"/head)
|
2007-09-25 23:43:15 +08:00
|
|
|
case $HEADNAME in
|
|
|
|
refs/*)
|
|
|
|
git symbolic-ref HEAD $HEADNAME
|
|
|
|
;;
|
|
|
|
esac &&
|
2007-07-24 06:45:49 +08:00
|
|
|
output git reset --hard $HEAD &&
|
2007-06-25 08:11:14 +08:00
|
|
|
rm -rf "$DOTEST"
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
--skip)
|
2008-07-12 23:48:20 +08:00
|
|
|
is_standalone "$@" || usage
|
|
|
|
get_saved_options
|
2007-06-25 08:11:14 +08:00
|
|
|
comment_for_reflog skip
|
|
|
|
|
2007-11-22 19:18:10 +08:00
|
|
|
git rerere clear
|
2007-06-25 08:11:14 +08:00
|
|
|
test -d "$DOTEST" || die "No interactive rebase running"
|
|
|
|
|
2007-07-24 06:45:49 +08:00
|
|
|
output git reset --hard && do_rest
|
2007-06-25 08:11:14 +08:00
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
-s)
|
2007-06-25 08:11:14 +08:00
|
|
|
case "$#,$1" in
|
|
|
|
*,*=*)
|
2007-11-09 05:47:36 +08:00
|
|
|
STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
|
2007-06-25 08:11:14 +08:00
|
|
|
1,*)
|
|
|
|
usage ;;
|
|
|
|
*)
|
|
|
|
STRATEGY="-s $2"
|
|
|
|
shift ;;
|
|
|
|
esac
|
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
-m)
|
2007-06-25 08:11:14 +08:00
|
|
|
# we use merge anyway
|
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
-v)
|
2007-06-25 08:11:14 +08:00
|
|
|
VERBOSE=t
|
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
-p)
|
2007-06-26 01:59:43 +08:00
|
|
|
PRESERVE_MERGES=t
|
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
-i)
|
2007-06-25 08:11:14 +08:00
|
|
|
# yeah, we know
|
|
|
|
;;
|
2010-03-25 04:34:04 +08:00
|
|
|
--no-ff)
|
|
|
|
NEVER_FF=t
|
|
|
|
;;
|
2009-01-03 06:28:28 +08:00
|
|
|
--root)
|
|
|
|
REBASE_ROOT=t
|
|
|
|
;;
|
2009-12-08 11:13:14 +08:00
|
|
|
--autosquash)
|
|
|
|
AUTOSQUASH=t
|
|
|
|
;;
|
2010-07-14 19:59:57 +08:00
|
|
|
--no-autosquash)
|
|
|
|
AUTOSQUASH=
|
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
--onto)
|
|
|
|
shift
|
2010-01-07 19:05:09 +08:00
|
|
|
ONTO=$(parse_onto "$1") ||
|
2008-07-12 23:48:20 +08:00
|
|
|
die "Does not point to a valid commit: $1"
|
2007-06-25 08:11:14 +08:00
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
--)
|
|
|
|
shift
|
2011-02-07 02:43:35 +08:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
|
|
|
|
test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
|
|
|
|
test -d "$DOTEST" &&
|
|
|
|
die "Interactive rebase already started"
|
2007-09-26 09:30:13 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
git var GIT_COMMITTER_IDENT >/dev/null ||
|
|
|
|
die "You need to set your committer info first"
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
if test -z "$REBASE_ROOT"
|
|
|
|
then
|
|
|
|
UPSTREAM_ARG="$1"
|
|
|
|
UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
|
|
|
|
test -z "$ONTO" && ONTO=$UPSTREAM
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
UPSTREAM=
|
|
|
|
UPSTREAM_ARG=--root
|
|
|
|
test -z "$ONTO" &&
|
|
|
|
die "You must specify --onto when using --root"
|
|
|
|
fi
|
|
|
|
run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
|
|
|
|
|
|
|
|
comment_for_reflog start
|
|
|
|
|
|
|
|
require_clean_work_tree "rebase" "Please commit or stash them."
|
|
|
|
|
|
|
|
if test ! -z "$1"
|
|
|
|
then
|
|
|
|
output git checkout "$1" -- ||
|
|
|
|
die "Could not checkout $1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
|
|
|
|
mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
|
|
|
|
|
|
|
|
: > "$DOTEST"/interactive || die "Could not mark as interactive"
|
|
|
|
git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
|
|
|
|
echo "detached HEAD" > "$DOTEST"/head-name
|
|
|
|
|
|
|
|
echo $HEAD > "$DOTEST"/head
|
|
|
|
case "$REBASE_ROOT" in
|
|
|
|
'')
|
|
|
|
rm -f "$DOTEST"/rebase-root ;;
|
|
|
|
*)
|
|
|
|
: >"$DOTEST"/rebase-root ;;
|
|
|
|
esac
|
|
|
|
echo $ONTO > "$DOTEST"/onto
|
|
|
|
test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
|
|
|
|
test t = "$VERBOSE" && : > "$DOTEST"/verbose
|
|
|
|
if test t = "$PRESERVE_MERGES"
|
|
|
|
then
|
|
|
|
if test -z "$REBASE_ROOT"
|
|
|
|
then
|
|
|
|
mkdir "$REWRITTEN" &&
|
|
|
|
for c in $(git merge-base --all $HEAD $UPSTREAM)
|
2008-10-15 15:44:38 +08:00
|
|
|
do
|
2011-02-07 02:43:35 +08:00
|
|
|
echo $ONTO > "$REWRITTEN"/$c ||
|
|
|
|
die "Could not init rewritten commits"
|
2008-10-15 15:44:38 +08:00
|
|
|
done
|
2011-02-07 02:43:35 +08:00
|
|
|
else
|
|
|
|
mkdir "$REWRITTEN" &&
|
|
|
|
echo $ONTO > "$REWRITTEN"/root ||
|
|
|
|
die "Could not init rewritten commits"
|
|
|
|
fi
|
|
|
|
# No cherry-pick because our first pass is to determine
|
|
|
|
# parents to rewrite and skipping dropped commits would
|
|
|
|
# prematurely end our probe
|
|
|
|
MERGES_OPTION=
|
|
|
|
first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
|
|
|
|
else
|
|
|
|
MERGES_OPTION="--no-merges --cherry-pick"
|
|
|
|
fi
|
|
|
|
|
|
|
|
SHORTHEAD=$(git rev-parse --short $HEAD)
|
|
|
|
SHORTONTO=$(git rev-parse --short $ONTO)
|
|
|
|
if test -z "$REBASE_ROOT"
|
|
|
|
# this is now equivalent to ! -z "$UPSTREAM"
|
|
|
|
then
|
|
|
|
SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
|
|
|
|
REVISIONS=$UPSTREAM...$HEAD
|
|
|
|
SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
|
|
|
|
else
|
|
|
|
REVISIONS=$ONTO...$HEAD
|
|
|
|
SHORTREVISIONS=$SHORTHEAD
|
|
|
|
fi
|
|
|
|
git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
|
|
|
|
--abbrev=7 --reverse --left-right --topo-order \
|
|
|
|
$REVISIONS | \
|
|
|
|
sed -n "s/^>//p" |
|
|
|
|
while read -r shortsha1 rest
|
|
|
|
do
|
|
|
|
if test t != "$PRESERVE_MERGES"
|
|
|
|
then
|
|
|
|
printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
|
|
|
|
else
|
|
|
|
sha1=$(git rev-parse $shortsha1)
|
|
|
|
if test -z "$REBASE_ROOT"
|
2008-10-15 15:44:38 +08:00
|
|
|
then
|
2011-02-07 02:43:35 +08:00
|
|
|
preserve=t
|
|
|
|
for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
|
2008-10-15 15:44:38 +08:00
|
|
|
do
|
2011-02-07 02:43:35 +08:00
|
|
|
if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
|
2008-10-15 15:44:38 +08:00
|
|
|
then
|
2011-02-07 02:43:35 +08:00
|
|
|
preserve=f
|
2008-10-15 15:44:38 +08:00
|
|
|
fi
|
|
|
|
done
|
2011-02-07 02:43:35 +08:00
|
|
|
else
|
|
|
|
preserve=f
|
|
|
|
fi
|
|
|
|
if test f = "$preserve"
|
|
|
|
then
|
|
|
|
touch "$REWRITTEN"/$sha1
|
|
|
|
printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
|
2008-10-15 15:44:38 +08:00
|
|
|
fi
|
2011-02-07 02:43:35 +08:00
|
|
|
fi
|
|
|
|
done
|
2009-01-03 06:28:28 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
# Watch for commits that been dropped by --cherry-pick
|
|
|
|
if test t = "$PRESERVE_MERGES"
|
|
|
|
then
|
|
|
|
mkdir "$DROPPED"
|
|
|
|
# Save all non-cherry-picked changes
|
|
|
|
git rev-list $REVISIONS --left-right --cherry-pick | \
|
|
|
|
sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
|
|
|
|
# Now all commits and note which ones are missing in
|
|
|
|
# not-cherry-picks and hence being dropped
|
|
|
|
git rev-list $REVISIONS |
|
|
|
|
while read rev
|
|
|
|
do
|
|
|
|
if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
|
|
|
|
then
|
|
|
|
# Use -f2 because if rev-list is telling us this commit is
|
|
|
|
# not worthwhile, we don't want to track its multiple heads,
|
|
|
|
# just the history of its first-parent for others that will
|
|
|
|
# be rebasing on top of it
|
|
|
|
git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
|
|
|
|
short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
|
|
|
|
sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
|
|
|
|
rm "$REWRITTEN"/$rev
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
test -s "$TODO" || echo noop >> "$TODO"
|
|
|
|
test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
|
|
|
|
cat >> "$TODO" << EOF
|
2007-11-22 20:30:10 +08:00
|
|
|
|
2009-01-03 06:28:28 +08:00
|
|
|
# Rebase $SHORTREVISIONS onto $SHORTONTO
|
2007-06-25 08:11:14 +08:00
|
|
|
#
|
|
|
|
# Commands:
|
2008-06-08 00:20:19 +08:00
|
|
|
# p, pick = use commit
|
2009-10-07 14:13:23 +08:00
|
|
|
# r, reword = use commit, but edit the commit message
|
2008-06-08 00:20:19 +08:00
|
|
|
# e, edit = use commit, but stop for amending
|
|
|
|
# s, squash = use commit, but meld into previous commit
|
2009-12-07 17:20:59 +08:00
|
|
|
# f, fixup = like "squash", but discard this commit's log message
|
rebase -i: clarify in-editor documentation of "exec"
The hints in the current "instruction sheet" template look like so:
# Rebase 3f14246..a1d7e01 onto 3f14246
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x <cmd>, exec <cmd> = Run a shell command <cmd>, and stop if it fails
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
This does not make it clear that the format of each line is
<insn> <commit id> <explanatory text that will be printed>
but the reader will probably infer that from the automatically
generated pick examples above it.
What about the "exec" instruction? By analogy, I might imagine that
the format of that line is "exec <command> <explanatory text>", and
the "x <cmd>" hint does not address that question (at first I read it
as taking an argument <cmd> that is the name of a shell). Meanwhile,
the mention of <cmd> makes the hints harder to scan as a table.
So remove the <cmd> and add some words to remind the reader that
"exec" runs a command named by the rest of the line. To make room, it
is left to the manpage to explain that that command is run using
$SHELL and that nonzero status from that command will pause the
rebase.
Wording from Junio.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-01-21 08:36:24 +08:00
|
|
|
# x, exec = run command (the rest of the line) using shell
|
2007-07-09 04:32:22 +08:00
|
|
|
#
|
|
|
|
# If you remove a line here THAT COMMIT WILL BE LOST.
|
2007-11-22 20:30:10 +08:00
|
|
|
# However, if you remove everything, the rebase will be aborted.
|
2007-07-09 04:32:22 +08:00
|
|
|
#
|
2007-06-25 08:11:14 +08:00
|
|
|
EOF
|
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
has_action "$TODO" ||
|
|
|
|
die_abort "Nothing to do"
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
cp "$TODO" "$TODO".backup
|
|
|
|
git_editor "$TODO" ||
|
|
|
|
die_abort "Could not execute editor"
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
has_action "$TODO" ||
|
|
|
|
die_abort "Nothing to do"
|
2007-06-26 01:56:55 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
|
2009-03-03 17:55:31 +08:00
|
|
|
|
2011-02-07 02:43:35 +08:00
|
|
|
output git checkout $ONTO || die_abort "could not detach HEAD"
|
|
|
|
git update-ref ORIG_HEAD $HEAD
|
|
|
|
do_rest
|