worktree: refactor infer_backlink return

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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Caleb White
2024-11-29 22:22:41 +00:00
committed by Junio C Hamano
parent 1860ba1a2a
commit 5976310916

View File

@ -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,8 +725,10 @@ 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;
@ -747,12 +749,11 @@ static int infer_backlink(const char *gitfile, struct strbuf *inferred)
goto error;
strbuf_release(&actual);
return 1;
return inferred->len;
error:
strbuf_release(&actual);
strbuf_reset(inferred); /* clear invalid path */
return 0;
return -1;
}
/*