@@ -62,7 +62,7 @@ pub fn commit(repo_path: &str, msg: &str) {
62
62
. unwrap ( ) ;
63
63
}
64
64
65
- ///
65
+ /// add a file diff from workingdir to stage (will not add removed files see `stage_addremoved`)
66
66
pub fn stage_add ( repo_path : & str , path : & Path ) -> bool {
67
67
scope_time ! ( "stage_add" ) ;
68
68
@@ -78,6 +78,22 @@ pub fn stage_add(repo_path: &str, path: &Path) -> bool {
78
78
false
79
79
}
80
80
81
+ /// stage a removed file
82
+ pub fn stage_addremoved ( repo_path : & str , path : & Path ) -> bool {
83
+ scope_time ! ( "stage_addremoved" ) ;
84
+
85
+ let repo = repo ( repo_path) ;
86
+
87
+ let mut index = repo. index ( ) . unwrap ( ) ;
88
+
89
+ if index. remove_path ( path) . is_ok ( ) {
90
+ index. write ( ) . unwrap ( ) ;
91
+ return true ;
92
+ }
93
+
94
+ false
95
+ }
96
+
81
97
#[ cfg( test) ]
82
98
mod tests {
83
99
use super :: * ;
@@ -86,7 +102,11 @@ mod tests {
86
102
status:: { get_status, StatusType } ,
87
103
tests:: { repo_init, repo_init_empty} ,
88
104
} ;
89
- use std:: { fs:: File , io:: Write , path:: Path } ;
105
+ use std:: {
106
+ fs:: { remove_file, File } ,
107
+ io:: Write ,
108
+ path:: Path ,
109
+ } ;
90
110
91
111
#[ test]
92
112
fn test_commit ( ) {
@@ -172,4 +192,38 @@ mod tests {
172
192
assert_eq ! ( status_count( StatusType :: WorkingDir ) , 1 ) ;
173
193
assert_eq ! ( status_count( StatusType :: Stage ) , 1 ) ;
174
194
}
195
+
196
+ #[ test]
197
+ fn test_staging_deleted_file ( ) {
198
+ let file_path = Path :: new ( "file1.txt" ) ;
199
+ let ( _td, repo) = repo_init ( ) ;
200
+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
201
+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
202
+
203
+ let status_count = |s : StatusType | -> usize {
204
+ get_status ( repo_path, s) . len ( )
205
+ } ;
206
+
207
+ let full_path = & root. join ( file_path) ;
208
+
209
+ File :: create ( full_path)
210
+ . unwrap ( )
211
+ . write_all ( b"test file1 content" )
212
+ . unwrap ( ) ;
213
+
214
+ assert_eq ! ( stage_add( repo_path, file_path) , true ) ;
215
+
216
+ commit ( repo_path, "commit msg" ) ;
217
+
218
+ // delete the file now
219
+ assert_eq ! ( remove_file( full_path) . is_ok( ) , true ) ;
220
+
221
+ // deleted file in diff now
222
+ assert_eq ! ( status_count( StatusType :: WorkingDir ) , 1 ) ;
223
+
224
+ assert_eq ! ( stage_addremoved( repo_path, file_path) , true ) ;
225
+
226
+ assert_eq ! ( status_count( StatusType :: WorkingDir ) , 0 ) ;
227
+ assert_eq ! ( status_count( StatusType :: Stage ) , 1 ) ;
228
+ }
175
229
}
0 commit comments