branch, for-each-ref, tag: add option to omit empty lines

If the given format string expands to the empty string, a newline is still printed. This makes using the output linewise more tedious. For example, git update-ref --stdin does not accept empty lines. Add options to "git branch", "git for-each-ref", and "git tag" to not print these empty lines. The default behavior remains the same. Signed-off-by: Øystein Walle <oystwa@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Øystein Walle committed Apr 7, 2023 at 19:53 UTC aabfdc9514a51d8d9a2f3afbc29e5044ccf7191b
9 files changed +75 -4
Documentation/git-branch.txt
+4
@@ -156,6 +156,10 @@ in another worktree linked to the same repository.
156 --ignore-case::
157 Sorting and filtering branches are case insensitive.
158
159 +--omit-empty::
160 + Do not print a newline after formatted refs where the format expands
161 + to the empty string.
162 +
163 --column[=<options>]::
164 --no-column::
165 Display branch listing in columns. See configuration variable
Documentation/git-for-each-ref.txt
+4
@@ -93,6 +93,10 @@ OPTIONS
93 --ignore-case::
94 Sorting and filtering refs are case insensitive.
95
96 +--omit-empty::
97 + Do not print a newline after formatted refs where the format expands
98 + to the empty string.
99 +
100 FIELD NAMES
101 -----------
102
Documentation/git-tag.txt
+4
@@ -131,6 +131,10 @@ options for details.
131 --ignore-case::
132 Sorting and filtering tags are case insensitive.
133
134 +--omit-empty::
135 + Do not print a newline after formatted refs where the format expands
136 + to the empty string.
137 +
138 --column[=<options>]::
139 --no-column::
140 Display tag listing in columns. See configuration variable
builtin/branch.c
+5 -1
@@ -41,6 +41,7 @@ static const char *head;
41 static struct object_id head_oid;
42 static int recurse_submodules = 0;
43 static int submodule_propagate_branches = 0;
44 +static int omit_empty = 0;
45
46 static int branch_use_color = -1;
47 static char branch_colors[][COLOR_MAXLEN] = {
@@ -461,7 +462,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
462 string_list_append(output, out.buf);
463 } else {
464 fwrite(out.buf, 1, out.len, stdout);
464 - putchar('\n');
465 + if (out.len || !omit_empty)
466 + putchar('\n');
467 }
468 }
469
@@ -670,6 +672,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
672 OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
673 OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
674 OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
675 + OPT_BOOL(0, "omit-empty", &omit_empty,
676 + N_("do not output a newline after empty formatted refs")),
677 OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
678 OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
679 OPT_BOOL('l', "list", &list, N_("list branch names")),
builtin/for-each-ref.c
+5 -2
@@ -19,7 +19,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
19 int i;
20 struct ref_sorting *sorting;
21 struct string_list sorting_options = STRING_LIST_INIT_DUP;
22 - int maxcount = 0, icase = 0;
22 + int maxcount = 0, icase = 0, omit_empty = 0;
23 struct ref_array array;
24 struct ref_filter filter;
25 struct ref_format format = REF_FORMAT_INIT;
@@ -35,6 +35,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
35 N_("quote placeholders suitably for python"), QUOTE_PYTHON),
36 OPT_BIT(0 , "tcl", &format.quote_style,
37 N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
38 + OPT_BOOL(0, "omit-empty", &omit_empty,
39 + N_("do not output a newline after empty formatted refs")),
40
41 OPT_GROUP(""),
42 OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
@@ -88,7 +90,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
90 if (format_ref_array_item(array.items[i], &format, &output, &err))
91 die("%s", err.buf);
92 fwrite(output.buf, 1, output.len, stdout);
91 - putchar('\n');
93 + if (output.len || !omit_empty)
94 + putchar('\n');
95 }
96
97 strbuf_release(&err);
builtin/tag.c
+5 -1
@@ -37,6 +37,7 @@ static const char * const git_tag_usage[] = {
37 static unsigned int colopts;
38 static int force_sign_annotate;
39 static int config_sign_tag = -1; /* unspecified */
40 +static int omit_empty = 0;
41
42 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
43 struct ref_format *format)
@@ -74,7 +75,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
75 if (format_ref_array_item(array.items[i], format, &output, &err))
76 die("%s", err.buf);
77 fwrite(output.buf, 1, output.len, stdout);
77 - putchar('\n');
78 + if (output.len || !omit_empty)
79 + putchar('\n');
80 }
81
82 strbuf_release(&err);
@@ -473,6 +475,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
475 OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
476 OPT_MERGED(&filter, N_("print only tags that are merged")),
477 OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
478 + OPT_BOOL(0, "omit-empty", &omit_empty,
479 + N_("do not output a newline after empty formatted refs")),
480 OPT_REF_SORT(&sorting_options),
481 {
482 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
t/t3203-branch-output.sh
+24
@@ -341,6 +341,30 @@ test_expect_success 'git branch with --format=%(rest) must fail' '
341 test_must_fail git branch --format="%(rest)" >actual
342 '
343
344 +test_expect_success 'git branch --format --omit-empty' '
345 + cat >expect <<-\EOF &&
346 + Refname is (HEAD detached from fromtag)
347 + Refname is refs/heads/ambiguous
348 + Refname is refs/heads/branch-one
349 + Refname is refs/heads/branch-two
350 +
351 + Refname is refs/heads/ref-to-branch
352 + Refname is refs/heads/ref-to-remote
353 + EOF
354 + git branch --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
355 + test_cmp expect actual &&
356 + cat >expect <<-\EOF &&
357 + Refname is (HEAD detached from fromtag)
358 + Refname is refs/heads/ambiguous
359 + Refname is refs/heads/branch-one
360 + Refname is refs/heads/branch-two
361 + Refname is refs/heads/ref-to-branch
362 + Refname is refs/heads/ref-to-remote
363 + EOF
364 + git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
365 + test_cmp expect actual
366 +'
367 +
368 test_expect_success 'worktree colors correct' '
369 cat >expect <<-EOF &&
370 * <GREEN>(HEAD detached from fromtag)<RESET>
t/t6300-for-each-ref.sh
+8
@@ -1374,6 +1374,14 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
1374 test_cmp expect actual
1375 '
1376
1377 +test_expect_success 'for-each-ref --omit-empty works' '
1378 + git for-each-ref --format="%(refname)" >actual &&
1379 + test_line_count -gt 1 actual &&
1380 + git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
1381 + echo refs/heads/main >expect &&
1382 + test_cmp expect actual
1383 +'
1384 +
1385 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
1386 # name refs numerically to avoid case-insensitive filesystem conflicts
1387 nr=0 &&
t/t7004-tag.sh
+16
@@ -2001,6 +2001,22 @@ test_expect_success '--format should list tags as per format given' '
2001 test_cmp expect actual
2002 '
2003
2004 +test_expect_success '--format --omit-empty works' '
2005 + cat >expect <<-\EOF &&
2006 + refname : refs/tags/v1.0
2007 +
2008 + refname : refs/tags/v1.1.3
2009 + EOF
2010 + git tag -l --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
2011 + test_cmp expect actual &&
2012 + cat >expect <<-\EOF &&
2013 + refname : refs/tags/v1.0
2014 + refname : refs/tags/v1.1.3
2015 + EOF
2016 + git tag -l --omit-empty --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
2017 + test_cmp expect actual
2018 +'
2019 +
2020 test_expect_success 'git tag -l with --format="%(rest)" must fail' '
2021 test_must_fail git tag -l --format="%(rest)" "v1*"
2022 '