git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Øystein Walle" <oystwa@gmail.com>
To: git@vger.kernel.org
Cc: "Øystein Walle" <oystwa@gmail.com>
Subject: [PATCH] for-each-ref: add option to omit newlines
Date: Thu, 13 Feb 2014 19:00:29 +0100	[thread overview]
Message-ID: <1392314429-15281-1-git-send-email-oystwa@gmail.com> (raw)

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 <oystwa@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 b/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.
 
+-n::
+--no-newlines::
+	Do not print a newline after printing each formatted ref.
 
 FIELD NAMES
 -----------
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"), QUOTE_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_GROUP(""),
 		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
@@ -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;
 }
-- 
1.8.5

             reply	other threads:[~2014-02-13 18:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-13 18:00 Øystein Walle [this message]
2014-02-13 18:15 ` [PATCH] for-each-ref: add option to omit newlines Øystein Walle
2014-02-13 20:10 ` Junio C Hamano
2014-02-14  8:11   ` Øystein Walle
2014-02-14 16:28     ` Junio C Hamano
2014-02-14 23:03       ` Øystein Walle
2014-02-14 23:24         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1392314429-15281-1-git-send-email-oystwa@gmail.com \
    --to=oystwa@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).