@@ -44,6 +44,19 @@ pub fn stash_drop(repo_path: &str, stash_id: CommitId) -> Result<()> {
4444 Ok ( ( ) )
4545}
4646
47+ ///
48+ pub fn stash_pop ( repo_path : & str , stash_id : CommitId ) -> Result < ( ) > {
49+ scope_time ! ( "stash_pop" ) ;
50+
51+ let mut repo = repo ( repo_path) ?;
52+
53+ let index = get_stash_index ( & mut repo, stash_id. into ( ) ) ?;
54+
55+ repo. stash_pop ( index, None ) ?;
56+
57+ Ok ( ( ) )
58+ }
59+
4760///
4861pub fn stash_apply (
4962 repo_path : & str ,
@@ -122,7 +135,7 @@ mod tests {
122135 debug_cmd_print, get_statuses, repo_init,
123136 write_commit_file,
124137 } ,
125- utils:: repo_write_file,
138+ utils:: { repo_read_file , repo_write_file} ,
126139 } ;
127140 use std:: { fs:: File , io:: Write , path:: Path } ;
128141
@@ -286,4 +299,72 @@ mod tests {
286299
287300 assert ! ( res. is_ok( ) ) ;
288301 }
302+
303+ #[ test]
304+ fn test_stash_pop_no_conflict ( ) {
305+ let ( _td, repo) = repo_init ( ) . unwrap ( ) ;
306+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
307+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
308+
309+ write_commit_file ( & repo, "test.txt" , "test" , "c1" ) ;
310+
311+ repo_write_file ( & repo, "test.txt" , "test2" ) . unwrap ( ) ;
312+
313+ let id =
314+ stash_save ( repo_path, Some ( "foo" ) , true , false ) . unwrap ( ) ;
315+
316+ let res = stash_pop ( repo_path, id) ;
317+
318+ assert ! ( res. is_ok( ) ) ;
319+ assert_eq ! (
320+ repo_read_file( & repo, "test.txt" ) . unwrap( ) ,
321+ "test2"
322+ ) ;
323+ }
324+
325+ #[ test]
326+ fn test_stash_pop_conflict ( ) {
327+ let ( _td, repo) = repo_init ( ) . unwrap ( ) ;
328+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
329+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
330+
331+ repo_write_file ( & repo, "test.txt" , "test" ) . unwrap ( ) ;
332+
333+ let id =
334+ stash_save ( repo_path, Some ( "foo" ) , true , false ) . unwrap ( ) ;
335+
336+ repo_write_file ( & repo, "test.txt" , "test2" ) . unwrap ( ) ;
337+
338+ let res = stash_pop ( repo_path, id) ;
339+
340+ assert ! ( res. is_err( ) ) ;
341+ assert_eq ! (
342+ repo_read_file( & repo, "test.txt" ) . unwrap( ) ,
343+ "test2"
344+ ) ;
345+ }
346+
347+ #[ test]
348+ fn test_stash_pop_conflict_after_commit ( ) {
349+ let ( _td, repo) = repo_init ( ) . unwrap ( ) ;
350+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
351+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
352+
353+ write_commit_file ( & repo, "test.txt" , "test" , "c1" ) ;
354+
355+ repo_write_file ( & repo, "test.txt" , "test2" ) . unwrap ( ) ;
356+
357+ let id =
358+ stash_save ( repo_path, Some ( "foo" ) , true , false ) . unwrap ( ) ;
359+
360+ repo_write_file ( & repo, "test.txt" , "test3" ) . unwrap ( ) ;
361+
362+ let res = stash_pop ( repo_path, id) ;
363+
364+ assert ! ( res. is_err( ) ) ;
365+ assert_eq ! (
366+ repo_read_file( & repo, "test.txt" ) . unwrap( ) ,
367+ "test3"
368+ ) ;
369+ }
289370}
0 commit comments