Skip to content

Commit aafc1d1

Browse files
committed
Merge branch 'jc/forbid-head-as-tagname' into seen
"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 is a valid tagname refs: drop strbuf_ prefix from helpers refs: move ref name helpers around
2 parents f7fee42 + 6595acf commit aafc1d1

File tree

14 files changed

+100
-87
lines changed

14 files changed

+100
-87
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
372372
*/
373373
int validate_branchname(const char *name, struct strbuf *ref)
374374
{
375-
if (strbuf_check_branch_ref(ref, name)) {
375+
if (check_branch_ref(ref, name)) {
376376
int code = die_message(_("'%s' is not a valid branch name"), name);
377377
advise_if_enabled(ADVICE_REF_SYNTAX,
378378
_("See `man git check-ref-format`"));

builtin/branch.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
259259
char *target = NULL;
260260
int flags = 0;
261261

262-
strbuf_branchname(&bname, argv[i], allowed_interpret);
262+
copy_branchname(&bname, argv[i], allowed_interpret);
263263
free(name);
264264
name = mkpathdup(fmt, bname.buf);
265265

@@ -581,7 +581,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
581581
int recovery = 0, oldref_usage = 0;
582582
struct worktree **worktrees = get_worktrees();
583583

584-
if (strbuf_check_branch_ref(&oldref, oldname)) {
584+
if (check_branch_ref(&oldref, oldname)) {
585585
/*
586586
* Bad name --- this could be an attempt to rename a
587587
* ref that we used to allow to be created by accident.
@@ -898,7 +898,7 @@ int cmd_branch(int argc,
898898
die(_("cannot give description to detached HEAD"));
899899
branch_name = head;
900900
} else if (argc == 1) {
901-
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
901+
copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
902902
branch_name = buf.buf;
903903
} else {
904904
die(_("cannot edit description of more than one branch"));
@@ -941,7 +941,7 @@ int cmd_branch(int argc,
941941
if (!argc)
942942
branch = branch_get(NULL);
943943
else if (argc == 1) {
944-
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
944+
copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
945945
branch = branch_get(buf.buf);
946946
} else
947947
die(_("too many arguments to set new upstream"));
@@ -971,7 +971,7 @@ int cmd_branch(int argc,
971971
if (!argc)
972972
branch = branch_get(NULL);
973973
else if (argc == 1) {
974-
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
974+
copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
975975
branch = branch_get(buf.buf);
976976
} else
977977
die(_("too many arguments to unset upstream"));

builtin/check-ref-format.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int check_ref_format_branch(const char *arg)
4242
int nongit;
4343

4444
setup_git_directory_gently(&nongit);
45-
if (strbuf_check_branch_ref(&sb, arg) ||
45+
if (check_branch_ref(&sb, arg) ||
4646
!skip_prefix(sb.buf, "refs/heads/", &name))
4747
die("'%s' is not a valid branch name", arg);
4848
printf("%s\n", name);

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ static void setup_branch_path(struct branch_info *branch)
744744
&branch->oid, &branch->refname, 0))
745745
repo_get_oid_committish(the_repository, branch->name, &branch->oid);
746746

747-
strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
747+
copy_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
748748
if (strcmp(buf.buf, branch->name)) {
749749
free(branch->name);
750750
branch->name = xstrdup(buf.buf);

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
501501
char *found_ref = NULL;
502502
int len, early;
503503

504-
strbuf_branchname(&bname, remote, 0);
504+
copy_branchname(&bname, remote, 0);
505505
remote = bname.buf;
506506

507507
oidclr(&branch_head, the_repository->hash_algo);

builtin/tag.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,6 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
450450
return 0;
451451
}
452452

453-
static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
454-
{
455-
if (name[0] == '-')
456-
return -1;
457-
458-
strbuf_reset(sb);
459-
strbuf_addf(sb, "refs/tags/%s", name);
460-
461-
return check_refname_format(sb->buf, 0);
462-
}
463-
464453
int cmd_tag(int argc,
465454
const char **argv,
466455
const char *prefix,
@@ -653,7 +642,7 @@ int cmd_tag(int argc,
653642
if (repo_get_oid(the_repository, object_ref, &object))
654643
die(_("Failed to resolve '%s' as a valid ref."), object_ref);
655644

656-
if (strbuf_check_tag_ref(&ref, tag))
645+
if (check_tag_ref(&ref, tag))
657646
die(_("'%s' is not a valid tag name."), tag);
658647

659648
if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev))

builtin/worktree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static int add_worktree(const char *path, const char *refname,
440440
worktrees = NULL;
441441

442442
/* is 'refname' a branch or commit? */
443-
if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
443+
if (!opts->detach && !check_branch_ref(&symref, refname) &&
444444
refs_ref_exists(get_main_ref_store(the_repository), symref.buf)) {
445445
is_branch = 1;
446446
if (!opts->force)
@@ -607,7 +607,7 @@ static void print_preparing_worktree_line(int detach,
607607
fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
608608
} else {
609609
struct strbuf s = STRBUF_INIT;
610-
if (!detach && !strbuf_check_branch_ref(&s, branch) &&
610+
if (!detach && !check_branch_ref(&s, branch) &&
611611
refs_ref_exists(get_main_ref_store(the_repository), s.buf))
612612
fprintf_ln(stderr, _("Preparing worktree (checking out '%s')"),
613613
branch);
@@ -748,7 +748,7 @@ static char *dwim_branch(const char *path, char **new_branch)
748748
char *branchname = xstrndup(s, n);
749749
struct strbuf ref = STRBUF_INIT;
750750

751-
branch_exists = !strbuf_check_branch_ref(&ref, branchname) &&
751+
branch_exists = !check_branch_ref(&ref, branchname) &&
752752
refs_ref_exists(get_main_ref_store(the_repository),
753753
ref.buf);
754754
strbuf_release(&ref);
@@ -845,7 +845,7 @@ static int add(int ac, const char **av, const char *prefix,
845845
new_branch = new_branch_force;
846846

847847
if (!opts.force &&
848-
!strbuf_check_branch_ref(&symref, new_branch) &&
848+
!check_branch_ref(&symref, new_branch) &&
849849
refs_ref_exists(get_main_ref_store(the_repository), symref.buf))
850850
die_if_checked_out(symref.buf, 0);
851851
strbuf_release(&symref);

gitweb/gitweb.perl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2094,7 +2094,7 @@ sub format_log_line_html {
20942094
(
20952095
# The output of "git describe", e.g. v2.10.0-297-gf6727b0
20962096
# or hadoop-20160921-113441-20-g094fb7d
2097-
(?<!-) # see strbuf_check_tag_ref(). Tags can't start with -
2097+
(?<!-) # see check_tag_ref(). Tags can't start with -
20982098
[A-Za-z0-9.-]+
20992099
(?!\.) # refs can't end with ".", see check_refname_format()
21002100
-g$regex

object-name.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,42 +1735,6 @@ int repo_interpret_branch_name(struct repository *r,
17351735
return -1;
17361736
}
17371737

1738-
void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1739-
{
1740-
int len = strlen(name);
1741-
struct interpret_branch_name_options options = {
1742-
.allowed = allowed
1743-
};
1744-
int used = repo_interpret_branch_name(the_repository, name, len, sb,
1745-
&options);
1746-
1747-
if (used < 0)
1748-
used = 0;
1749-
strbuf_add(sb, name + used, len - used);
1750-
}
1751-
1752-
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1753-
{
1754-
if (startup_info->have_repository)
1755-
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1756-
else
1757-
strbuf_addstr(sb, name);
1758-
1759-
/*
1760-
* This splice must be done even if we end up rejecting the
1761-
* name; builtin/branch.c::copy_or_rename_branch() still wants
1762-
* to see what the name expanded to so that "branch -m" can be
1763-
* used as a tool to correct earlier mistakes.
1764-
*/
1765-
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1766-
1767-
if (*name == '-' ||
1768-
!strcmp(sb->buf, "refs/heads/HEAD"))
1769-
return -1;
1770-
1771-
return check_refname_format(sb->buf, 0);
1772-
}
1773-
17741738
void object_context_release(struct object_context *ctx)
17751739
{
17761740
free(ctx->path);

refs.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,53 @@ static char *substitute_branch_name(struct repository *r,
699699
return NULL;
700700
}
701701

702+
void copy_branchname(struct strbuf *sb, const char *name, unsigned allowed)
703+
{
704+
int len = strlen(name);
705+
struct interpret_branch_name_options options = {
706+
.allowed = allowed
707+
};
708+
int used = repo_interpret_branch_name(the_repository, name, len, sb,
709+
&options);
710+
711+
if (used < 0)
712+
used = 0;
713+
strbuf_add(sb, name + used, len - used);
714+
}
715+
716+
int check_branch_ref(struct strbuf *sb, const char *name)
717+
{
718+
if (startup_info->have_repository)
719+
copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
720+
else
721+
strbuf_addstr(sb, name);
722+
723+
/*
724+
* This splice must be done even if we end up rejecting the
725+
* name; builtin/branch.c::copy_or_rename_branch() still wants
726+
* to see what the name expanded to so that "branch -m" can be
727+
* used as a tool to correct earlier mistakes.
728+
*/
729+
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
730+
731+
if (*name == '-' ||
732+
!strcmp(sb->buf, "refs/heads/HEAD"))
733+
return -1;
734+
735+
return check_refname_format(sb->buf, 0);
736+
}
737+
738+
int check_tag_ref(struct strbuf *sb, const char *name)
739+
{
740+
if (name[0] == '-' || !strcmp(name, "HEAD"))
741+
return -1;
742+
743+
strbuf_reset(sb);
744+
strbuf_addf(sb, "refs/tags/%s", name);
745+
746+
return check_refname_format(sb->buf, 0);
747+
}
748+
702749
int repo_dwim_ref(struct repository *r, const char *str, int len,
703750
struct object_id *oid, char **ref, int nonfatal_dangling_mark)
704751
{

0 commit comments

Comments
 (0)