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 SIMPLEQ_ENTRY(got_object_qid) entry;
27 struct got_object_id *id;
28 void *data; /* managed by API user */
29 };
31 SIMPLEQ_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 /* Object types. */
39 #define GOT_OBJ_TYPE_ANY 0 /* wildcard value at run-time */
40 #define GOT_OBJ_TYPE_COMMIT 1
41 #define GOT_OBJ_TYPE_TREE 2
42 #define GOT_OBJ_TYPE_BLOB 3
43 #define GOT_OBJ_TYPE_TAG 4
44 /* 5 is reserved */
45 #define GOT_OBJ_TYPE_OFFSET_DELTA 6
46 #define GOT_OBJ_TYPE_REF_DELTA 7
48 /*
49 * Labels used in object data.
50 */
52 #define GOT_OBJ_LABEL_COMMIT "commit"
53 #define GOT_OBJ_LABEL_TREE "tree"
54 #define GOT_OBJ_LABEL_BLOB "blob"
55 #define GOT_OBJ_LABEL_TAG "tag"
57 #define GOT_COMMIT_LABEL_TREE "tree "
58 #define GOT_COMMIT_LABEL_PARENT "parent "
59 #define GOT_COMMIT_LABEL_AUTHOR "author "
60 #define GOT_COMMIT_LABEL_COMMITTER "committer "
62 #define GOT_TAG_LABEL_OBJECT "object "
63 #define GOT_TAG_LABEL_TYPE "type "
64 #define GOT_TAG_LABEL_TAG "tag "
65 #define GOT_TAG_LABEL_TAGGER "tagger "
67 struct got_repository;
69 /*
70 * Obtain a string representation of an object ID. The output depends on
71 * the hash function used by the repository format (currently SHA1).
72 */
73 const struct got_error *got_object_id_str(char **, struct got_object_id *);
75 /*
76 * Compare two object IDs. Return value behaves like memcmp(3).
77 */
78 int got_object_id_cmp(const struct got_object_id *,
79 const struct got_object_id *);
81 /*
82 * Created a newly allocated copy of an object ID.
83 * The caller should dispose of it with free(3).
84 */
85 struct got_object_id *got_object_id_dup(struct got_object_id *);
87 /*
88 * Get a newly allocated ID of the object which resides at the specified
89 * path in the tree of the specified commit.
90 * The caller should dispose of it with free(3).
91 */
92 const struct got_error *got_object_id_by_path(struct got_object_id **,
93 struct got_repository *, struct got_object_id *, const char *);
95 /*
96 * Obtain the type of an object.
97 * Returns one of the GOT_OBJ_TYPE_x values (see above).
98 */
99 const struct got_error *got_object_get_type(int *, struct got_repository *,
100 struct got_object_id *);
102 /*
103 * Attempt to resolve the textual representation of an object ID
104 * to the ID of an existing object in the repository.
105 * The caller should dispose of the ID with free(3).
106 */
107 const struct got_error *got_object_resolve_id_str(struct got_object_id **,
108 struct got_repository *, const char *);
110 /*
111 * Attempt to open a commit object in a repository.
112 * The caller must dispose of the commit with got_object_commit_close().
113 */
114 const struct got_error *got_object_open_as_commit(struct got_commit_object **,
115 struct got_repository *, struct got_object_id *);
117 /* Dispose of a commit object. */
118 void got_object_commit_close(struct got_commit_object *);
120 /* Obtain the ID of the tree created in a commit. */
121 struct got_object_id *got_object_commit_get_tree_id(struct got_commit_object *);
123 /* Obtain the number of parent commits of a commit. */
124 int got_object_commit_get_nparents(struct got_commit_object *);
126 /* Obtain the list of parent commits of a commit. */
127 const struct got_object_id_queue *got_object_commit_get_parent_ids(
128 struct got_commit_object *);
130 /* Get the author's name and email address. */
131 const char *got_object_commit_get_author(struct got_commit_object *);
133 /* Get an author's commit timestamp in UTC. */
134 time_t got_object_commit_get_author_time(struct got_commit_object *);
136 /* Get an author's timezone offset. */
137 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *);
139 /* Get the committer's name and email address. */
140 const char *got_object_commit_get_committer(struct got_commit_object *);
142 /* Get a committer's commit timestamp in UTC. */
143 time_t got_object_commit_get_committer_time(struct got_commit_object *);
145 /* Get a committer's timezone offset. */
146 time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
148 /*
149 * Get the commit log message.
150 * PGP-signatures contained in the log message will be stripped.
151 * The caller must dispose of it with free(3).
152 */
153 const struct got_error *got_object_commit_get_logmsg(char **,
154 struct got_commit_object *);
156 /* Get the raw commit log message.*/
157 const char *got_object_commit_get_logmsg_raw(struct got_commit_object *);
159 /*
160 * Attempt to open a tree object in a repository.
161 * The caller must dispose of the tree with got_object_tree_close().
162 */
163 const struct got_error *got_object_open_as_tree(struct got_tree_object **,
164 struct got_repository *, struct got_object_id *);
166 /* Dispose of a tree object. */
167 void got_object_tree_close(struct got_tree_object *);
169 /* Get the number of entries in this tree object. */
170 int got_object_tree_get_nentries(struct got_tree_object *);
172 /* Get the first tree entry from a tree, or NULL if there is none. */
173 struct got_tree_entry *got_object_tree_get_first_entry(struct got_tree_object *);
175 /* Get the last tree entry from a tree, or NULL if there is none. */
176 struct got_tree_entry *got_object_tree_get_last_entry(struct got_tree_object *);
178 /* Get the entry with the specified index from a tree object. */
179 struct got_tree_entry *got_object_tree_get_entry(
180 struct got_tree_object *, int);
182 /* Find a particular entry in a tree by name. */
183 struct got_tree_entry *got_object_tree_find_entry(
184 struct got_tree_object *, const char *);
186 /* Get the file permission mode of a tree entry. */
187 mode_t got_tree_entry_get_mode(struct got_tree_entry *);
189 /* Get the name of a tree entry. */
190 const char *got_tree_entry_get_name(struct got_tree_entry *);
192 /* Get the object ID of a tree entry. */
193 struct got_object_id *got_tree_entry_get_id(struct got_tree_entry *);
195 /*
196 * Get a string containing the target path of a given a symlink tree entry.
197 * The caller should dispose of it with free(3).
198 */
199 const struct got_error *got_tree_entry_get_symlink_target(char **,
200 struct got_tree_entry *, struct got_repository *);
202 /* Get the index of a tree entry. */
203 int got_tree_entry_get_index(struct got_tree_entry *);
205 /* Get the next tree entry from a tree, or NULL if there is none. */
206 struct got_tree_entry *got_tree_entry_get_next(struct got_tree_object *,
207 struct got_tree_entry *);
209 /* Get the previous tree entry from a tree, or NULL if there is none. */
210 struct got_tree_entry *got_tree_entry_get_prev(struct got_tree_object *,
211 struct got_tree_entry *);
213 /* Return non-zero if the specified tree entry is a Git submodule. */
214 int got_object_tree_entry_is_submodule(struct got_tree_entry *);
216 /* Return non-zero if the specified tree entry is a symbolic link. */
217 int got_object_tree_entry_is_symlink(struct got_tree_entry *);
219 /*
220 * Resolve an in-repository symlink at the specified path in the tree
221 * corresponding to the specified commit. If the specified path is not
222 * a symlink then set *link_target to NULL.
223 * Otherwise, resolve symlinks recursively and return the final link
224 * target path. The caller must dispose of it with free(3).
225 */
226 const struct got_error *got_object_resolve_symlinks(char **, const char *,
227 struct got_object_id *, struct got_repository *);
229 /*
230 * Compare two trees and indicate whether the entry at the specified path
231 * differs between them. The path must not be the root path "/"; the function
232 * got_object_id_cmp() should be used instead to compare the tree roots.
233 */
234 const struct got_error *got_object_tree_path_changed(int *,
235 struct got_tree_object *, struct got_tree_object *, const char *,
236 struct got_repository *);
238 /*
239 * Attempt to open a blob object in a repository.
240 * The size_t argument specifies the block size of an associated read buffer.
241 * The caller must dispose of the blob with got_object_blob_close().
242 */
243 const struct got_error *got_object_open_as_blob(struct got_blob_object **,
244 struct got_repository *, struct got_object_id *, size_t);
246 /* Dispose of a blob object. */
247 const struct got_error *got_object_blob_close(struct got_blob_object *);
249 /*
250 * Get the length of header data at the beginning of the blob's read buffer.
251 * Note that header data is only present upon the first invocation of
252 * got_object_blob_read_block() after the blob is opened.
253 */
254 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
256 /*
257 * Get a pointer to the blob's read buffer.
258 * The read buffer is filled by got_object_blob_read_block().
259 */
260 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
262 /*
263 * Read the next chunk of data from a blob, up to the blob's read buffer
264 * block size. The size_t output argument indicates how many bytes have
265 * been read into the blob's read buffer. Zero bytes will be reported if
266 * all data in the blob has been read.
267 */
268 const struct got_error *got_object_blob_read_block(size_t *,
269 struct got_blob_object *);
271 /* Rewind an open blob's data stream back to the beginning. */
272 void got_object_blob_rewind(struct got_blob_object *);
274 /*
275 * Read the entire content of a blob and write it to the specified file.
276 * Flush and rewind the file as well. Indicate the amount of bytes
277 * written in the size_t output argument, and the number of lines in the
278 * file in the int argument, and line offsets in the off_t argument
279 * (NULL can be passed for any output argument).
280 */
281 const struct got_error *got_object_blob_dump_to_file(off_t *, int *,
282 off_t **, FILE *, struct got_blob_object *);
284 /*
285 * Read the entire content of a blob into a newly allocated string buffer
286 * and terminate it with '\0'. This is intended for blobs which contain a
287 * symlink target path. It should not be used to process arbitrary blobs.
288 * Use got_object_blob_dump_to_file() or got_tree_entry_get_symlink_target()
289 * instead if possible. The caller must dispose of the string with free(3).
290 */
291 const struct got_error *got_object_blob_read_to_str(char **,
292 struct got_blob_object *);
294 /*
295 * Attempt to open a tag object in a repository.
296 * The caller must dispose of the tree with got_tag_object_close().
297 */
298 const struct got_error *got_object_open_as_tag(struct got_tag_object **,
299 struct got_repository *, struct got_object_id *);
301 /* Dispose of a tag object. */
302 void got_object_tag_close(struct got_tag_object *);
304 /* Get the name of a tag. */
305 const char *got_object_tag_get_name(struct got_tag_object *);
307 /* Get type of the object a tag points to. */
308 int got_object_tag_get_object_type(struct got_tag_object *);
310 /*
311 * Get ID of the object a tag points to.
312 * This must not be freed by the caller. Use got_object_id_dup() if needed.
313 */
314 struct got_object_id *got_object_tag_get_object_id(struct got_tag_object *);
317 /* Get the timestamp of the tag. */
318 time_t got_object_tag_get_tagger_time(struct got_tag_object *);
320 /* Get the tag's timestamp's GMT offset. */
321 time_t got_object_tag_get_tagger_gmtoff(struct got_tag_object *);
323 /* Get the author of the tag. */
324 const char *got_object_tag_get_tagger(struct got_tag_object *);
326 /* Get the tag message associated with the tag. */
327 const char *got_object_tag_get_message(struct got_tag_object *);
329 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
330 const char *);
332 /* Create a new tag object in the repository. */
333 const struct got_error *got_object_tag_create(struct got_object_id **,
334 const char *, struct got_object_id *, const char *,
335 time_t, const char *, struct got_repository *);