From 62b3e0d7e96ad05c1d0e3f18ee8b0f224bd47dd0 Mon Sep 17 00:00:00 2001 From: clyvari Date: Sat, 7 Jan 2023 16:30:11 +0100 Subject: [PATCH 1/2] Add IsDirty property to StatusEntry, Change RepositoryStatus.IsDirty logic to use StatusEntry.IsDirty --- LibGit2Sharp/RepositoryStatus.cs | 2 +- LibGit2Sharp/StatusEntry.cs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/RepositoryStatus.cs b/LibGit2Sharp/RepositoryStatus.cs index 490dc6798..1d686cbaa 100644 --- a/LibGit2Sharp/RepositoryStatus.cs +++ b/LibGit2Sharp/RepositoryStatus.cs @@ -68,7 +68,7 @@ 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); + isDirty = statusEntries.Any(entry => entry.IsDirty); } } diff --git a/LibGit2Sharp/StatusEntry.cs b/LibGit2Sharp/StatusEntry.cs index 7008712c6..b0ca4bdc0 100644 --- a/LibGit2Sharp/StatusEntry.cs +++ b/LibGit2Sharp/StatusEntry.cs @@ -41,6 +41,17 @@ public virtual FileStatus State get { return state; } } + /// + /// True if the has been altered since the last commit. False otherwise. + /// + public virtual bool IsDirty + { + get + { + return State != FileStatus.Ignored && State != FileStatus.Unaltered; + } + } + /// /// Gets the relative new filepath to the working directory of the file. /// From 916be53d413775e3bc19f0de72384ec7b35e5f03 Mon Sep 17 00:00:00 2001 From: clyvari Date: Sat, 7 Jan 2023 16:34:56 +0100 Subject: [PATCH 2/2] Add Dirty collection to RepositoryStatus Add Unit Tests --- LibGit2Sharp.Tests/StatusFixture.cs | 17 +++++++++++++++++ LibGit2Sharp/RepositoryStatus.cs | 12 +++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/StatusFixture.cs b/LibGit2Sharp.Tests/StatusFixture.cs index 698639aa4..0d62921ea 100644 --- a/LibGit2Sharp.Tests/StatusFixture.cs +++ b/LibGit2Sharp.Tests/StatusFixture.cs @@ -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()); @@ -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)); @@ -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() { @@ -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); } } @@ -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()); } } diff --git a/LibGit2Sharp/RepositoryStatus.cs b/LibGit2Sharp/RepositoryStatus.cs index 1d686cbaa..9675c1f5d 100644 --- a/LibGit2Sharp/RepositoryStatus.cs +++ b/LibGit2Sharp/RepositoryStatus.cs @@ -27,6 +27,7 @@ public class RepositoryStatus : IEnumerable private readonly List renamedInIndex = new List(); private readonly List renamedInWorkDir = new List(); private readonly List unaltered = new List(); + private readonly List dirty = new List(); private readonly bool isDirty; private readonly IDictionary> dispatcher = Build(); @@ -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.IsDirty); + dirty = statusEntries.Where(entry => entry.IsDirty).ToList(); + isDirty = dirty.Any(); } } @@ -309,6 +311,14 @@ public virtual IEnumerable Unaltered get { return unaltered; } } + /// + /// List of files that alter the index or the working directory since the last commit. + /// + public virtual IEnumerable Dirty + { + get { return dirty; } + } + /// /// True if the index or the working directory has been altered since the last commit. False otherwise. ///