git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase
@ 2019-02-28 15:27 Johannes Schindelin via GitGitGadget
  2019-02-28 15:27 ` [PATCH 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-02-28 15:27 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Junio C Hamano

It was reported by Nazri Ramliy that ORIG_HEAD was set incorrectly again,
this time caused by the shortcut to call git am directly, without detouring
to a Unix shell script.

Patch 2/4 might look like something completely unrelated, but without it,
the update to HEAD might use an incorrect reflog message.

Patch 1/4 is more a "while at it" patch; while looking which code paths
might need to update ORIG_HEAD, I noticed that this reset_head() call did
unnecessary work.

Johannes Schindelin (4):
  built-in rebase: no need to check out `onto` twice
  built-in rebase: use the correct reflog when switching branches
  built-in rebase: demonstrate that ORIG_HEAD is not set correctly
  built-in rebase: set ORIG_HEAD just once, before the rebase

 builtin/rebase.c  | 37 +++++++++++++++++++++----------------
 t/t3400-rebase.sh |  8 ++++++++
 2 files changed, 29 insertions(+), 16 deletions(-)


base-commit: 21853626eac565dd42572d90724b29863f61eb3b
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-153%2Fdscho%2Frebase-am-and-orig-head-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-153/dscho/rebase-am-and-orig-head-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/153
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-02-28 15:27 [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
@ 2019-02-28 15:27 ` Johannes Schindelin via GitGitGadget
  2019-02-28 20:55   ` Phillip Wood
  2019-02-28 15:27 ` [PATCH 2/4] built-in rebase: use the correct reflog when switching branches Johannes Schindelin via GitGitGadget
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-02-28 15:27 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

In the case that the rebase boils down to a fast-forward, the built-in
rebase reset the working tree twice: once to start the rebase at `onto`,
then realizing that the original HEAD was an ancestor, `reset_head()`
was called to update the original ref and to point HEAD back to it.

That second `reset_head()` call does not need to touch the working tree,
though, as it does not change the actual tip commit. So let's avoid that
unnecessary work.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 08ec4d52c7..813ec284ca 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -1740,8 +1740,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 		strbuf_addf(&msg, "rebase finished: %s onto %s",
 			options.head_name ? options.head_name : "detached HEAD",
 			oid_to_hex(&options.onto->object.oid));
