git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Caleb White <cdwhite3@pm.me>
To: git@vger.kernel.org
Cc: Taylor Blau <me@ttaylorr.com>,
	Phillip Wood <phillip.wood123@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Caleb White <cdwhite3@pm.me>
Subject: [PATCH v3 3/8] worktree: refactor infer_backlink return
Date: Thu, 31 Oct 2024 05:05:42 +0000	[thread overview]
Message-ID: <20241031-wt_relative_options-v3-3-3e44ccdf64e6@pm.me> (raw)
In-Reply-To: <20241031-wt_relative_options-v3-0-3e44ccdf64e6@pm.me>

The previous round[1] was merged a bit early before reviewer feedback
could be applied. This correctly indents a code block and updates the
`infer_backlink` function to return `-1` on failure and strbuf.len on
success.

[1]: https://lore.kernel.org/git/20241007-wt_relative_paths-v3-0-622cf18c45eb@pm.me

Signed-off-by: Caleb White <cdwhite3@pm.me>
---
 worktree.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/worktree.c b/worktree.c
index 77ff484d3ec48c547ee4e3d958dfa28a52c1eaa7..9346d51c438cbd029e9e57591edd8c9f30cc7638 100644
--- a/worktree.c
+++ b/worktree.c
@@ -111,9 +111,9 @@ struct worktree *get_linked_worktree(const char *id,
 	strbuf_strip_suffix(&worktree_path, "/.git");
 
 	if (!is_absolute_path(worktree_path.buf)) {
-	    strbuf_strip_suffix(&path, "gitdir");
-	    strbuf_addbuf(&path, &worktree_path);
-	    strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
+		strbuf_strip_suffix(&path, "gitdir");
+		strbuf_addbuf(&path, &worktree_path);
+		strbuf_realpath_forgiving(&worktree_path, path.buf, 0);
 	}
 
 	CALLOC_ARRAY(worktree, 1);
@@ -725,12 +725,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 int infer_backlink(const char *gitfile, struct strbuf *inferred)
+static ssize_t infer_backlink(const char *gitfile, struct strbuf *inferred)
 {
 	struct strbuf actual = STRBUF_INIT;
 	const char *id;
 
+	strbuf_reset(inferred);
 	if (strbuf_read_file(&actual, gitfile, 0) < 0)
 		goto error;
 	if (!starts_with(actual.buf, "gitdir:"))
@@ -741,18 +744,16 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)
 	id++; /* advance past '/' to point at <id> */
 	if (!*id)
 		goto error;
-	strbuf_reset(inferred);
 	strbuf_git_common_path(inferred, the_repository, "worktrees/%s", id);
 	if (!is_directory(inferred->buf))
 		goto error;
 
 	strbuf_release(&actual);
-	return 1;
-
+	return inferred->len;
 error:
 	strbuf_release(&actual);
 	strbuf_reset(inferred); /* clear invalid path */
-	return 0;
+	return -1;
 }
 
 /*

-- 
2.47.0



  parent reply	other threads:[~2024-10-31  5:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-31  5:05 [PATCH v3 0/8] Allow relative worktree linking to be configured by the user Caleb White
2024-10-31  5:05 ` [PATCH v3 1/8] setup: correctly reinitialize repository version Caleb White
2024-10-31  5:05 ` [PATCH v3 2/8] worktree: add `relativeWorktrees` extension Caleb White
2024-10-31  5:05 ` Caleb White [this message]
2024-10-31  5:05 ` [PATCH v3 4/8] worktree: add `write_worktree_linking_files()` function Caleb White
2024-10-31  5:05 ` [PATCH v3 5/8] worktree: add relative cli/config options to `add` command Caleb White
2024-10-31  5:06 ` [PATCH v3 6/8] worktree: add relative cli/config options to `move` command Caleb White
2024-10-31  5:06 ` [PATCH v3 7/8] worktree: add relative cli/config options to `repair` command Caleb White
2024-10-31  5:06 ` [PATCH v3 8/8] worktree: refactor `repair_worktree_after_gitdir_move()` Caleb White
2024-10-31 19:20 ` [PATCH v3 0/8] Allow relative worktree linking to be configured by the user Taylor Blau
2024-10-31 19:52   ` 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=20241031-wt_relative_options-v3-3-3e44ccdf64e6@pm.me \
    --to=cdwhite3@pm.me \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    --cc=phillip.wood123@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).