Skip to content

Commit 7c3c550

Browse files
adlternativegitster
authored andcommitted
push: allow delete single-level ref
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d81ba50 commit 7c3c550

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

builtin/receive-pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,9 @@ static const char *update(struct command *cmd, struct shallow_info *si)
14631463
find_shared_symref(worktrees, "HEAD", name);
14641464

14651465
/* only refs/... are allowed */
1466-
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
1466+
if (!starts_with(name, "refs/") ||
1467+
check_refname_format(name + 5, is_null_oid(new_oid) ?
1468+
REFNAME_ALLOW_ONELEVEL : 0)) {
14671469
rp_error("refusing to update funny ref '%s' remotely", name);
14681470
ret = "funny refname";
14691471
goto out;

connect.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ static int check_ref(const char *name, unsigned int flags)
3030
return 0;
3131

3232
/* REF_NORMAL means that we don't want the magic fake tag refs */
33-
if ((flags & REF_NORMAL) && check_refname_format(name, 0))
33+
if ((flags & REF_NORMAL) && check_refname_format(name,
34+
REFNAME_ALLOW_ONELEVEL))
3435
return 0;
3536

3637
/* REF_HEADS means that we want regular branch heads */

t/t5516-fetch-push.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ test_expect_success 'push with ambiguity' '
401401
402402
'
403403

404+
test_expect_success 'push with onelevel ref' '
405+
mk_test testrepo heads/main &&
406+
test_must_fail git push testrepo HEAD:refs/onelevel
407+
'
408+
404409
test_expect_success 'push with colon-less refspec (1)' '
405410
406411
mk_test testrepo heads/frotz tags/frotz &&
@@ -898,6 +903,13 @@ test_expect_success 'push --delete refuses empty string' '
898903
test_must_fail git push testrepo --delete ""
899904
'
900905

906+
test_expect_success 'push --delete onelevel refspecs' '
907+
mk_test testrepo heads/main &&
908+
git -C testrepo update-ref refs/onelevel refs/heads/main &&
909+
git push testrepo --delete refs/onelevel &&
910+
test_must_fail git -C testrepo rev-parse --verify refs/onelevel
911+
'
912+
901913
test_expect_success 'warn on push to HEAD of non-bare repository' '
902914
mk_test testrepo heads/main &&
903915
(

0 commit comments

Comments
 (0)