Skip to content

Add the streaming indexer to the bindings #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions LibGit2Sharp.Tests/IndexerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;
using LibGit2Sharp.Advanced;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
public class IndexerFixture : BaseFixture
{
[Fact]
public void CanIndexFromAFilePath()
{
TransferProgress copiedProgress = default(TransferProgress);
using (var repo = new Repository(SandboxStandardTestRepoGitDir()))
{
var expectedId = new ObjectId("a81e489679b7d3418f9ab594bda8ceb37dd4c695");
var path = Path.Combine(repo.Info.Path, "objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.pack");
TransferProgress progress;
var packId = Indexer.Index(out progress, path, repo.Info.WorkingDirectory, 438 /* 0666 */,
onProgress: (p) => {
copiedProgress = p;
return true;
});

Assert.Equal(expectedId, packId);
Assert.Equal(1628, progress.TotalObjects);
Assert.Equal(1628, copiedProgress.TotalObjects);
}
}

[Fact]
public void CanIndexFromAStream()
{
using (var repo = new Repository(SandboxStandardTestRepoGitDir()))
{
var expectedId = new ObjectId("a81e489679b7d3418f9ab594bda8ceb37dd4c695");
var path = Path.Combine(repo.Info.Path, "objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.pack");
TransferProgress progress;
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var packId = Indexer.Index(out progress, stream, repo.Info.WorkingDirectory, 438 /* 0666 */);
Assert.Equal(expectedId, packId);
Assert.Equal(1628, progress.TotalObjects);
}
}
}

}
}

