@@ -373,5 +373,109 @@ public void CanClearTheIndex()
373373 Assert . Equal ( FileStatus . Removed | FileStatus . Untracked , repo . RetrieveStatus ( testFile ) ) ;
374374 }
375375 }
376+
377+ [ Theory ]
378+ [ InlineData ( "new_tracked_file.txt" , FileStatus . Added , FileStatus . Untracked ) ]
379+ [ InlineData ( "modified_staged_file.txt" , FileStatus . Staged , FileStatus . Removed | FileStatus . Untracked ) ]
380+ [ InlineData ( "i_dont_exist.txt" , FileStatus . Nonexistent , FileStatus . Nonexistent ) ]
381+ public void CanRemoveAnEntryFromTheIndex ( string pathInTheIndex , FileStatus expectedBeforeStatus , FileStatus expectedAfterStatus )
382+ {
383+ var path = SandboxStandardTestRepoGitDir ( ) ;
384+ using ( var repo = new Repository ( path ) )
385+ {
386+ var before = repo . RetrieveStatus ( pathInTheIndex ) ;
387+ Assert . Equal ( expectedBeforeStatus , before ) ;
388+
389+ repo . Index . Remove ( pathInTheIndex ) ;
390+
391+ var after = repo . RetrieveStatus ( pathInTheIndex ) ;
392+ Assert . Equal ( expectedAfterStatus , after ) ;
393+ }
394+ }
395+
396+ [ Theory ]
397+ [ InlineData ( "new_untracked_file.txt" , FileStatus . Untracked , FileStatus . Added ) ]
398+ [ InlineData ( "modified_unstaged_file.txt" , FileStatus . Modified , FileStatus . Staged ) ]
399+ public void CanAddAnEntryToTheIndexFromAFileInTheWorkdir ( string pathInTheWorkdir , FileStatus expectedBeforeStatus , FileStatus expectedAfterStatus )
400+ {
401+ var path = SandboxStandardTestRepoGitDir ( ) ;
402+ using ( var repo = new Repository ( path ) )
403+ {
404+ var before = repo . RetrieveStatus ( pathInTheWorkdir ) ;
405+ Assert . Equal ( expectedBeforeStatus , before ) ;
406+
407+ repo . Index . Add ( pathInTheWorkdir ) ;
408+
409+ var after = repo . RetrieveStatus ( pathInTheWorkdir ) ;
410+ Assert . Equal ( expectedAfterStatus , after ) ;
411+ }
412+ }
413+
414+ [ Fact ]
415+ public void CanAddAnEntryToTheIndexFromABlob ( )
416+ {
417+ var path = SandboxStandardTestRepoGitDir ( ) ;
418+ using ( var repo = new Repository ( path ) )
419+ {
420+ const string targetIndexEntryPath = "1.txt" ;
421+ var before = repo . RetrieveStatus ( targetIndexEntryPath ) ;
422+ Assert . Equal ( FileStatus . Unaltered , before ) ;
423+
424+ var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
425+
426+ repo . Index . Add ( blob , targetIndexEntryPath , Mode . NonExecutableFile ) ;
427+
428+ var after = repo . RetrieveStatus ( targetIndexEntryPath ) ;
429+ Assert . Equal ( FileStatus . Staged | FileStatus . Modified , after ) ;
430+ }
431+ }
432+
433+ [ Fact ]
434+ public void AddingAnEntryToTheIndexFromAUnknwonFileInTheWorkdirThrows ( )
435+ {
436+ var path = SandboxStandardTestRepoGitDir ( ) ;
437+ using ( var repo = new Repository ( path ) )
438+ {
439+ const string filePath = "i_dont_exist.txt" ;
440+ var before = repo . RetrieveStatus ( filePath ) ;
441+ Assert . Equal ( FileStatus . Nonexistent , before ) ;
442+
443+ Assert . Throws < NotFoundException > ( ( ) => repo . Index . Add ( filePath ) ) ;
444+ }
445+ }
446+
447+ [ Fact ]
448+ public void CanMimicGitAddAll ( )
449+ {
450+ var path = SandboxStandardTestRepoGitDir ( ) ;
451+ using ( var repo = new Repository ( path ) )
452+ {
453+ var before = repo . RetrieveStatus ( ) ;
454+ Assert . True ( before . Any ( se => se . State == FileStatus . Untracked ) ) ;
455+ Assert . True ( before . Any ( se => se . State == FileStatus . Modified ) ) ;
456+ Assert . True ( before . Any ( se => se . State == FileStatus . Missing ) ) ;
457+
458+ AddSomeCornerCases ( repo ) ;
459+
460+ repo . Stage ( "*" ) ;
461+
462+ var after = repo . RetrieveStatus ( ) ;
463+ Assert . False ( after . Any ( se => se . State == FileStatus . Untracked ) ) ;
464+ Assert . False ( after . Any ( se => se . State == FileStatus . Modified ) ) ;
465+ Assert . False ( after . Any ( se => se . State == FileStatus . Missing ) ) ;
466+ }
467+ }
468+
469+ private static void AddSomeCornerCases ( Repository repo )
470+ {
471+ // Turn 1.txt into a directory in the Index
472+ repo . Index . Remove ( "1.txt" ) ;
473+ var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
474+ repo . Index . Add ( blob , "1.txt/Sneaky" , Mode . NonExecutableFile ) ;
475+
476+ // Turn README into a symlink
477+ Blob linkContent = OdbHelper . CreateBlob ( repo , "1.txt/sneaky" ) ;
478+ repo . Index . Add ( linkContent , "README" , Mode . SymbolicLink ) ;
479+ }
376480 }
377481}
0 commit comments