@@ -9,7 +9,7 @@ void free_worktrees(struct worktree **worktrees)
99
1010 for (i = 0 ; worktrees [i ]; i ++ ) {
1111 free (worktrees [i ]-> path );
12- free (worktrees [i ]-> git_dir );
12+ free (worktrees [i ]-> id );
1313 free (worktrees [i ]-> head_ref );
1414 free (worktrees [i ]);
1515 }
@@ -74,13 +74,11 @@ static struct worktree *get_main_worktree(void)
7474 struct worktree * worktree = NULL ;
7575 struct strbuf path = STRBUF_INIT ;
7676 struct strbuf worktree_path = STRBUF_INIT ;
77- struct strbuf gitdir = STRBUF_INIT ;
7877 struct strbuf head_ref = STRBUF_INIT ;
7978 int is_bare = 0 ;
8079 int is_detached = 0 ;
8180
82- strbuf_addf (& gitdir , "%s" , absolute_path (get_git_common_dir ()));
83- strbuf_addbuf (& worktree_path , & gitdir );
81+ strbuf_addstr (& worktree_path , absolute_path (get_git_common_dir ()));
8482 is_bare = !strbuf_strip_suffix (& worktree_path , "/.git" );
8583 if (is_bare )
8684 strbuf_strip_suffix (& worktree_path , "/." );
@@ -92,15 +90,14 @@ static struct worktree *get_main_worktree(void)
9290
9391 worktree = xmalloc (sizeof (struct worktree ));
9492 worktree -> path = strbuf_detach (& worktree_path , NULL );
95- worktree -> git_dir = strbuf_detach ( & gitdir , NULL ) ;
93+ worktree -> id = NULL ;
9694 worktree -> is_bare = is_bare ;
9795 worktree -> head_ref = NULL ;
9896 worktree -> is_detached = is_detached ;
9997 add_head_info (& head_ref , worktree );
10098
10199done :
102100 strbuf_release (& path );
103- strbuf_release (& gitdir );
104101 strbuf_release (& worktree_path );
105102 strbuf_release (& head_ref );
106103 return worktree ;
@@ -111,16 +108,13 @@ static struct worktree *get_linked_worktree(const char *id)
111108 struct worktree * worktree = NULL ;
112109 struct strbuf path = STRBUF_INIT ;
113110 struct strbuf worktree_path = STRBUF_INIT ;
114- struct strbuf gitdir = STRBUF_INIT ;
115111 struct strbuf head_ref = STRBUF_INIT ;
116112 int is_detached = 0 ;
117113
118114 if (!id )
119115 die ("Missing linked worktree name" );
120116
121- strbuf_addf (& gitdir , "%s/worktrees/%s" ,
122- absolute_path (get_git_common_dir ()), id );
123- strbuf_addf (& path , "%s/gitdir" , gitdir .buf );
117+ strbuf_git_common_path (& path , "worktrees/%s/gitdir" , id );
124118 if (strbuf_read_file (& worktree_path , path .buf , 0 ) <= 0 )
125119 /* invalid gitdir file */
126120 goto done ;
@@ -140,15 +134,14 @@ static struct worktree *get_linked_worktree(const char *id)
140134
141135 worktree = xmalloc (sizeof (struct worktree ));
142136 worktree -> path = strbuf_detach (& worktree_path , NULL );
143- worktree -> git_dir = strbuf_detach ( & gitdir , NULL );
137+ worktree -> id = xstrdup ( id );
144138 worktree -> is_bare = 0 ;
145139 worktree -> head_ref = NULL ;
146140 worktree -> is_detached = is_detached ;
147141 add_head_info (& head_ref , worktree );
148142
149143done :
150144 strbuf_release (& path );
151- strbuf_release (& gitdir );
152145 strbuf_release (& worktree_path );
153146 strbuf_release (& head_ref );
154147 return worktree ;
@@ -188,6 +181,16 @@ struct worktree **get_worktrees(void)
188181 return list ;
189182}
190183
184+ const char * get_worktree_git_dir (const struct worktree * wt )
185+ {
186+ if (!wt )
187+ return get_git_dir ();
188+ else if (!wt -> id )
189+ return get_git_common_dir ();
190+ else
191+ return git_common_path ("worktrees/%s" , wt -> id );
192+ }
193+
191194char * find_shared_symref (const char * symref , const char * target )
192195{
193196 char * existing = NULL ;
@@ -199,7 +202,9 @@ char *find_shared_symref(const char *symref, const char *target)
199202 for (i = 0 ; worktrees [i ]; i ++ ) {
200203 strbuf_reset (& path );
201204 strbuf_reset (& sb );
202- strbuf_addf (& path , "%s/%s" , worktrees [i ]-> git_dir , symref );
205+ strbuf_addf (& path , "%s/%s" ,
206+ get_worktree_git_dir (worktrees [i ]),
207+ symref );
203208
204209 if (parse_ref (path .buf , & sb , NULL )) {
205210 continue ;
0 commit comments