2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_git_pack_h__
8 #define INCLUDE_git_pack_h__
16 * @brief Git pack management routines
21 * Creation of packfiles requires two steps:
23 * - First, insert all the objects you want to put into the packfile
24 * using `git_packbuilder_insert` and `git_packbuilder_insert_tree`.
25 * It's important to add the objects in recency order ("in the order
26 * that they are 'reachable' from head").
28 * "ANY order will give you a working pack, ... [but it is] the thing
29 * that gives packs good locality. It keeps the objects close to the
30 * head (whether they are old or new, but they are _reachable_ from the
31 * head) at the head of the pack. So packs actually have absolutely
32 * _wonderful_ IO patterns." - Linus Torvalds
33 * git.git/Documentation/technical/pack-heuristics.txt
35 * - Second, use `git_packbuilder_write` or `git_packbuilder_foreach` to
36 * write the resulting packfile.
38 * libgit2 will take care of the delta ordering and generation.
39 * `git_packbuilder_set_threads` can be used to adjust the number of
40 * threads used for the process.
42 * See tests/pack/packbuilder.c for an example.
50 * Stages that are reported by the packbuilder progress callback.
53 GIT_PACKBUILDER_ADDING_OBJECTS
= 0,
54 GIT_PACKBUILDER_DELTAFICATION
= 1,
55 } git_packbuilder_stage_t
;
58 * Initialize a new packbuilder
60 * @param out The new packbuilder object
61 * @param repo The repository
63 * @return 0 or an error code
65 GIT_EXTERN(int) git_packbuilder_new(git_packbuilder
**out
, git_repository
*repo
);
68 * Set number of threads to spawn
70 * By default, libgit2 won't spawn any threads at all;
71 * when set to 0, libgit2 will autodetect the number of
74 * @param pb The packbuilder
75 * @param n Number of threads to spawn
76 * @return number of actual threads to be used
78 GIT_EXTERN(unsigned int) git_packbuilder_set_threads(git_packbuilder
*pb
, unsigned int n
);
81 * Insert a single object
83 * For an optimal pack it's mandatory to insert objects in recency order,
84 * commits followed by trees and blobs.
86 * @param pb The packbuilder
87 * @param id The oid of the commit
88 * @param name The name; might be NULL
90 * @return 0 or an error code
92 GIT_EXTERN(int) git_packbuilder_insert(git_packbuilder
*pb
, const git_oid
*id
, const char *name
);
95 * Insert a root tree object
97 * This will add the tree as well as all referenced trees and blobs.
99 * @param pb The packbuilder
100 * @param id The oid of the root tree
102 * @return 0 or an error code
104 GIT_EXTERN(int) git_packbuilder_insert_tree(git_packbuilder
*pb
, const git_oid
*id
);
107 * Insert a commit object
109 * This will add a commit as well as the completed referenced tree.
111 * @param pb The packbuilder
112 * @param id The oid of the commit
114 * @return 0 or an error code
116 GIT_EXTERN(int) git_packbuilder_insert_commit(git_packbuilder
*pb
, const git_oid
*id
);
119 * Insert objects as given by the walk
121 * Those commits and all objects they reference will be inserted into
124 * @param pb the packbuilder
125 * @param walk the revwalk to use to fill the packbuilder
127 * @return 0 or an error code
129 GIT_EXTERN(int) git_packbuilder_insert_walk(git_packbuilder
*pb
, git_revwalk
*walk
);
132 * Recursively insert an object and its referenced objects
134 * Insert the object as well as any object it references.
136 * @param pb the packbuilder
137 * @param id the id of the root object to insert
138 * @param name optional name for the object
139 * @return 0 or an error code
141 GIT_EXTERN(int) git_packbuilder_insert_recur(git_packbuilder
*pb
, const git_oid
*id
, const char *name
);
144 * Write the contents of the packfile to an in-memory buffer
146 * The contents of the buffer will become a valid packfile, even though there
147 * will be no attached index
149 * @param buf Buffer where to write the packfile
150 * @param pb The packbuilder
152 GIT_EXTERN(int) git_packbuilder_write_buf(git_buf
*buf
, git_packbuilder
*pb
);
155 * Write the new pack and corresponding index file to path.
157 * @param pb The packbuilder
158 * @param path Path to the directory where the packfile and index should be stored, or NULL for default location
159 * @param mode permissions to use creating a packfile or 0 for defaults
160 * @param progress_cb function to call with progress information from the indexer (optional)
161 * @param progress_cb_payload payload for the progress callback (optional)
163 * @return 0 or an error code
165 GIT_EXTERN(int) git_packbuilder_write(
169 git_indexer_progress_cb progress_cb
,
170 void *progress_cb_payload
);
173 * Get the packfile's hash
175 * A packfile's name is derived from the sorted hashing of all object
176 * names. This is only correct after the packfile has been written.
178 * @param pb The packbuilder object
180 GIT_EXTERN(const git_oid
*) git_packbuilder_hash(git_packbuilder
*pb
);
183 * Callback used to iterate over packed objects
185 * @see git_packbuilder_foreach
187 * @param buf A pointer to the object's data
188 * @param size The size of the underlying object
189 * @param payload Payload passed to git_packbuilder_foreach
190 * @return non-zero to terminate the iteration
192 typedef int GIT_CALLBACK(git_packbuilder_foreach_cb
)(void *buf
, size_t size
, void *payload
);
195 * Create the new pack and pass each object to the callback
197 * @param pb the packbuilder
198 * @param cb the callback to call with each packed object's buffer
199 * @param payload the callback's data
200 * @return 0 or an error code
202 GIT_EXTERN(int) git_packbuilder_foreach(git_packbuilder
*pb
, git_packbuilder_foreach_cb cb
, void *payload
);
205 * Get the total number of objects the packbuilder will write out
207 * @param pb the packbuilder
208 * @return the number of objects in the packfile
210 GIT_EXTERN(size_t) git_packbuilder_object_count(git_packbuilder
*pb
);
213 * Get the number of objects the packbuilder has already written out
215 * @param pb the packbuilder
216 * @return the number of objects which have already been written
218 GIT_EXTERN(size_t) git_packbuilder_written(git_packbuilder
*pb
);
220 /** Packbuilder progress notification function */
221 typedef int GIT_CALLBACK(git_packbuilder_progress
)(
228 * Set the callbacks for a packbuilder
230 * @param pb The packbuilder object
231 * @param progress_cb Function to call with progress information during
232 * pack building. Be aware that this is called inline with pack building
233 * operations, so performance may be affected.
234 * @param progress_cb_payload Payload for progress callback.
235 * @return 0 or an error code
237 GIT_EXTERN(int) git_packbuilder_set_callbacks(
239 git_packbuilder_progress progress_cb
,
240 void *progress_cb_payload
);
243 * Free the packbuilder and all associated data
245 * @param pb The packbuilder
247 GIT_EXTERN(void) git_packbuilder_free(git_packbuilder
*pb
);