-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSimpleEntry.cs
273 lines (245 loc) · 8.97 KB
/
SimpleEntry.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Text;
using LibGit2Sharp;
namespace GitRocketFilter
{
/// <summary>
/// Represents a git blob or link in a tree.
/// </summary>
public struct SimpleEntry
{
private readonly Repository repo;
private readonly TreeEntry entry;
private readonly GitObject target;
private readonly GitLink link;
private readonly Blob blob;
internal EntryValue NewEntryValue;
/// <summary>
/// Initializes a new instance of the <see cref="SimpleEntry" /> struct.
/// </summary>
/// <param name="repo">The repo.</param>
/// <param name="entry">The tree entry.</param>
/// <exception cref="System.ArgumentNullException">
/// repo
/// or
/// entry
/// </exception>
internal SimpleEntry(Repository repo, TreeEntry entry)
: this()
{
if (repo == null) throw new ArgumentNullException("repo");
if (entry == null) throw new ArgumentNullException("entry");
this.repo = repo;
this.entry = entry;
target = entry.Target;
this.blob = entry.Target as Blob;
this.link = entry.Target as GitLink;
}
/// <summary>
/// Gets the identifier object the blob or link object.
/// </summary>
/// <value>The identifier.</value>
public ObjectId Id
{
get { return target.Id; }
}
/// <summary>
/// Gets the text representation of <see cref="Id"/>.
/// </summary>
/// <value>The sha.</value>
public string Sha
{
get { return target.Sha; }
}
/// <summary>
/// Gets the name of this entry.
/// </summary>
/// <value>The name.</value>
public string Name
{
get { return entry.Name; }
}
/// <summary>
/// Gets the path of this entry.
/// </summary>
/// <value>The path.</value>
public string Path
{
get { return entry.Path; }
}
/// <summary>
/// Gets a value indicating whether this instance is a blob.
/// </summary>
/// <value><c>true</c> if this instance is a blob; otherwise, <c>false</c>.</value>
public bool IsBlob
{
get { return blob != null; }
}
/// <summary>
/// Gets a value indicating whether this instance is binary blob.
/// </summary>
/// <value><c>true</c> if this instance is binary blob; otherwise, <c>false</c>.</value>
public bool IsBinary
{
get { return blob != null && blob.IsBinary; }
}
/// <summary>
/// Gets a value indicating whether this instance is a git link.
/// </summary>
/// <value><c>true</c> if this instance is a git link; otherwise, <c>false</c>.</value>
public bool IsLink
{
get { return link != null; }
}
/// <summary>
/// Gets the attributes of this entry.
/// </summary>
/// <value>The attributes.</value>
public Mode Attributes
{
get { return entry.Mode; }
}
/// <summary>
/// Gets the size of the blob or 0 if it is not a blob.
/// </summary>
/// <value>The size.</value>
public long Size
{
get { return blob != null ? blob.Size : 0; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="SimpleEntry"/> should be discarded. Default is <c>false</c>.
/// </summary>
/// <value><c>true</c> if this commit should be discarded; otherwise, <c>false</c>.</value>
public bool Discard { get; set; }
/// <summary>
/// Gets or sets a tag object.
/// </summary>
/// <value>A tag object.</value>
public object Tag { get; set; }
/// <summary>
/// Gets the content of the blob as a byte array.
/// </summary>
/// <returns>The content of the blob as a byte array or null if no blob.</returns>
public byte[] GetBlobAsBytes()
{
if (blob == null)
{
return null;
}
var stream = blob.GetContentStream();
var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
return memoryStream.ToArray();
}
/// <summary>
/// Gets the content of the blob as a stream.
/// </summary>
/// <returns>The content of the blob as a stream or null if no blob.</returns>
public Stream GetBlobAsStream()
{
if (blob != null)
{
return blob.GetContentStream();
}
return null;
}
/// <summary>
/// Gets the content of the blob as a text.
/// </summary>
/// <returns>The content of the blob as a text or null if no blob.</returns>
public string GetBlobAsText()
{
if (blob != null)
{
return blob.GetContentText();
}
return null;
}
/// <summary>
/// Replace the content of this entry by a stream.
/// </summary>
/// <param name="content">The content stream.</param>
/// <param name="mode">The mode.</param>
/// <exception cref="System.ArgumentNullException">content</exception>
public void SetBlob(Stream content, Mode mode = Mode.NonExecutableFile)
{
if (content == null) throw new ArgumentNullException("content");
var newBlob = repo.ObjectDatabase.CreateBlob(content);
SetBlob(newBlob, mode);
}
/// <summary>
/// Replace the content of this entry by a byte array.
/// </summary>
/// <param name="content">The content byte array.</param>
/// <param name="mode">The mode.</param>
/// <exception cref="System.ArgumentNullException">content</exception>
public void SetBlob(byte[] content, Mode mode = Mode.NonExecutableFile)
{
if (content == null) throw new ArgumentNullException("content");
var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(content));
SetBlob(newBlob, mode);
}
/// <summary>
/// Replace the content of this entry by a string.
/// </summary>
/// <param name="content">The content string.</param>
/// <param name="mode">The mode.</param>
/// <exception cref="System.ArgumentNullException">content</exception>
public void SetBlob(string content, Mode mode = Mode.NonExecutableFile)
{
SetBlob(content, Encoding.UTF8, mode);
}
/// <summary>
/// Replace the content of this entry by a string with a particular encoding.
/// </summary>
/// <param name="content">The content string.</param>
/// <param name="encoding">The encoding mode for the string.</param>
/// <param name="mode">The mode.</param>
/// <exception cref="System.ArgumentNullException">content</exception>
public void SetBlob(string content, Encoding encoding, Mode mode = Mode.NonExecutableFile)
{
if (content == null) throw new ArgumentNullException("content");
var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(encoding.GetBytes(content)));
SetBlob(newBlob, mode);
}
/// <summary>
/// Replace the content of this entry by a blob.
/// </summary>
/// <param name="newBlob">The content blob.</param>
/// <param name="mode">The mode.</param>
/// <exception cref="System.ArgumentNullException">content</exception>
public void SetBlob(Blob newBlob, Mode mode = Mode.NonExecutableFile)
{
if (newBlob == null) throw new ArgumentNullException("newBlob");
NewEntryValue = new EntryValue(newBlob, mode);
}
/// <summary>
/// Performs an implicit conversion from <see cref="SimpleEntry"/> to <see cref="TreeEntry"/>.
/// </summary>
/// <param name="entry">The entry.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator TreeEntry(SimpleEntry entry)
{
return entry.entry;
}
public override string ToString()
{
return string.Format("Entry: {0} => {1}", Path, Id);
}
internal struct EntryValue
{
public EntryValue(Blob blob, Mode mode)
{
Blob = blob;
Mode = mode;
}
public readonly Blob Blob;
public readonly Mode Mode;
}
}
}