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 #define GOT_OBJECT_ID_MAXLEN SHA256_DIGEST_LENGTH
18 #define GOT_OBJECT_ID_HEX_MAXLEN SHA256_DIGEST_STRING_LENGTH
20 enum got_hash_algorithm {
21 GOT_HASH_SHA1,
22 GOT_HASH_SHA256,
23 };
25 struct got_object_id {
26 u_int8_t hash[GOT_OBJECT_ID_MAXLEN];
27 enum got_hash_algorithm algo;
28 };
30 struct got_blob_object;
31 struct got_tree_object;
32 struct got_tree_entry;
33 struct got_tag_object;
34 struct got_commit_object;
36 struct got_object_qid {
37 STAILQ_ENTRY(got_object_qid) entry;
38 struct got_object_id id;
39 void *data; /* managed by API user */
40 };
42 STAILQ_HEAD(got_object_id_queue, got_object_qid);
44 const struct got_error *got_object_qid_alloc(struct got_object_qid **,
45 struct got_object_id *);
46 void got_object_qid_free(struct got_object_qid *);
47 void got_object_id_queue_free(struct got_object_id_queue *);
49 /*
50 * Deep-copy elements from ID queue src to ID queue dest. Do not copy any
51 * qid->data pointers! This is the caller's responsibility if needed.
52 */
53 const struct got_error *got_object_id_queue_copy(
54 const struct got_object_id_queue *src, struct got_object_id_queue *dest);
56 /* Object types. */
57 #define GOT_OBJ_TYPE_ANY 0 /* wildcard value at run-time */
58 #define GOT_OBJ_TYPE_COMMIT 1
59 #define GOT_OBJ_TYPE_TREE 2
60 #define GOT_OBJ_TYPE_BLOB 3
61 #define GOT_OBJ_TYPE_TAG 4
62 /* 5 is reserved */
63 #define GOT_OBJ_TYPE_OFFSET_DELTA 6
64 #define GOT_OBJ_TYPE_REF_DELTA 7
66 /*
67 * Labels used in object data.
68 */
70 #define GOT_OBJ_LABEL_COMMIT "commit"
71 #define GOT_OBJ_LABEL_TREE "tree"
72 #define GOT_OBJ_LABEL_BLOB "blob"
73 #define GOT_OBJ_LABEL_TAG "tag"
75 #define GOT_COMMIT_LABEL_TREE "tree "
76 #define GOT_COMMIT_LABEL_PARENT "parent "
77 #define GOT_COMMIT_LABEL_AUTHOR "author "
78 #define GOT_COMMIT_LABEL_COMMITTER "committer "
80 #define GOT_TAG_LABEL_OBJECT "object "
81 #define GOT_TAG_LABEL_TYPE "type "
82 #define GOT_TAG_LABEL_TAG "tag "
83 #define GOT_TAG_LABEL_TAGGER "tagger "
85 struct got_repository;
87 /*
88 * Obtain a string representation of an object ID. The output depends on
89 * the hash function used by the repository format (currently SHA1).
90 */
91 const struct got_error *got_object_id_str(char **, struct got_object_id *);
93 /*
94 * Compare two object IDs. Return value behaves like memcmp(3).
95 */
96 int got_object_id_cmp(const struct got_object_id *,
97 const struct got_object_id *);
99 /*
100 * Created a newly allocated copy of an object ID.
101 * The caller should dispose of it with free(3).
102 */
103 struct got_object_id *got_object_id_dup(struct got_object_id *);
105 /*
106 * Get a newly allocated ID of the object which resides at the specified
107 * path in the specified tree.
108 * The caller should dispose of it with free(3).
109 */
110 const struct got_error *got_object_tree_find_path(struct got_object_id **id,
111 mode_t *mode, struct got_repository *repo, struct got_tree_object *tree,
112 const char *path);
114 /*
115 * Get a newly allocated ID of the object which resides at the specified
116 * path in the tree of the specified commit.
117 * The caller should dispose of it with free(3).
118 */
119 const struct got_error *got_object_id_by_path(struct got_object_id **,
120 struct got_repository *, struct got_commit_object *, const char *);
122 /*
123 * Obtain the type of an object.
124 * Returns one of the GOT_OBJ_TYPE_x values (see above).
125 */
126 const struct got_error *got_object_get_type(int *, struct got_repository *,
127 struct got_object_id *);
129 /*
130 * Attempt to resolve the textual representation of an object ID
131 * to the ID of an existing object in the repository.
132 * The caller should dispose of the ID with free(3).
133 */
134 const struct got_error *got_object_resolve_id_str(struct got_object_id **,
135 struct got_repository *, const char *);
137 /*
138 * Attempt to open a commit object in a repository.
139 * The caller must dispose of the commit with got_object_commit_close().
140 */
141 const struct got_error *got_object_open_as_commit(struct got_commit_object **,
142 struct got_repository *, struct got_object_id *);
144 /* Dispose of a commit object. */
145 void got_object_commit_close(struct got_commit_object *);
147 /* Obtain the ID of the tree created in a commit. */
148 struct got_object_id *got_object_commit_get_tree_id(struct got_commit_object *);
150 /* Obtain the number of parent commits of a commit. */
151 int got_object_commit_get_nparents(struct got_commit_object *);
153 /* Obtain the list of parent commits of a commit. */
154 const struct got_object_id_queue *got_object_commit_get_parent_ids(
155 struct got_commit_object *);
157 /* Get the author's name and email address. */
158 const char *got_object_commit_get_author(struct got_commit_object *);
160 /* Get an author's commit timestamp in UTC. */
161 time_t got_object_commit_get_author_time(struct got_commit_object *);
163 /* Get an author's timezone offset. */
164 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *);
166 /* Get the committer's name and email address. */
167 const char *got_object_commit_get_committer(struct got_commit_object *);
169 /* Get a committer's commit timestamp in UTC. */
170 time_t got_object_commit_get_committer_time(struct got_commit_object *);
172 /* Get a committer's timezone offset. */
173 time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
175 /*
176 * Get the commit log message.
177 * PGP-signatures contained in the log message will be stripped.
178 * The caller must dispose of it with free(3).
179 */
180 const struct got_error *got_object_commit_get_logmsg(char **,
181 struct got_commit_object *);
183 /* Get the raw commit log message.*/
184 const char *got_object_commit_get_logmsg_raw(struct got_commit_object *);
186 /*
187 * Attempt to open a tree object in a repository.
188 * The caller must dispose of the tree with got_object_tree_close().
189 */
190 const struct got_error *got_object_open_as_tree(struct got_tree_object **,
191 struct got_repository *, struct got_object_id *);
193 /* Dispose of a tree object. */
194 void got_object_tree_close(struct got_tree_object *);
196 /* Get the number of entries in this tree object. */
197 int got_object_tree_get_nentries(struct got_tree_object *);
199 /* Get the first tree entry from a tree, or NULL if there is none. */
200 struct got_tree_entry *got_object_tree_get_first_entry(
201 struct got_tree_object *);
203 /* Get the last tree entry from a tree, or NULL if there is none. */
204 struct got_tree_entry *got_object_tree_get_last_entry(struct got_tree_object *);
206 /* Get the entry with the specified index from a tree object. */
207 struct got_tree_entry *got_object_tree_get_entry(
208 struct got_tree_object *, int);
210 /* Find a particular entry in a tree by name. */
211 struct got_tree_entry *got_object_tree_find_entry(
212 struct got_tree_object *, const char *);
214 /* Get the file permission mode of a tree entry. */
215 mode_t got_tree_entry_get_mode(struct got_tree_entry *);
217 /* Get the name of a tree entry. */
218 const char *got_tree_entry_get_name(struct got_tree_entry *);
220 /* Get the object ID of a tree entry. */
221 struct got_object_id *got_tree_entry_get_id(struct got_tree_entry *);
223 /*
224 * Get a string containing the target path of a given a symlink tree entry.
225 * The caller should dispose of it with free(3).
226 */
227 const struct got_error *got_tree_entry_get_symlink_target(char **,
228 struct got_tree_entry *, struct got_repository *);
230 /* Get the index of a tree entry. */
231 int got_tree_entry_get_index(struct got_tree_entry *);
233 /* Get the next tree entry from a tree, or NULL if there is none. */
234 struct got_tree_entry *got_tree_entry_get_next(struct got_tree_object *,
235 struct got_tree_entry *);
237 /* Get the previous tree entry from a tree, or NULL if there is none. */
238 struct got_tree_entry *got_tree_entry_get_prev(struct got_tree_object *,
239 struct got_tree_entry *);
241 /* Return non-zero if the specified tree entry is a Git submodule. */
242 int got_object_tree_entry_is_submodule(struct got_tree_entry *);
244 /* Return non-zero if the specified tree entry is a symbolic link. */
245 int got_object_tree_entry_is_symlink(struct got_tree_entry *);
247 /*
248 * Resolve an in-repository symlink at the specified path in the tree
249 * corresponding to the specified commit. If the specified path is not
250 * a symlink then set *link_target to NULL.
251 * Otherwise, resolve symlinks recursively and return the final link
252 * target path. The caller must dispose of it with free(3).
253 */
254 const struct got_error *got_object_resolve_symlinks(char **, const char *,
255 struct got_commit_object *, struct got_repository *);
257 /*
258 * Compare two trees and indicate whether the entry at the specified path
259 * differs between them. The path must not be the root path "/"; the function
260 * got_object_id_cmp() should be used instead to compare the tree roots.
261 */
262 const struct got_error *got_object_tree_path_changed(int *,
263 struct got_tree_object *, struct got_tree_object *, const char *,
264 struct got_repository *);
266 /*
267 * Attempt to open a blob object in a repository.
268 * The size_t argument specifies the block size of an associated read buffer.
269 * The caller must dispose of the blob with got_object_blob_close().
270 */
271 const struct got_error *got_object_open_as_blob(struct got_blob_object **,
272 struct got_repository *, struct got_object_id *, size_t, int);
274 /* Dispose of a blob object. */
275 const struct got_error *got_object_blob_close(struct got_blob_object *);
277 /*
278 * Get the length of header data at the beginning of the blob's read buffer.
279 * Note that header data is only present upon the first invocation of
280 * got_object_blob_read_block() after the blob is opened.
281 */
282 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
284 /*
285 * Get a pointer to the blob's read buffer.
286 * The read buffer is filled by got_object_blob_read_block().
287 */
288 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
290 /*
291 * Read the next chunk of data from a blob, up to the blob's read buffer
292 * block size. The size_t output argument indicates how many bytes have
293 * been read into the blob's read buffer. Zero bytes will be reported if
294 * all data in the blob has been read.
295 */
296 const struct got_error *got_object_blob_read_block(size_t *,
297 struct got_blob_object *);
299 /* Rewind an open blob's data stream back to the beginning. */
300 void got_object_blob_rewind(struct got_blob_object *);
302 /*
303 * Heuristic to check whether the blob contains binary data. Rewinds
304 * the blob's data stream back after the header.
305 */
306 const struct got_error *got_object_blob_is_binary(int *,
307 struct got_blob_object *);
309 /*
310 * getline(3) for blobs.
311 */
312 const struct got_error *got_object_blob_getline(char **, ssize_t *,
313 size_t *, struct got_blob_object *);
315 /*
316 * Read the entire content of a blob and write it to the specified file.
317 * Flush and rewind the file as well. Indicate the amount of bytes
318 * written in the size_t output argument, and the number of lines in the
319 * file in the int argument, and line offsets in the off_t argument
320 * (NULL can be passed for any output argument).
321 */
322 const struct got_error *got_object_blob_dump_to_file(off_t *, int *,
323 off_t **, FILE *, struct got_blob_object *);
325 /*
326 * Read the entire content of a blob into a newly allocated string buffer
327 * and terminate it with '\0'. This is intended for blobs which contain a
328 * symlink target path. It should not be used to process arbitrary blobs.
329 * Use got_object_blob_dump_to_file() or got_tree_entry_get_symlink_target()
330 * instead if possible. The caller must dispose of the string with free(3).
331 */
332 const struct got_error *got_object_blob_read_to_str(char **,
333 struct got_blob_object *);
335 /*
336 * Attempt to open a tag object in a repository.
337 * The caller must dispose of the tree with got_tag_object_close().
338 */
339 const struct got_error *got_object_open_as_tag(struct got_tag_object **,
340 struct got_repository *, struct got_object_id *);
342 /* Dispose of a tag object. */
343 void got_object_tag_close(struct got_tag_object *);
345 /* Get the name of a tag. */
346 const char *got_object_tag_get_name(struct got_tag_object *);
348 /* Get type of the object a tag points to. */
349 int got_object_tag_get_object_type(struct got_tag_object *);
351 /*
352 * Get ID of the object a tag points to.
353 * This must not be freed by the caller. Use got_object_id_dup() if needed.
354 */
355 struct got_object_id *got_object_tag_get_object_id(struct got_tag_object *);
358 /* Get the timestamp of the tag. */
359 time_t got_object_tag_get_tagger_time(struct got_tag_object *);
361 /* Get the tag's timestamp's GMT offset. */
362 time_t got_object_tag_get_tagger_gmtoff(struct got_tag_object *);
364 /* Get the author of the tag. */
365 const char *got_object_tag_get_tagger(struct got_tag_object *);
367 /* Get the tag message associated with the tag. */
368 const char *got_object_tag_get_message(struct got_tag_object *);
370 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
371 const char *, enum got_hash_algorithm);
373 /* Create a new tag object in the repository. */
374 const struct got_error *got_object_tag_create(struct got_object_id **,
375 const char *, struct got_object_id *, const char *,
376 time_t, const char *, const char *, struct got_repository *, int verbosity);
378 /* Increment commit object reference counter. */
379 void got_object_commit_retain(struct got_commit_object *);