@@ -183,7 +183,7 @@ char *get_worktree_git_dir(const struct worktree *wt)
183183 else if (!wt -> id )
184184 return xstrdup (repo_get_common_dir (the_repository ));
185185 else
186- return xstrdup ( git_common_path ( "worktrees/%s" , wt -> id ) );
186+ return repo_common_path ( the_repository , "worktrees/%s" , wt -> id );
187187}
188188
189189static struct worktree * find_worktree_by_suffix (struct worktree * * list ,
@@ -314,6 +314,7 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
314314{
315315 struct strbuf wt_path = STRBUF_INIT ;
316316 struct strbuf realpath = STRBUF_INIT ;
317+ struct strbuf buf = STRBUF_INIT ;
317318 char * path = NULL ;
318319 int err , ret = -1 ;
319320
@@ -343,7 +344,7 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
343344 if (!is_absolute_path (wt -> path )) {
344345 strbuf_addf_gently (errmsg ,
345346 _ ("'%s' file does not contain absolute path to the working tree location" ),
346- git_common_path ( "worktrees/%s/gitdir" , wt -> id ));
347+ repo_common_path_replace ( the_repository , & buf , "worktrees/%s/gitdir" , wt -> id ));
347348 goto done ;
348349 }
349350
@@ -365,14 +366,16 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
365366 goto done ;
366367 }
367368
368- strbuf_realpath (& realpath , git_common_path ( "worktrees/%s" , wt -> id ), 1 );
369+ strbuf_realpath (& realpath , repo_common_path_replace ( the_repository , & buf , "worktrees/%s" , wt -> id ), 1 );
369370 ret = fspathcmp (path , realpath .buf );
370371
371372 if (ret )
372373 strbuf_addf_gently (errmsg , _ ("'%s' does not point back to '%s'" ),
373- wt -> path , git_common_path ("worktrees/%s" , wt -> id ));
374+ wt -> path , repo_common_path_replace (the_repository , & buf ,
375+ "worktrees/%s" , wt -> id ));
374376done :
375377 free (path );
378+ strbuf_release (& buf );
376379 strbuf_release (& wt_path );
377380 strbuf_release (& realpath );
378381 return ret ;
@@ -384,11 +387,13 @@ void update_worktree_location(struct worktree *wt, const char *path_,
384387 struct strbuf path = STRBUF_INIT ;
385388 struct strbuf dotgit = STRBUF_INIT ;
386389 struct strbuf gitdir = STRBUF_INIT ;
390+ char * wt_gitdir ;
387391
388392 if (is_main_worktree (wt ))
389393 BUG ("can't relocate main worktree" );
390394
391- strbuf_realpath (& gitdir , git_common_path ("worktrees/%s/gitdir" , wt -> id ), 1 );
395+ wt_gitdir = repo_common_path (the_repository , "worktrees/%s/gitdir" , wt -> id );
396+ strbuf_realpath (& gitdir , wt_gitdir , 1 );
392397 strbuf_realpath (& path , path_ , 1 );
393398 strbuf_addf (& dotgit , "%s/.git" , path .buf );
394399 if (fspathcmp (wt -> path , path .buf )) {
@@ -400,6 +405,7 @@ void update_worktree_location(struct worktree *wt, const char *path_,
400405 strbuf_release (& path );
401406 strbuf_release (& dotgit );
402407 strbuf_release (& gitdir );
408+ free (wt_gitdir );
403409}
404410
405411int is_worktree_being_rebased (const struct worktree * wt ,
@@ -585,6 +591,7 @@ static void repair_gitfile(struct worktree *wt,
585591 struct strbuf backlink = STRBUF_INIT ;
586592 char * dotgit_contents = NULL ;
587593 const char * repair = NULL ;
594+ char * path = NULL ;
588595 int err ;
589596
590597 /* missing worktree can't be repaired */
@@ -596,7 +603,8 @@ static void repair_gitfile(struct worktree *wt,
596603 goto done ;
597604 }
598605
599- strbuf_realpath (& repo , git_common_path ("worktrees/%s" , wt -> id ), 1 );
606+ path = repo_common_path (the_repository , "worktrees/%s" , wt -> id );
607+ strbuf_realpath (& repo , path , 1 );
600608 strbuf_addf (& dotgit , "%s/.git" , wt -> path );
601609 strbuf_addf (& gitdir , "%s/gitdir" , repo .buf );
602610 dotgit_contents = xstrdup_or_null (read_gitfile_gently (dotgit .buf , & err ));
@@ -626,6 +634,7 @@ static void repair_gitfile(struct worktree *wt,
626634
627635done :
628636 free (dotgit_contents );
637+ free (path );
629638 strbuf_release (& repo );
630639 strbuf_release (& dotgit );
631640 strbuf_release (& gitdir );
@@ -657,11 +666,13 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
657666 struct strbuf gitdir = STRBUF_INIT ;
658667 struct strbuf dotgit = STRBUF_INIT ;
659668 int is_relative_path ;
669+ char * path = NULL ;
660670
661671 if (is_main_worktree (wt ))
662672 goto done ;
663673
664- strbuf_realpath (& gitdir , git_common_path ("worktrees/%s/gitdir" , wt -> id ), 1 );
674+ path = repo_common_path (the_repository , "worktrees/%s/gitdir" , wt -> id );
675+ strbuf_realpath (& gitdir , path , 1 );
665676
666677 if (strbuf_read_file (& dotgit , gitdir .buf , 0 ) < 0 )
667678 goto done ;
@@ -680,6 +691,7 @@ void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path
680691done :
681692 strbuf_release (& gitdir );
682693 strbuf_release (& dotgit );
694+ free (path );
683695}
684696
685697void repair_worktrees_after_gitdir_move (const char * old_path )
@@ -871,7 +883,11 @@ int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath,
871883 ssize_t read_result ;
872884
873885 * wtpath = NULL ;
874- strbuf_realpath (& repo , git_common_path ("worktrees/%s" , id ), 1 );
886+
887+ path = repo_common_path (the_repository , "worktrees/%s" , id );
888+ strbuf_realpath (& repo , path , 1 );
889+ FREE_AND_NULL (path );
890+
875891 strbuf_addf (& gitdir , "%s/gitdir" , repo .buf );
876892 if (!is_directory (repo .buf )) {
877893 strbuf_addstr (reason , _ ("not a valid directory" ));
0 commit comments