From: Junio C Hamano <gitster@pobox.com>
To: Julian Phillips <julian@quantumfyre.co.uk>
Cc: git@vger.kernel.org, Eric Raymond <esr@thyrsus.com>
Subject: Re: [RFC/PATCH 2/3] add a library of code for producing structured output
Date: Sun, 11 Apr 2010 11:16:18 -0700 [thread overview]
Message-ID: <7vy6gtonwt.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: <20100411113733.80010.3767.julian@quantumfyre.co.uk> (Julian Phillips's message of "Sun\, 11 Apr 2010 12\:37\:31 +0100")
Julian Phillips <julian@quantumfyre.co.uk> writes:
> Add a library that allows commands to produce structured output in any
> of a range of formats using a single API.
>
> The API includes an OPT_OUTPUT and handle_output_arg so that the
> option handling for different commands will be as similar as possible.
I was hoping that the existing low-level -z routines (e.g. "diff-* -z")
follow similar enough patterns to have a corresponding output-z.c and be
handled inside output.c library. But that is not a requirement, just
"would have been nicer if the original were written that way".
> diff --git a/output-json.c b/output-json.c
> new file mode 100644
> index 0000000..0eb66b2
> --- /dev/null
> +++ b/output-json.c
> @@ -0,0 +1,128 @@
> +#include "git-compat-util.h"
> +#include "output.h"
> +#include "strbuf.h"
> +
> +static char *json_quote(char *s)
> +{
> + struct strbuf buf = STRBUF_INIT;
> +
> + while (*s) {
> + switch (*s) {
> +...
> + default:
> + /* All control characters must be encode, even if they
> + * don't have a specific escape character of their own */
> + if (*s < 0x20)
> + strbuf_addf(&buf, "\\u%04x", *s);
As you didn't say your "char" is either signed or unsigned upfront, this
will behave differently when you are fed a UTF-8 string. If it is signed,
you will end up showing bytes in a single letter separately at wrong
codepoint, and if it is unsigned, you will give UTF-8 string unquoted,
which probably is what you meant to do.
What is your design intention regarding legacy encoding? This code does
not yet declare "dear user, if you plan to use json/xml output, your
repository metadata (notably the pathnames) has to be in UTF-8", as the
caller _could_ transliterate legacy data before feeding it to output.c
layer. An alternative would be for the output.c layer to know about the
encoding of incoming data and transliterate when the output format
requires a particular encoding.
> +static void json_obj_item_start(FILE *file, char *name, int first)
> +{
> + char *quoted = json_quote(name);
> + if (!first)
> + fprintf(file, ",\n");
> + fprintf(file, "\"%s\" : ", quoted);
> + free(quoted);
> +}
> + ...
> +static void json_str(FILE *file, char *value)
> +{
> + char *quoted = json_quote(value);
> + fprintf(file, "\"%s\"", quoted);
> + free(quoted);
> +}
An obvious improvement would be to make json_quote() to take FILE * to
avoid wasteful allocation and copy, as it doesn't do anything but addstr
and addch, and all of its callers don't do anything but spitting the
result out to FILE *.
> diff --git a/output-xml.c b/output-xml.c
> new file mode 100644
> index 0000000..50dd7d6
> --- /dev/null
> +++ b/output-xml.c
> @@ -0,0 +1,68 @@
> +#include "git-compat-util.h"
> +#include "output.h"
This seems to totally lack quoting of any metacharacters for "name" and
string "value".
next prev parent reply other threads:[~2010-04-11 18:16 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-11 11:37 [RFC/PATCH 0/3] JSON/XML output for scripting interface Julian Phillips
2010-04-11 11:37 ` [RFC/PATCH 1/3] strbuf: Add strbuf_vaddf function Julian Phillips
2010-04-11 12:42 ` Erik Faye-Lund
2010-04-11 12:59 ` Julian Phillips
2010-04-11 11:37 ` [RFC/PATCH 2/3] add a library of code for producing structured output Julian Phillips
2010-04-11 12:51 ` Erik Faye-Lund
2010-04-11 13:03 ` Julian Phillips
2010-04-11 15:46 ` Jakub Narebski
2010-04-11 18:16 ` Junio C Hamano [this message]
2010-04-11 18:26 ` Sverre Rabbelier
2010-04-11 19:21 ` Julian Phillips
2010-04-11 20:34 ` Jakub Narebski
2010-04-11 20:46 ` Julian Phillips
2010-04-11 20:57 ` Eric Raymond
2010-04-11 11:37 ` [RFC/PATCH 3/3] status: add support for " Julian Phillips
2010-04-11 15:48 ` [RFC/PATCH 0/3] JSON/XML output for scripting interface Sverre Rabbelier
2010-04-11 17:30 ` Julian Phillips
2010-04-11 17:34 ` Sverre Rabbelier
2010-04-11 17:45 ` Julian Phillips
2010-04-11 17:50 ` Sverre Rabbelier
2010-04-11 22:22 ` Jon Seymour
2010-04-11 22:34 ` Eric Raymond
2010-04-11 23:25 ` Jon Seymour
2010-04-11 23:30 ` Julian Phillips
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=7vy6gtonwt.fsf@alter.siamese.dyndns.org \
--to=gitster@pobox.com \
--cc=esr@thyrsus.com \
--cc=git@vger.kernel.org \
--cc=julian@quantumfyre.co.uk \
/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).