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