@@ -120,12 +120,14 @@ struct add_opts {
120120 int quiet ;
121121 int checkout ;
122122 int orphan ;
123+ int relative_paths ;
123124 const char * keep_locked ;
124125};
125126
126127static int show_only ;
127128static int verbose ;
128129static int guess_remote ;
130+ static int use_relative_paths ;
129131static timestamp_t expire ;
130132
131133static int git_worktree_config (const char * var , const char * value ,
@@ -134,6 +136,9 @@ static int git_worktree_config(const char *var, const char *value,
134136 if (!strcmp (var , "worktree.guessremote" )) {
135137 guess_remote = git_config_bool (var , value );
136138 return 0 ;
139+ } else if (!strcmp (var , "worktree.userelativepaths" )) {
140+ use_relative_paths = git_config_bool (var , value );
141+ return 0 ;
137142 }
138143
139144 return git_default_config (var , value , ctx , cb );
@@ -415,8 +420,7 @@ static int add_worktree(const char *path, const char *refname,
415420 const struct add_opts * opts )
416421{
417422 struct strbuf sb_git = STRBUF_INIT , sb_repo = STRBUF_INIT ;
418- struct strbuf sb = STRBUF_INIT , sb_tmp = STRBUF_INIT ;
419- struct strbuf sb_path_realpath = STRBUF_INIT , sb_repo_realpath = STRBUF_INIT ;
423+ struct strbuf sb = STRBUF_INIT ;
420424 const char * name ;
421425 struct strvec child_env = STRVEC_INIT ;
422426 unsigned int counter = 0 ;
@@ -492,10 +496,7 @@ static int add_worktree(const char *path, const char *refname,
492496
493497 strbuf_reset (& sb );
494498 strbuf_addf (& sb , "%s/gitdir" , sb_repo .buf );
495- strbuf_realpath (& sb_path_realpath , path , 1 );
496- strbuf_realpath (& sb_repo_realpath , sb_repo .buf , 1 );
497- write_file (sb .buf , "%s/.git" , relative_path (sb_path_realpath .buf , sb_repo_realpath .buf , & sb_tmp ));
498- write_file (sb_git .buf , "gitdir: %s" , relative_path (sb_repo_realpath .buf , sb_path_realpath .buf , & sb_tmp ));
499+ write_worktree_linking_files (sb_git , sb , opts -> relative_paths );
499500 strbuf_reset (& sb );
500501 strbuf_addf (& sb , "%s/commondir" , sb_repo .buf );
501502 write_file (sb .buf , "../.." );
@@ -579,12 +580,9 @@ static int add_worktree(const char *path, const char *refname,
579580
580581 strvec_clear (& child_env );
581582 strbuf_release (& sb );
582- strbuf_release (& sb_tmp );
583583 strbuf_release (& symref );
584584 strbuf_release (& sb_repo );
585- strbuf_release (& sb_repo_realpath );
586585 strbuf_release (& sb_git );
587- strbuf_release (& sb_path_realpath );
588586 strbuf_release (& sb_name );
589587 free_worktree (wt );
590588 return ret ;
@@ -798,12 +796,15 @@ static int add(int ac, const char **av, const char *prefix,
798796 PARSE_OPT_NOARG | PARSE_OPT_OPTARG ),
799797 OPT_BOOL (0 , "guess-remote" , & guess_remote ,
800798 N_ ("try to match the new branch name with a remote-tracking branch" )),
799+ OPT_BOOL (0 , "relative-paths" , & opts .relative_paths ,
800+ N_ ("use relative paths for worktrees" )),
801801 OPT_END ()
802802 };
803803 int ret ;
804804
805805 memset (& opts , 0 , sizeof (opts ));
806806 opts .checkout = 1 ;
807+ opts .relative_paths = use_relative_paths ;
807808 ac = parse_options (ac , av , prefix , options , git_worktree_add_usage , 0 );
808809 if (!!opts .detach + !!new_branch + !!new_branch_force > 1 )
809810 die (_ ("options '%s', '%s', and '%s' cannot be used together" ), "-b" , "-B" , "--detach" );
@@ -1195,6 +1196,8 @@ static int move_worktree(int ac, const char **av, const char *prefix,
11951196 OPT__FORCE (& force ,
11961197 N_ ("force move even if worktree is dirty or locked" ),
11971198 PARSE_OPT_NOCOMPLETE ),
1199+ OPT_BOOL (0 , "relative-paths" , & use_relative_paths ,
1200+ N_ ("use relative paths for worktrees" )),
11981201 OPT_END ()
11991202 };
12001203 struct worktree * * worktrees , * wt ;
@@ -1247,7 +1250,7 @@ static int move_worktree(int ac, const char **av, const char *prefix,
12471250 if (rename (wt -> path , dst .buf ) == -1 )
12481251 die_errno (_ ("failed to move '%s' to '%s'" ), wt -> path , dst .buf );
12491252
1250- update_worktree_location (wt , dst .buf );
1253+ update_worktree_location (wt , dst .buf , use_relative_paths );
12511254
12521255 strbuf_release (& dst );
12531256 free_worktrees (worktrees );
@@ -1390,15 +1393,17 @@ static int repair(int ac, const char **av, const char *prefix,
13901393 const char * * p ;
13911394 const char * self [] = { "." , NULL };
13921395 struct option options [] = {
1396+ OPT_BOOL (0 , "relative-paths" , & use_relative_paths ,
1397+ N_ ("use relative paths for worktrees" )),
13931398 OPT_END ()
13941399 };
13951400 int rc = 0 ;
13961401
13971402 ac = parse_options (ac , av , prefix , options , git_worktree_repair_usage , 0 );
13981403 p = ac > 0 ? av : self ;
13991404 for (; * p ; p ++ )
1400- repair_worktree_at_path (* p , report_repair , & rc );
1401- repair_worktrees (report_repair , & rc );
1405+ repair_worktree_at_path (* p , report_repair , & rc , use_relative_paths );
1406+ repair_worktrees (report_repair , & rc , use_relative_paths );
14021407 return rc ;
14031408}
14041409
0 commit comments