Skip to content

Commit

Permalink
Introduce Blob.ContentAsText()
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed Aug 7, 2013
1 parent 14ab70e commit a50756e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/AttributesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static void AssertNormalization(Repository repo, string filename, bool s
var blob = repo.Lookup<Blob>(entry.Id);
Assert.NotNull(blob);

Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsUtf8().Contains("\r"));
Assert.Equal(!shouldHaveBeenNormalized, blob.ContentAsText().Contains("\r"));
}

private static void CreateAttributesFile(Repository repo)
Expand Down
40 changes: 38 additions & 2 deletions LibGit2Sharp.Tests/BlobFixture.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
public class BlobFixture : BaseFixture
{
[Fact]
public void CanGetBlobAsUtf8()
public void CanGetBlobAsText()
{
using (var repo = new Repository(BareTestRepoPath))
{
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");

string text = blob.ContentAsUtf8();
var text = blob.ContentAsText();

Assert.Equal("hey there\n", text);
}
}

[Theory]
[InlineData("ascii", 4, "31 32 33 34")]
[InlineData("utf-7", 4, "31 32 33 34")]
// [InlineData("utf-8", 7, "EF BB BF 31 32 33 34")] // Fails due to BOM
// [InlineData("utf-16", 10, "FF FE 31 00 32 00 33 00 34 00")] // Fails due to BOM
// [InlineData("unicodeFFFE", 10, "FE FF 00 31 00 32 00 33 00 34")] // Fails due to BOM
// [InlineData("utf-32", 20, "FF FE 00 00 31 00 00 00 32 00 00 00 33 00 00 00 34 00 00 00")] // Fails due to BOM
public void CanGetBlobAsTextWithVariousEncodings(string encodingName, int expectedContentBytes, string expectedUtf7Chars)
{
var path = CloneStandardTestRepo();
using (var repo = new Repository(path))
{
var bomFile = "bom.txt";
var content = "1234";
var encoding = Encoding.GetEncoding(encodingName);

var bomPath = Touch(repo.Info.WorkingDirectory, bomFile, content, encoding);
Assert.Equal(expectedContentBytes, File.ReadAllBytes(bomPath).Length);

repo.Index.Stage(bomFile);
var commit = repo.Commit("bom", Constants.Signature, Constants.Signature);

var blob = (Blob)commit.Tree[bomFile].Target;
Assert.Equal(expectedContentBytes, blob.Content.Length);

var text = blob.ContentAsText(encoding);
Assert.Equal(content, text);

var utf7Chars = blob.ContentAsText(Encoding.UTF7).Select(c => ((int)c).ToString("X2")).ToArray();
Assert.Equal(expectedUtf7Chars, string.Join(" ", utf7Chars));
}
}

[Fact]
public void CanGetBlobSize()
{
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public void CanDirectlyAccessABlobOfTheCommit()
var blob = commit["1/branch_file.txt"].Target as Blob;
Assert.NotNull(blob);

Assert.Equal("hi\n", blob.ContentAsUtf8());
Assert.Equal("hi\n", blob.ContentAsText());
}
}

Expand Down Expand Up @@ -694,7 +694,7 @@ public void CanCommitALittleBit()
private static void AssertBlobContent(TreeEntry entry, string expectedContent)
{
Assert.Equal(TreeEntryTargetType.Blob, entry.TargetType);
Assert.Equal(expectedContent, ((Blob)(entry.Target)).ContentAsUtf8());
Assert.Equal(expectedContent, ((Blob)(entry.Target)).ContentAsText());
}

private static void AddCommitToRepo(string path)
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/ObjectDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void CanCreateABlobIntoTheDatabaseOfABareRepository()

Assert.NotNull(blob);
Assert.Equal("dc53d4c6b8684c21b0b57db29da4a2afea011565", blob.Sha);
Assert.Equal("I'm a new file\n", blob.ContentAsUtf8());
Assert.Equal("I'm a new file\n", blob.ContentAsText());

var fetchedBlob = repo.Lookup<Blob>(blob.Id);
Assert.Equal(blob, fetchedBlob);
Expand Down
25 changes: 22 additions & 3 deletions LibGit2Sharp/BlobExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text;
using System;
using System.IO;
using System.Text;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
Expand All @@ -8,28 +10,45 @@ namespace LibGit2Sharp
/// </summary>
public static class BlobExtensions
{
/// <summary>
/// Gets the blob content decoded with the specified encoding,
/// or UTF8 if encoding is not specified.
/// </summary>
/// <param name="blob">The blob for which the content will be returned.</param>
/// <param name="encoding">The encoding of the text. (default: UTF8)</param>
/// <returns>Blob content as text.</returns>
public static string ContentAsText(this Blob blob, Encoding encoding = null)
{
Ensure.ArgumentNotNull(blob, "blob");
encoding = encoding ?? Encoding.UTF8;

return encoding.GetString(blob.Content);
}

/// <summary>
/// Gets the blob content decoded as UTF-8.
/// </summary>
/// <param name="blob">The blob for which the content will be returned.</param>
/// <returns>Blob content as UTF-8</returns>
[Obsolete("This method will be removed in the next release. Please use ContentAsText()")]
public static string ContentAsUtf8(this Blob blob)
{
Ensure.ArgumentNotNull(blob, "blob");

return Encoding.UTF8.GetString(blob.Content);
return blob.ContentAsText(Encoding.UTF8);
}

/// <summary>
/// Gets the blob content decoded as Unicode.
/// </summary>
/// <param name="blob">The blob for which the content will be returned.</param>
/// <returns>Blob content as unicode.</returns>
[Obsolete("This method will be removed in the next release. Please use ContentAsText()")]
public static string ContentAsUnicode(this Blob blob)
{
Ensure.ArgumentNotNull(blob, "blob");

return Encoding.Unicode.GetString(blob.Content);
return blob.ContentAsText(Encoding.Unicode);
}
}
}

0 comments on commit a50756e

Please sign in to comment.