1- using LibGit2Sharp . Tests . TestHelpers ;
2- using System ;
1+ using System ;
32using System . Collections . Generic ;
43using System . IO ;
54using System . Linq ;
6- using System . Text ;
7- using System . Threading . Tasks ;
5+ using LibGit2Sharp . Tests . TestHelpers ;
86using Xunit ;
97
108namespace LibGit2Sharp . Tests
@@ -238,7 +236,7 @@ public void CanForcePruneLockedWorktree()
238236 }
239237
240238 [ Fact ]
241- public void CanAddWorktree ( )
239+ public void CanAddWorktree_WithUncommitedChanges ( )
242240 {
243241 var repoPath = SandboxWorktreeTestRepo ( ) ;
244242 using ( var repo = new Repository ( repoPath ) )
@@ -252,11 +250,54 @@ public void CanAddWorktree()
252250 Assert . False ( worktree . IsLocked ) ;
253251
254252 Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
253+
254+ // Check that branch contains same number of files and folders
255+ Assert . True ( repo . RetrieveStatus ( ) . IsDirty ) ;
256+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
257+ var filesInBranch = GetFilesOfRepo ( path ) ;
258+ Assert . NotEqual ( filesInMain , filesInBranch ) ;
259+
260+ repo . Reset ( ResetMode . Hard ) ;
261+ repo . RemoveUntrackedFiles ( ) ;
262+
263+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
264+ filesInMain = GetFilesOfRepo ( repoPath ) ;
265+ filesInBranch = GetFilesOfRepo ( path ) ;
266+ Assert . Equal ( filesInMain , filesInBranch ) ;
255267 }
256268 }
257269
258270 [ Fact ]
259- public void CanAddLockedWorktree ( )
271+ public void CanAddWorktree_WithCommitedChanges ( )
272+ {
273+ var repoPath = SandboxWorktreeTestRepo ( ) ;
274+ using ( var repo = new Repository ( repoPath ) )
275+ {
276+ // stage all changes
277+ Commands . Stage ( repo , "*" ) ;
278+ repo . Commit ( "Apply all changes" , Constants . Signature , Constants . Signature ) ;
279+
280+ Assert . Equal ( 2 , repo . Worktrees . Count ( ) ) ;
281+
282+ var name = "blah" ;
283+ var path = Path . Combine ( repo . Info . WorkingDirectory , ".." , "worktrees" , name ) ;
284+ var worktree = repo . Worktrees . Add ( name , path , false ) ;
285+ Assert . Equal ( name , worktree . Name ) ;
286+ Assert . False ( worktree . IsLocked ) ;
287+
288+ Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
289+
290+ // Check that branch contains same number of files and folders
291+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
292+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
293+ var filesInBranch = GetFilesOfRepo ( path ) ;
294+
295+ Assert . Equal ( filesInMain , filesInBranch ) ;
296+ }
297+ }
298+
299+ [ Fact ]
300+ public void CanAddLockedWorktree_WithUncommitedChanges ( )
260301 {
261302 var repoPath = SandboxWorktreeTestRepo ( ) ;
262303 using ( var repo = new Repository ( repoPath ) )
@@ -270,6 +311,48 @@ public void CanAddLockedWorktree()
270311 Assert . True ( worktree . IsLocked ) ;
271312
272313 Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
314+
315+ // Check that branch contains same number of files and folders
316+ Assert . True ( repo . RetrieveStatus ( ) . IsDirty ) ;
317+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
318+ var filesInBranch = GetFilesOfRepo ( path ) ;
319+ Assert . NotEqual ( filesInMain , filesInBranch ) ;
320+
321+ repo . Reset ( ResetMode . Hard ) ;
322+ repo . RemoveUntrackedFiles ( ) ;
323+
324+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
325+ filesInMain = GetFilesOfRepo ( repoPath ) ;
326+ filesInBranch = GetFilesOfRepo ( path ) ;
327+ Assert . Equal ( filesInMain , filesInBranch ) ;
328+ }
329+ }
330+
331+ [ Fact ]
332+ public void CanAddLockedWorktree_WithCommitedChanges ( )
333+ {
334+ var repoPath = SandboxWorktreeTestRepo ( ) ;
335+ using ( var repo = new Repository ( repoPath ) )
336+ {
337+ // stage all changes
338+ Commands . Stage ( repo , "*" ) ;
339+ repo . Commit ( "Apply all changes" , Constants . Signature , Constants . Signature ) ;
340+
341+ Assert . Equal ( 2 , repo . Worktrees . Count ( ) ) ;
342+
343+ var name = "blah" ;
344+ var path = Path . Combine ( repo . Info . WorkingDirectory , ".." , "worktrees" , name ) ;
345+ var worktree = repo . Worktrees . Add ( name , path , true ) ;
346+ Assert . Equal ( name , worktree . Name ) ;
347+ Assert . True ( worktree . IsLocked ) ;
348+
349+ Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
350+
351+ // Check that branch contains same number of files and folders
352+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
353+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
354+ var filesInBranch = GetFilesOfRepo ( path ) ;
355+ Assert . Equal ( filesInMain , filesInBranch ) ;
273356 }
274357 }
275358
@@ -292,7 +375,21 @@ public void CanAddWorktreeForCommittish()
292375 Assert . Equal ( committish , repository . Head . FriendlyName ) ;
293376 }
294377 Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
378+
379+ // Check that branch contains same number of files and folders
380+ var filesInCommittish = new string [ ] { "numbers.txt" , "super-file.txt" } ;
381+ var filesInBranch = GetFilesOfRepo ( path ) ;
382+ Assert . Equal ( filesInCommittish , filesInBranch ) ;
295383 }
296384 }
385+
386+ private static IEnumerable < string > GetFilesOfRepo ( string repoPath )
387+ {
388+ return Directory . GetFiles ( repoPath , "*" , SearchOption . AllDirectories )
389+ . Where ( fileName => ! fileName . StartsWith ( Path . Combine ( repoPath , ".git" ) ) )
390+ . Select ( fileName => fileName . Replace ( $ "{ repoPath } { Path . DirectorySeparatorChar } ", "" ) )
391+ . OrderBy ( fileName => fileName )
392+ . ToList ( ) ;
393+ }
297394 }
298395}
0 commit comments