git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC][PATCH 1/2] worktree: represent the primary worktree with '/' instead of NULL

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 13, 2026, 21:35 UTC
Message-ID
<xmqq7bsgl42j.fsf@gitster.g>
In-Reply-To
<20260213120529.15475-2-shreyanshpaliwalcmsmn@gmail.com>
Shreyansh Paliwal <shreyanshpaliwalcmsmn@gmail.com> writes:
Show 12 quoted lines
> 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) ||

Presumably we left .id = NULL from CALLOC_ARRAY(), so this looks sensible. When releasing resources from an instance of worktree, we'd blindly free(worktree->id) and in the old world, free(NULL) turned into no-op, and this xstrdup()'d copy will be freed in the new world, so there is nothing funny here, I hope? This one, and the change to is_main_worktree() go together.

Show 6 quoted lines
> @@ -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");
Makes me wonder if this is a BUG not die; where does id come from?
	... goes and looks ...

The only caller is worktree.c:get_worktrees_internal() and it is feeding d->d_name that came from readdir_skip_dot_and_dotdot(), so it cannot be "/".

By the way, I suspect that get_linked_worktree() should become file-scope static, as there is no other caller.

Show 11 quoted lines
> @@ -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);

Good spotting. This series needs to spot any and all places that use these other conventions (i.e. wt->id == NULL) to identify the primary worktree and rewrite them to call is_main_worktree(), which may be a chore, but once it is done, it would become a lot easier to follow the resulting code.

Show 7 quoted lines
> @@ -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, "/");
>  }
OK.
Show 6 quoted lines
> @@ -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) {
OK.
Show 6 quoted lines
> @@ -629,6 +630,9 @@ static void repair_gitfile(struct worktree *wt,
>  	char *path = NULL;
>  	int err;
>  
> +	if (is_main_worktree(wt))
> +		goto done;
This is a bit new.
The original did not say 
	if (!wt || !wt->id || !strcmp(wt->id, "/"))
		goto done;

The only caller is iterating over the resulting list of worktrees returned from get_worktrees_internal(1) *BUT* it already skips the primary worktree (the function MUST return the primary one as the first one, and the callers MUST be aware of the convention).

So I am not sure if the new check is even needed.  Or rather, this ...
	if (!wt || !wt->id || !strcmp(wt->id, "/"))
		BUG("why are you feeding me the primary worktree???");
	
... might be more appropriate, perhaps?  I dunno.
>  	/* missing worktree can't be repaired */
>  	if (!file_exists(wt->path))
>  		goto done;
Previous: Shreyansh PaliwalNext: Shreyansh Paliwal
Message 3 of 39 in “worktree: change representation and usage of primary worktree”
  1. Shreyansh PaliwalFeb 13, 2026
  2. [RFC][PATCH 1/2] worktree: represent the primary worktree with '/' instead of NULLShreyansh Paliwal, Feb 13, 2026
  3. Junio C HamanoFeb 13, 2026
  4. Shreyansh PaliwalFeb 14, 2026
  5. [RFC][PATCH 2/2] worktree: stop passing NULL as primary worktreeShreyansh Paliwal, Feb 13, 2026
  6. Junio C HamanoFeb 13, 2026
  7. Shreyansh PaliwalFeb 14, 2026
  8. Phillip WoodFeb 14, 2026
  9. Junio C HamanoFeb 14, 2026
  10. Shreyansh PaliwalFeb 15, 2026
  11. Phillip WoodFeb 16, 2026
  12. Junio C HamanoFeb 17, 2026
  13. Shreyansh PaliwalFeb 17, 2026
  14. 0/2 worktree_git_path(): remove repository argumentPhillip Wood, Feb 16, 2026
  15. 1/2 wt-status: avoid passing NULL worktreePhillip Wood, Feb 16, 2026
  16. Phillip WoodFeb 17, 2026
  17. Shreyansh PaliwalFeb 17, 2026
  18. Phillip WoodFeb 17, 2026
  19. Shreyansh PaliwalFeb 17, 2026
  20. Junio C HamanoFeb 17, 2026
  21. Karthik NayakFeb 17, 2026
  22. Phillip WoodFeb 18, 2026
  23. Junio C HamanoFeb 17, 2026
  24. Phillip WoodFeb 18, 2026
  25. 2/2 path: remove repository argument from worktree_git_path()Phillip Wood, Feb 16, 2026
  26. Karthik NayakFeb 17, 2026
  27. Shreyansh PaliwalFeb 17, 2026
  28. Phillip WoodFeb 17, 2026
  29. Shreyansh PaliwalFeb 17, 2026
  30. 0/2 worktree_git_path(): remove repository argumentPhillip Wood, Feb 19, 2026
  31. 1/2 wt-status: avoid passing NULL worktreePhillip Wood, Feb 19, 2026
  32. Junio C HamanoFeb 19, 2026
  33. Junio C HamanoFeb 19, 2026
  34. Phillip WoodFeb 25, 2026
  35. Junio C HamanoFeb 25, 2026
  36. Phillip WoodFeb 26, 2026
  37. Junio C HamanoFeb 26, 2026
  38. 2/2 path: remove repository argument from worktree_git_path()Phillip Wood, Feb 19, 2026
  39. Junio C HamanoFeb 19, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.