8000 Teach low level Index.Add() to accept a Blob · markcapaldi/libgit2sharp@f4c7bd6 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit f4c7bd6

Browse files
committed
Teach low level Index.Add() to accept a Blob
Fix libgit2#195
1 parent 18745ed commit f4c7bd6

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,25 @@ public void CanAddAnEntryToTheIndexFromAFileInTheWorkdir(string pathInTheWorkdir
411411
}
412412
}
413413

414+
[Fact]
415+
public void CanAddAnEntryToTheIndexFromABlob()
416+
{
417+
var path = SandboxStandardTestRepoGitDir();
418+
using (var repo = new Repository(path))
419+
{
420+
const string targetIndexEntryPath = "1.txt";
421+
var before = repo.RetrieveStatus(targetIndexEntryPath);
422+
Assert.Equal(FileStatus.Unaltered, before);
423+
424+
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
425+
426+
repo.Index.Add(blob, targetIndexEntryPath, Mode.NonExecutableFile);
427+
428+
var after = repo.RetrieveStatus(targetIndexEntryPath);
429+
Assert.Equal(FileStatus.Staged | FileStatus.Modified, after);
430+
}
431+
}
432+
414433
[Fact]
415434
public void AddingAnEntryToTheIndexFromAUnknwonFileInTheWorkdirThrows()
416435
{

LibGit2Sharp/Index.cs

Lines changed: 39 additions & 5 deletions
< 8000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,36 @@ public virtual void Add(string pathInTheWorkdir)
198198
UpdatePhysicalIndex();
199199
}
200200

201+
/// <summary>
202+
/// Adds an entry in the <see cref="Index"/> from a <see cref="Blob"/>.
203+
/// <para>
204+
/// If an entry with the same path already exists in the <see cref="Index"/>,
205+
/// the newly added one will overwrite it.
206+
/// </para>
207+
/// </summary>
208+
/// <param name="blob">The <see cref="Blob"/> which content should be added to the <see cref="Index"/>.</param>
209+
/// <param name="indexEntryPath">The path to be used in the <see cref="Index"/>.</param>
210+
/// <param name="indexEntryMode">Either <see cref="Mode.NonExecutableFile"/>, <see cref="Mode.ExecutableFile"/>
211+
/// or <see cref="Mode.SymbolicLink"/>.</param>
212+
public virtual void Add(Blob blob, string indexEntryPath, Mode indexEntryMode)
213+
{
214+
Ensure.ArgumentConformsTo(indexEntryMode, m => m.HasAny(TreeEntryDefinition.BlobModes), "indexEntryMode");
215+
216+
if (blob == null)
217+
{
218+
throw new ArgumentNullException("blob");
219+
}
220+
221+
if (indexEntryPath == null)
222+
{
223+
throw new ArgumentNullException("indexEntryPath");
224+
}
225+
226+
AddEntryToTheIndex(indexEntryPath, blob.Id, indexEntryMode);
227+
228+
UpdatePhysicalIndex();
229+
}
230+
201231
private void UpdatePhysicalIndex()
202232
{
203233
Proxy.git_index_write(handle);
@@ -219,7 +249,11 @@ internal void Replace(TreeChanges changes)
219249
case ChangeKind.Deleted:
220250
/* Fall through */
221251
case ChangeKind.Modified:
222-
ReplaceIndexEntryWith(treeEntryChanges);
252+
AddEntryToTheIndex(
253+
treeEntryChanges.OldPath,
254+
treeEntryChanges.OldOid,
255+
treeEntryChanges.OldMode);
256+
223257
continue;
224258

225259
default:
@@ -241,13 +275,13 @@ public virtual ConflictCollection Conflicts
241275
}
242276
}
243277

244-
private void ReplaceIndexEntryWith(TreeEntryChanges treeEntryChanges)
278+
private void AddEntryToTheIndex(string path, ObjectId id, Mode mode)
245279
{
246280
var indexEntry = new GitIndexEntry
247281
{
248-
Mode = (uint)treeEntryChanges.OldMode,
249-
Id = treeEntryChanges.OldOid.Oid,
250-
Path = StrictFilePathMarshaler.FromManaged(treeEntryChanges.OldPath),
282+
Mode = (uint)mode,
283+
Id = id.Oid,
284+
Path = StrictFilePathMarshaler.FromManaged(path),
251285
};
252286

253287
Proxy.git_index_add(handle, indexEntry);

0 commit comments

Comments
 (0)
0