Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 struct got_object_id;
19 struct got_blob_object;
20 struct got_tree_object;
21 struct got_tree_entry;
22 struct got_tag_object;
23 struct got_commit_object;
25 struct got_object_qid {
26 STAILQ_ENTRY(got_object_qid) entry;
27 struct got_object_id *id;
28 void *data; /* managed by API user */
29 };
31 STAILQ_HEAD(got_object_id_queue, got_object_qid);
33 const struct got_error *got_object_qid_alloc(struct got_object_qid **,
34 struct got_object_id *);
35 void got_object_qid_free(struct got_object_qid *);
36 void got_object_id_queue_free(struct got_object_id_queue *);
38 /*
39 * Deep-copy elements from ID queue src to ID queue dest. Do not copy any
40 * qid->data pointers! This is the caller's responsibility if needed.
41 */
42 const struct got_error *got_object_id_queue_copy(
43 const struct got_object_id_queue *src, struct got_object_id_queue *dest);
45 /* Object types. */
46 #define GOT_OBJ_TYPE_ANY 0 /* wildcard value at run-time */
47 #define GOT_OBJ_TYPE_COMMIT 1
48 #define GOT_OBJ_TYPE_TREE 2
49 #define GOT_OBJ_TYPE_BLOB 3
50 #define GOT_OBJ_TYPE_TAG 4
51 /* 5 is reserved */
52 #define GOT_OBJ_TYPE_OFFSET_DELTA 6
53 #define GOT_OBJ_TYPE_REF_DELTA 7
55 /*
56 * Labels used in object data.
57 */
59 #define GOT_OBJ_LABEL_COMMIT "commit"
60 #define GOT_OBJ_LABEL_TREE "tree"
61 #define GOT_OBJ_LABEL_BLOB "blob"
62 #define GOT_OBJ_LABEL_TAG "tag"
64 #define GOT_COMMIT_LABEL_TREE "tree "
65 #define GOT_COMMIT_LABEL_PARENT "parent "
66 #define GOT_COMMIT_LABEL_AUTHOR "author "
67 #define GOT_COMMIT_LABEL_COMMITTER "committer "
69 #define GOT_TAG_LABEL_OBJECT "object "
70 #define GOT_TAG_LABEL_TYPE "type "
71 #define GOT_TAG_LABEL_TAG "tag "
72 #define GOT_TAG_LABEL_TAGGER "tagger "
74 struct got_repository;
76 /*
77 * Obtain a string representation of an object ID. The output depends on
78 * the hash function used by the repository format (currently SHA1).
79 */
80 const struct got_error *got_object_id_str(char **, struct got_object_id *);
82 /*
83 * Compare two object IDs. Return value behaves like memcmp(3).
84 */
85 int got_object_id_cmp(const struct got_object_id *,
86 const struct got_object_id *);
88 /*
89 * Created a newly allocated copy of an object ID.
90 * The caller should dispose of it with free(3).
91 */
92 struct got_object_id *got_object_id_dup(struct got_object_id *);
94 /*
95 * Get a newly allocated ID of the object which resides at the specified
96 * path in the specified tree.
97 * The caller should dispose of it with free(3).
98 */
99 const struct got_error *got_object_tree_find_path(struct got_object_id **id,
100 mode_t *mode, struct got_repository *repo, struct got_tree_object *tree,
101 const char *path);
103 /*
104 * Get a newly allocated ID of the object which resides at the specified
105 * path in the tree of the specified commit.
106 * The caller should dispose of it with free(3).
107 */
108 const struct got_error *got_object_id_by_path(struct got_object_id **,
109 struct got_repository *, struct got_object_id *, const char *);
111 /*
112 * Obtain the type of an object.
113 * Returns one of the GOT_OBJ_TYPE_x values (see above).
114 */
115 const struct got_error *got_object_get_type(int *, struct got_repository *,
116 struct got_object_id *);
118 /*
119 * Attempt to resolve the textual representation of an object ID
120 * to the ID of an existing object in the repository.
121 * The caller should dispose of the ID with free(3).
122 */
123 const struct got_error *got_object_resolve_id_str(struct got_object_id **,
124 struct got_repository *, const char *);
126 /*
127 * Attempt to open a commit object in a repository.
128 * The caller must dispose of the commit with got_object_commit_close().
129 */
130 const struct got_error *got_object_open_as_commit(struct got_commit_object **,
131 struct got_repository *, struct got_object_id *);
133 /* Dispose of a commit object. */
134 void got_object_commit_close(struct got_commit_object *);
136 /* Obtain the ID of the tree created in a commit. */
137 struct got_object_id *got_object_commit_get_tree_id(struct got_commit_object *);
139 /* Obtain the number of parent commits of a commit. */
140 int got_object_commit_get_nparents(struct got_commit_object *);
142 /* Obtain the list of parent commits of a commit. */
143 const struct got_object_id_queue *got_object_commit_get_parent_ids(
144 struct got_commit_object *);
146 /* Get the author's name and email address. */
147 const char *got_object_commit_get_author(struct got_commit_object *);
149 /* Get an author's commit timestamp in UTC. */
150 time_t got_object_commit_get_author_time(struct got_commit_object *);
152 /* Get an author's timezone offset. */
153 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *);
155 /* Get the committer's name and email address. */
156 const char *got_object_commit_get_committer(struct got_commit_object *);
158 /* Get a committer's commit timestamp in UTC. */
159 time_t got_object_commit_get_committer_time(struct got_commit_object *);
161 /* Get a committer's timezone offset. */
162 time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
164 /*
165 * Get the commit log message.
166 * PGP-signatures contained in the log message will be stripped.
167 * The caller must dispose of it with free(3).
168 */
169 const struct got_error *got_object_commit_get_logmsg(char **,
170 struct got_commit_object *);
172 /* Get the raw commit log message.*/
173 const char *got_object_commit_get_logmsg_raw(struct got_commit_object *);
175 /*
176 * Attempt to open a tree object in a repository.
177 * The caller must dispose of the tree with got_object_tree_close().
178 */
179 const struct got_error *got_object_open_as_tree(struct got_tree_object **,
180 struct got_repository *, struct got_object_id *);
182 /* Dispose of a tree object. */
183 void got_object_tree_close(struct got_tree_object *);
185 /* Get the number of entries in this tree object. */
186 int got_object_tree_get_nentries(struct got_tree_object *);
188 /* Get the first tree entry from a tree, or NULL if there is none. */
189 struct got_tree_entry *got_object_tree_get_first_entry(
190 struct got_tree_object *);
192 /* Get the last tree entry from a tree, or NULL if there is none. */
193 struct got_tree_entry *got_object_tree_get_last_entry(struct got_tree_object *);
195 /* Get the entry with the specified index from a tree object. */
196 struct got_tree_entry *got_object_tree_get_entry(
197 struct got_tree_object *, int);
199 /* Find a particular entry in a tree by name. */
200 struct got_tree_entry *got_object_tree_find_entry(
201 struct got_tree_object *, const char *);
203 /* Get the file permission mode of a tree entry. */
204 mode_t got_tree_entry_get_mode(struct got_tree_entry *);
206 /* Get the name of a tree entry. */
207 const char *got_tree_entry_get_name(struct got_tree_entry *);
209 /* Get the object ID of a tree entry. */
210 struct got_object_id *got_tree_entry_get_id(struct got_tree_entry *);
212 /*
213 * Get a string containing the target path of a given a symlink tree entry.
214 * The caller should dispose of it with free(3).
215 */
216 const struct got_error *got_tree_entry_get_symlink_target(char **,
217 struct got_tree_entry *, struct got_repository *);
219 /* Get the index of a tree entry. */
220 int got_tree_entry_get_index(struct got_tree_entry *);
222 /* Get the next tree entry from a tree, or NULL if there is none. */
223 struct got_tree_entry *got_tree_entry_get_next(struct got_tree_object *,
224 struct got_tree_entry *);
226 /* Get the previous tree entry from a tree, or NULL if there is none. */
227 struct got_tree_entry *got_tree_entry_get_prev(struct got_tree_object *,
228 struct got_tree_entry *);
230 /* Return non-zero if the specified tree entry is a Git submodule. */
231 int got_object_tree_entry_is_submodule(struct got_tree_entry *);
233 /* Return non-zero if the specified tree entry is a symbolic link. */
234 int got_object_tree_entry_is_symlink(struct got_tree_entry *);
236 /*
237 * Resolve an in-repository symlink at the specified path in the tree
238 * corresponding to the specified commit. If the specified path is not
239 * a symlink then set *link_target to NULL.
240 * Otherwise, resolve symlinks recursively and return the final link
241 * target path. The caller must dispose of it with free(3).
242 */
243 const struct got_error *got_object_resolve_symlinks(char **, const char *,
244 struct got_object_id *, struct got_repository *);
246 /*
247 * Compare two trees and indicate whether the entry at the specified path
248 * differs between them. The path must not be the root path "/"; the function
249 * got_object_id_cmp() should be used instead to compare the tree roots.
250 */
251 const struct got_error *got_object_tree_path_changed(int *,
252 struct got_tree_object *, struct got_tree_object *, const char *,
253 struct got_repository *);
255 /*
256 * Attempt to open a blob object in a repository.
257 * The size_t argument specifies the block size of an associated read buffer.
258 * The caller must dispose of the blob with got_object_blob_close().
259 */
260 const struct got_error *got_object_open_as_blob(struct got_blob_object **,
261 struct got_repository *, struct got_object_id *, size_t);
263 /* Dispose of a blob object. */
264 const struct got_error *got_object_blob_close(struct got_blob_object *);
266 /*
267 * Get the length of header data at the beginning of the blob's read buffer.
268 * Note that header data is only present upon the first invocation of
269 * got_object_blob_read_block() after the blob is opened.
270 */
271 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
273 /*
274 * Get a pointer to the blob's read buffer.
275 * The read buffer is filled by got_object_blob_read_block().
276 */
277 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
279 /*
280 * Read the next chunk of data from a blob, up to the blob's read buffer
281 * block size. The size_t output argument indicates how many bytes have
282 * been read into the blob's read buffer. Zero bytes will be reported if
283 * all data in the blob has been read.
284 */
285 const struct got_error *got_object_blob_read_block(size_t *,
286 struct got_blob_object *);
288 /* Rewind an open blob's data stream back to the beginning. */
289 void got_object_blob_rewind(struct got_blob_object *);
291 /*
292 * Read the entire content of a blob and write it to the specified file.
293 * Flush and rewind the file as well. Indicate the amount of bytes
294 * written in the size_t output argument, and the number of lines in the
295 * file in the int argument, and line offsets in the off_t argument
296 * (NULL can be passed for any output argument).
297 */
298 const struct got_error *got_object_blob_dump_to_file(off_t *, int *,
299 off_t **, FILE *, struct got_blob_object *);
301 /*
302 * Read the entire content of a blob into a newly allocated string buffer
303 * and terminate it with '\0'. This is intended for blobs which contain a
304 * symlink target path. It should not be used to process arbitrary blobs.
305 * Use got_object_blob_dump_to_file() or got_tree_entry_get_symlink_target()
306 * instead if possible. The caller must dispose of the string with free(3).
307 */
308 const struct got_error *got_object_blob_read_to_str(char **,
309 struct got_blob_object *);
311 /*
312 * Attempt to open a tag object in a repository.
313 * The caller must dispose of the tree with got_tag_object_close().
314 */
315 const struct got_error *got_object_open_as_tag(struct got_tag_object **,
316 struct got_repository *, struct got_object_id *);
318 /* Dispose of a tag object. */
319 void got_object_tag_close(struct got_tag_object *);
321 /* Get the name of a tag. */
322 const char *got_object_tag_get_name(struct got_tag_object *);
324 /* Get type of the object a tag points to. */
325 int got_object_tag_get_object_type(struct got_tag_object *);
327 /*
328 * Get ID of the object a tag points to.
329 * This must not be freed by the caller. Use got_object_id_dup() if needed.
330 */
331 struct got_object_id *got_object_tag_get_object_id(struct got_tag_object *);
334 /* Get the timestamp of the tag. */
335 time_t got_object_tag_get_tagger_time(struct got_tag_object *);
337 /* Get the tag's timestamp's GMT offset. */
338 time_t got_object_tag_get_tagger_gmtoff(struct got_tag_object *);
340 /* Get the author of the tag. */
341 const char *got_object_tag_get_tagger(struct got_tag_object *);
343 /* Get the tag message associated with the tag. */
344 const char *got_object_tag_get_message(struct got_tag_object *);
346 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
347 const char *);
349 /* Create a new tag object in the repository. */
350 const struct got_error *got_object_tag_create(struct got_object_id **,
351 const char *, struct got_object_id *, const char *,
352 time_t, const char *, struct got_repository *);