1
- using LibGit2Sharp . Tests . TestHelpers ;
2
- using System ;
1
+ using System ;
3
2
using System . Collections . Generic ;
4
3
using System . IO ;
5
4
using System . Linq ;
6
- using System . Text ;
7
- using System . Threading . Tasks ;
5
+ using LibGit2Sharp . Tests . TestHelpers ;
8
6
using Xunit ;
9
7
10
8
namespace LibGit2Sharp . Tests
@@ -238,7 +236,7 @@ public void CanForcePruneLockedWorktree()
238
236
}
239
237
240
238
[ Fact ]
241
- public void CanAddWorktree ( )
239
+ public void CanAddWorktree_WithUncommitedChanges ( )
242
240
{
243
241
var repoPath = SandboxWorktreeTestRepo ( ) ;
244
242
using ( var repo = new Repository ( repoPath ) )
@@ -252,11 +250,54 @@ public void CanAddWorktree()
252
250
Assert . False ( worktree . IsLocked ) ;
253
251
254
252
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 ) ;
255
267
}
256
268
}
257
269
258
270
[ 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 ( )
260
301
{
261
302
var repoPath = SandboxWorktreeTestRepo ( ) ;
262
303
using ( var repo = new Repository ( repoPath ) )
@@ -270,6 +311,48 @@ public void CanAddLockedWorktree()
270
311
Assert . True ( worktree . IsLocked ) ;
271
312
272
313
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 ) ;
273
356
}
274
357
}
275
358
@@ -292,7 +375,21 @@ public void CanAddWorktreeForCommittish()
292
375
Assert . Equal ( committish , repository . Head . FriendlyName ) ;
293
376
}
294
377
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 ) ;
295
383
}
296
384
}
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
+ }
297
394
}
298
395
}
0 commit comments