forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagCollection.cs
More file actions
158 lines (133 loc) · 5.71 KB
/
Copy pathTagCollection.cs
File metadata and controls
158 lines (133 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// The collection of <see cref = "Tag" />s in a <see cref = "Repository" />
/// </summary>
public class TagCollection : IEnumerable<Tag>
{
private readonly Repository repo;
private const string refsTagsPrefix = "refs/tags/";
/// <summary>
/// Initializes a new instance of the <see cref = "TagCollection" /> class.
/// </summary>
/// <param name = "repo">The repo.</param>
internal TagCollection(Repository repo)
{
this.repo = repo;
}
/// <summary>
/// Gets the <see cref = "Tag" /> with the specified name.
/// </summary>
public Tag this[string name]
{
get
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
var canonicalName = NormalizeToCanonicalName(name);
var reference = repo.Refs.Resolve<Reference>(canonicalName);
return reference == null ? null : new Tag(repo, reference, canonicalName);
}
}
#region IEnumerable<Tag> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
public IEnumerator<Tag> GetEnumerator()
{
return Libgit2UnsafeHelper
.ListAllTagNames(repo.Handle)
.Select(n => this[n])
.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref = "IEnumerator" /> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
/// <summary>
/// Creates an annotated tag with the specified name.
/// </summary>
/// <param name = "name">The name.</param>
/// <param name = "target">The target which can be sha or a canonical reference name.</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 Tag Create(string name, string target, Signature tagger, string message, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(target, "target");
Ensure.ArgumentNotNull(tagger, "tagger");
Ensure.ArgumentNotNull(message, "message");
GitObject objectToTag = repo.Lookup(target, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound);
int res;
using (var objectPtr = new ObjectSafeWrapper(objectToTag.Id, repo))
using (SignatureSafeHandle taggerHandle = tagger.BuildHandle())
{
GitOid oid;
res = NativeMethods.git_tag_create(out oid, repo.Handle, name, objectPtr.ObjectPtr, taggerHandle, message, allowOverwrite);
}
Ensure.Success(res);
return this[name];
}
/// <summary>
/// Creates a lightweight tag with the specified name.
/// </summary>
/// <param name = "name">The name.</param>
/// <param name = "target">The target which can be sha or a canonical reference name.</param>
/// <param name = "allowOverwrite">True to allow silent overwriting a potentially existing tag, false otherwise.</param>
/// <returns></returns>
public Tag Create(string name, string target, bool allowOverwrite = false)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(target, "target");
GitObject objectToTag = repo.Lookup(target, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound);
int res;
using (var objectPtr = new ObjectSafeWrapper(objectToTag.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>
/// Deletes the tag with the specified name.
/// </summary>
/// <param name = "name">The short or canonical name of the tag to delete.</param>
public void Delete(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
int res = NativeMethods.git_tag_delete(repo.Handle, UnCanonicalizeName(name));
Ensure.Success(res);
}
private static string NormalizeToCanonicalName(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
if (name.StartsWith(refsTagsPrefix, StringComparison.Ordinal))
{
return name;
}
return string.Concat(refsTagsPrefix, name);
}
private static string UnCanonicalizeName(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
if (!name.StartsWith(refsTagsPrefix, StringComparison.Ordinal))
{
return name;
}
return name.Substring(refsTagsPrefix.Length);
}
}
}