File tree Expand file tree Collapse file tree 4 files changed +44
-10
lines changed Expand file tree Collapse file tree 4 files changed +44
-10
lines changed Original file line number Diff line number Diff line change 1
- use std:: path:: { Path , PathBuf } ;
1
+ use std:: path:: { Component , Path , PathBuf } ;
2
2
3
3
use anyhow:: { anyhow, bail, Context , Result } ;
4
4
use gitbutler_error:: error;
@@ -101,18 +101,29 @@ impl Controller {
101
101
}
102
102
}
103
103
104
- let id = uuid :: Uuid :: new_v4 ( ) . to_string ( ) ;
104
+ let id = ProjectId :: generate ( ) ;
105
105
106
- // title is the base name of the file
107
- let title = path
108
- . iter ( )
106
+ // Resolve the path first to get the actual directory name
107
+ let resolved_path = gix:: path:: realpath ( path) ?;
108
+ let title_is_not_normal_component = path
109
+ . components ( )
109
110
. next_back ( )
110
- . map_or_else ( || id. clone ( ) , |p| p. to_str ( ) . unwrap ( ) . to_string ( ) ) ;
111
+ . is_none_or ( |c| !matches ! ( c, Component :: Normal ( _) ) ) ;
112
+ let path_for_title = if title_is_not_normal_component {
113
+ & resolved_path
114
+ } else {
115
+ path
116
+ } ;
117
+
118
+ let title = path_for_title. file_name ( ) . map_or_else (
119
+ || id. to_string ( ) ,
120
+ |name| name. to_string_lossy ( ) . into_owned ( ) ,
121
+ ) ;
111
122
112
123
let project = Project {
113
- id : ProjectId :: generate ( ) ,
124
+ id,
114
125
title,
115
- path : gix :: path :: realpath ( path ) ? ,
126
+ path : resolved_path ,
116
127
api : None ,
117
128
..Default :: default ( )
118
129
} ;
Original file line number Diff line number Diff line change @@ -68,7 +68,10 @@ pub fn add<P: AsRef<Path>>(path: P) -> anyhow::Result<AddProjectOutcome> {
68
68
}
69
69
70
70
/// Testing purpose only.
71
- pub fn add_with_path < P : AsRef < Path > > ( data_dir : P , path : P ) -> anyhow:: Result < AddProjectOutcome > {
71
+ pub fn add_with_path (
72
+ data_dir : impl AsRef < Path > ,
73
+ path : impl AsRef < Path > ,
74
+ ) -> anyhow:: Result < AddProjectOutcome > {
72
75
let controller = Controller :: from_path ( data_dir. as_ref ( ) ) ;
73
76
controller. add ( path)
74
77
}
Original file line number Diff line number Diff line change
1
+ use gitbutler_testsupport:: paths;
2
+
3
+ #[ test]
4
+ fn current_directory_dot ( ) -> anyhow:: Result < ( ) > {
5
+ let tmp = paths:: data_dir ( ) ;
6
+ let repository = gitbutler_testsupport:: TestProject :: default ( ) ;
7
+ let repo_path = repository. path ( ) ;
8
+
9
+ // Change to the repository directory and add "." as the path
10
+ std:: env:: set_current_dir ( repo_path) ?;
11
+
12
+ let project = gitbutler_project:: add_with_path ( tmp. path ( ) , "." ) ?. unwrap_project ( ) ;
13
+
14
+ let expected_title = repo_path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
15
+ assert_eq ! (
16
+ project. title, expected_title,
17
+ "Project title should be the actual directory name, not '.'"
18
+ ) ;
19
+ Ok ( ( ) )
20
+ }
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ mod add {
49
49
let data_dir = paths:: data_dir ( ) ;
50
50
let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
51
51
let outcome =
52
- gitbutler_project:: add_with_path ( data_dir. path ( ) , & tmp. path ( ) . join ( "missing" ) )
52
+ gitbutler_project:: add_with_path ( data_dir. path ( ) , tmp. path ( ) . join ( "missing" ) )
53
53
. unwrap ( ) ;
54
54
assert ! ( matches!( outcome, AddProjectOutcome :: PathNotFound ) ) ;
55
55
}
You can’t perform that action at this time.
0 commit comments