@@ -41,9 +41,8 @@ public virtual Worktree this[string name]
4141 {
4242 Ensure . ArgumentNotNullOrEmptyString ( name , "name" ) ;
4343
44- return Lookup ( name , handle => new Worktree ( handle , repo ,
44+ return Lookup ( name , handle => new Worktree ( repo ,
4545 name ,
46- new Repository ( handle ) ,
4746 Proxy . git_worktree_is_locked ( handle ) ) ) ;
4847 }
4948 }
@@ -70,15 +69,21 @@ public virtual Worktree Add(string committishOrBranchSpec, string name, string p
7069 locked = Convert . ToInt32 ( isLocked )
7170 } ;
7271
73- var handle = Proxy . git_worktree_add ( repo . Handle , name , path , options ) ;
74- var worktree = new Worktree ( handle ,
75- repo ,
76- name ,
77- new Repository ( handle ) ,
78- Proxy . git_worktree_is_locked ( handle ) ) ;
72+ using ( var handle = Proxy . git_worktree_add ( repo . Handle , name , path , options ) )
73+ {
74+ var worktree = new Worktree (
75+ repo ,
76+ name ,
77+ Proxy . git_worktree_is_locked ( handle ) ) ;
78+
79+ // switch the worktree to the target branch
80+ using ( var repository = worktree . WorktreeRepository )
81+ {
82+ Commands . Checkout ( repository , committishOrBranchSpec ) ;
83+ }
84+ }
7985
80- // switch the worktree to the target branch
81- Commands . Checkout ( worktree . WorktreeRepository , committishOrBranchSpec ) ;
86+
8287
8388 return this [ name ] ;
8489 }
@@ -97,12 +102,13 @@ public virtual Worktree Add(string name, string path, bool isLocked)
97102 locked = Convert . ToInt32 ( isLocked )
98103 } ;
99104
100- var handle = Proxy . git_worktree_add ( repo . Handle , name , path , options ) ;
101- return new Worktree ( handle ,
102- repo ,
103- name ,
104- new Repository ( handle ) ,
105- Proxy . git_worktree_is_locked ( handle ) ) ;
105+ using ( var handle = Proxy . git_worktree_add ( repo . Handle , name , path , options ) )
106+ {
107+ return new Worktree (
108+ repo ,
109+ name ,
110+ Proxy . git_worktree_is_locked ( handle ) ) ;
111+ }
106112 }
107113
108114 /// <summary>
@@ -123,26 +129,32 @@ public virtual bool Prune(Worktree worktree)
123129 /// <returns></returns>
124130 public virtual bool Prune ( Worktree worktree , bool ifLocked )
125131 {
126- string wd = worktree . WorktreeRepository . Info . WorkingDirectory ;
127-
128- if ( ! Directory . Exists ( wd ) )
129- {
130- return false ;
131- }
132-
133- git_worktree_prune_options options = new git_worktree_prune_options
132+ using ( var handle = worktree . GetWorktreeHandle ( ) )
134133 {
135- version = 1 ,
136- // default
137- flags = GitWorktreePruneOptionFlags . GIT_WORKTREE_PRUNE_WORKING_TREE | GitWorktreePruneOptionFlags . GIT_WORKTREE_PRUNE_VALID
138- } ;
139-
140- if ( ifLocked )
141- {
142- options . flags |= GitWorktreePruneOptionFlags . GIT_WORKTREE_PRUNE_LOCKED ;
134+ using ( var repository = worktree . WorktreeRepository )
135+ {
136+ string wd = repository . Info . WorkingDirectory ;
137+
138+ if ( ! Directory . Exists ( wd ) )
139+ {
140+ return false ;
141+ }
142+
143+ git_worktree_prune_options options = new git_worktree_prune_options
144+ {
145+ version = 1 ,
146+ // default
147+ flags = GitWorktreePruneOptionFlags . GIT_WORKTREE_PRUNE_WORKING_TREE | GitWorktreePruneOptionFlags . GIT_WORKTREE_PRUNE_VALID
148+ } ;
149+
150+ if ( ifLocked )
151+ {
152+ options . flags |= GitWorktreePruneOptionFlags . GIT_WORKTREE_PRUNE_LOCKED ;
153+ }
154+
155+ return Proxy . git_worktree_prune ( handle , options ) ;
156+ }
143157 }
144-
145- return Proxy . git_worktree_prune ( worktree . Handle , options ) ;
146158 }
147159
148160 internal T Lookup < T > ( string name , Func < WorktreeHandle , T > selector , bool throwIfNotFound = false )
@@ -170,8 +182,7 @@ internal T Lookup<T>(string name, Func<WorktreeHandle, T> selector, bool throwIf
170182 public virtual IEnumerator < Worktree > GetEnumerator ( )
171183 {
172184 return Proxy . git_worktree_list ( repo . Handle )
173- . Select ( n => Lookup ( n , handle => new Worktree ( handle , repo , n ,
174- new Repository ( handle ) , Proxy . git_worktree_is_locked ( handle ) ) ) )
185+ . Select ( n => Lookup ( n , handle => new Worktree ( repo , n , Proxy . git_worktree_is_locked ( handle ) ) ) )
175186 . GetEnumerator ( ) ;
176187 }
177188
0 commit comments