Skip to content

Commit

Permalink
Add Archive() to ObjectDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
yorah committed Dec 13, 2013
1 parent 27cd2dc commit 2eb72b5
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
100 changes: 100 additions & 0 deletions LibGit2Sharp.Tests/ArchiveFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections;
using System.IO;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
public class ArchiveFixture : BaseFixture
{
[Fact]
public void CanArchiveATree()
{
using (var repo = new Repository(BareTestRepoPath))
{
var tree = repo.Lookup<Tree>("581f9824ecaf824221bd36edf5430f2739a7c4f5");

var archiver = new MockArchiver();

repo.ObjectDatabase.Archive(tree, archiver);

var expected = new ArrayList
{
new { Path = "1", Sha = "7f76480d939dc401415927ea7ef25c676b8ddb8f" },
new { Path = Path.Combine("1", "branch_file.txt"), Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" },
new { Path = "README", Sha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6" },
new { Path = "branch_file.txt", Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" },
new { Path = "new.txt", Sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" },
};
Assert.Equal(expected, archiver.Files);
Assert.Null(archiver.ReceivedCommitSha);
Assert.InRange(archiver.ModificationTime, DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMilliseconds(100)), DateTimeOffset.UtcNow);
}
}

[Fact]
public void CanArchiveACommit()
{
using (var repo = new Repository(BareTestRepoPath))
{
var commit = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345");

var archiver = new MockArchiver();

repo.ObjectDatabase.Archive(commit, archiver);

var expected = new ArrayList
{
new { Path = "1", Sha = "7f76480d939dc401415927ea7ef25c676b8ddb8f" },
new { Path = Path.Combine("1", "branch_file.txt"), Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" },
new { Path = "README", Sha = "a8233120f6ad708f843d861ce2b7228ec4e3dec6" },
new { Path = "branch_file.txt", Sha = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" },
new { Path = "new.txt", Sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" },
};
Assert.Equal(expected, archiver.Files);
Assert.Equal(commit.Sha, archiver.ReceivedCommitSha);
Assert.Equal(commit.Committer.When, archiver.ModificationTime);
}
}

[Fact]
public void ArchivingANullTreeOrCommitThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Commit)null, null));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Tree)null, null));
}
}

#region MockArchiver

private class MockArchiver : ArchiverBase
{
public readonly ArrayList Files = new ArrayList();
public string ReceivedCommitSha;
public DateTimeOffset ModificationTime;

#region Overrides of ArchiverBase

public override void BeforeArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime)
{
if (oid != null)
{
ReceivedCommitSha = oid.Sha;
}
ModificationTime = modificationTime;
}

protected override void AddTreeEntry(string path, TreeEntry entry, DateTimeOffset modificationTime)
{
Files.Add(new { Path = path, entry.Target.Sha });
}

#endregion
}

#endregion
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="SubmoduleFixture.cs" />
<Compile Include="IgnoreFixture.cs" />
<Compile Include="FetchHeadFixture.cs" />
<Compile Include="ArchiveFixture.cs" />
<Compile Include="MergeFixture.cs" />
<Compile Include="CleanFixture.cs" />
<Compile Include="CurrentOperationFixture.cs" />
Expand Down
65 changes: 65 additions & 0 deletions LibGit2Sharp/ArchiverBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace LibGit2Sharp
{
/// <summary>
/// The archiving method needs to be passed an inheritor of this class, which will then be used
/// to provide low-level archiving facilities (tar, zip, ...).
/// <para>
/// <see cref="ObjectDatabase.Archive(LibGit2Sharp.Commit,LibGit2Sharp.ArchiverBase)"/>
/// </para>
/// </summary>
public abstract class ArchiverBase
{
/// <summary>
/// Override this method to perform operations before the archiving of each entry of the tree takes place.
/// </summary>
/// <param name="tree">The tree that will be archived</param>
/// <param name="oid">The ObjectId of the commit being archived, or null if there is no commit.</param>
/// <param name="modificationTime">The modification time that will be used for the files in the archive.</param>
public virtual void BeforeArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime)
{ }

/// <summary>
/// Override this method to perform operations after the archiving of each entry of the tree took place.
/// </summary>
/// <param name="tree">The tree that was archived</param>
/// <param name="oid">The ObjectId of the commit being archived, or null if there is no commit.</param>
/// <param name="modificationTime">The modification time that was used for the files in the archive.</param>
public virtual void AfterArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime)
{ }

internal void OrchestrateArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime)
{
BeforeArchiving(tree, oid, modificationTime);

ArchiveTree(tree, "", modificationTime);

AfterArchiving(tree, oid, modificationTime);
}

private void ArchiveTree(IEnumerable<TreeEntry> tree, string path, DateTimeOffset modificationTime)
{
foreach (var entry in tree)
{
AddTreeEntry(Path.Combine(path, entry.Name), entry, modificationTime);

// Recurse if we have subtrees
if (entry.Mode == Mode.Directory)
{
ArchiveTree((Tree)entry.Target, Path.Combine(path, entry.Name), modificationTime);
}
}
}

/// <summary>
/// Implements the archiving of a TreeEntry in a given format.
/// </summary>
/// <param name="path">The path of the entry in the archive.</param>
/// <param name="entry">The entry to archive.</param>
/// <param name="modificationTime">The datetime the entry was last modified.</param>
protected abstract void AddTreeEntry(string path, TreeEntry entry, DateTimeOffset modificationTime);
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AmbiguousSpecificationException.cs" />
<Compile Include="ArchiverBase.cs" />
<Compile Include="BareRepositoryException.cs" />
<Compile Include="BlameHunkCollection.cs" />
<Compile Include="BlameHunk.cs" />
Expand Down
27 changes: 27 additions & 0 deletions LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using LibGit2Sharp.Handlers;

namespace LibGit2Sharp
{
Expand Down Expand Up @@ -236,6 +237,32 @@ public virtual TagAnnotation CreateTagAnnotation(string name, GitObject target,
return repo.Lookup<TagAnnotation>(tagId);
}

/// <summary>
/// Archive the given commit.
/// </summary>
/// <param name="commit">The commit.</param>
/// <param name="archiver">The archiver to use.</param>
public virtual void Archive(Commit commit, ArchiverBase archiver)
{
Ensure.ArgumentNotNull(commit, "commit");
Ensure.ArgumentNotNull(archiver, "archiver");

archiver.OrchestrateArchiving(commit.Tree, commit.Id, commit.Committer.When);
}

/// <summary>
/// Archive the given tree.
/// </summary>
/// <param name="tree">The tree.</param>
/// <param name="archiver">The archiver to use.</param>
public virtual void Archive(Tree tree, ArchiverBase archiver)
{
Ensure.ArgumentNotNull(tree, "tree");
Ensure.ArgumentNotNull(archiver, "archiver");

archiver.OrchestrateArchiving(tree, null, DateTimeOffset.UtcNow);
}

/// <summary>
/// Returns the merge base (best common ancestor) of the given commits
/// and the distance between each of these commits and this base.
Expand Down

0 comments on commit 2eb72b5

Please sign in to comment.