Even when having specified a format string for-each-ref still prints a
newline after printing each ref according to the format. This breaks
splitting the output on newlines, for example.

Signed-off-by: Øystein Walle <oys...@gmail.com>
I was somewhat surprised by this behaviour; I expected to be in full control of
the output but I could not get rid of the newlines.

As far as I'm *personally* concerned the line "putchar('\n');" could just as
well be removed completely and an '\n' appended to the default format string.
But since this command (and this behaviour) as been around since 2006 I assume
that it's in Git's best interest to not change the default behaviour. Hence the
introduction of this option. Although I was taken aback by this behaviour, is
it patch-worthy? The fix isn't very pretty.

On to the patch itself: I contemplated putting '\n' in the default format and
removing it if -n was given, which would get rid of the need to pass an exta
argument to show_ref(). But that means we would need to *insert it* when a
format is given and -n is not...

I've changed how the default format is assigned so that we can check if it's
NULL after option parsing to spit out an error. Alternatively we could just
allow it.

 Documentation/git-for-each-ref.txt |  3 +++
 builtin/for-each-ref.c             | 20 +++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-for-each-ref.txt 
index 4240875..805914a 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -62,6 +62,9 @@ OPTIONS
        the specified host language.  This is meant to produce
        a scriptlet that can directly be `eval`ed.
+       Do not print a newline after printing each formatted ref.
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 51798b4..32d6b12 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -998,7 +998,8 @@ static void emit(const char *cp, const char *ep)
-static void show_ref(struct refinfo *info, const char *format, int quote_style)
+static void show_ref(struct refinfo *info, const char *format, int quote_style,
+                    int no_newlines)
        const char *cp, *sp, *ep;
@@ -1023,7 +1024,8 @@ static void show_ref(struct refinfo *info, const char 
*format, int quote_style)
                resetv.s = color;
                print_value(&resetv, quote_style);
-       putchar('\n');
+       if (!no_newlines)
+               putchar('\n');
 static struct ref_sort *default_sort(void)
@@ -1067,9 +1069,9 @@ static char const * const for_each_ref_usage[] = {
 int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
        int i, num_refs;
-       const char *format = "%(objectname) %(objecttype)\t%(refname)";
+       const char *format = NULL;
        struct ref_sort *sort = NULL, **sort_tail = &sort;
-       int maxcount = 0, quote_style = 0;
+       int maxcount = 0, quote_style = 0, no_newlines = 0;
        struct refinfo **refs;
        struct grab_ref_cbdata cbdata;
@@ -1082,6 +1084,8 @@ int cmd_for_each_ref(int argc, const char **argv, const 
char *prefix)
                        N_("quote placeholders suitably for python"), 
                OPT_BIT(0 , "tcl",  &quote_style,
                        N_("quote placeholders suitably for tcl"), QUOTE_TCL),
+               OPT_BOOL('n' , "no-newlines",  &no_newlines,
+                       N_("do not output a newline after each ref")),
                OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched 
@@ -1100,6 +1104,12 @@ int cmd_for_each_ref(int argc, const char **argv, const 
char *prefix)
                error("more than one quoting style?");
                usage_with_options(for_each_ref_usage, opts);
+       if (no_newlines && !format) {
+               error("--no-newlines without --format does not make sense");
+               usage_with_options(for_each_ref_usage, opts);
+       }
+       if (!format)
+               format = "%(objectname) %(objecttype)\t%(refname)";
        if (verify_format(format))
                usage_with_options(for_each_ref_usage, opts);
@@ -1120,6 +1130,6 @@ int cmd_for_each_ref(int argc, const char **argv, const 
char *prefix)
        if (!maxcount || num_refs < maxcount)
                maxcount = num_refs;
        for (i = 0; i < maxcount; i++)
-               show_ref(refs[i], format, quote_style);
+               show_ref(refs[i], format, quote_style, no_newlines);
        return 0;

To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to