|
1 | 1 | using System;
|
2 | 2 | using System.IO;
|
| 3 | +using System.Text; |
3 | 4 | using LibGit2Sharp.Core;
|
4 | 5 |
|
5 | 6 | namespace LibGit2Sharp
|
@@ -55,7 +56,64 @@ public virtual Stream GetContentStream()
|
55 | 56 | public virtual Stream GetContentStream(FilteringOptions filteringOptions)
|
56 | 57 | {
|
57 | 58 | Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");
|
| 59 | + |
58 | 60 | return Proxy.git_blob_filtered_content_stream(repo.Handle, Id, filteringOptions.HintPath, false);
|
59 | 61 | }
|
| 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 | + } |
60 | 118 | }
|
61 | 119 | }
|
0 commit comments