Skip to content

Commit

Permalink
Make Index.Remove() cope with files which have been deleted from the …
Browse files Browse the repository at this point in the history
…working directory

Fixes issue libgit2#95 - 1/2.

One can GitRemove a file which has been deleted from the workdir

$ mkdir test
$ cd test
$ git init .
$ echo "a" > a.txt

$ git add .
$ git commit -m "Initial commit"

$ git status
nothing to commit (working directory clean)

$ rm a.txt

$ git status
On branch master
 Changes not staged for commit:
   (use "git add/rm <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

       deleted:    a.txt

$ git rm a.txt

$ git status
On branch master
Changes to be committed:
   (use "git reset HEAD <file>..." to unstage)
       deleted:    a.txt
  • Loading branch information
fbezdeka authored and nulltoken committed Dec 15, 2011
1 parent 30036ec commit fc334d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
13 changes: 6 additions & 7 deletions LibGit2Sharp.Tests/IndexFixture.cs
Expand Up @@ -456,30 +456,29 @@ private static void InvalidMoveUseCases(string sourcePath, FileStatus sourceStat
}
}

[Test]
public void CanRemoveAFile()
[TestCase("1/branch_file.txt", FileStatus.Unaltered, true, FileStatus.Removed)]
[TestCase("deleted_unstaged_file.txt", FileStatus.Missing, false, FileStatus.Removed)]
public void CanRemoveAFile(string filename, FileStatus initialStatus, bool shouldInitiallyExist, FileStatus finalStatus)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
int count = repo.Index.Count;

string filename = "1" + Path.DirectorySeparatorChar + "branch_file.txt";
string fullpath = Path.Combine(repo.Info.WorkingDirectory, filename);

File.Exists(fullpath).ShouldBeTrue();
repo.Index.RetrieveStatus(filename).ShouldEqual(FileStatus.Unaltered);
File.Exists(fullpath).ShouldEqual(shouldInitiallyExist);
repo.Index.RetrieveStatus(filename).ShouldEqual(initialStatus);

repo.Index.Remove(filename);

repo.Index.Count.ShouldEqual(count - 1);
File.Exists(fullpath).ShouldBeFalse();
repo.Index.RetrieveStatus(filename).ShouldEqual(FileStatus.Removed);
repo.Index.RetrieveStatus(filename).ShouldEqual(finalStatus);
}
}

[TestCase("deleted_staged_file.txt")]
[TestCase("deleted_unstaged_file.txt")]
[TestCase("shadowcopy_of_an_unseen_ghost.txt")]
public void RemovingAInvalidFileThrows(string filepath)
{
Expand Down
16 changes: 14 additions & 2 deletions LibGit2Sharp/Index.cs
Expand Up @@ -303,6 +303,10 @@ public void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinatio

/// <summary>
/// Removes a file from the working directory and promotes the removal to the staging area.
/// <para>
/// If the file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// </summary>
/// <param name = "path">The path of the file within the working directory.</param>
public void Remove(string path)
Expand All @@ -312,6 +316,10 @@ public void Remove(string path)

/// <summary>
/// Removes a collection of files from the working directory and promotes the removal to the staging area.
/// <para>
/// If a file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// </summary>
/// <param name = "paths">The collection of paths of the files within the working directory.</param>
public void Remove(IEnumerable<string> paths)
Expand All @@ -335,7 +343,7 @@ public void Remove(IEnumerable<string> paths)
throw new NotImplementedException();
}

if (!keyValuePair.Value.HasAny(new[] { FileStatus.Missing, FileStatus.Nonexistent, FileStatus.Removed, FileStatus.Untracked }))
if (!keyValuePair.Value.HasAny(new[] { FileStatus.Nonexistent, FileStatus.Removed, FileStatus.Untracked }))
{
continue;
}
Expand All @@ -347,7 +355,11 @@ public void Remove(IEnumerable<string> paths)
foreach (KeyValuePair<string, FileStatus> keyValuePair in batch)
{
RemoveFromIndex(keyValuePair.Key);
File.Delete(Path.Combine(wd, keyValuePair.Key));

if (File.Exists(Path.Combine(wd, keyValuePair.Key)))
{
File.Delete(Path.Combine(wd, keyValuePair.Key));
}
}

UpdatePhysicalIndex();
Expand Down

0 comments on commit fc334d2

Please sign in to comment.