for-each-ref: don't print out elements directly

Currently, the entire callchain starting from show_ref() parses and
prints immediately.  This inflexibility limits our ability to extend the
parser.  So, convert the entire callchain to accept a strbuf argument to
write to.  Also introduce a show_refs() helper that calls show_ref() in
a loop to avoid cluttering up cmd_for_each_ref() with the task of
initializing/freeing the strbuf.

[rr: commit message]

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
tar-tree: remove dependency on sq_quote_print()

Currently, there is exactly one caller of sq_quote_print(), namely
cmd_tar_tree().  In the interest of removing sq_quote_print() and
simplification, replace it with an equivalent call to sq_quote_argv().
No functional changes intended.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
quote: remove sq_quote_print()

Remove sq_quote_print() since it has no callers.  A nicer alternative
sq_quote_buf() exists: its callers aren't forced to print immediately.

For historical context, sq_quote_print() was first introduced in
575ba9d6 (GIT_TRACE: show which built-in/external commands are executed,
2006-06-25) for the purpose of printing argv for $GIT_TRACE.  Today, we
achieve this using trace_argv_printf() -> sq_quote_argv() ->
sq_quote_buf(), which ultimately fills in a strbuf.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
pretty: extend pretty_print_context with callback

The struct pretty_print_context contains the context in which the
placeholders in format_commit_one() should be parsed.  Although
format_commit_one() primarily acts as a parser, there is no way for a
caller to plug in custom callbacks.  Now, callers can:

1. Parse a custom placeholder that is not supported by
   format_commit_one(), and act on it independently of the pretty
   machinery.

2. Parse a custom placeholder to perform an in-place substitution to
   substitute the custom placeholder with a placeholder that
   format_commit_one() understands.  This is especially useful for
   supporting %>(*), where * is substituted by a computed length by the
   caller.

To support these two usecases, the interface for the function looks
like:

typedef size_t (*format_message_fn)(struct strbuf *sb,
				const char *placeholder,
				void *format_context,
				void *user_data,
				struct strbuf *placeholder_subst)

It looks exactly like format_commit_one(), except that there are two
additional fields: user_data (to pass custom data to the callback), and
placeholder_subst (to do the in-place substitution).  The callback
should return the length of the original string parsed, and optionally
set placeholder_subst.

[rr: commit message, minor modifications]

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
pretty: limit recursion in format_commit_one()

To make sure that a pretty_ctx->format substitution doesn't result in an
infinite recursion, change the prototype of format_commit_one() to
accept one last argument: no_recurse.  So, a single substitution by
format() must yield a result that can be parsed by format_commit_one()
without the help of format().

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
pretty: allow passing NULL commit to format_commit_message()

The new formatter, for-each-ref, may use non-commit placeholders only.
While it could audit the format line and warn/exclude commit
placeholders, that's a lot more work than simply ignore them.
Unrecognized placeholders are displayed as-is, pretty obvious that they
are not handled.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
for-each-ref: use format_commit_message as the main formatter

This brings a whole lot more format specifiers to "for-each-ref
--format". The most interesting are coloring and alignment
specifiers. It comes with shortcomings too as the two formats are
designed separately.

Dead functions are left in place to avoid cluttering the diffs. They
will be removed separately.
for-each-ref: remove dead functions
for-each-ref: introduce format specifier %>(*) and %<(*)

pretty placeholders %>(N) and %<(N) require a user provided width N,
which makes sense because the commit chain could be really long and
the user only needs to look at a few at the top, going to the end just
to calculate the best width wastes CPU cycles and makes Git feel slow.

for-each-ref is different. The display set is small, and we display
them all at once. We even support sorting, which goes through all
display items anyway. This patch introduces new %>(*) and %<(*), which
are supposed to be followed immediately by %(something) (i.e. original
for-each-ref specifiers, not ones coming from pretty.c). They
calculate the best width for the %(something), ignoring ansi escape
sequences if any.