8000 Expose low level Index.Add() · GiTechLab/libgit2sharp@18745ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 18745ed

Browse files
committed
Expose low level Index.Add()
1 parent a8973a4 commit 18745ed

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,5 +392,37 @@ public void CanRemoveAnEntryFromTheIndex(string pathInTheIndex, FileStatus expec
392392
Assert.Equal(expectedAfterStatus, after);
393393
}
394394
}
395+
396+
[Theory]
397+
[InlineData("new_untracked_file.txt", FileStatus.Untracked, FileStatus.Added)]
398+
[InlineData("modified_unstaged_file.txt", FileStatus.Modified, FileStatus.Staged)]
399+
public void CanAddAnEntryToTheIndexFromAFileInTheWorkdir(string pathInTheWorkdir, FileStatus expectedBeforeStatus, FileStatus expectedAfterStatus)
400+
{
401+
var path = SandboxStandardTestRepoGitDir();
402+
using (var repo = new Repository(path))
403+
{
404+
var before = repo.RetrieveStatus(pathInTheWorkdir);
405+
Assert.Equal(expectedBeforeStatus, before);
406+
407+
repo.Index.Add(pathInTheWorkdir);
408+
409+
var after = repo.RetrieveStatus(pathInTheWorkdir);
410+
Assert.Equal(expectedAfterStatus, after);
411+
}
412+
}
413+
414+
[Fact]
415+
public void AddingAnEntryToTheIndexFromAUnknwonFileInTheWorkdirThrows()
416+
{
417+
var path = SandboxStandardTestRepoGitDir();
418+
using (var repo = new Repository(path))
419+
{
420+
const string filePath = "i_dont_exist.txt";
421+
var before = repo.RetrieveStatus(filePath);
422+
Assert.Equal(FileStatus.Nonexistent, before);
423+
424+
Assert.Throws<NotFoundException>(() => repo.Index.Add(filePath));
425+
}
426+
}
395427
}
396428
}

LibGit2Sharp/Index.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ public virtual void Remove(string indexEntryPath)
178178
UpdatePhysicalIndex();
179179
}
180180

181+
/// <summary>
182+
/// Adds a file from the workdir in the <see cref="Index"/>.
183+
/// <para>
184+
/// If an entry with the same path already exists in the <see cref="Index"/>,
185+
/// the newly added one will overwrite it.
186+
/// </para>
187+
/// </summary>
188+
/// <param name="pathInTheWorkdir">The path, in the working directory, of the file to be added.</param>
189+
public virtual void Add(string pathInTheWorkdir)
190+
{
191+
if (pathInTheWorkdir == null)
192+
{
193+
throw new ArgumentNullException("pathInTheWorkdir");
194+
}
195+
196+
Proxy.git_index_add_bypath(handle, pathInTheWorkdir);
197+
198+
UpdatePhysicalIndex();
199+
}
200+
181201
private void UpdatePhysicalIndex()
182202
{
183203
Proxy.git_index_write(handle);

0 commit comments

Comments
 (0)
0