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

Re: [PATCH 0/6] nd/worktree-move update

From
Jeff King <peff@peff.net>
Date
Apr 21, 2017, 14:59 UTC
Message-ID
<20170421145916.mknekgqzhxffu7di@sigill.intra.peff.net>
In-Reply-To
<20170420101024.7593-1-pclouds@gmail.com>
On Thu, Apr 20, 2017 at 05:10:18PM +0700, Nguyễn Thái Ngọc Duy wrote:
>  - fixes the compile problem on latest master (because prefix_filename
>    takes one argument less)
It also now returns an allocated buffer.
So:
Show 10 quoted lines
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -561,9 +561,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
>  	if (ac != 2)
>  		usage_with_options(worktree_usage, options);
>  
> -	strbuf_addstr(&dst, prefix_filename(prefix,
> -					    strlen(prefix),
> -					    av[1]));
> +	strbuf_addstr(&dst, prefix_filename(prefix, av[1]));
...this is now a leak. Probably:
  const char *filename = prefix_filename(prefix, av[1]);
  strbuf_attach(&dst, filename, strlen(filename), strlen(filename));

is what you want. That would be less awkward if we had a strbuf_attach_str().

Or if we had a strbuf variant of prefix_filename(), you could do:
  prefix_filename_buf(&dst, prefix, av[1]);

I almost added that when I did the prefix_filename() work, since it uses a strbuf internally. But there were no callers that would have used it. Maybe it's worth doing now.

-- >8 --
From d0b933fc023023a017df9268360aa327c28b90f0 Mon Sep 17 00:00:00 2001
From: Jeff King <peff@peff.net>
Date: Fri, 21 Apr 2017 10:53:07 -0400
Subject: [PATCH] prefix_filename: add strbuf variant

Now that prefix_filename() always allocates, it's awkward to put its value directly into a strbuf. You have to either free:

  const char *filename = prefix_filename(prefix, arg);
  strbuf_addstr(&buf, filename);
  free(filename);
or you have to attach:
  const char *filename = prefix_filename(prefix, arg);
  strbuf_attach(&buf, filename, strlen(filename), strlen(filename));

Since we're already using a strbuf internally, it's easy to provide a variant that lets you write directly into one:

  prefix_filename_buf(&buf, prefix, arg);

For consistency with git_path_buf(), the function overwrites the strbuf rather than appending.

Signed-off-by: Jeff King <peff@peff.net>
---
 abspath.c | 17 ++++++++++++-----
 cache.h   |  6 ++++++
 2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/abspath.c b/abspath.c
index 7f1cfe979..7594f62a5 100644
--- a/abspath.c
+++ b/abspath.c
@@ -246,21 +246,28 @@ char *absolute_pathdup(const char *path)
 	return strbuf_detach(&sb, NULL);
 }
 
-char *prefix_filename(const char *pfx, const char *arg)
+void prefix_filename_buf(struct strbuf *path, const char *pfx, const char *arg)
 {
-	struct strbuf path = STRBUF_INIT;
 	size_t pfx_len = pfx ? strlen(pfx) : 0;
 
+	strbuf_reset(path);
+
 	if (!pfx_len)
 		; /* nothing to prefix */
 	else if (is_absolute_path(arg))
 		pfx_len = 0;
 	else
-		strbuf_add(&path, pfx, pfx_len);
+		strbuf_add(path, pfx, pfx_len);
 
-	strbuf_addstr(&path, arg);
+	strbuf_addstr(path, arg);
 #ifdef GIT_WINDOWS_NATIVE
-	convert_slashes(path.buf + pfx_len);
+	convert_slashes(path->buf + pfx_len);
 #endif
+}
+
+char *prefix_filename(const char *pfx, const char *arg)
+{
+	struct strbuf path = STRBUF_INIT;
+	prefix_filename_buf(&path, pfx, arg);
 	return strbuf_detach(&path, NULL);
 }
diff --git a/cache.h b/cache.h
index ba27595d5..209039a98 100644
--- a/cache.h
+++ b/cache.h
@@ -548,6 +548,12 @@ extern char *prefix_path_gently(const char *prefix, int len, int *remaining, con
  */
 extern char *prefix_filename(const char *prefix, const char *path);
 
+/*
+ * Like prefix_filename, but write into "buf", overwriting any
+ * previous contents of the strbuf.
+ */
+extern void prefix_filename_buf(struct strbuf *out, const char *prefix, const char *path);
+
 extern int check_filename(const char *prefix, const char *name);
 extern void verify_filename(const char *prefix,
 			    const char *name,
-- 
2.13.0.rc0.364.g36b4d8031
Previous: Junio C Hamano
Message 15 of 15 in “nd/worktree-move update”
  1. 0/6 nd/worktree-move updateNguyễn Thái Ngọc Duy, Apr 20, 2017
  2. 1/6 worktree.c: add validate_worktree()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  3. Junio C HamanoApr 21, 2017
  4. Duy NguyenApr 24, 2017
  5. 2/6 worktree.c: add update_worktree_location()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  6. Junio C HamanoApr 21, 2017
  7. 3/6 worktree move: new commandNguyễn Thái Ngọc Duy, Apr 20, 2017
  8. Junio C HamanoApr 21, 2017
  9. 4/6 worktree move: accept destination as directoryNguyễn Thái Ngọc Duy, Apr 20, 2017
  10. Junio C HamanoApr 21, 2017
  11. 5/6 worktree move: refuse to move worktrees with submodulesNguyễn Thái Ngọc Duy, Apr 20, 2017
  12. Junio C HamanoApr 21, 2017
  13. 6/6 worktree remove: new commandNguyễn Thái Ngọc Duy, Apr 20, 2017
  14. Junio C HamanoApr 21, 2017
  15. Jeff KingApr 21, 2017

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.