-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathPackBuilder.cs
203 lines (178 loc) · 6.95 KB
/
PackBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// Representation of a git PackBuilder.
/// </summary>
public sealed class PackBuilder : IDisposable
{
private readonly PackBuilderHandle packBuilderHandle;
/// <summary>
/// Constructs a PackBuilder for a <see cref="Repository"/>.
/// </summary>
internal PackBuilder(Repository repository)
{
Ensure.ArgumentNotNull(repository, "repository");
packBuilderHandle = Proxy.git_packbuilder_new(repository.Handle);
}
/// <summary>
/// Inserts a single <see cref="GitObject"/> to the PackBuilder.
/// For an optimal pack it's mandatory to insert objects in recency order, commits followed by trees and blobs. (quoted from libgit2 API ref)
/// </summary>
/// <param name="gitObject">The object to be inserted.</param>
/// <exception cref="ArgumentNullException">if the gitObject is null</exception>
public void Add<T>(T gitObject) where T : GitObject
{
Ensure.ArgumentNotNull(gitObject, "gitObject");
Add(gitObject.Id);
}
/// <summary>
/// Recursively inserts a <see cref="GitObject"/> and its referenced objects.
/// Inserts the object as well as any object it references.
/// </summary>
/// <param name="gitObject">The object to be inserted recursively.</param>
/// <exception cref="ArgumentNullException">if the gitObject is null</exception>
public void AddRecursively<T>(T gitObject) where T : GitObject
{
Ensure.ArgumentNotNull(gitObject, "gitObject");
AddRecursively(gitObject.Id);
}
/// <summary>
/// Inserts a single object to the PackBuilder by its <see cref="ObjectId"/>.
/// For an optimal pack it's mandatory to insert objects in recency order, commits followed by trees and blobs. (quoted from libgit2 API ref)
/// </summary>
/// <param name="id">The object ID to be inserted.</param>
/// <exception cref="ArgumentNullException">if the id is null</exception>
public void Add(ObjectId id)
{
Ensure.ArgumentNotNull(id, "id");
Proxy.git_packbuilder_insert(packBuilderHandle, id, null);
}
/// <summary>
/// Recursively inserts an object and its referenced objects by its <see cref="ObjectId"/>.
/// Inserts the object as well as any object it references.
/// </summary>
/// <param name="id">The object ID to be recursively inserted.</param>
/// <exception cref="ArgumentNullException">if the id is null</exception>
public void AddRecursively(ObjectId id)
{
Ensure.ArgumentNotNull(id, "id");
Proxy.git_packbuilder_insert_recur(packBuilderHandle, id, null);
}
/// <summary>
/// Disposes the PackBuilder object.
/// </summary>
void IDisposable.Dispose()
{
packBuilderHandle.SafeDispose();
}
/// <summary>
/// Writes the pack file and corresponding index file to path.
/// </summary>
/// <param name="path">The path that pack and index files will be written to it.</param>
internal void Write(string path)
{
Proxy.git_packbuilder_write(packBuilderHandle, path);
}
/// <summary>
/// Sets number of threads to spawn.
/// </summary>
/// <returns> Returns the number of actual threads to be used.</returns>
/// <param name="nThread">The Number of threads to spawn. An argument of 0 ensures using all available CPUs</param>
internal int SetMaximumNumberOfThreads(int nThread)
{
// Libgit2 set the number of threads to 1 by default, 0 ensures git_online_cpus
return (int)Proxy.git_packbuilder_set_threads(packBuilderHandle, (uint)nThread);
}
/// <summary>
/// Number of objects the PackBuilder will write out.
/// </summary>
internal long ObjectsCount
{
get { return (long)Proxy.git_packbuilder_object_count(packBuilderHandle); }
}
/// <summary>
/// Number of objects the PackBuilder has already written out.
/// This is only correct after the pack file has been written.
/// </summary>
internal long WrittenObjectsCount
{
get { return (long)Proxy.git_packbuilder_written(packBuilderHandle); }
}
internal PackBuilderHandle Handle
{
get { return packBuilderHandle; }
}
}
/// <summary>
/// The results of pack process of the <see cref="ObjectDatabase"/>.
/// </summary>
public struct PackBuilderResults
{
/// <summary>
/// Number of objects the PackBuilder has already written out.
/// </summary>
public long WrittenObjectsCount { get; internal set; }
}
/// <summary>
/// Packing options of the <see cref="ObjectDatabase"/>.
/// </summary>
public sealed class PackBuilderOptions
{
private string path;
private int nThreads;
/// <summary>
/// Constructor
/// </summary>
/// <param name="packDirectory">Directory path to write the pack and index files to it</param>
/// The default value for maximum number of threads to spawn is 0 which ensures using all available CPUs.
/// <exception cref="ArgumentNullException">if packDirectory is null or empty</exception>
/// <exception cref="DirectoryNotFoundException">if packDirectory doesn't exist</exception>
public PackBuilderOptions(string packDirectory)
{
PackDirectoryPath = packDirectory;
MaximumNumberOfThreads = 0;
}
/// <summary>
/// Directory path to write the pack and index files to it.
/// </summary>
public string PackDirectoryPath
{
set
{
Ensure.ArgumentNotNullOrEmptyString(value, "packDirectory");
if (!Directory.Exists(value))
{
throw new DirectoryNotFoundException("The Directory " + value + " does not exist.");
}
path = value;
}
get
{
return path;
}
}
/// <summary>
/// Maximum number of threads to spawn.
/// The default value is 0 which ensures using all available CPUs.
/// </summary>
public int MaximumNumberOfThreads
{
set
{
if (value < 0)
{
throw new ArgumentException("Argument can not be negative", nameof(value));
}
nThreads = value;
}
get
{
return nThreads;
}
}
}
}