Skip to content

Commit 198fee5

Browse files
committed
Indexer: add bindings for the pack indexer
This allows the user to index a packfile they have in a file or are retrieving through a Stream.
1 parent d677741 commit 198fee5

File tree

7 files changed

+182
-1
lines changed

7 files changed

+182
-1
lines changed

LibGit2Sharp.Tests/IndexerFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using LibGit2Sharp.Advanced;
4+
using LibGit2Sharp.Tests.TestHelpers;
5+
using Xunit;
6+
7+
namespace LibGit2Sharp.Tests
8+
{
9+
public class IndexerFixture : BaseFixture
10+
{
11+
[Fact]
12+
public void CanIndexFromAFilePath()
13+
{
14+
var repo = new Repository(CloneStandardTestRepo());
15+
var path = repo.Info.Path + "/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.pack";
16+
var indexer = new Indexer(repo.Info.Path);
17+
indexer.Index(path);
18+
Assert.Equal(1628, indexer.Progress.TotalObjects);
19+
}
20+
21+
[Fact]
22+
public void CanIndexFromAStream()
23+
{
24+
var repo = new Repository(CloneStandardTestRepo());
25+
var path = repo.Info.Path + "/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.pack";
26+
var indexer = new Indexer(repo.Info.Path);
27+
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
28+
indexer.Index(stream);
29+
Assert.Equal(1628, indexer.Progress.TotalObjects);
30+
}
31+
32+
}
33+
}
34+

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Compile Include="TreeFixture.cs" />
123123
<Compile Include="TupleFixture.cs" />
124124
<Compile Include="UnstageFixture.cs" />
125+
<Compile Include="IndexerFixture.cs" />
125126
</ItemGroup>
126127
<ItemGroup>
127128
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj">

LibGit2Sharp/Advanced/Indexer.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using System.Collections.Generic;
5+
using LibGit2Sharp.Core;
6+
using LibGit2Sharp.Core.Compat;
7+
using LibGit2Sharp.Core.Handles;
8+
9+
namespace LibGit2Sharp.Advanced
10+
{
11+
public class Indexer
12+
{
13+
14+
GitTransferProgress progress;
15+
readonly IndexerSafeHandle handle;
16+
byte[] buffer;
17+
18+
/// <summary>
19+
/// The indexing progress
20+
/// </summary>
21+
/// <value>The progres information for the current operation.</value>
22+
public TransferProgress Progress {
23+
get {
24+
return new TransferProgress(progress);
25+
}
26+
}
27+
28+
public Indexer(string dir)
29+
{
30+
handle = Proxy.git_indexer_new(dir);
31+
progress = new GitTransferProgress();
32+
}
33+
34+
/// <summary>
35+
/// Index the packfile at the specified path. This function runs synchronously and should usually be run
36+
/// in a background thread.
37+
/// </summary>
38+
/// <param name="path">The packfile's path</param>
39+
public ObjectId Index(string path)
40+
{
41+
var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
42+
return Index(fs);
43+
}
44+
45+
/// <summary>
46+
/// Index the packfile from the specified stream. This function runs synchronously and should usually be run
47+
/// in a background thread.
48+
/// </summary>
49+
/// <param name="stream">The stream from which to read the packfile data</param>
50+
public ObjectId Index(Stream stream)
51+
{
52+
buffer = new byte[65536];
53+
int read;
54+
55+
do {
56+
read = stream.Read(buffer, 0, buffer.Length);
57+
Proxy.git_indexer_append(handle, buffer, (UIntPtr)read, ref progress);
58+
} while (read > 0);
59+
60+
Proxy.git_indexer_commit(handle, ref progress);
61+
62+
return Proxy.git_indexer_hash(handle);
63+
}
64+
}
65+
}
66+
67+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
namespace LibGit2Sharp.Core.Handles
3+
{
4+
internal class IndexerSafeHandle : SafeHandleBase
5+
{
6+
protected override bool ReleaseHandleImpl()
7+
{
8+
Proxy.git_indexer_free(handle);
9+
return true;
10+
}
11+
}
12+
}
13+

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,23 @@ internal static extern int git_treebuilder_insert(
13271327

13281328
[DllImport(libgit2)]
13291329
internal static extern int git_blob_is_binary(GitObjectSafeHandle blob);
1330+
1331+
[DllImport(libgit2)]
1332+
public static extern int git_indexer_new(
1333+
out IndexerSafeHandle idx,
1334+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath dir);
1335+
1336+
[DllImport(libgit2)]
1337+
public static extern int git_indexer_append(IndexerSafeHandle idx, byte[] data, UIntPtr size, ref GitTransferProgress progress);
1338+
1339+
[DllImport(libgit2)]
1340+
public static extern int git_indexer_commit(IndexerSafeHandle idx, ref GitTransferProgress progress);
1341+
1342+
[DllImport(libgit2)]
1343+
public static extern OidSafeHandle git_indexer_hash(IndexerSafeHandle idx);
1344+
1345+
[DllImport(libgit2)]
1346+
public static extern void git_indexer_free(IntPtr idx);
13301347
}
13311348
}
13321349
// ReSharper restore InconsistentNaming

