Permalink
Browse files

Make Tags.Add() accept a GitObject as its target

  • Loading branch information...
nulltoken committed Aug 27, 2012
1 parent 58932d0 commit e5f216f587e0f20c50fc80fcc4eebfa69edd67e7
Showing with 86 additions and 2 deletions.
  1. +31 −2 LibGit2Sharp.Tests/TagFixture.cs
  2. +55 −0 LibGit2Sharp/TagCollection.cs
@@ -30,7 +30,6 @@ public void CanAddALightWeightTagFromSha()
}
}
-
[Fact]
public void CanAddALightWeightTagFromObjectId()
{
@@ -44,6 +43,21 @@ public void CanAddALightWeightTagFromObjectId()
}
}
+ [Fact]
+ public void CanAddALightWeightTagFromAGitObject()
+ {
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
+ using (var repo = new Repository(path.RepositoryPath))
+ {
+ GitObject obj = repo.Lookup(commitE90810BSha);
+
+ Tag newTag = repo.Tags.Add("i_am_lightweight", obj);
+ Assert.NotNull(newTag);
+ Assert.False(newTag.IsAnnotated);
+ Assert.Equal(commitE90810BSha, newTag.Target.Sha);
+ }
+ }
+
[Fact]
public void CanAddALightWeightTagFromAbbreviatedSha()
{
@@ -153,7 +167,6 @@ public void CanAddAnAnnotatedTagFromSha()
}
}
-
[Fact]
public void CanAddAnAnnotatedTagFromObjectId()
{
@@ -167,6 +180,21 @@ public void CanAddAnAnnotatedTagFromObjectId()
}
}
+ [Fact]
+ public void CanAddAnAnnotatedTagFromObject()
+ {
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo();
+ using (var repo = new Repository(path.RepositoryPath))
+ {
+ GitObject obj = repo.Lookup(tagTestSha);
+
+ Tag newTag = repo.Tags.Add("unit_test",obj, signatureTim, "a new tag");
+ Assert.NotNull(newTag);
+ Assert.True(newTag.IsAnnotated);
+ Assert.Equal(tagTestSha, newTag.Target.Sha);
+ }
+ }
+
[Fact]
public void CanAddAnAnnotatedTagFromARevparseSpec()
{
@@ -491,6 +519,7 @@ public void AddTagWithNullTargetThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
+ Assert.Throws<ArgumentNullException>(() => repo.Tags.Add("test_tag", (GitObject)null, signatureTim, "message"));
Assert.Throws<ArgumentNullException>(() => repo.Tags.Add("test_tag", (string)null, signatureTim, "message"));
Assert.Throws<ArgumentNullException>(() => repo.Tags.Add("test_tag", (ObjectId)null, signatureTim, "message"));
}
@@ -100,6 +100,37 @@ public virtual Tag Add(string name, ObjectId targetId, Signature tagger, string
return this[name];
}
+ /// <summary>
+ /// Creates an annotated tag with the specified name.
+ /// </summary>
+ /// <param name = "name">The name.</param>
+ /// <param name = "target">The target <see cref="GitObject"/>.</param>
+ /// <param name = "tagger">The tagger.</param>
+ /// <param name = "message">The message.</param>
+ /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing tag, false otherwise.</param>
+ /// <returns></returns>
+ public virtual Tag Add(string name, GitObject target, Signature tagger, string message, bool allowOverwrite = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+ Ensure.ArgumentNotNull(target, "target");
+ Ensure.ArgumentNotNull(tagger, "tagger");
+ Ensure.ArgumentNotNull(message, "message");
+
+ string prettifiedMessage = ObjectDatabase.PrettifyMessage(message);
+
+ int res;
+ using (var objectPtr = new ObjectSafeWrapper(target.Id, repo))
+ using (SignatureSafeHandle taggerHandle = tagger.BuildHandle())
+ {
+ GitOid oid;
+ res = NativeMethods.git_tag_create(out oid, repo.Handle, name, objectPtr.ObjectPtr, taggerHandle, prettifiedMessage, allowOverwrite);
+ }
+
+ Ensure.Success(res);
+
+ return this[name];
+ }
+
internal static string PrettifyMessage(string message)
{
var buffer = new byte[NativeMethods.GIT_PATH_MAX];
@@ -124,6 +155,30 @@ public virtual Tag Create(string name, string target, Signature tagger, string m
return this.Add(name, target, tagger, message, allowOverwrite);
}
+ /// <summary>
+ /// Creates a lightweight tag with the specified name.
+ /// </summary>
+ /// <param name = "name">The name.</param>
+ /// <param name = "target">The target <see cref="GitObject"/>.</param>
+ /// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing tag, false otherwise.</param>
+ /// <returns></returns>
+ public virtual Tag Add(string name, GitObject target, bool allowOverwrite = false)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(name, "name");
+ Ensure.ArgumentNotNull(target, "target");
+
+ int res;
+ using (var objectPtr = new ObjectSafeWrapper(target.Id, repo))
+ {
+ GitOid oid;
+ res = NativeMethods.git_tag_create_lightweight(out oid, repo.Handle, name, objectPtr.ObjectPtr, allowOverwrite);
+ }
+
+ Ensure.Success(res);
+
+ return this[name];
+ }
+
/// <summary>
/// Creates a lightweight tag with the specified name.
/// </summary>

0 comments on commit e5f216f

Please sign in to comment.