Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify getting dirty files in repository #2004

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions LibGit2Sharp.Tests/StatusFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void CanRetrieveTheStatusOfTheWholeWorkingDirectory(bool includeUnaltered
Assert.NotNull(status);
Assert.Equal(6 + unalteredCount, status.Count());
Assert.True(status.IsDirty);
Assert.Equal(6, status.Dirty.Count());

Assert.Equal("new_untracked_file.txt", status.Untracked.Select(s => s.FilePath).Single());
Assert.Equal("modified_unstaged_file.txt", status.Modified.Select(s => s.FilePath).Single());
Expand All @@ -139,6 +140,7 @@ public void CanRetrieveTheStatusOfTheWholeWorkingDirectory(bool includeUnaltered
Assert.NotNull(status2);
Assert.Equal(6 + unalteredCount, status2.Count());
Assert.True(status2.IsDirty);
Assert.Equal(6, status2.Dirty.Count());

Assert.Equal("new_untracked_file.txt", status2.Untracked.Select(s => s.FilePath).Single());
Assert.Equal(new[] { file, "modified_unstaged_file.txt" }, status2.Modified.Select(s => s.FilePath));
Expand All @@ -149,6 +151,19 @@ public void CanRetrieveTheStatusOfTheWholeWorkingDirectory(bool includeUnaltered
}
}

[Fact]
public void CanRetrieveListOfDirtyFilesInWorkDir()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
RepositoryStatus status = repo.RetrieveStatus();
Assert.True(status.IsDirty);
Assert.Equal(6, status.Dirty.Count());
Assert.Equal(status.Where(s => s.IsDirty).Select(s => s.FilePath), status.Dirty.Select(s => s.FilePath));
}
}

[Fact]
public void CanRetrieveTheStatusOfRenamedFilesInWorkDir()
{
Expand Down Expand Up @@ -264,6 +279,7 @@ public void CanRetrieveTheStatusOfANewRepository(bool includeUnaltered)
Assert.Empty(status.Added);
Assert.Empty(status.Staged);
Assert.Empty(status.Removed);
Assert.Empty(status.Dirty);
}
}

Expand Down Expand Up @@ -657,6 +673,7 @@ public void UnalteredFilesDontMarkIndexAsDirty()
RepositoryStatus status = repo.RetrieveStatus(new StatusOptions() { IncludeUnaltered = true });

Assert.False(status.IsDirty);
Assert.Empty(status.Dirty);
Assert.Equal(9, status.Count());
}
}
Expand Down
12 changes: 11 additions & 1 deletion LibGit2Sharp/RepositoryStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class RepositoryStatus : IEnumerable<StatusEntry>
private readonly List<StatusEntry> renamedInIndex = new List<StatusEntry>();
private readonly List<StatusEntry> renamedInWorkDir = new List<StatusEntry>();
private readonly List<StatusEntry> unaltered = new List<StatusEntry>();
private readonly List<StatusEntry> dirty = new List<StatusEntry>();
private readonly bool isDirty;

private readonly IDictionary<FileStatus, Action<RepositoryStatus, StatusEntry>> dispatcher = Build();
Expand Down Expand Up @@ -68,7 +69,8 @@ internal unsafe RepositoryStatus(Repository repo, StatusOptions options)
AddStatusEntryForDelta(entry->status, entry->head_to_index, entry->index_to_workdir);
}

isDirty = statusEntries.Any(entry => entry.State != FileStatus.Ignored && entry.State != FileStatus.Unaltered);
dirty = statusEntries.Where(entry => entry.IsDirty).ToList();
isDirty = dirty.Any();
}
}

Expand Down Expand Up @@ -309,6 +311,14 @@ public virtual IEnumerable<StatusEntry> Unaltered
get { return unaltered; }
}

/// <summary>
/// List of files that alter the index or the working directory since the last commit.
/// </summary>
public virtual IEnumerable<StatusEntry> Dirty
{
get { return dirty; }
}

/// <summary>
/// True if the index or the working directory has been altered since the last commit. False otherwise.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions LibGit2Sharp/StatusEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public virtual FileStatus State
get { return state; }
}

/// <summary>
/// True if the <see cref="StatusEntry"/> has been altered since the last commit. False otherwise.
/// </summary>
public virtual bool IsDirty
{
get
{
return State != FileStatus.Ignored && State != FileStatus.Unaltered;
}
}

/// <summary>
/// Gets the relative new filepath to the working directory of the file.
/// </summary>
Expand Down