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 ncolored, nfound, ntrees, ncommits;
72 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb;
73 13b2bc37 2022-10-23 stsp void *progress_arg;
74 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl;
75 13b2bc37 2022-10-23 stsp got_cancel_cb cancel_cb;
76 13b2bc37 2022-10-23 stsp void *cancel_arg;
77 13b2bc37 2022-10-23 stsp };
78 13b2bc37 2022-10-23 stsp
79 13b2bc37 2022-10-23 stsp static const struct got_error *
80 13b2bc37 2022-10-23 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
81 13b2bc37 2022-10-23 stsp {
82 13b2bc37 2022-10-23 stsp const struct got_error *err;
83 13b2bc37 2022-10-23 stsp struct search_deltas_arg *a = arg;
84 13b2bc37 2022-10-23 stsp int obj_idx;
85 13b2bc37 2022-10-23 stsp uint8_t *delta_buf = NULL;
86 13b2bc37 2022-10-23 stsp uint64_t base_size, result_size;
87 13b2bc37 2022-10-23 stsp size_t delta_size, delta_compressed_size;
88 24b7de1c 2022-12-03 stsp off_t delta_offset, delta_data_offset, base_offset;
89 13b2bc37 2022-10-23 stsp struct got_object_id base_id;
90 13b2bc37 2022-10-23 stsp
91 13b2bc37 2022-10-23 stsp if (a->cancel_cb) {
92 13b2bc37 2022-10-23 stsp err = a->cancel_cb(a->cancel_arg);
93 13b2bc37 2022-10-23 stsp if (err)
94 13b2bc37 2022-10-23 stsp return err;
95 13b2bc37 2022-10-23 stsp }
96 13b2bc37 2022-10-23 stsp
97 13b2bc37 2022-10-23 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
98 13b2bc37 2022-10-23 stsp if (obj_idx == -1)
99 13b2bc37 2022-10-23 stsp return NULL; /* object not present in our pack file */
100 13b2bc37 2022-10-23 stsp
101 13b2bc37 2022-10-23 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
102 24b7de1c 2022-12-03 stsp &delta_compressed_size, &delta_offset, &delta_data_offset,
103 24b7de1c 2022-12-03 stsp &base_offset, &base_id, &base_size, &result_size,
104 24b7de1c 2022-12-03 stsp 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
124 13b2bc37 2022-10-23 stsp m = got_object_idset_get(a->idset, id);
125 13b2bc37 2022-10-23 stsp if (m == NULL) {
126 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NO_OBJ,
127 13b2bc37 2022-10-23 stsp "delta object not found");
128 13b2bc37 2022-10-23 stsp goto done;
129 13b2bc37 2022-10-23 stsp }
130 13b2bc37 2022-10-23 stsp
131 13b2bc37 2022-10-23 stsp base = got_object_idset_get(a->idset, &base_id);
132 13b2bc37 2022-10-23 stsp if (m == NULL) {
133 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NO_OBJ,
134 13b2bc37 2022-10-23 stsp "delta base object not found");
135 13b2bc37 2022-10-23 stsp goto done;
136 13b2bc37 2022-10-23 stsp }
137 13b2bc37 2022-10-23 stsp
138 13b2bc37 2022-10-23 stsp m->base_obj_id = got_object_id_dup(&base_id);
139 13b2bc37 2022-10-23 stsp if (m->base_obj_id == NULL) {
140 13b2bc37 2022-10-23 stsp err = got_error_from_errno("got_object_id_dup");
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 m->prev = base;
145 13b2bc37 2022-10-23 stsp m->size = result_size;
146 13b2bc37 2022-10-23 stsp m->delta_len = delta_size;
147 13b2bc37 2022-10-23 stsp m->delta_compressed_len = delta_compressed_size;
148 24b7de1c 2022-12-03 stsp m->reused_delta_offset = delta_data_offset;
149 24b7de1c 2022-12-03 stsp m->delta_offset = 0;
150 13b2bc37 2022-10-23 stsp
151 13b2bc37 2022-10-23 stsp err = got_pack_add_meta(m, a->v);
152 13b2bc37 2022-10-23 stsp if (err)
153 13b2bc37 2022-10-23 stsp goto done;
154 13b2bc37 2022-10-23 stsp
155 13b2bc37 2022-10-23 stsp err = got_pack_report_progress(a->progress_cb, a->progress_arg,
156 13b2bc37 2022-10-23 stsp a->rl, a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
157 13b2bc37 2022-10-23 stsp got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
158 13b2bc37 2022-10-23 stsp if (err)
159 13b2bc37 2022-10-23 stsp goto done;
160 13b2bc37 2022-10-23 stsp }
161 13b2bc37 2022-10-23 stsp done:
162 13b2bc37 2022-10-23 stsp free(delta_buf);
163 13b2bc37 2022-10-23 stsp return err;
164 13b2bc37 2022-10-23 stsp }
165 13b2bc37 2022-10-23 stsp
166 13b2bc37 2022-10-23 stsp const struct got_error *
167 24b7de1c 2022-12-03 stsp got_pack_search_deltas(struct got_packidx **packidx, struct got_pack **pack,
168 24b7de1c 2022-12-03 stsp struct got_pack_metavec *v, struct got_object_idset *idset,
169 13b2bc37 2022-10-23 stsp int ncolored, int nfound, int ntrees, int ncommits,
170 13b2bc37 2022-10-23 stsp struct got_repository *repo,
171 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
172 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
173 13b2bc37 2022-10-23 stsp {
174 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
175 13b2bc37 2022-10-23 stsp struct search_deltas_arg sda;
176 13b2bc37 2022-10-23 stsp
177 24b7de1c 2022-12-03 stsp *packidx = NULL;
178 24b7de1c 2022-12-03 stsp *pack = NULL;
179 24b7de1c 2022-12-03 stsp
180 24b7de1c 2022-12-03 stsp err = got_pack_find_pack_for_reuse(packidx, repo);
181 13b2bc37 2022-10-23 stsp if (err)
182 13b2bc37 2022-10-23 stsp return err;
183 13b2bc37 2022-10-23 stsp
184 24b7de1c 2022-12-03 stsp if (*packidx == NULL)
185 13b2bc37 2022-10-23 stsp return NULL;
186 13b2bc37 2022-10-23 stsp
187 24b7de1c 2022-12-03 stsp err = got_pack_cache_pack_for_packidx(pack, *packidx, repo);
188 13b2bc37 2022-10-23 stsp if (err)
189 13b2bc37 2022-10-23 stsp return err;
190 13b2bc37 2022-10-23 stsp
191 13b2bc37 2022-10-23 stsp memset(&sda, 0, sizeof(sda));
192 13b2bc37 2022-10-23 stsp sda.v = v;
193 13b2bc37 2022-10-23 stsp sda.idset = idset;
194 24b7de1c 2022-12-03 stsp sda.pack = *pack;
195 24b7de1c 2022-12-03 stsp sda.packidx = *packidx;
196 13b2bc37 2022-10-23 stsp sda.ncolored = ncolored;
197 13b2bc37 2022-10-23 stsp sda.nfound = nfound;
198 13b2bc37 2022-10-23 stsp sda.ntrees = ntrees;
199 13b2bc37 2022-10-23 stsp sda.ncommits = ncommits;
200 13b2bc37 2022-10-23 stsp sda.progress_cb = progress_cb;
201 13b2bc37 2022-10-23 stsp sda.progress_arg = progress_arg;
202 13b2bc37 2022-10-23 stsp sda.rl = rl;
203 13b2bc37 2022-10-23 stsp sda.cancel_cb = cancel_cb;
204 13b2bc37 2022-10-23 stsp sda.cancel_arg = cancel_arg;
205 13b2bc37 2022-10-23 stsp return got_object_idset_for_each(idset, search_delta_for_object, &sda);
206 13b2bc37 2022-10-23 stsp }
207 13b2bc37 2022-10-23 stsp
208 13b2bc37 2022-10-23 stsp const struct got_error *
209 13b2bc37 2022-10-23 stsp got_pack_load_packed_object_ids(int *found_all_objects,
210 13b2bc37 2022-10-23 stsp struct got_object_id **ours, int nours,
211 13b2bc37 2022-10-23 stsp struct got_object_id **theirs, int ntheirs,
212 13b2bc37 2022-10-23 stsp int want_meta, uint32_t seed, struct got_object_idset *idset,
213 13b2bc37 2022-10-23 stsp struct got_object_idset *idset_exclude, int loose_obj_only,
214 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_packidx *packidx,
215 13b2bc37 2022-10-23 stsp int *ncolored, int *nfound, int *ntrees,
216 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
217 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
218 13b2bc37 2022-10-23 stsp {
219 13b2bc37 2022-10-23 stsp /* We do not need this optimized traversal while using direct I/O. */
220 13b2bc37 2022-10-23 stsp *found_all_objects = 0;
221 13b2bc37 2022-10-23 stsp return NULL;
222 13b2bc37 2022-10-23 stsp }
223 13b2bc37 2022-10-23 stsp
224 13b2bc37 2022-10-23 stsp const struct got_error *
225 13b2bc37 2022-10-23 stsp got_pack_paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
226 13b2bc37 2022-10-23 stsp struct got_object_idset *keep, struct got_object_idset *drop,
227 13b2bc37 2022-10-23 stsp struct got_object_idset *skip, struct got_repository *repo,
228 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
229 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
230 13b2bc37 2022-10-23 stsp {
231 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
232 13b2bc37 2022-10-23 stsp struct got_commit_object *commit = NULL;
233 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
234 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
235 13b2bc37 2022-10-23 stsp const struct got_object_id_queue *parents;
236 13b2bc37 2022-10-23 stsp struct got_object_qid *qid = NULL;
237 13b2bc37 2022-10-23 stsp int nqueued = nids, nskip = 0;
238 13b2bc37 2022-10-23 stsp
239 13b2bc37 2022-10-23 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
240 13b2bc37 2022-10-23 stsp intptr_t color;
241 13b2bc37 2022-10-23 stsp
242 13b2bc37 2022-10-23 stsp if (cancel_cb) {
243 13b2bc37 2022-10-23 stsp err = cancel_cb(cancel_arg);
244 13b2bc37 2022-10-23 stsp if (err)
245 13b2bc37 2022-10-23 stsp break;
246 13b2bc37 2022-10-23 stsp }
247 13b2bc37 2022-10-23 stsp
248 13b2bc37 2022-10-23 stsp qid = STAILQ_FIRST(ids);
249 13b2bc37 2022-10-23 stsp STAILQ_REMOVE_HEAD(ids, entry);
250 13b2bc37 2022-10-23 stsp nqueued--;
251 13b2bc37 2022-10-23 stsp color = (intptr_t)qid->data;
252 13b2bc37 2022-10-23 stsp if (color == COLOR_SKIP)
253 13b2bc37 2022-10-23 stsp nskip--;
254 13b2bc37 2022-10-23 stsp
255 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(skip, &qid->id)) {
256 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
257 13b2bc37 2022-10-23 stsp qid = NULL;
258 13b2bc37 2022-10-23 stsp continue;
259 13b2bc37 2022-10-23 stsp }
260 13b2bc37 2022-10-23 stsp if (color == COLOR_KEEP &&
261 13b2bc37 2022-10-23 stsp got_object_idset_contains(keep, &qid->id)) {
262 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
263 13b2bc37 2022-10-23 stsp qid = NULL;
264 13b2bc37 2022-10-23 stsp continue;
265 13b2bc37 2022-10-23 stsp }
266 13b2bc37 2022-10-23 stsp if (color == COLOR_DROP &&
267 13b2bc37 2022-10-23 stsp got_object_idset_contains(drop, &qid->id)) {
268 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
269 13b2bc37 2022-10-23 stsp qid = NULL;
270 13b2bc37 2022-10-23 stsp continue;
271 13b2bc37 2022-10-23 stsp }
272 13b2bc37 2022-10-23 stsp
273 13b2bc37 2022-10-23 stsp switch (color) {
274 13b2bc37 2022-10-23 stsp case COLOR_KEEP:
275 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(drop, &qid->id)) {
276 13b2bc37 2022-10-23 stsp err = got_pack_paint_commit(qid, COLOR_SKIP);
277 13b2bc37 2022-10-23 stsp if (err)
278 13b2bc37 2022-10-23 stsp goto done;
279 13b2bc37 2022-10-23 stsp } else
280 13b2bc37 2022-10-23 stsp (*ncolored)++;
281 13b2bc37 2022-10-23 stsp err = got_object_idset_add(keep, &qid->id, NULL);
282 13b2bc37 2022-10-23 stsp if (err)
283 13b2bc37 2022-10-23 stsp goto done;
284 13b2bc37 2022-10-23 stsp break;
285 13b2bc37 2022-10-23 stsp case COLOR_DROP:
286 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(keep, &qid->id)) {
287 13b2bc37 2022-10-23 stsp err = got_pack_paint_commit(qid, COLOR_SKIP);
288 13b2bc37 2022-10-23 stsp if (err)
289 13b2bc37 2022-10-23 stsp goto done;
290 13b2bc37 2022-10-23 stsp } else
291 13b2bc37 2022-10-23 stsp (*ncolored)++;
292 13b2bc37 2022-10-23 stsp err = got_object_idset_add(drop, &qid->id, NULL);
293 13b2bc37 2022-10-23 stsp if (err)
294 13b2bc37 2022-10-23 stsp goto done;
295 13b2bc37 2022-10-23 stsp break;
296 13b2bc37 2022-10-23 stsp case COLOR_SKIP:
297 13b2bc37 2022-10-23 stsp if (!got_object_idset_contains(skip, &qid->id)) {
298 13b2bc37 2022-10-23 stsp err = got_object_idset_add(skip, &qid->id,
299 13b2bc37 2022-10-23 stsp NULL);
300 13b2bc37 2022-10-23 stsp if (err)
301 13b2bc37 2022-10-23 stsp goto done;
302 13b2bc37 2022-10-23 stsp }
303 13b2bc37 2022-10-23 stsp break;
304 13b2bc37 2022-10-23 stsp default:
305 13b2bc37 2022-10-23 stsp /* should not happen */
306 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
307 13b2bc37 2022-10-23 stsp "%s invalid commit color %"PRIdPTR, __func__,
308 13b2bc37 2022-10-23 stsp color);
309 13b2bc37 2022-10-23 stsp goto done;
310 13b2bc37 2022-10-23 stsp }
311 13b2bc37 2022-10-23 stsp
312 13b2bc37 2022-10-23 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
313 13b2bc37 2022-10-23 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
314 13b2bc37 2022-10-23 stsp if (err)
315 13b2bc37 2022-10-23 stsp break;
316 13b2bc37 2022-10-23 stsp
317 13b2bc37 2022-10-23 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
318 13b2bc37 2022-10-23 stsp if (err)
319 13b2bc37 2022-10-23 stsp break;
320 13b2bc37 2022-10-23 stsp
321 13b2bc37 2022-10-23 stsp parents = got_object_commit_get_parent_ids(commit);
322 13b2bc37 2022-10-23 stsp if (parents) {
323 13b2bc37 2022-10-23 stsp struct got_object_qid *pid;
324 13b2bc37 2022-10-23 stsp color = (intptr_t)qid->data;
325 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(pid, parents, entry) {
326 13b2bc37 2022-10-23 stsp err = got_pack_queue_commit_id(ids, &pid->id,
327 13b2bc37 2022-10-23 stsp color, repo);
328 13b2bc37 2022-10-23 stsp if (err)
329 13b2bc37 2022-10-23 stsp break;
330 13b2bc37 2022-10-23 stsp nqueued++;
331 13b2bc37 2022-10-23 stsp if (color == COLOR_SKIP)
332 13b2bc37 2022-10-23 stsp nskip++;
333 13b2bc37 2022-10-23 stsp }
334 13b2bc37 2022-10-23 stsp }
335 13b2bc37 2022-10-23 stsp
336 13b2bc37 2022-10-23 stsp if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
337 13b2bc37 2022-10-23 stsp /*
338 13b2bc37 2022-10-23 stsp * We now know that at least one pack file exists.
339 13b2bc37 2022-10-23 stsp * Pin a suitable pack to ensure it remains cached
340 13b2bc37 2022-10-23 stsp * while we are churning through commit history.
341 13b2bc37 2022-10-23 stsp */
342 13b2bc37 2022-10-23 stsp if (packidx == NULL) {
343 13b2bc37 2022-10-23 stsp err = got_pack_find_pack_for_commit_painting(
344 13b2bc37 2022-10-23 stsp &packidx, ids, nqueued, repo);
345 13b2bc37 2022-10-23 stsp if (err)
346 13b2bc37 2022-10-23 stsp goto done;
347 13b2bc37 2022-10-23 stsp }
348 13b2bc37 2022-10-23 stsp if (packidx != NULL) {
349 13b2bc37 2022-10-23 stsp err = got_pack_cache_pack_for_packidx(&pack,
350 13b2bc37 2022-10-23 stsp packidx, repo);
351 13b2bc37 2022-10-23 stsp if (err)
352 13b2bc37 2022-10-23 stsp goto done;
353 13b2bc37 2022-10-23 stsp err = got_repo_pin_pack(repo, packidx, pack);
354 13b2bc37 2022-10-23 stsp if (err)
355 13b2bc37 2022-10-23 stsp goto done;
356 13b2bc37 2022-10-23 stsp }
357 13b2bc37 2022-10-23 stsp }
358 13b2bc37 2022-10-23 stsp
359 13b2bc37 2022-10-23 stsp got_object_commit_close(commit);
360 13b2bc37 2022-10-23 stsp commit = NULL;
361 13b2bc37 2022-10-23 stsp
362 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
363 13b2bc37 2022-10-23 stsp qid = NULL;
364 13b2bc37 2022-10-23 stsp }
365 13b2bc37 2022-10-23 stsp done:
366 13b2bc37 2022-10-23 stsp if (commit)
367 13b2bc37 2022-10-23 stsp got_object_commit_close(commit);
368 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
369 13b2bc37 2022-10-23 stsp got_repo_unpin_pack(repo);
370 13b2bc37 2022-10-23 stsp return err;
371 13b2bc37 2022-10-23 stsp }