git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/2] [RFC] push: allow delete one level ref
@ 2023-01-17 10:32 ZheNing Hu via GitGitGadget
  2023-01-17 10:32 ` [PATCH 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-01-17 10:32 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu

This might be an odd request: allow git push to delete one level refs like
"ref/foo".

Some users on my side often push "refs/for/master" to the remote for code
review, but due to a user's typo, "refs/master" is pushed to the remote.

Pushing a one level ref like "refs/foo" should not have been successful, but
since my server side directly updated the ref during the proc-receive-hook
phase of git receive-pack, "refs/foo" was mistakenly left at on the server.

But for some reasons, users cannot delete this special branch through "git
push -d".

First, I executed git update-ref --stdin inside my proc-receive-hook to
delete the branch. As a result, update-ref reported an error: "cannot lock
ref 'refs/foo': reference already exists".

So I tried GIT_TRACKET_PACKET to debug and found that git push did not seem
to pass the correct ref old-oid, which is why update-ref reported an error.

"18:13:41.214872 pkt-line.c:80           packet: receive-pack< 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/foo\0 report-status-v2 side-band-64k object-format=sha1 agent=git/2.39.0.227.g262c45b6a1"


Tracing it back, the check_ref() in the git push link didn't record the
old-oid for the remote "refs/foo".

At the same time, the server update() process will reject the one level ref
by default and return a "funny refname" error.

It is worth mentioning that since I deleted the branch, the error message
returned here is "refusing to create funny ref 'refs/foo' remotely", which
is also worth fixing.

So this patch series do:

v1.

 1. fix funny refname error message from "create" to "update".
 2. let server allow delete one level ref.
 3. let client pass correct one level ref old-oid.

ZheNing Hu (2):
  receive-pack: fix funny ref error messsage
  [RFC] push: allow delete one level ref

 builtin/receive-pack.c |  7 +++++--
 connect.c              |  2 +-
 t/t5516-fetch-push.sh  | 18 ++++++++++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)


base-commit: a38d39a4c50d1275833aba54c4dbdfce9e2e9ca1
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1465%2Fadlternative%2Fzh%2Fdelete-one-level-ref-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1465/adlternative/zh/delete-one-level-ref-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1465
-- 
gitgitgadget

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

* [PATCH 1/2] receive-pack: fix funny ref error messsage
  2023-01-17 10:32 [PATCH 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
@ 2023-01-17 10:32 ` ZheNing Hu via GitGitGadget
  2023-01-17 10:32 ` [PATCH 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2023-02-04 16:48 ` [PATCH v2 0/2] " ZheNing Hu via GitGitGadget
  2 siblings, 0 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-01-17 10:32 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

When the user deletes the remote one level branch through
"git push origin -d refs/foo", remote will return an error:
"refusing to create funny ref 'refs/foo' remotely", here we
are not creating "refs/foo" instead wants to delete it, so a
better error description here would be: "refusing to update
funny ref 'refs/foo' remotely".

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c | 2 +-
 t/t5516-fetch-push.sh  | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index a90af303630..13ff9fae3ba 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1464,7 +1464,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
-		rp_error("refusing to create funny ref '%s' remotely", name);
+		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
 	}
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 98a27a2948b..f37861efc40 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -401,6 +401,11 @@ test_expect_success 'push with ambiguity' '
 
 '
 
+test_expect_success 'push with onelevel ref' '
+	mk_test testrepo heads/main &&
+	test_must_fail git push testrepo HEAD:refs/onelevel
+'
+
 test_expect_success 'push with colon-less refspec (1)' '
 
 	mk_test testrepo heads/frotz tags/frotz &&
-- 
gitgitgadget


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

