8000 Fix Repo.Index.Unstage() behavior · dotjosh/libgit2sharp@bdf54dd · GitHub
[go: up one dir, main page]

Skip to content

Commit bdf54dd

Browse files
committed
Fix Repo.Index.Unstage() behavior
If the Blob was previously existing, the version in the Head is restored upon unstaging.
1 parent dcf3ae5 commit bdf54dd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void CanEnumeratorStagedFiles()
6363
}
6464

6565
[Test]
66-
public void CanReadIndexEntry()
66+
public void CanFetchAnIndexEntryByItsName()
6767
{
6868
using (var repo = new Repository(Constants.StandardTestRepoPath))
6969
{
@@ -99,6 +99,7 @@ public void CanStageANewFile()
9999
{
100100
var count = repo.Index.Count;
101101
const string filename = "unit_test.txt";
102+
repo.Index[filename].ShouldBeNull();
102103
File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, filename), "some contents");
103104

104105
repo.Index.Stage(filename);
@@ -108,6 +109,43 @@ public void CanStageANewFile()
108109
}
109110
}
110111

112+
[Test]
113+
public void StagingANewVersionOfAFileThenUnstagingRevertsTheBlobToTheVersionOfHead()
114+
{
115+
using (var scd = new SelfCleaningDirectory())
116+
{
117+
string dir = Repository.Init(scd.DirectoryPath);
118+
119+
using (var repo = new Repository(dir))
120+
{
121+
repo.Index.Count.ShouldEqual(0);
122+
123+
const string fileName = "myFile.txt";
124+
125+
var fullpath = Path.Combine(repo.Info.WorkingDirectory, fileName);
126+
127+
const string initialContent = "Hello?";
128+
File.AppendAllText(fullpath, initialContent);
129+
130+
repo.Index.Stage(fileName);
131+
var blobId = repo.Index[fileName].Id;
132+
133+
repo.Commit(Constants.Signature, Constants.Signature, "Initial commit");
134+
repo.Index.Count.ShouldEqual(1);
135+
136+
File.AppendAllText(fullpath, "Is there there anybody out there?");
137+
repo.Index.Stage(fileName);
138+
139+
repo.Index.Count.ShouldEqual(1);
140+
repo.Index[fileName].Id.ShouldNotEqual((blobId));
141+
142+
repo.Index.Unstage(fileName);
143+
repo.Index.Count.ShouldEqual(1);
144+
repo.Index[fileName].Id.ShouldEqual((blobId));
145+
}
146+
}
147+
}
148+
111149
[Test]
112150
public void CanStageANewFileInAPersistentManner()
113151
{

LibGit2Sharp/Index.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public void Unstage(string path)
140140

141141
RemoveFromIndex(relativePath);
142142

143+
RestorePotentialPreviousVersionOf(relativePath);
144+
143145
UpdatePhysicalIndex();
144146
}
145147

@@ -181,6 +183,18 @@ private void RemoveFromIndex(string relativePath)
181183
Ensure.Success(res);
182184
}
183185

186+
private void RestorePotentialPreviousVersionOf(string relativePath)
187+
{
188+
var currentHeadBlob = repo.Head.Tip.Tree[relativePath];
189+
if ((currentHeadBlob == null) || currentHeadBlob.Type != GitObjectType.Blob)
190+
{
191+
return;
192+
}
193+
194+
File.WriteAllBytes(Path.Combine(repo.Info.WorkingDirectory, relativePath), ((Blob) currentHeadBlob.Target).Content);
195+
AddToIndex(relativePath);
196+
}
197+
184198
private void UpdatePhysicalIndex()
185199
{
186200
int res = NativeMethods.git_index_write(handle);

0 commit comments

Comments
 (0)
0