diff mbox series

[1/2] sequencer: unify label lookup

Message ID ecb81b873a8d72c57fd4812ec86d103927437cf1.1668098622.git.gitgitgadget@gmail.com (mailing list archive)
State Accepted
Commit 82766b29611aea7c3fb42a99debfba1436852d38
Headers show
Series rebase: stricter label lookups | expand

Commit Message

Phillip Wood Nov. 10, 2022, 4:43 p.m. UTC
From: Phillip Wood <phillip.wood@dunelm.org.uk>

The arguments to the `reset` and `merge` commands may be a label created
with a `label` command or an arbitrary commit name. The `merge` command
uses the lookup_label() function to lookup its arguments but `reset` has
a slightly different version of that function in do_reset(). Reduce this
code duplication by calling lookup_label() from do_reset() as well.

This change improves the behavior of `reset` when the argument is a
tree.  Previously `reset` would accept a tree only for the rebase to
fail with

       update_ref failed for ref 'HEAD': cannot update ref 'HEAD': trying to write non-commit object da5497437fd67ca928333aab79c4b4b55036ea66 to branch 'HEAD'

Using lookup_label() means do_reset() will now error out straight away
if its argument is not a commit.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 sequencer.c              | 49 ++++++++++++++++++++--------------------
 t/t3430-rebase-merges.sh |  8 +++++++
 2 files changed, 33 insertions(+), 24 deletions(-)

Comments

Johannes Schindelin Nov. 18, 2022, 1:40 p.m. UTC | #1
Hi Phillip,

On Thu, 10 Nov 2022, Phillip Wood via GitGitGadget wrote:

> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> The arguments to the `reset` and `merge` commands may be a label created
> with a `label` command or an arbitrary commit name. The `merge` command
> uses the lookup_label() function to lookup its arguments but `reset` has
> a slightly different version of that function in do_reset(). Reduce this
> code duplication by calling lookup_label() from do_reset() as well.
>
> This change improves the behavior of `reset` when the argument is a
> tree.  Previously `reset` would accept a tree only for the rebase to
> fail with
>
>        update_ref failed for ref 'HEAD': cannot update ref 'HEAD': trying to write non-commit object da5497437fd67ca928333aab79c4b4b55036ea66 to branch 'HEAD'
>
> Using lookup_label() means do_reset() will now error out straight away
> if its argument is not a commit.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---

Very nice.