LibGit2Sharp/Core/Proxy.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,51 @@ public static ObjectId git_tree_create_fromindex(Index index)
921921

922922
#endregion
923923

924+
#region git_indexer_
925+
926+
public static IndexerSafeHandle git_indexer_new(FilePath dir)
927+
{
928+
using (ThreadAffinity())
929+
{
930+
IndexerSafeHandle handle;
931+
932+
int res = NativeMethods.git_indexer_new(out handle, dir);
933+
Ensure.ZeroResult(res);
934+
935+
return handle;
936+
}
937+
}
938+
939+
public static void git_indexer_append(IndexerSafeHandle handle, byte[] data, UIntPtr length, ref GitTransferProgress progress)
940+
{
941+
using (ThreadAffinity())
942+
{
943+
int res = NativeMethods.git_indexer_append(handle, data, length, ref progress);
944+
Ensure.ZeroResult(res);
945+
}
946+
}
947+
948+
public static void git_indexer_commit(IndexerSafeHandle handle, ref GitTransferProgress progress)
949+
{
950+
using (ThreadAffinity())
951+
{
952+
int res = NativeMethods.git_indexer_commit(handle, ref progress);
953+
Ensure.ZeroResult(res);
954+
}
955+
}
956+
957+
public static ObjectId git_indexer_hash(IndexerSafeHandle handle)
958+
{
959+
return NativeMethods.git_indexer_hash(handle).MarshalAsObjectId();
960+
}
961+
962+
public static void git_indexer_free(IntPtr handle)
963+
{
964+
NativeMethods.git_indexer_free(handle);
965+
}
966+
967+
#endregion
968+
924969
#region git_merge_
925970

926971
public static ObjectId git_merge_base(RepositorySafeHandle repo, Commit first, Commit second)

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<DefineConstants>TRACE;DEBUG;NET35</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25-
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
2625
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2726
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
2827
<DocumentationFile>bin\Debug\LibGit2Sharp.xml</DocumentationFile>
@@ -288,6 +287,8 @@
288287
<Compile Include="UserCanceledException.cs" />
289288
<Compile Include="VoidReference.cs" />
290289
<Compile Include="Core\RawContentStream.cs" />
290+
<Compile Include="Advanced\Indexer.cs" />
291+
<Compile Include="Core\Handles\IndexerSafeHandle.cs" />
291292
</ItemGroup>
292293
<ItemGroup>
293294
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
@@ -312,4 +313,7 @@
312313
</CreateItem>
313314
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
314315
</Target>
316+
<ItemGroup>
317+
<Folder Include="Advanced\" />
318+
</ItemGroup>
315319
</Project>

0 commit comments

Comments
 (0)