Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2020 Ori Bernstein
3 13b2bc37 2022-10-23 stsp * Copyright (c) 2021, 2022 Stefan Sperling <stsp@openbsd.org>
4 13b2bc37 2022-10-23 stsp *
5 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
6 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
7 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
8 13b2bc37 2022-10-23 stsp *
9 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 13b2bc37 2022-10-23 stsp */
17 13b2bc37 2022-10-23 stsp
18 13b2bc37 2022-10-23 stsp #include <sys/types.h>
19 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
20 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
21 13b2bc37 2022-10-23 stsp #include <sys/uio.h>
22 13b2bc37 2022-10-23 stsp
23 13b2bc37 2022-10-23 stsp #include <sha1.h>
24 13b2bc37 2022-10-23 stsp #include <limits.h>
25 13b2bc37 2022-10-23 stsp #include <stdio.h>
26 13b2bc37 2022-10-23 stsp #include <stdint.h>
27 13b2bc37 2022-10-23 stsp #include <stdlib.h>
28 13b2bc37 2022-10-23 stsp #include <string.h>
29 13b2bc37 2022-10-23 stsp #include <time.h>
30 13b2bc37 2022-10-23 stsp #include <imsg.h>
31 13b2bc37 2022-10-23 stsp #include <inttypes.h>
32 13b2bc37 2022-10-23 stsp #include <unistd.h>
33 13b2bc37 2022-10-23 stsp
34 13b2bc37 2022-10-23 stsp #include "got_error.h"
35 13b2bc37 2022-10-23 stsp #include "got_cancel.h"
36 13b2bc37 2022-10-23 stsp #include "got_object.h"
37 13b2bc37 2022-10-23 stsp #include "got_reference.h"
38 13b2bc37 2022-10-23 stsp #include "got_repository_admin.h"
39 13b2bc37 2022-10-23 stsp #include "got_path.h"
40 13b2bc37 2022-10-23 stsp
41 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
42 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
44 13b2bc37 2022-10-23 stsp #include "got_lib_object_idset.h"
45 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
46 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
47 13b2bc37 2022-10-23 stsp #include "got_lib_pack_create.h"
48 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
49 13b2bc37 2022-10-23 stsp
50 13b2bc37 2022-10-23 stsp static const struct got_error *
51 13b2bc37 2022-10-23 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
52 13b2bc37 2022-10-23 stsp off_t base_offset)
53 13b2bc37 2022-10-23 stsp {
54 13b2bc37 2022-10-23 stsp const struct got_error *err;
55 13b2bc37 2022-10-23 stsp int idx;
56 13b2bc37 2022-10-23 stsp
57 13b2bc37 2022-10-23 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
58 13b2bc37 2022-10-23 stsp if (err)
59 13b2bc37 2022-10-23 stsp return err;
60 13b2bc37 2022-10-23 stsp if (idx == -1)
61 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
62 13b2bc37 2022-10-23 stsp
63 13b2bc37 2022-10-23 stsp return got_packidx_get_object_id(base_id, packidx, idx);
64 13b2bc37 2022-10-23 stsp }
65 13b2bc37 2022-10-23 stsp
66 13b2bc37 2022-10-23 stsp struct search_deltas_arg {
67 13b2bc37 2022-10-23 stsp struct got_pack_metavec *v;
68 13b2bc37 2022-10-23 stsp struct got_packidx *packidx;
69 13b2bc37 2022-10-23 stsp struct got_pack *pack;
70 13b2bc37 2022-10-23 stsp struct got_object_idset *idset;
71 13b2bc37 2022-10-23 stsp int delta_cache_fd;
72 13b2bc37 2022-10-23 stsp int ncolored, nfound, ntrees, ncommits;
73 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb;
74 13b2bc37 2022-10-23 stsp void *progress_arg;
75 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl;
76 13b2bc37 2022-10-23 stsp got_cancel_cb cancel_cb;
77 13b2bc37 2022-10-23 stsp void *cancel_arg;
78 13b2bc37 2022-10-23 stsp };
79 13b2bc37 2022-10-23 stsp
80 13b2bc37 2022-10-23 stsp static const struct got_error *
81 13b2bc37 2022-10-23 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
82 13b2bc37 2022-10-23 stsp {
83 13b2bc37 2022-10-23 stsp const struct got_error *err;
84 13b2bc37 2022-10-23 stsp struct search_deltas_arg *a = arg;
85 13b2bc37 2022-10-23 stsp int obj_idx;
86 13b2bc37 2022-10-23 stsp uint8_t *delta_buf = NULL;
87 13b2bc37 2022-10-23 stsp uint64_t base_size, result_size;
88 13b2bc37 2022-10-23 stsp size_t delta_size, delta_compressed_size;
89 13b2bc37 2022-10-23 stsp off_t delta_offset, base_offset;
90 13b2bc37 2022-10-23 stsp struct got_object_id base_id;
91 13b2bc37 2022-10-23 stsp
92 13b2bc37 2022-10-23 stsp if (a->cancel_cb) {
93 13b2bc37 2022-10-23 stsp err = a->cancel_cb(a->cancel_arg);
94 13b2bc37 2022-10-23 stsp if (err)
95 13b2bc37 2022-10-23 stsp return err;
96 13b2bc37 2022-10-23 stsp }
97 13b2bc37 2022-10-23 stsp
98 13b2bc37 2022-10-23 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
99 13b2bc37 2022-10-23 stsp if (obj_idx == -1)
100 13b2bc37 2022-10-23 stsp return NULL; /* object not present in our pack file */
101 13b2bc37 2022-10-23 stsp
102 13b2bc37 2022-10-23 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
103 13b2bc37 2022-10-23 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
104 13b2bc37 2022-10-23 stsp &base_size, &result_size, a->pack, a->packidx, obj_idx);
105 13b2bc37 2022-10-23 stsp if (err) {
106 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_TYPE)
107 13b2bc37 2022-10-23 stsp return NULL; /* object not stored as a delta */
108 13b2bc37 2022-10-23 stsp return err;
109 13b2bc37 2022-10-23 stsp }
110 13b2bc37 2022-10-23 stsp
111 13b2bc37 2022-10-23 stsp /*
112 13b2bc37 2022-10-23 stsp * If this is an offset delta we must determine the base
113 13b2bc37 2022-10-23 stsp * object ID ourselves.
114 13b2bc37 2022-10-23 stsp */
115 13b2bc37 2022-10-23 stsp if (base_offset != 0) {
116 13b2bc37 2022-10-23 stsp err = get_base_object_id(&base_id, a->packidx, base_offset);
117 13b2bc37 2022-10-23 stsp if (err)
118 13b2bc37 2022-10-23 stsp goto done;
119 13b2bc37 2022-10-23 stsp }
120 13b2bc37 2022-10-23 stsp
121 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(a->idset, &base_id)) {
122 13b2bc37 2022-10-23 stsp struct got_pack_meta *m, *base;
123 13b2bc37 2022-10-23 stsp off_t delta_out_offset;
124 13b2bc37 2022-10-23 stsp ssize_t w;
125 13b2bc37 2022-10-23 stsp
126 13b2bc37 2022-10-23 stsp delta_out_offset = lseek(a->delta_cache_fd, 0, SEEK_CUR);
127 13b2bc37 2022-10-23 stsp if (delta_out_offset == -1) {
128 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
129 13b2bc37 2022-10-23 stsp goto done;
130 13b2bc37 2022-10-23 stsp }
131 13b2bc37 2022-10-23 stsp w = write(a->delta_cache_fd, delta_buf, delta_compressed_size);
132 13b2bc37 2022-10-23 stsp if (w != delta_compressed_size) {
133 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
134 13b2bc37 2022-10-23 stsp goto done;
135 13b2bc37 2022-10-23 stsp }
136 13b2bc37 2022-10-23 stsp
137 13b2bc37 2022-10-23 stsp m = got_object_idset_get(a->idset, id);
138 13b2bc37 2022-10-23 stsp if (m == NULL) {
139 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NO_OBJ,
140 13b2bc37 2022-10-23 stsp "delta object not found");
141 13b2bc37 2022-10-23 stsp goto done;
142 13b2bc37 2022-10-23 stsp }
143 13b2bc37 2022-10-23 stsp
144 13b2bc37 2022-10-23 stsp base = got_object_idset_get(a->idset, &base_id);
145 13b2bc37 2022-10-23 stsp if (m == NULL) {
146 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NO_OBJ,
147 13b2bc37 2022-10-23 stsp "delta base object not found");
148 13b2bc37 2022-10-23 stsp goto done;
149 13b2bc37 2022-10-23 stsp }
150 13b2bc37 2022-10-23 stsp
151 13b2bc37 2022-10-23 stsp m->base_obj_id = got_object_id_dup(&base_id);
152 13b2bc37 2022-10-23 stsp if (m->base_obj_id == NULL) {
153 13b2bc37 2022-10-23 stsp err = got_error_from_errno("got_object_id_dup");
154 13b2bc37 2022-10-23 stsp goto done;
155 13b2bc37 2022-10-23 stsp }
156 13b2bc37 2022-10-23 stsp
157 13b2bc37 2022-10-23 stsp m->prev = base;
158 13b2bc37 2022-10-23 stsp m->size = result_size;
159 13b2bc37 2022-10-23 stsp m->delta_len = delta_size;
160 13b2bc37 2022-10-23 stsp m->delta_compressed_len = delta_compressed_size;
161 13b2bc37 2022-10-23 stsp m->reused_delta_offset = delta_offset;
162 13b2bc37 2022-10-23 stsp m->delta_offset = delta_out_offset;
163 13b2bc37 2022-10-23 stsp
164 13b2bc37 2022-10-23 stsp err = got_pack_add_meta(m, a->v);
165 13b2bc37 2022-10-23 stsp if (err)
166 13b2bc37 2022-10-23 stsp goto done;
167 13b2bc37 2022-10-23 stsp
168 13b2bc37 2022-10-23 stsp err = got_pack_report_progress(a->progress_cb, a->progress_arg,
169 13b2bc37 2022-10-23 stsp a->rl, a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
170 13b2bc37 2022-10-23 stsp got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
171 13b2bc37 2022-10-23 stsp if (err)
172 13b2bc37 2022-10-23 stsp goto done;
173 13b2bc37 2022-10-23 stsp }
174 13b2bc37 2022-10-23 stsp done:
175 13b2bc37 2022-10-23 stsp free(delta_buf);
176 13b2bc37 2022-10-23 stsp return err;
177 13b2bc37 2022-10-23 stsp }
178 13b2bc37 2022-10-23 stsp
179 13b2bc37 2022-10-23 stsp const struct got_error *
180 13b2bc37 2022-10-23 stsp got_pack_search_deltas(struct got_pack_metavec *v,
181 13b2bc37 2022-10-23 stsp struct got_object_idset *idset, int delta_cache_fd,
182 13b2bc37 2022-10-23 stsp int ncolored, int nfound, int ntrees, int ncommits,
183 13b2bc37 2022-10-23 stsp struct got_repository *repo,
184 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
185 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
186 13b2bc37 2022-10-23 stsp {
187 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
188 13b2bc37 2022-10-23 stsp struct got_packidx *packidx;
189 13b2bc37 2022-10-23 stsp struct got_pack *pack;
190 13b2bc37 2022-10-23 stsp struct search_deltas_arg sda;
191 13b2bc37 2022-10-23 stsp
192 13b2bc37 2022-10-23 stsp err = got_pack_find_pack_for_reuse(&packidx, repo);
193 13b2bc37 2022-10-23 stsp if (err)
194 13b2bc37 2022-10-23 stsp return err;
195 13b2bc37 2022-10-23 stsp
196 13b2bc37 2022-10-23 stsp if (packidx == NULL)
197 13b2bc37 2022-10-23 stsp return NULL;
198 13b2bc37 2022-10-23 stsp
199 13b2bc37 2022-10-23 stsp err = got_pack_cache_pack_for_packidx(&pack, packidx, repo);
200 13b2bc37 2022-10-23 stsp if (err)
201 13b2bc37 2022-10-23 stsp return err;
202 13b2bc37 2022-10-23 stsp
203 13b2bc37 2022-10-23 stsp memset(&sda, 0, sizeof(sda));
204 13b2bc37 2022-10-23 stsp sda.v = v;
205 13b2bc37 2022-10-23 stsp sda.idset = idset;
206 13b2bc37 2022-10-23 stsp sda.pack = pack;
207 13b2bc37 2022-10-23 stsp sda.packidx = packidx;
208 13b2bc37 2022-10-23 stsp sda.delta_cache_fd = delta_cache_fd;
209 13b2bc37 2022-10-23 stsp sda.ncolored = ncolored;
210 13b2bc37 2022-10-23 stsp sda.nfound = nfound;
211 13b2bc37 2022-10-23 stsp sda.ntrees = ntrees;
212 13b2bc37 2022-10-23 stsp sda.ncommits = ncommits;
213 13b2bc37 2022-10-23 stsp sda.progress_cb = progress_cb;
214 13b2bc37 2022-10-23 stsp sda.progress_arg = progress_arg;
215 13b2bc37 2022-10-23 stsp sda.rl = rl;
216 13b2bc37 2022-10-23 stsp sda.cancel_cb = cancel_cb;
217 13b2bc37 2022-10-23 stsp sda.cancel_arg = cancel_arg;
218 13b2bc37 2022-10-23 stsp return got_object_idset_for_each(idset, search_delta_for_object, &sda);
219 13b2bc37 2022-10-23 stsp }
220 13b2bc37 2022-10-23 stsp
221 13b2bc37 2022-10-23 stsp const struct got_error *
222 13b2bc37 2022-10-23 stsp got_pack_load_packed_object_ids(int *found_all_objects,
223 13b2bc37 2022-10-23 stsp struct got_object_id **ours, int nours,
224 13b2bc37 2022-10-23 stsp struct got_object_id **theirs, int ntheirs,
225 13b2bc37 2022-10-23 stsp int want_meta, uint32_t seed, struct got_object_idset *idset,
226 13b2bc37 2022-10-23 stsp struct got_object_idset *idset_exclude, int loose_obj_only,
227 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_packidx *packidx,
228 13b2bc37 2022-10-23 stsp int *ncolored, int *nfound, int *ntrees,
229 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
230 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
231 13b2bc37 2022-10-23 stsp {
232 13b2bc37 2022-10-23 stsp /* We do not need this optimized traversal while using direct I/O. */
233 13b2bc37 2022-10-23 stsp *found_all_objects = 0;
234 13b2bc37 2022-10-23 stsp return NULL;
235 13b2bc37 2022-10-23 stsp }
236 13b2bc37 2022-10-23 stsp
237 13b2bc37 2022-10-23 stsp const struct got_error *
238 13b2bc37 2022-10-23 stsp got_pack_paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
239 13b2bc37 2022-10-23 stsp struct got_object_idset *keep, struct got_object_idset *drop,
240 13b2bc37 2022-10-23 stsp struct got_object_idset *skip, struct got_repository *repo,
241 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
242 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
243 13b2bc37 2022-10-23 stsp {
244 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
245 13b2bc37 2022-10-23 stsp struct got_commit_object *commit = NULL;
246 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
247 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
248 13b2bc37 2022-10-23 stsp const struct got_object_id_queue *parents;
249 13b2bc37 2022-10-23 stsp struct got_object_qid *qid = NULL;
250 13b2bc37 2022-10-23 stsp int nqueued = nids, nskip = 0;
251 13b2bc37 2022-10-23 stsp
252 13b2bc37 2022-10-23 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
253 13b2bc37 2022-10-23 stsp intptr_t color;
254 13b2bc37 2022-10-23 stsp
255 13b2bc37 2022-10-23 stsp if (cancel_cb) {
256 13b2bc37 2022-10-23 stsp err = cancel_cb(cancel_arg);
257 13b2bc37 2022-10-23 stsp if (err)
258 13b2bc37 2022-10-23 stsp break;
259 13b2bc37 2022-10-23 stsp }
260 13b2bc37 2022-10-23 stsp
261 13b2bc37 2022-10-23 stsp qid = STAILQ_FIRST(ids);
262 13b2bc37 2022-10-23 stsp STAILQ_REMOVE_HEAD(ids, entry);
263 13b2bc37 2022-10-23 stsp nqueued--;
264 13b2bc37 2022-10-23 stsp color = (intptr_t)qid->data;
265 13b2bc37 2022-10-23 stsp if (color == COLOR_SKIP)
266 13b2bc37 2022-10-23 stsp nskip--;
267 13b2bc37 2022-10-23 stsp
268 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(skip, &qid->id)) {
269 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
270 13b2bc37 2022-10-23 stsp qid = NULL;
271 13b2bc37 2022-10-23 stsp continue;
272 13b2bc37 2022-10-23 stsp }
273 13b2bc37 2022-10-23 stsp if (color == COLOR_KEEP &&
274 13b2bc37 2022-10-23 stsp got_object_idset_contains(keep, &qid->id)) {
275 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
276 13b2bc37 2022-10-23 stsp qid = NULL;
277 13b2bc37 2022-10-23 stsp continue;
278 13b2bc37 2022-10-23 stsp }
279 13b2bc37 2022-10-23 stsp if (color == COLOR_DROP &&
280 13b2bc37 2022-10-23 stsp got_object_idset_contains(drop, &qid->id)) {
281 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
282 13b2bc37 2022-10-23 stsp qid = NULL;
283 13b2bc37 2022-10-23 stsp continue;
284 13b2bc37 2022-10-23 stsp }
285 13b2bc37 2022-10-23 stsp
286 13b2bc37 2022-10-23 stsp switch (color) {
287 13b2bc37 2022-10-23 stsp case COLOR_KEEP:
288 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(drop, &qid->id)) {
289 13b2bc37 2022-10-23 stsp err = got_pack_paint_commit(qid, COLOR_SKIP);
290 13b2bc37 2022-10-23 stsp if (err)
291 13b2bc37 2022-10-23 stsp goto done;
292 13b2bc37 2022-10-23 stsp } else
293 13b2bc37 2022-10-23 stsp (*ncolored)++;
294 13b2bc37 2022-10-23 stsp err = got_object_idset_add(keep, &qid->id, NULL);
295 13b2bc37 2022-10-23 stsp if (err)
296 13b2bc37 2022-10-23 stsp goto done;
297 13b2bc37 2022-10-23 stsp break;
298 13b2bc37 2022-10-23 stsp case COLOR_DROP:
299 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(keep, &qid->id)) {
300 13b2bc37 2022-10-23 stsp err = got_pack_paint_commit(qid, COLOR_SKIP);
301 13b2bc37 2022-10-23 stsp if (err)
302 13b2bc37 2022-10-23 stsp goto done;
303 13b2bc37 2022-10-23 stsp } else
304 13b2bc37 2022-10-23 stsp (*ncolored)++;
305 13b2bc37 2022-10-23 stsp err = got_object_idset_add(drop, &qid->id, NULL);
306 13b2bc37 2022-10-23 stsp if (err)
307 13b2bc37 2022-10-23 stsp goto done;
308 13b2bc37 2022-10-23 stsp break;
309 13b2bc37 2022-10-23 stsp case COLOR_SKIP:
310 13b2bc37 2022-10-23 stsp if (!got_object_idset_contains(skip, &qid->id)) {
311 13b2bc37 2022-10-23 stsp err = got_object_idset_add(skip, &qid->id,
312 13b2bc37 2022-10-23 stsp NULL);
313 13b2bc37 2022-10-23 stsp if (err)
314 13b2bc37 2022-10-23 stsp goto done;
315 13b2bc37 2022-10-23 stsp }
316 13b2bc37 2022-10-23 stsp break;
317 13b2bc37 2022-10-23 stsp default:
318 13b2bc37 2022-10-23 stsp /* should not happen */
319 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
320 13b2bc37 2022-10-23 stsp "%s invalid commit color %"PRIdPTR, __func__,
321 13b2bc37 2022-10-23 stsp color);
322 13b2bc37 2022-10-23 stsp goto done;
323 13b2bc37 2022-10-23 stsp }
324 13b2bc37 2022-10-23 stsp
325 13b2bc37 2022-10-23 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
326 13b2bc37 2022-10-23 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
327 13b2bc37 2022-10-23 stsp if (err)
328 13b2bc37 2022-10-23 stsp break;
329 13b2bc37 2022-10-23 stsp
330 13b2bc37 2022-10-23 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
331 13b2bc37 2022-10-23 stsp if (err)
332 13b2bc37 2022-10-23 stsp break;
333 13b2bc37 2022-10-23 stsp
334 13b2bc37 2022-10-23 stsp parents = got_object_commit_get_parent_ids(commit);
335 13b2bc37 2022-10-23 stsp if (parents) {
336 13b2bc37 2022-10-23 stsp struct got_object_qid *pid;
337 13b2bc37 2022-10-23 stsp color = (intptr_t)qid->data;
338 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(pid, parents, entry) {
339 13b2bc37 2022-10-23 stsp err = got_pack_queue_commit_id(ids, &pid->id,
340 13b2bc37 2022-10-23 stsp color, repo);
341 13b2bc37 2022-10-23 stsp if (err)
342 13b2bc37 2022-10-23 stsp break;
343 13b2bc37 2022-10-23 stsp nqueued++;
344 13b2bc37 2022-10-23 stsp if (color == COLOR_SKIP)
345 13b2bc37 2022-10-23 stsp nskip++;
346 13b2bc37 2022-10-23 stsp }
347 13b2bc37 2022-10-23 stsp }
348 13b2bc37 2022-10-23 stsp
349 13b2bc37 2022-10-23 stsp if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
350 13b2bc37 2022-10-23 stsp /*
351 13b2bc37 2022-10-23 stsp * We now know that at least one pack file exists.
352 13b2bc37 2022-10-23 stsp * Pin a suitable pack to ensure it remains cached
353 13b2bc37 2022-10-23 stsp * while we are churning through commit history.
354 13b2bc37 2022-10-23 stsp */
355 13b2bc37 2022-10-23 stsp if (packidx == NULL) {
356 13b2bc37 2022-10-23 stsp err = got_pack_find_pack_for_commit_painting(
357 13b2bc37 2022-10-23 stsp &packidx, ids, nqueued, repo);
358 13b2bc37 2022-10-23 stsp if (err)
359 13b2bc37 2022-10-23 stsp goto done;
360 13b2bc37 2022-10-23 stsp }
361 13b2bc37 2022-10-23 stsp if (packidx != NULL) {
362 13b2bc37 2022-10-23 stsp err = got_pack_cache_pack_for_packidx(&pack,
363 13b2bc37 2022-10-23 stsp packidx, repo);
364 13b2bc37 2022-10-23 stsp if (err)
365 13b2bc37 2022-10-23 stsp goto done;
366 13b2bc37 2022-10-23 stsp err = got_repo_pin_pack(repo, packidx, pack);
367 13b2bc37 2022-10-23 stsp if (err)
368 13b2bc37 2022-10-23 stsp goto done;
369 13b2bc37 2022-10-23 stsp }
370 13b2bc37 2022-10-23 stsp }
371 13b2bc37 2022-10-23 stsp
372 13b2bc37 2022-10-23 stsp got_object_commit_close(commit);
373 13b2bc37 2022-10-23 stsp commit = NULL;
374 13b2bc37 2022-10-23 stsp
375 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
376 13b2bc37 2022-10-23 stsp qid = NULL;
377 13b2bc37 2022-10-23 stsp }
378 13b2bc37 2022-10-23 stsp done:
379 13b2bc37 2022-10-23 stsp if (commit)
380 13b2bc37 2022-10-23 stsp got_object_commit_close(commit);
381 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
382 13b2bc37 2022-10-23 stsp got_repo_unpin_pack(repo);
383 13b2bc37 2022-10-23 stsp return err;
384 13b2bc37 2022-10-23 stsp }