1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<Compile Include="TreeDefinitionFixture.cs" />
<Compile Include="TreeFixture.cs" />
<Compile Include="UnstageFixture.cs" />
<Compile Include="IndexerFixture.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj">
Expand Down
109 changes: 109 additions & 0 deletions LibGit2Sharp/Advanced/Indexer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp.Advanced
{
/// <summary>
/// The Indexer is our implementation of the git-index-pack command. It is used to process the packfile
/// which comes from the remote side on a fetch in order to create the corresponding .idx file.
/// </summary>
public class Indexer : IDisposable
{

readonly IndexerSafeHandle handle;
readonly TransferProgressHandler callback;

Indexer(string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
{
/* The runtime won't let us pass null as a SafeHandle, wo create a "dummy" one to represent NULL */
ObjectDatabaseSafeHandle odbHandle = odb != null ? odb.Handle : new ObjectDatabaseSafeHandle();
callback = onProgress;
handle = Proxy.git_indexer_new(prefix, odbHandle, mode, GitDownloadTransferProgressHandler);
}

/// <summary>
/// Index the specified stream. This function runs synchronously; you may want to run it
/// in a background thread.
/// </summary>
/// <param name="progress">The amount of objects processed etc will be written to this structure on exit</param>
/// <param name="stream">Stream to run the indexing process on</param>
/// <param name="prefix">Path in which to store the pack and index files</param>
/// <param name="mode">Filemode to use for creating the pack and index files</param>
/// <param name="odb">Optional object db to use if the pack contains thin deltas</param>
/// <param name="onProgress">Function to call to report progress. It returns a boolean indicating whether
/// to continue working on the stream</param>
public static ObjectId Index(out TransferProgress progress, Stream stream, string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have proper overloads here instead of optional params.

{
var buffer = new byte[65536];
int read;
var indexProgress = default(GitTransferProgress);

using (var idx = new Indexer(prefix, mode, odb, onProgress))
{
var handle = idx.handle;

do
{
read = stream.Read(buffer, 0, buffer.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream.Read can return negative here on failure - we should break if read <= 0.

Proxy.git_indexer_append(handle, buffer, (UIntPtr)read, ref indexProgress);
} while (read > 0);

Proxy.git_indexer_commit(handle, ref indexProgress);

progress = new TransferProgress(indexProgress);
return Proxy.git_indexer_hash(handle);
}
}

/// <summary>
/// Index the packfile at the specified path. This function runs synchronously; you may want to run it
/// in a background thread.
/// <param name="progress">The amount of objects processed etc will be written to this structure on exit</param>
/// <param name="path">Path to the file to index</param>
/// <param name="prefix">Path in which to store the pack and index files</param>
/// <param name="mode">Filemode to use for creating the pack and index files</param>
/// <param name="odb">Optional object db to use if the pack contains thin deltas</param>
/// <param name="onProgress">Function to call to report progress. It returns a boolean indicating whether
/// to continue working on the stream</param>

/// </summary>
public static ObjectId Index(out TransferProgress progress, string path, string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have proper overloads here instead of optional params.

{
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return Index(out progress, fs, prefix, mode, odb, onProgress);
}
}

// This comes from RemoteCallbacks
/// <summary>
/// The delegate with the signature that matches the native git_transfer_progress_callback function's signature.
/// </summary>
/// <param name="progress"><see cref="GitTransferProgress"/> structure containing progress information.</param>
/// <param name="payload">Payload data.</param>
/// <returns>the result of the wrapped <see cref="TransferProgressHandler"/></returns>
int GitDownloadTransferProgressHandler(ref GitTransferProgress progress, IntPtr payload)
{
bool shouldContinue = true;

if (callback != null)
{
shouldContinue = callback(new TransferProgress(progress));
}

return Proxy.ConvertResultToCancelFlag(shouldContinue);
}

#region IDisposable implementation

public void Dispose()
{
handle.SafeDispose();
}

#endregion
}
}
13 changes: 13 additions & 0 deletions LibGit2Sharp/Core/Handles/IndexerSafeHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
namespace LibGit2Sharp.Core.Handles
{
internal class IndexerSafeHandle : SafeHandleBase
{
protected override bool ReleaseHandleImpl()
{
Proxy.git_indexer_free(handle);
return true;
}
}
}

21 changes: 21 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,27 @@ internal static extern int git_treebuilder_insert(

[DllImport(libgit2)]
internal static extern int git_cherrypick(RepositorySafeHandle repo, GitObjectSafeHandle commit, GitCherryPickOptions options);

[DllImport(libgit2)]
public static extern int git_indexer_new(
out IndexerSafeHandle handle,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath prefix,
uint mode,
ObjectDatabaseSafeHandle odbHandle,
git_transfer_progress_callback progress_callback,
IntPtr payload);

[DllImport(libgit2)]
public static extern int git_indexer_append(IndexerSafeHandle idx, byte[] data, UIntPtr size, ref GitTransferProgress progress);

[DllImport(libgit2)]
public static extern int git_indexer_commit(IndexerSafeHandle idx, ref GitTransferProgress progress);

[DllImport(libgit2)]
public static extern OidSafeHandle git_indexer_hash(IndexerSafeHandle idx);

[DllImport(libgit2)]
public static extern void git_indexer_free(IntPtr idx);
}
}
// ReSharper restore InconsistentNaming
46 changes: 46 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,52 @@ public static void git_index_clear(Index index)

#endregion

#region git_indexer_

public static IndexerSafeHandle git_indexer_new(FilePath prefix, ObjectDatabaseSafeHandle odbHandle, uint mode,
NativeMethods.git_transfer_progress_callback progressCallback)
{
using (ThreadAffinity())
{
IndexerSafeHandle handle;

int res = NativeMethods.git_indexer_new(out handle, prefix, mode, odbHandle, progressCallback, IntPtr.Zero);
Ensure.ZeroResult(res);

return handle;
}
}

public static void git_indexer_append(IndexerSafeHandle handle, byte[] data, UIntPtr length, ref GitTransferProgress progress)
{
using (ThreadAffinity())
{
int res = NativeMethods.git_indexer_append(handle, data, length, ref progress);
Ensure.ZeroResult(res);
}
}

public static void git_indexer_commit(IndexerSafeHandle handle, ref GitTransferProgress progress)
{
using (ThreadAffinity())
{
int res = NativeMethods.git_indexer_commit(handle, ref progress);
Ensure.ZeroResult(res);
}
}

public static ObjectId git_indexer_hash(IndexerSafeHandle handle)
{
return NativeMethods.git_indexer_hash(handle).MarshalAsObjectId();
}

public static void git_indexer_free(IntPtr handle)
{
NativeMethods.git_indexer_free(handle);
}

#endregion

#region git_merge_

public static IndexSafeHandle git_merge_trees(RepositorySafeHandle repo, GitObjectSafeHandle ancestorTree, GitObjectSafeHandle ourTree, GitObjectSafeHandle theirTree)
Expand Down
2 changes: 2 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@
<Compile Include="Core\RawContentStream.cs" />
<Compile Include="Core\Handles\OdbStreamSafeHandle.cs" />
<Compile Include="SupportedCredentialTypes.cs" />
<Compile Include="Core\Handles\IndexerSafeHandle.cs" />
<Compile Include="Advanced\Indexer.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
Expand Down
5 changes: 5 additions & 0 deletions LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ IEnumerator IEnumerable.GetEnumerator()

#endregion

internal ObjectDatabaseSafeHandle Handle
{
get { return handle; }
}

/// <summary>
/// Determines if the given object can be found in the object database.
/// </summary>
Expand Down