diff options
author | nulltoken <emeric.fermas@gmail.com> | 2014-05-18 23:51:17 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2014-06-07 15:35:32 +0400 |
commit | dc815f8f5fc79b14cf85b0b15a10112b99038409 (patch) | |
tree | 8fd6e2f0b240525aceb7b82b04bd8085ee8ff72f /LibGit2Sharp.Tests | |
parent | eedeac0cd6db075ba061441fd0b75b36a7cd623b (diff) |
Introduce ObjectDatabase.ShortenObjectId()
Fix #677
Diffstat (limited to 'LibGit2Sharp.Tests')
-rw-r--r-- | LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs index 649cb26e..091876d8 100644 --- a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs +++ b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs @@ -556,5 +556,47 @@ namespace LibGit2Sharp.Tests () => repo.ObjectDatabase.CalculateHistoryDivergence(null, repo.Head.Tip)); } } + + [Fact] + public void CanShortenObjectIdentifier() + { + /* + * $ echo "aabqhq" | git hash-object -t blob --stdin + * dea509d0b3cb8ee0650f6ca210bc83f4678851ba + * + * $ echo "aaazvc" | git hash-object -t blob --stdin + * dea509d097ce692e167dfc6a48a7a280cc5e877e + */ + + string path = CloneBareTestRepo(); + using (var repo = new Repository(path)) + { + repo.Config.Set("core.abbrev", 4); + + Blob blob1 = CreateBlob(repo, "aabqhq\n"); + Assert.Equal("dea509d0b3cb8ee0650f6ca210bc83f4678851ba", blob1.Sha); + + Assert.Equal("dea5", repo.ObjectDatabase.ShortenObjectId(blob1)); + Assert.Equal("dea509d0b3cb", repo.ObjectDatabase.ShortenObjectId(blob1, 12)); + Assert.Equal("dea509d0b3cb8ee0650f6ca210bc83f4678851b", repo.ObjectDatabase.ShortenObjectId(blob1, 39)); + + Blob blob2 = CreateBlob(repo, "aaazvc\n"); + Assert.Equal("dea509d09", repo.ObjectDatabase.ShortenObjectId(blob2)); + Assert.Equal("dea509d09", repo.ObjectDatabase.ShortenObjectId(blob2, 4)); + Assert.Equal("dea509d0b", repo.ObjectDatabase.ShortenObjectId(blob1)); + Assert.Equal("dea509d0b", repo.ObjectDatabase.ShortenObjectId(blob1, 7)); + + Assert.Equal("dea509d0b3cb", repo.ObjectDatabase.ShortenObjectId(blob1, 12)); + Assert.Equal("dea509d097ce", repo.ObjectDatabase.ShortenObjectId(blob2, 12)); + } + } + + private static Blob CreateBlob(Repository repo, string content) + { + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) + { + return repo.ObjectDatabase.CreateBlob(stream); + } + } } } |