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