|
6 | 6 |
|
7 | 7 | namespace LibGit2Sharp.Advanced
|
8 | 8 | {
|
| 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> |
9 | 13 | public class Indexer : IDisposable
|
10 | 14 | {
|
11 | 15 |
|
12 | 16 | readonly IndexerSafeHandle handle;
|
13 | 17 | readonly TransferProgressHandler callback;
|
14 | 18 |
|
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) |
31 | 20 | {
|
32 | 21 | /* The runtime won't let us pass null as a SafeHandle, wo create a "dummy" one to represent NULL */
|
33 | 22 | ObjectDatabaseSafeHandle odbHandle = odb != null ? odb.Handle : new ObjectDatabaseSafeHandle();
|
34 | 23 | callback = onProgress;
|
35 | 24 | handle = Proxy.git_indexer_new(prefix, odbHandle, mode, GitDownloadTransferProgressHandler);
|
36 |
| - progress = new GitTransferProgress(); |
37 | 25 | }
|
38 | 26 |
|
39 | 27 | /// <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 |
41 | 29 | /// in a background thread.
|
42 | 30 | /// </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) |
45 | 39 | {
|
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)) |
47 | 45 | {
|
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); |
49 | 58 | }
|
50 | 59 | }
|
51 | 60 |
|
52 | 61 | /// <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 |
54 | 63 | /// 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 | + |
55 | 72 | /// </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) |
58 | 74 | {
|
59 |
| - buffer = new byte[65536]; |
60 |
| - int read; |
61 |
| - |
62 |
| - do |
| 75 | + using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) |
63 | 76 | {
|
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 | + } |
71 | 79 | }
|
72 | 80 |
|
73 | 81 | // This comes from RemoteCallbacks
|
|
0 commit comments