]> git.ipfire.org Git - thirdparty/git.git/commitdiff
branch, for-each-ref, tag: add option to omit empty lines
authorØystein Walle <oystwa@gmail.com>
Fri, 7 Apr 2023 17:53:16 +0000 (19:53 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 13 Apr 2023 15:07:45 +0000 (08:07 -0700)
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>
Documentation/git-branch.txt
Documentation/git-for-each-ref.txt
Documentation/git-tag.txt
builtin/branch.c
builtin/for-each-ref.c
builtin/tag.c
t/t3203-branch-output.sh
t/t6300-for-each-ref.sh
t/t7004-tag.sh

index d382ac69f77b1ce607d881f149a04f94aa930d82..d207da9101a5cfe6441a03360191d3b6b8a73a46 100644 (file)
@@ -156,6 +156,10 @@ in another worktree linked to the same repository.
 --ignore-case::
        Sorting and filtering branches are case insensitive.
 
+--omit-empty::
+       Do not print a newline after formatted refs where the format expands
+       to the empty string.
+
 --column[=<options>]::
 --no-column::
        Display branch listing in columns. See configuration variable
index 6da899c62964275a97854c0b338f83f51db4bdfe..af790bfa4e4cb18dd4e0670a878d60cd8a5744ec 100644 (file)
@@ -93,6 +93,10 @@ OPTIONS
 --ignore-case::
        Sorting and filtering refs are case insensitive.
 
+--omit-empty::
+       Do not print a newline after formatted refs where the format expands
+       to the empty string.
+
 FIELD NAMES
 -----------
 
index fdc72b5875a343aa9a0360e57bbdac684deee894..7f61c1edb3e65a3ed06ed0090b66ac855851b554 100644 (file)
@@ -131,6 +131,10 @@ options for details.
 --ignore-case::
        Sorting and filtering tags are case insensitive.
 
+--omit-empty::
+       Do not print a newline after formatted refs where the format expands
+       to the empty string.
+
 --column[=<options>]::
 --no-column::
        Display tag listing in columns. See configuration variable
index f63fd45edb96b513c24e1a9cd7c8a55e5a5f7b13..b47fef51fbc6fd38527428e45185eecae972525c 100644 (file)
@@ -41,6 +41,7 @@ static const char *head;
 static struct object_id head_oid;
 static int recurse_submodules = 0;
 static int submodule_propagate_branches = 0;
+static int omit_empty = 0;
 
 static int branch_use_color = -1;
 static char branch_colors[][COLOR_MAXLEN] = {
@@ -461,7 +462,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
                        string_list_append(output, out.buf);
                } else {
                        fwrite(out.buf, 1, out.len, stdout);
-                       putchar('\n');
+                       if (out.len || !omit_empty)
+                               putchar('\n');
                }
        }
 
@@ -670,6 +672,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
                OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
                OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
+               OPT_BOOL(0, "omit-empty",  &omit_empty,
+                       N_("do not output a newline after empty formatted refs")),
                OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
                OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
                OPT_BOOL('l', "list", &list, N_("list branch names")),
index 6f62f40d1263f44f8977125d9d54eca76f101685..1fc513048100972e7ea15e2c940e474c5b3afa72 100644 (file)
@@ -19,7 +19,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
        int i;
        struct ref_sorting *sorting;
        struct string_list sorting_options = STRING_LIST_INIT_DUP;
-       int maxcount = 0, icase = 0;
+       int maxcount = 0, icase = 0, omit_empty = 0;
        struct ref_array array;
        struct ref_filter filter;
        struct ref_format format = REF_FORMAT_INIT;
@@ -35,6 +35,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
                        N_("quote placeholders suitably for python"), QUOTE_PYTHON),
                OPT_BIT(0 , "tcl",  &format.quote_style,
                        N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
+               OPT_BOOL(0, "omit-empty",  &omit_empty,
+                       N_("do not output a newline after empty formatted refs")),
 
                OPT_GROUP(""),
                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)
                if (format_ref_array_item(array.items[i], &format, &output, &err))
                        die("%s", err.buf);
                fwrite(output.buf, 1, output.len, stdout);
