Skip to content

Make RetrieveStatus() reload on-disk index beforehand #525

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

Merged
merged 1 commit into from
Oct 5, 2013
Merged
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
34 changes: 34 additions & 0 deletions LibGit2Sharp.Tests/IndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,39 @@ public void StagingAFileWhenTheIndexIsLockedThrowsALockedFileException()
Assert.Throws<LockedFileException>(() => 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);
}
}
}
}
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ public virtual FileStatus RetrieveStatus(string filePath)
/// <returns>A <see cref="RepositoryStatus"/> holding the state of all the files.</returns>
public virtual RepositoryStatus RetrieveStatus()
{
ReloadFromDisk();
return new RepositoryStatus(repo);
}

Expand Down Expand Up @@ -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
Expand Down