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
|
|
|
|
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
|
|
|
|
"
|
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"
|
2007-07-29 07:44:40 +08:00
|
|
|
TODO="$DOTEST"/git-rebase-todo
|
2007-06-25 08:11:14 +08:00
|
|
|
DONE="$DOTEST"/done
|
2007-07-22 01:09:41 +08:00
|
|
|
MSG="$DOTEST"/message
|
|
|
|
SQUASH_MSG="$DOTEST"/message-squash
|
2007-06-26 01:59:43 +08:00
|
|
|
REWRITTEN="$DOTEST"/rewritten
|
|
|
|
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=
|
|
|
|
|
2007-11-28 16:06:36 +08:00
|
|
|
GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
|
|
|
|
mark the corrected paths with 'git add <paths>', and
|
|
|
|
run 'git rebase --continue'"
|
|
|
|
export GIT_CHERRY_PICK_HELP
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
warn () {
|
|
|
|
echo "$*" >&2
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
require_clean_work_tree () {
|
|
|
|
# test if working tree is dirty
|
|
|
|
git rev-parse --verify HEAD > /dev/null &&
|
2008-05-15 01:03:59 +08:00
|
|
|
git update-index --ignore-submodules --refresh &&
|
|
|
|
git diff-files --quiet --ignore-submodules &&
|
|
|
|
git diff-index --cached --quiet HEAD --ignore-submodules -- ||
|
2007-06-25 08:11:14 +08:00
|
|
|
die "Working tree is dirty"
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2008-03-13 05:32:17 +08:00
|
|
|
count=$(grep -c '^[^#]' < "$DONE")
|
|
|
|
total=$(($count+$(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 () {
|
2007-09-25 23:42:36 +08:00
|
|
|
parent_sha1=$(git rev-parse --verify "$1"^) ||
|
|
|
|
die "Cannot get patch for $1^"
|
2007-10-09 20:59:43 +08:00
|
|
|
git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
|
2007-09-25 23:42:36 +08:00
|
|
|
test -f "$DOTEST"/message ||
|
|
|
|
git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
|
|
|
|
test -f "$DOTEST"/author-script ||
|
|
|
|
get_author_ident_from_commit "$1" > "$DOTEST"/author-script
|
2007-06-25 08:11:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
die_with_patch () {
|
|
|
|
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 () {
|
2008-03-13 05:32:17 +08:00
|
|
|
grep '^[^#]' "$1" >/dev/null
|
2007-09-25 23:42:51 +08:00
|
|
|
}
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
pick_one () {
|
2007-08-23 16:55:41 +08:00
|
|
|
no_ff=
|
|
|
|
case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; 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
|
2007-09-25 23:42:51 +08:00
|
|
|
parent_sha1=$(git rev-parse --verify $sha1^) ||
|
|
|
|
die "Could not get the parent of $sha1"
|
2007-06-25 08:11:14 +08:00
|
|
|
current_sha1=$(git rev-parse --verify HEAD)
|
2007-10-16 01:48:27 +08:00
|
|
|
if test "$no_ff$current_sha1" = "$parent_sha1"; then
|
2007-07-24 06:45:49 +08:00
|
|
|
output git reset --hard $sha1
|
|
|
|
test "a$1" = a-n && output git reset --soft $current_sha1
|
2007-06-25 08:11:14 +08:00
|
|
|
sha1=$(git rev-parse --short $sha1)
|
2007-07-24 06:45:49 +08:00
|
|
|
output warn Fast forward to $sha1
|
2007-06-25 08:11:14 +08:00
|
|
|
else
|
2007-10-31 10:20:31 +08:00
|
|
|
output git cherry-pick "$@"
|
2007-06-25 08:11:14 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2007-06-26 01:59:43 +08:00
|
|
|
pick_one_preserving_merges () {
|
|
|
|
case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
|
|
|
|
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
|
|
|
|
current_commit=$(cat "$DOTEST"/current-commit) &&
|
|
|
|
git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
|
|
|
|
rm "$DOTEST"/current-commit ||
|
|
|
|
die "Cannot write current commit's replacement sha1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# rewrite parents; if none were rewritten, we can fast-forward.
|
|
|
|
fast_forward=t
|
|
|
|
preserve=t
|
|
|
|
new_parents=
|
2007-09-25 23:42:51 +08:00
|
|
|
for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
|
2007-06-26 01:59:43 +08:00
|
|
|
do
|
2007-07-24 10:18:28 +08:00
|
|
|
if test -f "$REWRITTEN"/$p
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
|
|
|
preserve=f
|
|
|
|
new_p=$(cat "$REWRITTEN"/$p)
|
|
|
|
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
|
|
|
|
new_parents="$new_parents $p"
|
2007-06-26 01:59:43 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
case $fast_forward in
|
|
|
|
t)
|
2007-07-24 06:45:49 +08:00
|
|
|
output warn "Fast forward to $sha1"
|
2007-09-25 23:42:51 +08:00
|
|
|
test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
|
2007-06-26 01:59:43 +08:00
|
|
|
;;
|
|
|
|
f)
|
|
|
|
test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
|
|
|
|
|
2007-09-25 23:42:51 +08:00
|
|
|
first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
|
2007-06-26 01:59:43 +08:00
|
|
|
# detach HEAD to current parent
|
2007-07-24 06:45:49 +08:00
|
|
|
output git checkout $first_parent 2> /dev/null ||
|
2007-06-26 01:59:43 +08:00
|
|
|
die "Cannot move HEAD to $first_parent"
|
|
|
|
|
|
|
|
echo $sha1 > "$DOTEST"/current-commit
|
|
|
|
case "$new_parents" in
|
2007-09-25 23:42:51 +08:00
|
|
|
' '*' '*)
|
2007-06-26 01:59:43 +08:00
|
|
|
# redo merge
|
|
|
|
author_script=$(get_author_ident_from_commit $sha1)
|
|
|
|
eval "$author_script"
|
2007-09-25 23:42:51 +08:00
|
|
|
msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
|
2007-10-31 10:20:32 +08:00
|
|
|
# No point in merging the first parent, that's HEAD
|
|
|
|
new_parents=${new_parents# $first_parent}
|
2007-09-25 23:43:44 +08:00
|
|
|
if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
|
|
|
|
GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
|
|
|
|
GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
|
|
|
|
output git merge $STRATEGY -m "$msg" \
|
|
|
|
$new_parents
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
2007-11-22 19:18:10 +08:00
|
|
|
git rerere
|
2007-09-25 23:42:51 +08:00
|
|
|
printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
|
2007-07-08 10:01:29 +08:00
|
|
|
die Error redoing merge $sha1
|
2007-06-26 01:59:43 +08:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
make_squash_message () {
|
2007-07-24 10:18:28 +08:00
|
|
|
if test -f "$SQUASH_MSG"; then
|
2007-09-01 17:17:28 +08:00
|
|
|
COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
|
2008-03-13 05:34:34 +08:00
|
|
|
< "$SQUASH_MSG" | sed -ne '$p')+1))
|
2007-07-22 01:09:41 +08:00
|
|
|
echo "# This is a combination of $COUNT commits."
|
2007-12-31 04:37:59 +08:00
|
|
|
sed -e 1d -e '2,/^./{
|
|
|
|
/^$/d
|
|
|
|
}' <"$SQUASH_MSG"
|
2007-07-22 01:09:41 +08:00
|
|
|
else
|
|
|
|
COUNT=2
|
|
|
|
echo "# This is a combination of two commits."
|
|
|
|
echo "# The first commit's message is:"
|
|
|
|
echo
|
|
|
|
git cat-file commit HEAD | sed -e '1,/^$/d'
|
|
|
|
fi
|
2007-12-31 04:37:59 +08:00
|
|
|
echo
|
2007-07-22 01:09:41 +08:00
|
|
|
echo "# This is the $(nth_string $COUNT) commit message:"
|
|
|
|
echo
|
|
|
|
git cat-file commit $1 | sed -e '1,/^$/d'
|
|
|
|
}
|
|
|
|
|
|
|
|
peek_next_command () {
|
|
|
|
sed -n "1s/ .*$//p" < "$TODO"
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
do_next () {
|
2007-09-25 23:42:51 +08:00
|
|
|
rm -f "$DOTEST"/message "$DOTEST"/author-script \
|
|
|
|
"$DOTEST"/amend || exit
|
2007-06-25 08:11:14 +08:00
|
|
|
read command sha1 rest < "$TODO"
|
|
|
|
case "$command" in
|
2007-09-25 23:42:51 +08:00
|
|
|
'#'*|'')
|
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"
|
|
|
|
;;
|
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"
|
|
|
|
make_patch $sha1
|
2007-09-25 23:42:36 +08:00
|
|
|
: > "$DOTEST"/amend
|
2007-06-25 08:11:14 +08:00
|
|
|
warn
|
|
|
|
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
|
|
|
|
;;
|
2007-09-29 10:32:11 +08:00
|
|
|
squash|s)
|
2007-06-25 08:11:14 +08:00
|
|
|
comment_for_reflog squash
|
|
|
|
|
2007-09-25 23:42:51 +08:00
|
|
|
has_action "$DONE" ||
|
2007-06-25 08:11:14 +08:00
|
|
|
die "Cannot 'squash' without a previous commit"
|
|
|
|
|
|
|
|
mark_action_done
|
2007-07-22 01:09:41 +08:00
|
|
|
make_squash_message $sha1 > "$MSG"
|
|
|
|
case "$(peek_next_command)" in
|
2007-09-29 10:32:11 +08:00
|
|
|
squash|s)
|
2007-07-22 01:09:41 +08:00
|
|
|
EDIT_COMMIT=
|
2007-07-26 14:35:51 +08:00
|
|
|
USE_OUTPUT=output
|
2007-07-22 01:09:41 +08:00
|
|
|
cp "$MSG" "$SQUASH_MSG"
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-07-22 01:09:41 +08:00
|
|
|
*)
|
|
|
|
EDIT_COMMIT=-e
|
2007-07-26 14:35:51 +08:00
|
|
|
USE_OUTPUT=
|
2007-09-25 23:42:51 +08:00
|
|
|
rm -f "$SQUASH_MSG" || exit
|
|
|
|
;;
|
2007-07-22 01:09:41 +08:00
|
|
|
esac
|
|
|
|
|
2007-07-13 06:30:35 +08:00
|
|
|
failed=f
|
2007-09-30 07:34:23 +08:00
|
|
|
author_script=$(get_author_ident_from_commit HEAD)
|
2007-07-24 06:45:49 +08:00
|
|
|
output git reset --soft HEAD^
|
2007-07-25 04:43:09 +08:00
|
|
|
pick_one -n $sha1 || failed=t
|
2007-07-08 10:01:29 +08:00
|
|
|
echo "$author_script" > "$DOTEST"/author-script
|
2007-12-19 14:45:00 +08:00
|
|
|
if test $failed = f
|
|
|
|
then
|
2007-06-25 08:11:14 +08:00
|
|
|
# This is like --amend, but with a different message
|
|
|
|
eval "$author_script"
|
2007-09-25 23:43:44 +08:00
|
|
|
GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
|
|
|
|
GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
|
|
|
|
GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
|
2007-12-19 14:45:00 +08:00
|
|
|
$USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
|
|
|
|
fi
|
|
|
|
if test $failed = t
|
|
|
|
then
|
2007-06-25 08:11:14 +08:00
|
|
|
cp "$MSG" "$GIT_DIR"/MERGE_MSG
|
|
|
|
warn
|
|
|
|
warn "Could not apply $sha1... $rest"
|
|
|
|
die_with_patch $sha1 ""
|
2007-12-19 14:45:00 +08:00
|
|
|
fi
|
2007-06-25 08:11:14 +08:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
warn "Unknown command: $command $sha1 $rest"
|
|
|
|
die_with_patch $sha1 "Please fix this in the file $TODO."
|
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)) &&
|
2007-07-24 10:18:28 +08:00
|
|
|
if test -d "$REWRITTEN"
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
|
|
|
test -f "$DOTEST"/current-commit &&
|
|
|
|
current_commit=$(cat "$DOTEST"/current-commit) &&
|
|
|
|
git rev-parse HEAD > "$REWRITTEN"/$current_commit
|
2007-12-18 05:01:25 +08:00
|
|
|
if test -f "$REWRITTEN"/$OLDHEAD
|
|
|
|
then
|
|
|
|
NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
|
|
|
|
else
|
|
|
|
NEWHEAD=$OLDHEAD
|
|
|
|
fi
|
2007-06-26 01:59:43 +08:00
|
|
|
else
|
|
|
|
NEWHEAD=$(git rev-parse HEAD)
|
|
|
|
fi &&
|
2007-09-25 23:43:15 +08:00
|
|
|
case $HEADNAME in
|
|
|
|
refs/*)
|
|
|
|
message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
|
|
|
|
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
|
|
|
} &&
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2007-09-24 04:42:08 +08:00
|
|
|
while test $# != 0
|
2007-06-25 08:11:14 +08:00
|
|
|
do
|
|
|
|
case "$1" in
|
|
|
|
--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
|
|
|
|
. "$DOTEST"/author-script ||
|
|
|
|
die "Cannot find the author identity"
|
|
|
|
if test -f "$DOTEST"/amend
|
|
|
|
then
|
|
|
|
git reset --soft HEAD^ ||
|
|
|
|
die "Cannot rewind the HEAD"
|
|
|
|
fi
|
|
|
|
export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
|
|
|
|
git commit --no-verify -F "$DOTEST"/message -e ||
|
2007-12-19 14:45:00 +08:00
|
|
|
die "Could not commit staged changes."
|
2007-12-20 15:12:12 +08:00
|
|
|
fi
|
2007-07-08 10:01:29 +08:00
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
require_clean_work_tree
|
|
|
|
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
|
|
|
|
;;
|
2008-07-12 23:48:20 +08:00
|
|
|
--onto)
|
|
|
|
shift
|
|
|
|
ONTO=$(git rev-parse --verify "$1") ||
|
|
|
|
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
|
|
|
|
test $# -eq 1 -o $# -eq 2 || usage
|
2007-06-25 08:11:14 +08:00
|
|
|
test -d "$DOTEST" &&
|
|
|
|
die "Interactive rebase already started"
|
|
|
|
|
|
|
|
git var GIT_COMMITTER_IDENT >/dev/null ||
|
|
|
|
die "You need to set your committer info first"
|
|
|
|
|
|
|
|
comment_for_reflog start
|
|
|
|
|
|
|
|
require_clean_work_tree
|
|
|
|
|
2008-06-02 22:01:40 +08:00
|
|
|
UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
|
|
|
|
test -z "$ONTO" && ONTO=$UPSTREAM
|
|
|
|
|
2007-07-24 10:18:28 +08:00
|
|
|
if test ! -z "$2"
|
2007-06-25 08:11:14 +08:00
|
|
|
then
|
2007-07-24 06:45:49 +08:00
|
|
|
output git show-ref --verify --quiet "refs/heads/$2" ||
|
2007-06-25 08:11:14 +08:00
|
|
|
die "Invalid branchname: $2"
|
2007-07-24 06:45:49 +08:00
|
|
|
output git checkout "$2" ||
|
2007-06-25 08:11:14 +08:00
|
|
|
die "Could not checkout $2"
|
|
|
|
fi
|
|
|
|
|
|
|
|
HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
|
2007-09-26 09:30:13 +08:00
|
|
|
mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
|
|
|
|
|
2007-06-25 08:11:14 +08:00
|
|
|
: > "$DOTEST"/interactive || die "Could not mark as interactive"
|
2007-09-25 23:43:15 +08:00
|
|
|
git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
|
|
|
|
echo "detached HEAD" > "$DOTEST"/head-name
|
2007-06-25 08:11:14 +08:00
|
|
|
|
|
|
|
echo $HEAD > "$DOTEST"/head
|
|
|
|
echo $UPSTREAM > "$DOTEST"/upstream
|
|
|
|
echo $ONTO > "$DOTEST"/onto
|
2007-07-08 10:02:47 +08:00
|
|
|
test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
|
2007-06-25 08:11:14 +08:00
|
|
|
test t = "$VERBOSE" && : > "$DOTEST"/verbose
|
2007-07-24 10:18:28 +08:00
|
|
|
if test t = "$PRESERVE_MERGES"
|
2007-06-26 01:59:43 +08:00
|
|
|
then
|
|
|
|
# $REWRITTEN contains 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.)
|
|
|
|
mkdir "$REWRITTEN" &&
|
|
|
|
for c in $(git merge-base --all $HEAD $UPSTREAM)
|
|
|
|
do
|
|
|
|
echo $ONTO > "$REWRITTEN"/$c ||
|
|
|
|
die "Could not init rewritten commits"
|
|
|
|
done
|
|
|
|
MERGES_OPTION=
|
|
|
|
else
|
|
|
|
MERGES_OPTION=--no-merges
|
|
|
|
fi
|
2007-06-25 08:11:14 +08:00
|
|
|
|
2007-06-26 01:56:55 +08:00
|
|
|
SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
|
|
|
|
SHORTHEAD=$(git rev-parse --short $HEAD)
|
|
|
|
SHORTONTO=$(git rev-parse --short $ONTO)
|
2007-11-22 20:30:10 +08:00
|
|
|
git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
|
|
|
|
--abbrev=7 --reverse --left-right --cherry-pick \
|
|
|
|
$UPSTREAM...$HEAD | \
|
|
|
|
sed -n "s/^>/pick /p" > "$TODO"
|
|
|
|
cat >> "$TODO" << EOF
|
|
|
|
|
|
|
|
# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
|
2007-06-25 08:11:14 +08:00
|
|
|
#
|
|
|
|
# Commands:
|
2008-06-08 00:20:19 +08:00
|
|
|
# p, pick = use commit
|
|
|
|
# e, edit = use commit, but stop for amending
|
|
|
|
# s, squash = use commit, but meld into previous commit
|
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
|
|
|
|
|
2007-09-25 23:42:51 +08:00
|
|
|
has_action "$TODO" ||
|
2007-06-26 01:56:55 +08:00
|
|
|
die_abort "Nothing to do"
|
2007-06-25 08:11:14 +08:00
|
|
|
|
|
|
|
cp "$TODO" "$TODO".backup
|
2007-07-20 13:09:35 +08:00
|
|
|
git_editor "$TODO" ||
|
2007-06-25 08:11:14 +08:00
|
|
|
die "Could not execute editor"
|
|
|
|
|
2007-09-25 23:42:51 +08:00
|
|
|
has_action "$TODO" ||
|
2007-06-26 01:56:55 +08:00
|
|
|
die_abort "Nothing to do"
|
|
|
|
|
2008-07-07 15:16:38 +08:00
|
|
|
git update-ref ORIG_HEAD $HEAD
|
2007-07-24 06:45:49 +08:00
|
|
|
output git checkout $ONTO && do_rest
|
2007-09-25 23:42:51 +08:00
|
|
|
;;
|
2007-06-25 08:11:14 +08:00
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|