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_tag_object;
22 struct got_commit_object;
24 struct got_tree_entry {
25 SIMPLEQ_ENTRY(got_tree_entry) entry;
26 mode_t mode;
27 char *name;
28 struct got_object_id *id;
29 };
31 SIMPLEQ_HEAD(got_tree_entries_queue, got_tree_entry);
33 struct got_tree_entries {
34 int nentries;
35 struct got_tree_entries_queue head;
36 };
38 struct got_object_qid {
39 SIMPLEQ_ENTRY(got_object_qid) entry;
40 struct got_object_id *id;
41 };
43 SIMPLEQ_HEAD(got_object_id_queue, got_object_qid);
45 const struct got_error *got_object_qid_alloc(struct got_object_qid **,
46 struct got_object_id *);
47 void got_object_qid_free(struct got_object_qid *);
48 void got_object_id_queue_free(struct got_object_id_queue *);
50 /* Object types. */
51 #define GOT_OBJ_TYPE_ANY 0 /* wildcard value at run-time */
52 #define GOT_OBJ_TYPE_COMMIT 1
53 #define GOT_OBJ_TYPE_TREE 2
54 #define GOT_OBJ_TYPE_BLOB 3
55 #define GOT_OBJ_TYPE_TAG 4
56 /* 5 is reserved */
57 #define GOT_OBJ_TYPE_OFFSET_DELTA 6
58 #define GOT_OBJ_TYPE_REF_DELTA 7
60 /*
61 * Labels used in object data.
62 */
64 #define GOT_OBJ_LABEL_COMMIT "commit"
65 #define GOT_OBJ_LABEL_TREE "tree"
66 #define GOT_OBJ_LABEL_BLOB "blob"
67 #define GOT_OBJ_LABEL_TAG "tag"
69 #define GOT_COMMIT_LABEL_TREE "tree "
70 #define GOT_COMMIT_LABEL_PARENT "parent "
71 #define GOT_COMMIT_LABEL_AUTHOR "author "
72 #define GOT_COMMIT_LABEL_COMMITTER "committer "
74 #define GOT_TAG_LABEL_OBJECT "object "
75 #define GOT_TAG_LABEL_TYPE "type "
76 #define GOT_TAG_LABEL_TAG "tag "
77 #define GOT_TAG_LABEL_TAGGER "tagger "
79 struct got_repository;
81 /*
82 * Obtain a string representation of an object ID. The output depends on
83 * the hash function used by the repository format (currently SHA1).
84 */
85 const struct got_error *got_object_id_str(char **, struct got_object_id *);
87 /*
88 * Compare two object IDs. Return value behaves like memcmp(3).
89 */
90 int got_object_id_cmp(const struct got_object_id *,
91 const struct got_object_id *);
93 /*
94 * Created a newly allocated copy of an object ID.
95 * The caller should dispose of it with free(3).
96 */
97 struct got_object_id *got_object_id_dup(struct got_object_id *);
99 /*
100 * Get a newly allocated ID of the object which resides at the specified
101 * path in the tree of the specified commit.
102 * The caller should dispose of it with free(3).
103 */
104 const struct got_error *got_object_id_by_path(struct got_object_id **,
105 struct got_repository *, struct got_object_id *, const char *);
107 /*
108 * Obtain the type of an object.
109 * Returns one of the GOT_OBJ_TYPE_x values (see above).
110 */
111 const struct got_error *got_object_get_type(int *, struct got_repository *,
112 struct got_object_id *);
114 /*
115 * Attempt to resolve the textual representation of an object ID
116 * to the ID of an existing object in the repository.
117 * The caller should dispose of the ID with free(3).
118 */
119 const struct got_error *got_object_resolve_id_str(struct got_object_id **,
120 struct got_repository *, const char *);
122 /*
123 * Attempt to open a commit object in a repository.
124 * The caller must dispose of the commit with got_object_commit_close().
125 */
126 const struct got_error *got_object_open_as_commit(struct got_commit_object **,
127 struct got_repository *, struct got_object_id *);
129 /* Dispose of a commit object. */
130 void got_object_commit_close(struct got_commit_object *);
132 /* Obtain the ID of the tree created in a commit. */
133 struct got_object_id *got_object_commit_get_tree_id(struct got_commit_object *);
135 /* Obtain the number of parent commits of a commit. */
136 int got_object_commit_get_nparents(struct got_commit_object *);
138 /* Obtain the list of parent commits of a commit. */
139 const struct got_object_id_queue *got_object_commit_get_parent_ids(
140 struct got_commit_object *);
142 /* Get the author's name and email address. */
143 const char *got_object_commit_get_author(struct got_commit_object *);
145 /* Get an author's commit timestamp in UTC. */
146 time_t got_object_commit_get_author_time(struct got_commit_object *);
148 /* Get an author's timezone offset. */
149 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *);
151 /* Get the committer's name and email address. */
152 const char *got_object_commit_get_committer(struct got_commit_object *);
154 /* Get a committer's commit timestamp in UTC. */
155 time_t got_object_commit_get_committer_time(struct got_commit_object *);
157 /* Get a committer's timezone offset. */
158 time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
160 /*
161 * Get the commit log message.
162 * PGP-signatures contained in the log message will be stripped.
163 * The caller must dispose of it with free(3).
164 */
165 const struct got_error *got_object_commit_get_logmsg(char **,
166 struct got_commit_object *);
168 /* Get the raw commit log message.*/
169 const char *got_object_commit_get_logmsg_raw(struct got_commit_object *);
171 /*
172 * Attempt to open a tree object in a repository.
173 * The caller must dispose of the tree with got_object_tree_close().
174 */
175 const struct got_error *got_object_open_as_tree(struct got_tree_object **,
176 struct got_repository *, struct got_object_id *);
178 /* Dispose of a tree object. */
179 void got_object_tree_close(struct got_tree_object *);
181 /* Get the entries of a tree object. */
182 const struct got_tree_entries *got_object_tree_get_entries(
183 struct got_tree_object *);
185 /* Find a particular entry in a tree. */
186 const struct got_tree_entry *got_object_tree_find_entry(
187 struct got_tree_object *, const char *);
189 /* Return non-zero if the specified tree entry is a Git submodule. */
190 int got_object_tree_entry_is_submodule(const struct got_tree_entry *);
192 /*
193 * Compare two trees and indicate whether the entry at the specified path
194 * differs between them. The path must not be the root path "/"; the function
195 * got_object_id_cmp() should be used instead to compare the tree roots.
196 */
197 const struct got_error *got_object_tree_path_changed(int *,
198 struct got_tree_object *, struct got_tree_object *, const char *,
199 struct got_repository *);
201 /*
202 * Attempt to open a blob object in a repository.
203 * The size_t argument specifies the block size of an associated read buffer.
204 * The caller must dispose of the blob with got_object_blob_close().
205 */
206 const struct got_error *got_object_open_as_blob(struct got_blob_object **,
207 struct got_repository *, struct got_object_id *, size_t);
209 /* Dispose of a blob object. */
210 const struct got_error *got_object_blob_close(struct got_blob_object *);
212 /*
213 * Get the length of header data at the beginning of the blob's read buffer.
214 * Note that header data is only present upon the first invocation of
215 * got_object_blob_read_block() after the blob is opened.
216 */
217 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
219 /*
220 * Get a pointer to the blob's read buffer.
221 * The read buffer is filled by got_object_blob_read_block().
222 */
223 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
225 /*
226 * Read the next chunk of data from a blob, up to the blob's read buffer
227 * block size. The size_t output argument indicates how many bytes have
228 * been read into the blob's read buffer. Zero bytes will be reported if
229 * all data in the blob has been read.
230 */
231 const struct got_error *got_object_blob_read_block(size_t *,
232 struct got_blob_object *);
234 /*
235 * Read the entire content of a blob and write it to the specified file.
236 * Flush and rewind the file as well. Indicate the amount of bytes
237 * written in the size_t output argument, and the number of lines in the
238 * file in the int argument, and line offsets in the off_t argument
239 * (NULL can be passed for any output argument).
240 */
241 const struct got_error *got_object_blob_dump_to_file(size_t *, int *,
242 off_t **, FILE *, struct got_blob_object *);
244 /*
245 * Attempt to open a tag object in a repository.
246 * The caller must dispose of the tree with got_tag_object_close().
247 */
248 const struct got_error *got_object_open_as_tag(struct got_tag_object **,
249 struct got_repository *, struct got_object_id *);
251 /* Dispose of a tag object. */
252 void got_object_tag_close(struct got_tag_object *);
254 /* Get the name of a tag. */
255 const char *got_object_tag_get_name(struct got_tag_object *);
257 /* Get type of the object a tag points to. */
258 int got_object_tag_get_object_type(struct got_tag_object *);
260 /*
261 * Get ID of the object a tag points to.
262 * This must not be freed by the caller. Use got_object_id_dup() if needed.
263 */
264 struct got_object_id *got_object_tag_get_object_id(struct got_tag_object *);
267 /* Get the timestamp of the tag. */
268 time_t got_object_tag_get_tagger_time(struct got_tag_object *);
270 /* Get the tag's timestamp's GMT offset. */
271 time_t got_object_tag_get_tagger_gmtoff(struct got_tag_object *);
273 /* Get the author of the tag. */
274 const char *got_object_tag_get_tagger(struct got_tag_object *);
276 /* Get the tag message associated with the tag. */
277 const char *got_object_tag_get_message(struct got_tag_object *);
279 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
280 const char *);
282 /* Create a new tag object in the repository. */
283 const struct got_error *got_object_tag_create(struct got_object_id **,
284 const char *, struct got_object_id *, const char *,
285 time_t, const char *, struct got_repository *);