Skip to contentSkip to conversation

rebuash - squash/rebase in a single step

PATCH In review Edmundo Carmona Antoranz 5 participants 13 messages Jun 30, 2019 – Jul 2, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 rebuash - squash/rebase in a single step

Rebuash allows us to do squash/rebase without having to resort to use rebase.

Consider the case where we have feature branches with merges in history:

Rx: revisions in main branch
Fx: Revisions in feature branch
Mx: Merge revisions (some of them with conflicts)

------------

	R1---R2---R3---R4---R5---R6---R7
	 \         \              \
	  F1---F2---M1---F3---F4---M2---F5

------------

If on M1 there were conflicts, it's more than likely that if we tried to cherry-pick R1..F2 on top of R7, we will have to deal with a conflict. But that conflict has already been taken care of on M1. So, in order to leverage that work that has already been done, instead of looking back, we look forward.

First, we create a (temporary) merge commit of both branches (M3)

------------

	R1---R2---R3---R4---R5---R6---R7---M3
	 \         \              \       /
	  F1---F2---M1---F3---F4---M2---F5

------------

At this point, all differences between M3 and R7 are the changes related to the feature branch, so we can run git reset --soft from M3 to R7 to put all those differeces in index, and then we create single revision that is both squashed/rebased for our feature branch.

---
 .gitignore                    |   1 +
 Documentation/git-rebuash.txt |  76 ++++++++
 Makefile                      |   1 +
 command-list.txt              |   1 +
 git-rebuash.sh                | 324 ++++++++++++++++++++++++++++++++++
 5 files changed, 403 insertions(+)
 create mode 100644 Documentation/git-rebuash.txt
 create mode 100644 git-rebuash.sh
.gitignore +1 -0
index 4470d7cfc0..46b5c94c8f 100644
@@ -123,6 +123,7 @@
 /git-read-tree
 /git-rebase
 /git-rebase--preserve-merges
+/git-rebuash
 /git-receive-pack
 /git-reflog
 /git-remote
Documentation/git-rebuash.txt new file +76 -0

76 lines not on this page · raw message

Makefile +1 -0
index f58bf14c7b..c2869d7c11 100644
@@ -614,6 +614,7 @@ SCRIPT_SH += git-merge-resolve.sh
 SCRIPT_SH += git-mergetool.sh
 SCRIPT_SH += git-quiltimport.sh
 SCRIPT_SH += git-legacy-stash.sh
+SCRIPT_SH += git-rebuash.sh
 SCRIPT_SH += git-request-pull.sh
 SCRIPT_SH += git-submodule.sh
 SCRIPT_SH += git-web--browse.sh
command-list.txt +1 -0
index 3a9af104b5..cf1cb48fde 100644
@@ -143,6 +143,7 @@ git-quiltimport                         foreignscminterface
 git-range-diff                          mainporcelain
 git-read-tree                           plumbingmanipulators
 git-rebase                              mainporcelain           history
+git-rebuash                             mainporcelain           history
 git-receive-pack                        synchelpers
 git-reflog                              ancillarymanipulators           complete
 git-remote                              ancillarymanipulators           complete
git-rebuash.sh new file +324 -0

324 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 rebuash - support for status

---
 wt-status.c | 49 +++++++++++++++++++++++++++++++++++++++++++------
 wt-status.h |  1 +
 2 files changed, 44 insertions(+), 6 deletions(-)
