Skip to content

Commit 3403508

Browse files
committed
Indexer: use static methods
Since you cannot re-use the same Indexer object to index two different files, it doesn't make much sense to provide a class which would allow you to do this. Instead expose two static methods which internally create an Indexer instance and feed it from the stream or file.
1 parent df12e68 commit 3403508

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

LibGit2Sharp.Tests/IndexerFixture.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ public void CanIndexFromAFilePath()
1414
TransferProgress copiedProgress = default(TransferProgress);
1515
using (var repo = new Repository(SandboxStandardTestRepoGitDir()))
1616
{
17+
var expectedId = new ObjectId("a81e489679b7d3418f9ab594bda8ceb37dd4c695");
1718
var path = Path.Combine(repo.Info.Path, "objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.pack");
18-
using (var indexer = new Indexer(repo.Info.WorkingDirectory, 438 /* 0666 */,
19+
TransferProgress progress;
20+
var packId = Indexer.Index(out progress, path, repo.Info.WorkingDirectory, 438 /* 0666 */,
1921
onProgress: (p) => {
2022
copiedProgress = p;
2123
return true;
22-
}))
23-
{
24-
indexer.Index(path);
25-
Assert.Equal(1628, indexer.Progress.TotalObjects);
26-
Assert.Equal(indexer.Progress.TotalObjects, copiedProgress.TotalObjects);
27-
}
24+
});
25+
26+
Assert.Equal(expectedId, packId);
27+
Assert.Equal(1628, progress.TotalObjects);
28+
Assert.Equal(1628, copiedProgress.TotalObjects);
2829
}
2930
}
3031

@@ -33,12 +34,14 @@ public void CanIndexFromAStream()
3334
{
3435
using (var repo = new Repository(SandboxStandardTestRepoGitDir()))
3536
{
37+
var expectedId = new ObjectId("a81e489679b7d3418f9ab594bda8ceb37dd4c695");
3638
var path = Path.Combine(repo.Info.Path, "objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.pack");
37-
using (var indexer = new Indexer(repo.Info.WorkingDirectory, 438 /* 0666 */))
39+
TransferProgress progress;
3840
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
3941
{
40-
indexer.Index(stream);
41-
Assert.Equal(1628, indexer.Progress.TotalObjects);
42+
var packId = Indexer.Index(out progress, stream, repo.Info.WorkingDirectory, 438 /* 0666 */);
43+
Assert.Equal(expectedId, packId);
44+
Assert.Equal(1628, progress.TotalObjects);
4245
}
4346
}
4447
}

LibGit2Sharp/Advanced/Indexer.cs

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,76 @@
66

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

1216
readonly IndexerSafeHandle handle;
1317
readonly TransferProgressHandler callback;
1418

15-
GitTransferProgress progress;
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-
{
24-
get
25-
{
26-
return new TransferProgress(progress);
27-
}
28-
}
29-
30-
public Indexer(string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
19+
Indexer(string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
3120
{
3221
/* The runtime won't let us pass null as a SafeHandle, wo create a "dummy" one to represent NULL */
3322
ObjectDatabaseSafeHandle odbHandle = odb != null ? odb.Handle : new ObjectDatabaseSafeHandle();
3423
callback = onProgress;
3524
handle = Proxy.git_indexer_new(prefix, odbHandle, mode, GitDownloadTransferProgressHandler);
36-
progress = new GitTransferProgress();
3725
}
3826

3927
/// <summary>
40-
/// Index the packfile at the specified path. This function runs synchronously and should usually be run
28+
/// Index the specified stream. This function runs synchronously; you may want to run it
4129
/// in a background thread.
4230
/// </summary>
43-
/// <param name="path">The packfile's path</param>
44-
public ObjectId Index(string path)
31+
/// <param name="progress">The amount of objects processed etc will be written to this structure on exit</param>
32+
/// <param name="stream">Stream to run the indexing process on</param>
33+
/// <param name="prefix">Path in which to store the pack and index files</param>
34+
/// <param name="mode">Filemode to use for creating the pack and index files</param>
35+
/// <param name="odb">Optional object db to use if the pack contains thin deltas</param>
36+
/// <param name="onProgress">Function to call to report progress. It returns a boolean indicating whether
37+
/// to continue working on the stream</param>
38+
public static ObjectId Index(out TransferProgress progress, Stream stream, string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
4539
{
46-
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
40+
var buffer = new byte[65536];
41+
int read;
42+
var indexProgress = default(GitTransferProgress);
43+
44+
using (var idx = new Indexer(prefix, mode, odb, onProgress))
4745
{
48-
return Index(fs);
46+
var handle = idx.handle;
47+
48+
do
49+
{
50+
read = stream.Read(buffer, 0, buffer.Length);
51+
Proxy.git_indexer_append(handle, buffer, (UIntPtr)read, ref indexProgress);
52+
} while (read > 0);
53+
54+
Proxy.git_indexer_commit(handle, ref indexProgress);
55+
56+
progress = new TransferProgress(indexProgress);
57+
return Proxy.git_indexer_hash(handle);
4958
}
5059
}
5160

5261
/// <summary>
53-
/// Index the packfile from the specified stream. This function runs synchronously and should usually be run
62+
/// Index the packfile at the specified path. This function runs synchronously; you may want to run it
5463
/// in a background thread.
64+
/// <param name="progress">The amount of objects processed etc will be written to this structure on exit</param>
65+
/// <param name="path">Path to the file to index</param>
66+
/// <param name="prefix">Path in which to store the pack and index files</param>
67+
/// <param name="mode">Filemode to use for creating the pack and index files</param>
68+
/// <param name="odb">Optional object db to use if the pack contains thin deltas</param>
69+
/// <param name="onProgress">Function to call to report progress. It returns a boolean indicating whether
70+
/// to continue working on the stream</param>
71+
5572
/// </summary>
56-
/// <param name="stream">The stream from which to read the packfile data</param>
57-
public ObjectId Index(Stream stream)
73+
public static ObjectId Index(out TransferProgress progress, string path, string prefix, uint mode, ObjectDatabase odb = null, TransferProgressHandler onProgress = null)
5874
{
59-
buffer = new byte[65536];
60-
int read;
61-
62-
do
75+
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
6376
{
64-
read = stream.Read(buffer, 0, buffer.Length);
65-
Proxy.git_indexer_append(handle, buffer, (UIntPtr)read, ref progress);
66-
} while (read > 0);
67-
68-
Proxy.git_indexer_commit(handle, ref progress);
69-
70-
return Proxy.git_indexer_hash(handle);
77+
return Index(out progress, fs, prefix, mode, odb, onProgress);
78+
}
7179
}
7280

7381
// This comes from RemoteCallbacks

0 commit comments

Comments
 (0)