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;
22 struct got_tree_entry {
23 SIMPLEQ_ENTRY(got_tree_entry) entry;
24 mode_t mode;
25 char *name;
26 struct got_object_id *id;
27 };
29 SIMPLEQ_HEAD(got_tree_entries_queue, got_tree_entry);
31 struct got_tree_entries {
32 int nentries;
33 struct got_tree_entries_queue head;
34 };
36 struct got_object_qid {
37 SIMPLEQ_ENTRY(got_object_qid) entry;
38 struct got_object_id *id;
39 };
41 SIMPLEQ_HEAD(got_object_id_queue, got_object_qid);
43 const struct got_error *got_object_qid_alloc(struct got_object_qid **,
44 struct got_object_id *);
45 void got_object_qid_free(struct got_object_qid *);
47 struct got_commit_object {
48 struct got_object_id *tree_id;
49 unsigned int nparents;
50 struct got_object_id_queue parent_ids;
51 char *author;
52 struct tm tm_author; /* UTC */
53 char *committer;
54 struct tm tm_committer; /* UTC */
55 char *logmsg;
57 int refcnt; /* for internal use only */
58 };
60 /* A generic object. Used as a handle which holds an ID and an object type. */
61 struct got_object;
62 #define GOT_OBJ_TYPE_COMMIT 1
63 #define GOT_OBJ_TYPE_TREE 2
64 #define GOT_OBJ_TYPE_BLOB 3
65 #define GOT_OBJ_TYPE_TAG 4
66 /* 5 is reserved */
67 #define GOT_OBJ_TYPE_OFFSET_DELTA 6
68 #define GOT_OBJ_TYPE_REF_DELTA 7
70 struct got_repository;
72 /*
73 * Obtain a string representation of an object ID. The output depends on
74 * the hash function used by the repository format (currently SHA1).
75 */
76 const struct got_error *got_object_id_str(char **, struct got_object_id *);
78 /*
79 * Compare two object IDs. Return value behaves like memcmp(3).
80 */
81 int got_object_id_cmp(struct got_object_id *, struct got_object_id *);
83 /*
84 * Created a newly allocated copy of an object ID.
85 * The caller should dispose of it with free(3).
86 */
87 struct got_object_id *got_object_id_dup(struct got_object_id *);
89 /*
90 * Get a newly allocated copy of an object's ID.
91 * The caller must treat the ID as read-only and must not call free(3) on it.
92 * Use got_object_id_dup() to get a writable copy.
93 */
94 struct got_object_id *got_object_get_id(struct got_object *);
96 /*
97 * Get a newly allocated copy of an object's ID string.
98 * The caller should dispose of it with free(3).
99 */
100 const struct got_error *got_object_get_id_str(char **, struct got_object *);
102 /*
103 * Get a newly allocated ID of the object which resides at the specified
104 * path in the tree of the specified commit.
105 * The caller should dispose of it with free(3).
106 */
107 const struct got_error *
108 got_object_id_by_path(struct got_object_id **, struct got_repository *,
109 struct got_object_id *, const char *);
111 /*
112 * Obtain the type of an object.
113 * Returns one of the GOT_OBJ_TYPE_x values (see above).
114 */
115 int got_object_get_type(struct got_object *);
117 /*
118 * Attempt to open the object in a repository with the provided ID.
119 * Caller must dispose of it with got_object_close().
120 */
121 const struct got_error *got_object_open(struct got_object **,
122 struct got_repository *, struct got_object_id *);
124 /*
125 * Attempt to map the provided ID string to an object ID and then
126 * attempt to open the object in a repository with this ID.
127 * The form of an ID string depends on the hash function used by the
128 * repository format (currently SHA1).
129 * Caller must dispose of the object with got_object_close().
130 */
131 const struct got_error *got_object_open_by_id_str(struct got_object **,
132 struct got_repository *, const char *);
134 /* Dispose of an object. */
135 void got_object_close(struct got_object *);
137 /*
138 * Attempt to open a commit object in a repository.
139 * The provided object must be of type GOT_OBJ_TYPE_COMMIT.
140 * The caller must dispose of the commit with got_object_commit_close().
141 */
142 const struct got_error *got_object_commit_open(struct got_commit_object **,
143 struct got_repository *, struct got_object *);
145 /* Dispose of a commit object. */
146 void got_object_commit_close(struct got_commit_object *);
148 /*
149 * Attempt to open a tree object in a repository.
150 * The provided object must be of type GOT_OBJ_TYPE_TREE.
151 * The caller must dispose of the tree with got_object_tree_close().
152 */
153 const struct got_error *got_object_tree_open(struct got_tree_object **,
154 struct got_repository *, struct got_object *);
156 /* Dispose of a tree object. */
157 void got_object_tree_close(struct got_tree_object *);
159 /* Get the entries of a tree object. */
160 const struct got_tree_entries *got_object_tree_get_entries(
161 struct got_tree_object *);
163 /*
164 * Compare two trees and indicate whether the entry at the specified path
165 * differs between them. The path must not be the root path "/"; the function
166 * got_object_id_cmp() should be used instead to compare the tree roots.
167 */
168 const struct got_error *got_object_tree_path_changed(int *,
169 struct got_tree_object *, struct got_tree_object *, const char *,
170 struct got_repository *);
172 /*
173 * Attempt to open a blob object in a repository.
174 * The provided object must be of type GOT_OBJ_TYPE_BLOB.
175 * The size_t argument specifies the block size of an associated read buffer.
176 * The caller must dispose of the blob with got_object_blob_close().
177 */
178 const struct got_error *got_object_blob_open(struct got_blob_object **,
179 struct got_repository *, struct got_object *, size_t);
181 /* Dispose of a blob object. */
182 void got_object_blob_close(struct got_blob_object *);
184 /*
185 * Write the string representation of the object ID of a blob to a buffer.
186 * The size_t argument speficies the size of the buffer. In the current
187 * implementation, it must be at least SHA1_DIGEST_STRING_LENGTH bytes.
188 * XXX This is a bad API, use got_object_id_str() instead.
189 */
190 char *got_object_blob_id_str(struct got_blob_object*, char *, size_t);
192 /*
193 * Get the length of header data at the beginning of the blob's read buffer.
194 * Note that header data is only present upon the first invocation of
195 * got_object_blob_read_block() after the blob is opened.
196 */
197 size_t got_object_blob_get_hdrlen(struct got_blob_object *);
199 /*
200 * Get a pointer to the blob's read buffer.
201 * The read buffer is filled by got_object_blob_read_block().
202 */
203 const uint8_t *got_object_blob_get_read_buf(struct got_blob_object *);
205 /*
206 * Read the next chunk of data from a blob, up to the blob's read buffer
207 * block size. The size_t output argument indicates how many bytes have
208 * been read into the blob's read buffer. Zero bytes will be reported if
209 * all data in the blob has been read.
210 */
211 const struct got_error *got_object_blob_read_block(size_t *,
212 struct got_blob_object *);
214 /*
215 * Read the entire content of a blob and write it to the specified file.
216 * Flush and rewind the file as well. Indicate the amount of bytes
217 * written in the first size_t output argument, and the number of lines
218 * in the file in the second size_t output argument (NULL can be passed
219 * for either output argument).
220 */
221 const struct got_error *got_object_blob_dump_to_file(size_t *, size_t *,
222 FILE *, struct got_blob_object *);
224 const struct got_error *
225 got_object_open_as_commit(struct got_commit_object **,
226 struct got_repository *, struct got_object_id *);
227 const struct got_error *
228 got_object_open_as_tree(struct got_tree_object **,
229 struct got_repository *, struct got_object_id *);
230 const struct got_error *
231 got_object_open_as_blob(struct got_blob_object **,
232 struct got_repository *, struct got_object_id *, size_t);
234 const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
235 const char *);