Re: [PATCH] log -L: improve error message on malformed argument
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Apr 16, 2015, 20:59 UTC
- Message-ID
- <xmqqzj67hjs5.fsf@gitster.dls.corp.google.com>
- In-Reply-To
- <1429195387-20573-1-git-send-email-Matthieu.Moy@imag.fr>
Matthieu Moy <Matthieu.Moy@imag.fr> writes:
Show 25 quoted lines
> The old message did not mention the :regex:file form.
>
> To avoid overly long lines, split the message into two lines (in case
> item->string is long, it will be the only part truncated in a narrow
> terminal).
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> ---
> line-log.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/line-log.c b/line-log.c
> index a490efe..e725248 100644
> --- a/line-log.c
> +++ b/line-log.c
> @@ -575,7 +575,8 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
>
> name_part = skip_range_arg(item->string);
> if (!name_part || *name_part != ':' || !name_part[1])
> - die("-L argument '%s' not of the form start,end:file",
> + die("invalid -L argument '%s'.\n"
> + "It should be of the form start,end:file or :regex:file.",
> item->string);
> range_part = xstrndup(item->string, name_part - item->string);
> name_part++;Hmm.
While I understand and even agree with the reasoning behind chomping the line after a potentially long user-supplied argument, it somewhat bothers me that the output of subsequent lines would begin without the prefix.
Do we have any other multi-line die/error/warning message?
I am tempted to suggest doing something along the lines of the attached, if we were to start using multi-line die/error/warning like this one. Obviously we would need something similar on the vwritef() side as well.
usage.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/usage.c b/usage.c index ed14645..09710fa 100644 --- a/usage.c +++ b/usage.c @@ -8,9 +8,26 @@ void vreportf(const char *prefix, const char *err, va_list params) { - char msg[4096]; - vsnprintf(msg, sizeof(msg), err, params); - fprintf(stderr, "%s%s\n", prefix, msg); + char msg[4096], *bol; + int len; + + len = vsnprintf(msg, sizeof(msg), err, params); + if (sizeof(msg) < len) + len = sizeof(msg); + bol = msg; + while (1) { + int linelen; + char *eol = memchr(bol, '\n', len); + if (!eol) + linelen = len; + else + linelen = eol - bol; + fprintf(stderr, "%s%.*s\n", prefix, linelen, bol); + if (!eol) + break; + bol = eol + 1; + len -= linelen + 1; + } } void vwritef(int fd, const char *prefix, const char *err, va_list params)