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 *);
49 /* A generic object. Used as a handle which holds an ID and an object type. */
50 struct got_object;
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 struct got_repository;
61 /*
62 * Obtain a string representation of an object ID. The output depends on
63 * the hash function used by the repository format (currently SHA1).
64 */
65 const struct got_error *got_object_id_str(char **, struct got_object_id *);
67 /*
68 * Compare two object IDs. Return value behaves like memcmp(3).
69 */
70 int got_object_id_cmp(const struct got_object_id *,
71 const struct got_object_id *);
73 /*
74 * Created a newly allocated copy of an object ID.
75 * The caller should dispose of it with free(3).
76 */
77 struct got_object_id *got_object_id_dup(struct got_object_id *);
79 /*
80 * Get a newly allocated copy of an object's ID.
81 * The caller must treat the ID as read-only and must not call free(3) on it.
82 * Use got_object_id_dup() to get a writable copy.
83 */
84 struct got_object_id *got_object_get_id(struct got_object *);
86 /*
87 * Get a newly allocated copy of an object's ID string.
88 * The caller should dispose of it with free(3).
89 */
90 const struct got_error *got_object_get_id_str(char **, struct got_object *);
92 /*
93 * Get a newly allocated ID of the object which resides at the specified
94 * path in the tree of the specified commit.
95 * The caller should dispose of it with free(3).
96 */
97 const struct got_error *
98 got_object_id_by_path(struct got_object_id **, struct got_repository *,
99 struct got_object_id *, const char *);
101 /*
102 * Obtain the type of an object.
103 * Returns one of the GOT_OBJ_TYPE_x values (see above).
104 */
105 int got_object_get_type(struct got_object *);
107 /*
108 * Attempt to open the object in a repository with the provided ID.
109 * Caller must dispose of it with got_object_close().
110 */
111 const struct got_error *got_object_open(struct got_object **,
112 struct got_repository *, struct got_object_id *);
114 /*
115 * Attempt to map the provided ID string to an object ID and then
116 * attempt to open the object in a repository with this ID.
117 * The form of an ID string depends on the hash function used by the
118 * repository format (currently SHA1).
119 * Caller must dispose of the object with got_object_close().
120 */
121 const struct got_error *got_object_open_by_id_str(struct got_object **,
122 struct got_repository *, const char *);
124 /* Dispose of an object. */
125 void got_object_close(struct got_object *);
127 /*
128 * Attempt to open a commit object in a repository.
129 * The provided object must be of type GOT_OBJ_TYPE_COMMIT.
130 * The caller must dispose of the commit with got_object_commit_close().
131 */
132 const struct got_error *got_object_commit_open(struct got_commit_object **,
133 struct got_repository *, struct got_object *);
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 an committer's commit timestamp in UTC. */
161 time_t got_object_commit_get_committer_time(struct got_commit_object *);
163 /* Get an committer's timezone offset. */
164 time_t got_object_commit_get_committer_gmtoff(struct got_commit_object *);
166 /* Get the commit log message. */
167 const char *got_object_commit_get_logmsg(struct got_commit_object *);
169 /*
170 * Attempt to open a tree object in a repository.
171 * The provided object must be of type GOT_OBJ_TYPE_TREE.
172 * The caller must dispose of the tree with got_object_tree_close().
173 */
174 const struct got_error *got_object_tree_open(struct got_tree_object **,
175 struct got_repository *, struct got_object *);
177 /* Dispose of a tree object. */
178 void got_object_tree_close(struct got_tree_object *);
180 /* Get the entries of a tree object. */
181 const struct got_tree_entries *got_object_tree_get_entries(
182 struct got_tree_object *);
184 /*
185 * Compare two trees and indicate whether the entry at the specified path
186 * differs between them. The path must not be the root path "/"; the function
187 * got_object_id_cmp() should be used instead to compare the tree roots.
188 */
189 const struct got_error *got_object_tree_path_changed(int *,
190 struct got_tree_object *, struct got_tree_object *, const char *,
191 struct got_repository *);
193 /*
194 * Attempt to open a blob object in a repository.
195 * The provided object must be of type GOT_OBJ_TYPE_BLOB.
196 * The size_t argument specifies the block size of an associated read buffer.
197 * The caller must dispose of the blob with got_object_blob_close().
198 */
199 const struct got_error *got_object_blob_open(struct got_blob_object **,
200 struct got_repository *, struct got_object *, size_t);
202 /* Dispose of a blob object. */
203 void got_object_blob_close(struct got_blob_object *);
205 /*
206 * Write the string representation of the object ID of a blob to a buffer.
207 * The size_t argument speficies the size of the buffer. In the current
208 * implementation, it must be at least SHA1_DIGEST_STRING_LENGTH bytes.
209 * XXX This is a bad API, use got_object_id_str() instead.
210 */
211 char *got_object_blob_id_str(struct got_blob_object*, char *, size_t);
213 /*
214 * Get the length of header data at the beginning of the blob's read buffer.
215 * Note that header data is only present upon the first invocation of
216 * got_object_blob_read_block() after the blob is opened.
217 */
218 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
220 /*
221 * Get a pointer to the blob's read buffer.
222 * The read buffer is filled by got_object_blob_read_block().
223 */
224 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
226 /*
227 * Read the next chunk of data from a blob, up to the blob's read buffer
228 * block size. The size_t output argument indicates how many bytes have
229 * been read into the blob's read buffer. Zero bytes will be reported if
230 * all data in the blob has been read.
231 */
232 const struct got_error *got_object_blob_read_block(size_t *,
233 struct got_blob_object *);
235 /*
236 * Read the entire content of a blob and write it to the specified file.
237 * Flush and rewind the file as well. Indicate the amount of bytes
238 * written in the size_t output argument, and the number of lines in
239 * the file in int argument (NULL can be passed for either output argument).
240 */
241 const struct got_error *got_object_blob_dump_to_file(size_t *, int *,
242 FILE *, struct got_blob_object *);
244 /*
245 * Attempt to open a tag object in a repository.
246 * The provided object must be of type GOT_OBJ_TYPE_TAG.
247 * The caller must dispose of the tree with got_object_tag_close().
248 */
249 const struct got_error *got_object_tag_open(struct got_tag_object **,
250 struct got_repository *, struct got_object *);
252 /* Dispose of a tag object. */
253 void got_object_tag_close(struct got_tag_object *);
255 const struct got_error *
256 got_object_open_as_commit(struct got_commit_object **,
257 struct got_repository *, struct got_object_id *);
258 const struct got_error *
259 got_object_open_as_tree(struct got_tree_object **,
260 struct got_repository *, struct got_object_id *);
261 const struct got_error *
262 got_object_open_as_blob(struct got_blob_object **,
263 struct got_repository *, struct got_object_id *, size_t);
264 const struct got_error *got_object_open_as_tag(struct got_tag_object **,
265 struct got_repository *, struct got_object_id *);
267 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
268 const char *);