diff --git a/libgit2sharp.Tests/ApplyingATag.cs b/libgit2sharp.Tests/ApplyingATag.cs index 33806f0d5cfde99bfc975077e1a698fc5690044c..c8e4da7ce88210610d6c7b97be84f68f0112f4af 100644 --- a/libgit2sharp.Tests/ApplyingATag.cs +++ b/libgit2sharp.Tests/ApplyingATag.cs @@ -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(() => 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; diff --git a/libgit2sharp/ObjectNotFoundException.cs b/libgit2sharp/ObjectNotFoundException.cs new file mode 100644 index 0000000000000000000000000000000000000000..a4a0b5545a42b081910c13af8b02ebe8cae0d8b6 --- /dev/null +++ b/libgit2sharp/ObjectNotFoundException.cs @@ -0,0 +1,8 @@ +using System; + +namespace libgit2sharp +{ + public class ObjectNotFoundException : Exception + { + } +} \ No newline at end of file diff --git a/libgit2sharp/Repository.cs b/libgit2sharp/Repository.cs index 4361b0f5f979912a24efbfaac04af1bf57965f32..df9e3e23570f5aaf700e5400e244b4d45ac4aa6e 100644 --- a/libgit2sharp/Repository.cs +++ b/libgit2sharp/Repository.cs @@ -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 diff --git a/libgit2sharp/libgit2sharp.csproj b/libgit2sharp/libgit2sharp.csproj index 23017e0b20f63151c1cab2578eb857d964dfd5dd..88bcd79518ded541f032cc813cab7d03a38d02d0 100644 --- a/libgit2sharp/libgit2sharp.csproj +++ b/libgit2sharp/libgit2sharp.csproj @@ -49,6 +49,7 @@ +