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>
This commit is contained in:
Karthik Nayak 2024-02-23 11:01:12 +01:00 коммит произвёл Junio C Hamano
Родитель 810f7a1aac
Коммит 33d15b5435
6 изменённых файлов: 79 добавлений и 11 удалений

Просмотреть файл

@ -10,7 +10,7 @@ SYNOPSIS
[verse] [verse]
'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl] 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
[(--sort=<key>)...] [--format=<format>] [(--sort=<key>)...] [--format=<format>]
[ --stdin | <pattern>... ] [--include-root-refs] [ --stdin | <pattern>... ]
[--points-at=<object>] [--points-at=<object>]
[--merged[=<object>]] [--no-merged[=<object>]] [--merged[=<object>]] [--no-merged[=<object>]]
[--contains[=<object>]] [--no-contains[=<object>]] [--contains[=<object>]] [--no-contains[=<object>]]
@ -105,6 +105,9 @@ TAB %(refname)`.
any excluded pattern(s) are shown. Matching is done using the any excluded pattern(s) are shown. Matching is done using the
same rules as `<pattern>` above. same rules as `<pattern>` above.
--include-root-refs::
List root refs (HEAD and pseudorefs) apart from regular refs.
FIELD NAMES FIELD NAMES
----------- -----------

Просмотреть файл

@ -20,10 +20,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
{ {
struct ref_sorting *sorting; struct ref_sorting *sorting;
struct string_list sorting_options = STRING_LIST_INIT_DUP; struct string_list sorting_options = STRING_LIST_INIT_DUP;
int icase = 0; int icase = 0, include_root_refs = 0, from_stdin = 0;
struct ref_filter filter = REF_FILTER_INIT; struct ref_filter filter = REF_FILTER_INIT;
struct ref_format format = REF_FORMAT_INIT; struct ref_format format = REF_FORMAT_INIT;
int from_stdin = 0; unsigned int flags = FILTER_REFS_REGULAR;
struct strvec vec = STRVEC_INIT; struct strvec vec = STRVEC_INIT;
struct option opts[] = { struct option opts[] = {
@ -53,6 +53,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")), OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")), OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")), OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")),
OPT_END(), OPT_END(),
}; };
@ -96,8 +97,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
filter.name_patterns = argv; filter.name_patterns = argv;
} }
if (include_root_refs)
flags |= FILTER_REFS_ROOT_REFS;
filter.match_as_path = 1; filter.match_as_path = 1;
filter_and_format_refs(&filter, FILTER_REFS_REGULAR, sorting, &format); filter_and_format_refs(&filter, flags, sorting, &format);
ref_filter_clear(&filter); ref_filter_clear(&filter);
ref_sorting_release(sorting); ref_sorting_release(sorting);

Просмотреть файл

@ -2622,6 +2622,12 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
each_ref_fn cb, each_ref_fn cb,
void *cb_data) void *cb_data)
{ {
if (filter->kind == FILTER_REFS_KIND_MASK) {
/* In this case, we want to print all refs including root refs. */
return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
cb, cb_data);
}
if (!filter->match_as_path) { if (!filter->match_as_path) {
/* /*
* in this case, the patterns are applied after * in this case, the patterns are applied after
@ -2744,6 +2750,9 @@ static int ref_kind_from_refname(const char *refname)
return ref_kind[i].kind; return ref_kind[i].kind;
} }
if (is_pseudoref(get_main_ref_store(the_repository), refname))
return FILTER_REFS_PSEUDOREFS;
return FILTER_REFS_OTHERS; return FILTER_REFS_OTHERS;
} }
@ -2775,7 +2784,16 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
/* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */ /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
kind = filter_ref_kind(filter, refname); kind = filter_ref_kind(filter, refname);
if (!(kind & filter->kind))
/*
* Generally HEAD refs are printed with special description denoting a rebase,
* detached state and so forth. This is useful when only printing the HEAD ref
* But when it is being printed along with other pseudorefs, it makes sense to
* keep the formatting consistent. So we mask the type to act like a pseudoref.
*/
if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD)
kind = FILTER_REFS_PSEUDOREFS;
else if (!(kind & filter->kind))
return NULL; return NULL;
if (!filter_pattern_match(filter, refname)) if (!filter_pattern_match(filter, refname))
@ -3043,7 +3061,13 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
ret = for_each_fullref_in("refs/tags/", fn, cb_data); ret = for_each_fullref_in("refs/tags/", fn, cb_data);
else if (filter->kind & FILTER_REFS_REGULAR) else if (filter->kind & FILTER_REFS_REGULAR)
ret = for_each_fullref_in_pattern(filter, fn, cb_data); ret = for_each_fullref_in_pattern(filter, fn, cb_data);
if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
/*
* When printing all ref types, HEAD is already included,
* so we don't want to print HEAD again.
*/
if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) &&
(filter->kind & FILTER_REFS_DETACHED_HEAD))
head_ref(fn, cb_data); head_ref(fn, cb_data);
} }

Просмотреть файл

@ -22,7 +22,10 @@
#define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \ #define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
FILTER_REFS_REMOTES | FILTER_REFS_OTHERS) FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
#define FILTER_REFS_DETACHED_HEAD 0x0020 #define FILTER_REFS_DETACHED_HEAD 0x0020
#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD) #define FILTER_REFS_PSEUDOREFS 0x0040
#define FILTER_REFS_ROOT_REFS (FILTER_REFS_DETACHED_HEAD | FILTER_REFS_PSEUDOREFS)
#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \
FILTER_REFS_PSEUDOREFS)
struct atom_value; struct atom_value;
struct ref_sorting; struct ref_sorting;

Просмотреть файл

@ -364,12 +364,15 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator)
break; break;
/* /*
* The files backend only lists references contained in * The files backend only lists references contained in "refs/" unless
* "refs/". We emulate the same behaviour here and thus skip * the root refs are to be included. We emulate the same behaviour here.
* all references that don't start with this prefix.
*/ */
if (!starts_with(iter->ref.refname, "refs/")) if (!starts_with(iter->ref.refname, "refs/") &&
!(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS &&
(is_pseudoref(&iter->refs->base, iter->ref.refname) ||
is_headref(&iter->refs->base, iter->ref.refname)))) {
continue; continue;
}
if (iter->prefix && if (iter->prefix &&
strncmp(iter->prefix, iter->ref.refname, strlen(iter->prefix))) { strncmp(iter->prefix, iter->ref.refname, strlen(iter->prefix))) {

Просмотреть файл

@ -31,6 +31,37 @@ test_expect_success 'setup some history and refs' '
git update-ref refs/odd/spot main git update-ref refs/odd/spot main
' '
test_expect_success '--include-root-refs pattern prints pseudorefs' '
cat >expect <<-\EOF &&
HEAD
ORIG_HEAD
refs/heads/main
refs/heads/side
refs/odd/spot
refs/tags/annotated-tag
refs/tags/doubly-annotated-tag
refs/tags/doubly-signed-tag
refs/tags/four
refs/tags/one
refs/tags/signed-tag
refs/tags/three
refs/tags/two
EOF
git update-ref ORIG_HEAD main &&
git for-each-ref --format="%(refname)" --include-root-refs >actual &&
test_cmp expect actual
'
test_expect_success '--include-root-refs with other patterns' '
cat >expect <<-\EOF &&
HEAD
ORIG_HEAD
EOF
git update-ref ORIG_HEAD main &&
git for-each-ref --format="%(refname)" --include-root-refs "*HEAD" >actual &&
test_cmp expect actual
'
test_expect_success 'filtering with --points-at' ' test_expect_success 'filtering with --points-at' '
cat >expect <<-\EOF && cat >expect <<-\EOF &&
refs/heads/main refs/heads/main