From: shejialuo <shejialuo@gmail.com>
To: Caleb White <cdwhite3@pm.me>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2 2/4] worktree: link worktrees with relative paths
Date: Mon, 7 Oct 2024 11:45:09 +0800 [thread overview]
Message-ID: <ZwNZRX1LHlxawJJc@ArchLinux> (raw)
In-Reply-To: <I3BmSHTyOELt2DzfSaLhRYLouu5iAPZIZGZ2Ne73AygO35CM0mq44INa68t6VD4XV61DgzbrfUW0m8fivd3N9Rejgm-tecXQHXQRs1BP9CQ=@pm.me>
On Sun, Oct 06, 2024 at 11:57:22PM +0000, Caleb White wrote:
[snip]
> > Still, we do not need to call "strbuf_reset" again for "tmp". But there
> > is another question here. Should we define the "file" just in this "if"
> > block and free "file" also in the block?
>
> The style this code uses seems to place most / all of the declarations at
> the top of the function and frees at the bottom so I think this fits in.
>
Yes, as you have said, the code style places most / all of the
declarations at the top and free at the bottom. But the trouble here is
we will free the "file" in the "if" block.
char *file = NULL;
if (...) {
file = xstrfmt(...);
free(file);
file = xstrfmt(...);
}
free(file);
If we want to follow the original code style, should we create two
variables at the top and free them at the bottom?
> > And I don't think it's a good idea to use "xstrfmt". Here, we need to
> > allocate two times and free two times. Why not just define a "struct
> > strbuf" and the use "strbuf_*" method here?
>
> I can use strbufs, I just wasn't sure if I really needed a strbuf for
> each of the paths and was just trying to reuse a var.
>
You don't need to create a new "strbuf" for each of the paths. You could
just use "strbuf_reset" for only one "struct strbuf".
struct strbuf file = STRBUF_INIT;
if (...) {
strbuf_addf(...);
strbuf_reset(&file);
strbuf_addf(...);
}
strbuf_release(&file);
> > > strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);
> > > strbuf_addf(&dotgit, "%s/.git", wt->path);
> > > - backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
> > > + git_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
> >
>
> >
>
> > Why here we need to use "xstrdup_or_null". The life cycle of the
> > "git_contents" variable is in the "repair_gitfile" function.
>
> This what the existing code used and I saw no reason to change it...
Actually, I somehow understand why in the original code, it will use
"xstrdup_or_null" to initialize the "backlink". Because in
"read_gitfile_gently", we will use a static "struct strbuf" as the
buffer.
I guess the intention of the original code is that if we call again
"read_gitfile_gently" in this function or we have another thread which
calls this function, the content of the buffer will be changed. So we
explicitly use "xstrdup_or_null" to create a copy here to avoid this.
But I wonder whether we really need to consider above problem?
next prev parent reply other threads:[~2024-10-07 3:45 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-06 6:00 [PATCH v2 0/4] Link worktrees with relative paths Caleb White
2024-10-06 6:00 ` [PATCH v2 1/4] worktree: refactor infer_backlink() to use *strbuf Caleb White
2024-10-06 15:09 ` shejialuo
2024-10-06 15:13 ` Kristoffer Haugsbakk
2024-10-06 18:41 ` Eric Sunshine
2024-10-07 2:26 ` Caleb White
2024-10-07 4:12 ` shejialuo
2024-10-07 4:19 ` Caleb White
2024-10-07 4:28 ` shejialuo
2024-10-07 4:31 ` Caleb White
2024-10-07 3:56 ` shejialuo
2024-10-07 4:01 ` Caleb White
2024-10-07 4:19 ` shejialuo
2024-10-06 23:47 ` Caleb White
2024-10-06 18:16 ` Eric Sunshine
2024-10-07 2:42 ` Caleb White
2024-10-07 3:26 ` Eric Sunshine
2024-10-07 3:28 ` Caleb White
2024-10-06 6:01 ` [PATCH v2 2/4] worktree: link worktrees with relative paths Caleb White
2024-10-06 11:05 ` Eric Sunshine
2024-10-06 22:37 ` Caleb White
2024-10-06 15:37 ` shejialuo
2024-10-06 23:57 ` Caleb White
2024-10-07 3:45 ` shejialuo [this message]
2024-10-07 4:02 ` Eric Sunshine
2024-10-07 16:59 ` Caleb White
2024-10-06 6:01 ` [PATCH v2 3/4] worktree: sync worktree paths after gitdir move Caleb White
2024-10-06 11:12 ` Eric Sunshine
2024-10-06 22:41 ` Caleb White
2024-10-06 22:48 ` Eric Sunshine
2024-10-06 23:13 ` Caleb White
2024-10-06 23:27 ` Eric Sunshine
2024-10-06 6:01 ` [PATCH v2 4/4] worktree: prevent null pointer dereference Caleb White
2024-10-06 11:24 ` Eric Sunshine
2024-10-06 23:03 ` Caleb White
2024-10-06 23:24 ` Eric Sunshine
2024-10-07 3:09 ` Caleb White
2024-10-06 6:18 ` [PATCH v2 0/4] Link worktrees with relative paths Eric Sunshine
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=ZwNZRX1LHlxawJJc@ArchLinux \
--to=shejialuo@gmail.com \
--cc=cdwhite3@pm.me \
--cc=git@vger.kernel.org \
/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).