Skip to content

Commit 8effe57

Browse files
dahlbyknulltoken
authored andcommitted
Make RetrieveStatus() reload on-disk index beforehand
Fix #322 and #522
1 parent 467fbd0 commit 8effe57

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,39 @@ public void StagingAFileWhenTheIndexIsLockedThrowsALockedFileException()
244244
Assert.Throws<LockedFileException>(() => repo.Index.Stage("newfile"));
245245
}
246246
}
247+
248+
[Fact]
249+
public void CanCopeWithExternalChangesToTheIndex()
250+
{
251+
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
252+
253+
Touch(scd.DirectoryPath, "a.txt", "a\n");
254+
Touch(scd.DirectoryPath, "b.txt", "b\n");
255+
256+
string path = Repository.Init(scd.DirectoryPath);
257+
258+
using (var repoWrite = new Repository(path))
259+
using (var repoRead = new Repository(path))
260+
{
261+
var writeStatus = repoWrite.Index.RetrieveStatus();
262+
Assert.True(writeStatus.IsDirty);
263+
Assert.Equal(0, repoWrite.Index.Count);
264+
265+
var readStatus = repoRead.Index.RetrieveStatus();
266+
Assert.True(readStatus.IsDirty);
267+
Assert.Equal(0, repoRead.Index.Count);
268+
269+
repoWrite.Index.Stage("*");
270+
repoWrite.Commit("message", Constants.Signature, Constants.Signature);
271+
272+
writeStatus = repoWrite.Index.RetrieveStatus();
273+
Assert.False(writeStatus.IsDirty);
274+
Assert.Equal(2, repoWrite.Index.Count);
275+
276+
readStatus = repoRead.Index.RetrieveStatus();
277+
Assert.False(readStatus.IsDirty);
278+
Assert.Equal(2, repoRead.Index.Count);
279+
}
280+
}
247281
}
248282
}

LibGit2Sharp/Core/NativeMethods.cs

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

495+
[DllImport(libgit2)]
496+
internal static extern int git_index_read(IndexSafeHandle index);
497+
495498
[DllImport(libgit2)]
496499
internal static extern int git_index_remove_bypath(
497500
IndexSafeHandle index,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,15 @@ public static IndexSafeHandle git_index_open(FilePath indexpath)
776776
}
777777
}
778778

779+
public static void git_index_read(IndexSafeHandle index)
780+
{
781+
using (ThreadAffinity())
782+
{
783+
int res = NativeMethods.git_index_read(index);
784+
Ensure.ZeroResult(res);
785+
}
786+
}
787+
779788
public static void git_index_remove_bypath(IndexSafeHandle index, FilePath path)
780789
{
781790
using (ThreadAffinity())

LibGit2Sharp/Index.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ public virtual FileStatus RetrieveStatus(string filePath)
505505
/// <returns>A <see cref="RepositoryStatus"/> holding the state of all the files.</returns>
506506
public virtual RepositoryStatus RetrieveStatus()
507507
{
508+
ReloadFromDisk();
508509
return new RepositoryStatus(repo);
509510
}
510511

@@ -559,6 +560,11 @@ private void ReplaceIndexEntryWith(TreeEntryChanges treeEntryChanges)
559560
Marshal.FreeHGlobal(indexEntry.Path);
560561
}
561562

563+
internal void ReloadFromDisk()
564+
{
565+
Proxy.git_index_read(handle);
566+
}
567+
562568
private string DebuggerDisplay
563569
{
564570
get

0 commit comments

Comments
 (0)