@@ -199,19 +199,9 @@ pub fn get_status(
199199/// discard all changes in the working directory
200200pub fn discard_status ( repo_path : & RepoPath ) -> Result < bool > {
201201 let repo = repo ( repo_path) ?;
202- let statuses = repo. statuses ( None ) ?;
202+ let commit = repo. head ( ) ? . peel_to_commit ( ) ?;
203203
204- for status in statuses. iter ( ) {
205- if status. status ( ) . is_wt_modified ( )
206- || status. status ( ) . is_wt_new ( )
207- {
208- let oid = repo. head ( ) ?. target ( ) . unwrap ( ) ;
209- let obj = repo. find_object ( oid, None ) ?;
210-
211- repo. checkout_tree ( & obj, None ) ?;
212- repo. reset ( & obj, git2:: ResetType :: Hard , None ) ?;
213- }
214- }
204+ repo. reset ( commit. as_object ( ) , git2:: ResetType :: Hard , None ) ?;
215205
216206 Ok ( true )
217207}
@@ -235,14 +225,16 @@ mod tests {
235225 . write_all ( b"test\n foo" )
236226 . unwrap ( ) ;
237227
238- let statuses = get_status ( repo_path, StatusType :: WorkingDir , None )
239- . unwrap ( ) ;
240- assert_eq ! ( statuses. len( ) , 1 ) ;
228+ let statuses =
229+ get_status ( repo_path, StatusType :: WorkingDir , None )
230+ . unwrap ( ) ;
231+ assert_eq ! ( statuses. len( ) , 1 ) ;
241232
242- discard_status ( repo_path) . unwrap ( ) ;
233+ discard_status ( repo_path) . unwrap ( ) ;
243234
244- let statuses = get_status ( repo_path, StatusType :: WorkingDir , None )
245- . unwrap ( ) ;
246- assert_eq ! ( statuses. len( ) , 0 ) ;
235+ let statuses =
236+ get_status ( repo_path, StatusType :: WorkingDir , None )
237+ . unwrap ( ) ;
238+ assert_eq ! ( statuses. len( ) , 0 ) ;
247239 }
248240}
0 commit comments