-		reset_head(NULL, "Fast-forwarded", options.head_name, 0,
-			   "HEAD", msg.buf);
+		reset_head(NULL, "Fast-forwarded", options.head_name,
+			   RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
 		strbuf_release(&msg);
 		ret = !!finish_rebase(&options);
 		goto cleanup;
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 2/4] built-in rebase: use the correct reflog when switching branches
  2019-02-28 15:27 [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
  2019-02-28 15:27 ` [PATCH 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
@ 2019-02-28 15:27 ` Johannes Schindelin via GitGitGadget
  2019-02-28 15:27 ` [PATCH 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly Johannes Schindelin via GitGitGadget
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-02-28 15:27 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

By mistake, we used the reflog intended for ORIG_HEAD.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 813ec284ca..aa469ec964 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -475,7 +475,7 @@ static int reset_head(struct object_id *oid, const char *action,
 				 detach_head ? REF_NO_DEREF : 0,
 				 UPDATE_REFS_MSG_ON_ERR);
 	else {
-		ret = update_ref(reflog_orig_head, switch_to_branch, oid,
+		ret = update_ref(reflog_head, switch_to_branch, oid,
 				 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
 		if (!ret)
 			ret = create_symref("HEAD", switch_to_branch,
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly
  2019-02-28 15:27 [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
  2019-02-28 15:27 ` [PATCH 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
  2019-02-28 15:27 ` [PATCH 2/4] built-in rebase: use the correct reflog when switching branches Johannes Schindelin via GitGitGadget
@ 2019-02-28 15:27 ` Johannes Schindelin via GitGitGadget
  2019-02-28 15:27 ` [PATCH 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase Johannes Schindelin via GitGitGadget
  2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
  4 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-02-28 15:27 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

The ORIG_HEAD pseudo ref is supposed to refer to the original,
pre-rebase state after a successful rebase. Let's add a regression test
to prove that this regressed: With GIT_TEST_REBASE_USE_BUILTIN=false,
this test case passes, with GIT_TEST_REBASE_USE_BUILTIN=true (or unset),
it fails.

Reported by Nazri Ramliy.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/t3400-rebase.sh | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index 3e73f7584c..7e8d5bb200 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -59,6 +59,14 @@ test_expect_success 'rebase against master' '
 	git rebase master
 '
 
+test_expect_failure 'rebase sets ORIG_HEAD to pre-rebase state' '
+	git checkout -b orig-head topic &&
+	pre="$(git rev-parse --verify HEAD)" &&
+	git rebase master &&
+	test_cmp_rev "$pre" ORIG_HEAD &&
+	! test_cmp_rev "$pre" HEAD
+'
+
 test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
 	test_when_finished "git branch -D torebase" &&
 	git checkout -b torebase my-topic-branch^ &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase
  2019-02-28 15:27 [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
                   ` (2 preceding siblings ...)
  2019-02-28 15:27 ` [PATCH 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly Johannes Schindelin via GitGitGadget
@ 2019-02-28 15:27 ` Johannes Schindelin via GitGitGadget
  2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
  4 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-02-28 15:27 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

Technically, the scripted version set ORIG_HEAD only in two spots (which
really could have been one, because it called `git checkout $onto^0` to
start the rebase and also if it could take a shortcut, and in both cases
it called `git update-ref $orig_head`).

Practically, it *implicitly* reset ORIG_HEAD whenever `git reset --hard`
was called.

However, what we really want is that it is set exactly once, at the
beginning of the rebase.

So let's do that.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c  | 31 ++++++++++++++++++-------------
 t/t3400-rebase.sh |  2 +-
 2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index aa469ec964..0f4e1ead49 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
 #define RESET_HEAD_DETACH (1<<0)
 #define RESET_HEAD_HARD (1<<1)
 #define RESET_HEAD_REFS_ONLY (1<<2)
+#define RESET_ORIG_HEAD (1<<3)
 
 static int reset_head(struct object_id *oid, const char *action,
 		      const char *switch_to_branch, unsigned flags,
@@ -377,6 +378,7 @@ static int reset_head(struct object_id *oid, const char *action,
 	unsigned detach_head = flags & RESET_HEAD_DETACH;
 	unsigned reset_hard = flags & RESET_HEAD_HARD;
 	unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
+	unsigned update_orig_head = flags & RESET_ORIG_HEAD;
 	struct object_id head_oid;
 	struct tree_desc desc[2] = { { NULL }, { NULL } };
 	struct lock_file lock = LOCK_INIT;
@@ -453,18 +455,21 @@ static int reset_head(struct object_id *oid, const char *action,
 	strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
 	prefix_len = msg.len;
 
-	if (!get_oid("ORIG_HEAD", &oid_old_orig))
-		old_orig = &oid_old_orig;
-	if (!get_oid("HEAD", &oid_orig)) {
-		orig = &oid_orig;
-		if (!reflog_orig_head) {
-			strbuf_addstr(&msg, "updating ORIG_HEAD");
-			reflog_orig_head = msg.buf;
-		}
-		update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
-			   UPDATE_REFS_MSG_ON_ERR);
-	} else if (old_orig)
-		delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
+	if (update_orig_head) {
+		if (!get_oid("ORIG_HEAD", &oid_old_orig))
+			old_orig = &oid_old_orig;
+		if (!get_oid("HEAD", &oid_orig)) {
+			orig = &oid_orig;
+			if (!reflog_orig_head) {
+				strbuf_addstr(&msg, "updating ORIG_HEAD");
+				reflog_orig_head = msg.buf;
+			}
+			update_ref(reflog_orig_head, "ORIG_HEAD", orig,
+				   old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
+		} else if (old_orig)
+			delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
+	}
+
 	if (!reflog_head) {
 		strbuf_setlen(&msg, prefix_len);
 		strbuf_addstr(&msg, "updating HEAD");
@@ -1725,7 +1730,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 	strbuf_addf(&msg, "%s: checkout %s",
 		    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
 	if (reset_head(&options.onto->object.oid, "checkout", NULL,
-		       RESET_HEAD_DETACH, NULL, msg.buf))
+		       RESET_HEAD_DETACH | RESET_ORIG_HEAD, NULL, msg.buf))
 		die(_("Could not detach HEAD"));
 	strbuf_release(&msg);
 
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index 7e8d5bb200..460d0523be 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -59,7 +59,7 @@ test_expect_success 'rebase against master' '
 	git rebase master
 '
 
-test_expect_failure 'rebase sets ORIG_HEAD to pre-rebase state' '
+test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
 	git checkout -b orig-head topic &&
 	pre="$(git rev-parse --verify HEAD)" &&
 	git rebase master &&
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-02-28 15:27 ` [PATCH 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
@ 2019-02-28 20:55   ` Phillip Wood
  2019-03-01 13:19     ` Johannes Schindelin
  0 siblings, 1 reply; 16+ messages in thread
From: Phillip Wood @ 2019-02-28 20:55 UTC (permalink / raw)
  To: Johannes Schindelin via GitGitGadget, git
  Cc: Nazri Ramliy, Junio C Hamano, Johannes Schindelin

Hi Johannes

On 28/02/2019 15:27, Johannes Schindelin via GitGitGadget wrote:
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> In the case that the rebase boils down to a fast-forward, the built-in
> rebase reset the working tree twice: once to start the rebase at `onto`,
> then realizing that the original HEAD was an ancestor, `reset_head()`
> was called to update the original ref and to point HEAD back to it.
> 
> That second `reset_head()` call does not need to touch the working tree,
> though, as it does not change the actual tip commit. So let's avoid that
> unnecessary work.

I'm confused by this I think I must be missing something. If we've 
checked out onto then why does the working copy not need updating when 
we fast forward. (also why to we checkout onto before seeing if we can 
fast-forward but that's not related to this patch series)

Best Wishes

Phillip

> 
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>   builtin/rebase.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/builtin/rebase.c b/builtin/rebase.c
> index 08ec4d52c7..813ec284ca 100644
> --- a/builtin/rebase.c
> +++ b/builtin/rebase.c
> @@ -1740,8 +1740,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
>   		strbuf_addf(&msg, "rebase finished: %s onto %s",
>   			options.head_name ? options.head_name : "detached HEAD",
>   			oid_to_hex(&options.onto->object.oid));
> -		reset_head(NULL, "Fast-forwarded", options.head_name, 0,
> -			   "HEAD", msg.buf);
> +		reset_head(NULL, "Fast-forwarded", options.head_name,
> +			   RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
>   		strbuf_release(&msg);
>   		ret = !!finish_rebase(&options);
>   		goto cleanup;
> 

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-02-28 20:55   ` Phillip Wood
@ 2019-03-01 13:19     ` Johannes Schindelin
  2019-03-01 15:00       ` Phillip Wood
  0 siblings, 1 reply; 16+ messages in thread
From: Johannes Schindelin @ 2019-03-01 13:19 UTC (permalink / raw)
  To: phillip.wood
  Cc: Johannes Schindelin via GitGitGadget, git, Nazri Ramliy,
	Junio C Hamano

Hi Phillip,

On Thu, 28 Feb 2019, Phillip Wood wrote:

> On 28/02/2019 15:27, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> > 
> > In the case that the rebase boils down to a fast-forward, the built-in
> > rebase reset the working tree twice: once to start the rebase at `onto`,
> > then realizing that the original HEAD was an ancestor, `reset_head()`
> > was called to update the original ref and to point HEAD back to it.
> > 
> > That second `reset_head()` call does not need to touch the working tree,
> > though, as it does not change the actual tip commit. So let's avoid that
> > unnecessary work.
> 
> I'm confused by this I think I must be missing something. If we've checked out
> onto then why does the working copy not need updating when we fast forward.
> (also why to we checkout onto before seeing if we can fast-forward but that's
> not related to this patch series)

Sorry, I really try to learn how to express things clearly. Still learning
a lot.

So what happens is this: we detect the situation where the pre-rebase
`HEAD` is an ancestor of `onto`. We do this *after* checking out `onto`.
So we now know that we essentially fast-forwarded to the post-rebase
state.

What we still need to do is to update the original ref (unless we were on
a detached HEAD when the rebase was started).

The original shell code updates the original ref to the current HEAD, and
the updates HEAD to point to that symbolic ref instead of being detached.

In the C code, we abused `reset_head()` to do the same. But `reset_head()`
does more: it does a two-way merge (in this instance, because
`RESET_HEAD_HARD` was not part of the flags). Which is unnecessary.

That's all this commit is about.

Ciao,
Dscho

> 
> Best Wishes
> 
> Phillip
> 
> > 
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> >   builtin/rebase.c | 4 ++--
> >   1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/builtin/rebase.c b/builtin/rebase.c
> > index 08ec4d52c7..813ec284ca 100644
> > --- a/builtin/rebase.c
> > +++ b/builtin/rebase.c
> > @@ -1740,8 +1740,8 @@ int cmd_rebase(int argc, const char **argv, const char
> > *prefix)
> >     strbuf_addf(&msg, "rebase finished: %s onto %s",
> >      options.head_name ? options.head_name : "detached HEAD",
> >      oid_to_hex(&options.onto->object.oid));
> > -		reset_head(NULL, "Fast-forwarded", options.head_name, 0,
> > -			   "HEAD", msg.buf);
> > +		reset_head(NULL, "Fast-forwarded", options.head_name,
> > +			   RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
> >     strbuf_release(&msg);
> >     ret = !!finish_rebase(&options);
> >     goto cleanup;
> > 
> 

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-03-01 13:19     ` Johannes Schindelin
@ 2019-03-01 15:00       ` Phillip Wood
  2019-03-03  1:35         ` Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Phillip Wood @ 2019-03-01 15:00 UTC (permalink / raw)
  To: Johannes Schindelin, phillip.wood
  Cc: Johannes Schindelin via GitGitGadget, git, Nazri Ramliy,
	Junio C Hamano

Hi Dscho

On 01/03/2019 13:19, Johannes Schindelin wrote:
> Hi Phillip,
> 
> On Thu, 28 Feb 2019, Phillip Wood wrote:
> 
>> On 28/02/2019 15:27, Johannes Schindelin via GitGitGadget wrote:
>>> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>>>
>>> In the case that the rebase boils down to a fast-forward, the built-in
>>> rebase reset the working tree twice: once to start the rebase at `onto`,
>>> then realizing that the original HEAD was an ancestor, `reset_head()`
>>> was called to update the original ref and to point HEAD back to it.
>>>
>>> That second `reset_head()` call does not need to touch the working tree,
>>> though, as it does not change the actual tip commit. So let's avoid that
>>> unnecessary work.
>>
>> I'm confused by this I think I must be missing something. If we've checked out
>> onto then why does the working copy not need updating when we fast forward.
>> (also why to we checkout onto before seeing if we can fast-forward but that's
>> not related to this patch series)
> 
> Sorry, I really try to learn how to express things clearly. Still learning
> a lot.
> 
> So what happens is this: we detect the situation where the pre-rebase
> `HEAD` is an ancestor of `onto`. We do this *after* checking out `onto`.
> So we now know that we essentially fast-forwarded to the post-rebase
> state.

Ah that makes sense, for some reason I misread the commit message and 
thought that we were dealing with the case where onto was an ancestor of 
the original HEAD and we were fast-forwarding from onto back to the 
original HEAD.

> What we still need to do is to update the original ref (unless we were on
> a detached HEAD when the rebase was started).
> 
> The original shell code updates the original ref to the current HEAD, and
> the updates HEAD to point to that symbolic ref instead of being detached.
> 
> In the C code, we abused `reset_head()` to do the same. But `reset_head()`
> does more: it does a two-way merge (in this instance, because
> `RESET_HEAD_HARD` was not part of the flags). Which is unnecessary.
> 
> That's all this commit is about.

Thanks for explaining, it all makes sense to me now

Best Wishes

Phillip

> Ciao,
> Dscho
> 
>>
>> Best Wishes
>>
>> Phillip
>>
>>>
>>> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
>>> ---
>>>    builtin/rebase.c | 4 ++--
>>>    1 file changed, 2 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/builtin/rebase.c b/builtin/rebase.c
>>> index 08ec4d52c7..813ec284ca 100644
>>> --- a/builtin/rebase.c
>>> +++ b/builtin/rebase.c
>>> @@ -1740,8 +1740,8 @@ int cmd_rebase(int argc, const char **argv, const char
>>> *prefix)
>>>      strbuf_addf(&msg, "rebase finished: %s onto %s",
>>>       options.head_name ? options.head_name : "detached HEAD",
>>>       oid_to_hex(&options.onto->object.oid));
>>> -		reset_head(NULL, "Fast-forwarded", options.head_name, 0,
>>> -			   "HEAD", msg.buf);
>>> +		reset_head(NULL, "Fast-forwarded", options.head_name,
>>> +			   RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
>>>      strbuf_release(&msg);
>>>      ret = !!finish_rebase(&options);
>>>      goto cleanup;
>>>
>>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-03-01 15:00       ` Phillip Wood
@ 2019-03-03  1:35         ` Junio C Hamano
  2019-03-03 17:09           ` Johannes Schindelin
  2019-03-03 19:11           ` Phillip Wood
  0 siblings, 2 replies; 16+ messages in thread
From: Junio C Hamano @ 2019-03-03  1:35 UTC (permalink / raw)
  To: Phillip Wood
  Cc: Johannes Schindelin, phillip.wood,
	Johannes Schindelin via GitGitGadget, git, Nazri Ramliy

Phillip Wood <phillip.wood123@gmail.com> writes:

> Thanks for explaining, it all makes sense to me now

It would be necessary to make sure that it all makes sense to all
future readers.  Are they patches good enough as-is for that, or do
they need some updates before I take a look at them to pick up?

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-03-03  1:35         ` Junio C Hamano
@ 2019-03-03 17:09           ` Johannes Schindelin
  2019-03-03 19:11           ` Phillip Wood
  1 sibling, 0 replies; 16+ messages in thread
From: Johannes Schindelin @ 2019-03-03 17:09 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Phillip Wood, phillip.wood, Johannes Schindelin via GitGitGadget,
	git, Nazri Ramliy

Hi Junio,

On Sun, 3 Mar 2019, Junio C Hamano wrote:

> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
> > Thanks for explaining, it all makes sense to me now
> 
> It would be necessary to make sure that it all makes sense to all
> future readers.  Are they patches good enough as-is for that, or do
> they need some updates before I take a look at them to pick up?

As you frequently say: if a reviewer gets puzzled, it is often a good
indicator that the commit message needs to be improved. I did exactly that
and will send out the next iteration in a minute.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase
  2019-02-28 15:27 [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
                   ` (3 preceding siblings ...)
  2019-02-28 15:27 ` [PATCH 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase Johannes Schindelin via GitGitGadget
@ 2019-03-03 17:11 ` Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
                     ` (3 more replies)
  4 siblings, 4 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-03-03 17:11 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Phillip Wood, Junio C Hamano

It was reported by Nazri Ramliy that ORIG_HEAD was set incorrectly again,
this time caused by the shortcut to call git am directly, without detouring
to a Unix shell script.

Patch 2/4 might look like something completely unrelated, but without it,
the update to HEAD might use an incorrect reflog message.

Patch 1/4 is more a "while at it" patch; while looking which code paths
might need to update ORIG_HEAD, I noticed that this reset_head() call did
unnecessary work.

Changes since v1:

 * Reworded the unclear commit message of patch 1/4.

Johannes Schindelin (4):
  built-in rebase: no need to check out `onto` twice
  built-in rebase: use the correct reflog when switching branches
  built-in rebase: demonstrate that ORIG_HEAD is not set correctly
  built-in rebase: set ORIG_HEAD just once, before the rebase

 builtin/rebase.c  | 37 +++++++++++++++++++++----------------
 t/t3400-rebase.sh |  8 ++++++++
 2 files changed, 29 insertions(+), 16 deletions(-)


base-commit: 21853626eac565dd42572d90724b29863f61eb3b
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-153%2Fdscho%2Frebase-am-and-orig-head-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-153/dscho/rebase-am-and-orig-head-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/153

Range-diff vs v1:

 1:  2d99429387 ! 1:  59c3266ed5 built-in rebase: no need to check out `onto` twice
     @@ -4,12 +4,21 @@
      
          In the case that the rebase boils down to a fast-forward, the built-in
          rebase reset the working tree twice: once to start the rebase at `onto`,
     -    then realizing that the original HEAD was an ancestor, `reset_head()`
     -    was called to update the original ref and to point HEAD back to it.
     +    then realizing that the original (pre-rebase) HEAD was an ancestor and
     +    we basically already fast-forwarded to the post-rebase HEAD,
     +    `reset_head()` was called to update the original ref and to point HEAD
     +    back to it.
      
          That second `reset_head()` call does not need to touch the working tree,
     -    though, as it does not change the actual tip commit. So let's avoid that
     -    unnecessary work.
     +    though, as it does not change the actual tip commit (and therefore the
     +    working tree should stay unchanged anyway): only the ref needs to be
     +    updated (because the rebase detached the HEAD, and we want to go back to
     +    the branch on which the rebase was started).
     +
     +    But that second `reset_head()` was called without the flag to leave the
     +    working tree alone (the reason: when that call was introduced, that flag
     +    was not yet even thought of). Let's avoid that unnecessary work by
     +    passing that flag.
      
          Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
      
 2:  b92d76065d = 2:  67e41c032a built-in rebase: use the correct reflog when switching branches
 3:  1bc3cbad26 = 3:  17737998f4 built-in rebase: demonstrate that ORIG_HEAD is not set correctly
 4:  1786c172ef = 4:  7891c05f51 built-in rebase: set ORIG_HEAD just once, before the rebase

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 1/4] built-in rebase: no need to check out `onto` twice
  2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
@ 2019-03-03 17:11   ` Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly Johannes Schindelin via GitGitGadget
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-03-03 17:11 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Phillip Wood, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

In the case that the rebase boils down to a fast-forward, the built-in
rebase reset the working tree twice: once to start the rebase at `onto`,
then realizing that the original (pre-rebase) HEAD was an ancestor and
we basically already fast-forwarded to the post-rebase HEAD,
`reset_head()` was called to update the original ref and to point HEAD
back to it.

That second `reset_head()` call does not need to touch the working tree,
though, as it does not change the actual tip commit (and therefore the
working tree should stay unchanged anyway): only the ref needs to be
updated (because the rebase detached the HEAD, and we want to go back to
the branch on which the rebase was started).

But that second `reset_head()` was called without the flag to leave the
working tree alone (the reason: when that call was introduced, that flag
was not yet even thought of). Let's avoid that unnecessary work by
passing that flag.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 08ec4d52c7..813ec284ca 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -1740,8 +1740,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 		strbuf_addf(&msg, "rebase finished: %s onto %s",
 			options.head_name ? options.head_name : "detached HEAD",
 			oid_to_hex(&options.onto->object.oid));
-		reset_head(NULL, "Fast-forwarded", options.head_name, 0,
-			   "HEAD", msg.buf);
+		reset_head(NULL, "Fast-forwarded", options.head_name,
+			   RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
 		strbuf_release(&msg);
 		ret = !!finish_rebase(&options);
 		goto cleanup;
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 2/4] built-in rebase: use the correct reflog when switching branches
  2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly Johannes Schindelin via GitGitGadget
@ 2019-03-03 17:11   ` Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase Johannes Schindelin via GitGitGadget
  3 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-03-03 17:11 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Phillip Wood, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

By mistake, we used the reflog intended for ORIG_HEAD.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index 813ec284ca..aa469ec964 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -475,7 +475,7 @@ static int reset_head(struct object_id *oid, const char *action,
 				 detach_head ? REF_NO_DEREF : 0,
 				 UPDATE_REFS_MSG_ON_ERR);
 	else {
-		ret = update_ref(reflog_orig_head, switch_to_branch, oid,
+		ret = update_ref(reflog_head, switch_to_branch, oid,
 				 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
 		if (!ret)
 			ret = create_symref("HEAD", switch_to_branch,
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly
  2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
@ 2019-03-03 17:11   ` Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 2/4] built-in rebase: use the correct reflog when switching branches Johannes Schindelin via GitGitGadget
  2019-03-03 17:11   ` [PATCH v2 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase Johannes Schindelin via GitGitGadget
  3 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-03-03 17:11 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Phillip Wood, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

The ORIG_HEAD pseudo ref is supposed to refer to the original,
pre-rebase state after a successful rebase. Let's add a regression test
to prove that this regressed: With GIT_TEST_REBASE_USE_BUILTIN=false,
this test case passes, with GIT_TEST_REBASE_USE_BUILTIN=true (or unset),
it fails.

Reported by Nazri Ramliy.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 t/t3400-rebase.sh | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index 3e73f7584c..7e8d5bb200 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -59,6 +59,14 @@ test_expect_success 'rebase against master' '
 	git rebase master
 '
 
+test_expect_failure 'rebase sets ORIG_HEAD to pre-rebase state' '
+	git checkout -b orig-head topic &&
+	pre="$(git rev-parse --verify HEAD)" &&
+	git rebase master &&
+	test_cmp_rev "$pre" ORIG_HEAD &&
+	! test_cmp_rev "$pre" HEAD
+'
+
 test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
 	test_when_finished "git branch -D torebase" &&
 	git checkout -b torebase my-topic-branch^ &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase
  2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
                     ` (2 preceding siblings ...)
  2019-03-03 17:11   ` [PATCH v2 2/4] built-in rebase: use the correct reflog when switching branches Johannes Schindelin via GitGitGadget
@ 2019-03-03 17:11   ` Johannes Schindelin via GitGitGadget
  3 siblings, 0 replies; 16+ messages in thread
From: Johannes Schindelin via GitGitGadget @ 2019-03-03 17:11 UTC (permalink / raw)
  To: git; +Cc: Nazri Ramliy, Phillip Wood, Junio C Hamano, Johannes Schindelin

From: Johannes Schindelin <johannes.schindelin@gmx.de>

Technically, the scripted version set ORIG_HEAD only in two spots (which
really could have been one, because it called `git checkout $onto^0` to
start the rebase and also if it could take a shortcut, and in both cases
it called `git update-ref $orig_head`).

Practically, it *implicitly* reset ORIG_HEAD whenever `git reset --hard`
was called.

However, what we really want is that it is set exactly once, at the
beginning of the rebase.

So let's do that.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/rebase.c  | 31 ++++++++++++++++++-------------
 t/t3400-rebase.sh |  2 +-
 2 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/builtin/rebase.c b/builtin/rebase.c
index aa469ec964..0f4e1ead49 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
 #define RESET_HEAD_DETACH (1<<0)
 #define RESET_HEAD_HARD (1<<1)
 #define RESET_HEAD_REFS_ONLY (1<<2)
+#define RESET_ORIG_HEAD (1<<3)
 
 static int reset_head(struct object_id *oid, const char *action,
 		      const char *switch_to_branch, unsigned flags,
@@ -377,6 +378,7 @@ static int reset_head(struct object_id *oid, const char *action,
 	unsigned detach_head = flags & RESET_HEAD_DETACH;
 	unsigned reset_hard = flags & RESET_HEAD_HARD;
 	unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
+	unsigned update_orig_head = flags & RESET_ORIG_HEAD;
 	struct object_id head_oid;
 	struct tree_desc desc[2] = { { NULL }, { NULL } };
 	struct lock_file lock = LOCK_INIT;
@@ -453,18 +455,21 @@ static int reset_head(struct object_id *oid, const char *action,
 	strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
 	prefix_len = msg.len;
 
-	if (!get_oid("ORIG_HEAD", &oid_old_orig))
-		old_orig = &oid_old_orig;
-	if (!get_oid("HEAD", &oid_orig)) {
-		orig = &oid_orig;
-		if (!reflog_orig_head) {
-			strbuf_addstr(&msg, "updating ORIG_HEAD");
-			reflog_orig_head = msg.buf;
-		}
-		update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
-			   UPDATE_REFS_MSG_ON_ERR);
-	} else if (old_orig)
-		delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
+	if (update_orig_head) {
+		if (!get_oid("ORIG_HEAD", &oid_old_orig))
+			old_orig = &oid_old_orig;
+		if (!get_oid("HEAD", &oid_orig)) {
+			orig = &oid_orig;
+			if (!reflog_orig_head) {
+				strbuf_addstr(&msg, "updating ORIG_HEAD");
+				reflog_orig_head = msg.buf;
+			}
+			update_ref(reflog_orig_head, "ORIG_HEAD", orig,
+				   old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
+		} else if (old_orig)
+			delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
+	}
+
 	if (!reflog_head) {
 		strbuf_setlen(&msg, prefix_len);
 		strbuf_addstr(&msg, "updating HEAD");
@@ -1725,7 +1730,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
 	strbuf_addf(&msg, "%s: checkout %s",
 		    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
 	if (reset_head(&options.onto->object.oid, "checkout", NULL,
-		       RESET_HEAD_DETACH, NULL, msg.buf))
+		       RESET_HEAD_DETACH | RESET_ORIG_HEAD, NULL, msg.buf))
 		die(_("Could not detach HEAD"));
 	strbuf_release(&msg);
 
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index 7e8d5bb200..460d0523be 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -59,7 +59,7 @@ test_expect_success 'rebase against master' '
 	git rebase master
 '
 
-test_expect_failure 'rebase sets ORIG_HEAD to pre-rebase state' '
+test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
 	git checkout -b orig-head topic &&
 	pre="$(git rev-parse --verify HEAD)" &&
 	git rebase master &&
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/4] built-in rebase: no need to check out `onto` twice
  2019-03-03  1:35         ` Junio C Hamano
  2019-03-03 17:09           ` Johannes Schindelin
@ 2019-03-03 19:11           ` Phillip Wood
  1 sibling, 0 replies; 16+ messages in thread
From: Phillip Wood @ 2019-03-03 19:11 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Johannes Schindelin, phillip.wood,
	Johannes Schindelin via GitGitGadget, git, Nazri Ramliy

Hi Junio

On 03/03/2019 01:35, Junio C Hamano wrote:
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>> Thanks for explaining, it all makes sense to me now
> 
> It would be necessary to make sure that it all makes sense to all
> future readers.  Are they patches good enough as-is for that, or do
> they need some updates before I take a look at them to pick up?
> 

I've just re-read them and I think they're fine as is, though the first
 paragraph of the first commit message might be clearer if "HEAD was an
ancestor and" was changed to "HEAD was an ancestor of onto and".

Best Wishes

Phillip


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2019-03-03 19:11 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-28 15:27 [PATCH 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
2019-02-28 15:27 ` [PATCH 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
2019-02-28 20:55   ` Phillip Wood
2019-03-01 13:19     ` Johannes Schindelin
2019-03-01 15:00       ` Phillip Wood
2019-03-03  1:35         ` Junio C Hamano
2019-03-03 17:09           ` Johannes Schindelin
2019-03-03 19:11           ` Phillip Wood
2019-02-28 15:27 ` [PATCH 2/4] built-in rebase: use the correct reflog when switching branches Johannes Schindelin via GitGitGadget
2019-02-28 15:27 ` [PATCH 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly Johannes Schindelin via GitGitGadget
2019-02-28 15:27 ` [PATCH 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase Johannes Schindelin via GitGitGadget
2019-03-03 17:11 ` [PATCH v2 0/4] Fix ORIG_HEAD behavior of the built-in rebase Johannes Schindelin via GitGitGadget
2019-03-03 17:11   ` [PATCH v2 1/4] built-in rebase: no need to check out `onto` twice Johannes Schindelin via GitGitGadget
2019-03-03 17:11   ` [PATCH v2 3/4] built-in rebase: demonstrate that ORIG_HEAD is not set correctly Johannes Schindelin via GitGitGadget
2019-03-03 17:11   ` [PATCH v2 2/4] built-in rebase: use the correct reflog when switching branches Johannes Schindelin via GitGitGadget
2019-03-03 17:11   ` [PATCH v2 4/4] built-in rebase: set ORIG_HEAD just once, before the rebase Johannes Schindelin via GitGitGadget

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).