git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Johannes Gilger <heipei@hackvalue.de>
Cc: Git ML <git@vger.kernel.org>, Thomas Rast <trast@student.ethz.ch>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] pretty: Initialize notes if %N is used
Date: Mon, 12 Apr 2010 04:56:47 -0400	[thread overview]
Message-ID: <20100412085647.GA26840@coredump.intra.peff.net> (raw)
In-Reply-To: <1270997662-25430-1-git-send-email-heipei@hackvalue.de>

On Sun, Apr 11, 2010 at 04:54:22PM +0200, Johannes Gilger wrote:

> something like this? I didn't see why userformat_fill_want had to have an extra
> argument for the format, since the user_format variable is static in pretty.c.

Yes, this is getting closer.

The reason to take the format argument is that there are places which
call format_commit_message() with an arbitrary string. We would want
them to be able to call userformat_fill_want(), too (right now, I don't
think any of them should need it, though).

Maybe it should take a format string, and use the user_format string if
you pass NULL?

> Sorry for the many very different patches to the bug, as you can see I'm not
> really familiar with best-practices in git.git.

Not at all. Sometimes seemingly simple bugs end up raising a whole host
of other issues. I am glad you are sticking around to help come up with
a good solution.

> diff --git a/builtin/log.c b/builtin/log.c
> index b706a5f..f8f5d22 100644
> --- a/builtin/log.c
> +++ b/builtin/log.c
> @@ -58,7 +58,11 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
>  		usage(builtin_log_usage);
>  	argc = setup_revisions(argc, argv, rev, opt);
>  
> -	if (!rev->show_notes_given && !rev->pretty_given)
> +	struct userformat_want w;

Don't declare variables in the middle of a function. It's a C99-ism that
we avoid for older compilers.

> +	if (rev->commit_format == CMIT_FMT_USERFORMAT)
> +		userformat_fill_want(&w);
> +
> +	if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
>  		rev->show_notes = 1;

Hmm. If we didn't get a userformat, what will be in w? It will be random
cruft from the stack, because we didn't call userformat_fill_want.

You can just call it unconditionally, and it should do the right thing
with a NULL user_format.

> +void userformat_fill_want(struct userformat_want *w)
> +{
> +	if (!user_format)
> +		return;
> +	struct strbuf dummy = STRBUF_INIT;
> +	memset(w, 0, sizeof(*w));
> +	strbuf_expand(&dummy, user_format, userformat_want_item, w);
> +	strbuf_release(&dummy);
> +}

This does nothing with a NULL user_format. It should probably still do
the memset() to indicate that nothing is wanted (otherwise the caller
has to be sure to initialize w themselves if they think user_format
might be NULL).

So combined with my above suggestion:

  void userformat_fill_want(const char *s, struct userformat_want *w)
  {
          struct strbuf dummy = STRBUF_INIT;
          memset(w, 0, sizeof(*w));
          if (!s) {
                  if (!user_format)
                          return;
                  s = user_format;
          }
          strbuf_expand(&dummy, user_format, userformat_want_item, w);
          strbuf_release(&dummy);
  }

and then you can just call

  userformat_fill_want(NULL, w);

safely from log.c (you don't even need to check rev->commit_format).

Also, even though I picked the name, userformat_fill_want is kind of a
lousy name. It was the best I could come up with, but maybe somebody has
a better suggestion.

-Peff

  reply	other threads:[~2010-04-12  8:57 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-05 11:55 [PATCH] Initialize notes trees if %N is used and no --show-notes given Johannes Gilger
2010-04-06  5:32 ` Jeff King
2010-04-06  9:27 ` Thomas Rast
2010-04-06 11:19   ` Johannes Gilger
2010-04-06 11:52     ` Thomas Rast
2010-04-06 16:22       ` Jeff King
2010-04-07  6:18       ` Junio C Hamano
2010-04-07  6:36         ` Jeff King
2010-04-10  7:05   ` [PATCH] pretty.c: Don't expand %N without --show-notes Johannes Gilger
2010-04-10 20:00     ` Junio C Hamano
2010-04-10 21:30       ` [PATCH] Notes: Connect the %N flag to --{show,no}-notes Johannes Gilger
2010-04-10 21:51         ` Junio C Hamano
2010-04-10 22:08           ` Jeff King
2010-04-11 14:54             ` [PATCH] pretty: Initialize notes if %N is used Johannes Gilger
2010-04-12  8:56               ` Jeff King [this message]
2010-04-13  8:59                 ` [PATCHv2] " Johannes Gilger
2010-04-13 10:03                   ` Jeff King
2010-04-13 10:36                     ` Johannes Gilger
2010-04-13 10:57                       ` [PATCHv3] " y
2010-04-13 10:57                       ` y
2010-04-13 11:01                       ` Johannes Gilger
2010-04-13 11:07                         ` Jeff King
2010-04-13 11:26                           ` [PATCHv4] " Johannes Gilger
2010-04-13 20:01                             ` Junio C Hamano
2010-04-13 20:31                               ` [PATCHv5] " Johannes Gilger
2010-04-10 22:20           ` [PATCH] Notes: Connect the %N flag to --{show,no}-notes Johannes Gilger

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=20100412085647.GA26840@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=heipei@hackvalue.de \
    --cc=trast@student.ethz.ch \
    /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).