for-each-ref: add new option to include root refs

The git-for-each-ref(1) command doesn't provide a way to print root refs i.e pseudorefs and HEAD with the regular "refs/" prefixed refs. This commit adds a new option "--include-root-refs" to git-for-each-ref(1). When used this would also print pseudorefs and HEAD for the current worktree. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Feb 23, 2024 at 11:01 UTC 33d15b54358d8ec7fc0bd70062ddd1116402c8fe
6 files changed +79 -11
Documentation/git-for-each-ref.txt
+4 -1
@@ -10,7 +10,7 @@ SYNOPSIS
10 [verse]
11 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
12 [(--sort=<key>)...] [--format=<format>]
13 - [ --stdin | <pattern>... ]
13 + [--include-root-refs] [ --stdin | <pattern>... ]
14 [--points-at=<object>]
15 [--merged[=<object>]] [--no-merged[=<object>]]
16 [--contains[=<object>]] [--no-contains[=<object>]]
@@ -105,6 +105,9 @@ TAB %(refname)`.
105 any excluded pattern(s) are shown. Matching is done using the
106 same rules as `<pattern>` above.
107
108 +--include-root-refs::
109 + List root refs (HEAD and pseudorefs) apart from regular refs.
110 +
111 FIELD NAMES
112 -----------
113
builtin/for-each-ref.c
+7 -3
@@ -20,10 +20,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
20 {
21 struct ref_sorting *sorting;
22 struct string_list sorting_options = STRING_LIST_INIT_DUP;
23 - int icase = 0;
23 + int icase = 0, include_root_refs = 0, from_stdin = 0;
24 struct ref_filter filter = REF_FILTER_INIT;
25 struct ref_format format = REF_FORMAT_INIT;
26 - int from_stdin = 0;
26 + unsigned int flags = FILTER_REFS_REGULAR;
27 struct strvec vec = STRVEC_INIT;
28
29 struct option opts[] = {
@@ -53,6 +53,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
53 OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
54 OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
55 OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
56 + OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")),
57 OPT_END(),
58 };
59
@@ -96,8 +97,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
97 filter.name_patterns = argv;
98 }
99
100 + if (include_root_refs)
101 + flags |= FILTER_REFS_ROOT_REFS;
102 +
103 filter.match_as_path = 1;
100 - filter_and_format_refs(&filter, FILTER_REFS_REGULAR, sorting, &format);
104 + filter_and_format_refs(&filter, flags, sorting, &format);
105
106 ref_filter_clear(&filter);
107 ref_sorting_release(sorting);
ref-filter.c
+26 -2
@@ -2622,6 +2622,12 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
2622 each_ref_fn cb,
2623 void *cb_data)
2624 {
2625 + if (filter->kind == FILTER_REFS_KIND_MASK) {
2626 + /* In this case, we want to print all refs including root refs. */
2627 + return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
2628 + cb, cb_data);
2629 + }
2630 +
2631 if (!filter->match_as_path) {
2632 /*
2633 * in this case, the patterns are applied after
@@ -2744,6 +2750,9 @@ static int ref_kind_from_refname(const char *refname)
2750 return ref_kind[i].kind;
2751 }
2752
2753 + if (is_pseudoref(get_main_ref_store(the_repository), refname))
2754 + return FILTER_REFS_PSEUDOREFS;
2755 +
2756 return FILTER_REFS_OTHERS;
2757 }
2758
@@ -2775,7 +2784,16 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
2784
2785 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2786 kind = filter_ref_kind(filter, refname);
2778 - if (!(kind & filter->kind))
2787 +
2788 + /*
2789 + * Generally HEAD refs are printed with special description denoting a rebase,
2790 + * detached state and so forth. This is useful when only printing the HEAD ref
2791 + * But when it is being printed along with other pseudorefs, it makes sense to
2792 + * keep the formatting consistent. So we mask the type to act like a pseudoref.
2793 + */
2794 + if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD)
2795 + kind = FILTER_REFS_PSEUDOREFS;
2796 + else if (!(kind & filter->kind))
2797 return NULL;
2798
2799 if (!filter_pattern_match(filter, refname))
@@ -3043,7 +3061,13 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
3061 ret = for_each_fullref_in("refs/tags/", fn, cb_data);
3062 else if (filter->kind & FILTER_REFS_REGULAR)
3063 ret = for_each_fullref_in_pattern(filter, fn, cb_data);
3046 - if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
3064 +
3065 + /*
3066 + * When printing all ref types, HEAD is already included,
3067 + * so we don't want to print HEAD again.
3068 + */
3069 + if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) &&
3070 + (filter->kind & FILTER_REFS_DETACHED_HEAD))
3071 head_ref(fn, cb_data);
3072 }
3073
ref-filter.h
+4 -1
@@ -22,7 +22,10 @@
22 #define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
23 FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
24 #define FILTER_REFS_DETACHED_HEAD 0x0020
25 -#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD)
25 +#define FILTER_REFS_PSEUDOREFS 0x0040
26 +#define FILTER_REFS_ROOT_REFS (FILTER_REFS_DETACHED_HEAD | FILTER_REFS_PSEUDOREFS)
27 +#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \
28 + FILTER_REFS_PSEUDOREFS)
29
30 struct atom_value;
31 struct ref_sorting;
refs/reftable-backend.c
+7 -4
@@ -364,12 +364,15 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
364 break;
365
366 /*
367 - * The files backend only lists references contained in
368 - * "refs/". We emulate the same behaviour here and thus skip
369 - * all references that don't start with this prefix.
367 + * The files backend only lists references contained in "refs/" unless
368 + * the root refs are to be included. We emulate the same behaviour here.
369 */
371 - if (!starts_with(iter->ref.refname, "refs/"))
370 + if (!starts_with(iter->ref.refname, "refs/") &&
371 + !(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS &&
372 + (is_pseudoref(&iter->refs->base, iter->ref.refname) ||
373 + is_headref(&iter->refs->base, iter->ref.refname)))) {
374 continue;
375 + }
376
377 if (iter->prefix &&
378 strncmp(iter->prefix, iter->ref.refname, strlen(iter->prefix))) {
t/t6302-for-each-ref-filter.sh
+31
@@ -31,6 +31,37 @@ test_expect_success 'setup some history and refs' '
31 git update-ref refs/odd/spot main
32 '
33
34 +test_expect_success '--include-root-refs pattern prints pseudorefs' '
35 + cat >expect <<-\EOF &&
36 + HEAD
37 + ORIG_HEAD
38 + refs/heads/main
39 + refs/heads/side
40 + refs/odd/spot
41 + refs/tags/annotated-tag
42 + refs/tags/doubly-annotated-tag
43 + refs/tags/doubly-signed-tag
44 + refs/tags/four
45 + refs/tags/one
46 + refs/tags/signed-tag
47 + refs/tags/three
48 + refs/tags/two
49 + EOF
50 + git update-ref ORIG_HEAD main &&
51 + git for-each-ref --format="%(refname)" --include-root-refs >actual &&
52 + test_cmp expect actual
53 +'
54 +
55 +test_expect_success '--include-root-refs with other patterns' '
56 + cat >expect <<-\EOF &&
57 + HEAD
58 + ORIG_HEAD
59 + EOF
60 + git update-ref ORIG_HEAD main &&
61 + git for-each-ref --format="%(refname)" --include-root-refs "*HEAD" >actual &&
62 + test_cmp expect actual
63 +'
64 +
65 test_expect_success 'filtering with --points-at' '
66 cat >expect <<-\EOF &&
67 refs/heads/main