From: Jeff King <peff@peff.net>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 0/6] nd/worktree-move update
Date: Fri, 21 Apr 2017 10:59:16 -0400 [thread overview]
Message-ID: <20170421145916.mknekgqzhxffu7di@sigill.intra.peff.net> (raw)
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:
> --- 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
prev parent reply other threads:[~2017-04-21 15:56 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-20 10:10 [PATCH 0/6] nd/worktree-move update Nguyễn Thái Ngọc Duy
2017-04-20 10:10 ` [PATCH 1/6] worktree.c: add validate_worktree() Nguyễn Thái Ngọc Duy
2017-04-21 2:16 ` Junio C Hamano
2017-04-24 11:13 ` Duy Nguyen
2017-04-20 10:10 ` [PATCH 2/6] worktree.c: add update_worktree_location() Nguyễn Thái Ngọc Duy
2017-04-21 2:22 ` Junio C Hamano
2017-04-20 10:10 ` [PATCH 3/6] worktree move: new command Nguyễn Thái Ngọc Duy
2017-04-21 2:39 ` Junio C Hamano
2017-04-20 10:10 ` [PATCH 4/6] worktree move: accept destination as directory Nguyễn Thái Ngọc Duy
2017-04-21 2:44 ` Junio C Hamano
2017-04-20 10:10 ` [PATCH 5/6] worktree move: refuse to move worktrees with submodules Nguyễn Thái Ngọc Duy
2017-04-21 2:47 ` Junio C Hamano
2017-04-20 10:10 ` [PATCH 6/6] worktree remove: new command Nguyễn Thái Ngọc Duy
2017-04-21 3:33 ` Junio C Hamano
2017-04-21 14:59 ` Jeff King [this message]
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170421145916.mknekgqzhxffu7di@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).