Skip to content

Commit 1eba224

Browse files
KarthikNayakgitster
authored andcommitted
refs: introduce is_pseudoref() and is_headref()
Introduce two new functions `is_pseudoref()` and `is_headref()`. This provides the necessary functionality for us to add pseudorefs and HEAD to the loose ref cache in the files backend, allowing us to build tooling to print these refs. The `is_pseudoref()` function internally calls `is_pseudoref_syntax()` but adds onto it by also checking to ensure that the pseudoref either ends with a "_HEAD" suffix or matches a list of exceptions. After which we also parse the contents of the pseudoref to ensure that it conforms to the ref format. We cannot directly add the new syntax checks to `is_pseudoref_syntax()` because the function is also used by `is_current_worktree_ref()` and making it stricter to match only known pseudorefs might have unintended consequences due to files like 'BISECT_START' which isn't a pseudoref but sometimes contains object ID. Keeping this in mind, we leave `is_pseudoref_syntax()` as is and create `is_pseudoref()` which is stricter. Ideally we'd want to move the new syntax checks to `is_pseudoref_syntax()` but a prerequisite for this would be to actually remove the exception list by converting those pseudorefs to also contain a '_HEAD' suffix and perhaps move bisect related files like 'BISECT_START' to a new directory similar to the 'rebase-merge' directory. Helped-by: Jeff King <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c684b58 commit 1eba224

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

refs.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,47 @@ static int is_pseudoref_syntax(const char *refname)
860860
return 1;
861861
}
862862

863+
int is_pseudoref(struct ref_store *refs, const char *refname)
864+
{
865+
static const char *const irregular_pseudorefs[] = {
866+
"AUTO_MERGE",
867+
"BISECT_EXPECTED_REV",
868+
"NOTES_MERGE_PARTIAL",
869+
"NOTES_MERGE_REF",
870+
"MERGE_AUTOSTASH",
871+
};
872+
struct object_id oid;
873+
size_t i;
874+
875+
if (!is_pseudoref_syntax(refname))
876+
return 0;
877+
878+
if (ends_with(refname, "_HEAD")) {
879+
refs_resolve_ref_unsafe(refs, refname,
880+
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
881+
&oid, NULL);
882+
return !is_null_oid(&oid);
883+
}
884+
885+
for (i = 0; i < ARRAY_SIZE(irregular_pseudorefs); i++)
886+
if (!strcmp(refname, irregular_pseudorefs[i])) {
887+
refs_resolve_ref_unsafe(refs, refname,
888+
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
889+
&oid, NULL);
890+
return !is_null_oid(&oid);
891+
}
892+
893+
return 0;
894+
}
895+
896+
int is_headref(struct ref_store *refs, const char *refname)
897+
{
898+
if (!strcmp(refname, "HEAD"))
899+
return refs_ref_exists(refs, refname);
900+
901+
return 0;
902+
}
903+
863904
static int is_current_worktree_ref(const char *ref) {
864905
return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref);
865906
}

refs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,4 +1023,7 @@ extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
10231023
*/
10241024
void update_ref_namespace(enum ref_namespace namespace, char *ref);
10251025

1026+
int is_pseudoref(struct ref_store *refs, const char *refname);
1027+
int is_headref(struct ref_store *refs, const char *refname);
1028+
10261029
#endif /* REFS_H */

0 commit comments

Comments
 (0)