Merge branch 'jc/forbid-head-as-tagname'

"git tag" has been taught to refuse to create refs/tags/HEAD
as such a tag will be confusing in the context of UI provided by
the Git Porcelain commands.

* jc/forbid-head-as-tagname:
  tag: "git tag" refuses to use HEAD as a tagname
  t5604: do not expect that HEAD can be a valid tagname
  refs: drop strbuf_ prefix from helpers
  refs: move ref name helpers around
This commit is contained in:
Junio C Hamano
2024-12-15 17:54:26 -08:00
14 changed files with 106 additions and 87 deletions

47
refs.c
View File

@ -698,6 +698,53 @@ static char *substitute_branch_name(struct repository *r,
return NULL;
}
void copy_branchname(struct strbuf *sb, const char *name, unsigned allowed)
{
int len = strlen(name);
struct interpret_branch_name_options options = {
.allowed = allowed
};
int used = repo_interpret_branch_name(the_repository, name, len, sb,
&options);
if (used < 0)
used = 0;
strbuf_add(sb, name + used, len - used);
}
int check_branch_ref(struct strbuf *sb, const char *name)
{
if (startup_info->have_repository)
copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
else
strbuf_addstr(sb, name);
/*
* This splice must be done even if we end up rejecting the
* name; builtin/branch.c::copy_or_rename_branch() still wants
* to see what the name expanded to so that "branch -m" can be
* used as a tool to correct earlier mistakes.
*/
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
if (*name == '-' ||
!strcmp(sb->buf, "refs/heads/HEAD"))
return -1;
return check_refname_format(sb->buf, 0);
}
int check_tag_ref(struct strbuf *sb, const char *name)
{
if (name[0] == '-' || !strcmp(name, "HEAD"))
return -1;
strbuf_reset(sb);
strbuf_addf(sb, "refs/tags/%s", name);
return check_refname_format(sb->buf, 0);
}
int repo_dwim_ref(struct repository *r, const char *str, int len,
struct object_id *oid, char **ref, int nonfatal_dangling_mark)
{