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

[RFC PATCH 1/1] for-each-ref: do not output empty lines

From
Eric Freese <ericdfreese@gmail.com>
Date
Sep 10, 2019, 05:17 UTC
Message-ID
<20190910051705.2765-2-ericdfreese@gmail.com>
In-Reply-To
<20190910051705.2765-1-ericdfreese@gmail.com>

If the format string expands to an empty string for a given ref, do not print the empty line.

This is helpful when wanting to print only certain kinds of refs that you can't already filter for.

For example, to exclude symbolic refs, use format string:
  "%(if)%(symref)%(then)%(else)%(refname)%(end)"
Or to include only symbolic refs, use:
  "%(if)%(symref)%(then)%(refname)%(end)"
Signed-off-by: Eric Freese <ericdfreese@gmail.com>
---
 ref-filter.c            | 3 ++-
 t/t6300-for-each-ref.sh | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 7338cfc671..b5b3c4ddf6 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2395,9 +2395,10 @@ void show_ref_array_item(struct ref_array_item *info,
 	if (format_ref_array_item(info, format, &final_buf, &error_buf))
 		die("%s", error_buf.buf);
 	fwrite(final_buf.buf, 1, final_buf.len, stdout);
+	if (final_buf.len)
+		putchar('\n');
 	strbuf_release(&error_buf);
 	strbuf_release(&final_buf);
-	putchar('\n');
 }
 
 void pretty_print_ref(const char *name, const struct object_id *oid,
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 9c910ce746..1f070e63e2 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -42,7 +42,7 @@ test_atom() {
 		 sym) ref=refs/heads/sym ;;
 		   *) ref=$1 ;;
 	esac
-	printf '%s\n' "$3" >expected
+	{ test -n "$3" && printf '%s\n' "$3"; } >expected
 	test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
 		git for-each-ref --format='%($2)' $ref >actual &&
 		sanitize_pgp <actual >actual.clean &&
@@ -313,7 +313,7 @@ test_expect_success 'Check format of strftime-local date fields' '
 '
 
 test_expect_success 'exercise strftime with odd fields' '
-	echo >expected &&
+	>expected &&
 	git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
 	test_cmp expected actual &&
 	long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
-- 
2.23.0
Previous: Eric FreeseNext: Junio C Hamano
Message 2 of 5 in “for-each-ref: do not output empty lines”
  1. 0/1 for-each-ref: do not output empty linesEric Freese, Sep 10, 2019
  2. 1/1 for-each-ref: do not output empty linesEric Freese, Sep 10, 2019
  3. Junio C HamanoSep 10, 2019
  4. Junio C HamanoSep 10, 2019
  5. Jeff KingSep 10, 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.