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

Re: [PATCH v4 2/2] diff: generate pretty filenames in prep_temp_blob()

From
David Aguilar <davvid@gmail.com>
Date
May 30, 2009, 01:22 UTC
Message-ID
<20090530012204.GA26210@gmail.com>
In-Reply-To
<20090529195537.GA13961@coredump.intra.peff.net>
On Fri, May 29, 2009 at 03:55:37PM -0400, Jeff King wrote:
Show 10 quoted lines
> On Thu, May 28, 2009 at 05:49:24PM -0700, David Aguilar wrote:
> 
> > @@ -1964,8 +1964,24 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
> >  {
> >  	int fd;
> >  	struct strbuf buf = STRBUF_INIT;
> > +	struct strbuf template = STRBUF_INIT;
> > +	char *basename = ((char*)path) + strlen(path) - 1;
> 
> Why do you drop constness in this assignment?
That was a mistake.
Show 11 quoted lines
> > +	/* Windows lacks basename() */
> > +	while(*basename && basename > path) {
> > +		basename--;
> > +		if (is_dir_sep(*basename)) {
> > +			basename++;
> > +			break;
> > +		}
> > +	}
> 
> This is such an easily-factorable bit, maybe it makes sense to add as
> basename() in compat/?

I would feel a little better calling it git_basename(const char *) and keeping it in path.c (or another name if basename is no good)

The reason is from the manpage for basename:

"Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions."

The above snippet is much simpler and does not have that restriction since we can guarantee const-ness.

All this version cares about is being able to find the last dir_sep and returning a pointer.

What do you think?
-- 
	David
Previous: Jeff KingNext: Jeff King
Message 4 of 5 in “compat: add a mkstemps() compatibility function”
  1. 1/2 compat: add a mkstemps() compatibility functionDavid Aguilar, May 29, 2009
  2. 2/2 diff: generate pretty filenames in prep_temp_blob()David Aguilar, May 29, 2009
  3. Jeff KingMay 29, 2009
  4. David AguilarMay 30, 2009
  5. Jeff KingMay 30, 2009

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.