git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/6] update-ref: add support for 'symref-verify' command

From
Karthik Nayak <karthik.188@gmail.com>
Date
May 17, 2024, 16:21 UTC
Message-ID
<CAOLa=ZSUV1X==x2CBivgu=L7SQryXNZZkLwxgyNth=a+bH9SQg@mail.gmail.com>
In-Reply-To
<ZkXpYep2MKdsyNyV@tanuki>
Patrick Steinhardt <ps@pks.im> writes:
Show 26 quoted lines
> On Tue, May 14, 2024 at 02:44:07PM +0200, Karthik Nayak wrote:
>> From: Karthik Nayak <karthik.188@gmail.com>
>>
>> The 'symref-verify' command allows users to verify if a provided <ref>
>> contains the provided <old-target> without changing the <ref>. If
>> <old-target> is not provided, the command will verify that the <ref>
>> doesn't exist.
>>
>> The command allows users to verify symbolic refs within a transaction,
>> and this means users can perform a set of changes in a transaction only
>> when the verification holds good.
>>
>> Since we're checking for symbolic refs, this command will only work with
>> the 'no-deref' mode. This is because any dereferenced symbolic ref will
>> point to an object and not a ref and the regular 'verify' command can be
>> used in such situations.
>>
>> Add required tests for symref support in 'verify' while also adding
>> reflog checks for the pre-existing 'verify' tests.
>
> I'm a bit surprised that you add reflog-related tests, and you don't
> really explain why you do it. Do we change any behaviour relating to
> reflogs here? If there is a particular reason that is independent of the
> new "symref-verify" command, then I'd expect this to be part of a
> separate commit.
>

Ah! There is no divergence in behavior, rather this is behavior which is never captured in tests. So I thought it makes to have tests around it.