wt-status.c +43 -6
index 0bccef542f..2a7627b331 100644
@@ -1118,11 +1118,24 @@ static void show_merge_in_progress(struct wt_status *s,
 	if (has_unmerged(s)) {
 		status_printf_ln(s, color, _("You have unmerged paths."));
 		if (s->hints) {
-			status_printf_ln(s, color,
-					 _("  (fix conflicts and run \"git commit\")"));
-			status_printf_ln(s, color,
-					 _("  (use \"git merge --abort\" to abort the merge)"));
+			if (s->state.rebuash_in_progress) {
+				status_printf_ln(s, color,
+						_("  (fix conflicts and run \"git rebuash --continue\")"));
+				status_printf_ln(s, color,
+						_("  (use \"git rebuash --abort\" to abort the operation)"));
+			} else {
+				status_printf_ln(s, color,
+						_("  (fix conflicts and run \"git commit\")"));
+				status_printf_ln(s, color,
+						_("  (use \"git merge --abort\" to abort the merge)"));
+			}
 		}
+	} else if (s->state.rebuash_in_progress) {
+		status_printf_ln(s, color,
+			_("All conflicts fixed but you are still rebuashing."));
+		if (s->hints)
+			status_printf_ln(s, color,
+				_("  (use \"git rebuash --continue\" to conclude rebuash)"));
 	} else {
 		status_printf_ln(s, color,
 			_("All conflicts fixed but you are still merging."));
@@ -1386,6 +1399,15 @@ static void show_rebase_in_progress(struct wt_status *s,
 	wt_longstatus_print_trailer(s);
 }
 
+static void show_rebuash_in_progress(struct wt_status *s,
+				    const char *color)
+{
+	if (&s->state.rebuash_in_progress)
+		status_printf_ln(s, color,
+			_("Rebuash currently in progress."));
+	wt_longstatus_print_trailer(s);
+}
+
 static void show_cherry_pick_in_progress(struct wt_status *s,
 					 const char *color)
 {
@@ -1583,6 +1605,18 @@ int wt_status_check_rebase(const struct worktree *wt,
 	return 1;
 }
 
+int wt_status_check_rebuash(const struct worktree *wt,
+			   struct wt_status_state *state)
+{
+	struct stat st;
+
+	if (!stat(worktree_git_path(wt, "REBUASH_STATE"), &st)) {
+		state->rebuash_in_progress = 1;
+	} else
+		return 0;
+	return 1;
+}
+
 int wt_status_check_bisect(const struct worktree *wt,
 			   struct wt_status_state *state)
 {
@@ -1614,6 +1648,7 @@ void wt_status_get_state(struct repository *r,
 		state->cherry_pick_in_progress = 1;
 		oidcpy(&state->cherry_pick_head_oid, &oid);
 	}
+	wt_status_check_rebuash(NULL, state);
 	wt_status_check_bisect(NULL, state);
 	if (!stat(git_path_revert_head(r), &st) &&
 	    !get_oid("REVERT_HEAD", &oid)) {
@@ -1643,6 +1678,8 @@ static void wt_longstatus_print_state(struct wt_status *s)
 			show_rebase_information(s, state_color);
 			fputs("\n", s->fp);
 		}
+		if (state->rebuash_in_progress)
+			show_rebuash_in_progress(s, state_color);
 		show_merge_in_progress(s, state_color);
 	} else if (state->am_in_progress)
 		show_am_in_progress(s, state_color);
@@ -1688,7 +1725,7 @@ static void wt_longstatus_print(struct wt_status *s)
 		status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
 		status_printf_more(s, branch_status_color, "%s", on_what);
 		status_printf_more(s, branch_color, "%s\n", branch_name);
-		if (!s->is_initial)
+		if (!(s->is_initial || s->state.rebuash_in_progress))
 			wt_longstatus_print_tracking(s);
 	}
 
@@ -1731,7 +1768,7 @@ static void wt_longstatus_print(struct wt_status *s)
 
 	if (s->verbose)
 		wt_longstatus_print_verbose(s);
-	if (!s->committable) {
+	if (!(s->committable || s->state.rebuash_in_progress)) {
 		if (s->amend)
 			status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
 		else if (s->nowarn)
wt-status.h +1 -0
index 64f1ddc9fd..2995866327 100644
@@ -72,6 +72,7 @@ struct wt_status_state {
 	int rebase_in_progress;
 	int rebase_interactive_in_progress;
 	int cherry_pick_in_progress;
+	int rebuash_in_progress;
 	int bisect_in_progress;
 	int revert_in_progress;
 	int detached_at;
Signature · 1 line
2.20.1
#2 lore raw

Re: [RFC/PATCH 2/2] rebuash - support for status

Quoted 6 lines · Edmundo Carmona Antoranz
---
 wt-status.c | 49 +++++++++++++++++++++++++++++++++++++++++++------
 wt-status.h |  1 +
 2 files changed, 44 insertions(+), 6 deletions(-)

I bet there are more things to do... like:
- what happens if the user runs git checkout --force some-branch? The state file has to be deleted, right?
- what should happen on the reflog when there's the merge then reset? Is it possible to control what will show up on the reflog from the script?

So any guiding word on the things I should take a look at would be greatly appreciated.

#3 lore raw
In reply to Edmundo Carmona Antoranz
Quoted 26 lines · Edmundo Carmona Antoranz

Rebuash allows us to do squash/rebase without having to resort to use rebase.

Consider the case where we have feature branches with merges in history:

Rx: revisions in main branch
Fx: Revisions in feature branch
Mx: Merge revisions (some of them with conflicts)

------------

	R1---R2---R3---R4---R5---R6---R7
	 \         \              \
	  F1---F2---M1---F3---F4---M2---F5

------------

If on M1 there were conflicts, it's more than likely that if we tried to cherry-pick R1..F2 on top of R7, we will have to deal with a conflict. But that conflict has already been taken care of on M1. So, in order to leverage that work that has already been done, instead of looking back, we look forward.

First, we create a (temporary) merge commit of both branches (M3)

------------

	R1---R2---R3---R4---R5---R6---R7---M3
	 \         \              \       /
	  F1---F2---M1---F3---F4---M2---F5
Quoted 6 lines

------------

At this point, all differences between M3 and R7 are the changes related to the feature branch, so we can run git reset --soft from M3 to R7 to put all those differeces in index, and then we create single revision that is both squashed/rebased for our feature branch.

So if I understand correctly, our goal is:

R1--R2--...--R7--R8

where R8 has the same tree as M3?

Wouldn't doing "git merge --squash" do the same thing?

If I set up that scenario like so:

git init repo
cd repo

  commit() {
    for i in "$@"; do echo $i >file && git add file && git commit -m $i; done
  }

commit R1 R2 R3
git checkout -b feature HEAD~2
commit F1 F2
git merge master
commit M1
git checkout master
commit R4 R5 R6
git checkout feature
commit F3 F4
git merge master
commit M2 F5
git checkout master
commit R7

and then do:

  git merge --squash feature

I get the same merge that rebuash is doing (with R6 as the merge base, so we see F5 and R7 conflicting with each other). And then when I finish it with "git commit", the result is a linear strand with M3 at the tip (and its commit message is even auto-populated with information from the squashed commits).

-Peff

#4 lore raw
Quoted 6 lines · Jeff King

and then do:

  git merge --squash feature
Quoted 7 lines

I get the same merge that rebuash is doing (with R6 as the merge base, so we see F5 and R7 conflicting with each other). And then when I finish it with "git commit", the result is a linear strand with M3 at the tip (and its commit message is even auto-populated with information from the squashed commits).

-Peff

From the point of view of the revisions that you produce in the end, it's the same thing, but you are not rebasing/squashing your feature branch, you are moving your upstream branch to where you want the squashed/rebased branch to be. So, in fact you would need more steps, something like (starting from your feature branch being checked out):

git checkout --detach $( git rev-parse --abbrev-ref --symbolic-full-name @{u} ) git merge --squash my-feature-branch
git branch -f my-feature-branch
git checkout my-feature-branch

Yes, it works. Only that with rebuash you would do (starting from the feature branch being checked out branch):

git rebuash

as long as the upstream branch is set, of course.

I think it makes more sense in terms of development flow of feature branches, if you know in the end you will give up a squashed branch:

modify
commit
modify
commit
git pull # no need to use pull --rebase, merges will be fine modify
commit
modify
commit
git pull
git modify

# now I'm ready to rebase/squash
git fetch
git rebuash

adding history could be done with an additional option (--hist (default) and --no-hist?)

But, as you said, it's not like it's not possible to do it (with a little more effort) with available tools like merge --squash

#5 lore raw
Quoted 8 lines · Edmundo Carmona Antoranz
Quoted 7 lines
  git merge --squash feature

I get the same merge that rebuash is doing (with R6 as the merge base, so we see F5 and R7 conflicting with each other). And then when I finish it with "git commit", the result is a linear strand with M3 at the tip (and its commit message is even auto-populated with information from the squashed commits).

Quoted 5 lines

From the point of view of the revisions that you produce in the end, it's the same thing, but you are not rebasing/squashing your feature branch, you are moving your upstream branch to where you want the squashed/rebased branch to be. So, in fact you would need more steps, something like (starting from your feature branch being checked out):

Ah, OK, that's what I was missing. I agree that "merge --squash" isn't quite what you want, then. It's sort of a "reverse squash": do the merge, but use the _other_ side as the parent of the new squash commit.

I wonder if it might be easier to implement as an option to git-merge. I noticed when I hit a conflict with rebuash that it emphatically told me not to use "git commit" once I had resolved everything. If this were just a special case of a merge, that might be a bit more seamless (though I imagine it might still require teaching git-commit about the feature, since it generally wants to mark HEAD as the parent).

Quoted 2 lines

I think it makes more sense in terms of development flow of feature branches, if you know in the end you will give up a squashed branch:

Yeah, I agree it is a separate operation that by itself makes sense. I do wonder a little why you'd care about squashing on the branch. If you're eventually going to squash the whole thing anyway, you don't care about a messy history. So you can just continue to back-merge from master into the feature branch and build on top.

But perhaps the squashed version is easier to work with for further modifications? I'm not sure how, though. Certainly in your example rewriting changes in F1 with "rebase --interactive" would be a pain. But I think the end-state of the tree after your rebuash is identical to what you'd get by just merging from master. So in either case, just building new work on top should be the same.

Quoted 2 lines

But, as you said, it's not like it's not possible to do it (with a little more effort) with available tools like merge --squash

Yeah, but I agree with you that just because it is possible to do something does not mean that it is not a good idea to make the workflow easier or safer.

I'm still not quite sure of the greater workflow where having the rebuash-ed commit on the feature branch is more useful than just having a merge from master.

-Peff

#6 lore raw
Quoted 9 lines · Jeff King

But perhaps the squashed version is easier to work with for further modifications? I'm not sure how, though. Certainly in your example rewriting changes in F1 with "rebase --interactive" would be a pain. But I think the end-state of the tree after your rebuash is identical to what you'd get by just merging from master. So in either case, just building new work on top should be the same.

Quoted 3 lines

I'm still not quite sure of the greater workflow where having the rebuash-ed commit on the feature branch is more useful than just having a merge from master.

Hmm... I as a gatekeeper would rather get either a straight line of revisions for a feature with no merges (even if a final merge takes care of solving conflicts with the upstream branch) or a single revision (if I thought that the change is not worth having more than a single revision). I'd ask the developer to rebase the whole thing and give a straight line (with rebase -i or cherry-picks) or to give me a single revision (where rebuash would come into the picture).

Also, I wonder how it would make life easier for people that are learning to use git and the command that they see thrown around very often is to use git pull in order to get updates from the other developers.

But that might be me being opinionated.

PS About rebuash ordering not to use commit: Sure, at the moment, rebuash is not commit-safe.... or merge-continue-safe.... but I can add checks for that in case the user runs them before using rebuash --continue

#7 lore raw
Quoted 7 lines · Edmundo Carmona Antoranz

Hmm... I as a gatekeeper would rather get either a straight line of revisions for a feature with no merges (even if a final merge takes care of solving conflicts with the upstream branch) or a single revision (if I thought that the change is not worth having more than a single revision). I'd ask the developer to rebase the whole thing and give a straight line (with rebase -i or cherry-picks) or to give me a single revision (where rebuash would come into the picture).

That part is understandable, but is "rebase-and-squash" a tool intended to be used by the contributor to respond to that request?

Wouldn't the developer just do

git checkout topic
git fetch
git rebase [-i] [@{upstream}]
git push [publish +topic]

to update the topic and ask to be pulled again? The two steps in the middle may be "pull --rebase", but my point is I do not quite see where the new squash/rebase-in-a-single-step thing comes into this picture. There may be a different picture that it fits, but I do not think it is this one.

#8 lore raw
Quoted 12 lines · Junio C Hamano

That part is understandable, but is "rebase-and-squash" a tool intended to be used by the contributor to respond to that request?

Wouldn't the developer just do

git checkout topic
git fetch
git rebase [-i] [@{upstream}]
git push [publish +topic]

Quoted 7 lines

to update the topic and ask to be pulled again? The two steps in the middle may be "pull --rebase", but my point is I do not quite see where the new squash/rebase-in-a-single-step thing comes into this picture. There may be a different picture that it fits, but I do not think it is this one.

I think rebasing -i makes sense* _if_ there are no conflicts on the way to reach the current state of the branch if the developer was pulling while developing the feature branch. If there are conflicts that I took care of when i was pulling, i don't want to run rebase -i to have to deal with them yet again. So rebuash would help developers with or without merges with upstream on their feature branch, with or without conflicts on those merges (if there are merges), get their development into a single revision without having to write 4 or 5 git commands, as Jeff was saying, if it makes workflows simpler....

* rebase -i is the way I see people solve their "squash and rebase" needs (pick the first revision, then squash all the others... but what happens if the very first revision is conflicting with the current state of upstream?), but I (for one) do it the way rebuash is doing:
merge with upstream, reset --soft to upstream, commit, voila! That's why i wrote rebuash in the first place, just so you can see where I'm coming from.

#9 lore raw
In reply to Jeff King
Quoted 13 lines · Jeff King
Quoted 6 lines

First, we create a (temporary) merge commit of both branches (M3)

------------

	R1---R2---R3---R4---R5---R6---R7---M3
	 \         \              \       /
	  F1---F2---M1---F3---F4---M2---F5
Quoted 6 lines

------------

At this point, all differences between M3 and R7 are the changes related to the feature branch, so we can run git reset --soft from M3 to R7 to put all those differeces in index, and then we create single revision that is both squashed/rebased for our feature branch.

Quoted 7 lines

So if I understand correctly, our goal is:

R1--R2--...--R7--R8

where R8 has the same tree as M3?

Wouldn't doing "git merge --squash" do the same thing?

Yup, from Edmundo's description, I agree that they are equivalent, modulo the merge direction.

That affects two things, though. Who becomes the first parent is obviously swapped, but equally importantly, the merge conflicts are presented as if you are merging from the upstream, taking assortment of random changes into a stale codebase with slight modification. Swapping the direction would present the merge much better in that it let you pretend as if you started from the up-to-date upstream and replayed your own changes in the topic, and because you are by definition more familiar with your own changes, during conflict resolution, you would understand the output from "git diff HEAD" much better than the case where you merge upstream into your topic.

But "rebase the feature branch on updated upstream and then merge the result, optionally squashing all of them into one big ball of wax" *could* be a lot more useful feature, serving as poor-man's imitation of "git imerge", *but* only if the final squashing is made optional (that at the same time means that sometimes the M3 merge can be unmanageably messy and stepwise rebase may make it manageable).

If M3 merge is always easier to manage than incremental stepwise rebase of the topic, then doing the "git merge --reverse-squash" would be a saner interface and also conceptually simpler.

#10 lore raw
Quoted 25 lines · Junio C Hamano
Quoted 13 lines · Jeff King
Quoted 6 lines

First, we create a (temporary) merge commit of both branches (M3)

------------

	R1---R2---R3---R4---R5---R6---R7---M3
	 \         \              \       /
	  F1---F2---M1---F3---F4---M2---F5
Quoted 6 lines

------------

At this point, all differences between M3 and R7 are the changes related to the feature branch, so we can run git reset --soft from M3 to R7 to put all those differeces in index, and then we create single revision that is both squashed/rebased for our feature branch.

Quoted 7 lines

So if I understand correctly, our goal is:

R1--R2--...--R7--R8

where R8 has the same tree as M3?

Wouldn't doing "git merge --squash" do the same thing?

Yup, from Edmundo's description, I agree that they are equivalent, modulo the merge direction.

[snip]

Quoted 3 lines

If M3 merge is always easier to manage than incremental stepwise rebase of the topic, then doing the "git merge --reverse-squash" would be a saner interface and also conceptually simpler.

I agree that this would be a better way to expose this behavior, and likely the implementation could be very clean.

Thanks,
-Stolee

#11 lore raw
Quoted 11 lines · Derrick Stolee
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Jeff King
Quoted 8 lines

First, we create a (temporary) merge commit of both branches (M3)

------------

	R1---R2---R3---R4---R5---R6---R7---M3
	 \         \              \       /
	  F1---F2---M1---F3---F4---M2---F5

------------

Quoted 7 lines

...

Quoted 3 lines

If M3 merge is always easier to manage than incremental stepwise rebase of the topic, then doing the "git merge --reverse-squash" would be a saner interface and also conceptually simpler.

I agree that this would be a better way to expose this behavior, and likely the implementation could be very clean.

What I was sort-of hoping to get comments on was actually something else.

Would there be cases where the merge M3 gets unmanageably complex even if rebasing the feature commits one by one is relatively simple (and how often would that happen)? "merge --squash" would not work well (and extending the command to merge in a different direction would not help) in such a situation, but "rebase -i" would work much better (and "imerge" would, too).

#12 lore raw

Am 02.07.19 um 19:20 schrieb Junio C Hamano:

Quoted 24 lines
Quoted 11 lines · Derrick Stolee
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Jeff King
Quoted 8 lines

First, we create a (temporary) merge commit of both branches (M3)

------------

	R1---R2---R3---R4---R5---R6---R7---M3
	 \         \              \       /
	  F1---F2---M1---F3---F4---M2---F5

------------

Quoted 7 lines

...

Quoted 3 lines

If M3 merge is always easier to manage than incremental stepwise rebase of the topic, then doing the "git merge --reverse-squash" would be a saner interface and also conceptually simpler.

I agree that this would be a better way to expose this behavior, and likely the implementation could be very clean.

What I was sort-of hoping to get comments on was actually something else.

Quoted 6 lines

Would there be cases where the merge M3 gets unmanageably complex even if rebasing the feature commits one by one is relatively simple (and how often would that happen)? "merge --squash" would not work well (and extending the command to merge in a different direction would not help) in such a situation, but "rebase -i" would work much better (and "imerge" would, too).

I've come across the situation occasionally. Once I have resolved a bunch of conflicts in M1 and M2, I think twice whether I should rebase individual commits; it is typically much more tedious.

A common situation is that a line is "A" in F1, "B" in F2, and "C" in R3; then I have to resolve ONE conflict in M1 ("<B=C>"), but with individual commits rebased on top of R3, I have two conflicts, "<C=A>" and "<AC=B>", neither of which is helped by rerere.

After merges M1 and M2, it is all a done deal, and M3 is typically a much simpler merge than the sum of conflicts incurred by the individual commits. I would generally not recommend a rebase in this situation.

But I wouldn't turn M3 into a squashed merged commit, either, as long as F1...F5 aren't messy.

-- Hannes