Skip to content

Commit 0e95c58

Browse files
committed
Always read from physical index on RetrieveStatus()
Fixes #322
1 parent 89b6e9b commit 0e95c58

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,37 @@ public void CanReadIndexEntryAttributes()
242242
Assert.Equal(Mode.ExecutableFile, repo.Index["1/branch_file.txt"].Mode);
243243
}
244244
}
245+
246+
[Fact]
247+
public void CanCopeWithExternalChangesToTheIndex()
248+
{
249+
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
250+
251+
Touch(scd.DirectoryPath, "a.txt", "a\n");
252+
Touch(scd.DirectoryPath, "b.txt", "b\n");
253+
254+
using (var repoWrite = Repository.Init(scd.DirectoryPath))
255+
using (var repoRead = new Repository(scd.DirectoryPath))
256+
{
257+
var writeStatus = repoWrite.Index.RetrieveStatus();
258+
Assert.True(writeStatus.IsDirty);
259+
Assert.Equal(0, repoWrite.Index.Count);
260+
261+
var readStatus = repoRead.Index.RetrieveStatus();
262+
Assert.True(readStatus.IsDirty);
263+
Assert.Equal(0, repoRead.Index.Count);
264+
265+
repoWrite.Index.Stage("*");
266+
repoWrite.Commit("message", DummySignature);
267+
268+
writeStatus = repoWrite.Index.RetrieveStatus();
269+
Assert.False(writeStatus.IsDirty);
270+
Assert.Equal(2, repoWrite.Index.Count);
271+
272+
readStatus = repoRead.Index.RetrieveStatus();
273+
Assert.False(readStatus.IsDirty);
274+
Assert.Equal(2, repoRead.Index.Count);
275+
}
276+
}
245277
}
246278
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ internal static extern int git_index_open(
490490
out IndexSafeHandle index,
491491
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath indexpath);
492492

493+
[DllImport(libgit2)]
494+
internal static extern int git_index_read(IndexSafeHandle index);
495+
496+
[DllImport(libgit2)]
497+
internal static extern int git_index_read_tree(
498+
IndexSafeHandle index,
499+
GitObjectSafeHandle tree);
500+
493501
[DllImport(libgit2)]
494502
internal static extern int git_index_remove_bypath(
495503
IndexSafeHandle index,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,25 @@ public static IndexSafeHandle git_index_open(FilePath indexpath)
771771
}
772772
}
773773

774+
public static void git_index_read(IndexSafeHandle index)
775+
{
776+
using (ThreadAffinity())
777+
{
778+
int res = NativeMethods.git_index_read(index);
779+
Ensure.ZeroResult(res);
780+
}
781+
}
782+
783+
public static void git_index_read_tree(IndexSafeHandle index, Repository repo, Tree tree)
784+
{
785+
using (ThreadAffinity())
786+
using (var treePtr = new ObjectSafeWrapper(tree.Id, repo.Handle))
787+
{
788+
int res = NativeMethods.git_index_read_tree(index, treePtr.ObjectPtr);
789+
Ensure.ZeroResult(res);
790+
}
791+
}
792+
774793
public static void git_index_remove_bypath(IndexSafeHandle index, FilePath path)
775794
{
776795
using (ThreadAffinity())

LibGit2Sharp/Index.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,16 @@ private string RemoveFromIndex(string relativePath)
477477
return relativePath;
478478
}
479479

480+
private void UpdateFromPhysicalIndex()
481+
{
482+
Proxy.git_index_read(handle);
483+
var tip = repo.Head.Tip;
484+
if (tip != null)
485+
{
486+
Proxy.git_index_read_tree(handle, repo, tip.Tree);
487+
}
488+
}
489+
480490
private void UpdatePhysicalIndex()
481491
{
482492
Proxy.git_index_write(handle);
@@ -502,6 +512,7 @@ public virtual FileStatus RetrieveStatus(string filePath)
502512
/// <returns>A <see cref = "RepositoryStatus" /> holding the state of all the files.</returns>
503513
public virtual RepositoryStatus RetrieveStatus()
504514
{
515+
UpdateFromPhysicalIndex();
505516
return new RepositoryStatus(repo);
506517
}
507518

0 commit comments

Comments
 (0)