diff --git a/LibGit2Sharp.Tests/IndexFixture.cs b/LibGit2Sharp.Tests/IndexFixture.cs index 5b6f6e4fa..f6c1690a2 100644 --- a/LibGit2Sharp.Tests/IndexFixture.cs +++ b/LibGit2Sharp.Tests/IndexFixture.cs @@ -244,5 +244,39 @@ public void StagingAFileWhenTheIndexIsLockedThrowsALockedFileException() Assert.Throws(() => repo.Index.Stage("newfile")); } } + + [Fact] + public void CanCopeWithExternalChangesToTheIndex() + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + Touch(scd.DirectoryPath, "a.txt", "a\n"); + Touch(scd.DirectoryPath, "b.txt", "b\n"); + + string path = Repository.Init(scd.DirectoryPath); + + using (var repoWrite = new Repository(path)) + using (var repoRead = new Repository(path)) + { + var writeStatus = repoWrite.Index.RetrieveStatus(); + Assert.True(writeStatus.IsDirty); + Assert.Equal(0, repoWrite.Index.Count); + + var readStatus = repoRead.Index.RetrieveStatus(); + Assert.True(readStatus.IsDirty); + Assert.Equal(0, repoRead.Index.Count); + + repoWrite.Index.Stage("*"); + repoWrite.Commit("message", Constants.Signature, Constants.Signature); + + writeStatus = repoWrite.Index.RetrieveStatus(); + Assert.False(writeStatus.IsDirty); + Assert.Equal(2, repoWrite.Index.Count); + + readStatus = repoRead.Index.RetrieveStatus(); + Assert.False(readStatus.IsDirty); + Assert.Equal(2, repoRead.Index.Count); + } + } } } diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index dc6417f3b..e2f48bc3f 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -492,6 +492,9 @@ internal static extern int git_index_open( out IndexSafeHandle index, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath indexpath); + [DllImport(libgit2)] + internal static extern int git_index_read(IndexSafeHandle index); + [DllImport(libgit2)] internal static extern int git_index_remove_bypath( IndexSafeHandle index, diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 5f224e075..3585516ae 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -776,6 +776,15 @@ public static IndexSafeHandle git_index_open(FilePath indexpath) } } + public static void git_index_read(IndexSafeHandle index) + { + using (ThreadAffinity()) + { + int res = NativeMethods.git_index_read(index); + Ensure.ZeroResult(res); + } + } + public static void git_index_remove_bypath(IndexSafeHandle index, FilePath path) { using (ThreadAffinity()) diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index d077f0e7f..a863e777c 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -505,6 +505,7 @@ public virtual FileStatus RetrieveStatus(string filePath) /// A holding the state of all the files. public virtual RepositoryStatus RetrieveStatus() { + ReloadFromDisk(); return new RepositoryStatus(repo); } @@ -559,6 +560,11 @@ private void ReplaceIndexEntryWith(TreeEntryChanges treeEntryChanges) Marshal.FreeHGlobal(indexEntry.Path); } + internal void ReloadFromDisk() + { + Proxy.git_index_read(handle); + } + private string DebuggerDisplay { get