2013-11-25 22:03:52 +01:00
|
|
|
# This shell script fragment is sourced by git-rebase to implement
|
|
|
|
# its interactive mode. "git rebase --interactive" makes it easy
|
|
|
|
# to fix up commits in the middle of a series and rearrange commits.
|
2007-06-25 02:11:14 +02:00
|
|
|
#
|
|
|
|
# Copyright (c) 2006 Johannes E. Schindelin
|
|
|
|
#
|
|
|
|
# The original idea comes from Eric W. Biederman, in
|
2017-05-08 03:38:59 +02:00
|
|
|
# https://public-inbox.org/git/m1odwkyuf5.fsf_-_@ebiederm.dsl.xmission.com/
|
2012-06-26 16:51:54 +02:00
|
|
|
#
|
2010-01-14 06:54:45 +01: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
|
2011-02-06 19:43:37 +01:00
|
|
|
# file and written to the tail of $done.
|
2011-02-06 19:43:50 +01:00
|
|
|
todo="$state_dir"/git-rebase-todo
|
2010-01-14 06:54:45 +01:00
|
|
|
|
2011-02-06 19:43:49 +01:00
|
|
|
GIT_CHERRY_PICK_HELP="$resolvemsg"
|
2007-11-28 09:06:36 +01:00
|
|
|
export GIT_CHERRY_PICK_HELP
|
|
|
|
|
2016-11-21 15:18:29 +01:00
|
|
|
comment_char=$(git config --get core.commentchar 2>/dev/null)
|
|
|
|
case "$comment_char" in
|
|
|
|
'' | auto)
|
|
|
|
comment_char="#"
|
|
|
|
;;
|
|
|
|
?)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
comment_char=$(echo "$comment_char" | cut -c1)
|
|
|
|
;;
|
|
|
|
esac
|
2013-02-12 00:08:04 +01:00
|
|
|
|
2011-02-06 19:43:37 +01:00
|
|
|
orig_reflog_action="$GIT_REFLOG_ACTION"
|
2007-06-25 02:11:14 +02:00
|
|
|
|
|
|
|
comment_for_reflog () {
|
2011-02-06 19:43:37 +01:00
|
|
|
case "$orig_reflog_action" in
|
2007-06-25 02:11:14 +02:00
|
|
|
''|rebase*)
|
|
|
|
GIT_REFLOG_ACTION="rebase -i ($1)"
|
|
|
|
export GIT_REFLOG_ACTION
|
2007-09-25 17:42:51 +02:00
|
|
|
;;
|
2007-06-25 02:11:14 +02:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2007-06-25 19:56:55 +02:00
|
|
|
die_abort () {
|
2016-06-29 08:21:27 +02:00
|
|
|
apply_autostash
|
2011-02-06 19:43:50 +01:00
|
|
|
rm -rf "$state_dir"
|
2007-06-25 19:56:55 +02:00
|
|
|
die "$1"
|
|
|
|
}
|
|
|
|
|
2007-09-25 17:42:51 +02:00
|
|
|
has_action () {
|
2013-02-12 00:08:04 +01:00
|
|
|
test -n "$(git stripspace --strip-comments <"$1")"
|
2007-09-25 17:42:51 +02:00
|
|
|
}
|
|
|
|
|
2011-10-17 22:26:23 +02:00
|
|
|
git_sequence_editor () {
|
|
|
|
if test -z "$GIT_SEQUENCE_EDITOR"
|
|
|
|
then
|
|
|
|
GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
|
|
|
|
if [ -z "$GIT_SEQUENCE_EDITOR" ]
|
|
|
|
then
|
|
|
|
GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval "$GIT_SEQUENCE_EDITOR" '"$@"'
|
|
|
|
}
|
|
|
|
|
2013-08-24 02:10:42 +02:00
|
|
|
expand_todo_ids() {
|
2017-07-14 16:45:11 +02:00
|
|
|
git rebase--helper --expand-ids
|
2013-08-24 02:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
collapse_todo_ids() {
|
2017-07-14 16:45:11 +02:00
|
|
|
git rebase--helper --shorten-ids
|
2009-12-08 04:13:14 +01:00
|
|
|
}
|
|
|
|
|
2015-06-29 22:20:31 +02:00
|
|
|
# Switch to the branch in $into and notify it in the reflog
|
|
|
|
checkout_onto () {
|
2018-08-10 18:51:33 +02:00
|
|
|
comment_for_reflog start
|
2015-06-29 22:20:31 +02:00
|
|
|
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
|
2016-06-17 22:21:03 +02:00
|
|
|
output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
|
2015-06-29 22:20:31 +02:00
|
|
|
git update-ref ORIG_HEAD $orig_head
|
|
|
|
}
|
|
|
|
|
|
|
|
get_missing_commit_check_level () {
|
|
|
|
check_level=$(git config --get rebase.missingCommitsCheck)
|
|
|
|
check_level=${check_level:-ignore}
|
|
|
|
# Don't be case sensitive
|
|
|
|
printf '%s' "$check_level" | tr 'A-Z' 'a-z'
|
|
|
|
}
|
|
|
|
|
2018-03-23 22:25:25 +01:00
|
|
|
# Initiate an action. If the cannot be any
|
|
|
|
# further action it may exec a command
|
|
|
|
# or exit and not return.
|
|
|
|
#
|
|
|
|
# TODO: Consider a cleaner return model so it
|
|
|
|
# never exits and always return 0 if process
|
|
|
|
# is complete.
|
|
|
|
#
|
|
|
|
# Parameter 1 is the action to initiate.
|
|
|
|
#
|
|
|
|
# Returns 0 if the action was able to complete
|
|
|
|
# and if 1 if further processing is required.
|
|
|
|
initiate_action () {
|
|
|
|
case "$1" in
|
2018-03-23 22:25:24 +01:00
|
|
|
continue)
|
2018-05-28 14:34:22 +02:00
|
|
|
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
|
|
|
|
--continue
|
2018-03-23 22:25:24 +01:00
|
|
|
;;
|
|
|
|
skip)
|
|
|
|
git rerere clear
|
2018-05-28 14:34:22 +02:00
|
|
|
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
|
|
|
|
--continue
|
2018-03-23 22:25:24 +01:00
|
|
|
;;
|
|
|
|
edit-todo)
|
2018-08-10 18:51:31 +02:00
|
|
|
exec git rebase--helper --edit-todo
|
2018-03-23 22:25:24 +01:00
|
|
|
;;
|
|
|
|
show-current-patch)
|
|
|
|
exec git show REBASE_HEAD --
|
|
|
|
;;
|
2018-03-23 22:25:25 +01:00
|
|
|
*)
|
|
|
|
return 1 # continue
|
|
|
|
;;
|
2018-03-23 22:25:24 +01:00
|
|
|
esac
|
2018-03-23 22:25:25 +01:00
|
|
|
}
|
2013-06-16 10:45:13 +02:00
|
|
|
|
2018-03-23 22:25:25 +01:00
|
|
|
init_basic_state () {
|
2018-03-23 22:25:24 +01:00
|
|
|
orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
|
|
|
|
mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
|
|
|
|
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
|
2012-04-20 16:36:17 +02:00
|
|
|
|
2018-03-23 22:25:24 +01:00
|
|
|
: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
|
|
|
|
write_basic_state
|
2018-03-23 22:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
init_revisions_and_shortrevisions () {
|
|
|
|
shorthead=$(git rev-parse --short $orig_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...$orig_head
|
|
|
|
shortrevisions=$shortupstream..$shorthead
|
|
|
|
else
|
|
|
|
revisions=$onto...$orig_head
|
|
|
|
shortrevisions=$shorthead
|
rebase -i --root: let the sequencer handle even the initial part
In this developer's earlier attempt to accelerate interactive rebases by
converting large parts from Unix shell script into portable, performant
C, the --root handling was specifically excluded (to simplify the task a
little bit; it still took over a year to get that reduced set of patches
into Git proper).
This patch ties up that loose end: now only --preserve-merges uses the
slow Unix shell script implementation to perform the interactive rebase.
As the rebase--helper reports progress to stderr (unlike the scripted
interactive rebase, which reports it to stdout, of all places), we have
to adjust a couple of tests that did not expect that for `git rebase -i
--root`.
This patch fixes -- at long last! -- the really old bug reported in
6a6bc5bdc4d (add tests for rebasing root, 2013-06-06) that rebasing with
--root *always* rewrote the root commit, even if there were no changes.
The bug still persists in --preserve-merges mode, of course, but that
mode will be deprecated as soon as the new --rebase-merges mode
stabilizes, anyway.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-05-04 01:01:18 +02:00
|
|
|
test -z "$squash_onto" ||
|
|
|
|
echo "$squash_onto" >"$state_dir"/squash-onto
|
2018-03-23 22:25:25 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
complete_action() {
|
|
|
|
test -s "$todo" || echo noop >> "$todo"
|
|
|
|
test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
|
|
|
|
test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"
|
|
|
|
|
|
|
|
todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
|
|
|
|
todocount=${todocount##* }
|
|
|
|
|
|
|
|
cat >>"$todo" <<EOF
|
|
|
|
|
|
|
|
$comment_char $(eval_ngettext \
|
|
|
|
"Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
|
|
|
|
"Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
|
|
|
|
"$todocount")
|
|
|
|
EOF
|
2018-08-10 18:51:29 +02:00
|
|
|
git rebase--helper --append-todo-help ${keep_empty:+--keep-empty}
|
2018-03-23 22:25:25 +01:00
|
|
|
|
|
|
|
has_action "$todo" ||
|
|
|
|
return 2
|
|
|
|
|
|
|
|
cp "$todo" "$todo".backup
|
|
|
|
collapse_todo_ids
|
|
|
|
git_sequence_editor "$todo" ||
|
|
|
|
die_abort "$(gettext "Could not execute editor")"
|
|
|
|
|
|
|
|
has_action "$todo" ||
|
|
|
|
return 2
|
|
|
|
|
|
|
|
git rebase--helper --check-todo-list || {
|
|
|
|
ret=$?
|
|
|
|
checkout_onto
|
|
|
|
exit $ret
|
|
|
|
}
|
|
|
|
|
|
|
|
expand_todo_ids
|
|
|
|
|
2018-05-28 14:34:22 +02:00
|
|
|
test -n "$force_rebase" ||
|
2018-03-23 22:25:25 +01:00
|
|
|
onto="$(git rebase--helper --skip-unnecessary-picks)" ||
|
|
|
|
die "Could not skip unnecessary pick commands"
|
|
|
|
|
|
|
|
checkout_onto
|
2018-05-28 14:34:22 +02:00
|
|
|
require_clean_work_tree "rebase"
|
|
|
|
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
|
|
|
|
--continue
|
2018-03-23 22:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
git_rebase__interactive () {
|
|
|
|
initiate_action "$action"
|
|
|
|
ret=$?
|
|
|
|
if test $ret = 0; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2018-08-10 18:51:33 +02:00
|
|
|
git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
|
2018-03-23 22:25:25 +01:00
|
|
|
init_basic_state
|
|
|
|
|
|
|
|
init_revisions_and_shortrevisions
|
|
|
|
|
2018-03-23 22:25:27 +01:00
|
|
|
git rebase--helper --make-script ${keep_empty:+--keep-empty} \
|
rebase: introduce the --rebase-merges option
Once upon a time, this here developer thought: wouldn't it be nice if,
say, Git for Windows' patches on top of core Git could be represented as
a thicket of branches, and be rebased on top of core Git in order to
maintain a cherry-pick'able set of patch series?
The original attempt to answer this was: git rebase --preserve-merges.
However, that experiment was never intended as an interactive option,
and it only piggy-backed on git rebase --interactive because that
command's implementation looked already very, very familiar: it was
designed by the same person who designed --preserve-merges: yours truly.
Some time later, some other developer (I am looking at you, Andreas!
;-)) decided that it would be a good idea to allow --preserve-merges to
be combined with --interactive (with caveats!) and the Git maintainer
(well, the interim Git maintainer during Junio's absence, that is)
agreed, and that is when the glamor of the --preserve-merges design
started to fall apart rather quickly and unglamorously.
The reason? In --preserve-merges mode, the parents of a merge commit (or
for that matter, of *any* commit) were not stated explicitly, but were
*implied* by the commit name passed to the `pick` command.
This made it impossible, for example, to reorder commits. Not to mention
to move commits between branches or, deity forbid, to split topic branches
into two.
Alas, these shortcomings also prevented that mode (whose original
purpose was to serve Git for Windows' needs, with the additional hope
that it may be useful to others, too) from serving Git for Windows'
needs.
Five years later, when it became really untenable to have one unwieldy,
big hodge-podge patch series of partly related, partly unrelated patches
in Git for Windows that was rebased onto core Git's tags from time to
time (earning the undeserved wrath of the developer of the ill-fated
git-remote-hg series that first obsoleted Git for Windows' competing
approach, only to be abandoned without maintainer later) was really
untenable, the "Git garden shears" were born [*1*/*2*]: a script,
piggy-backing on top of the interactive rebase, that would first
determine the branch topology of the patches to be rebased, create a
pseudo todo list for further editing, transform the result into a real
todo list (making heavy use of the `exec` command to "implement" the
missing todo list commands) and finally recreate the patch series on
top of the new base commit.
That was in 2013. And it took about three weeks to come up with the
design and implement it as an out-of-tree script. Needless to say, the
implementation needed quite a few years to stabilize, all the while the
design itself proved itself sound.
With this patch, the goodness of the Git garden shears comes to `git
rebase -i` itself. Passing the `--rebase-merges` option will generate
a todo list that can be understood readily, and where it is obvious
how to reorder commits. New branches can be introduced by inserting
`label` commands and calling `merge <label>`. And once this mode will
have become stable and universally accepted, we can deprecate the design
mistake that was `--preserve-merges`.
Link *1*:
https://github.com/msysgit/msysgit/blob/master/share/msysGit/shears.sh
Link *2*:
https://github.com/git-for-windows/build-extra/blob/master/shears.sh
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-04-25 14:29:04 +02:00
|
|
|
${rebase_merges:+--rebase-merges} \
|
rebase -i: introduce --rebase-merges=[no-]rebase-cousins
When running `git rebase --rebase-merges` non-interactively with an
ancestor of HEAD as <upstream> (or leaving the todo list unmodified),
we would ideally recreate the exact same commits as before the rebase.
However, if there are commits in the commit range <upstream>.. that do not
have <upstream> as direct ancestor (i.e. if `git log <upstream>..` would
show commits that are omitted by `git log --ancestry-path <upstream>..`),
this is currently not the case: we would turn them into commits that have
<upstream> as direct ancestor.
Let's illustrate that with a diagram:
C
/ \
A - B - E - F
\ /
D
Currently, after running `git rebase -i --rebase-merges B`, the new branch
structure would be (pay particular attention to the commit `D`):
--- C' --
/ \
A - B ------ E' - F'
\ /
D'
This is not really preserving the branch topology from before! The
reason is that the commit `D` does not have `B` as ancestor, and
therefore it gets rebased onto `B`.
This is unintuitive behavior. Even worse, when recreating branch
structure, most use cases would appear to want cousins *not* to be
rebased onto the new base commit. For example, Git for Windows (the
heaviest user of the Git garden shears, which served as the blueprint
for --rebase-merges) frequently merges branches from `next` early, and
these branches certainly do *not* want to be rebased. In the example
above, the desired outcome would look like this:
--- C' --
/ \
A - B ------ E' - F'
\ /
-- D' --
Let's introduce the term "cousins" for such commits ("D" in the
example), and let's not rebase them by default. For hypothetical
use cases where cousins *do* need to be rebased, `git rebase
--rebase=merges=rebase-cousins` needs to be used.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-04-25 14:29:40 +02:00
|
|
|
${rebase_cousins:+--rebase-cousins} \
|
2018-03-23 22:25:27 +01:00
|
|
|
$revisions ${restrict_revision+^$restrict_revision} >"$todo" ||
|
|
|
|
die "$(gettext "Could not generate todo list")"
|
2018-03-23 22:25:26 +01:00
|
|
|
|
|
|
|
complete_action
|
|
|
|
}
|