-
Notifications
You must be signed in to change notification settings - Fork 899
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule libgit2
updated
15 files
+0 −13 | include/git2/pack.h | |
+0 −2 | src/describe.c | |
+0 −2 | src/indexer.c | |
+1 −244 | src/pack-objects.c | |
+0 −10 | src/pack-objects.h | |
+0 −3 | src/pack.c | |
+3 −1 | src/pack.h | |
+0 −2 | src/revwalk.c | |
+1 −1 | src/revwalk.h | |
+0 −5 | src/stream.h | |
+2 −2 | src/submodule.c | |
+1 −2 | src/transports/http.c | |
+32 −10 | src/transports/local.c | |
+0 −7 | tests/online/clone.c | |
+3 −3 | tests/submodule/modify.c |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.