git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH 1/1] for-each-ref: add '--no-symbolic' option

From
Eric Freese <ericdfreese@gmail.com>
Date
Sep 7, 2019, 21:36 UTC
Message-ID
<20190907213646.21231-2-ericdfreese@gmail.com>
In-Reply-To
<20190907213646.21231-1-ericdfreese@gmail.com>
Using the new flag will omit symbolic refs from the output.
Without this flag, it is possible to get this behavior by using the
`%(symref)` formatting field name and piping output through grep to
include only those refs that do not output a value for `%(symref)`, but
having this flag is more elegant and intention revealing.
---
 Documentation/git-for-each-ref.txt | 3 +++
 builtin/for-each-ref.c             | 4 +++-
 ref-filter.c                       | 4 ++++
 ref-filter.h                       | 3 ++-
 t/t6302-for-each-ref-filter.sh     | 6 ++++++
 5 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index 6dcd39f6f6..be19111510 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -95,6 +95,9 @@ OPTIONS
 --ignore-case::
 	Sorting and filtering refs are case insensitive.
 
+--no-symbolic::
+	Only list refs that are not symbolic.
+
 FIELD NAMES
 -----------
 
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 465153e853..b71ab2f135 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -18,7 +18,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 {
 	int i;
 	struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
-	int maxcount = 0, icase = 0;
+	int maxcount = 0, icase = 0, nosym = 0;
 	struct ref_array array;
 	struct ref_filter filter;
 	struct ref_format format = REF_FORMAT_INIT;
@@ -46,6 +46,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_CONTAINS(&filter.with_commit, N_("print only refs which 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, "no-symbolic", &nosym, N_("exclude symbolic refs")),
 		OPT_END(),
 	};
 
@@ -72,6 +73,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		sorting = ref_default_sorting();
 	sorting->ignore_case = icase;
 	filter.ignore_case = icase;
+	filter.no_symbolic = nosym;
 
 	filter.name_patterns = argv;
 	filter.match_as_path = 1;
diff --git a/ref-filter.c b/ref-filter.c
index f27cfc8c3e..01beb279dc 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2093,6 +2093,10 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
 		return 0;
 	}
 
+	if (filter->no_symbolic && flag & REF_ISSYMREF) {
+		return 0;
+	}
+
 	/* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
 	kind = filter_ref_kind(filter, refname);
 	if (!(kind & filter->kind))
diff --git a/ref-filter.h b/ref-filter.h
index f1dcff4c6e..23e0d01a33 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -65,7 +65,8 @@ struct ref_filter {
 	unsigned int with_commit_tag_algo : 1,
 		match_as_path : 1,
 		ignore_case : 1,
-		detached : 1;
+		detached : 1,
+		no_symbolic : 1;
 	unsigned int kind,
 		lines;
 	int abbrev,
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
index 35408d53fd..ab9c00fff4 100755
--- a/t/t6302-for-each-ref-filter.sh
+++ b/t/t6302-for-each-ref-filter.sh
@@ -454,4 +454,10 @@ test_expect_success 'validate worktree atom' '
 	test_cmp expect actual
 '
 
+test_expect_success 'filtering with --no-symbolic' '
+	git symbolic-ref refs/symbolic refs/heads/master &&
+	git for-each-ref --format="%(refname)" --no-symbolic >actual &&
+	test_must_fail grep refs/symbolic actual
+'
+
 test_done
-- 
2.23.0
Previous: Eric FreeseNext: Taylor Blau
Message 2 of 13 in “for-each-ref: Add '--no-symbolic' option”
  1. 0/1 for-each-ref: Add '--no-symbolic' optionEric Freese, Sep 7, 2019
  2. 1/1 for-each-ref: add '--no-symbolic' optionEric Freese, Sep 7, 2019
  3. Taylor BlauSep 7, 2019
  4. Taylor BlauSep 7, 2019
  5. Jeff KingSep 8, 2019
  6. Taylor BlauSep 7, 2019
  7. Jeff KingSep 8, 2019
  8. Junio C HamanoSep 8, 2019
  9. Junio C HamanoSep 8, 2019
  10. Eric FreeseSep 9, 2019
  11. Jeff KingSep 9, 2019
  12. Junio C HamanoSep 9, 2019
  13. Taylor BlauSep 7, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.