Skip to content

Commit b64894c

Browse files
committed
Merge branch 'ow/ref-filter-omit-empty'
"git branch --format=..." and "git format-patch --format=..." learns "--omit-empty" to hide refs that whose formatting result becomes an empty string from the output. * ow/ref-filter-omit-empty: branch, for-each-ref, tag: add option to omit empty lines
2 parents 9e0d1aa + aabfdc9 commit b64894c

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
@@ -98,6 +98,10 @@ OPTIONS
9898
--ignore-case::
9999
Sorting and filtering refs are case insensitive.
100100

101+
--omit-empty::
102+
Do not print a newline after formatted refs where the format expands
103+
to the empty string.
104+
101105
FIELD NAMES
102106
-----------
103107

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
@@ -44,6 +44,7 @@ static const char *head;
4444
static struct object_id head_oid;
4545
static int recurse_submodules = 0;
4646
static int submodule_propagate_branches = 0;
47+
static int omit_empty = 0;
4748

4849
static int branch_use_color = -1;
4950
static char branch_colors[][COLOR_MAXLEN] = {
@@ -483,7 +484,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
483484
string_list_append(output, out.buf);
484485
} else {
485486
fwrite(out.buf, 1, out.len, stdout);
486-
putchar('\n');
487+
if (out.len || !omit_empty)
488+
putchar('\n');
487489
}
488490
}
489491

@@ -692,6 +694,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
692694
OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
693695
OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
694696
OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
697+
OPT_BOOL(0, "omit-empty", &omit_empty,
698+
N_("do not output a newline after empty formatted refs")),
695699
OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
696700
OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
697701
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
@@ -22,7 +22,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
2222
int i;
2323
struct ref_sorting *sorting;
2424
struct string_list sorting_options = STRING_LIST_INIT_DUP;
25-
int maxcount = 0, icase = 0;
25+
int maxcount = 0, icase = 0, omit_empty = 0;
2626
struct ref_array array;
2727
struct ref_filter filter;
2828
struct ref_format format = REF_FORMAT_INIT;
@@ -40,6 +40,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
4040
N_("quote placeholders suitably for python"), QUOTE_PYTHON),
4141
OPT_BIT(0 , "tcl", &format.quote_style,
4242
N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
43+
OPT_BOOL(0, "omit-empty", &omit_empty,
44+
N_("do not output a newline after empty formatted refs")),
4345

4446
OPT_GROUP(""),
4547
OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
@@ -112,7 +114,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
112114
if (format_ref_array_item(array.items[i], &format, &output, &err))
113115
die("%s", err.buf);
114116
fwrite(output.buf, 1, output.len, stdout);
115-
putchar('\n');
117+
if (output.len || !omit_empty)
118+
putchar('\n');
116119
}
117120

118121
strbuf_release(&err);

builtin/tag.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static const char * const git_tag_usage[] = {
4141
static unsigned int colopts;
4242
static int force_sign_annotate;
4343
static int config_sign_tag = -1; /* unspecified */
44+
static int omit_empty = 0;
4445

4546
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
4647
struct ref_format *format)
@@ -79,7 +80,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
7980
if (format_ref_array_item(array.items[i], format, &output, &err))
8081
die("%s", err.buf);
8182
fwrite(output.buf, 1, output.len, stdout);
82-
putchar('\n');
83+
if (output.len || !omit_empty)
84+
putchar('\n');
8385
}
8486

8587
strbuf_release(&err);
@@ -474,6 +476,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
474476
OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
475477
OPT_MERGED(&filter, N_("print only tags that are merged")),
476478
OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
479+
OPT_BOOL(0, "omit-empty", &omit_empty,
480+
N_("do not output a newline after empty formatted refs")),
477481
OPT_REF_SORT(&sorting_options),
478482
{
479483
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
@@ -355,6 +355,30 @@ test_expect_success 'git branch with --format=%(rest) must fail' '
355355
test_must_fail git branch --format="%(rest)" >actual
356356
'
357357

358+
test_expect_success 'git branch --format --omit-empty' '
359+
cat >expect <<-\EOF &&
360+
Refname is (HEAD detached from fromtag)
361+
Refname is refs/heads/ambiguous
362+
Refname is refs/heads/branch-one
363+
Refname is refs/heads/branch-two
364+
365+
Refname is refs/heads/ref-to-branch
366+
Refname is refs/heads/ref-to-remote
367+
EOF
368+
git branch --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
369+
test_cmp expect actual &&
370+
cat >expect <<-\EOF &&
371+
Refname is (HEAD detached from fromtag)
372+
Refname is refs/heads/ambiguous
373+
Refname is refs/heads/branch-one
374+
Refname is refs/heads/branch-two
375+
Refname is refs/heads/ref-to-branch
376+
Refname is refs/heads/ref-to-remote
377+
EOF
378+
git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
379+
test_cmp expect actual
380+
'
381+
358382
test_expect_success 'worktree colors correct' '
359383
cat >expect <<-EOF &&
360384
* <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
@@ -2046,6 +2046,22 @@ test_expect_success '--format should list tags as per format given' '
20462046
test_cmp expect actual
20472047
'
20482048

2049+
test_expect_success '--format --omit-empty works' '
2050+
cat >expect <<-\EOF &&
2051+
refname : refs/tags/v1.0
2052+
2053+
refname : refs/tags/v1.1.3
2054+
EOF
2055+
git tag -l --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
2056+
test_cmp expect actual &&
2057+
cat >expect <<-\EOF &&
2058+
refname : refs/tags/v1.0
2059+
refname : refs/tags/v1.1.3
2060+
EOF
2061+
git tag -l --omit-empty --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
2062+
test_cmp expect actual
2063+
'
2064+
20492065
test_expect_success 'git tag -l with --format="%(rest)" must fail' '
20502066
test_must_fail git tag -l --format="%(rest)" "v1*"
20512067
'

0 commit comments

Comments
 (0)