@@ -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 );
@@ -414,8 +419,7 @@ static int add_worktree(const char *path, const char *refname,
414419 const struct add_opts * opts )
415420{
416421 struct strbuf sb_git = STRBUF_INIT , sb_repo = STRBUF_INIT ;
417- struct strbuf sb = STRBUF_INIT , sb_tmp = STRBUF_INIT ;
418- struct strbuf sb_path_realpath = STRBUF_INIT , sb_repo_realpath = STRBUF_INIT ;
422+ struct strbuf sb = STRBUF_INIT ;
419423 const char * name ;
420424 struct strvec child_env = STRVEC_INIT ;
421425 unsigned int counter = 0 ;
@@ -491,10 +495,7 @@ static int add_worktree(const char *path, const char *refname,
491495
492496 strbuf_reset (& sb );
493497 strbuf_addf (& sb , "%s/gitdir" , sb_repo .buf );
494- strbuf_realpath (& sb_path_realpath , path , 1 );
495- strbuf_realpath (& sb_repo_realpath , sb_repo .buf , 1 );
496- write_file (sb .buf , "%s/.git" , relative_path (sb_path_realpath .buf , sb_repo_realpath .buf , & sb_tmp ));
497- write_file (sb_git .buf , "gitdir: %s" , relative_path (sb_repo_realpath .buf , sb_path_realpath .buf , & sb_tmp ));
498+ write_worktree_linking_files (sb_git , sb , opts -> relative_paths );
498499 strbuf_reset (& sb );
499500 strbuf_addf (& sb , "%s/commondir" , sb_repo .buf );
500501 write_file (sb .buf , "../.." );
@@ -578,12 +579,9 @@ static int add_worktree(const char *path, const char *refname,
578579
579580 strvec_clear (& child_env );
580581 strbuf_release (& sb );
581- strbuf_release (& sb_tmp );
582582 strbuf_release (& symref );
583583 strbuf_release (& sb_repo );
584- strbuf_release (& sb_repo_realpath );
585584 strbuf_release (& sb_git );
586- strbuf_release (& sb_path_realpath );
587585 strbuf_release (& sb_name );
588586 free_worktree (wt );
589587 return ret ;
@@ -796,12 +794,15 @@ static int add(int ac, const char **av, const char *prefix)
796794 PARSE_OPT_NOARG | PARSE_OPT_OPTARG ),
797795 OPT_BOOL (0 , "guess-remote" , & guess_remote ,
798796 N_ ("try to match the new branch name with a remote-tracking branch" )),
797+ OPT_BOOL (0 , "relative-paths" , & opts .relative_paths ,
798+ N_ ("use relative paths for worktrees" )),
799799 OPT_END ()
800800 };
801801 int ret ;
802802
803803 memset (& opts , 0 , sizeof (opts ));
804804 opts .checkout = 1 ;
805+ opts .relative_paths = use_relative_paths ;
805806 ac = parse_options (ac , av , prefix , options , git_worktree_add_usage , 0 );
806807 if (!!opts .detach + !!new_branch + !!new_branch_force > 1 )
807808 die (_ ("options '%s', '%s', and '%s' cannot be used together" ), "-b" , "-B" , "--detach" );
@@ -1189,6 +1190,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
11891190 OPT__FORCE (& force ,
11901191 N_ ("force move even if worktree is dirty or locked" ),
11911192 PARSE_OPT_NOCOMPLETE ),
1193+ OPT_BOOL (0 , "relative-paths" , & use_relative_paths ,
1194+ N_ ("use relative paths for worktrees" )),
11921195 OPT_END ()
11931196 };
11941197 struct worktree * * worktrees , * wt ;
@@ -1241,7 +1244,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
12411244 if (rename (wt -> path , dst .buf ) == -1 )
12421245 die_errno (_ ("failed to move '%s' to '%s'" ), wt -> path , dst .buf );
12431246
1244- update_worktree_location (wt , dst .buf );
1247+ update_worktree_location (wt , dst .buf , use_relative_paths );
12451248
12461249 strbuf_release (& dst );
12471250 free_worktrees (worktrees );
@@ -1382,15 +1385,17 @@ static int repair(int ac, const char **av, const char *prefix)
13821385 const char * * p ;
13831386 const char * self [] = { "." , NULL };
13841387 struct option options [] = {
1388+ OPT_BOOL (0 , "relative-paths" , & use_relative_paths ,
1389+ N_ ("use relative paths for worktrees" )),
13851390 OPT_END ()
13861391 };
13871392 int rc = 0 ;
13881393
13891394 ac = parse_options (ac , av , prefix , options , git_worktree_repair_usage , 0 );
13901395 p = ac > 0 ? av : self ;
13911396 for (; * p ; p ++ )
1392- repair_worktree_at_path (* p , report_repair , & rc );
1393- repair_worktrees (report_repair , & rc );
1397+ repair_worktree_at_path (* p , report_repair , & rc , use_relative_paths );
1398+ repair_worktrees (report_repair , & rc , use_relative_paths );
13941399 return rc ;
13951400}
13961401
0 commit comments