Skip to content

Commit 104efdc

Browse files
committed
can create, lookup tags
1 parent d5ae5db commit 104efdc

11 files changed

+375
-61
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.Net35.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="Properties\AssemblyInfo.cs" />
5252
<Compile Include="ReferenceFixture.cs" />
5353
<Compile Include="RepositoryFixture.cs" />
54+
<Compile Include="TagFixture.cs" />
5455
<Compile Include="TestHelpers\AssertExtensions.cs" />
5556
<Compile Include="TestHelpers\DirectoryHelper.cs" />
5657
<Compile Include="TestHelpers\SelfCleaningDirectory.cs" />

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="EpochFixture.cs" />
5353
<Compile Include="ObjectIdFixture.cs" />
5454
<Compile Include="ReferenceFixture.cs" />
55+
<Compile Include="TagFixture.cs" />
5556
<Compile Include="TestHelpers\AssertExtensions.cs" />
5657
<Compile Include="TestHelpers\DirectoryHelper.cs" />
5758
<Compile Include="Properties\AssemblyInfo.cs" />

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using NUnit.Framework;
6+
7+
namespace LibGit2Sharp.Tests
8+
{
9+
[TestFixture]
10+
public class TagFixture
11+
{
12+
private readonly List<string> expectedTags = new List<string> {"test", "very-simple"};
13+
14+
private static readonly Signature signature = new Signature("Tim Clem", "timothy.clem@gail.com", DateTimeOffset.UtcNow);
15+
16+
[Test]
17+
public void CanCreateTag()
18+
{
19+
using (var path = new TemporaryRepositoryPath())
20+
using (var repo = new Repository(path.RepositoryPath))
21+
{
22+
var newTag = repo.Tags.Create("unit_test", "refs/heads/master", signature, "a new tag");
23+
newTag.ShouldNotBeNull();
24+
}
25+
}
26+
27+
[Test]
28+
public void CanCreateTagFromSha()
29+
{
30+
using (var path = new TemporaryRepositoryPath())
31+
using (var repo = new Repository(path.RepositoryPath))
32+
{
33+
var newTag = repo.Tags.Create("unit_test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1", signature, "a new tag");
34+
newTag.ShouldNotBeNull();
35+
}
36+
}
37+
38+
[Test]
39+
[Ignore("TODO: the very-simple tag is coming back with an odd name.")]
40+
public void CanListTags()
41+
{
42+
using (var repo = new Repository(Constants.TestRepoPath))
43+
{
44+
foreach (var tag in repo.Tags)
45+
{
46+
Trace.WriteLine(tag.Name);
47+
expectedTags.Contains(tag.Name).ShouldBeTrue();
48+
}
49+
repo.Tags.Count().ShouldEqual(2);
50+
}
51+
}
52+
53+
[Test]
54+
public void CanLookupTag()
55+
{
56+
using (var repo = new Repository(Constants.TestRepoPath))
57+
{
58+
var tag = repo.Tags["test"];
59+
tag.ShouldNotBeNull();
60+
tag.Name.ShouldEqual("test");
61+
tag.Sha.ShouldEqual("b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
62+
tag.Tagger.Email.ShouldEqual("tanoku@gmail.com");
63+
tag.Tagger.Name.ShouldEqual("Vicent Marti");
64+
tag.Tagger.When.ToSecondsSinceEpoch().ShouldEqual(1281578440);
65+
tag.Message.ShouldEqual("This is a test tag\n");
66+
}
67+
}
68+
69+
[Test]
70+
public void CreateTagWithEmptyMessageThrows()
71+
{
72+
using (var repo = new Repository(Constants.TestRepoPath))
73+
{
74+
Assert.Throws<ArgumentException>(() => repo.Tags.Create("test_tag", "refs/heads/master", signature, string.Empty));
75+
}
76+
}
77+
78+
[Test]
79+
public void CreateTagWithEmptyNameThrows()
80+
{
81+
using (var repo = new Repository(Constants.TestRepoPath))
82+
{
83+
Assert.Throws<ArgumentException>(() => repo.Tags.Create(string.Empty, "refs/heads/master", signature, "message"));
84+
}
85+
}
86+
87+
[Test]
88+
public void CreateTagWithEmptyTargetThrows()
89+
{
90+
using (var repo = new Repository(Constants.TestRepoPath))
91+
{
92+
Assert.Throws<ArgumentException>(() => repo.Tags.Create("test_tag", string.Empty, signature, "message"));
93+
}
94+
}
95+
96+
[Test]
97+
public void CreateTagWithNullMessageThrows()
98+
{
99+
using (var repo = new Repository(Constants.TestRepoPath))
100+
{
101+
Assert.Throws<ArgumentNullException>(() => repo.Tags.Create("test_tag", "refs/heads/master", signature, null));
102+
}
103+
}
104+
105+
[Test]
106+
public void CreateTagWithNullNameThrows()
107+
{
108+
using (var repo = new Repository(Constants.TestRepoPath))
109+
{
110+
Assert.Throws<ArgumentNullException>(() => repo.Tags.Create(null, "refs/heads/master", signature, "message"));
111+
}
112+
}
113+
114+
[Test]
115+
public void CreateTagWithNullSignatureThrows()
116+
{
117+
using (var repo = new Repository(Constants.TestRepoPath))
118+
{
119+
Assert.Throws<ArgumentNullException>(() => repo.Tags.Create("test_tag", "refs/heads/master", null, "message"));
120+
}
121+
}
122+
123+
[Test]
124+
public void CreateTagWithNullTargetThrows()
125+
{
126+
using (var repo = new Repository(Constants.TestRepoPath))
127+
{
128+
Assert.Throws<ArgumentNullException>(() => repo.Tags.Create("test_tag", null, signature, "message"));
129+
}
130+
}
131+
132+
[Test]
133+
public void LookupEmptyTagNameThrows()
134+
{
135+
using (var repo = new Repository(Constants.TestRepoPath))
136+
{
137+
Assert.Throws<ArgumentException>(() => { var t = repo.Tags[string.Empty]; });
138+
}
139+
}
140+
141+
[Test]
142+
public void LookupNullTagNameThrows()
143+
{
144+
using (var repo = new Repository(Constants.TestRepoPath))
145+
{
146+
Assert.Throws<ArgumentNullException>(() => { var t = repo.Tags[null]; });
147+
}
148+
}
149+
}
150+
}

LibGit2Sharp/LibGit2Sharp.Net35.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@
7474
<Compile Include="Signature.cs" />
7575
<Compile Include="SymbolicReference.cs" />
7676
<Compile Include="Tag.cs" />
77+
<Compile Include="TagCollection.cs" />
7778
<Compile Include="Tree.cs" />
79+
<Compile Include="UnSafeNativeMethods.cs" />
7880
</ItemGroup>
7981
<ItemGroup>
8082
<None Include="..\Lib\git2.lib">

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
<Compile Include="Signature.cs" />
7373
<Compile Include="SymbolicReference.cs" />
7474
<Compile Include="Tag.cs" />
75+
<Compile Include="TagCollection.cs" />
7576
<Compile Include="Tree.cs" />
77+
<Compile Include="UnSafeNativeMethods.cs" />
7678
</ItemGroup>
7779
<ItemGroup>
7880
<None Include="..\Lib\git2.lib">
@@ -89,7 +91,6 @@
8991
</ItemGroup>
9092
<ItemGroup />
9193
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
92-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
9394
<PropertyGroup>
9495
<PreBuildEvent>
9596
</PreBuildEvent>

LibGit2Sharp/NativeMethods.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,6 @@
33

44
namespace LibGit2Sharp
55
{
6-
internal unsafe class UnSafeNativeMethods
7-
{
8-
private const string libgit2 = "git2.dll";
9-
10-
[DllImport(libgit2)]
11-
public static extern int git_reference_listall(git_strarray* array, RepositorySafeHandle repo, GitReferenceType flags);
12-
13-
[DllImport(libgit2)]
14-
public static extern void git_strarray_free(git_strarray* array);
15-
16-
#region Nested type: git_strarray
17-
18-
internal struct git_strarray
19-
{
20-
public sbyte** strings;
21-
public IntPtr size;
22-
}
23-
24-
#endregion
25-
}
26-
276
internal class NativeMethods
287
{
298
private const string libgit2 = "git2.dll";
@@ -145,5 +124,22 @@ internal class NativeMethods
145124

146125
[DllImport(libgit2, SetLastError = true)]
147126
public static extern IntPtr git_signature_new(string name, string email, long time, int offset);
127+
128+
[DllImport(libgit2, SetLastError = true)]
129+
public static extern int git_tag_create(out GitOid oid, RepositorySafeHandle repo, string name, ref GitOid target, GitObjectType type, GitSignature signature, string message);
130+
131+
[DllImport(libgit2, SetLastError = true)]
132+
[return: MarshalAs(UnmanagedType.AnsiBStr)]
133+
public static extern string git_tag_message(IntPtr tag);
134+
135+
[DllImport(libgit2, SetLastError = true)]
136+
[return: MarshalAs(UnmanagedType.AnsiBStr)]
137+
public static extern string git_tag_name(IntPtr tag);
138+
139+
[DllImport(libgit2, SetLastError = true)]
140+
public static extern IntPtr git_tag_tagger(IntPtr tag);
141+
142+
[DllImport(libgit2, SetLastError = true)]
143+
public static extern IntPtr git_tag_target_oid(IntPtr tag);
148144
}
149145
}

0 commit comments

Comments
 (0)