Skip to content

Commit f1701f2

Browse files
pks-tgitster
authored andcommitted
ref-filter: properly distinuish pseudo and root refs
The ref-filter interfaces currently define root refs as either a detached HEAD or a pseudo ref. Pseudo refs aren't root refs though, so let's properly distinguish those ref types. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 993d57e commit f1701f2

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

builtin/for-each-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
9898
}
9999

100100
if (include_root_refs)
101-
flags |= FILTER_REFS_ROOT_REFS;
101+
flags |= FILTER_REFS_ROOT_REFS | FILTER_REFS_DETACHED_HEAD;
102102

103103
filter.match_as_path = 1;
104104
filter_and_format_refs(&filter, flags, sorting, &format);

ref-filter.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,7 +2628,7 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
26282628
each_ref_fn cb,
26292629
void *cb_data)
26302630
{
2631-
if (filter->kind == FILTER_REFS_KIND_MASK) {
2631+
if (filter->kind & FILTER_REFS_ROOT_REFS) {
26322632
/* In this case, we want to print all refs including root refs. */
26332633
return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
26342634
cb, cb_data);
@@ -2756,8 +2756,10 @@ static int ref_kind_from_refname(const char *refname)
27562756
return ref_kind[i].kind;
27572757
}
27582758

2759-
if (is_root_ref(refname))
2759+
if (is_pseudo_ref(refname))
27602760
return FILTER_REFS_PSEUDOREFS;
2761+
if (is_root_ref(refname))
2762+
return FILTER_REFS_ROOT_REFS;
27612763

27622764
return FILTER_REFS_OTHERS;
27632765
}
@@ -2794,11 +2796,11 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
27942796
/*
27952797
* Generally HEAD refs are printed with special description denoting a rebase,
27962798
* detached state and so forth. This is useful when only printing the HEAD ref
2797-
* But when it is being printed along with other pseudorefs, it makes sense to
2798-
* keep the formatting consistent. So we mask the type to act like a pseudoref.
2799+
* But when it is being printed along with other root refs, it makes sense to
2800+
* keep the formatting consistent. So we mask the type to act like a root ref.
27992801
*/
2800-
if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD)
2801-
kind = FILTER_REFS_PSEUDOREFS;
2802+
if (filter->kind & FILTER_REFS_ROOT_REFS && kind == FILTER_REFS_DETACHED_HEAD)
2803+
kind = FILTER_REFS_ROOT_REFS;
28022804
else if (!(kind & filter->kind))
28032805
return NULL;
28042806

@@ -3072,7 +3074,7 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
30723074
* When printing all ref types, HEAD is already included,
30733075
* so we don't want to print HEAD again.
30743076
*/
3075-
if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) &&
3077+
if (!ret && !(filter->kind & FILTER_REFS_ROOT_REFS) &&
30763078
(filter->kind & FILTER_REFS_DETACHED_HEAD))
30773079
head_ref(fn, cb_data);
30783080
}

ref-filter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
2424
#define FILTER_REFS_DETACHED_HEAD 0x0020
2525
#define FILTER_REFS_PSEUDOREFS 0x0040
26-
#define FILTER_REFS_ROOT_REFS (FILTER_REFS_DETACHED_HEAD | FILTER_REFS_PSEUDOREFS)
26+
#define FILTER_REFS_ROOT_REFS 0x0080
2727
#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \
28-
FILTER_REFS_PSEUDOREFS)
28+
FILTER_REFS_PSEUDOREFS | FILTER_REFS_ROOT_REFS)
2929

3030
struct atom_value;
3131
struct ref_sorting;

refs.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -844,24 +844,8 @@ int is_per_worktree_ref(const char *refname)
844844
starts_with(refname, "refs/rewritten/");
845845
}
846846

847-
static int is_pseudo_ref(const char *refname)
847+
int is_pseudo_ref(const char *refname)
848848
{
849-
/*
850-
* Pseudorefs are refs that have different semantics compared to
851-
* "normal" refs. These refs can thus not be stored in the ref backend,
852-
* but must always be accessed via the filesystem. The following refs
853-
* are pseudorefs:
854-
*
855-
* - FETCH_HEAD may contain multiple object IDs, and each one of them
856-
* carries additional metadata like where it came from.
857-
*
858-
* - MERGE_HEAD may contain multiple object IDs when merging multiple
859-
* heads.
860-
*
861-
* Reading, writing or deleting references must consistently go either
862-
* through the filesystem (pseudorefs) or through the reference
863-
* backend (normal ones).
864-
*/
865849
static const char * const pseudo_refs[] = {
866850
"FETCH_HEAD",
867851
"MERGE_HEAD",

refs.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,4 +1080,22 @@ void update_ref_namespace(enum ref_namespace namespace, char *ref);
10801080
*/
10811081
int is_root_ref(const char *refname);
10821082

1083+
/*
1084+
* Pseudorefs are refs that have different semantics compared to
1085+
* "normal" refs. These refs can thus not be stored in the ref backend,
1086+
* but must always be accessed via the filesystem. The following refs
1087+
* are pseudorefs:
1088+
*
1089+
* - FETCH_HEAD may contain multiple object IDs, and each one of them
1090+
* carries additional metadata like where it came from.
1091+
*
1092+
* - MERGE_HEAD may contain multiple object IDs when merging multiple
1093+
* heads.
1094+
*
1095+
* Reading, writing or deleting references must consistently go either
1096+
* through the filesystem (pseudorefs) or through the reference
1097+
* backend (normal ones).
1098+
*/
1099+
int is_pseudo_ref(const char *refname);
1100+
10831101
#endif /* REFS_H */

0 commit comments

Comments
 (0)