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 tree of the specified commit.
97 * The caller should dispose of it with free(3).
98 */
99 const struct got_error *got_object_id_by_path(struct got_object_id **,
100 struct got_repository *, struct got_object_id *, const char *);
102 /*
103 * Obtain the type of an object.
104 * Returns one of the GOT_OBJ_TYPE_x values (see above).
105 */
106 const struct got_error *got_object_get_type(int *, struct got_repository *,
107 struct got_object_id *);
109 /*
110 * Attempt to resolve the textual representation of an object ID
111 * to the ID of an existing object in the repository.
112 * The caller should dispose of the ID with free(3).
113 */
114 const struct got_error *got_object_resolve_id_str(struct got_object_id **,
115 struct got_repository *, const char *);
117 /*
118 * Attempt to open a commit object in a repository.
119 * The caller must dispose of the commit with got_object_commit_close().
120 */
121 const struct got_error *got_object_open_as_commit(struct got_commit_object **,
122 struct got_repository *, struct got_object_id *);
124 /* Dispose of a commit object. */
125 void got_object_commit_close(struct got_commit_object *);
127 /* Obtain the ID of the tree created in a commit. */
128 struct got_object_id *got_object_commit_get_tree_id(struct got_commit_object *);
130 /* Obtain the number of parent commits of a commit. */
131 int got_object_commit_get_nparents(struct got_commit_object *);
133 /* Obtain the list of parent commits of a commit. */
134 const struct got_object_id_queue *got_object_commit_get_parent_ids(
135 struct got_commit_object *);
137 /* Get the author's name and email address. */
138 const char *got_object_commit_get_author(struct got_commit_object *);
140 /* Get an author's commit timestamp in UTC. */
141 time_t got_object_commit_get_author_time(struct got_commit_object *);
143 /* Get an author's timezone offset. */
144 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *);
146 /* Get the committer's name and email address. */
147 const char *got_object_commit_get_committer(struct got_commit_object *);
149 /* Get a committer's commit timestamp in UTC. */
150 time_t got_object_commit_get_committer_time(struct got_commit_object *);
152 /* Get a committer's timezone offset. */
153 time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
155 /*
156 * Get the commit log message.
157 * PGP-signatures contained in the log message will be stripped.
158 * The caller must dispose of it with free(3).
159 */
160 const struct got_error *got_object_commit_get_logmsg(char **,
161 struct got_commit_object *);
163 /* Get the raw commit log message.*/
164 const char *got_object_commit_get_logmsg_raw(struct got_commit_object *);
166 /*
167 * Attempt to open a tree object in a repository.
168 * The caller must dispose of the tree with got_object_tree_close().
169 */
170 const struct got_error *got_object_open_as_tree(struct got_tree_object **,
171 struct got_repository *, struct got_object_id *);
173 /* Dispose of a tree object. */
174 void got_object_tree_close(struct got_tree_object *);
176 /* Get the number of entries in this tree object. */
177 int got_object_tree_get_nentries(struct got_tree_object *);
179 /* Get the first tree entry from a tree, or NULL if there is none. */
180 struct got_tree_entry *got_object_tree_get_first_entry(struct got_tree_object *);
182 /* Get the last tree entry from a tree, or NULL if there is none. */
183 struct got_tree_entry *got_object_tree_get_last_entry(struct got_tree_object *);
185 /* Get the entry with the specified index from a tree object. */
186 struct got_tree_entry *got_object_tree_get_entry(
187 struct got_tree_object *, int);
189 /* Find a particular entry in a tree by name. */
190 struct got_tree_entry *got_object_tree_find_entry(
191 struct got_tree_object *, const char *);
193 /* Get the file permission mode of a tree entry. */
194 mode_t got_tree_entry_get_mode(struct got_tree_entry *);
196 /* Get the name of a tree entry. */
197 const char *got_tree_entry_get_name(struct got_tree_entry *);
199 /* Get the object ID of a tree entry. */
200 struct got_object_id *got_tree_entry_get_id(struct got_tree_entry *);
202 /*
203 * Get a string containing the target path of a given a symlink tree entry.
204 * The caller should dispose of it with free(3).
205 */
206 const struct got_error *got_tree_entry_get_symlink_target(char **,
207 struct got_tree_entry *, struct got_repository *);
209 /* Get the index of a tree entry. */
210 int got_tree_entry_get_index(struct got_tree_entry *);
212 /* Get the next tree entry from a tree, or NULL if there is none. */
213 struct got_tree_entry *got_tree_entry_get_next(struct got_tree_object *,
214 struct got_tree_entry *);
216 /* Get the previous tree entry from a tree, or NULL if there is none. */
217 struct got_tree_entry *got_tree_entry_get_prev(struct got_tree_object *,
218 struct got_tree_entry *);
220 /* Return non-zero if the specified tree entry is a Git submodule. */
221 int got_object_tree_entry_is_submodule(struct got_tree_entry *);
223 /* Return non-zero if the specified tree entry is a symbolic link. */
224 int got_object_tree_entry_is_symlink(struct got_tree_entry *);
226 /*
227 * Resolve an in-repository symlink at the specified path in the tree
228 * corresponding to the specified commit. If the specified path is not
229 * a symlink then set *link_target to NULL.
230 * Otherwise, resolve symlinks recursively and return the final link
231 * target path. The caller must dispose of it with free(3).
232 */
233 const struct got_error *got_object_resolve_symlinks(char **, const char *,
234 struct got_object_id *, struct got_repository *);
236 /*
237 * Compare two trees and indicate whether the entry at the specified path
238 * differs between them. The path must not be the root path "/"; the function
239 * got_object_id_cmp() should be used instead to compare the tree roots.
240 */
241 const struct got_error *got_object_tree_path_changed(int *,
242 struct got_tree_object *, struct got_tree_object *, const char *,
243 struct got_repository *);
245 /*
246 * Attempt to open a blob object in a repository.
247 * The size_t argument specifies the block size of an associated read buffer.
248 * The caller must dispose of the blob with got_object_blob_close().
249 */
250 const struct got_error *got_object_open_as_blob(struct got_blob_object **,
251 struct got_repository *, struct got_object_id *, size_t);
253 /* Dispose of a blob object. */
254 const struct got_error *got_object_blob_close(struct got_blob_object *);
256 /*
257 * Get the length of header data at the beginning of the blob's read buffer.
258 * Note that header data is only present upon the first invocation of
259 * got_object_blob_read_block() after the blob is opened.
260 */
261 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
263 /*
264 * Get a pointer to the blob's read buffer.
265 * The read buffer is filled by got_object_blob_read_block().
266 */
267 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
269 /*
270 * Read the next chunk of data from a blob, up to the blob's read buffer
271 * block size. The size_t output argument indicates how many bytes have
272 * been read into the blob's read buffer. Zero bytes will be reported if
273 * all data in the blob has been read.
274 */
275 const struct got_error *got_object_blob_read_block(size_t *,
276 struct got_blob_object *);
278 /* Rewind an open blob's data stream back to the beginning. */
279 void got_object_blob_rewind(struct got_blob_object *);
281 /*
282 * Read the entire content of a blob and write it to the specified file.
283 * Flush and rewind the file as well. Indicate the amount of bytes
284 * written in the size_t output argument, and the number of lines in the
285 * file in the int argument, and line offsets in the off_t argument
286 * (NULL can be passed for any output argument).
287 */
288 const struct got_error *got_object_blob_dump_to_file(off_t *, int *,
289 off_t **, FILE *, struct got_blob_object *);
291 /*
292 * Read the entire content of a blob into a newly allocated string buffer
293 * and terminate it with '\0'. This is intended for blobs which contain a
294 * symlink target path. It should not be used to process arbitrary blobs.
295 * Use got_object_blob_dump_to_file() or got_tree_entry_get_symlink_target()
296 * instead if possible. The caller must dispose of the string with free(3).
297 */
298 const struct got_error *got_object_blob_read_to_str(char **,
299 struct got_blob_object *);
301 /*
302 * Attempt to open a tag object in a repository.
303 * The caller must dispose of the tree with got_tag_object_close().
304 */
305 const struct got_error *got_object_open_as_tag(struct got_tag_object **,
306 struct got_repository *, struct got_object_id *);
308 /* Dispose of a tag object. */
309 void got_object_tag_close(struct got_tag_object *);
311 /* Get the name of a tag. */
312 const char *got_object_tag_get_name(struct got_tag_object *);
314 /* Get type of the object a tag points to. */
315 int got_object_tag_get_object_type(struct got_tag_object *);
317 /*
318 * Get ID of the object a tag points to.
319 * This must not be freed by the caller. Use got_object_id_dup() if needed.
320 */
321 struct got_object_id *got_object_tag_get_object_id(struct got_tag_object *);
324 /* Get the timestamp of the tag. */
325 time_t got_object_tag_get_tagger_time(struct got_tag_object *);
327 /* Get the tag's timestamp's GMT offset. */
328 time_t got_object_tag_get_tagger_gmtoff(struct got_tag_object *);
330 /* Get the author of the tag. */
331 const char *got_object_tag_get_tagger(struct got_tag_object *);
333 /* Get the tag message associated with the tag. */
334 const char *got_object_tag_get_message(struct got_tag_object *);
336 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
337 const char *);
339 /* Create a new tag object in the repository. */
340 const struct got_error *got_object_tag_create(struct got_object_id **,
341 const char *, struct got_object_id *, const char *,
342 time_t, const char *, struct got_repository *);