Skip to content

Commit aabfdc9

Browse files
Ossegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73876f4 commit aabfdc9

File tree

9 files changed

+75
-4
lines changed

9 files changed

+75
-4
lines changed

Documentation/git-branch.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ in another worktree linked to the same repository.
156156
--ignore-case::
157157
Sorting and filtering branches are case insensitive.
158158

159+
--omit-empty::
160+
Do not print a newline after formatted refs where the format expands
161+
to the empty string.
162+
159163
--column[=<options>]::
160164
--no-column::
161165
Display branch listing in columns. See configuration variable

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ OPTIONS
9393
--ignore-case::
9494
Sorting and filtering refs are case insensitive.
9595

96+
--omit-empty::
97+
Do not print a newline after formatted refs where the format expands
98+
to the empty string.
99+
96100
FIELD NAMES
97101
-----------
98102

Documentation/git-tag.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ options for details.
131131
--ignore-case::
132132
Sorting and filtering tags are case insensitive.
133133

134+
--omit-empty::
135+
Do not print a newline after formatted refs where the format expands
136+
to the empty string.
137+
134138
--column[=<options>]::
135139
--no-column::
136140
Display tag listing in columns. See configuration variable

builtin/branch.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static const char *head;
4141
static struct object_id head_oid;
4242
static int recurse_submodules = 0;
4343
static int submodule_propagate_branches = 0;
44+
static int omit_empty = 0;
4445

4546
static int branch_use_color = -1;
4647
static char branch_colors[][COLOR_MAXLEN] = {
@@ -461,7 +462,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
461462
string_list_append(output, out.buf);
462463
} else {
463464
fwrite(out.buf, 1, out.len, stdout);
464-
putchar('\n');
465+
if (out.len || !omit_empty)
466+
putchar('\n');
465467
}
466468
}
467469

@@ -670,6 +672,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
670672
OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
671673
OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
672674
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")),
673677
OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
674678
OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
675679
OPT_BOOL('l', "list", &list, N_("list branch names")),

builtin/for-each-ref.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1919
int i;
2020
struct ref_sorting *sorting;
2121
struct string_list sorting_options = STRING_LIST_INIT_DUP;
22-
int maxcount = 0, icase = 0;
22+
int maxcount = 0, icase = 0, omit_empty = 0;
2323
struct ref_array array;
2424
struct ref_filter filter;
2525
struct ref_format format = REF_FORMAT_INIT;
@@ -35,6 +35,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
3535
N_("quote placeholders suitably for python"), QUOTE_PYTHON),
3636
OPT_BIT(0 , "tcl", &format.quote_style,
3737
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")),
3840

3941
OPT_GROUP(""),
4042
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)
8890
if (format_ref_array_item(array.items[i], &format, &output, &err))
8991
die("%s", err.buf);
9092
fwrite(output.buf, 1, output.len, stdout);
91-
putchar('\n');
93+
if (output.len || !omit_empty)
94+
putchar('\n');
9295
}
9396

9497
strbuf_release(&err);

builtin/tag.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static const char * const git_tag_usage[] = {
3737
static unsigned int colopts;
3838
static int force_sign_annotate;
3939
static int config_sign_tag = -1; /* unspecified */
40+
static int omit_empty = 0;
4041

4142
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
4243
struct ref_format *format)
@@ -74,7 +75,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
7475
if (format_ref_array_item(array.items[i], format, &output, &err))
7576
die("%s", err.buf);
7677
fwrite(output.buf, 1, output.len, stdout);
77-
putchar('\n');
78+
if (output.len || !omit_empty)
79+
putchar('\n');
7880
}
7981

8082
strbuf_release(&err);
@@ -473,6 +475,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
473475
OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
474476
OPT_MERGED(&filter, N_("print only tags that are merged")),
475477
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")),
476480
OPT_REF_SORT(&sorting_options),
477481
{
478482
OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),

t/t3203-branch-output.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,30 @@ test_expect_success 'git branch with --format=%(rest) must fail' '
341341
test_must_fail git branch --format="%(rest)" >actual
342342
'
343343

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+
344368
test_expect_success 'worktree colors correct' '
345369
cat >expect <<-EOF &&
346370
* <GREEN>(HEAD detached from fromtag)<RESET>

t/t6300-for-each-ref.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,14 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
13741374
test_cmp expect actual
13751375
'
13761376

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+
13771385
test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
13781386
# name refs numerically to avoid case-insensitive filesystem conflicts
13791387
nr=0 &&

t/t7004-tag.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,22 @@ test_expect_success '--format should list tags as per format given' '
20012001
test_cmp expect actual
20022002
'
20032003

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+
20042020
test_expect_success 'git tag -l with --format="%(rest)" must fail' '
20052021
test_must_fail git tag -l --format="%(rest)" "v1*"
20062022
'

0 commit comments

Comments
 (0)