File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change 11use std:: path:: { Path , PathBuf } ;
22
3+ use crate :: bstr:: BStr ;
34use gix_path:: realpath:: MAX_SYMLINKS ;
45
56impl crate :: Repository {
@@ -59,6 +60,16 @@ impl crate::Repository {
5960 self . work_tree . as_deref ( )
6061 }
6162
63+ /// Turn `rela_path` into a path qualified with the [`workdir()`](Self::workdir()) of this instance,
64+ /// if one is available.
65+ pub fn workdir_path ( & self , rela_path : impl AsRef < BStr > ) -> Option < PathBuf > {
66+ self . workdir ( ) . and_then ( |wd| {
67+ gix_path:: try_from_bstr ( rela_path. as_ref ( ) )
68+ . ok ( )
69+ . map ( |rela| wd. join ( rela) )
70+ } )
71+ }
72+
6273 // TODO: tests, respect precomposeUnicode
6374 /// The directory of the binary path of the current process.
6475 pub fn install_dir ( & self ) -> std:: io:: Result < PathBuf > {
Original file line number Diff line number Diff line change 1+ use crate :: util:: named_subrepo_opts;
2+ use gix:: bstr:: BString ;
13use std:: borrow:: Cow ;
24use std:: error:: Error ;
35
4- use crate :: util:: named_subrepo_opts;
5-
66#[ test]
77fn on_root_with_decomposed_unicode ( ) -> crate :: Result {
88 let tmp = gix_testtools:: tempfile:: TempDir :: new ( ) ?;
@@ -47,6 +47,10 @@ fn on_root_with_decomposed_unicode() -> crate::Result {
4747 Cow :: Owned ( _) ,
4848 ) ) ;
4949 }
50+ assert ! (
51+ repo. workdir_path( "" ) . expect( "non-bare" ) . is_dir( ) ,
52+ "decomposed or not, we generate a valid path given what Git would store"
53+ ) ;
5054
5155 Ok ( ( ) )
5256}
@@ -129,6 +133,21 @@ fn none_bare_repo_without_index() -> crate::Result {
129133 ) ?;
130134 assert ! ( !repo. is_bare( ) , "worktree isn't dependent on an index file" ) ;
131135 assert ! ( repo. worktree( ) . is_some( ) ) ;
136+ assert_eq ! (
137+ repo. workdir_path( BString :: from( "this" ) ) . map( |p| p. is_file( ) ) ,
138+ Some ( true )
139+ ) ;
140+ assert_eq ! (
141+ repo. workdir_path( & BString :: from( "this" ) ) . map( |p| p. is_file( ) ) ,
142+ Some ( true )
143+ ) ;
144+ assert ! (
145+ repo. workdir_path( "this" )
146+ . expect( "non-bare" )
147+ . strip_prefix( repo. workdir( ) . expect( "non-bare" ) )
148+ . is_ok( ) ,
149+ "this is a minimal path"
150+ ) ;
132151 Ok ( ( ) )
133152}
134153
You can’t perform that action at this time.
0 commit comments