git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/3] format_config: simplify buffer handling

From
Jeff King <peff@peff.net>
Date
Aug 21, 2015, 19:40 UTC
Message-ID
<20150821194001.GA26466@sigill.intra.peff.net>
In-Reply-To
<xmqqr3mw5yg1.fsf@gitster.dls.corp.google.com>
On Fri, Aug 21, 2015 at 10:43:58AM -0700, Junio C Hamano wrote:
Show 15 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > I wonder if we can do this instead
> >
> > 	if (!omit_values) {
> > -		if (show_keys)
> > +		if (show_keys && value_)
> > 			strbuf_addch(buf, key_delim);
> >
> > though.  That would eliminate the need for rolling back.
> 
> No we cannot.  "config --bool --get-regexp" could get a NULL value_
> and has to turn it to " true".
> 
> Sorry for the noise.
Right, I had the same thought and reached the same conclusion.

By the way, I do not think the rollback by itself is gross, it is just that it has to reproduce the "show_keys" logic. That is, it _really_ wants to be:

  else {
	  /* nothing to show; rollback delim */
	  if (we_added_delim)
		  strbuf_setlen(buf, buf->len - 1);
  }

and I use "show_keys" as a proxy for "did we add it". Which is reproducing the logic that you quoted above, and if the two ever get out of sync, it will be a bug. So you could write it as:

  int we_added_delim = 0;
  if (show_keys) {
	strbuf_addch(buf, key_delim);
	we_added_delim = 1;
  }

I didn't, because it's ugly, and the function is short enough and unlikely enough to change that it probably won't matter.

You could perhaps also write it as:
  size_t baselen = buf->len;
  if (show_keys)
	strbuf_addch(buf, key_delim);
  ...
  else {
	/* rollback delim */
	strbuf_setlen(buf, baselen);
  }
-Peff
Previous: Junio C HamanoNext: Jeff King
Message 14 of 18 in “[PATCHv3 0/2] 'git config --names-only' to help the completion script”
  1. SZEDER GáborAug 10, 2015
  2. 1/2 config: add '--names-only' option to list only variable namesSZEDER Gábor, Aug 10, 2015
  3. Jeff KingAug 10, 2015
  4. Junio C HamanoAug 10, 2015
  5. SZEDER GáborAug 12, 2015
  6. Junio C HamanoAug 13, 2015
  7. config: restructure format_config() for better control flowSZEDER Gábor, Aug 20, 2015
  8. Jeff KingAug 20, 2015
  9. 1/3 format_config: don't init strbufJeff King, Aug 20, 2015
  10. 2/3 format_config: simplify buffer handlingJeff King, Aug 20, 2015
  11. SZEDER GáborAug 21, 2015
  12. Junio C HamanoAug 21, 2015
  13. Junio C HamanoAug 21, 2015
  14. Jeff KingAug 21, 2015
  15. 3/3 get_urlmatch: avoid useless strbuf writeJeff King, Aug 20, 2015
  16. Junio C HamanoAug 20, 2015
  17. 2/2 completion: list variable names reliably with 'git config --names-only'SZEDER Gábor, Aug 10, 2015
  18. Jeff KingAug 10, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.