@@ -15,6 +15,7 @@ use crate::oid_array::OidArray;
1515use crate :: stash:: { stash_cb, StashApplyOptions , StashCbData } ;
1616use crate :: string_array:: StringArray ;
1717use crate :: util:: { self , path_to_repo_path, Binding } ;
18+ use crate :: worktree:: { Worktree , WorktreeAddOptions } ;
1819use crate :: CherrypickOptions ;
1920use crate :: {
2021 init, raw, AttrCheckFlags , Buf , Error , Object , Remote , RepositoryOpenFlags , RepositoryState ,
@@ -2700,6 +2701,57 @@ impl Repository {
27002701 Ok ( ( ) )
27012702 }
27022703 }
2704+
2705+
2706+ /// Lists all the worktrees for the repository
2707+ pub fn worktrees ( & self ) -> Result < StringArray , Error > {
2708+ let mut arr = raw:: git_strarray {
2709+ strings : 0 as * mut * mut c_char ,
2710+ count : 0 ,
2711+ } ;
2712+ unsafe {
2713+ try_call ! ( raw:: git_worktree_list( & mut arr, self . raw) ) ;
2714+ Ok ( Binding :: from_raw ( arr) )
2715+ }
2716+ }
2717+
2718+ /// Opens a worktree by name for the given repository
2719+ ///
2720+ /// This can open any worktree that the worktrees method returns.
2721+ pub fn worktree_lookup ( & self , name : & str ) -> Result < Worktree , Error > {
2722+ let mut raw = ptr:: null_mut ( ) ;
2723+ let raw_name = CString :: new ( name) ?;
2724+ unsafe {
2725+ try_call ! ( raw:: git_worktree_lookup( & mut raw, self . raw, raw_name) ) ;
2726+ Ok ( Binding :: from_raw ( raw) )
2727+ }
2728+ }
2729+
2730+ /// Open a worktree of a the repository
2731+ ///
2732+ /// If a repository is not the main tree but a worktree, this
2733+ /// function will look up the worktree inside the parent
2734+ /// repository and create a new `git_worktree` structure.
2735+ pub fn worktree_open_from_repository ( & self ) -> Result < Worktree , Error > {
2736+ let mut raw = ptr:: null_mut ( ) ;
2737+ unsafe {
2738+ try_call ! ( raw:: git_worktree_open_from_repository( & mut raw, self . raw) ) ;
2739+ Ok ( Binding :: from_raw ( raw) )
2740+ }
2741+ }
2742+
2743+ /// Creates a new worktree for the repository
2744+ pub fn worktree_add ( & self , name : & str , path : & Path , opts : & WorktreeAddOptions < ' _ > ) -> Result < Worktree , Error > {
2745+ let mut raw = ptr:: null_mut ( ) ;
2746+ let raw_name = CString :: new ( name) ?;
2747+ let raw_path = path. into_c_string ( ) ?;
2748+
2749+ unsafe {
2750+ try_call ! ( raw:: git_worktree_add( & mut raw, self . raw, raw_name, raw_path, & opts. raw( ) ) ) ;
2751+ Ok ( Binding :: from_raw ( raw) )
2752+ }
2753+ }
2754+
27032755}
27042756
27052757impl Binding for Repository {
0 commit comments