提交 f48f5d11 编写于 作者: N nulltoken

Improved Repository.ApplyTag() error handling.

上级 48c6043b
......@@ -6,16 +6,29 @@ namespace libgit2sharp.Tests
[TestFixture]
public class ApplyingATag : ReadWriteRepositoryFixtureBase
{
private static readonly Signature _signature = new Signature("me", "me@me.me", DateTimeOffset.Now);
[Test]
public void ShouldThrowIfPassedANonExistingTarget()
{
const string invalidTargetId = "deadbeef1b46c854b31185ea97743be6a8774479";
using (var repo = new Repository(PathToRepository))
{
Assert.Throws<ObjectNotFoundException>(() => repo.ApplyTag(invalidTargetId, "tagged", "messaged", _signature));
}
}
[Test]
public void ShouldWork() // TODO: Split into different tests (returnATag, PersistTheObject, MultipleApplies, ...)
{
const string targetId = "8496071c1b46c854b31185ea97743be6a8774479";
var signature = new Signature("me", "me@me.me", DateTimeOffset.Now);
Tag appliedTag;
using (var repo = new Repository(PathToRepository))
{
appliedTag = repo.ApplyTag(targetId, "tagged", "messaged", signature);
appliedTag = repo.ApplyTag(targetId, "tagged", "messaged", _signature);
}
var target = appliedTag.Target as Commit;
......
using System;
namespace libgit2sharp
{
public class ObjectNotFoundException : Exception
{
}
}
\ No newline at end of file
......@@ -101,9 +101,19 @@ public Tag ApplyTag(string targetId, string tagName, string tagMessage, Signatur
{
// TODO: To be refactored.
IntPtr tag;
OperationResult t = LibGit2Api.wrapped_git_apply_tag(out tag, _lifecycleManager.RepositoryPtr, targetId, tagName, tagMessage, signature.Name, signature.Email, (ulong)((GitDate)signature.When).UnixTimeStamp);
return (Tag)_builder.BuildFrom(tag, ObjectType.Tag);
OperationResult result = LibGit2Api.wrapped_git_apply_tag(out tag, _lifecycleManager.RepositoryPtr, targetId, tagName, tagMessage, signature.Name, signature.Email, (ulong)((GitDate)signature.When).UnixTimeStamp);
switch (result)
{
case OperationResult.GIT_SUCCESS:
return (Tag)_builder.BuildFrom(tag, ObjectType.Tag);
case OperationResult.GIT_ENOTFOUND:
throw new ObjectNotFoundException();
default:
throw new Exception(Enum.GetName(typeof(OperationResult), result));
}
}
}
}
\ No newline at end of file
......@@ -49,6 +49,7 @@
<Compile Include="IResolver.cs" />
<Compile Include="NotAValidRepositoryException.cs" />
<Compile Include="ObjectId.cs" />
<Compile Include="ObjectNotFoundException.cs" />
<Compile Include="ObjectResolver.cs" />
<Compile Include="RepositoryLifecycleManager.cs" />
<Compile Include="Signature.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册