Re: [PATCH] for-each-ref: add option to omit newlines

Subsystems: the rest

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] for-each-ref: add option to omit newlines

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:59:54

Øystein Walle [off-list ref] writes:
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 would rather see us go in the direction to add "-z" output option,
which is what everybody else that produces NUL terminated entries in
our suite of subcommands does.

IOW, something along with this line (untested).

 builtin/for-each-ref.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 51798b4..2c8cac8 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -94,6 +94,7 @@ static const char **used_atom;
 static cmp_type *used_atom_type;
 static int used_atom_cnt, need_tagged, need_symref;
 static int need_color_reset_at_eol;
+static int line_termination = '\n';
 
 /*
  * Used to parse format string and sort specifiers
@@ -1023,7 +1024,7 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
 		resetv.s = color;
 		print_value(&resetv, quote_style);
 	}
-	putchar('\n');
+	putchar(line_termination);
 }
 
 static struct ref_sort *default_sort(void)
@@ -1088,6 +1089,9 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
 		OPT_CALLBACK(0 , "sort", sort_tail, N_("key"),
 			    N_("field name to sort on"), &opt_parse_sort),
+		OPT_SET_INT('z', NULL, &line_termination,
+			    N_("Use NUL instead of LF to end each output records"),
+			    '\0'),
 		OPT_END(),
 	};
 

Re: [PATCH] for-each-ref: add option to omit newlines

From: Øystein Walle <hidden>
Date: 2016-06-15 22:59:54

Junio C Hamano <gitster <at> pobox.com> writes:
I would rather see us go in the direction to add "-z" output option,
which is what everybody else that produces NUL terminated entries in
our suite of subcommands does.
I agree that -z would help in this case and I very much appreciate that
option when using diff --name-only, ls-files, etc.

However, when specifying a format string it's just a matter of ending
the format string in '%00' and you're good to go. But then you get the
null byte *and* a newline. And with your proposal there would be no way
of saying you want neither.

I expected the output to be formatted according to a (repetition of) the
format string, not some variation of it that I couldn't opt out of. But
I see that git-log also shows this behavior and already has a -z option,
so I guess that's fairly ingrained.

Maybe it's just me? In that case I've no problem with throwing in the
towel.

Øsse
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help