Skip to content

Commit 31951c2

Browse files
pks-tgitster
authored andcommitted
refs: classify HEAD as a root ref
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent afcd067 commit 31951c2

File tree

4 files changed

+5
-15
lines changed

4 files changed

+5
-15
lines changed

refs.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ static int is_root_ref_syntax(const char *refname)
859859
int is_root_ref(const char *refname)
860860
{
861861
static const char *const irregular_root_refs[] = {
862+
"HEAD",
862863
"AUTO_MERGE",
863864
"BISECT_EXPECTED_REV",
864865
"NOTES_MERGE_PARTIAL",
@@ -880,14 +881,6 @@ int is_root_ref(const char *refname)
880881
return 0;
881882
}
882883

883-
int is_headref(struct ref_store *refs, const char *refname)
884-
{
885-
if (!strcmp(refname, "HEAD"))
886-
return refs_ref_exists(refs, refname);
887-
888-
return 0;
889-
}
890-
891884
static int is_current_worktree_ref(const char *ref) {
892885
return is_root_ref_syntax(ref) || is_per_worktree_ref(ref);
893886
}

refs.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,8 @@ void update_ref_namespace(enum ref_namespace namespace, char *ref);
10601060
*
10611061
* - Their name must be all-uppercase or underscores ("_").
10621062
*
1063-
* - Their name must end with "_HEAD".
1063+
* - Their name must end with "_HEAD". As a special rule, "HEAD" is a root
1064+
* ref, as well.
10641065
*
10651066
* - Their name may not contain a slash.
10661067
*
@@ -1079,6 +1080,4 @@ void update_ref_namespace(enum ref_namespace namespace, char *ref);
10791080
*/
10801081
int is_root_ref(const char *refname);
10811082

1082-
int is_headref(struct ref_store *refs, const char *refname);
1083-
10841083
#endif /* REFS_H */

refs/files-backend.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ static void add_pseudoref_and_head_entries(struct ref_store *ref_store,
351351
strbuf_addstr(&refname, de->d_name);
352352

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

358357
strbuf_setlen(&refname, dirnamelen);

refs/reftable-backend.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
356356
*/
357357
if (!starts_with(iter->ref.refname, "refs/") &&
358358
!(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS &&
359-
(is_root_ref(iter->ref.refname) ||
360-
is_headref(&iter->refs->base, iter->ref.refname)))) {
359+
is_root_ref(iter->ref.refname))) {
361360
continue;
362361
}
363362

0 commit comments

Comments
 (0)