* [PATCH 2/2] [RFC] push: allow delete one level ref
  2023-01-17 10:32 [PATCH 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2023-01-17 10:32 ` [PATCH 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
@ 2023-01-17 10:32 ` ZheNing Hu via GitGitGadget
  2023-01-17 13:14   ` Ævar Arnfjörð Bjarmason
  2023-02-04 16:48 ` [PATCH v2 0/2] " ZheNing Hu via GitGitGadget
  2 siblings, 1 reply; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-01-17 10:32 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

Git will reject the deletion of one level refs e,g. "refs/foo"
through "git push -d", however, some users want to be able to
clean up these branches that were created unexpectedly on the
remote.

Therefore, when updating branches on the server with
"git receive-pack", by checking whether it is a branch deletion
operation, it will determine whether to allow the update of
one level refs. This avoids creating/updating such one level
branches, but allows them to be deleted.

On the client side, "git push" also does not properly fill in
the old-oid of one level refs, which causes the server-side
"git receive-pack" to think that the ref's old-oid has changed
when deleting one level refs, this causes the push to be rejected.

So the solution is to fix the client to be able to delete
one level refs by properly filling old-oid.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c |  5 ++++-
 connect.c              |  2 +-
 t/t5516-fetch-push.sh  | 13 +++++++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 13ff9fae3ba..ad21877ea1b 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1463,7 +1463,10 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 		find_shared_symref(worktrees, "HEAD", name);
 
 	/* only refs/... are allowed */
-	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
+	if (!starts_with(name, "refs/") ||
+	    check_refname_format(name + 5,
+				 is_null_oid(new_oid) ?
+				 REFNAME_ALLOW_ONELEVEL : 0)) {
 		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
diff --git a/connect.c b/connect.c
index 63e59641c0d..b841ae58e03 100644
--- a/connect.c
+++ b/connect.c
@@ -30,7 +30,7 @@ static int check_ref(const char *name, unsigned int flags)
 		return 0;
 
 	/* REF_NORMAL means that we don't want the magic fake tag refs */
-	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
+	if ((flags & REF_NORMAL) && check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
 		return 0;
 
 	/* REF_HEADS means that we want regular branch heads */
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index f37861efc40..dec8950a392 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -903,6 +903,19 @@ test_expect_success 'push --delete refuses empty string' '
 	test_must_fail git push testrepo --delete ""
 '
 
+test_expect_success 'push --delete onelevel refspecs' '
+	mk_test testrepo heads/main &&
+	(
+		cd testrepo &&
+		git update-ref refs/onelevel refs/heads/main
+	) &&
+	git push testrepo --delete refs/onelevel &&
+	(
+		cd testrepo &&
+		test_must_fail git rev-parse --verify refs/onelevel
+	)
+'
+
 test_expect_success 'warn on push to HEAD of non-bare repository' '
 	mk_test testrepo heads/main &&
 	(
-- 
gitgitgadget

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

* Re: [PATCH 2/2] [RFC] push: allow delete one level ref
  2023-01-17 10:32 ` [PATCH 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
@ 2023-01-17 13:14   ` Ævar Arnfjörð Bjarmason
  2023-01-19 17:39     ` ZheNing Hu
  0 siblings, 1 reply; 20+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2023-01-17 13:14 UTC (permalink / raw)
  To: ZheNing Hu via GitGitGadget
  Cc: git, Junio C Hamano, Derrick Stolee, Elijah Newren,
	Michael Haggerty, Christian Couder, Jeff King, ZheNing Hu


On Tue, Jan 17 2023, ZheNing Hu via GitGitGadget wrote:

> From: ZheNing Hu <adlternative@gmail.com>
>
> Git will reject the deletion of one level refs e,g. "refs/foo"
> through "git push -d", however, some users want to be able to
> clean up these branches that were created unexpectedly on the
> remote.
>
> Therefore, when updating branches on the server with
> "git receive-pack", by checking whether it is a branch deletion
> operation, it will determine whether to allow the update of
> one level refs. This avoids creating/updating such one level
> branches, but allows them to be deleted.
>
> On the client side, "git push" also does not properly fill in
> the old-oid of one level refs, which causes the server-side
> "git receive-pack" to think that the ref's old-oid has changed
> when deleting one level refs, this causes the push to be rejected.
>
> So the solution is to fix the client to be able to delete
> one level refs by properly filling old-oid.
>
> Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> ---
>  builtin/receive-pack.c |  5 ++++-
>  connect.c              |  2 +-
>  t/t5516-fetch-push.sh  | 13 +++++++++++++
>  3 files changed, 18 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> index 13ff9fae3ba..ad21877ea1b 100644
> --- a/builtin/receive-pack.c
> +++ b/builtin/receive-pack.c
> @@ -1463,7 +1463,10 @@ static const char *update(struct command *cmd, struct shallow_info *si)
>  		find_shared_symref(worktrees, "HEAD", name);
>  
>  	/* only refs/... are allowed */
> -	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
> +	if (!starts_with(name, "refs/") ||
> +	    check_refname_format(name + 5,
> +				 is_null_oid(new_oid) ?
> +				 REFNAME_ALLOW_ONELEVEL : 0)) {

Style nit: We tend to wrap at 79 characters, adn with argument lists you
"keep going" until you hit that limit.

In this case strictly following that rule will lead to funny
indentation, as we'll have to wrap at "is_null_oid(...)" etc.

But even when avoiding that (which seems good in this case) this should
be:

	if (!starts_with(name, "refs/") ||
	    check_refname_format(name + 5, is_null_oid(new_oid) ?
				 REFNAME_ALLOW_ONELEVEL : 0)) {



>  		rp_error("refusing to update funny ref '%s' remotely", name);
>  		ret = "funny refname";
>  		goto out;
> diff --git a/connect.c b/connect.c
> index 63e59641c0d..b841ae58e03 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -30,7 +30,7 @@ static int check_ref(const char *name, unsigned int flags)
>  		return 0;
>  
>  	/* REF_NORMAL means that we don't want the magic fake tag refs */
> -	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
> +	if ((flags & REF_NORMAL) && check_refname_format(name, REFNAME_ALLOW_ONELEVEL))

Here we should wrap after "name,", we end up with a too-long line.

>  		return 0;
>  
>  	/* REF_HEADS means that we want regular branch heads */
> diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
> index f37861efc40..dec8950a392 100755
> --- a/t/t5516-fetch-push.sh
> +++ b/t/t5516-fetch-push.sh
> @@ -903,6 +903,19 @@ test_expect_success 'push --delete refuses empty string' '
>  	test_must_fail git push testrepo --delete ""
>  '
>  
> +test_expect_success 'push --delete onelevel refspecs' '
> +	mk_test testrepo heads/main &&
> +	(
> +		cd testrepo &&
> +		git update-ref refs/onelevel refs/heads/main
> +	) &&

Avoid the subshell here with:

	git -C update-ref ....

> +	git push testrepo --delete refs/onelevel &&
> +	(
> +		cd testrepo &&
> +		test_must_fail git rev-parse --verify refs/onelevel

Ditto.

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

* Re: [PATCH 2/2] [RFC] push: allow delete one level ref
  2023-01-17 13:14   ` Ævar Arnfjörð Bjarmason
@ 2023-01-19 17:39     ` ZheNing Hu
  0 siblings, 0 replies; 20+ messages in thread
From: ZheNing Hu @ 2023-01-19 17:39 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: ZheNing Hu via GitGitGadget, git, Junio C Hamano, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King

Ævar Arnfjörð Bjarmason <avarab@gmail.com> 于2023年1月17日周二 21:18写道:
>
>
> On Tue, Jan 17 2023, ZheNing Hu via GitGitGadget wrote:
>
> > From: ZheNing Hu <adlternative@gmail.com>
> >
> > Git will reject the deletion of one level refs e,g. "refs/foo"
> > through "git push -d", however, some users want to be able to
> > clean up these branches that were created unexpectedly on the
> > remote.
> >
> > Therefore, when updating branches on the server with
> > "git receive-pack", by checking whether it is a branch deletion
> > operation, it will determine whether to allow the update of
> > one level refs. This avoids creating/updating such one level
> > branches, but allows them to be deleted.
> >
> > On the client side, "git push" also does not properly fill in
> > the old-oid of one level refs, which causes the server-side
> > "git receive-pack" to think that the ref's old-oid has changed
> > when deleting one level refs, this causes the push to be rejected.
> >
> > So the solution is to fix the client to be able to delete
> > one level refs by properly filling old-oid.
> >
> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> > ---
> >  builtin/receive-pack.c |  5 ++++-
> >  connect.c              |  2 +-
> >  t/t5516-fetch-push.sh  | 13 +++++++++++++
> >  3 files changed, 18 insertions(+), 2 deletions(-)
> >
> > diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> > index 13ff9fae3ba..ad21877ea1b 100644
> > --- a/builtin/receive-pack.c
> > +++ b/builtin/receive-pack.c
> > @@ -1463,7 +1463,10 @@ static const char *update(struct command *cmd, struct shallow_info *si)
> >               find_shared_symref(worktrees, "HEAD", name);
> >
> >       /* only refs/... are allowed */
> > -     if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
> > +     if (!starts_with(name, "refs/") ||
> > +         check_refname_format(name + 5,
> > +                              is_null_oid(new_oid) ?
> > +                              REFNAME_ALLOW_ONELEVEL : 0)) {
>
> Style nit: We tend to wrap at 79 characters, adn with argument lists you
> "keep going" until you hit that limit.
>
> In this case strictly following that rule will lead to funny
> indentation, as we'll have to wrap at "is_null_oid(...)" etc.
>
> But even when avoiding that (which seems good in this case) this should
> be:
>
>         if (!starts_with(name, "refs/") ||
>             check_refname_format(name + 5, is_null_oid(new_oid) ?
>                                  REFNAME_ALLOW_ONELEVEL : 0)) {
>
>

Make sense, I'm going to fix the formatting here.

>
> >               rp_error("refusing to update funny ref '%s' remotely", name);
> >               ret = "funny refname";
> >               goto out;
> > diff --git a/connect.c b/connect.c
> > index 63e59641c0d..b841ae58e03 100644
> > --- a/connect.c
> > +++ b/connect.c
> > @@ -30,7 +30,7 @@ static int check_ref(const char *name, unsigned int flags)
> >               return 0;
> >
> >       /* REF_NORMAL means that we don't want the magic fake tag refs */
> > -     if ((flags & REF_NORMAL) && check_refname_format(name, 0))
> > +     if ((flags & REF_NORMAL) && check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
>
> Here we should wrap after "name,", we end up with a too-long line.
>

To be honest, sometimes it's hard to notice if the current line is longer
than 79 characters, it's often a matter of intuition. Is there any ci script
that can detect this kind of problem?

> >               return 0;
> >
> >       /* REF_HEADS means that we want regular branch heads */
> > diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
> > index f37861efc40..dec8950a392 100755
> > --- a/t/t5516-fetch-push.sh
> > +++ b/t/t5516-fetch-push.sh
> > @@ -903,6 +903,19 @@ test_expect_success 'push --delete refuses empty string' '
> >       test_must_fail git push testrepo --delete ""
> >  '
> >
> > +test_expect_success 'push --delete onelevel refspecs' '
> > +     mk_test testrepo heads/main &&
> > +     (
> > +             cd testrepo &&
> > +             git update-ref refs/onelevel refs/heads/main
> > +     ) &&
>
> Avoid the subshell here with:
>
>         git -C update-ref ....
>

OK.

> > +     git push testrepo --delete refs/onelevel &&
> > +     (
> > +             cd testrepo &&
> > +             test_must_fail git rev-parse --verify refs/onelevel
>
> Ditto.


Thanks,
ZheNing Hu

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

* [PATCH v2 0/2] [RFC] push: allow delete one level ref
  2023-01-17 10:32 [PATCH 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2023-01-17 10:32 ` [PATCH 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
  2023-01-17 10:32 ` [PATCH 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
@ 2023-02-04 16:48 ` ZheNing Hu via GitGitGadget
  2023-02-04 16:48   ` [PATCH v2 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
                     ` (2 more replies)
  2 siblings, 3 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-02-04 16:48 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu

This might be an odd request: allow git push to delete one level refs like
"ref/foo".

Some users on my side often push "refs/for/master" to the remote for code
review, but due to a user's typo, "refs/master" is pushed to the remote.

Pushing a one level ref like "refs/foo" should not have been successful, but
since my server side directly updated the ref during the proc-receive-hook
phase of git receive-pack, "refs/foo" was mistakenly left at on the server.

But for some reasons, users cannot delete this special branch through "git
push -d".

First, I executed git update-ref --stdin inside my proc-receive-hook to
delete the branch. As a result, update-ref reported an error: "cannot lock
ref 'refs/foo': reference already exists".

So I tried GIT_TRACKET_PACKET to debug and found that git push did not seem
to pass the correct ref old-oid, which is why update-ref reported an error.

"18:13:41.214872 pkt-line.c:80           packet: receive-pack< 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/foo\0 report-status-v2 side-band-64k object-format=sha1 agent=git/2.39.0.227.g262c45b6a1"


Tracing it back, the check_ref() in the git push link didn't record the
old-oid for the remote "refs/foo".

At the same time, the server update() process will reject the one level ref
by default and return a "funny refname" error.

It is worth mentioning that since I deleted the branch, the error message
returned here is "refusing to create funny ref 'refs/foo' remotely", which
is also worth fixing.

So this patch series do:

v1.

 1. fix funny refname error message from "create" to "update".
 2. let server allow delete one level ref.
 3. let client pass correct one level ref old-oid.

v2.

 1. fix code style.

ZheNing Hu (2):
  receive-pack: fix funny ref error messsage
  [RFC] push: allow delete one level ref

 builtin/receive-pack.c |  6 ++++--
 connect.c              |  3 ++-
 t/t5516-fetch-push.sh  | 12 ++++++++++++
 3 files changed, 18 insertions(+), 3 deletions(-)


base-commit: a38d39a4c50d1275833aba54c4dbdfce9e2e9ca1
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1465%2Fadlternative%2Fzh%2Fdelete-one-level-ref-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1465/adlternative/zh/delete-one-level-ref-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1465

Range-diff vs v1:

 1:  214a2b662e3 = 1:  214a2b662e3 receive-pack: fix funny ref error messsage
 2:  605b95bf8ab ! 2:  966cb49c388 [RFC] push: allow delete one level ref
     @@ builtin/receive-pack.c: static const char *update(struct command *cmd, struct sh
       	/* only refs/... are allowed */
      -	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
      +	if (!starts_with(name, "refs/") ||
     -+	    check_refname_format(name + 5,
     -+				 is_null_oid(new_oid) ?
     ++	    check_refname_format(name + 5, is_null_oid(new_oid) ?
      +				 REFNAME_ALLOW_ONELEVEL : 0)) {
       		rp_error("refusing to update funny ref '%s' remotely", name);
       		ret = "funny refname";
     @@ connect.c: static int check_ref(const char *name, unsigned int flags)
       
       	/* REF_NORMAL means that we don't want the magic fake tag refs */
      -	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
     -+	if ((flags & REF_NORMAL) && check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
     ++	if ((flags & REF_NORMAL) && check_refname_format(name,
     ++							 REFNAME_ALLOW_ONELEVEL))
       		return 0;
       
       	/* REF_HEADS means that we want regular branch heads */
     @@ t/t5516-fetch-push.sh: test_expect_success 'push --delete refuses empty string'
       
      +test_expect_success 'push --delete onelevel refspecs' '
      +	mk_test testrepo heads/main &&
     -+	(
     -+		cd testrepo &&
     -+		git update-ref refs/onelevel refs/heads/main
     -+	) &&
     ++	git -C testrepo update-ref refs/onelevel refs/heads/main &&
      +	git push testrepo --delete refs/onelevel &&
     -+	(
     -+		cd testrepo &&
     -+		test_must_fail git rev-parse --verify refs/onelevel
     -+	)
     ++	test_must_fail git -C testrepo rev-parse --verify refs/onelevel
      +'
      +
       test_expect_success 'warn on push to HEAD of non-bare repository' '

-- 
gitgitgadget

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

* [PATCH v2 1/2] receive-pack: fix funny ref error messsage
  2023-02-04 16:48 ` [PATCH v2 0/2] " ZheNing Hu via GitGitGadget
@ 2023-02-04 16:48   ` ZheNing Hu via GitGitGadget
  2023-02-04 16:48   ` [PATCH v2 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2023-02-27  1:57   ` [PATCH v3 0/2] " ZheNing Hu via GitGitGadget
  2 siblings, 0 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-02-04 16:48 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

When the user deletes the remote one level branch through
"git push origin -d refs/foo", remote will return an error:
"refusing to create funny ref 'refs/foo' remotely", here we
are not creating "refs/foo" instead wants to delete it, so a
better error description here would be: "refusing to update
funny ref 'refs/foo' remotely".

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c | 2 +-
 t/t5516-fetch-push.sh  | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index a90af303630..13ff9fae3ba 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1464,7 +1464,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
-		rp_error("refusing to create funny ref '%s' remotely", name);
+		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
 	}
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 98a27a2948b..f37861efc40 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -401,6 +401,11 @@ test_expect_success 'push with ambiguity' '
 
 '
 
+test_expect_success 'push with onelevel ref' '
+	mk_test testrepo heads/main &&
+	test_must_fail git push testrepo HEAD:refs/onelevel
+'
+
 test_expect_success 'push with colon-less refspec (1)' '
 
 	mk_test testrepo heads/frotz tags/frotz &&
-- 
gitgitgadget


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

* [PATCH v2 2/2] [RFC] push: allow delete one level ref
  2023-02-04 16:48 ` [PATCH v2 0/2] " ZheNing Hu via GitGitGadget
  2023-02-04 16:48   ` [PATCH v2 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
@ 2023-02-04 16:48   ` ZheNing Hu via GitGitGadget
  2023-02-06 22:13     ` Junio C Hamano
  2023-02-27  1:57   ` [PATCH v3 0/2] " ZheNing Hu via GitGitGadget
  2 siblings, 1 reply; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-02-04 16:48 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

Git will reject the deletion of one level refs e,g. "refs/foo"
through "git push -d", however, some users want to be able to
clean up these branches that were created unexpectedly on the
remote.

Therefore, when updating branches on the server with
"git receive-pack", by checking whether it is a branch deletion
operation, it will determine whether to allow the update of
one level refs. This avoids creating/updating such one level
branches, but allows them to be deleted.

On the client side, "git push" also does not properly fill in
the old-oid of one level refs, which causes the server-side
"git receive-pack" to think that the ref's old-oid has changed
when deleting one level refs, this causes the push to be rejected.

So the solution is to fix the client to be able to delete
one level refs by properly filling old-oid.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c | 4 +++-
 connect.c              | 3 ++-
 t/t5516-fetch-push.sh  | 7 +++++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 13ff9fae3ba..77088f5ba8d 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1463,7 +1463,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 		find_shared_symref(worktrees, "HEAD", name);
 
 	/* only refs/... are allowed */
-	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
+	if (!starts_with(name, "refs/") ||
+	    check_refname_format(name + 5, is_null_oid(new_oid) ?
+				 REFNAME_ALLOW_ONELEVEL : 0)) {
 		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
diff --git a/connect.c b/connect.c
index 63e59641c0d..7a396ad72e9 100644
--- a/connect.c
+++ b/connect.c
@@ -30,7 +30,8 @@ static int check_ref(const char *name, unsigned int flags)
 		return 0;
 
 	/* REF_NORMAL means that we don't want the magic fake tag refs */
-	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
+	if ((flags & REF_NORMAL) && check_refname_format(name,
+							 REFNAME_ALLOW_ONELEVEL))
 		return 0;
 
 	/* REF_HEADS means that we want regular branch heads */
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index f37861efc40..19ebefa5ace 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -903,6 +903,13 @@ test_expect_success 'push --delete refuses empty string' '
 	test_must_fail git push testrepo --delete ""
 '
 
+test_expect_success 'push --delete onelevel refspecs' '
+	mk_test testrepo heads/main &&
+	git -C testrepo update-ref refs/onelevel refs/heads/main &&
+	git push testrepo --delete refs/onelevel &&
+	test_must_fail git -C testrepo rev-parse --verify refs/onelevel
+'
+
 test_expect_success 'warn on push to HEAD of non-bare repository' '
 	mk_test testrepo heads/main &&
 	(
-- 
gitgitgadget

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

* Re: [PATCH v2 2/2] [RFC] push: allow delete one level ref
  2023-02-04 16:48   ` [PATCH v2 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
@ 2023-02-06 22:13     ` Junio C Hamano
  2023-02-24  6:02       ` ZheNing Hu
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2023-02-06 22:13 UTC (permalink / raw)
  To: ZheNing Hu via GitGitGadget
  Cc: git, Ævar Arnfjörð Bjarmason, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King,
	ZheNing Hu

"ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> index 13ff9fae3ba..77088f5ba8d 100644
> --- a/builtin/receive-pack.c
> +++ b/builtin/receive-pack.c
> @@ -1463,7 +1463,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
>  		find_shared_symref(worktrees, "HEAD", name);
>  
>  	/* only refs/... are allowed */
> -	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
> +	if (!starts_with(name, "refs/") ||
> +	    check_refname_format(name + 5, is_null_oid(new_oid) ?
> +				 REFNAME_ALLOW_ONELEVEL : 0)) {

I am somehow smelling that this is about "refs/stash" and it may be
a good thing to allow removing a leftover refs/stash remotely.  I am
not sure why we need to treat it differently while still blocking
update/modification.

Is it that we are actively discouraging one-level refs like
refs/stash, so removing is fine but once removed we do not allow
creating or updating?  It somewhat smells a hard to explain rule to
the users, at least to me.

> diff --git a/connect.c b/connect.c
> index 63e59641c0d..7a396ad72e9 100644
> --- a/connect.c
> +++ b/connect.c
> @@ -30,7 +30,8 @@ static int check_ref(const char *name, unsigned int flags)
>  		return 0;
>  
>  	/* REF_NORMAL means that we don't want the magic fake tag refs */
> -	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
> +	if ((flags & REF_NORMAL) && check_refname_format(name,
> +							 REFNAME_ALLOW_ONELEVEL))
>  		return 0;

This side of the change does make sense.

Thanks.

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

* Re: [PATCH v2 2/2] [RFC] push: allow delete one level ref
  2023-02-06 22:13     ` Junio C Hamano
@ 2023-02-24  6:02       ` ZheNing Hu
  2023-02-24 17:12         ` Junio C Hamano
  0 siblings, 1 reply; 20+ messages in thread
From: ZheNing Hu @ 2023-02-24  6:02 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: ZheNing Hu via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King

Junio C Hamano <gitster@pobox.com> 于2023年2月7日周二 06:13写道:
>
> "ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
> > index 13ff9fae3ba..77088f5ba8d 100644
> > --- a/builtin/receive-pack.c
> > +++ b/builtin/receive-pack.c
> > @@ -1463,7 +1463,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
> >               find_shared_symref(worktrees, "HEAD", name);
> >
> >       /* only refs/... are allowed */
> > -     if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
> > +     if (!starts_with(name, "refs/") ||
> > +         check_refname_format(name + 5, is_null_oid(new_oid) ?
> > +                              REFNAME_ALLOW_ONELEVEL : 0)) {
>
> I am somehow smelling that this is about "refs/stash" and it may be
> a good thing to allow removing a leftover refs/stash remotely.  I am
> not sure why we need to treat it differently while still blocking
> update/modification.
>
> Is it that we are actively discouraging one-level refs like
> refs/stash, so removing is fine but once removed we do not allow
> creating or updating?  It somewhat smells a hard to explain rule to
> the users, at least to me.
>

As I mentioned before, the problem we encountered was that
the refs/master created unexpectedly cannot be deleted. Additionally,
some programs only search for references in the command space of
refs/heads/ or refs/tags/, so they may ignore one-level references
such as refs/master. Therefore, generally speaking, it's better not to
let users create/update special one-level references. This can be achieved
directly through git receive-pack or implemented by upper-layer
services in positions such as pre-receive-hook.

Certainly, I can remove the previous section as you requested.

> > diff --git a/connect.c b/connect.c
> > index 63e59641c0d..7a396ad72e9 100644
> > --- a/connect.c
> > +++ b/connect.c
> > @@ -30,7 +30,8 @@ static int check_ref(const char *name, unsigned int flags)
> >               return 0;
> >
> >       /* REF_NORMAL means that we don't want the magic fake tag refs */
> > -     if ((flags & REF_NORMAL) && check_refname_format(name, 0))
> > +     if ((flags & REF_NORMAL) && check_refname_format(name,
> > +                                                      REFNAME_ALLOW_ONELEVEL))
> >               return 0;
>
> This side of the change does make sense.
>
> Thanks.

Thanks.

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

* Re: [PATCH v2 2/2] [RFC] push: allow delete one level ref
  2023-02-24  6:02       ` ZheNing Hu
@ 2023-02-24 17:12         ` Junio C Hamano
  2023-02-25 14:11           ` ZheNing Hu
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2023-02-24 17:12 UTC (permalink / raw)
  To: ZheNing Hu
  Cc: ZheNing Hu via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King

ZheNing Hu <adlternative@gmail.com> writes:

> ... Therefore, generally speaking, it's better not to let users
> create/update special one-level references.

The question was "Is it that we are actively discouraging one-level
refs like refs/stash, so removing is fine but once removed we do not
allow creating or updating?"

You could just have given a single word answer, Yes, then.

> Certainly, I can remove the previous section as you requested.

I didn't request to do anything to the change.  I asked you to
explain why you allow only delete without create/update, and without
knowing why, I didn't have enough information to make such a request.

I think "we discourage a single-level refnames so allow deleting one
that may have been created by mistake, but do not allow creation or
deletion as before" does make sense.  As long as that is explained
in the proposed log message and in the end-user facing documentation,
I am happy with the new behaviour.

Thanks.

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

* Re: [PATCH v2 2/2] [RFC] push: allow delete one level ref
  2023-02-24 17:12         ` Junio C Hamano
@ 2023-02-25 14:11           ` ZheNing Hu
  0 siblings, 0 replies; 20+ messages in thread
From: ZheNing Hu @ 2023-02-25 14:11 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: ZheNing Hu via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King

Junio C Hamano <gitster@pobox.com> 于2023年2月25日周六 01:12写道:
>
> ZheNing Hu <adlternative@gmail.com> writes:
>
> > ... Therefore, generally speaking, it's better not to let users
> > create/update special one-level references.
>
> The question was "Is it that we are actively discouraging one-level
> refs like refs/stash, so removing is fine but once removed we do not
> allow creating or updating?"
>
> You could just have given a single word answer, Yes, then.
>
> > Certainly, I can remove the previous section as you requested.
>
> I didn't request to do anything to the change.  I asked you to
> explain why you allow only delete without create/update, and without
> knowing why, I didn't have enough information to make such a request.
>
> I think "we discourage a single-level refnames so allow deleting one
> that may have been created by mistake, but do not allow creation or
> deletion as before" does make sense.  As long as that is explained
> in the proposed log message and in the end-user facing documentation,
> I am happy with the new behaviour.
>

I understand. I didn't mention the reason for not allowing the
creation/update of one-level refs in the commit message, I will add it later.

> Thanks.

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

* [PATCH v3 0/2] [RFC] push: allow delete one level ref
  2023-02-04 16:48 ` [PATCH v2 0/2] " ZheNing Hu via GitGitGadget
  2023-02-04 16:48   ` [PATCH v2 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
  2023-02-04 16:48   ` [PATCH v2 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
@ 2023-02-27  1:57   ` ZheNing Hu via GitGitGadget
  2023-02-27  1:57     ` [PATCH v3 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
                       ` (2 more replies)
  2 siblings, 3 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-02-27  1:57 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu

This might be an odd request: allow git push to delete one level refs like
"ref/foo".

Some users on my side often push "refs/for/master" to the remote for code
review, but due to a user's typo, "refs/master" is pushed to the remote.

Pushing a one level ref like "refs/foo" should not have been successful, but
since my server side directly updated the ref during the proc-receive-hook
phase of git receive-pack, "refs/foo" was mistakenly left at on the server.

But for some reasons, users cannot delete this special branch through "git
push -d".

First, I executed git update-ref --stdin inside my proc-receive-hook to
delete the branch. As a result, update-ref reported an error: "cannot lock
ref 'refs/foo': reference already exists".

So I tried GIT_TRACKET_PACKET to debug and found that git push did not seem
to pass the correct ref old-oid, which is why update-ref reported an error.

"18:13:41.214872 pkt-line.c:80           packet: receive-pack< 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/foo\0 report-status-v2 side-band-64k object-format=sha1 agent=git/2.39.0.227.g262c45b6a1"


Tracing it back, the check_ref() in the git push link didn't record the
old-oid for the remote "refs/foo".

At the same time, the server update() process will reject the one level ref
by default and return a "funny refname" error.

It is worth mentioning that since I deleted the branch, the error message
returned here is "refusing to create funny ref 'refs/foo' remotely", which
is also worth fixing.

So this patch series do:

v1.

 1. fix funny refname error message from "create" to "update".
 2. let server allow delete one level ref.
 3. let client pass correct one level ref old-oid.

v2.

 1. fix code style.

v3.

 1. clarify in the docs why single-level refs are allowed to be deleted but
    not created/updated.

ZheNing Hu (2):
  receive-pack: fix funny ref error messsage
  [RFC] push: allow delete single-level ref

 builtin/receive-pack.c |  6 ++++--
 connect.c              |  3 ++-
 t/t5516-fetch-push.sh  | 12 ++++++++++++
 3 files changed, 18 insertions(+), 3 deletions(-)


base-commit: dadc8e6dacb629f46aee39bde90b6f09b73722eb
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1465%2Fadlternative%2Fzh%2Fdelete-one-level-ref-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1465/adlternative/zh/delete-one-level-ref-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1465

Range-diff vs v2:

 1:  214a2b662e3 = 1:  857d2435caf receive-pack: fix funny ref error messsage
 2:  966cb49c388 ! 2:  4dc75f5b961 [RFC] push: allow delete one level ref
     @@ Metadata
      Author: ZheNing Hu <adlternative@gmail.com>
      
       ## Commit message ##
     -    [RFC] push: allow delete one level ref
     +    [RFC] push: allow delete single-level ref
      
     -    Git will reject the deletion of one level refs e,g. "refs/foo"
     -    through "git push -d", however, some users want to be able to
     -    clean up these branches that were created unexpectedly on the
     -    remote.
     +    We discourage the creation/update of single-level refs
     +    because some upper-layer applications only work in specified
     +    reference namespaces, such as "refs/heads/*" or "refs/tags/*",
     +    these single-level refnames may not be recognized. However,
     +    we still hope users can delete them which have been created
     +    by mistake.
      
          Therefore, when updating branches on the server with
          "git receive-pack", by checking whether it is a branch deletion
          operation, it will determine whether to allow the update of
     -    one level refs. This avoids creating/updating such one level
     -    branches, but allows them to be deleted.
     +    a single-level refs. This avoids creating/updating such
     +    single-level refs, but allows them to be deleted.
      
          On the client side, "git push" also does not properly fill in
     -    the old-oid of one level refs, which causes the server-side
     +    the old-oid of single-level refs, which causes the server-side
          "git receive-pack" to think that the ref's old-oid has changed
     -    when deleting one level refs, this causes the push to be rejected.
     -
     -    So the solution is to fix the client to be able to delete
     -    one level refs by properly filling old-oid.
     +    when deleting single-level refs, this causes the push to be
     +    rejected. So the solution is to fix the client to be able to
     +    delete single-level refs by properly filling old-oid.
      
          Signed-off-by: ZheNing Hu <adlternative@gmail.com>
      

-- 
gitgitgadget

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

* [PATCH v3 1/2] receive-pack: fix funny ref error messsage
  2023-02-27  1:57   ` [PATCH v3 0/2] " ZheNing Hu via GitGitGadget
@ 2023-02-27  1:57     ` ZheNing Hu via GitGitGadget
  2023-02-27 16:07       ` Junio C Hamano
  2023-02-27  1:57     ` [PATCH v3 2/2] [RFC] push: allow delete single-level ref ZheNing Hu via GitGitGadget
  2023-03-01 10:20     ` [PATCH v4 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2 siblings, 1 reply; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-02-27  1:57 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

When the user deletes the remote one level branch through
"git push origin -d refs/foo", remote will return an error:
"refusing to create funny ref 'refs/foo' remotely", here we
are not creating "refs/foo" instead wants to delete it, so a
better error description here would be: "refusing to update
funny ref 'refs/foo' remotely".

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c | 2 +-
 t/t5516-fetch-push.sh  | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index cd5c7a28eff..c24616a3ac6 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1464,7 +1464,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
-		rp_error("refusing to create funny ref '%s' remotely", name);
+		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
 	}
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 98a27a2948b..f37861efc40 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -401,6 +401,11 @@ test_expect_success 'push with ambiguity' '
 
 '
 
+test_expect_success 'push with onelevel ref' '
+	mk_test testrepo heads/main &&
+	test_must_fail git push testrepo HEAD:refs/onelevel
+'
+
 test_expect_success 'push with colon-less refspec (1)' '
 
 	mk_test testrepo heads/frotz tags/frotz &&
-- 
gitgitgadget


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

* [PATCH v3 2/2] [RFC] push: allow delete single-level ref
  2023-02-27  1:57   ` [PATCH v3 0/2] " ZheNing Hu via GitGitGadget
  2023-02-27  1:57     ` [PATCH v3 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
@ 2023-02-27  1:57     ` ZheNing Hu via GitGitGadget
  2023-03-01 10:20     ` [PATCH v4 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2 siblings, 0 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-02-27  1:57 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

We discourage the creation/update of single-level refs
because some upper-layer applications only work in specified
reference namespaces, such as "refs/heads/*" or "refs/tags/*",
these single-level refnames may not be recognized. However,
we still hope users can delete them which have been created
by mistake.

Therefore, when updating branches on the server with
"git receive-pack", by checking whether it is a branch deletion
operation, it will determine whether to allow the update of
a single-level refs. This avoids creating/updating such
single-level refs, but allows them to be deleted.

On the client side, "git push" also does not properly fill in
the old-oid of single-level refs, which causes the server-side
"git receive-pack" to think that the ref's old-oid has changed
when deleting single-level refs, this causes the push to be
rejected. So the solution is to fix the client to be able to
delete single-level refs by properly filling old-oid.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c | 4 +++-
 connect.c              | 3 ++-
 t/t5516-fetch-push.sh  | 7 +++++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index c24616a3ac6..af61725a388 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1463,7 +1463,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 		find_shared_symref(worktrees, "HEAD", name);
 
 	/* only refs/... are allowed */
-	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
+	if (!starts_with(name, "refs/") ||
+	    check_refname_format(name + 5, is_null_oid(new_oid) ?
+				 REFNAME_ALLOW_ONELEVEL : 0)) {
 		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
diff --git a/connect.c b/connect.c
index 63e59641c0d..7a396ad72e9 100644
--- a/connect.c
+++ b/connect.c
@@ -30,7 +30,8 @@ static int check_ref(const char *name, unsigned int flags)
 		return 0;
 
 	/* REF_NORMAL means that we don't want the magic fake tag refs */
-	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
+	if ((flags & REF_NORMAL) && check_refname_format(name,
+							 REFNAME_ALLOW_ONELEVEL))
 		return 0;
 
 	/* REF_HEADS means that we want regular branch heads */
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index f37861efc40..19ebefa5ace 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -903,6 +903,13 @@ test_expect_success 'push --delete refuses empty string' '
 	test_must_fail git push testrepo --delete ""
 '
 
+test_expect_success 'push --delete onelevel refspecs' '
+	mk_test testrepo heads/main &&
+	git -C testrepo update-ref refs/onelevel refs/heads/main &&
+	git push testrepo --delete refs/onelevel &&
+	test_must_fail git -C testrepo rev-parse --verify refs/onelevel
+'
+
 test_expect_success 'warn on push to HEAD of non-bare repository' '
 	mk_test testrepo heads/main &&
 	(
-- 
gitgitgadget

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

* Re: [PATCH v3 1/2] receive-pack: fix funny ref error messsage
  2023-02-27  1:57     ` [PATCH v3 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
@ 2023-02-27 16:07       ` Junio C Hamano
  2023-03-01  9:31         ` ZheNing Hu
  0 siblings, 1 reply; 20+ messages in thread
From: Junio C Hamano @ 2023-02-27 16:07 UTC (permalink / raw)
  To: ZheNing Hu via GitGitGadget
  Cc: git, Ævar Arnfjörð Bjarmason, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King,
	ZheNing Hu

"ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: ZheNing Hu <adlternative@gmail.com>
>
> When the user deletes the remote one level branch through
> "git push origin -d refs/foo", remote will return an error:
> "refusing to create funny ref 'refs/foo' remotely", here we
> are not creating "refs/foo" instead wants to delete it, so a
> better error description here would be: "refusing to update
> funny ref 'refs/foo' remotely".

OK, update() works on each ref affected, not just the ones that are
updated, but also created and deleted.  The updated wording may
probably be better.


>
> Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> ---
>  builtin/receive-pack.c | 2 +-
>  t/t5516-fetch-push.sh  | 5 +++++
>  2 files changed, 6 insertions(+), 1 deletion(-)
> ...
> +test_expect_success 'push with onelevel ref' '
> +	mk_test testrepo heads/main &&
> +	test_must_fail git push testrepo HEAD:refs/onelevel
> +'
> +

I am not sure what relevance this new test has to the proposed
update to the message, though.

Thanks.

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

* Re: [PATCH v3 1/2] receive-pack: fix funny ref error messsage
  2023-02-27 16:07       ` Junio C Hamano
@ 2023-03-01  9:31         ` ZheNing Hu
  0 siblings, 0 replies; 20+ messages in thread
From: ZheNing Hu @ 2023-03-01  9:31 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: ZheNing Hu via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, Derrick Stolee,
	Elijah Newren, Michael Haggerty, Christian Couder, Jeff King

Junio C Hamano <gitster@pobox.com> 于2023年2月28日周二 00:07写道:
>
> "ZheNing Hu via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > From: ZheNing Hu <adlternative@gmail.com>
> >
> > When the user deletes the remote one level branch through
> > "git push origin -d refs/foo", remote will return an error:
> > "refusing to create funny ref 'refs/foo' remotely", here we
> > are not creating "refs/foo" instead wants to delete it, so a
> > better error description here would be: "refusing to update
> > funny ref 'refs/foo' remotely".
>
> OK, update() works on each ref affected, not just the ones that are
> updated, but also created and deleted.  The updated wording may
> probably be better.
>
>
> >
> > Signed-off-by: ZheNing Hu <adlternative@gmail.com>
> > ---
> >  builtin/receive-pack.c | 2 +-
> >  t/t5516-fetch-push.sh  | 5 +++++
> >  2 files changed, 6 insertions(+), 1 deletion(-)
> > ...
> > +test_expect_success 'push with onelevel ref' '
> > +     mk_test testrepo heads/main &&
> > +     test_must_fail git push testrepo HEAD:refs/onelevel
> > +'
> > +
>
> I am not sure what relevance this new test has to the proposed
> update to the message, though.
>

Ah, I should have incorporated this part of the test into another
patch test.

> Thanks.

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

* [PATCH v4 0/2] [RFC] push: allow delete one level ref
  2023-02-27  1:57   ` [PATCH v3 0/2] " ZheNing Hu via GitGitGadget
  2023-02-27  1:57     ` [PATCH v3 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
  2023-02-27  1:57     ` [PATCH v3 2/2] [RFC] push: allow delete single-level ref ZheNing Hu via GitGitGadget
@ 2023-03-01 10:20     ` ZheNing Hu via GitGitGadget
  2023-03-01 10:20       ` [PATCH v4 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
  2023-03-01 10:20       ` [PATCH v4 2/2] push: allow delete single-level ref ZheNing Hu via GitGitGadget
  2 siblings, 2 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-03-01 10:20 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu

This might be an odd request: allow git push to delete one level refs like
"ref/foo".

Some users on my side often push "refs/for/master" to the remote for code
review, but due to a user's typo, "refs/master" is pushed to the remote.

Pushing a one level ref like "refs/foo" should not have been successful, but
since my server side directly updated the ref during the proc-receive-hook
phase of git receive-pack, "refs/foo" was mistakenly left at on the server.

But for some reasons, users cannot delete this special branch through "git
push -d".

First, I executed git update-ref --stdin inside my proc-receive-hook to
delete the branch. As a result, update-ref reported an error: "cannot lock
ref 'refs/foo': reference already exists".

So I tried GIT_TRACKET_PACKET to debug and found that git push did not seem
to pass the correct ref old-oid, which is why update-ref reported an error.

"18:13:41.214872 pkt-line.c:80           packet: receive-pack< 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 refs/foo\0 report-status-v2 side-band-64k object-format=sha1 agent=git/2.39.0.227.g262c45b6a1"


Tracing it back, the check_ref() in the git push link didn't record the
old-oid for the remote "refs/foo".

At the same time, the server update() process will reject the one level ref
by default and return a "funny refname" error.

It is worth mentioning that since I deleted the branch, the error message
returned here is "refusing to create funny ref 'refs/foo' remotely", which
is also worth fixing.

So this patch series do:

v1.

 1. fix funny refname error message from "create" to "update".
 2. let server allow delete one level ref.
 3. let client pass correct one level ref old-oid.

v2.

 1. fix code style.

v3.

 1. clarify in the docs why single-level refs are allowed to be deleted but
    not created/updated.

v4.

 1. move the testing part of the first patch to the second patch.

ZheNing Hu (2):
  receive-pack: fix funny ref error messsage
  push: allow delete single-level ref

 builtin/receive-pack.c |  6 ++++--
 connect.c              |  3 ++-
 t/t5516-fetch-push.sh  | 12 ++++++++++++
 3 files changed, 18 insertions(+), 3 deletions(-)


base-commit: dadc8e6dacb629f46aee39bde90b6f09b73722eb
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1465%2Fadlternative%2Fzh%2Fdelete-one-level-ref-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1465/adlternative/zh/delete-one-level-ref-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/1465

Range-diff vs v3:

 1:  857d2435caf ! 1:  eef7bca63c6 receive-pack: fix funny ref error messsage
     @@ builtin/receive-pack.c: static const char *update(struct command *cmd, struct sh
       		ret = "funny refname";
       		goto out;
       	}
     -
     - ## t/t5516-fetch-push.sh ##
     -@@ t/t5516-fetch-push.sh: test_expect_success 'push with ambiguity' '
     - 
     - '
     - 
     -+test_expect_success 'push with onelevel ref' '
     -+	mk_test testrepo heads/main &&
     -+	test_must_fail git push testrepo HEAD:refs/onelevel
     -+'
     -+
     - test_expect_success 'push with colon-less refspec (1)' '
     - 
     - 	mk_test testrepo heads/frotz tags/frotz &&
 2:  4dc75f5b961 ! 2:  022818f0e9f [RFC] push: allow delete single-level ref
     @@ Metadata
      Author: ZheNing Hu <adlternative@gmail.com>
      
       ## Commit message ##
     -    [RFC] push: allow delete single-level ref
     +    push: allow delete single-level ref
      
          We discourage the creation/update of single-level refs
          because some upper-layer applications only work in specified
     @@ connect.c: static int check_ref(const char *name, unsigned int flags)
       	/* REF_HEADS means that we want regular branch heads */
      
       ## t/t5516-fetch-push.sh ##
     +@@ t/t5516-fetch-push.sh: test_expect_success 'push with ambiguity' '
     + 
     + '
     + 
     ++test_expect_success 'push with onelevel ref' '
     ++	mk_test testrepo heads/main &&
     ++	test_must_fail git push testrepo HEAD:refs/onelevel
     ++'
     ++
     + test_expect_success 'push with colon-less refspec (1)' '
     + 
     + 	mk_test testrepo heads/frotz tags/frotz &&
      @@ t/t5516-fetch-push.sh: test_expect_success 'push --delete refuses empty string' '
       	test_must_fail git push testrepo --delete ""
       '

-- 
gitgitgadget

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

* [PATCH v4 1/2] receive-pack: fix funny ref error messsage
  2023-03-01 10:20     ` [PATCH v4 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
@ 2023-03-01 10:20       ` ZheNing Hu via GitGitGadget
  2023-03-01 10:20       ` [PATCH v4 2/2] push: allow delete single-level ref ZheNing Hu via GitGitGadget
  1 sibling, 0 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-03-01 10:20 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

When the user deletes the remote one level branch through
"git push origin -d refs/foo", remote will return an error:
"refusing to create funny ref 'refs/foo' remotely", here we
are not creating "refs/foo" instead wants to delete it, so a
better error description here would be: "refusing to update
funny ref 'refs/foo' remotely".

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index cd5c7a28eff..c24616a3ac6 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1464,7 +1464,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
-		rp_error("refusing to create funny ref '%s' remotely", name);
+		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
 	}
-- 
gitgitgadget


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

* [PATCH v4 2/2] push: allow delete single-level ref
  2023-03-01 10:20     ` [PATCH v4 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
  2023-03-01 10:20       ` [PATCH v4 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
@ 2023-03-01 10:20       ` ZheNing Hu via GitGitGadget
  1 sibling, 0 replies; 20+ messages in thread
From: ZheNing Hu via GitGitGadget @ 2023-03-01 10:20 UTC (permalink / raw)
  To: git
  Cc: Ævar Arnfjörð Bjarmason, Junio C Hamano,
	Derrick Stolee, Elijah Newren, Michael Haggerty, Christian Couder,
	Jeff King, ZheNing Hu, ZheNing Hu

From: ZheNing Hu <adlternative@gmail.com>

We discourage the creation/update of single-level refs
because some upper-layer applications only work in specified
reference namespaces, such as "refs/heads/*" or "refs/tags/*",
these single-level refnames may not be recognized. However,
we still hope users can delete them which have been created
by mistake.

Therefore, when updating branches on the server with
"git receive-pack", by checking whether it is a branch deletion
operation, it will determine whether to allow the update of
a single-level refs. This avoids creating/updating such
single-level refs, but allows them to be deleted.

On the client side, "git push" also does not properly fill in
the old-oid of single-level refs, which causes the server-side
"git receive-pack" to think that the ref's old-oid has changed
when deleting single-level refs, this causes the push to be
rejected. So the solution is to fix the client to be able to
delete single-level refs by properly filling old-oid.

Signed-off-by: ZheNing Hu <adlternative@gmail.com>
---
 builtin/receive-pack.c |  4 +++-
 connect.c              |  3 ++-
 t/t5516-fetch-push.sh  | 12 ++++++++++++
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index c24616a3ac6..af61725a388 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1463,7 +1463,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 		find_shared_symref(worktrees, "HEAD", name);
 
 	/* only refs/... are allowed */
-	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
+	if (!starts_with(name, "refs/") ||
+	    check_refname_format(name + 5, is_null_oid(new_oid) ?
+				 REFNAME_ALLOW_ONELEVEL : 0)) {
 		rp_error("refusing to update funny ref '%s' remotely", name);
 		ret = "funny refname";
 		goto out;
diff --git a/connect.c b/connect.c
index 63e59641c0d..7a396ad72e9 100644
--- a/connect.c
+++ b/connect.c
@@ -30,7 +30,8 @@ static int check_ref(const char *name, unsigned int flags)
 		return 0;
 
 	/* REF_NORMAL means that we don't want the magic fake tag refs */
-	if ((flags & REF_NORMAL) && check_refname_format(name, 0))
+	if ((flags & REF_NORMAL) && check_refname_format(name,
+							 REFNAME_ALLOW_ONELEVEL))
 		return 0;
 
 	/* REF_HEADS means that we want regular branch heads */
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 98a27a2948b..19ebefa5ace 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -401,6 +401,11 @@ test_expect_success 'push with ambiguity' '
 
 '
 
+test_expect_success 'push with onelevel ref' '
+	mk_test testrepo heads/main &&
+	test_must_fail git push testrepo HEAD:refs/onelevel
+'
+
 test_expect_success 'push with colon-less refspec (1)' '
 
 	mk_test testrepo heads/frotz tags/frotz &&
@@ -898,6 +903,13 @@ test_expect_success 'push --delete refuses empty string' '
 	test_must_fail git push testrepo --delete ""
 '
 
+test_expect_success 'push --delete onelevel refspecs' '
+	mk_test testrepo heads/main &&
+	git -C testrepo update-ref refs/onelevel refs/heads/main &&
+	git push testrepo --delete refs/onelevel &&
+	test_must_fail git -C testrepo rev-parse --verify refs/onelevel
+'
+
 test_expect_success 'warn on push to HEAD of non-bare repository' '
 	mk_test testrepo heads/main &&
 	(
-- 
gitgitgadget

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

end of thread, other threads:[~2023-03-01 10:20 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-17 10:32 [PATCH 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
2023-01-17 10:32 ` [PATCH 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
2023-01-17 10:32 ` [PATCH 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
2023-01-17 13:14   ` Ævar Arnfjörð Bjarmason
2023-01-19 17:39     ` ZheNing Hu
2023-02-04 16:48 ` [PATCH v2 0/2] " ZheNing Hu via GitGitGadget
2023-02-04 16:48   ` [PATCH v2 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
2023-02-04 16:48   ` [PATCH v2 2/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
2023-02-06 22:13     ` Junio C Hamano
2023-02-24  6:02       ` ZheNing Hu
2023-02-24 17:12         ` Junio C Hamano
2023-02-25 14:11           ` ZheNing Hu
2023-02-27  1:57   ` [PATCH v3 0/2] " ZheNing Hu via GitGitGadget
2023-02-27  1:57     ` [PATCH v3 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
2023-02-27 16:07       ` Junio C Hamano
2023-03-01  9:31         ` ZheNing Hu
2023-02-27  1:57     ` [PATCH v3 2/2] [RFC] push: allow delete single-level ref ZheNing Hu via GitGitGadget
2023-03-01 10:20     ` [PATCH v4 0/2] [RFC] push: allow delete one level ref ZheNing Hu via GitGitGadget
2023-03-01 10:20       ` [PATCH v4 1/2] receive-pack: fix funny ref error messsage ZheNing Hu via GitGitGadget
2023-03-01 10:20       ` [PATCH v4 2/2] push: allow delete single-level ref ZheNing Hu 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).