Skip to contentSkip to conversation

forbid HEAD as a tagname

PATCH Superseded Junio C Hamano 6 participants 22 messages Dec 2, 2024 – Dec 5, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover forbid HEAD as a tagname

So, here is a cleaned up version, which ended up to be a 4-patch series.

- The first two steps move code around to get rid of the "strbuf_" prefix from three misnamed helper functions that are more about refs than they are about string manipulation operations. We now have:

- copy_branchname() that copies a branchname while applying interpret_branch_name() like @{-1} for previous branch, @{u} for upstream, etc.

- check_branch_ref() and check_tag_ref() that are allowed to be a bit stricter than check_refname_format(), e.g., to reject "HEAD" as the name of a branch or a tag.

- The third step updates a test that assumes HEAD can be usable as the name of a tag, but the breakage the test tries to protect is not specific to any tagname.

- The final step then forbids "git tag" from using "HEAD" as the name of a tag.

Junio C Hamano (4):
refs: move ref name helpers around
refs: drop strbuf_ prefix from helpers
t5604: do not expect that HEAD is a valid tagname
tag: "git tag" refuses to use HEAD as a tagname

 branch.c                   |  2 +-
 builtin/branch.c           | 10 ++++----
 builtin/check-ref-format.c |  2 +-
 builtin/checkout.c         |  2 +-
 builtin/merge.c            |  2 +-
 builtin/tag.c              | 13 +----------
 builtin/worktree.c         |  8 +++----
 gitweb/gitweb.perl         |  2 +-
 object-name.c              | 36 -----------------------------
 refs.c                     | 47 ++++++++++++++++++++++++++++++++++++++
 refs.h                     | 29 +++++++++++++++++++++++
 strbuf.h                   | 22 ------------------
 t/t5604-clone-reference.sh |  6 ++---
 t/t7004-tag.sh             |  6 +++++
 14 files changed, 100 insertions(+), 87 deletions(-)
Signature · 1 line
2.47.1-514-g9b43e7ecc4
#1 lore raw

PATCH 1/4 refs: move ref name helpers around

strbuf_branchname(), strbuf_check_{branch,tag}_ref() are helper functions to deal with branch and tag names, and the fact that they happen to use strbuf to hold the name of a branch or a tag is not essential. These functions fit better in the refs API than strbuf API, the latter of which is about string manipulations.

---
 builtin/tag.c | 11 -----------
 object-name.c | 36 ------------------------------------
 refs.c        | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 refs.h        | 29 +++++++++++++++++++++++++++++
 strbuf.h      | 22 ----------------------
 5 files changed, 76 insertions(+), 69 deletions(-)
