Fix interactive rebase on dropped commits.
PATCH 1/1 Fix interactive rebase on dropped commits.
Interactive rebase got its rev-list of commits to keep by --left-right and --cherry-pick. Adding --cherry-pick would throw out commits that were just duplicating changes already in the rebase target.
Which is desirable, except the dropped commit has forgotten about when it came to rewriting the parents of its descendents, so the descendents would get cherry-picked as-in and essentially make the rebase a no-op.
This change adds a $DOTEST/dropped directory to remember dropped commits and rewrite its children's parents as the children's (possibly rewritten) grandparents.
--- git-rebase--interactive.sh | 52 ++++++++++++--- t/t3409-rebase-interactive-cherry-picked.sh | 94 +++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 t/t3409-rebase-interactive-cherry-picked.sh
git-rebase--interactive.sh +42 -10
@@ -37,6 +37,7 @@ DONE="$DOTEST"/done MSG="$DOTEST"/message SQUASH_MSG="$DOTEST"/message-squash REWRITTEN="$DOTEST"/rewritten +DROPPED="$DOTEST"/dropped PRESERVE_MERGES= STRATEGY= ONTO=
@@ -146,15 +147,7 @@ pick_one () { pick_one_preserving_merges () { fast_forward=t - case "$1" in - -n) - fast_forward=f - sha1=$2 - ;; - *) - sha1=$1 - ;; - esac + case "$1" in -n) fast_forward=f sha1=$2 ;; *) sha1=$1 ;; esac sha1=$(git rev-parse $sha1) if test -f "$DOTEST"/current-commit
@@ -183,7 +176,28 @@ pick_one_preserving_merges () { ;; esac else - new_parents="$new_parents $p" + if test -f "$DROPPED"/$p + then + fast_forward=f + cat "$DROPPED"/$p | while read grandparent + do + if test -f "$REWRITTEN"/$grandparent + then + new_p=$(cat "$REWRITTEN"/$grandparent) + else + new_p=$grandparent + fi + case "$new_parents" in + *$new_p*) + ;; # do nothing; that parent is already there + *) + new_parents="$new_parents $new_p" + ;; + esac + done + else + new_parents="$new_parents $p" + fi fi done case $fast_forward in
@@ -574,6 +588,24 @@ do # EOF + # Watch for commits that been dropped by --cherry-pick + if test t = "$PRESERVE_MERGES" + then + mkdir "$DROPPED" + # drop the --cherry-pick parameter this time + git rev-list $MERGES_OPTION --abbrev-commit \ + --abbrev=7 $UPSTREAM...$HEAD --left-right | \ + sed -n "s/^>//p" | while read rev + do + grep --quiet "$rev" "$TODO" + if [ $? -ne 0 ] + then + full=$(git rev-parse $rev) + git rev-list --parents -1 $rev | cut -d' ' -f2- | sed 's/ /\n/g' > "$DROPPED"/$full + fi + done + fi + has_action "$TODO" || die_abort "Nothing to do"
t/t3409-rebase-interactive-cherry-picked.sh new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
1.6.0.2
RFC PATCH v2 1/1 Fix interactive rebase on dropped commits.
Interactive rebase got it's rev-list of commits to keep by --left-right and --cherry-pick. Adding --cherry-pick would throw out commits that were just duplicating changes already in the rebase target.
Which is cool, except the dropped commit was forgotten about when it came to rewriting the parents of its descendents, so the descendents would get cherry-picked as-in and essentially make the rebase a noop.
This change adds a $DOTEST/dropped directory to remember dropped commits and rewrite its children's parents as the children's grandparents (possibly rewritten).
---
This includes 3410, which borrows the 3404 DAG in a separate test to do more a complex test of the dropped commits than my previous test.
The implementation is also simpler--instead of following all of a dropped commit's parents, we follow just the first. We can trust rev-list is telling us to drop it for a reason, so only the first-parent is needed so we can correctly rewrite commits based on top of ours.
t3404 is still passing. (Finally.)
git-rebase--interactive.sh | 37 +++++++- t/t3410-rebase-preserve-dropped-merges.sh | 140 +++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 t/t3410-rebase-preserve-dropped-merges.sh
git-rebase--interactive.sh +35 -2
@@ -37,6 +37,7 @@ DONE="$DOTEST"/done MSG="$DOTEST"/message SQUASH_MSG="$DOTEST"/message-squash REWRITTEN="$DOTEST"/rewritten +DROPPED="$DOTEST"/dropped PRESERVE_MERGES= STRATEGY= ONTO=
@@ -169,8 +170,12 @@ pick_one_preserving_merges () { # rewrite parents; if none were rewritten, we can fast-forward. new_parents= - for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) + pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)" + while [ "$pend" != "" ] do + p=$(expr "$pend" : ' \([^ ]*\)') + pend="${pend# $p}" + if test -f "$REWRITTEN"/$p then new_p=$(cat "$REWRITTEN"/$p)
@@ -183,7 +188,13 @@ pick_one_preserving_merges () { ;; esac else - new_parents="$new_parents $p" + if test -f "$DROPPED"/$p + then + fast_forward=f + pend="$pend $(cat "$DROPPED"/$p)" + else + new_parents="$new_parents $new_p" + fi fi done case $fast_forward in
@@ -582,6 +593,28 @@ first and then run 'git rebase --continue' again." # EOF + # Watch for commits that been dropped by --cherry-pick + if test t = "$PRESERVE_MERGES" + then + mkdir "$DROPPED" + # drop the --cherry-pick parameter this time + git rev-list $MERGES_OPTION --abbrev-commit \ + --abbrev=7 $UPSTREAM...$HEAD --left-right | \ + sed -n "s/^>//p" | while read rev + do + grep --quiet "$rev" "$TODO" + if [ $? -ne 0 ] + then + # Use -f2 because if rev-list is telling 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 us + full=$(git rev-parse $rev) + git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$full + fi + done + fi + has_action "$TODO" || die_abort "Nothing to do"
t/t3410-rebase-preserve-dropped-merges.sh new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
1.6.0.2
PATCH v3 1/1 rebase--interactive: fix parent rewriting for dropped commits
rebase -i -p got its rev-list of commits to keep by --left-right and --cherry-pick. Adding --cherry-pick would drop commits that duplicated changes already in the rebase target.
The dropped commits were then forgotten about when it came to rewriting the parents of their descendents, so the descendents would get cherry-picked with their old, unwritten parents and essentially make the rebase a no-op.
This commit adds a $DOTEST/dropped directory to remember dropped commits and rewrite their children's parent as the dropped commit's possibly-rewritten first-parent.
---
These two lines changed from the v2 patch:
pend=" $(cat "$DROPPED"/$p)$pend"
It now puts $p at the start of $pend instead of the end. I have no tests that assert this behavior (vs. the old of putting it at the end), but it seems rational to put the dropped-replacement parent first so that the current commit's parents come out in the same order (otherwise a commit with parents "p1 p2dropped p3" would end up "p1 p3 p2firstparent").
Also, this line changed:
new_parents="$new_parents $p"
Previously it said "$new_parents $new_p" which was a copy/paste bug--$new_p was from up in the code a bit and here we just want to use $p.
I also cleaned up the commit message.
git-rebase--interactive.sh | 37 +++++++- t/t3410-rebase-preserve-dropped-merges.sh | 140 +++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 t/t3410-rebase-preserve-dropped-merges.sh
git-rebase--interactive.sh +35 -2
@@ -37,6 +37,7 @@ DONE="$DOTEST"/done MSG="$DOTEST"/message SQUASH_MSG="$DOTEST"/message-squash REWRITTEN="$DOTEST"/rewritten +DROPPED="$DOTEST"/dropped PRESERVE_MERGES= STRATEGY= ONTO=
@@ -169,8 +170,12 @@ pick_one_preserving_merges () { # rewrite parents; if none were rewritten, we can fast-forward. new_parents= - for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) + pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)" + while [ "$pend" != "" ] do + p=$(expr "$pend" : ' \([^ ]*\)') + pend="${pend# $p}" + if test -f "$REWRITTEN"/$p then new_p=$(cat "$REWRITTEN"/$p)
@@ -183,7 +188,13 @@ pick_one_preserving_merges () { ;; esac else - new_parents="$new_parents $p" + if test -f "$DROPPED"/$p + then + fast_forward=f + pend=" $(cat "$DROPPED"/$p)$pend" + else + new_parents="$new_parents $p" + fi fi done case $fast_forward in
@@ -582,6 +593,28 @@ first and then run 'git rebase --continue' again." # EOF + # Watch for commits that been dropped by --cherry-pick + if test t = "$PRESERVE_MERGES" + then + mkdir "$DROPPED" + # drop the --cherry-pick parameter this time + git rev-list $MERGES_OPTION --abbrev-commit \ + --abbrev=7 $UPSTREAM...$HEAD --left-right | \ + sed -n "s/^>//p" | while read rev + do + grep --quiet "$rev" "$TODO" + if [ $? -ne 0 ] + then + # Use -f2 because if rev-list is telling 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 us + full=$(git rev-parse $rev) + git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$full + fi + done + fi + has_action "$TODO" || die_abort "Nothing to do"
t/t3410-rebase-preserve-dropped-merges.sh new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
1.6.0.2