Skip to content

Commit ab738b2

Browse files
committed
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
2 parents 73b7e03 + bbd445d commit ab738b2

File tree

14 files changed

+106
-87
lines changed

14 files changed

+106
-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
@@ -257,7 +257,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
257257
char *target = NULL;
258258
int flags = 0;
259259

260-
strbuf_branchname(&bname, argv[i], allowed_interpret);
260+
copy_branchname(&bname, argv[i], allowed_interpret);
261261
free(name);
262262
name = mkpathdup(fmt, bname.buf);
263263

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

582-
if (strbuf_check_branch_ref(&oldref, oldname)) {
582+
if (check_branch_ref(&oldref, oldname)) {
583583
/*
584584
* Bad name --- this could be an attempt to rename a
585585
* ref that we used to allow to be created by accident.
@@ -896,7 +896,7 @@ int cmd_branch(int argc,
896896
die(_("cannot give description to detached HEAD"));
897897
branch_name = head;
898898
} else if (argc == 1) {
899-
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
899+
copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
900900
branch_name = buf.buf;
901901
} else {
902902
die(_("cannot edit description of more than one branch"));
@@ -939,7 +939,7 @@ int cmd_branch(int argc,
939939
if (!argc)
940940
branch = branch_get(NULL);
941941
else if (argc == 1) {
942-
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
942+
copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
943943
branch = branch_get(buf.buf);
944944
} else
945945
die(_("too many arguments to set new upstream"));
@@ -969,7 +969,7 @@ int cmd_branch(int argc,
969969
if (!argc)
970970
branch = branch_get(NULL);
971971
else if (argc == 1) {
972-
strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
972+
copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
973973
branch = branch_get(buf.buf);
974974
} else
975975
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
@@ -742,7 +742,7 @@ static void setup_branch_path(struct branch_info *branch)
742742
&branch->oid, &branch->refname, 0))
743743
repo_get_oid_committish(the_repository, branch->name, &branch->oid);
744744

745-
strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
745+
copy_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
746746
if (strcmp(buf.buf, branch->name)) {
747747
free(branch->name);
748748
branch->name = xstrdup(buf.buf);

builtin/merge.c

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

501-
strbuf_branchname(&bname, remote, 0);
501+
copy_branchname(&bname, remote, 0);
502502
remote = bname.buf;
503503

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

builtin/tag.c

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

450-
static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
451-
{
452-
if (name[0] == '-')
453-
return -1;
454-
455-
strbuf_reset(sb);
456-
strbuf_addf(sb, "refs/tags/%s", name);
457-
458-
return check_refname_format(sb->buf, 0);
459-
}
460-
461450
int cmd_tag(int argc,
462451
const char **argv,
463452
const char *prefix,
@@ -650,7 +639,7 @@ int cmd_tag(int argc,
650639
if (repo_get_oid(the_repository, object_ref, &object))
651640
die(_("Failed to resolve '%s' as a valid ref."), object_ref);
652641

653-
if (strbuf_check_tag_ref(&ref, tag))
642+
if (check_tag_ref(&ref, tag))
654643
die(_("'%s' is not a valid tag name."), tag);
655644

656645
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
@@ -438,7 +438,7 @@ static int add_worktree(const char *path, const char *refname,
438438
worktrees = NULL;
439439

440440
/* is 'refname' a branch or commit? */
441-
if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
441+
if (!opts->detach && !check_branch_ref(&symref, refname) &&
442442
refs_ref_exists(get_main_ref_store(the_repository), symref.buf)) {
443443
is_branch = 1;
444444
if (!opts->force)
@@ -605,7 +605,7 @@ static void print_preparing_worktree_line(int detach,
605605
fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
606606
} else {
607607
struct strbuf s = STRBUF_INIT;
608-
if (!detach && !strbuf_check_branch_ref(&s, branch) &&
608+
if (!detach && !check_branch_ref(&s, branch) &&
609609
refs_ref_exists(get_main_ref_store(the_repository), s.buf))
610610
fprintf_ln(stderr, _("Preparing worktree (checking out '%s')"),
611611
branch);
@@ -746,7 +746,7 @@ static char *dwim_branch(const char *path, char **new_branch)
746746
char *branchname = xstrndup(s, n);
747747
struct strbuf ref = STRBUF_INIT;
748748

749-
branch_exists = !strbuf_check_branch_ref(&ref, branchname) &&
749+
branch_exists = !check_branch_ref(&ref, branchname) &&
750750
refs_ref_exists(get_main_ref_store(the_repository),
751751
ref.buf);
752752
strbuf_release(&ref);
@@ -843,7 +843,7 @@ static int add(int ac, const char **av, const char *prefix,
843843
new_branch = new_branch_force;
844844

845845
if (!opts.force &&
846-
!strbuf_check_branch_ref(&symref, new_branch) &&
846+
!check_branch_ref(&symref, new_branch) &&
847847
refs_ref_exists(get_main_ref_store(the_repository), symref.buf))
848848
die_if_checked_out(symref.buf, 0);
849849
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
@@ -1734,42 +1734,6 @@ int repo_interpret_branch_name(struct repository *r,
17341734
return -1;
17351735
}
17361736

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

refs.c

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

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

0 commit comments

Comments
 (0)