From: Shreyansh Paliwal Date: Fri, 13 Feb 2026 11:59:53 GMT Subject: [RFC][PATCH 1/2] worktree: represent the primary worktree with '/' instead of NULL Message-ID: <20260213120529.15475-2-shreyanshpaliwalcmsmn@gmail.com> In-Reply-To: <20260213120529.15475-1-shreyanshpaliwalcmsmn@gmail.com> The worktree API uses NULL to represent the primary worktree. As a result, many callers pass NULL to implicitly refer to it, which in turn requires additional checks to ensure that `wt` is defined before use. Represent the main worktree explicitly by setting `wt->id` to "/" in `get_main_worktree()` and update `is_main_worktree()` accordingly. Replace checks for wt to be defined in worktree.c functions with calls to `is_main_worktree()` (or strcmp(wt->id, "/")). Signed-off-by: Shreyansh Paliwal --- worktree.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/worktree.c b/worktree.c index 9308389cb6..b29934407f 100644 --- a/worktree.c +++ b/worktree.c @@ -101,6 +101,7 @@ static struct worktree *get_main_worktree(int skip_reading_head) CALLOC_ARRAY(worktree, 1); worktree->repo = the_repository; + worktree->id = xstrdup("/"); worktree->path = strbuf_detach(&worktree_path, NULL); worktree->is_current = is_current_worktree(worktree); worktree->is_bare = (is_bare_repository_cfg == 1) || @@ -127,6 +128,8 @@ struct worktree *get_linked_worktree(const char *id, if (!id) die("Missing linked worktree name"); + if (!strcmp(id, "/")) + die("'/' is reserved for primary worktree"); repo_common_path_append(the_repository, &path, "worktrees/%s/gitdir", id); if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) @@ -206,9 +209,7 @@ struct worktree **get_worktrees_without_reading_head(void) char *get_worktree_git_dir(const struct worktree *wt) { - if (!wt) - return xstrdup(repo_get_git_dir(the_repository)); - else if (!wt->id) + if (is_main_worktree(wt)) return xstrdup(repo_get_common_dir(the_repository)); else return repo_common_path(the_repository, "worktrees/%s", wt->id); @@ -277,7 +278,7 @@ struct worktree *find_worktree_by_path(struct worktree **list, const char *p) int is_main_worktree(const struct worktree *wt) { - return !wt->id; + return !strcmp(wt->id, "/"); } const char *worktree_lock_reason(struct worktree *wt) @@ -566,7 +567,7 @@ void strbuf_worktree_ref(const struct worktree *wt, { if (parse_worktree_ref(refname, NULL, NULL, NULL) == REF_WORKTREE_CURRENT && - wt && !wt->is_current) { + !wt->is_current) { if (is_main_worktree(wt)) strbuf_addstr(sb, "main-worktree/"); else @@ -629,6 +630,9 @@ static void repair_gitfile(struct worktree *wt, char *path = NULL; int err; + if (is_main_worktree(wt)) + goto done; + /* missing worktree can't be repaired */ if (!file_exists(wt->path)) goto done; -- 2.53.0