1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 /**
8  * License: GPL-2.0(Linking Exception)
9  */
10 module libgit2.pack;
11 
12 
13 private static import libgit2.buffer;
14 private static import libgit2.indexer;
15 private static import libgit2.oid;
16 private static import libgit2.types;
17 private import libgit2.common: GIT_EXTERN;
18 
19 /*
20  * @file git2/pack.h
21  * @brief Git pack management routines
22  *
23  * Packing objects
24  * ---------------
25  *
26  * Creation of packfiles requires two steps:
27  *
28  * - First, insert all the objects you want to put into the packfile
29  *   using `git_packbuilder_insert` and `git_packbuilder_insert_tree`.
30  *   It's important to add the objects in recency order ("in the order
31  *   that they are 'reachable' from head").
32  *
33  *   "ANY order will give you a working pack, ... [but it is] the thing
34  *   that gives packs good locality. It keeps the objects close to the
35  *   head (whether they are old or new, but they are _reachable_ from the
36  *   head) at the head of the pack. So packs actually have absolutely
37  *   _wonderful_ IO patterns." - Linus Torvalds
38  *   git.git/Documentation/technical/pack-heuristics.txt
39  *
40  * - Second, use `git_packbuilder_write` or `git_packbuilder_foreach` to
41  *   write the resulting packfile.
42  *
43  *   libgit2 will take care of the delta ordering and generation.
44  *   `git_packbuilder_set_threads` can be used to adjust the number of
45  *   threads used for the process.
46  *
47  * See tests/pack/packbuilder.c for an example.
48  *
49  * @ingroup Git
50  * @{
51  */
52 extern (C):
53 nothrow @nogc:
54 public:
55 
56 /**
57  * Stages that are reported by the packbuilder progress callback.
58  */
59 enum git_packbuilder_stage_t
60 {
61 	GIT_PACKBUILDER_ADDING_OBJECTS = 0,
62 	GIT_PACKBUILDER_DELTAFICATION = 1,
63 }
64 
65 //Declaration name in C language
66 enum
67 {
68 	GIT_PACKBUILDER_ADDING_OBJECTS = .git_packbuilder_stage_t.GIT_PACKBUILDER_ADDING_OBJECTS,
69 	GIT_PACKBUILDER_DELTAFICATION = .git_packbuilder_stage_t.GIT_PACKBUILDER_DELTAFICATION,
70 }
71 
72 /**
73  * Initialize a new packbuilder
74  *
75  * Params:
76  *      out_ = The new packbuilder object
77  *      repo = The repository
78  *
79  * Returns: 0 or an error code
80  */
81 @GIT_EXTERN
82 int git_packbuilder_new(libgit2.types.git_packbuilder** out_, libgit2.types.git_repository* repo);
83 
84 /**
85  * Set number of threads to spawn
86  *
87  * By default, libgit2 won't spawn any threads at all;
88  * when set to 0, libgit2 will autodetect the number of
89  * CPUs.
90  *
91  * Params:
92  *      pb = The packbuilder
93  *      n = Number of threads to spawn
94  *
95  * Returns: number of actual threads to be used
96  */
97 @GIT_EXTERN
98 uint git_packbuilder_set_threads(libgit2.types.git_packbuilder* pb, uint n);
99 
100 /**
101  * Insert a single object
102  *
103  * For an optimal pack it's mandatory to insert objects in recency order,
104  * commits followed by trees and blobs.
105  *
106  * Params:
107  *      pb = The packbuilder
108  *      id = The oid of the commit
109  *      name = The name; might be null
110  *
111  * Returns: 0 or an error code
112  */
113 @GIT_EXTERN
114 int git_packbuilder_insert(libgit2.types.git_packbuilder* pb, const (libgit2.oid.git_oid)* id, const (char)* name);
115 
116 /**
117  * Insert a root tree object
118  *
119  * This will add the tree as well as all referenced trees and blobs.
120  *
121  * Params:
122  *      pb = The packbuilder
123  *      id = The oid of the root tree
124  *
125  * Returns: 0 or an error code
126  */
127 @GIT_EXTERN
128 int git_packbuilder_insert_tree(libgit2.types.git_packbuilder* pb, const (libgit2.oid.git_oid)* id);
129 
130 /**
131  * Insert a commit object
132  *
133  * This will add a commit as well as the completed referenced tree.
134  *
135  * Params:
136  *      pb = The packbuilder
137  *      id = The oid of the commit
138  *
139  * Returns: 0 or an error code
140  */
141 @GIT_EXTERN
142 int git_packbuilder_insert_commit(libgit2.types.git_packbuilder* pb, const (libgit2.oid.git_oid)* id);
143 
144 /**
145  * Insert objects as given by the walk
146  *
147  * Those commits and all objects they reference will be inserted into
148  * the packbuilder.
149  *
150  * Params:
151  *      pb = the packbuilder
152  *      walk = the revwalk to use to fill the packbuilder
153  *
154  * Returns: 0 or an error code
155  */
156 @GIT_EXTERN
157 int git_packbuilder_insert_walk(libgit2.types.git_packbuilder* pb, libgit2.types.git_revwalk* walk);
158 
159 /**
160  * Recursively insert an object and its referenced objects
161  *
162  * Insert the object as well as any object it references.
163  *
164  * Params:
165  *      pb = the packbuilder
166  *      id = the id of the root object to insert
167  *      name = optional name for the object
168  *
169  * Returns: 0 or an error code
170  */
171 @GIT_EXTERN
172 int git_packbuilder_insert_recur(libgit2.types.git_packbuilder* pb, const (libgit2.oid.git_oid)* id, const (char)* name);
173 
174 /**
175  * Write the contents of the packfile to an in-memory buffer
176  *
177  * The contents of the buffer will become a valid packfile, even though there
178  * will be no attached index
179  *
180  * Params:
181  *      buf = Buffer where to write the packfile
182  *      pb = The packbuilder
183  *
184  * Returns: 0 or an error code
185  */
186 @GIT_EXTERN
187 int git_packbuilder_write_buf(libgit2.buffer.git_buf* buf, libgit2.types.git_packbuilder* pb);
188 
189 /**
190  * Write the new pack and corresponding index file to path.
191  *
192  * Params:
193  *      pb = The packbuilder
194  *      path = Path to the directory where the packfile and index should be stored, or null for default location
195  *      mode = permissions to use creating a packfile or 0 for defaults
196  *      progress_cb = function to call with progress information from the indexer (optional)
197  *      progress_cb_payload = payload for the progress callback (optional)
198  *
199  * Returns: 0 or an error code
200  */
201 @GIT_EXTERN
202 int git_packbuilder_write(libgit2.types.git_packbuilder* pb, const (char)* path, uint mode, libgit2.indexer.git_indexer_progress_cb progress_cb, void* progress_cb_payload);
203 
204 version (GIT_DEPRECATE_HARD) {
205 } else {
206 	/**
207 	* Get the packfile's hash
208 	*
209 	* A packfile's name is derived from the sorted hashing of all object
210 	* names. This is only correct after the packfile has been written.
211 	*
212 	* @deprecated use git_packbuilder_name
213 	* @param pb The packbuilder object
214 	* @return 0 or an error code
215 	*/
216 	@GIT_EXTERN
217 	const (libgit2.oid.git_oid)* git_packbuilder_hash(libgit2.types.git_packbuilder* pb);
218 }
219 
220 /**
221  * Get the unique name for the resulting packfile.
222  *
223  * The packfile's name is derived from the packfile's content.
224  * This is only correct after the packfile has been written.
225  *
226  * Params:
227  *      pb = the packbuilder instance
228  *
229  * Returns: a null terminated string for the packfile name
230  */
231 @GIT_EXTERN
232 const (char)* git_packbuilder_name(libgit2.types.git_packbuilder* pb);
233 
234 /**
235  * Callback used to iterate over packed objects
236  *
237  * @see git_packbuilder_foreach
238  *
239  * Returns: non-zero to terminate the iteration
240  */
241 /*
242  * Params:
243  *      buf = A pointer to the object's data
244  *      size = The size of the underlying object
245  *      payload = Payload passed to git_packbuilder_foreach
246  */
247 alias git_packbuilder_foreach_cb = int function(void* buf, size_t size, void* payload);
248 
249 /**
250  * Create the new pack and pass each object to the callback
251  *
252  * Params:
253  *      pb = the packbuilder
254  *      cb = the callback to call with each packed object's buffer
255  *      payload = the callback's data
256  *
257  * Returns: 0 or an error code
258  */
259 @GIT_EXTERN
260 int git_packbuilder_foreach(libgit2.types.git_packbuilder* pb, .git_packbuilder_foreach_cb cb, void* payload);
261 
262 /**
263  * Get the total number of objects the packbuilder will write out
264  *
265  * Params:
266  *      pb = the packbuilder
267  *
268  * Returns: the number of objects in the packfile
269  */
270 @GIT_EXTERN
271 size_t git_packbuilder_object_count(libgit2.types.git_packbuilder* pb);
272 
273 /**
274  * Get the number of objects the packbuilder has already written out
275  *
276  * Params:
277  *      pb = the packbuilder
278  *
279  * Returns: the number of objects which have already been written
280  */
281 @GIT_EXTERN
282 size_t git_packbuilder_written(libgit2.types.git_packbuilder* pb);
283 
284 /**
285  * Packbuilder progress notification function
286  */
287 alias git_packbuilder_progress = int function(int stage, uint current, uint total, void* payload);
288 
289 /**
290  * Set the callbacks for a packbuilder
291  *
292  * Params:
293  *      pb = The packbuilder object
294  *      progress_cb = Function to call with progress information during pack building. Be aware that this is called inline with pack building operations, so performance may be affected.
295  *      progress_cb_payload = Payload for progress callback.
296  *
297  * Returns: 0 or an error code
298  */
299 @GIT_EXTERN
300 int git_packbuilder_set_callbacks(libgit2.types.git_packbuilder* pb, .git_packbuilder_progress progress_cb, void* progress_cb_payload);
301 
302 /**
303  * Free the packbuilder and all associated data
304  *
305  * Params:
306  *      pb = The packbuilder
307  */
308 @GIT_EXTERN
309 void git_packbuilder_free(libgit2.types.git_packbuilder* pb);
310 
311 /* @} */