@@ -32,7 +32,8 @@ OPTIONS
If one or more patterns are given, only refs are shown that
match against at least one pattern, either using fnmatch(3) or
literally, in the latter case matching completely or from the
- beginning up to a slash.
+ beginning up to a slash. If an empty string is provided all refs
+ are printed, including HEAD and pseudorefs.
--stdin::
If `--stdin` is supplied, then the list of patterns is read from
@@ -25,6 +25,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
struct ref_format format = REF_FORMAT_INIT;
int from_stdin = 0;
struct strvec vec = STRVEC_INIT;
+ unsigned int flags = FILTER_REFS_ALL;
struct option opts[] = {
OPT_BIT('s', "shell", &format.quote_style,
@@ -93,11 +94,29 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
/* vec.v is NULL-terminated, just like 'argv'. */
filter.name_patterns = vec.v;
} else {
+ size_t i;
+
filter.name_patterns = argv;
+
+ /*
+ * Search for any empty string pattern, if it exists then we
+ * print all refs without any filtering.
+ */
+ i = 0;
+ while (argv[i]) {
+ if (!argv[i][0]) {
+ flags = FILTER_REFS_NO_FILTER;
+ /* doing this removes any pattern from being matched */
+ filter.name_patterns[0] = NULL;
+ break;
+ }
+
+ i++;
+ }
}
filter.match_as_path = 1;
- filter_and_format_refs(&filter, FILTER_REFS_ALL, sorting, &format);
+ filter_and_format_refs(&filter, flags, sorting, &format);
ref_filter_clear(&filter);
ref_sorting_release(sorting);
@@ -2622,6 +2622,11 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
each_ref_fn cb,
void *cb_data)
{
+ if (filter->kind & FILTER_REFS_NO_FILTER) {
+ return refs_for_each_all_refs(
+ get_main_ref_store(the_repository), cb, cb_data);
+ }
+
if (!filter->match_as_path) {
/*
* in this case, the patterns are applied after
@@ -2775,8 +2780,12 @@ 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. */
kind = filter_ref_kind(filter, refname);
- if (!(kind & filter->kind))
+ if (filter->kind & FILTER_REFS_NO_FILTER) {
+ if (kind == FILTER_REFS_DETACHED_HEAD)
+ kind = FILTER_REFS_OTHERS;
+ } else if (!(kind & filter->kind)) {
return NULL;
+ }
if (!filter_pattern_match(filter, refname))
return NULL;
@@ -3041,7 +3050,7 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
ret = for_each_fullref_in("refs/remotes/", fn, cb_data);
else if (filter->kind == FILTER_REFS_TAGS)
ret = for_each_fullref_in("refs/tags/", fn, cb_data);
- else if (filter->kind & FILTER_REFS_ALL)
+ else if (filter->kind & FILTER_REFS_ALL || filter->kind & FILTER_REFS_NO_FILTER)
ret = for_each_fullref_in_pattern(filter, fn, cb_data);
if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
head_ref(fn, cb_data);
@@ -22,7 +22,9 @@
#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
#define FILTER_REFS_DETACHED_HEAD 0x0020
-#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
+#define FILTER_REFS_NO_FILTER 0x0040
+#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD | \
+ FILTER_REFS_NO_FILTER)
struct atom_value;
struct ref_sorting;
@@ -31,6 +31,40 @@ test_expect_success 'setup some history and refs' '
git update-ref refs/odd/spot main
'
+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
+
+test_expect_success 'empty pattern prints pseudorefs' '
+ git update-ref ORIG_HEAD main &&
+ git for-each-ref --format="%(refname)" "" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'empty pattern with other patterns' '
+ git update-ref ORIG_HEAD main &&
+ git for-each-ref --format="%(refname)" "" "refs/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'empty pattern towards the end' '
+ git update-ref ORIG_HEAD main &&
+ git for-each-ref --format="%(refname)" "refs/" "" >actual &&
+ test_cmp expect actual
+'
+
test_expect_success 'filtering with --points-at' '
cat >expect <<-\EOF &&
refs/heads/main
When the user uses an empty string pattern (""), we don't match any refs in git-for-each-ref(1). This is because in git-for-each-ref(1), we use path based matching and an empty string doesn't match any path. In this commit we change this behavior by making empty string pattern match all references. This is done by introducing a new flag `FILTER_REFS_NO_FILTER` in `ref-filter.c`, which uses the newly introduced `refs_for_each_all_refs()` function to iterate over all the refs in the repository. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> --- Documentation/git-for-each-ref.txt | 3 ++- builtin/for-each-ref.c | 21 +++++++++++++++++- ref-filter.c | 13 ++++++++++-- ref-filter.h | 4 +++- t/t6302-for-each-ref-filter.sh | 34 ++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 5 deletions(-)