File tree 2 files changed +32
-2
lines changed
2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change 1
1
use std:: path:: { Path , PathBuf } ;
2
2
3
+ use crate :: bstr:: BStr ;
3
4
use gix_path:: realpath:: MAX_SYMLINKS ;
4
5
5
6
impl crate :: Repository {
@@ -59,6 +60,16 @@ impl crate::Repository {
59
60
self . work_tree . as_deref ( )
60
61
}
61
62
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
+
62
73
// TODO: tests, respect precomposeUnicode
63
74
/// The directory of the binary path of the current process.
64
75
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 ;
1
3
use std:: borrow:: Cow ;
2
4
use std:: error:: Error ;
3
5
4
- use crate :: util:: named_subrepo_opts;
5
-
6
6
#[ test]
7
7
fn on_root_with_decomposed_unicode ( ) -> crate :: Result {
8
8
let tmp = gix_testtools:: tempfile:: TempDir :: new ( ) ?;
@@ -47,6 +47,10 @@ fn on_root_with_decomposed_unicode() -> crate::Result {
47
47
Cow :: Owned ( _) ,
48
48
) ) ;
49
49
}
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
+ ) ;
50
54
51
55
Ok ( ( ) )
52
56
}
@@ -129,6 +133,21 @@ fn none_bare_repo_without_index() -> crate::Result {
129
133
) ?;
130
134
assert ! ( !repo. is_bare( ) , "worktree isn't dependent on an index file" ) ;
131
135
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
+ ) ;
132
151
Ok ( ( ) )
133
152
}
134
153
You can’t perform that action at this time.
0 commit comments