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 5822e79e 2023-02-23 op #include <sha2.h>
25 13b2bc37 2022-10-23 stsp #include <limits.h>
26 13b2bc37 2022-10-23 stsp #include <stdio.h>
27 13b2bc37 2022-10-23 stsp #include <stdint.h>
28 13b2bc37 2022-10-23 stsp #include <stdlib.h>
29 13b2bc37 2022-10-23 stsp #include <string.h>
30 13b2bc37 2022-10-23 stsp #include <time.h>
31 13b2bc37 2022-10-23 stsp #include <imsg.h>
32 13b2bc37 2022-10-23 stsp #include <inttypes.h>
33 13b2bc37 2022-10-23 stsp #include <unistd.h>
34 13b2bc37 2022-10-23 stsp
35 13b2bc37 2022-10-23 stsp #include "got_error.h"
36 13b2bc37 2022-10-23 stsp #include "got_cancel.h"
37 13b2bc37 2022-10-23 stsp #include "got_object.h"
38 13b2bc37 2022-10-23 stsp #include "got_reference.h"
39 13b2bc37 2022-10-23 stsp #include "got_repository_admin.h"
40 13b2bc37 2022-10-23 stsp #include "got_path.h"
41 13b2bc37 2022-10-23 stsp
42 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
44 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
45 13b2bc37 2022-10-23 stsp #include "got_lib_object_idset.h"
46 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
47 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
48 13b2bc37 2022-10-23 stsp #include "got_lib_pack_create.h"
49 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
50 13b2bc37 2022-10-23 stsp
51 13b2bc37 2022-10-23 stsp static const struct got_error *
52 13b2bc37 2022-10-23 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
53 13b2bc37 2022-10-23 stsp off_t base_offset)
54 13b2bc37 2022-10-23 stsp {
55 13b2bc37 2022-10-23 stsp const struct got_error *err;
56 13b2bc37 2022-10-23 stsp int idx;
57 13b2bc37 2022-10-23 stsp
58 13b2bc37 2022-10-23 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
59 13b2bc37 2022-10-23 stsp if (err)
60 13b2bc37 2022-10-23 stsp return err;
61 13b2bc37 2022-10-23 stsp if (idx == -1)
62 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
63 13b2bc37 2022-10-23 stsp
64 13b2bc37 2022-10-23 stsp return got_packidx_get_object_id(base_id, packidx, idx);
65 13b2bc37 2022-10-23 stsp }
66 13b2bc37 2022-10-23 stsp
67 13b2bc37 2022-10-23 stsp struct search_deltas_arg {
68 13b2bc37 2022-10-23 stsp struct got_pack_metavec *v;
69 13b2bc37 2022-10-23 stsp struct got_packidx *packidx;
70 13b2bc37 2022-10-23 stsp struct got_pack *pack;
71 13b2bc37 2022-10-23 stsp struct got_object_idset *idset;
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 24b7de1c 2022-12-03 stsp off_t delta_offset, delta_data_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 24b7de1c 2022-12-03 stsp &delta_compressed_size, &delta_offset, &delta_data_offset,
104 24b7de1c 2022-12-03 stsp &base_offset, &base_id, &base_size, &result_size,
105 24b7de1c 2022-12-03 stsp a->pack, a->packidx, obj_idx);
106 13b2bc37 2022-10-23 stsp if (err) {
107 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_TYPE)
108 13b2bc37 2022-10-23 stsp return NULL; /* object not stored as a delta */
109 13b2bc37 2022-10-23 stsp return err;
110 13b2bc37 2022-10-23 stsp }
111 13b2bc37 2022-10-23 stsp
112 13b2bc37 2022-10-23 stsp /*
113 13b2bc37 2022-10-23 stsp * If this is an offset delta we must determine the base
114 13b2bc37 2022-10-23 stsp * object ID ourselves.
115 13b2bc37 2022-10-23 stsp */
116 13b2bc37 2022-10-23 stsp if (base_offset != 0) {
117 13b2bc37 2022-10-23 stsp err = get_base_object_id(&base_id, a->packidx, base_offset);
118 13b2bc37 2022-10-23 stsp if (err)
119 13b2bc37 2022-10-23 stsp goto done;
120 13b2bc37 2022-10-23 stsp }
121 13b2bc37 2022-10-23 stsp
122 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(a->idset, &base_id)) {
123 13b2bc37 2022-10-23 stsp struct got_pack_meta *m, *base;
124 13b2bc37 2022-10-23 stsp
125 13b2bc37 2022-10-23 stsp m = got_object_idset_get(a->idset, id);
126 13b2bc37 2022-10-23 stsp if (m == NULL) {
127 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NO_OBJ,
128 13b2bc37 2022-10-23 stsp "delta object not found");
129 13b2bc37 2022-10-23 stsp goto done;
130 13b2bc37 2022-10-23 stsp }
131 13b2bc37 2022-10-23 stsp
132 13b2bc37 2022-10-23 stsp base = got_object_idset_get(a->idset, &base_id);
133 13b2bc37 2022-10-23 stsp if (m == NULL) {
134 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NO_OBJ,
135 13b2bc37 2022-10-23 stsp "delta base object not found");
136 13b2bc37 2022-10-23 stsp goto done;
137 13b2bc37 2022-10-23 stsp }
138 13b2bc37 2022-10-23 stsp
139 13b2bc37 2022-10-23 stsp m->base_obj_id = got_object_id_dup(&base_id);
140 13b2bc37 2022-10-23 stsp if (m->base_obj_id == NULL) {
141 13b2bc37 2022-10-23 stsp err = got_error_from_errno("got_object_id_dup");
142 13b2bc37 2022-10-23 stsp goto done;
143 13b2bc37 2022-10-23 stsp }
144 13b2bc37 2022-10-23 stsp
145 13b2bc37 2022-10-23 stsp m->prev = base;
146 13b2bc37 2022-10-23 stsp m->size = result_size;
147 13b2bc37 2022-10-23 stsp m->delta_len = delta_size;
148 13b2bc37 2022-10-23 stsp m->delta_compressed_len = delta_compressed_size;
149 24b7de1c 2022-12-03 stsp m->reused_delta_offset = delta_data_offset;
150 24b7de1c 2022-12-03 stsp m->delta_offset = 0;
151 13b2bc37 2022-10-23 stsp
152 13b2bc37 2022-10-23 stsp err = got_pack_add_meta(m, a->v);
153 13b2bc37 2022-10-23 stsp if (err)
154 13b2bc37 2022-10-23 stsp goto done;
155 13b2bc37 2022-10-23 stsp
156 13b2bc37 2022-10-23 stsp err = got_pack_report_progress(a->progress_cb, a->progress_arg,
157 13b2bc37 2022-10-23 stsp a->rl, a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
158 13b2bc37 2022-10-23 stsp got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
159 13b2bc37 2022-10-23 stsp if (err)
160 13b2bc37 2022-10-23 stsp goto done;
161 13b2bc37 2022-10-23 stsp }
162 13b2bc37 2022-10-23 stsp done:
163 13b2bc37 2022-10-23 stsp free(delta_buf);
164 13b2bc37 2022-10-23 stsp return err;
165 13b2bc37 2022-10-23 stsp }
166 13b2bc37 2022-10-23 stsp
167 13b2bc37 2022-10-23 stsp const struct got_error *
168 24b7de1c 2022-12-03 stsp got_pack_search_deltas(struct got_packidx **packidx, struct got_pack **pack,
169 24b7de1c 2022-12-03 stsp struct got_pack_metavec *v, struct got_object_idset *idset,
170 13b2bc37 2022-10-23 stsp int ncolored, int nfound, int ntrees, int ncommits,
171 13b2bc37 2022-10-23 stsp struct got_repository *repo,
172 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
173 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
174 13b2bc37 2022-10-23 stsp {
175 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
176 13b2bc37 2022-10-23 stsp struct search_deltas_arg sda;
177 13b2bc37 2022-10-23 stsp
178 24b7de1c 2022-12-03 stsp *packidx = NULL;
179 24b7de1c 2022-12-03 stsp *pack = NULL;
180 24b7de1c 2022-12-03 stsp
181 24b7de1c 2022-12-03 stsp err = got_pack_find_pack_for_reuse(packidx, repo);
182 13b2bc37 2022-10-23 stsp if (err)
183 13b2bc37 2022-10-23 stsp return err;
184 13b2bc37 2022-10-23 stsp
185 24b7de1c 2022-12-03 stsp if (*packidx == NULL)
186 13b2bc37 2022-10-23 stsp return NULL;
187 13b2bc37 2022-10-23 stsp
188 24b7de1c 2022-12-03 stsp err = got_pack_cache_pack_for_packidx(pack, *packidx, repo);
189 13b2bc37 2022-10-23 stsp if (err)
190 13b2bc37 2022-10-23 stsp return err;
191 13b2bc37 2022-10-23 stsp
192 13b2bc37 2022-10-23 stsp memset(&sda, 0, sizeof(sda));
193 13b2bc37 2022-10-23 stsp sda.v = v;
194 13b2bc37 2022-10-23 stsp sda.idset = idset;
195 24b7de1c 2022-12-03 stsp sda.pack = *pack;
196 24b7de1c 2022-12-03 stsp sda.packidx = *packidx;
197 13b2bc37 2022-10-23 stsp sda.ncolored = ncolored;
198 13b2bc37 2022-10-23 stsp sda.nfound = nfound;
199 13b2bc37 2022-10-23 stsp sda.ntrees = ntrees;
200 13b2bc37 2022-10-23 stsp sda.ncommits = ncommits;
201 13b2bc37 2022-10-23 stsp sda.progress_cb = progress_cb;
202 13b2bc37 2022-10-23 stsp sda.progress_arg = progress_arg;
203 13b2bc37 2022-10-23 stsp sda.rl = rl;
204 13b2bc37 2022-10-23 stsp sda.cancel_cb = cancel_cb;
205 13b2bc37 2022-10-23 stsp sda.cancel_arg = cancel_arg;
206 13b2bc37 2022-10-23 stsp return got_object_idset_for_each(idset, search_delta_for_object, &sda);
207 13b2bc37 2022-10-23 stsp }
208 13b2bc37 2022-10-23 stsp
209 13b2bc37 2022-10-23 stsp const struct got_error *
210 13b2bc37 2022-10-23 stsp got_pack_load_packed_object_ids(int *found_all_objects,
211 13b2bc37 2022-10-23 stsp struct got_object_id **ours, int nours,
212 13b2bc37 2022-10-23 stsp struct got_object_id **theirs, int ntheirs,
213 13b2bc37 2022-10-23 stsp int want_meta, uint32_t seed, struct got_object_idset *idset,
214 13b2bc37 2022-10-23 stsp struct got_object_idset *idset_exclude, int loose_obj_only,
215 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_packidx *packidx,
216 13b2bc37 2022-10-23 stsp int *ncolored, int *nfound, int *ntrees,
217 13b2bc37 2022-10-23 stsp got_pack_progress_cb progress_cb, void *progress_arg,
218 13b2bc37 2022-10-23 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
219 13b2bc37 2022-10-23 stsp {
220 13b2bc37 2022-10-23 stsp /* We do not need this optimized traversal while using direct I/O. */
221 13b2bc37 2022-10-23 stsp *found_all_objects = 0;
222 13b2bc37 2022-10-23 stsp return NULL;
223 13b2bc37 2022-10-23 stsp }
224 13b2bc37 2022-10-23 stsp
225 13b2bc37 2022-10-23 stsp const struct got_error *
226 13b2bc37 2022-10-23 stsp got_pack_paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
227 13b2bc37 2022-10-23 stsp struct got_object_idset *keep, struct got_object_idset *drop,
228 13b2bc37 2022-10-23 stsp struct got_object_idset *skip, struct got_repository *repo,
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 const struct got_error *err = NULL;
233 13b2bc37 2022-10-23 stsp struct got_commit_object *commit = NULL;
234 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
235 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
236 13b2bc37 2022-10-23 stsp const struct got_object_id_queue *parents;
237 13b2bc37 2022-10-23 stsp struct got_object_qid *qid = NULL;
238 13b2bc37 2022-10-23 stsp int nqueued = nids, nskip = 0;
239 13b2bc37 2022-10-23 stsp
240 13b2bc37 2022-10-23 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
241 13b2bc37 2022-10-23 stsp intptr_t color;
242 13b2bc37 2022-10-23 stsp
243 13b2bc37 2022-10-23 stsp if (cancel_cb) {
244 13b2bc37 2022-10-23 stsp err = cancel_cb(cancel_arg);
245 13b2bc37 2022-10-23 stsp if (err)
246 13b2bc37 2022-10-23 stsp break;
247 13b2bc37 2022-10-23 stsp }
248 13b2bc37 2022-10-23 stsp
249 13b2bc37 2022-10-23 stsp qid = STAILQ_FIRST(ids);
250 13b2bc37 2022-10-23 stsp STAILQ_REMOVE_HEAD(ids, entry);
251 13b2bc37 2022-10-23 stsp nqueued--;
252 13b2bc37 2022-10-23 stsp color = (intptr_t)qid->data;
253 13b2bc37 2022-10-23 stsp if (color == COLOR_SKIP)
254 13b2bc37 2022-10-23 stsp nskip--;
255 13b2bc37 2022-10-23 stsp
256 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(skip, &qid->id)) {
257 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
258 13b2bc37 2022-10-23 stsp qid = NULL;
259 13b2bc37 2022-10-23 stsp continue;
260 13b2bc37 2022-10-23 stsp }
261 13b2bc37 2022-10-23 stsp if (color == COLOR_KEEP &&
262 13b2bc37 2022-10-23 stsp got_object_idset_contains(keep, &qid->id)) {
263 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
264 13b2bc37 2022-10-23 stsp qid = NULL;
265 13b2bc37 2022-10-23 stsp continue;
266 13b2bc37 2022-10-23 stsp }
267 13b2bc37 2022-10-23 stsp if (color == COLOR_DROP &&
268 13b2bc37 2022-10-23 stsp got_object_idset_contains(drop, &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
274 13b2bc37 2022-10-23 stsp switch (color) {
275 13b2bc37 2022-10-23 stsp case COLOR_KEEP:
276 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(drop, &qid->id)) {
277 13b2bc37 2022-10-23 stsp err = got_pack_paint_commit(qid, COLOR_SKIP);
278 13b2bc37 2022-10-23 stsp if (err)
279 13b2bc37 2022-10-23 stsp goto done;
280 13b2bc37 2022-10-23 stsp } else
281 13b2bc37 2022-10-23 stsp (*ncolored)++;
282 13b2bc37 2022-10-23 stsp err = got_object_idset_add(keep, &qid->id, NULL);
283 13b2bc37 2022-10-23 stsp if (err)
284 13b2bc37 2022-10-23 stsp goto done;
285 13b2bc37 2022-10-23 stsp break;
286 13b2bc37 2022-10-23 stsp case COLOR_DROP:
287 13b2bc37 2022-10-23 stsp if (got_object_idset_contains(keep, &qid->id)) {
288 13b2bc37 2022-10-23 stsp err = got_pack_paint_commit(qid, COLOR_SKIP);
289 13b2bc37 2022-10-23 stsp if (err)
290 13b2bc37 2022-10-23 stsp goto done;
291 13b2bc37 2022-10-23 stsp } else
292 13b2bc37 2022-10-23 stsp (*ncolored)++;
293 13b2bc37 2022-10-23 stsp err = got_object_idset_add(drop, &qid->id, NULL);
294 13b2bc37 2022-10-23 stsp if (err)
295 13b2bc37 2022-10-23 stsp goto done;
296 13b2bc37 2022-10-23 stsp break;
297 13b2bc37 2022-10-23 stsp case COLOR_SKIP:
298 13b2bc37 2022-10-23 stsp if (!got_object_idset_contains(skip, &qid->id)) {
299 13b2bc37 2022-10-23 stsp err = got_object_idset_add(skip, &qid->id,
300 13b2bc37 2022-10-23 stsp NULL);
301 13b2bc37 2022-10-23 stsp if (err)
302 13b2bc37 2022-10-23 stsp goto done;
303 13b2bc37 2022-10-23 stsp }
304 13b2bc37 2022-10-23 stsp break;
305 13b2bc37 2022-10-23 stsp default:
306 13b2bc37 2022-10-23 stsp /* should not happen */
307 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
308 13b2bc37 2022-10-23 stsp "%s invalid commit color %"PRIdPTR, __func__,
309 13b2bc37 2022-10-23 stsp color);
310 13b2bc37 2022-10-23 stsp goto done;
311 13b2bc37 2022-10-23 stsp }
312 13b2bc37 2022-10-23 stsp
313 13b2bc37 2022-10-23 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
314 13b2bc37 2022-10-23 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
315 13b2bc37 2022-10-23 stsp if (err)
316 13b2bc37 2022-10-23 stsp break;
317 13b2bc37 2022-10-23 stsp
318 13b2bc37 2022-10-23 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
319 13b2bc37 2022-10-23 stsp if (err)
320 13b2bc37 2022-10-23 stsp break;
321 13b2bc37 2022-10-23 stsp
322 13b2bc37 2022-10-23 stsp parents = got_object_commit_get_parent_ids(commit);
323 13b2bc37 2022-10-23 stsp if (parents) {
324 13b2bc37 2022-10-23 stsp struct got_object_qid *pid;
325 13b2bc37 2022-10-23 stsp color = (intptr_t)qid->data;
326 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(pid, parents, entry) {
327 13b2bc37 2022-10-23 stsp err = got_pack_queue_commit_id(ids, &pid->id,
328 13b2bc37 2022-10-23 stsp color, repo);
329 13b2bc37 2022-10-23 stsp if (err)
330 13b2bc37 2022-10-23 stsp break;
331 13b2bc37 2022-10-23 stsp nqueued++;
332 13b2bc37 2022-10-23 stsp if (color == COLOR_SKIP)
333 13b2bc37 2022-10-23 stsp nskip++;
334 13b2bc37 2022-10-23 stsp }
335 13b2bc37 2022-10-23 stsp }
336 13b2bc37 2022-10-23 stsp
337 13b2bc37 2022-10-23 stsp if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
338 13b2bc37 2022-10-23 stsp /*
339 13b2bc37 2022-10-23 stsp * We now know that at least one pack file exists.
340 13b2bc37 2022-10-23 stsp * Pin a suitable pack to ensure it remains cached
341 13b2bc37 2022-10-23 stsp * while we are churning through commit history.
342 13b2bc37 2022-10-23 stsp */
343 13b2bc37 2022-10-23 stsp if (packidx == NULL) {
344 13b2bc37 2022-10-23 stsp err = got_pack_find_pack_for_commit_painting(
345 13b2bc37 2022-10-23 stsp &packidx, ids, nqueued, repo);
346 13b2bc37 2022-10-23 stsp if (err)
347 13b2bc37 2022-10-23 stsp goto done;
348 13b2bc37 2022-10-23 stsp }
349 13b2bc37 2022-10-23 stsp if (packidx != NULL) {
350 13b2bc37 2022-10-23 stsp err = got_pack_cache_pack_for_packidx(&pack,
351 13b2bc37 2022-10-23 stsp packidx, repo);
352 13b2bc37 2022-10-23 stsp if (err)
353 13b2bc37 2022-10-23 stsp goto done;
354 13b2bc37 2022-10-23 stsp err = got_repo_pin_pack(repo, packidx, pack);
355 13b2bc37 2022-10-23 stsp if (err)
356 13b2bc37 2022-10-23 stsp goto done;
357 13b2bc37 2022-10-23 stsp }
358 13b2bc37 2022-10-23 stsp }
359 13b2bc37 2022-10-23 stsp
360 13b2bc37 2022-10-23 stsp got_object_commit_close(commit);
361 13b2bc37 2022-10-23 stsp commit = NULL;
362 13b2bc37 2022-10-23 stsp
363 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
364 13b2bc37 2022-10-23 stsp qid = NULL;
365 13b2bc37 2022-10-23 stsp }
366 13b2bc37 2022-10-23 stsp done:
367 13b2bc37 2022-10-23 stsp if (commit)
368 13b2bc37 2022-10-23 stsp got_object_commit_close(commit);
369 13b2bc37 2022-10-23 stsp got_object_qid_free(qid);
370 13b2bc37 2022-10-23 stsp got_repo_unpin_pack(repo);
371 13b2bc37 2022-10-23 stsp return err;
372 13b2bc37 2022-10-23 stsp }