]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: classify HEAD as a root ref
authorPatrick Steinhardt <ps@pks.im>
Wed, 15 May 2024 06:50:56 +0000 (08:50 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 May 2024 14:30:52 +0000 (07:30 -0700)
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 call both
    `is_root_ref()` and `is_headref()` together.

This also aligns behaviour or `is_root_ref()` and `is_headref()` such
that we stop checking for ref existence. This changes semantics for our
backends:

  - In the reftable backend we already know that the ref must exist
    because `is_headref()` is called as part of the ref iterator. The
    existence check is thus redundant, and the change is safe to do.

  - In the files backend we use it when populating root refs, where we
    would skip adding the "HEAD" file if it was not possible to resolve
    it. The new behaviour is to instead mark "HEAD" as broken, which
    will cause us to emit warnings in various places.

As there are no callers of `is_headref()` left afer the refactoring, we
can absorb it completely into `is_root_ref()`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
refs.h
refs/files-backend.c
refs/reftable-backend.c

diff --git a/refs.c b/refs.c
index 4fec29e6608077f2f192953a88b3d1422b53b74a..9fb1061d52a4d5721a1114d2b273a49001f69725 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -859,6 +859,7 @@ static int is_root_ref_syntax(const char *refname)
 int is_root_ref(const char *refname)
 {
        static const char *const irregular_root_refs[] = {
+               "HEAD",
                "AUTO_MERGE",
                "BISECT_EXPECTED_REV",
                "NOTES_MERGE_PARTIAL",
@@ -880,14 +881,6 @@ int is_root_ref(const char *refname)
        return 0;
 }
 
-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 8a574a22c71379fb74502361fb90fe056584add4..8489b45265c49d59668d9752e9e8e8da952d9fea 100644 (file)
--- 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(const char *refname);
 
-int is_headref(struct ref_store *refs, const char *refname);
-
 #endif /* REFS_H */
index 06240ce3271619470f8d9647312fc8ea74984ad7..6f9a63159267c4689190cd188cb69f5715174ee2 100644 (file)
@@ -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(de->d_name) ||
-                                       is_headref(ref_store, de->d_name)))
+               if (dtype == DT_REG && is_root_ref(de->d_name))
                        loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
 
                strbuf_setlen(&refname, dirnamelen);
index c11ba95736322bdbddccf9737c424bb88775ec0e..b6668e7c7eb67f86b44694a6a2bcede4b415a975 100644 (file)
@@ -356,8 +356,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->ref.refname) ||
-                     is_headref(&iter->refs->base, iter->ref.refname)))) {
+                     is_root_ref(iter->ref.refname))) {
                        continue;
                }