-               putchar('\n');
+               if (output.len || !omit_empty)
+                       putchar('\n');
        }
 
        strbuf_release(&err);
index d428c45dc8d812255336ed5da20804981d3be4ab..61bc732c92c99b101edd00bbf49fd3e964782b78 100644 (file)
@@ -37,6 +37,7 @@ static const char * const git_tag_usage[] = {
 static unsigned int colopts;
 static int force_sign_annotate;
 static int config_sign_tag = -1; /* unspecified */
+static int omit_empty = 0;
 
 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
                     struct ref_format *format)
@@ -74,7 +75,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
                if (format_ref_array_item(array.items[i], format, &output, &err))
                        die("%s", err.buf);
                fwrite(output.buf, 1, output.len, stdout);
-               putchar('\n');
+               if (output.len || !omit_empty)
+                       putchar('\n');
        }
 
        strbuf_release(&err);
@@ -473,6 +475,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
                OPT_WITHOUT(&filter.no_commit, N_("print only tags that don't contain the commit")),
                OPT_MERGED(&filter, N_("print only tags that are merged")),
                OPT_NO_MERGED(&filter, N_("print only tags that are not merged")),
+               OPT_BOOL(0, "omit-empty",  &omit_empty,
+                       N_("do not output a newline after empty formatted refs")),
                OPT_REF_SORT(&sorting_options),
                {
                        OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
index d34d77f89348d86518375a66e277bb118ff29c22..c06906d83e2be9be0130f990ea56fa0428982e9b 100755 (executable)
@@ -341,6 +341,30 @@ test_expect_success 'git branch with --format=%(rest) must fail' '
        test_must_fail git branch --format="%(rest)" >actual
 '
 
+test_expect_success 'git branch --format --omit-empty' '
+       cat >expect <<-\EOF &&
+       Refname is (HEAD detached from fromtag)
+       Refname is refs/heads/ambiguous
+       Refname is refs/heads/branch-one
+       Refname is refs/heads/branch-two
+
+       Refname is refs/heads/ref-to-branch
+       Refname is refs/heads/ref-to-remote
+       EOF
+       git branch --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
+       test_cmp expect actual &&
+       cat >expect <<-\EOF &&
+       Refname is (HEAD detached from fromtag)
+       Refname is refs/heads/ambiguous
+       Refname is refs/heads/branch-one
+       Refname is refs/heads/branch-two
+       Refname is refs/heads/ref-to-branch
+       Refname is refs/heads/ref-to-remote
+       EOF
+       git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'worktree colors correct' '
        cat >expect <<-EOF &&
        * <GREEN>(HEAD detached from fromtag)<RESET>
index c466fd989f168b0fc696e83516474f11040a58be..d4ccc22d99f5a8433aa1b979f202b8305f1954e2 100755 (executable)
@@ -1374,6 +1374,14 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
        test_cmp expect actual
 '
 
+test_expect_success 'for-each-ref --omit-empty works' '
+       git for-each-ref --format="%(refname)" >actual &&
+       test_line_count -gt 1 actual &&
+       git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
+       echo refs/heads/main >expect &&
+       test_cmp expect actual
+'
+
 test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
        # name refs numerically to avoid case-insensitive filesystem conflicts
        nr=0 &&
index 9aa1660651b8a96397ce2a83cb3d107c8b7d43dc..6493583110a2033e820abbbc8394ad0d4e4b4bb6 100755 (executable)
@@ -2001,6 +2001,22 @@ test_expect_success '--format should list tags as per format given' '
        test_cmp expect actual
 '
 
+test_expect_success '--format --omit-empty works' '
+       cat >expect <<-\EOF &&
+       refname : refs/tags/v1.0
+
+       refname : refs/tags/v1.1.3
+       EOF
+       git tag -l --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
+       test_cmp expect actual &&
+       cat >expect <<-\EOF &&
+       refname : refs/tags/v1.0
+       refname : refs/tags/v1.1.3
+       EOF
+       git tag -l --omit-empty --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'git tag -l with --format="%(rest)" must fail' '
        test_must_fail git tag -l --format="%(rest)" "v1*"
 '