Skip to content

Commit 1232e69

Browse files
committed
Fold BlobExtensions.cs into Blob.cs
1 parent 87067b5 commit 1232e69

File tree

3 files changed

+58
-77
lines changed

3 files changed

+58
-77
lines changed

LibGit2Sharp/Blob.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34
using LibGit2Sharp.Core;
45

56
namespace LibGit2Sharp
@@ -55,7 +56,64 @@ public virtual Stream GetContentStream()
5556
public virtual Stream GetContentStream(FilteringOptions filteringOptions)
5657
{
5758
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");
59+
5860
return Proxy.git_blob_filtered_content_stream(repo.Handle, Id, filteringOptions.HintPath, false);
5961
}
62+
63+
/// <summary>
64+
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected from the byte order mark
65+
/// </summary>
66+
/// <returns>Blob content as text.</returns>
67+
public virtual string GetContentText()
68+
{
69+
return ReadToEnd(GetContentStream(), null);
70+
}
71+
72+
/// <summary>
73+
/// Gets the blob content decoded with the specified encoding,
74+
/// or according to byte order marks, or the specified encoding as a fallback
75+
/// </summary>
76+
/// <param name="encoding">The encoding of the text to use, if it cannot be detected</param>
77+
/// <returns>Blob content as text.</returns>
78+
public virtual string GetContentText(Encoding encoding)
79+
{
80+
Ensure.ArgumentNotNull(encoding, "encoding");
81+
82+
return ReadToEnd(GetContentStream(), encoding);
83+
}
84+
85+
/// <summary>
86+
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected
87+
/// </summary>
88+
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
89+
/// <returns>Blob content as text.</returns>
90+
public virtual string GetContentText(FilteringOptions filteringOptions)
91+
{
92+
return GetContentText(filteringOptions, null);
93+
}
94+
95+
/// <summary>
96+
/// Gets the blob content as it would be checked out to the
97+
/// working directory, decoded with the specified encoding,
98+
/// or according to byte order marks, with UTF8 as fallback,
99+
/// if <paramref name="encoding"/> is null.
100+
/// </summary>
101+
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
102+
/// <param name="encoding">The encoding of the text. (default: detected or UTF8)</param>
103+
/// <returns>Blob content as text.</returns>
104+
public virtual string GetContentText(FilteringOptions filteringOptions, Encoding encoding)
105+
{
106+
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");
107+
108+
return ReadToEnd(GetContentStream(filteringOptions), encoding);
109+
}
110+
111+
private static string ReadToEnd(Stream stream, Encoding encoding)
112+
{
113+
using (var reader = new StreamReader(stream, encoding ?? LaxUtf8Marshaler.Encoding, encoding == null))
114+
{
115+
return reader.ReadToEnd();
116+
}
117+
}
60118
}
61119
}

LibGit2Sharp/BlobExtensions.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
<Compile Include="BlameHunk.cs" />
5252
<Compile Include="BlameOptions.cs" />
5353
<Compile Include="Blob.cs" />
54-
<Compile Include="BlobExtensions.cs" />
5554
<Compile Include="Branch.cs" />
5655
<Compile Include="BranchCollection.cs" />
5756
<Compile Include="BranchCollectionExtensions.cs" />

0 commit comments

Comments
 (0)