From reading the patch, I get the impression that this also addresses
https://github.com/gitgitgadget/git/issues/199, i.e. allowing `reset
<tag>`.

Am I right?
Dscho

P.S.: FWIW here is my ACK for both patches.

>  sequencer.c              | 49 ++++++++++++++++++++--------------------
>  t/t3430-rebase-merges.sh |  8 +++++++
>  2 files changed, 33 insertions(+), 24 deletions(-)
>
> diff --git a/sequencer.c b/sequencer.c
> index e658df7e8ff..21f5032df0d 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -3696,6 +3696,26 @@ static const char *reflog_message(struct replay_opts *opts,
>  	return buf.buf;
>  }
>
> +static struct commit *lookup_label(const char *label, int len,
> +				   struct strbuf *buf)
> +{
> +	struct commit *commit;
> +
> +	strbuf_reset(buf);
> +	strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
> +	commit = lookup_commit_reference_by_name(buf->buf);
> +	if (!commit) {
> +		/* fall back to non-rewritten ref or commit */
> +		strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
> +		commit = lookup_commit_reference_by_name(buf->buf);
> +	}
> +
> +	if (!commit)
> +		error(_("could not resolve '%s'"), buf->buf);
> +
> +	return commit;
> +}
> +
>  static int do_reset(struct repository *r,
>  		    const char *name, int len,
>  		    struct replay_opts *opts)
> @@ -3727,6 +3747,7 @@ static int do_reset(struct repository *r,
>  		oidcpy(&oid, &opts->squash_onto);
>  	} else {
>  		int i;
> +		struct commit *commit;
>
>  		/* Determine the length of the label */
>  		for (i = 0; i < len; i++)
> @@ -3734,12 +3755,12 @@ static int do_reset(struct repository *r,
>  				break;
>  		len = i;
>
> -		strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
> -		if (get_oid(ref_name.buf, &oid) &&
> -		    get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) {
> -			ret = error(_("could not read '%s'"), ref_name.buf);
> +		commit = lookup_label(name, len, &ref_name);
> +		if (!commit) {
> +			ret = -1;
>  			goto cleanup;
>  		}
> +		oid = commit->object.oid;
>  	}
>
>  	setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
> @@ -3786,26 +3807,6 @@ cleanup:
>  	return ret;
>  }
>
> -static struct commit *lookup_label(const char *label, int len,
> -				   struct strbuf *buf)
> -{
> -	struct commit *commit;
> -
> -	strbuf_reset(buf);
> -	strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
> -	commit = lookup_commit_reference_by_name(buf->buf);
> -	if (!commit) {
> -		/* fall back to non-rewritten ref or commit */
> -		strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
> -		commit = lookup_commit_reference_by_name(buf->buf);
> -	}
> -
> -	if (!commit)
> -		error(_("could not resolve '%s'"), buf->buf);
> -
> -	return commit;
> -}
> -
>  static int do_merge(struct repository *r,
>  		    struct commit *commit,
>  		    const char *arg, int arg_len,
> diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
> index f351701fec2..fbbc4439bfe 100755
> --- a/t/t3430-rebase-merges.sh
> +++ b/t/t3430-rebase-merges.sh
> @@ -138,6 +138,14 @@ test_expect_success '`reset` refuses to overwrite untracked files' '
>  	git rebase --abort
>  '
>
> +test_expect_success '`reset` rejects trees' '
> +	test_when_finished "test_might_fail git rebase --abort" &&
> +	test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
> +		git rebase -i B C >out 2>err &&
> +	grep "object .* is a tree" err &&
> +	test_must_be_empty out
> +'
> +
>  test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
>  	test_when_finished "test_might_fail git rebase --abort" &&
>  	git checkout -b conflicting-merge A &&
> --
> gitgitgadget
>
>
Phillip Wood Nov. 21, 2022, 9:49 a.m. UTC | #2
Hi Dscho

On 18/11/2022 13:40, Johannes Schindelin wrote:
> Hi Phillip,
> 
> On Thu, 10 Nov 2022, Phillip Wood via GitGitGadget wrote:
> 
>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>>
>> The arguments to the `reset` and `merge` commands may be a label created
>> with a `label` command or an arbitrary commit name. The `merge` command
>> uses the lookup_label() function to lookup its arguments but `reset` has
>> a slightly different version of that function in do_reset(). Reduce this
>> code duplication by calling lookup_label() from do_reset() as well.
>>
>> This change improves the behavior of `reset` when the argument is a
>> tree.  Previously `reset` would accept a tree only for the rebase to
>> fail with
>>
>>         update_ref failed for ref 'HEAD': cannot update ref 'HEAD': trying to write non-commit object da5497437fd67ca928333aab79c4b4b55036ea66 to branch 'HEAD'
>>
>> Using lookup_label() means do_reset() will now error out straight away
>> if its argument is not a commit.
>>
>> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
>> ---
> 
> Very nice.
> 
>  From reading the patch, I get the impression that this also addresses
> https://github.com/gitgitgadget/git/issues/199, i.e. allowing `reset
> <tag>`.
> 
> Am I right?
> Dscho

I think so, the tag will be dereferenced by lookup_commit_reference()

Best Wishes

Phillip

> P.S.: FWIW here is my ACK for both patches.
> 
>>   sequencer.c              | 49 ++++++++++++++++++++--------------------
>>   t/t3430-rebase-merges.sh |  8 +++++++
>>   2 files changed, 33 insertions(+), 24 deletions(-)
>>
>> diff --git a/sequencer.c b/sequencer.c
>> index e658df7e8ff..21f5032df0d 100644
>> --- a/sequencer.c
>> +++ b/sequencer.c
>> @@ -3696,6 +3696,26 @@ static const char *reflog_message(struct replay_opts *opts,
>>   	return buf.buf;
>>   }
>>
>> +static struct commit *lookup_label(const char *label, int len,
>> +				   struct strbuf *buf)
>> +{
>> +	struct commit *commit;
>> +
>> +	strbuf_reset(buf);
>> +	strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
>> +	commit = lookup_commit_reference_by_name(buf->buf);
>> +	if (!commit) {
>> +		/* fall back to non-rewritten ref or commit */
>> +		strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
>> +		commit = lookup_commit_reference_by_name(buf->buf);
>> +	}
>> +
>> +	if (!commit)
>> +		error(_("could not resolve '%s'"), buf->buf);
>> +
>> +	return commit;
>> +}
>> +
>>   static int do_reset(struct repository *r,
>>   		    const char *name, int len,
>>   		    struct replay_opts *opts)
>> @@ -3727,6 +3747,7 @@ static int do_reset(struct repository *r,
>>   		oidcpy(&oid, &opts->squash_onto);
>>   	} else {
>>   		int i;
>> +		struct commit *commit;
>>
>>   		/* Determine the length of the label */
>>   		for (i = 0; i < len; i++)
>> @@ -3734,12 +3755,12 @@ static int do_reset(struct repository *r,
>>   				break;
>>   		len = i;
>>
>> -		strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
>> -		if (get_oid(ref_name.buf, &oid) &&
>> -		    get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) {
>> -			ret = error(_("could not read '%s'"), ref_name.buf);
>> +		commit = lookup_label(name, len, &ref_name);
>> +		if (!commit) {
>> +			ret = -1;
>>   			goto cleanup;
>>   		}
>> +		oid = commit->object.oid;
>>   	}
>>
>>   	setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
>> @@ -3786,26 +3807,6 @@ cleanup:
>>   	return ret;
>>   }
>>
>> -static struct commit *lookup_label(const char *label, int len,
>> -				   struct strbuf *buf)
>> -{
>> -	struct commit *commit;
>> -
>> -	strbuf_reset(buf);
>> -	strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
>> -	commit = lookup_commit_reference_by_name(buf->buf);
>> -	if (!commit) {
>> -		/* fall back to non-rewritten ref or commit */
>> -		strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
>> -		commit = lookup_commit_reference_by_name(buf->buf);
>> -	}
>> -
>> -	if (!commit)
>> -		error(_("could not resolve '%s'"), buf->buf);
>> -
>> -	return commit;
>> -}
>> -
>>   static int do_merge(struct repository *r,
>>   		    struct commit *commit,
>>   		    const char *arg, int arg_len,
>> diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
>> index f351701fec2..fbbc4439bfe 100755
>> --- a/t/t3430-rebase-merges.sh
>> +++ b/t/t3430-rebase-merges.sh
>> @@ -138,6 +138,14 @@ test_expect_success '`reset` refuses to overwrite untracked files' '
>>   	git rebase --abort
>>   '
>>
>> +test_expect_success '`reset` rejects trees' '
>> +	test_when_finished "test_might_fail git rebase --abort" &&
>> +	test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
>> +		git rebase -i B C >out 2>err &&
>> +	grep "object .* is a tree" err &&
>> +	test_must_be_empty out
>> +'
>> +
>>   test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
>>   	test_when_finished "test_might_fail git rebase --abort" &&
>>   	git checkout -b conflicting-merge A &&
>> --
>> gitgitgadget
>>
>>
diff mbox series

Patch

diff --git a/sequencer.c b/sequencer.c
index e658df7e8ff..21f5032df0d 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -3696,6 +3696,26 @@  static const char *reflog_message(struct replay_opts *opts,
 	return buf.buf;
 }
 
+static struct commit *lookup_label(const char *label, int len,
+				   struct strbuf *buf)
+{
+	struct commit *commit;
+
+	strbuf_reset(buf);
+	strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
+	commit = lookup_commit_reference_by_name(buf->buf);
+	if (!commit) {
+		/* fall back to non-rewritten ref or commit */
+		strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
+		commit = lookup_commit_reference_by_name(buf->buf);
+	}
+
+	if (!commit)
+		error(_("could not resolve '%s'"), buf->buf);
+
+	return commit;
+}
+
 static int do_reset(struct repository *r,
 		    const char *name, int len,
 		    struct replay_opts *opts)
@@ -3727,6 +3747,7 @@  static int do_reset(struct repository *r,
 		oidcpy(&oid, &opts->squash_onto);
 	} else {
 		int i;
+		struct commit *commit;
 
 		/* Determine the length of the label */
 		for (i = 0; i < len; i++)
@@ -3734,12 +3755,12 @@  static int do_reset(struct repository *r,
 				break;
 		len = i;
 
-		strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
-		if (get_oid(ref_name.buf, &oid) &&
-		    get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) {
-			ret = error(_("could not read '%s'"), ref_name.buf);
+		commit = lookup_label(name, len, &ref_name);
+		if (!commit) {
+			ret = -1;
 			goto cleanup;
 		}
+		oid = commit->object.oid;
 	}
 
 	setup_unpack_trees_porcelain(&unpack_tree_opts, "reset");
@@ -3786,26 +3807,6 @@  cleanup:
 	return ret;
 }
 
-static struct commit *lookup_label(const char *label, int len,
-				   struct strbuf *buf)
-{
-	struct commit *commit;
-
-	strbuf_reset(buf);
-	strbuf_addf(buf, "refs/rewritten/%.*s", len, label);
-	commit = lookup_commit_reference_by_name(buf->buf);
-	if (!commit) {
-		/* fall back to non-rewritten ref or commit */
-		strbuf_splice(buf, 0, strlen("refs/rewritten/"), "", 0);
-		commit = lookup_commit_reference_by_name(buf->buf);
-	}
-
-	if (!commit)
-		error(_("could not resolve '%s'"), buf->buf);
-
-	return commit;
-}
-
 static int do_merge(struct repository *r,
 		    struct commit *commit,
 		    const char *arg, int arg_len,
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
index f351701fec2..fbbc4439bfe 100755
--- a/t/t3430-rebase-merges.sh
+++ b/t/t3430-rebase-merges.sh
@@ -138,6 +138,14 @@  test_expect_success '`reset` refuses to overwrite untracked files' '
 	git rebase --abort
 '
 
+test_expect_success '`reset` rejects trees' '
+	test_when_finished "test_might_fail git rebase --abort" &&
+	test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
+		git rebase -i B C >out 2>err &&
+	grep "object .* is a tree" err &&
+	test_must_be_empty out
+'
+
 test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
 	test_when_finished "test_might_fail git rebase --abort" &&
 	git checkout -b conflicting-merge A &&