builtin/tag.c +0 -11
index 93d10d5915..8279dccbe0 100644
@@ -447,17 +447,6 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
-static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
-{
-	if (name[0] == '-')
-		return -1;
-
-	strbuf_reset(sb);
-	strbuf_addf(sb, "refs/tags/%s", name);
-
-	return check_refname_format(sb->buf, 0);
-}
-
 int cmd_tag(int argc,
 	    const char **argv,
 	    const char *prefix,
object-name.c +0 -36
index c892fbe80a..9f2ae164e4 100644
@@ -1734,42 +1734,6 @@ int repo_interpret_branch_name(struct repository *r,
 	return -1;
 }
 
-void strbuf_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 strbuf_check_branch_ref(struct strbuf *sb, const char *name)
-{
-	if (startup_info->have_repository)
-		strbuf_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);
-}
-
 void object_context_release(struct object_context *ctx)
 {
 	free(ctx->path);
refs.c +47 -0

53 lines not on this page · raw message

refs.h +29 -0
index 108dfc93b3..f19b0ad92f 100644
@@ -180,6 +180,35 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
  */
 char *repo_default_branch_name(struct repository *r, int quiet);
 
+/*
+ * Copy "name" to "sb", expanding any special @-marks as handled by
+ * repo_interpret_branch_name(). The result is a non-qualified branch name
+ * (so "foo" or "origin/master" instead of "refs/heads/foo" or
+ * "refs/remotes/origin/master").
+ *
+ * Note that the resulting name may not be a syntactically valid refname.
+ *
+ * If "allowed" is non-zero, restrict the set of allowed expansions. See
+ * repo_interpret_branch_name() for details.
+ */
+void strbuf_branchname(struct strbuf *sb, const char *name,
+		       unsigned allowed);
+
+/*
+ * Like strbuf_branchname() above, but confirm that the result is
+ * syntactically valid to be used as a local branch name in refs/heads/.
+ *
+ * The return value is "0" if the result is valid, and "-1" otherwise.
+ */
+int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
+
+/*
+ * Similar for a tag name in refs/tags/.
+ *
+ * The return value is "0" if the result is valid, and "-1" otherwise.
+ */
+int strbuf_check_tag_ref(struct strbuf *sb, const char *name);
+
 /*
  * A ref_transaction represents a collection of reference updates that
  * should succeed or fail together.
strbuf.h +0 -22
index 003f880ff7..4dc05b4ba7 100644
@@ -637,28 +637,6 @@ static inline void strbuf_complete_line(struct strbuf *sb)
 	strbuf_complete(sb, '\n');
 }
 
-/*
- * Copy "name" to "sb", expanding any special @-marks as handled by
- * repo_interpret_branch_name(). The result is a non-qualified branch name
- * (so "foo" or "origin/master" instead of "refs/heads/foo" or
- * "refs/remotes/origin/master").
- *
- * Note that the resulting name may not be a syntactically valid refname.
- *
- * If "allowed" is non-zero, restrict the set of allowed expansions. See
- * repo_interpret_branch_name() for details.
- */
-void strbuf_branchname(struct strbuf *sb, const char *name,
-		       unsigned allowed);
-
-/*
- * Like strbuf_branchname() above, but confirm that the result is
- * syntactically valid to be used as a local branch name in refs/heads/.
- *
- * The return value is "0" if the result is valid, and "-1" otherwise.
- */
-int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
-
 typedef int (*char_predicate)(char ch);
 
 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
Signature · 1 line
2.47.1-514-g9b43e7ecc4
#2 lore raw

Re: [PATCH 1/4] refs: move ref name helpers around

Quoted 5 lines · Junio C Hamano

strbuf_branchname(), strbuf_check_{branch,tag}_ref() are helper functions to deal with branch and tag names, and the fact that they happen to use strbuf to hold the name of a branch or a tag is not essential. These functions fit better in the refs API than strbuf API, the latter of which is about string manipulations.

Wow, they are declared in strbuf.h but not even implemented there. So it was doubly confusing. This looks like a nice cleanup.

-Peff

#3 lore raw

Re: [PATCH 1/4] refs: move ref name helpers around

Quoted 10 lines · Jeff King
Quoted 5 lines · Junio C Hamano

strbuf_branchname(), strbuf_check_{branch,tag}_ref() are helper functions to deal with branch and tag names, and the fact that they happen to use strbuf to hold the name of a branch or a tag is not essential. These functions fit better in the refs API than strbuf API, the latter of which is about string manipulations.

Wow, they are declared in strbuf.h but not even implemented there. So it was doubly confusing. This looks like a nice cleanup.

Yup. Another home that may want to adopt them is the object-name API, but I think refs API is good enough, and certainly better than strbuf.

#4 lore raw

PATCH 2/4 refs: drop strbuf_ prefix from helpers

In reply to Junio C Hamano

The helper functions (strbuf_branchname, strbuf_check_branch_ref, and strbuf_check_tag_ref) are about handling branch and tag names, and it is a non-essential fact that these functions use strbuf to hold these names. Rename them to make it clarify that these are more about "ref".

---
 branch.c                   |  2 +-
 builtin/branch.c           | 10 +++++-----
 builtin/check-ref-format.c |  2 +-
 builtin/checkout.c         |  2 +-
 builtin/merge.c            |  2 +-
 builtin/tag.c              |  2 +-
 builtin/worktree.c         |  8 ++++----
 gitweb/gitweb.perl         |  2 +-
 refs.c                     |  8 ++++----
 refs.h                     |  8 ++++----
 10 files changed, 23 insertions(+), 23 deletions(-)
branch.c +1 -1
index 08fa4094d2..58b61831af 100644
@@ -372,7 +372,7 @@ int read_branch_desc(struct strbuf *buf, const char *branch_name)
  */
 int validate_branchname(const char *name, struct strbuf *ref)
 {
-	if (strbuf_check_branch_ref(ref, name)) {
+	if (check_branch_ref(ref, name)) {
 		int code = die_message(_("'%s' is not a valid branch name"), name);
 		advise_if_enabled(ADVICE_REF_SYNTAX,
 				  _("See `man git check-ref-format`"));
builtin/branch.c +5 -5
index fd1611ebf5..17acf598d2 100644
@@ -257,7 +257,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		char *target = NULL;
 		int flags = 0;
 
-		strbuf_branchname(&bname, argv[i], allowed_interpret);
+		copy_branchname(&bname, argv[i], allowed_interpret);
 		free(name);
 		name = mkpathdup(fmt, bname.buf);
 
@@ -579,7 +579,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
 	int recovery = 0, oldref_usage = 0;
 	struct worktree **worktrees = get_worktrees();
 
-	if (strbuf_check_branch_ref(&oldref, oldname)) {
+	if (check_branch_ref(&oldref, oldname)) {
 		/*
 		 * Bad name --- this could be an attempt to rename a
 		 * ref that we used to allow to be created by accident.
@@ -894,7 +894,7 @@ int cmd_branch(int argc,
 				die(_("cannot give description to detached HEAD"));
 			branch_name = head;
 		} else if (argc == 1) {
-			strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
+			copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
 			branch_name = buf.buf;
 		} else {
 			die(_("cannot edit description of more than one branch"));
@@ -933,7 +933,7 @@ int cmd_branch(int argc,
 		if (!argc)
 			branch = branch_get(NULL);
 		else if (argc == 1) {
-			strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
+			copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
 			branch = branch_get(buf.buf);
 		} else
 			die(_("too many arguments to set new upstream"));
@@ -963,7 +963,7 @@ int cmd_branch(int argc,
 		if (!argc)
 			branch = branch_get(NULL);
 		else if (argc == 1) {
-			strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
+			copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
 			branch = branch_get(buf.buf);
 		} else
 			die(_("too many arguments to unset upstream"));
builtin/check-ref-format.c +1 -1
index e86d8ef980..cef1ffe3ce 100644
@@ -42,7 +42,7 @@ static int check_ref_format_branch(const char *arg)
 	int nongit;
 
 	setup_git_directory_gently(&nongit);
-	if (strbuf_check_branch_ref(&sb, arg) ||
+	if (check_branch_ref(&sb, arg) ||
 	    !skip_prefix(sb.buf, "refs/heads/", &name))
 		die("'%s' is not a valid branch name", arg);
 	printf("%s\n", name);
builtin/checkout.c +1 -1
index c449558e66..5e5afa0f26 100644
@@ -742,7 +742,7 @@ static void setup_branch_path(struct branch_info *branch)
 			   &branch->oid, &branch->refname, 0))
 		repo_get_oid_committish(the_repository, branch->name, &branch->oid);
 
-	strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
+	copy_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
 	if (strcmp(buf.buf, branch->name)) {
 		free(branch->name);
 		branch->name = xstrdup(buf.buf);
builtin/merge.c +1 -1
index 84d0f3604b..d0c31d7714 100644
@@ -498,7 +498,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
 	char *found_ref = NULL;
 	int len, early;
 
-	strbuf_branchname(&bname, remote, 0);
+	copy_branchname(&bname, remote, 0);
 	remote = bname.buf;
 
 	oidclr(&branch_head, the_repository->hash_algo);
builtin/tag.c +1 -1
index 8279dccbe0..670e564178 100644
@@ -639,7 +639,7 @@ int cmd_tag(int argc,
 	if (repo_get_oid(the_repository, object_ref, &object))
 		die(_("Failed to resolve '%s' as a valid ref."), object_ref);
 
-	if (strbuf_check_tag_ref(&ref, tag))
+	if (check_tag_ref(&ref, tag))
 		die(_("'%s' is not a valid tag name."), tag);
 
 	if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev))
builtin/worktree.c +4 -4
index fc31d072a6..c68f601358 100644
@@ -432,7 +432,7 @@ static int add_worktree(const char *path, const char *refname,
 	worktrees = NULL;
 
 	/* is 'refname' a branch or commit? */
-	if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
+	if (!opts->detach && !check_branch_ref(&symref, refname) &&
 	    refs_ref_exists(get_main_ref_store(the_repository), symref.buf)) {
 		is_branch = 1;
 		if (!opts->force)
@@ -604,7 +604,7 @@ static void print_preparing_worktree_line(int detach,
 		fprintf_ln(stderr, _("Preparing worktree (new branch '%s')"), new_branch);
 	} else {
 		struct strbuf s = STRBUF_INIT;
-		if (!detach && !strbuf_check_branch_ref(&s, branch) &&
+		if (!detach && !check_branch_ref(&s, branch) &&
 		    refs_ref_exists(get_main_ref_store(the_repository), s.buf))
 			fprintf_ln(stderr, _("Preparing worktree (checking out '%s')"),
 				  branch);
@@ -745,7 +745,7 @@ static char *dwim_branch(const char *path, char **new_branch)
 	char *branchname = xstrndup(s, n);
 	struct strbuf ref = STRBUF_INIT;
 
-	branch_exists = !strbuf_check_branch_ref(&ref, branchname) &&
+	branch_exists = !check_branch_ref(&ref, branchname) &&
 			refs_ref_exists(get_main_ref_store(the_repository),
 					ref.buf);
 	strbuf_release(&ref);
@@ -838,7 +838,7 @@ static int add(int ac, const char **av, const char *prefix)
 		new_branch = new_branch_force;
 
 		if (!opts.force &&
-		    !strbuf_check_branch_ref(&symref, new_branch) &&
+		    !check_branch_ref(&symref, new_branch) &&
 		    refs_ref_exists(get_main_ref_store(the_repository), symref.buf))
 			die_if_checked_out(symref.buf, 0);
 		strbuf_release(&symref);
gitweb/gitweb.perl +1 -1
index b09a8d0523..8cdb0d9b9f 100755
@@ -2094,7 +2094,7 @@ sub format_log_line_html {
         (
             # The output of "git describe", e.g. v2.10.0-297-gf6727b0
             # or hadoop-20160921-113441-20-g094fb7d
-            (?<!-) # see strbuf_check_tag_ref(). Tags can't start with -
+            (?<!-) # see check_tag_ref(). Tags can't start with -
             [A-Za-z0-9.-]+
             (?!\.) # refs can't end with ".", see check_refname_format()
             -g$regex
refs.c +4 -4
index 59a9223d4c..a24bfe3845 100644
@@ -697,7 +697,7 @@ static char *substitute_branch_name(struct repository *r,
 	return NULL;
 }
 
-void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
+void copy_branchname(struct strbuf *sb, const char *name, unsigned allowed)
 {
 	int len = strlen(name);
 	struct interpret_branch_name_options options = {
@@ -711,10 +711,10 @@ void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
 	strbuf_add(sb, name + used, len - used);
 }
 
-int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
+int check_branch_ref(struct strbuf *sb, const char *name)
 {
 	if (startup_info->have_repository)
-		strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
+		copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
 	else
 		strbuf_addstr(sb, name);
 
@@ -733,7 +733,7 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
 	return check_refname_format(sb->buf, 0);
 }
 
-int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
+int check_tag_ref(struct strbuf *sb, const char *name)
 {
 	if (name[0] == '-')
 		return -1;
refs.h +4 -4
index f19b0ad92f..c5280477f0 100644
@@ -191,23 +191,23 @@ char *repo_default_branch_name(struct repository *r, int quiet);
  * If "allowed" is non-zero, restrict the set of allowed expansions. See
  * repo_interpret_branch_name() for details.
  */
-void strbuf_branchname(struct strbuf *sb, const char *name,
+void copy_branchname(struct strbuf *sb, const char *name,
 		       unsigned allowed);
 
 /*
- * Like strbuf_branchname() above, but confirm that the result is
+ * Like copy_branchname() above, but confirm that the result is
  * syntactically valid to be used as a local branch name in refs/heads/.
  *
  * The return value is "0" if the result is valid, and "-1" otherwise.
  */
-int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
+int check_branch_ref(struct strbuf *sb, const char *name);
 
 /*
  * Similar for a tag name in refs/tags/.
  *
  * The return value is "0" if the result is valid, and "-1" otherwise.
  */
-int strbuf_check_tag_ref(struct strbuf *sb, const char *name);
+int check_tag_ref(struct strbuf *sb, const char *name);
 
 /*
  * A ref_transaction represents a collection of reference updates that
Signature · 1 line
2.47.1-514-g9b43e7ecc4
#5 lore raw

Re: [PATCH 2/4] refs: drop strbuf_ prefix from helpers

Quoted 5 lines · Junio C Hamano

The helper functions (strbuf_branchname, strbuf_check_branch_ref, and strbuf_check_tag_ref) are about handling branch and tag names, and it is a non-essential fact that these functions use strbuf to hold these names. Rename them to make it clarify that these are more about "ref".

Sounds good. I wasn't quite sure about the name copy_branchname(), since it actually expands/interprets the name. But the word "interpret" is already used for another similar function, repo_interpret_branch_name().

In fact, this function is a very thin wrapper around it, which made me wonder if it has any value. It looks like the main useful bit is that on error it will copy the name verbatim.

So I guess it is really more like copy_or_expand_branchname(). I don't know if that is really adding much, though. Probably just the name copy_branchname(), coupled with the documentation above the declaration, will be sufficient.

As a side note, repo_interpret_branch_name() is in object-file.[ch], but probably should also be in refs.[ch], as in your first patch. Let's not worry about it for your series, though.

-Peff

#6 lore raw

PATCH 3/4 t5604: do not expect that HEAD is a valid tagname

In reply to Junio C Hamano

09116a1c (refs: loosen over-strict "format" check, 2011-11-16) introduced a test piece (originally in t5700) that expects to be able to create a tag named "HEAD" and then a local clone using the repository as its own reference works correctly. Later, another test piece started using this tag starting at acede2eb (t5700:
document a failure of alternates to affect fetch, 2012-02-11).

But the breakage 09116a1c fixed was not specific to the tagname HEAD. It would have failed exactly the same way if the tag used were foo instead of HEAD.

Before forbidding "git tag" from creating "refs/tags/HEAD", update these tests to use 'foo', not 'HEAD', as the name of the test tag.

---
 t/t5604-clone-reference.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
t/t5604-clone-reference.sh +3 -3
index 9b32db8478..5f5c650ff8 100755
@@ -131,7 +131,7 @@ test_expect_success 'cloning with multiple references drops duplicates' '
 
 test_expect_success 'clone with reference from a tagged repository' '
 	(
-		cd A && git tag -a -m tagged HEAD
+		cd A && git tag -a -m tagged foo
 	) &&
 	git clone --reference=A A I
 '
@@ -156,10 +156,10 @@ test_expect_success 'fetch with incomplete alternates' '
 		git remote add J "file://$base_dir/J" &&
 		GIT_TRACE_PACKET=$U.K git fetch J
 	) &&
-	main_object=$(cd A && git for-each-ref --format="%(objectname)" refs/heads/main) &&
+	main_object=$(git -C A rev-parse --verify refs/heads/main) &&
 	test -s "$U.K" &&
 	! grep " want $main_object" "$U.K" &&
-	tag_object=$(cd A && git for-each-ref --format="%(objectname)" refs/tags/HEAD) &&
+	tag_object=$(git -C A rev-parse --verify refs/tags/foo) &&
 	! grep " want $tag_object" "$U.K"
 '
 
Signature · 1 line
2.47.1-514-g9b43e7ecc4
#7 lore raw

Re: [PATCH 3/4] t5604: do not expect that HEAD is a valid tagname

Quoted 1 line · Junio C Hamano

09116a1c (refs: loosen over-strict "format" check, 2011-11-16)

Nit/confusion: the abbreviated hash is only eight hexes long. I’m used to it being 11 for this project?

Does the age of the commit matter?

#8 lore raw

Re: [PATCH 3/4] t5604: do not expect that HEAD is a valid tagname

Quoted 5 lines · Kristoffer Haugsbakk
Quoted 1 line · Junio C Hamano

09116a1c (refs: loosen over-strict "format" check, 2011-11-16)

Nit/confusion: the abbreviated hash is only eight hexes long. I’m used to it being 11 for this project?

It's not a fixed size. Long ago, the rule was "enough to be unique, but at least 7 (or whatever you set core.abbrev to)". These days that "7" is scaled based on the number of objects in the repo. See e6c587c733 (abbrev: auto size the default abbreviation, 2016-09-30).

So I'd expect 10 digits in a fresh clone of git.git. It's possible Junio has set core.abbrev to something fixed, though.

Quoted 1 line

Does the age of the commit matter?

Nope, it shouldn't.

-Peff

#9 lore raw

Re: [PATCH 3/4] t5604: do not expect that HEAD is a valid tagname

Quoted 5 lines · Jeff King
Quoted 3 lines · Kristoffer Haugsbakk
Quoted 1 line · Junio C Hamano

09116a1c (refs: loosen over-strict "format" check, 2011-11-16)

Quoted 7 lines
Quoted 2 lines

Nit/confusion: the abbreviated hash is only eight hexes long. I’m used to it being 11 for this project?

It's not a fixed size. Long ago, the rule was "enough to be unique, but at least 7 (or whatever you set core.abbrev to)". These days that "7" is scaled based on the number of objects in the repo. See e6c587c733 (abbrev: auto size the default abbreviation, 2016-09-30).

Yes. 11 was based on the output I get as well as what seemed normal in the recent git log.

Quoted 7 lines

So I'd expect 10 digits in a fresh clone of git.git. It's possible Junio has set core.abbrev to something fixed, though.

Quoted 1 line

Does the age of the commit matter?

Nope, it shouldn't.

Makes sense. Thanks.

Signature · 1 line
Kristoffer Haugsbakk
#10 lore raw

Re: [PATCH 3/4] t5604: do not expect that HEAD is a valid tagname

In reply to Jeff King
Quoted 2 lines · Jeff King

So I'd expect 10 digits in a fresh clone of git.git. It's possible Junio has set core.abbrev to something fixed, though.

Thanks.

I have "git one" (and "git who") aliased to this script:

    $ cat $(type --path git-onewho)
    #!/bin/sh
    if sha1=$(git rev-parse -q --verify "$1")
    then
            git show --date=short -s --abbrev=8 --pretty='format:%h (%s, %ad)' "$1"
    else
            git log -1 --format="%aN <%aE>" --author="$1" --all
    fi | tr -d "\012"
    $ git help one
    'one' is aliased to 'onewho'
    $ git help who
    'who' is aliased to 'onewho'

so that I can say "\C-u ESC ! git one HEAD" (or "git one peff") while writing a piece of e-mail. I can drop --abbrev=8 from there but the machinery knows to bust that limit if it is necessary to ensure uniqueness, so ...

#11 lore raw

Re: [PATCH 3/4] t5604: do not expect that HEAD is a valid tagname

Quoted 15 lines · Junio C Hamano

I have "git one" (and "git who") aliased to this script:

    $ cat $(type --path git-onewho)
    #!/bin/sh
    if sha1=$(git rev-parse -q --verify "$1")
    then
            git show --date=short -s --abbrev=8 --pretty='format:%h (%s, %ad)' "$1"
    else
            git log -1 --format="%aN <%aE>" --author="$1" --all
    fi | tr -d "\012"
    $ git help one
    'one' is aliased to 'onewho'
    $ git help who
    'who' is aliased to 'onewho'
Quoted 4 lines

so that I can say "\C-u ESC ! git one HEAD" (or "git one peff") while writing a piece of e-mail. I can drop --abbrev=8 from there but the machinery knows to bust that limit if it is necessary to ensure uniqueness, so ...

Yeah, I have something similar. IMHO a manual --abbrev there is working against your goal.

We do increase that to find a unique answer, but that is not very future-proof; it is only extending by one character taking into account what objects you have _now_. It might not be true for somebody else's repo with more objects, or even your own repo in the near future.

The auto-scaling of core.abbrev done by default these days also suffers from that problem (it can only count how many objects you have now, not how many you expect to have a year from now). But I think our heuristics there give a bit higher safety margin for future-proofing the values.

-Peff

#12 lore raw

Re: [PATCH 3/4] t5604: do not expect that HEAD is a valid tagname

In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano

09116a1c (refs: loosen over-strict "format" check, 2011-11-16) introduced a test piece (originally in t5700) that expects to be able to create a tag named "HEAD" and then a local clone using the repository as its own reference works correctly. Later, another test piece started using this tag starting at acede2eb (t5700:
document a failure of alternates to affect fetch, 2012-02-11).

Quoted 6 lines

But the breakage 09116a1c fixed was not specific to the tagname HEAD. It would have failed exactly the same way if the tag used were foo instead of HEAD.

Before forbidding "git tag" from creating "refs/tags/HEAD", update these tests to use 'foo', not 'HEAD', as the name of the test tag.

Yeah, I think this is worth doing independently. The patch looks good, though...

Quoted 11 lines
t/t5604-clone-reference.sh +1 -1
@@ -131,7 +131,7 @@ test_expect_success 'cloning with multiple references drops duplicates' '
 
 test_expect_success 'clone with reference from a tagged repository' '
 	(
-		cd A && git tag -a -m tagged HEAD
+		cd A && git tag -a -m tagged foo
 	) &&
 	git clone --reference=A A I
 '
@@ -156,10 +156,10 @@ test_expect_success 'fetch with incomplete alternates' '
 		git remote add J "file://$base_dir/J" &&

t/t5604-clone-reference.sh test_expect_success 'fetch with incomplete alternates' ' · line 164

Quoted 10 lines
t/t5604-clone-reference.sh +2 -2
 		GIT_TRACE_PACKET=$U.K git fetch J
 	) &&
-	main_object=$(cd A && git for-each-ref --format="%(objectname)" refs/heads/main) &&
+	main_object=$(git -C A rev-parse --verify refs/heads/main) &&
 	test -s "$U.K" &&
 	! grep " want $main_object" "$U.K" &&
-	tag_object=$(cd A && git for-each-ref --format="%(objectname)" refs/tags/HEAD) &&
+	tag_object=$(git -C A rev-parse --verify refs/tags/foo) &&
 	! grep " want $tag_object" "$U.K"
 '

I notice that you swapped out "cd A && git" for "git -C A" in the second hunk (evne in the line which does not otherwise need to be touched). I think that is good, but is it worth doing the same in the first hunk? That would actually let us drop the subshell.

-Peff

#13 lore raw

PATCH 4/4 tag: "git tag" refuses to use HEAD as a tagname

In reply to Junio C Hamano

Even though the plumbing level allows you to create refs/tags/HEAD and refs/heads/HEAD, doing so makes it confusing within the context of the UI Git Porcelain commands provides. Just like we prevent a branch from getting called "HEAD" at the Porcelain layer (i.e. "git branch" command), teach "git tag" to refuse to create a tag "HEAD".

---
 refs.c         | 2 +-
 t/t7004-tag.sh | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
refs.c +1 -1
index a24bfe3845..01ef2a3093 100644
@@ -735,7 +735,7 @@ int check_branch_ref(struct strbuf *sb, const char *name)
 
 int check_tag_ref(struct strbuf *sb, const char *name)
 {
-	if (name[0] == '-')
+	if (name[0] == '-' || !strcmp(name, "HEAD"))
 		return -1;
 
 	strbuf_reset(sb);
t/t7004-tag.sh +6 -0
index b1316e62f4..2082ce63f7 100755
@@ -91,6 +91,12 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
 	test_must_fail git reflog exists refs/tags/mytag
 '
 
+test_expect_success 'HEAD is forbidden as a tagname' '
+	test_when_finished "git tag -d HEAD || :" &&
+	test_must_fail git tag HEAD &&
+	test_must_fail git tag -a -m "useless" HEAD
+'
+
 test_expect_success 'creating a tag with --create-reflog should create reflog' '
 	git log -1 \
 		--format="format:tag: tagging %h (%s, %cd)%n" \
Signature · 1 line
2.47.1-514-g9b43e7ecc4
#14 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

refs.c int check_branch_ref(struct strbuf *sb, const char *name) · line 741

Quoted 13 lines · Junio C Hamano
refs.c +1 -1
index a24bfe3845..01ef2a3093 100644
@@ -735,7 +735,7 @@ int check_branch_ref(struct strbuf *sb, const char *name)
 
 int check_tag_ref(struct strbuf *sb, const char *name)
 {
-	if (name[0] == '-')
+	if (name[0] == '-' || !strcmp(name, "HEAD"))
 		return -1;
 
 	strbuf_reset(sb);

I was thinking a bit about whether we can spin this a bit wider and disallow creation of any refname that looks like a root ref. But I don't think that would make sense, as root refs are defined as all-uppercase refs living in the root, and disallowing tags that look like this would go way too far.

So I then wondered what a reasonable alternative would be, and the only rule I could come up with was to disallow root refs with "HEAD" in it. But even that doesn't feel reasonable to me.

All to say: singling out "HEAD" feels like a sensible step, and I don't think we should handle root refs more generally here.

The other patches look good to me, as well. Thanks!

Patrick

#15 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

In reply to Junio C Hamano

On Mon, Dec 02, 2024 at 04:07:14PM +0900, Junio C Hamano wrote:

[snip]

refs.c int check_branch_ref(struct strbuf *sb, const char *name) · line 738

Quoted 10 lines
refs.c +1 -1
index a24bfe3845..01ef2a3093 100644
@@ -735,7 +735,7 @@ int check_branch_ref(struct strbuf *sb, const char *name)
 
 int check_tag_ref(struct strbuf *sb, const char *name)
 {
-	if (name[0] == '-')
+	if (name[0] == '-' || !strcmp(name, "HEAD"))

I am wondering whether we should also update "check_refname_format" function to report "refs/heads/HEAD" and "refs/tags/HEAD" is bad ref name.

refs.c int check_branch_ref(struct strbuf *sb, const char *name) · line 741

Quoted 3 lines
refs.c +0 -0
 		return -1;
 
 	strbuf_reset(sb);

Thanks,
Jialuo

#16 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

Quoted 8 lines · shejialuo

refs.c int check_branch_ref(struct strbuf *sb, const char *name) · line 738

Quoted 4 lines

int check_tag_ref(struct strbuf *sb, const char *name) {

refs.c +1 -1
-	if (name[0] == '-')
+	if (name[0] == '-' || !strcmp(name, "HEAD"))

I am wondering whether we should also update "check_refname_format" function to report "refs/heads/HEAD" and "refs/tags/HEAD" is bad ref name.

Check the list archive for the past few days; it was considered and rejected.

Thanks.

#17 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

Quoted 11 lines · Junio C Hamano
Quoted 8 lines · shejialuo

refs.c int check_branch_ref(struct strbuf *sb, const char *name) · line 738

Quoted 4 lines

int check_tag_ref(struct strbuf *sb, const char *name) {

refs.c +1 -1
-	if (name[0] == '-')
+	if (name[0] == '-' || !strcmp(name, "HEAD"))

I am wondering whether we should also update "check_refname_format" function to report "refs/heads/HEAD" and "refs/tags/HEAD" is bad ref name.

Quoted 2 lines

Check the list archive for the past few days; it was considered and rejected.

Agreed, but maybe that is an indication we should discuss that alternative in the commit message. (I had looked for similar justification in the existing "branch" restriction, but couldn't find it, to the point that I wondered if I had hallucinated our reasoning back then).

-Peff

#18 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

Quoted 4 lines · Jeff King
Quoted 4 lines
Quoted 3 lines

I am wondering whether we should also update "check_refname_format" function to report "refs/heads/HEAD" and "refs/tags/HEAD" is bad ref name.

Quoted 8 lines
Quoted 2 lines

Check the list archive for the past few days; it was considered and rejected.

Agreed, but maybe that is an indication we should discuss that alternative in the commit message. (I had looked for similar justification in the existing "branch" restriction, but couldn't find it, to the point that I wondered if I had hallucinated our reasoning back then).

Ah, nevermind. I just read your v2 and I think it covers this nicely.

-Peff

#19 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

In reply to Junio C Hamano
Quoted 5 lines · Junio C Hamano

Even though the plumbing level allows you to create refs/tags/HEAD and refs/heads/HEAD, doing so makes it confusing within the context of the UI Git Porcelain commands provides. Just like we prevent a branch from getting called "HEAD" at the Porcelain layer (i.e. "git branch" command), teach "git tag" to refuse to create a tag "HEAD".

This sounds like a good step in the right direction for me.

From the subject in this patch, I was worried that we were also preventing deletion. However, I have confirmed that we still allow the intuitive deletion of a tag named 'HEAD' with "git tag -d HEAD"; for example, in repositories where such a tag already exists.

Perhaps tangential, but a silly change like this hasn't broken any tests:

builtin/tag.c +2 -0
index 670e564178..b65f56e5b4 100644
@@ -88,6 +88,8 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
 
        for (p = argv; *p; p++) {
                strbuf_reset(&ref);
+               if (!strcmp(*p, "HEAD"))
+                     die("Hi!");
                strbuf_addf(&ref, "refs/tags/%s", *p);
                if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) {
                        error(_("tag '%s' not found."), *p);

Therefore, if the previous seems reasonable, perhaps we should add a test like:

t/t7004-tag.sh +5 -0
@@ -97,6 +97,11 @@ test_expect_success 'HEAD is forbidden as a tagname' '
 	test_must_fail git tag -a -m "useless" HEAD
 '
 
+test_expect_success 'allow deleting a tag named HEAD' '
+	git update-ref refs/tags/HEAD HEAD &&
+	git tag -d HEAD
+'
+
 test_expect_success 'creating a tag with --create-reflog should create reflog' '
 	git log -1 \
 		--format="format:tag: tagging %h (%s, %cd)%n" \
Quoted 26 lines
---
 refs.c         | 2 +-
 t/t7004-tag.sh | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
refs.c +1 -1
index a24bfe3845..01ef2a3093 100644
@@ -735,7 +735,7 @@ int check_branch_ref(struct strbuf *sb, const char *name)
 
 int check_tag_ref(struct strbuf *sb, const char *name)
 {
-	if (name[0] == '-')
+	if (name[0] == '-' || !strcmp(name, "HEAD"))
 		return -1;
 
 	strbuf_reset(sb);
t/t7004-tag.sh +0 -0
index b1316e62f4..2082ce63f7 100755

t/t7004-tag.sh test_expect_success 'creating a tag using default HEAD should succeed' ' · line 95

Quoted 5 lines
t/t7004-tag.sh +2 -0
@@ -91,6 +91,12 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
 	test_must_fail git reflog exists refs/tags/mytag
 '
 
+test_expect_success 'HEAD is forbidden as a tagname' '
+	test_when_finished "git tag -d HEAD || :" &&

I'm not considering this as a test for it :-)

Quoted 10 lines
t/t7004-tag.sh +4 -0
+	test_must_fail git tag HEAD &&
+	test_must_fail git tag -a -m "useless" HEAD
+'
+
 test_expect_success 'creating a tag with --create-reflog should create reflog' '
 	git log -1 \
 		--format="format:tag: tagging %h (%s, %cd)%n" \
Signature · 1 line
2.47.1-514-g9b43e7ecc4
#20 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

t/t7004-tag.sh test_expect_success 'HEAD is forbidden as a tagname' ' · line 103

Quoted 10 lines · Rubén Justo
t/t7004-tag.sh +4 -0
@@ -97,6 +97,11 @@ test_expect_success 'HEAD is forbidden as a tagname' '
 	test_must_fail git tag -a -m "useless" HEAD
 '
 
+test_expect_success 'allow deleting a tag named HEAD' '
+	git update-ref refs/tags/HEAD HEAD &&
+	git tag -d HEAD
+'

This concisely captures exactly what we want to see. The plumbing is still allowed to create something that the worldview given by the Git Porcelain UI may not like (so that people can create different UI) and our tool can still be usable to recover from a mistake of using HEAD as the name of a tag, which we more strongly discourage.

Good idea.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+test_expect_success 'HEAD is forbidden as a tagname' '
+	test_when_finished "git tag -d HEAD || :" &&

I'm not considering this as a test for it :-)

It is not. If "git tag HEAD" get broken in the future, the must-fail steps below may create such a tag, and we want to remove it before moving along.

#21 lore raw

Re: [PATCH 4/4] tag: "git tag" refuses to use HEAD as a tagname

In reply to Junio C Hamano
Quoted 5 lines · Junio C Hamano

Even though the plumbing level allows you to create refs/tags/HEAD and refs/heads/HEAD, doing so makes it confusing within the context of the UI Git Porcelain commands provides. Just like we prevent a branch from getting called "HEAD" at the Porcelain layer (i.e. "git branch" command), teach "git tag" to refuse to create a tag "HEAD".

This looks good and mostly as expected. I do think Rubén's suggestion to add an explicit deletion test might be worth having to future-proof things.

t/t7004-tag.sh test_expect_success 'creating a tag using default HEAD should succeed' ' · line 98

Quoted 9 lines
t/t7004-tag.sh +5 -0
@@ -91,6 +91,12 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
 	test_must_fail git reflog exists refs/tags/mytag
 '
 
+test_expect_success 'HEAD is forbidden as a tagname' '
+	test_when_finished "git tag -d HEAD || :" &&
+	test_must_fail git tag HEAD &&
+	test_must_fail git tag -a -m "useless" HEAD
+'

The test_when_finished surprised me a little, just because we would not expect anything to have been created. I don't think we usually bother with cleaning up failure modes, as it is a losing battle (if the test did not succeed you are only guessing at what mess may have been left behind). But I don't think it's hurting anything, beyond a few wasted cycles to run what should be a noop.

-Peff