11use std:: path:: PathBuf ;
22
33use anyhow:: { Context , Result } ;
4- use bstr:: BStr ;
54use but_graph:: VirtualBranchesTomlMetadata ;
65use but_workspace:: { StacksFilter , stacks_v3} ;
76use gitbutler_command_context:: CommandContext ;
@@ -21,7 +20,7 @@ pub struct NewWorktreeOutcome {
2120pub fn worktree_new (
2221 ctx : & mut CommandContext ,
2322 _perm : & WorktreeReadPermission ,
24- refname : & str ,
23+ refname : & gix :: refs :: PartialNameRef ,
2524) -> Result < NewWorktreeOutcome > {
2625 let repo = ctx. gix_repo_for_merging ( ) ?;
2726 let meta = VirtualBranchesTomlMetadata :: from_path (
@@ -31,22 +30,26 @@ pub fn worktree_new(
3130 let head = stacks
3231 . into_iter ( )
3332 . flat_map ( |s| s. heads )
34- . find ( |h| h. name == BStr :: new ( refname) )
33+ . find ( |h| {
34+ gix:: refs:: PartialName :: try_from ( h. name . clone ( ) )
35+ . map ( |n| n. as_ref ( ) == refname)
36+ . unwrap_or ( false )
37+ } )
3538 . context ( "Failed to find matching head" ) ?;
3639
3740 // Used as a method of generating the path & refrence name.
3841 let id = uuid:: Uuid :: new_v4 ( ) ;
3942
4043 let path = worktree_path ( ctx. project ( ) , id) ;
41- let branch_name = worktree_branch_name ( id) ;
44+ let branch_name = gix :: refs :: PartialName :: try_from ( format ! ( "gitbutler/worktree/{}" , id) ) ? ;
4245
43- git_worktree_add ( & ctx. project ( ) . path , & path, & branch_name, head. tip ) ?;
46+ git_worktree_add ( & ctx. project ( ) . path , & path, branch_name. as_ref ( ) , head. tip ) ?;
4447
4548 let worktree = Worktree {
4649 path : path. canonicalize ( ) ?,
47- reference : format ! ( "refs/heads/{}" , branch_name ) ,
50+ reference : gix :: refs :: FullName :: try_from ( format ! ( "refs/heads/gitbutler/worktree/ {}" , id ) ) ? ,
4851 base : head. tip ,
49- source : WorktreeSource :: Branch ( head . name . to_string ( ) ) ,
52+ source : WorktreeSource :: Branch ( refname . to_owned ( ) ) ,
5053 } ;
5154 save_worktree ( ctx, worktree. clone ( ) ) ?;
5255
@@ -56,7 +59,3 @@ pub fn worktree_new(
5659fn worktree_path ( project : & Project , id : uuid:: Uuid ) -> PathBuf {
5760 project. gb_dir ( ) . join ( "worktrees" ) . join ( id. to_string ( ) )
5861}
59-
60- fn worktree_branch_name ( id : uuid:: Uuid ) -> String {
61- format ! ( "gitbutler/worktree/{}" , id)
62- }
0 commit comments