3
3
using System . Collections . Generic ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
- using System . Text ;
7
- using System . Threading . Tasks ;
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,15 +250,96 @@ 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
+ // NOTE: There is an open bug - [Repository.Worktrees.Add leaves now worktree empty](https://github.com/libgit2/libgit2sharp/issues/2037)
256
+ Assert . True ( repo . RetrieveStatus ( ) . IsDirty ) ;
257
+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
258
+ var filesInBranch = GetFilesOfRepo ( path ) ;
259
+ Assert . NotEqual ( filesInMain , filesInBranch ) ;
260
+
261
+ repo . Reset ( ResetMode . Hard ) ;
262
+ repo . RemoveUntrackedFiles ( ) ;
263
+
264
+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
265
+ filesInMain = GetFilesOfRepo ( repoPath ) ;
266
+ filesInBranch = GetFilesOfRepo ( path ) ;
267
+ Assert . Equal ( filesInMain , filesInBranch ) ;
268
+ }
269
+ }
270
+
271
+ [ Fact ]
272
+ public void CanAddWorktree_WithCommitedChanges ( )
273
+ {
274
+ var repoPath = SandboxWorktreeTestRepo ( ) ;
275
+ using ( var repo = new Repository ( repoPath ) )
276
+ {
277
+ // stage all changes
278
+ Commands . Stage ( repo , "*" ) ;
279
+ repo . Commit ( "Apply all changes" , Constants . Signature , Constants . Signature ) ;
280
+
281
+ Assert . Equal ( 2 , repo . Worktrees . Count ( ) ) ;
282
+
283
+ var name = "blah" ;
284
+ var path = Path . Combine ( repo . Info . WorkingDirectory , ".." , "worktrees" , name ) ;
285
+ var worktree = repo . Worktrees . Add ( name , path , false ) ;
286
+ Assert . Equal ( name , worktree . Name ) ;
287
+ Assert . False ( worktree . IsLocked ) ;
288
+
289
+ Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
290
+
291
+ // Check that branch contains same number of files and folders
292
+ // NOTE: There is an open bug - [Repository.Worktrees.Add leaves now worktree empty](https://github.com/libgit2/libgit2sharp/issues/2037)
293
+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
294
+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
295
+ var filesInBranch = GetFilesOfRepo ( path ) ;
296
+ Assert . Equal ( filesInMain , filesInBranch ) ;
297
+ }
298
+ }
299
+
300
+ [ Fact ]
301
+ public void CanAddLockedWorktree_WithUncommitedChanges ( )
302
+ {
303
+ var repoPath = SandboxWorktreeTestRepo ( ) ;
304
+ using ( var repo = new Repository ( repoPath ) )
305
+ {
306
+ Assert . Equal ( 2 , repo . Worktrees . Count ( ) ) ;
307
+
308
+ var name = "blah" ;
309
+ var path = Path . Combine ( repo . Info . WorkingDirectory , ".." , "worktrees" , name ) ;
310
+ var worktree = repo . Worktrees . Add ( name , path , true ) ;
311
+ Assert . Equal ( name , worktree . Name ) ;
312
+ Assert . True ( worktree . IsLocked ) ;
313
+
314
+ Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
315
+
316
+ // Check that branch contains same number of files and folders
317
+ // NOTE: There is an open bug - [Repository.Worktrees.Add leaves now worktree empty](https://github.com/libgit2/libgit2sharp/issues/2037)
318
+ Assert . True ( repo . RetrieveStatus ( ) . IsDirty ) ;
319
+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
320
+ var filesInBranch = GetFilesOfRepo ( path ) ;
321
+ Assert . NotEqual ( filesInMain , filesInBranch ) ;
322
+
323
+ repo . Reset ( ResetMode . Hard ) ;
324
+ repo . RemoveUntrackedFiles ( ) ;
325
+
326
+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
327
+ filesInMain = GetFilesOfRepo ( repoPath ) ;
328
+ filesInBranch = GetFilesOfRepo ( path ) ;
329
+ Assert . Equal ( filesInMain , filesInBranch ) ;
255
330
}
256
331
}
257
332
258
333
[ Fact ]
259
- public void CanAddLockedWorktree ( )
334
+ public void CanAddLockedWorktree_WithCommitedChanges ( )
260
335
{
261
336
var repoPath = SandboxWorktreeTestRepo ( ) ;
262
337
using ( var repo = new Repository ( repoPath ) )
263
338
{
339
+ // stage all changes
340
+ Commands . Stage ( repo , "*" ) ;
341
+ repo . Commit ( "Apply all changes" , Constants . Signature , Constants . Signature ) ;
342
+
264
343
Assert . Equal ( 2 , repo . Worktrees . Count ( ) ) ;
265
344
266
345
var name = "blah" ;
@@ -270,6 +349,13 @@ public void CanAddLockedWorktree()
270
349
Assert . True ( worktree . IsLocked ) ;
271
350
272
351
Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
352
+
353
+ // Check that branch contains same number of files and folders
354
+ // NOTE: There is an open bug - [Repository.Worktrees.Add leaves now worktree empty](https://github.com/libgit2/libgit2sharp/issues/2037)
355
+ Assert . False ( repo . RetrieveStatus ( ) . IsDirty ) ;
356
+ var filesInMain = GetFilesOfRepo ( repoPath ) ;
357
+ var filesInBranch = GetFilesOfRepo ( path ) ;
358
+ Assert . Equal ( filesInMain , filesInBranch ) ;
273
359
}
274
360
}
275
361
@@ -292,7 +378,22 @@ public void CanAddWorktreeForCommittish()
292
378
Assert . Equal ( committish , repository . Head . FriendlyName ) ;
293
379
}
294
380
Assert . Equal ( 3 , repo . Worktrees . Count ( ) ) ;
381
+
382
+ // Check that branch contains same number of files and folders
383
+ // NOTE: There is an open bug - [Repository.Worktrees.Add leaves now worktree empty](https://github.com/libgit2/libgit2sharp/issues/2037)
384
+ var filesInCommittish = new string [ ] { "numbers.txt" , "super-file.txt" } ;
385
+ var filesInBranch = GetFilesOfRepo ( path ) ;
386
+ Assert . Equal ( filesInCommittish , filesInBranch ) ;
295
387
}
296
388
}
389
+
390
+ private static IEnumerable < string > GetFilesOfRepo ( string repoPath )
391
+ {
392
+ return Directory . GetFiles ( repoPath , "*" , SearchOption . AllDirectories )
393
+ . Where ( fileName => ! fileName . StartsWith ( $ "{ repoPath } \\ .git", StringComparison . InvariantCultureIgnoreCase ) )
394
+ . Select ( fileName => fileName . Replace ( $ "{ repoPath } \\ ", "" , StringComparison . InvariantCultureIgnoreCase ) )
395
+ . OrderBy ( fileName => fileName , StringComparer . InvariantCultureIgnoreCase )
396
+ . ToList ( ) ;
397
+ }
297
398
}
298
399
}
0 commit comments