Skip to content

Commit 33d15b5

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 810f7a1 commit 33d15b5

File tree

6 files changed

+79
-11
lines changed

6 files changed

+79
-11
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
1212
[(--sort=<key>)...] [--format=<format>]
13-
[ --stdin | <pattern>... ]
13+
[--include-root-refs] [ --stdin | <pattern>... ]
1414
[--points-at=<object>]
1515
[--merged[=<object>]] [--no-merged[=<object>]]
1616
[--contains[=<object>]] [--no-contains[=<object>]]
@@ -105,6 +105,9 @@ TAB %(refname)`.
105105
any excluded pattern(s) are shown. Matching is done using the
106106
same rules as `<pattern>` above.
107107

108+
--include-root-refs::
109+
List root refs (HEAD and pseudorefs) apart from regular refs.
110+
108111
FIELD NAMES
109112
-----------
110113

builtin/for-each-ref.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
2020
{
2121
struct ref_sorting *sorting;
2222
struct string_list sorting_options = STRING_LIST_INIT_DUP;
23-
int icase = 0;
23+
int icase = 0, include_root_refs = 0, from_stdin = 0;
2424
struct ref_filter filter = REF_FILTER_INIT;
2525
struct ref_format format = REF_FORMAT_INIT;
26-
int from_stdin = 0;
26+
unsigned int flags = FILTER_REFS_REGULAR;
2727
struct strvec vec = STRVEC_INIT;
2828

2929
struct option opts[] = {
@@ -53,6 +53,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
5353
OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
5454
OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
5555
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")),
5657
OPT_END(),
5758
};
5859

@@ -96,8 +97,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
9697
filter.name_patterns = argv;
9798
}
9899

100+
if (include_root_refs)
101+
flags |= FILTER_REFS_ROOT_REFS;
102+
99103
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);
101105

102106
ref_filter_clear(&filter);
103107
ref_sorting_release(sorting);

ref-filter.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,12 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
26222622
each_ref_fn cb,
26232623
void *cb_data)
26242624
{
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+
26252631
if (!filter->match_as_path) {
26262632
/*
26272633
* in this case, the patterns are applied after
@@ -2744,6 +2750,9 @@ static int ref_kind_from_refname(const char *refname)
27442750
return ref_kind[i].kind;
27452751
}
27462752

2753+
if (is_pseudoref(get_main_ref_store(the_repository), refname))
2754+
return FILTER_REFS_PSEUDOREFS;
2755+
27472756
return FILTER_REFS_OTHERS;
27482757
}
27492758

@@ -2775,7 +2784,16 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
27752784

27762785
/* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
27772786
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))
27792797
return NULL;
27802798

27812799
if (!filter_pattern_match(filter, refname))
@@ -3043,7 +3061,13 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
30433061
ret = for_each_fullref_in("refs/tags/", fn, cb_data);
30443062
else if (filter->kind & FILTER_REFS_REGULAR)
30453063
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))
30473071
head_ref(fn, cb_data);
30483072
}
30493073

ref-filter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
#define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
2323
FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
2424
#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)
2629

2730
struct atom_value;
2831
struct ref_sorting;

refs/reftable-backend.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,15 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
364364
break;
365365

366366
/*
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.
370369
*/
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)))) {
372374
continue;
375+
}
373376

374377
if (iter->prefix &&
375378
strncmp(iter->prefix, iter->ref.refname, strlen(iter->prefix))) {

t/t6302-for-each-ref-filter.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@ test_expect_success 'setup some history and refs' '
3131
git update-ref refs/odd/spot main
3232
'
3333

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+
3465
test_expect_success 'filtering with --points-at' '
3566
cat >expect <<-\EOF &&
3667
refs/heads/main

0 commit comments

Comments
 (0)