@@ -285,24 +285,24 @@ pub fn branch_compare_upstream(
285285 Ok ( BranchCompare { ahead, behind } )
286286}
287287
288- /// Switch branch to given `branch_ref `.
288+ /// Switch branch to given `branch_name `.
289289///
290290/// Method will fail if there are conflicting changes between current and target branch. However,
291291/// if files are not conflicting, they will remain in tree (e.g. tracked new file is not
292292/// conflicting and therefore is kept in tree even after checkout).
293293pub fn checkout_branch (
294294 repo_path : & RepoPath ,
295- branch_ref : & str ,
295+ branch_name : & str ,
296296) -> Result < ( ) > {
297297 scope_time ! ( "checkout_branch" ) ;
298298
299299 let repo = repo ( repo_path) ?;
300300
301- let branch_name =
302- branch_ref. split ( '/' ) . last ( ) . ok_or ( Error :: PathString ) ?;
303-
304301 let branch = repo. find_branch ( branch_name, BranchType :: Local ) ?;
305- let target_treeish = branch. into_reference ( ) . peel_to_tree ( ) ?;
302+
303+ let branch_ref = branch. into_reference ( ) ;
304+
305+ let target_treeish = branch_ref. peel_to_tree ( ) ?;
306306 let target_treeish_object = target_treeish. as_object ( ) ;
307307
308308 // modify state to match branch's state
@@ -311,8 +311,12 @@ pub fn checkout_branch(
311311 Some ( & mut git2:: build:: CheckoutBuilder :: new ( ) ) ,
312312 ) ?;
313313
314+ let branch_ref = branch_ref. name ( ) . ok_or_else ( || {
315+ Error :: Generic ( String :: from ( "branch ref not found" ) )
316+ } ) ;
317+
314318 // modify HEAD to point to given branch
315- repo. set_head ( branch_ref) ?;
319+ repo. set_head ( branch_ref? ) ?;
316320
317321 Ok ( ( ) )
318322}
@@ -687,12 +691,8 @@ mod tests_checkout {
687691 let repo_path: & RepoPath =
688692 & root. as_os_str ( ) . to_str ( ) . unwrap ( ) . into ( ) ;
689693
690- assert ! (
691- checkout_branch( repo_path, "refs/heads/master" ) . is_ok( )
692- ) ;
693- assert ! (
694- checkout_branch( repo_path, "refs/heads/foobar" ) . is_err( )
695- ) ;
694+ assert ! ( checkout_branch( repo_path, "master" ) . is_ok( ) ) ;
695+ assert ! ( checkout_branch( repo_path, "foobar" ) . is_err( ) ) ;
696696 }
697697
698698 #[ test]
@@ -704,11 +704,20 @@ mod tests_checkout {
704704
705705 create_branch ( repo_path, "test" ) . unwrap ( ) ;
706706
707- assert ! ( checkout_branch( repo_path, "refs/heads/test" ) . is_ok( ) ) ;
708- assert ! (
709- checkout_branch( repo_path, "refs/heads/master" ) . is_ok( )
710- ) ;
711- assert ! ( checkout_branch( repo_path, "refs/heads/test" ) . is_ok( ) ) ;
707+ assert ! ( checkout_branch( repo_path, "test" ) . is_ok( ) ) ;
708+ assert ! ( checkout_branch( repo_path, "master" ) . is_ok( ) ) ;
709+ assert ! ( checkout_branch( repo_path, "test" ) . is_ok( ) ) ;
710+ }
711+
712+ #[ test]
713+ fn test_branch_with_slash_in_name ( ) {
714+ let ( _td, repo) = repo_init ( ) . unwrap ( ) ;
715+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
716+ let repo_path: & RepoPath =
717+ & root. as_os_str ( ) . to_str ( ) . unwrap ( ) . into ( ) ;
718+
719+ create_branch ( repo_path, "foo/bar" ) . unwrap ( ) ;
720+ checkout_branch ( repo_path, "foo/bar" ) . unwrap ( ) ;
712721 }
713722
714723 #[ test]
@@ -726,7 +735,7 @@ mod tests_checkout {
726735
727736 stage_add_file ( & repo_path, & Path :: new ( filename) ) . unwrap ( ) ;
728737
729- assert ! ( checkout_branch( repo_path, "refs/heads/ test" ) . is_ok( ) ) ;
738+ assert ! ( checkout_branch( repo_path, "test" ) . is_ok( ) ) ;
730739 }
731740}
732741
@@ -772,7 +781,7 @@ mod test_delete_branch {
772781 create_branch ( repo_path, "branch1" ) . unwrap ( ) ;
773782 create_branch ( repo_path, "branch2" ) . unwrap ( ) ;
774783
775- checkout_branch ( repo_path, "refs/heads/ branch1" ) . unwrap ( ) ;
784+ checkout_branch ( repo_path, "branch1" ) . unwrap ( ) ;
776785
777786 assert_eq ! (
778787 repo. branches( None )
0 commit comments