8000 Fix staging/unstaging issue · joncham/libgit2sharp@faa21e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit faa21e0

Browse files
committed
Fix staging/unstaging issue
Modifications to the in-memory index were not written back to disk. Credit goes to @davidebbo for having reported this.
1 parent a1f568b commit faa21e0

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public void Setup()
3030
bool gitRepoExists = Directory.Exists(Constants.TestRepoWithWorkingDirPath);
3131
bool dotGitDirExists = Directory.Exists(tempDotGit);
3232

33-
if (gitRepoExists )
33+
if (gitRepoExists)
3434
{
3535
if (dotGitDirExists)
3636
{
37-
DirectoryHelper.DeleteDirectory(tempDotGit);
37+
DirectoryHelper.DeleteDirectory(tempDotGit);
3838
}
3939

4040
return;
@@ -118,6 +118,28 @@ public void CanStageANewFile()
118118
}
119119
}
120120

121+
[Test]
122+
public void CanStageANewFileInAPersistentManner()
123+
{
124+
using (var path = new TemporaryCloneOfTestRepo(Constants.TestRepoWithWorkingDirPath))
125+
{
126+
using (var repo = new Repository(path.RepositoryPath))
127+
{
128+
const string filename = "unit_test.txt";
129+
File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, filename), "some contents");
130+
131+
repo.Index.Stage(filename);
132+
repo.Index[filename].ShouldNotBeNull();
133+
}
134+
135+
using (var repo = new Repository(path.RepositoryPath))
136+
{
137+
const string filename = "unit_test.txt";
138+
repo.Index[filename].ShouldNotBeNull();
139+
}
140+
}
141+
}
142+
121143
[Test]
122144
public void CanStageANewFileWithAFullPath()
123145
{

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace LibGit2Sharp.Core
66
internal static class NativeMethods
77
{
88
private const string libgit2 = "git2";
9-
9+
1010
[DllImport(libgit2)]
1111
public static extern IntPtr git_blob_rawcontent(IntPtr blob);
1212

@@ -60,7 +60,10 @@ internal static class NativeMethods
6060

6161
[DllImport(libgit2)]
6262
public static extern int git_index_open_inrepo(out IndexSafeHandle index, RepositorySafeHandle repo);
63-
63+
64+
[DllImport(libgit2)]
65+
public static extern int git_index_write(IndexSafeHandle index);
66+
6467
[DllImport(libgit2)]
6568
public static extern IntPtr git_lasterror();
6669

LibGit2Sharp/Index.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ IEnumerator IEnumerable.GetEnumerator()
110110

111111
#endregion
112112

113-
114-
115113
public void Stage(string path)
116114
{
117115
Ensure.ArgumentNotNullOrEmptyString(path, "path");
118116

119117
var res = NativeMethods.git_index_add(handle, BuildRelativePathFrom(path));
120118
Ensure.Success(res);
119+
120+
UpdatePhysicalIndex();
121121
}
122122

123123
public void Unstage(string path)
@@ -129,6 +129,14 @@ public void Unstage(string path)
129129

130130
res = NativeMethods.git_index_remove(handle, res);
131131
Ensure.Success(res);
132+
133+
UpdatePhysicalIndex();
134+
}
135+
136+
private void UpdatePhysicalIndex()
137+
{
138+
int res = NativeMethods.git_index_write(handle);
139+
Ensure.Success(res);
132140
}
133141

134142
private string BuildRelativePathFrom(string path) //TODO: To be removed when libgit2 natively implements this

0 commit comments

Comments
 (0)
0