From: Caleb White <cdwhite3@pm.me>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Eric Sunshine <sunshine@sunshineco.com>,
Phillip Wood <phillip.wood123@gmail.com>,
shejialuo <shejialuo@gmail.com>,
Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com>,
Caleb White <cdwhite3@pm.me>
Subject: [PATCH v3 1/5] worktree: refactor infer_backlink() to use strbuf*
Date: Fri, 25 Oct 2024 20:57:25 +0000 [thread overview]
Message-ID: <20241025-wt_relative_paths-v3-1-8860a5321c01@pm.me> (raw)
In-Reply-To: <20241025-wt_relative_paths-v3-0-8860a5321c01@pm.me>
The infer_backlink() function initializes a `strbuf` for the inferred
backlink and returns the result as a `char*` by detaching the `strbuf`.
The next patch needs the backlink returned from infer_backlink() as a
`strbuf`. It seemed inefficient to convert from `strbuf` to `char*`
and back to `strbuf` again. This refactors infer_backlink() to return
an integer result and accept a pre-allocated `strbuf` for the inferred
backlink path, improving efficiency.
Signed-off-by: Caleb White <cdwhite3@pm.me>
---
worktree.c | 52 ++++++++++++++++++++++++++--------------------------
1 file changed, 26 insertions(+), 26 deletions(-)
diff --git a/worktree.c b/worktree.c
index ec95ea2986107b3bc12d38b0825d7c6e87402bc6..ad60ba0b5843f1676e89b05eca3c82aace5fb49b 100644
--- a/worktree.c
+++ b/worktree.c
@@ -641,13 +641,15 @@ static int is_main_worktree_path(const char *path)
* won't know which <repo>/worktrees/<id>/gitdir to repair. However, we may
* be able to infer the gitdir by manually reading /path/to/worktree/.git,
* extracting the <id>, and checking if <repo>/worktrees/<id> exists.
+ *
+ * Returns -1 on failure and strbuf.len on success.
*/
-static char *infer_backlink(const char *gitfile)
+static int infer_backlink(const char *gitfile, struct strbuf *inferred)
{
struct strbuf actual = STRBUF_INIT;
- struct strbuf inferred = STRBUF_INIT;
const char *id;
+ strbuf_reset(inferred);
if (strbuf_read_file(&actual, gitfile, 0) < 0)
goto error;
if (!starts_with(actual.buf, "gitdir:"))
@@ -658,17 +660,16 @@ static char *infer_backlink(const char *gitfile)
id++; /* advance past '/' to point at <id> */
if (!*id)
goto error;
- strbuf_git_common_path(&inferred, the_repository, "worktrees/%s", id);
- if (!is_directory(inferred.buf))
+ strbuf_git_common_path(inferred, the_repository, "worktrees/%s", id);
+ if (!is_directory(inferred->buf))
goto error;
strbuf_release(&actual);
- return strbuf_detach(&inferred, NULL);
-
+ return inferred->len;
error:
strbuf_release(&actual);
- strbuf_release(&inferred);
- return NULL;
+ strbuf_reset(inferred); /* clear invalid path */
+ return -1;
}
/*
@@ -680,10 +681,11 @@ void repair_worktree_at_path(const char *path,
{
struct strbuf dotgit = STRBUF_INIT;
struct strbuf realdotgit = STRBUF_INIT;
+ struct strbuf backlink = STRBUF_INIT;
+ struct strbuf inferred_backlink = STRBUF_INIT;
struct strbuf gitdir = STRBUF_INIT;
struct strbuf olddotgit = STRBUF_INIT;
- char *backlink = NULL;
- char *inferred_backlink = NULL;
+ char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -699,23 +701,23 @@ void repair_worktree_at_path(const char *path,
goto done;
}
- inferred_backlink = infer_backlink(realdotgit.buf);
- backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
- if (err == READ_GITFILE_ERR_NOT_A_FILE) {
+ infer_backlink(realdotgit.buf, &inferred_backlink);
+ dotgit_contents = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
+ if (dotgit_contents) {
+ strbuf_addstr(&backlink, dotgit_contents);
+ } else if (err == READ_GITFILE_ERR_NOT_A_FILE) {
fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
goto done;
} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
- if (inferred_backlink) {
+ if (inferred_backlink.len) {
/*
* Worktree's .git file does not point at a repository
* but we found a .git/worktrees/<id> in this
* repository with the same <id> as recorded in the
* worktree's .git file so make the worktree point at
- * the discovered .git/worktrees/<id>. (Note: backlink
- * is already NULL, so no need to free it first.)
+ * the discovered .git/worktrees/<id>.
*/
- backlink = inferred_backlink;
- inferred_backlink = NULL;
+ strbuf_swap(&backlink, &inferred_backlink);
} else {
fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
goto done;
@@ -743,13 +745,10 @@ void repair_worktree_at_path(const char *path,
* in the "copy" repository. In this case, point the "copy" worktree's
* .git file at the "copy" repository.
*/
- if (inferred_backlink && fspathcmp(backlink, inferred_backlink)) {
- free(backlink);
- backlink = inferred_backlink;
- inferred_backlink = NULL;
- }
+ if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))
+ strbuf_swap(&backlink, &inferred_backlink);
- strbuf_addf(&gitdir, "%s/gitdir", backlink);
+ strbuf_addf(&gitdir, "%s/gitdir", backlink.buf);
if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
repair = _("gitdir unreadable");
else {
@@ -763,9 +762,10 @@ void repair_worktree_at_path(const char *path,
write_file(gitdir.buf, "%s", realdotgit.buf);
}
done:
- free(backlink);
- free(inferred_backlink);
+ free(dotgit_contents);
strbuf_release(&olddotgit);
+ strbuf_release(&backlink);
+ strbuf_release(&inferred_backlink);
strbuf_release(&gitdir);
strbuf_release(&realdotgit);
strbuf_release(&dotgit);
--
2.47.0
next prev parent reply other threads:[~2024-10-25 20:57 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-25 20:57 [PATCH v3 0/5] Optionally link worktrees with relative paths Caleb White
2024-10-25 20:57 ` Caleb White [this message]
2024-10-25 20:57 ` [PATCH v3 2/5] worktree: support worktrees linked " Caleb White
2024-10-25 20:57 ` [PATCH v3 3/5] worktree: optionally link worktrees " Caleb White
2024-10-25 20:57 ` [PATCH v3 4/5] worktree: add test for path handling in linked worktrees Caleb White
2024-10-25 20:57 ` [PATCH v3 5/5] worktree: add `relativeWorktrees` extension Caleb White
2024-10-25 20:59 ` [PATCH v3 0/5] Optionally link worktrees with relative paths Caleb White
2024-10-25 21:05 ` Taylor Blau
2024-10-25 21:11 ` Caleb White
2024-10-25 21:38 ` Taylor Blau
2024-10-25 22:34 ` Caleb White
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=20241025-wt_relative_paths-v3-1-8860a5321c01@pm.me \
--to=cdwhite3@pm.me \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kristofferhaugsbakk@fastmail.com \
--cc=phillip.wood123@gmail.com \
--cc=shejialuo@gmail.com \
--cc=sunshine@sunshineco.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.
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).