[PATCH v4 07/10] refs: classify HEAD as a root ref

Patrick Steinhardt
2024-05-10T08:48:41Z

[-- Attachment #1: Type: text/plain, Size: 3716 bytes --]

Root refs are those refs that live in the root of the ref hierarchy.
Our old and venerable "HEAD" reference falls into this category, but we
don't yet classify it as such in `is_root_ref()`.

Adapt the function to also treat "HEAD" as a root ref. This change is
safe to do for all current callers:

- `ref_kind_from_refname()` already handles "HEAD" explicitly before
calling `is_root_ref()`.

- The "files" and "reftable" backends explicitly called both
`is_root_ref()` and `is_headref()`.

This also aligns behaviour or `is_root_ref()` and `is_headref()` such
that we also return a trueish value when the ref is a dangling symbolic
ref. As there are no callers of `is_headref()` left afer the refactoring
we absorb it completely into `is_root_ref()`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
refs.c | 9 +--------
refs.h | 5 ++---
refs/files-backend.c | 3 +--
refs/reftable-backend.c | 3 +--
4 files changed, 5 insertions(+), 15 deletions(-)

diff --git a/refs.c b/refs.c
index d63d60a0dc..077ae5756a 100644
--- a/refs.c
+++ b/refs.c
@@ -863,6 +863,7 @@ static int is_root_ref_syntax(const char *refname)
int is_root_ref(struct ref_store *refs, const char *refname)
{
static const char *const irregular_root_refs[] = {
+ "HEAD",
"AUTO_MERGE",
"BISECT_EXPECTED_REV",
"NOTES_MERGE_PARTIAL",
@@ -902,14 +903,6 @@ int is_root_ref(struct ref_store *refs, const char *refname)
return ret;
}

-int is_headref(struct ref_store *refs, const char *refname)
-{
- if (!strcmp(refname, "HEAD"))
- return refs_ref_exists(refs, refname);
-
- return 0;
-}
-
static int is_current_worktree_ref(const char *ref) {
return is_root_ref_syntax(ref) || is_per_worktree_ref(ref);
}
diff --git a/refs.h b/refs.h
index b15ac3835e..f6f4d61e1b 100644
--- a/refs.h
+++ b/refs.h
@@ -1060,7 +1060,8 @@ void update_ref_namespace(enum ref_namespace namespace, char *ref);
*
* - Their name must be all-uppercase or underscores ("_").
*
- * - Their name must end with "_HEAD".
+ * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
+ * ref, as well.
*
* - Their name may not contain a slash.
*
@@ -1079,6 +1080,4 @@ void update_ref_namespace(enum ref_namespace namespace, char *ref);
*/
int is_root_ref(struct ref_store *refs, const char *refname);

-int is_headref(struct ref_store *refs, const char *refname);
-
#endif /* REFS_H */
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 0fcb601444..ea927c516d 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -351,8 +351,7 @@ static void add_pseudoref_and_head_entries(struct ref_store *ref_store,
strbuf_addstr(&refname, de->d_name);

dtype = get_dtype(de, &path, 1);
- if (dtype == DT_REG && (is_root_ref(ref_store, de->d_name) ||
- is_headref(ref_store, de->d_name)))
+ if (dtype == DT_REG && is_root_ref(ref_store, de->d_name))
loose_fill_ref_dir_regular_file(refs, refname.buf, dir);

strbuf_setlen(&refname, dirnamelen);
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 36ab3357a7..7ad4337229 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -354,8 +354,7 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
*/
if (!starts_with(iter->ref.refname, "refs/") &&
!(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS &&
- (is_root_ref(&iter->refs->base, iter->ref.refname) ||
- is_headref(&iter->refs->base, iter->ref.refname)))) {
+ is_root_ref(&iter->refs->base, iter->ref.refname))) {
continue;
}

--
2.45.0