Skip to content

pr-1465/adlternative/zh/delete-one-level-ref-v4

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

Submitted-As: https://lore.kernel.org/git/pull.1465.v4.git.1677666029.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.1465.git.1673951562.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.1465.v2.git.1675529298.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.1465.v3.git.1677463022.gitgitgadget@gmail.com
Assets 2