Show 22 quoted lines
> [snip]
>> diff --git a/refs.c b/refs.c
>> index 59858fafdb..ee4c6ed99c 100644
>> --- a/refs.c
>> +++ b/refs.c
>> @@ -1331,14 +1331,17 @@ int ref_transaction_delete(struct ref_transaction *transaction,
>>  int ref_transaction_verify(struct ref_transaction *transaction,
>>  			   const char *refname,
>>  			   const struct object_id *old_oid,
>> +			   const char *old_target,
>>  			   unsigned int flags,
>>  			   struct strbuf *err)
>>  {
>> -	if (!old_oid)
>> -		BUG("verify called with old_oid set to NULL");
>> +	if (!old_target && !old_oid)
>> +		BUG("verify called with old_oid and old_target set to NULL");
>> +	if (old_target && !(flags & REF_NO_DEREF))
>> +		BUG("verify cannot operate on symrefs with deref mode");
>
> Should we also BUG on `old_target && old_oid`?
>

I didn't do this, because `ref_transaction_add_update` downstream from this already does that. But I guess no harm in adding it here too.

Show 18 quoted lines
>> @@ -1641,4 +1647,88 @@ test_expect_success PIPE 'transaction flushes status updates' '
>>  	test_cmp expected actual
>>  '
>>
>> +create_stdin_buf () {
>> +	if test "$1" = "-z"
>> +	then
>> +		shift
>> +		printf "$F" "$@" >stdin
>> +	else
>> +		echo "$@" >stdin
>> +	fi
>> +}
>
> I think this would be easier to use if you didn't handle the redirect to
> "stdin" over here, but at the calling site. Otherwise, the caller needs
> to be aware of the inner workings.
>

Not sure what you mean by easier here, but I think it would be nicer to read, since the client would now determine the destination of the formatting and this would align with what the test needs to do. Will change!

Show 7 quoted lines
>> +for type in "" "-z"
>> +do
>> +
>> +	test_expect_success "stdin ${type} symref-verify fails without --no-deref" '
>
> We typically avoid curly braces unless required.
>
Will change, thanks!
Show 45 quoted lines
> [snip]
>> diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh
>> index 067fd57290..fd58b902f4 100755
>> --- a/t/t1416-ref-transaction-hooks.sh
>> +++ b/t/t1416-ref-transaction-hooks.sh
>> @@ -157,4 +157,34 @@ test_expect_success 'hook captures git-symbolic-ref updates' '
>>  	test_cmp expect actual
>>  '
>>
>> +test_expect_success 'hook gets all queued symref updates' '
>> +	test_when_finished "rm actual" &&
>> +
>> +	git update-ref refs/heads/branch $POST_OID &&
>> +	git symbolic-ref refs/heads/symref refs/heads/main &&
>> +
>> +	test_hook reference-transaction <<-\EOF &&
>> +	echo "$*" >>actual
>> +	while read -r line
>> +	do
>> +		printf "%s\n" "$line"
>> +	done >>actual
>> +	EOF
>> +
>> +	cat >expect <<-EOF &&
>> +	prepared
>> +	ref:refs/heads/main $ZERO_OID refs/heads/symref
>> +	committed
>> +	ref:refs/heads/main $ZERO_OID refs/heads/symref
>> +	EOF
>> +
>> +	git update-ref --no-deref --stdin <<-EOF &&
>> +	start
>> +	symref-verify refs/heads/symref refs/heads/main
>> +	prepare
>> +	commit
>> +	EOF
>> +	test_cmp expect actual
>> +'
>> +
>>  test_done
>
> So the reference-transaction hook executes even for "symref-verify"?
> This feels quite unexpected to me. Do we do the same for "verify"?
>
> Patrick

Yes this is the same for verify as well. I was surprised to find this too. It's just the way ref update code is written, all updates land trigger the hook. This means verify, which is also a form of update, with just the new value not set, also triggers the hook. I've kept the same behavior with symref-verify.

Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 7 of 50 in “update-ref: add symref support for --stdin”
  1. 0/6 update-ref: add symref support for --stdinKarthik Nayak, May 14, 2024
  2. 1/6 refs: create and use `ref_update_ref_must_exist()`Karthik Nayak, May 14, 2024
  3. Patrick SteinhardtMay 16, 2024
  4. Karthik NayakMay 17, 2024
  5. 2/6 update-ref: add support for 'symref-verify' commandKarthik Nayak, May 14, 2024
  6. Patrick SteinhardtMay 16, 2024
  7. Karthik NayakMay 17, 2024
  8. Patrick SteinhardtMay 21, 2024
  9. 3/6 update-ref: add support for 'symref-delete' commandKarthik Nayak, May 14, 2024
  10. Patrick SteinhardtMay 16, 2024
  11. 4/6 update-ref: add support for 'symref-create' commandKarthik Nayak, May 14, 2024
  12. Patrick SteinhardtMay 16, 2024
  13. Karthik NayakMay 19, 2024
  14. 5/6 reftable: pick either 'oid' or 'target' for new updatesKarthik Nayak, May 14, 2024
  15. 6/6 update-ref: add support for 'symref-update' commandKarthik Nayak, May 14, 2024
  16. Patrick SteinhardtMay 16, 2024
  17. Karthik NayakMay 21, 2024
  18. Karthik NayakMay 22, 2024
  19. 0/6 update-ref: add symref support for --stdinKarthik Nayak, May 22, 2024
  20. 1/6 refs: create and use `ref_update_expects_existing_old_ref()`Karthik Nayak, May 22, 2024
  21. 2/6 update-ref: add support for 'symref-verify' commandKarthik Nayak, May 22, 2024
  22. 3/6 update-ref: add support for 'symref-delete' commandKarthik Nayak, May 22, 2024
  23. 4/6 update-ref: add support for 'symref-create' commandKarthik Nayak, May 22, 2024
  24. 5/6 reftable: pick either 'oid' or 'target' for new updatesKarthik Nayak, May 22, 2024
  25. 6/6 update-ref: add support for 'symref-update' commandKarthik Nayak, May 22, 2024
  26. Junio C HamanoMay 25, 2024
  27. Karthik NayakMay 29, 2024
  28. Junio C HamanoMay 23, 2024
  29. Karthik NayakMay 23, 2024
  30. Junio C HamanoMay 23, 2024
  31. Karthik NayakMay 23, 2024
  32. Eric SunshineMay 23, 2024
  33. Junio C HamanoMay 23, 2024
  34. Eric SunshineMay 23, 2024
  35. Junio C HamanoMay 23, 2024
  36. Junio C HamanoMay 23, 2024
  37. Junio C HamanoMay 23, 2024
  38. 0/6 update-ref: add symref support for --stdinKarthik Nayak, May 30, 2024
  39. Patrick SteinhardtJun 5, 2024
  40. 1/6 refs: create and use `ref_update_expects_existing_old_ref()`Karthik Nayak, May 30, 2024
  41. 2/6 update-ref: add support for 'symref-verify' commandKarthik Nayak, May 30, 2024
  42. 3/6 update-ref: add support for 'symref-delete' commandKarthik Nayak, May 30, 2024
  43. Patrick SteinhardtJun 5, 2024
  44. Karthik NayakJun 5, 2024
  45. Junio C HamanoJun 5, 2024
  46. 4/6 update-ref: add support for 'symref-create' commandKarthik Nayak, May 30, 2024
  47. Patrick SteinhardtJun 5, 2024
  48. 5/6 reftable: pick either 'oid' or 'target' for new updatesKarthik Nayak, May 30, 2024
  49. 6/6 update-ref: add support for 'symref-update' commandKarthik Nayak, May 30, 2024
  50. Patrick SteinhardtJun 5, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.