2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_cancel.h"
31 #include "got_object.h"
33 #include "got_lib_deltify.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_object.h"
36 #include "got_lib_object_idset.h"
37 #include "got_lib_deflate.h"
38 #include "got_lib_pack.h"
41 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
44 struct got_pack_meta {
45 struct got_object_id id;
50 /* The best delta we picked */
51 struct got_pack_meta *head;
52 struct got_pack_meta *prev;
53 struct got_delta_instruction *deltas;
57 /* Only used for delta window */
58 struct got_delta_table *dtab;
60 /* Only used for writing offset deltas */
64 struct got_pack_metavec {
65 struct got_pack_meta **meta;
70 static const struct got_error *
71 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
72 const char *path, int obj_type, time_t mtime)
74 const struct got_error *err = NULL;
75 struct got_pack_meta *m;
79 m = calloc(1, sizeof(*m));
81 return got_error_from_errno("malloc");
83 memcpy(&m->id, id, sizeof(m->id));
85 m->path = strdup(path);
86 if (m->path == NULL) {
87 err = got_error_from_errno("strdup");
92 m->obj_type = obj_type;
99 clear_meta(struct got_pack_meta *meta)
110 free_nmeta(struct got_pack_meta **meta, int nmeta)
114 for (i = 0; i < nmeta; i++)
120 delta_order_cmp(const void *pa, const void *pb)
122 struct got_pack_meta *a, *b;
125 a = *(struct got_pack_meta **)pa;
126 b = *(struct got_pack_meta **)pb;
128 if (a->obj_type != b->obj_type)
129 return a->obj_type - b->obj_type;
130 cmp = strcmp(a->path, b->path);
133 if (a->mtime != b->mtime)
134 return a->mtime - b->mtime;
135 return got_object_id_cmp(&a->id, &b->id);
139 showprogress(int x, int pct)
143 fprintf(stderr, "\b\b\b\b%3d%%", pct);
149 delta_size(struct got_delta_instruction *deltas, int ndeltas)
152 for (i = 0; i < ndeltas; i++) {
154 size += GOT_DELTA_SIZE_SHIFT;
156 size += deltas[i].len + 1;
162 static const struct got_error *
163 pick_deltas(struct got_pack_meta **meta, int nmeta, struct got_repository *repo,
164 got_cancel_cb cancel_cb, void *cancel_arg)
166 const struct got_error *err = NULL;
167 struct got_pack_meta *m = NULL, *base = NULL;
168 struct got_raw_object *raw = NULL, *base_raw = NULL;
169 struct got_delta_instruction *deltas;
170 int i, j, size, ndeltas, pct, best;
171 const int max_base_candidates = 10;
174 fprintf(stderr, "picking deltas\n");
175 fprintf(stderr, "deltifying %d objects: 0%%", nmeta);
176 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
177 for (i = 0; i < nmeta; i++) {
179 err = (*cancel_cb)(cancel_arg);
185 pct = showprogress((i*100) / nmeta, pct);
189 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
190 m->obj_type == GOT_OBJ_TYPE_TAG)
193 err = got_object_raw_open(&raw, repo, &m->id, 8192);
197 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
198 raw->size + raw->hdrlen);
202 if (i > max_base_candidates) {
203 struct got_pack_meta *n = NULL;
204 n = meta[i - (max_base_candidates + 1)];
205 got_deltify_free(n->dtab);
210 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
212 err = (*cancel_cb)(cancel_arg);
217 /* long chains make unpacking slow, avoid such bases */
218 if (base->nchain >= 128 ||
219 base->obj_type != m->obj_type)
222 err = got_object_raw_open(&base_raw, repo, &base->id,
226 err = got_deltify(&deltas, &ndeltas,
227 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
228 base->dtab, base_raw->f, base_raw->hdrlen,
229 base_raw->size + base_raw->hdrlen);
230 got_object_raw_close(base_raw);
235 size = delta_size(deltas, ndeltas);
236 if (size + 32 < best){
238 * if we already picked a best delta,
244 m->ndeltas = ndeltas;
245 m->nchain = base->nchain + 1;
247 m->head = base->head;
257 got_object_raw_close(raw);
261 fprintf(stderr, "\b\b\b\b100%%\n");
263 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
264 got_deltify_free(meta[i]->dtab);
265 meta[i]->dtab = NULL;
268 got_object_raw_close(raw);
270 got_object_raw_close(base_raw);
274 static const struct got_error *
275 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
276 struct got_object_id *id, const char *path, int obj_type,
279 const struct got_error *err;
280 struct got_pack_meta *m;
282 err = got_object_idset_add(idset, id, NULL);
289 err = alloc_meta(&m, id, path, obj_type, mtime);
293 if (v->nmeta == v->metasz){
294 size_t newsize = 2 * v->metasz;
295 struct got_pack_meta **new;
296 new = reallocarray(v->meta, newsize, sizeof(*new));
298 err = got_error_from_errno("reallocarray");
309 v->meta[v->nmeta++] = m;
314 static const struct got_error *
315 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
316 struct got_object_idset *idset, struct got_object_id *tree_id,
317 const char *dpath, time_t mtime, struct got_repository *repo,
318 got_cancel_cb cancel_cb, void *cancel_arg)
320 const struct got_error *err;
321 struct got_tree_object *tree;
325 err = got_object_open_as_tree(&tree, repo, tree_id);
329 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
330 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
331 struct got_object_id *id = got_tree_entry_get_id(e);
332 mode_t mode = got_tree_entry_get_mode(e);
335 err = (*cancel_cb)(cancel_arg);
340 if (got_object_tree_entry_is_symlink(e) ||
341 got_object_tree_entry_is_submodule(e) ||
342 got_object_idset_contains(idset, id))
345 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
346 got_tree_entry_get_name(e)) == -1) {
347 err = got_error_from_errno("asprintf");
352 struct got_object_qid *qid;
353 err = got_object_qid_alloc(&qid, id);
356 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
357 } else if (S_ISREG(mode)) {
358 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
367 got_object_tree_close(tree);
372 static const struct got_error *
373 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
374 struct got_object_id *tree_id, const char *dpath, time_t mtime,
375 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
377 const struct got_error *err = NULL;
378 struct got_object_id_queue tree_ids;
379 struct got_object_qid *qid;
381 if (got_object_idset_contains(idset, tree_id))
384 err = got_object_qid_alloc(&qid, tree_id);
388 SIMPLEQ_INIT(&tree_ids);
389 SIMPLEQ_INSERT_TAIL(&tree_ids, qid, entry);
391 while (!SIMPLEQ_EMPTY(&tree_ids)) {
393 err = (*cancel_cb)(cancel_arg);
398 qid = SIMPLEQ_FIRST(&tree_ids);
399 SIMPLEQ_REMOVE_HEAD(&tree_ids, entry);
401 if (got_object_idset_contains(idset, qid->id)) {
402 got_object_qid_free(qid);
406 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
409 got_object_qid_free(qid);
413 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
414 mtime, repo, cancel_cb, cancel_arg);
415 got_object_qid_free(qid);
420 got_object_id_queue_free(&tree_ids);
424 static const struct got_error *
425 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
426 struct got_object_id *id, struct got_repository *repo,
427 got_cancel_cb cancel_cb, void *cancel_arg)
429 const struct got_error *err;
430 struct got_commit_object *commit;
432 if (got_object_idset_contains(idset, id))
435 err = got_object_open_as_commit(&commit, repo, id);
439 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
440 got_object_commit_get_committer_time(commit));
444 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
445 "", got_object_commit_get_committer_time(commit), repo,
446 cancel_cb, cancel_arg);
448 got_object_commit_close(commit);
452 enum findtwixt_color {
457 static const int findtwixt_colors[] = {
463 static const struct got_error *
464 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
465 int color, struct got_repository *repo)
467 const struct got_error *err;
468 struct got_object_qid *qid;
472 err = got_object_get_type(&obj_type, repo, id);
476 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
477 err = got_object_id_str(&id_str, id);
480 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
481 "%s is not a commit", id_str);
485 err = got_object_qid_alloc(&qid, id);
489 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
490 qid->data = (void *)&findtwixt_colors[color];
494 static const struct got_error *
495 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
496 struct got_object_id *id, struct got_repository *repo,
497 got_cancel_cb cancel_cb, void *cancel_arg)
499 const struct got_error *err = NULL;
500 struct got_commit_object *commit;
501 const struct got_object_id_queue *parents;
502 struct got_object_id_queue ids;
503 struct got_object_qid *qid;
507 err = got_object_qid_alloc(&qid, id);
510 SIMPLEQ_INSERT_HEAD(&ids, qid, entry);
512 while (!SIMPLEQ_EMPTY(&ids)) {
514 err = (*cancel_cb)(cancel_arg);
519 qid = SIMPLEQ_FIRST(&ids);
520 SIMPLEQ_REMOVE_HEAD(&ids, entry);
522 if (got_object_idset_contains(drop, qid->id)) {
523 got_object_qid_free(qid);
527 err = got_object_idset_add(drop, qid->id, NULL);
529 got_object_qid_free(qid);
533 if (!got_object_idset_contains(keep, qid->id)) {
534 got_object_qid_free(qid);
538 err = got_object_open_as_commit(&commit, repo, qid->id);
539 got_object_qid_free(qid);
543 parents = got_object_commit_get_parent_ids(commit);
545 err = got_object_id_queue_copy(parents, &ids);
547 got_object_commit_close(commit);
551 got_object_commit_close(commit);
554 got_object_id_queue_free(&ids);
558 struct append_id_arg {
559 struct got_object_id **array;
563 static const struct got_error *
564 append_id(struct got_object_id *id, void *data, void *arg)
566 struct append_id_arg *a = arg;
568 a->array[a->idx] = got_object_id_dup(id);
569 if (a->array[a->idx] == NULL)
570 return got_error_from_errno("got_object_id_dup");
576 static const struct got_error *
577 findtwixt(struct got_object_id ***res, int *nres,
578 struct got_object_id **head, int nhead,
579 struct got_object_id **tail, int ntail,
580 struct got_repository *repo,
581 got_cancel_cb cancel_cb, void *cancel_arg)
583 const struct got_error *err = NULL;
584 struct got_object_id_queue ids;
585 struct got_object_idset *keep, *drop;
586 struct got_object_qid *qid;
587 int i, ncolor, nkeep;
593 keep = got_object_idset_alloc();
595 return got_error_from_errno("got_object_idset_alloc");
597 drop = got_object_idset_alloc();
599 err = got_error_from_errno("got_object_idset_alloc");
603 for (i = 0; i < nhead; i++){
605 err = queue_commit_id(&ids, head[i], COLOR_KEEP, repo);
610 for (i = 0; i < ntail; i++){
612 err = queue_commit_id(&ids, tail[i], COLOR_DROP, repo);
618 while (!SIMPLEQ_EMPTY(&ids)) {
620 qid = SIMPLEQ_FIRST(&ids);
621 qcolor = *((int *)qid->data);
623 if (got_object_idset_contains(drop, qid->id))
625 else if (got_object_idset_contains(keep, qid->id))
628 ncolor = COLOR_BLANK;
630 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
631 qcolor == COLOR_KEEP)) {
632 SIMPLEQ_REMOVE_HEAD(&ids, entry);
633 got_object_qid_free(qid);
637 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
638 err = drop_commit(keep, drop, qid->id, repo,
639 cancel_cb, cancel_arg);
642 } else if (ncolor == COLOR_BLANK) {
643 struct got_commit_object *commit;
644 struct got_object_id *id;
645 const struct got_object_id_queue *parents;
646 struct got_object_qid *pid;
648 id = got_object_id_dup(qid->id);
650 err = got_error_from_errno("got_object_id_dup");
653 if (qcolor == COLOR_KEEP)
654 err = got_object_idset_add(keep, id, NULL);
656 err = got_object_idset_add(drop, id, NULL);
662 err = got_object_open_as_commit(&commit, repo, id);
667 parents = got_object_commit_get_parent_ids(commit);
669 SIMPLEQ_FOREACH(pid, parents, entry) {
670 err = queue_commit_id(&ids, pid->id,
678 got_object_commit_close(commit);
681 /* should not happen */
682 err = got_error_fmt(GOT_ERR_NOT_IMPL,
683 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
687 SIMPLEQ_REMOVE_HEAD(&ids, entry);
688 got_object_qid_free(qid);
691 nkeep = got_object_idset_num_elements(keep);
693 struct append_id_arg arg;
694 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
695 if (arg.array == NULL) {
696 err = got_error_from_errno("calloc");
700 err = got_object_idset_for_each(keep, append_id, &arg);
709 got_object_idset_free(keep);
710 got_object_idset_free(drop);
711 got_object_id_queue_free(&ids);
715 static const struct got_error *
716 read_meta(struct got_pack_meta ***meta, int *nmeta,
717 struct got_object_id **theirs, int ntheirs,
718 struct got_object_id **ours, int nours, struct got_repository *repo,
719 got_cancel_cb cancel_cb, void *cancel_arg)
721 const struct got_error *err = NULL;
722 struct got_object_id **ids = NULL;
723 struct got_object_idset *idset;
725 struct got_pack_metavec v;
730 idset = got_object_idset_alloc();
732 return got_error_from_errno("got_object_idset_alloc");
736 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
737 if (v.meta == NULL) {
738 err = got_error_from_errno("reallocarray");
742 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
743 cancel_cb, cancel_arg);
744 if (err || nobj == 0)
747 for (i = 0; i < ntheirs; i++) {
748 if (theirs[i] != NULL) {
749 err = load_commit(NULL, idset, theirs[i], repo,
750 cancel_cb, cancel_arg);
756 for (i = 0; i < nobj; i++) {
757 err = load_commit(&v, idset, ids[i], repo,
758 cancel_cb, cancel_arg);
763 for (i = 0; i < nobj; i++) {
767 got_object_idset_free(idset);
777 const struct got_error *
778 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
782 SHA1Update(ctx, buf, len);
783 n = fwrite(buf, 1, len, f);
785 return got_ferror(f, GOT_ERR_IO);
790 putbe32(char *b, uint32_t n)
799 write_order_cmp(const void *pa, const void *pb)
801 struct got_pack_meta *a, *b, *ahd, *bhd;
803 a = *(struct got_pack_meta **)pa;
804 b = *(struct got_pack_meta **)pb;
805 ahd = (a->head == NULL) ? a : a->head;
806 bhd = (b->head == NULL) ? b : b->head;
807 if (ahd->mtime != bhd->mtime)
808 return bhd->mtime - ahd->mtime;
810 return (uintptr_t)bhd - (uintptr_t)ahd;
811 if (a->nchain != b->nchain)
812 return a->nchain - b->nchain;
813 return a->mtime - b->mtime;
816 static const struct got_error *
817 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
823 hdr[0] = obj_type << 4;
826 for (i = 1; len != 0; i++){
828 return got_error(GOT_ERR_NO_SPACE);
829 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
830 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
831 len >>= GOT_DELTA_SIZE_SHIFT;
838 static const struct got_error *
839 append(char **p, int *len, int *sz, void *seg, int nseg)
843 if (*len + nseg >= *sz) {
844 while (*len + nseg >= *sz)
846 n = realloc(*p, *sz);
848 return got_error_from_errno("realloc");
851 memcpy(*p + *len, seg, nseg);
857 static const struct got_error *
858 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
859 off_t base_size, char **pp)
861 const struct got_error *err = NULL;
863 unsigned char buf[16], *bp;
866 struct got_delta_instruction *d;
875 return got_error_from_errno("malloc");
877 /* base object size */
878 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
879 n = base_size >> GOT_DELTA_SIZE_SHIFT;
880 for (i = 1; n > 0; i++) {
881 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
882 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
883 n >>= GOT_DELTA_SIZE_SHIFT;
885 err = append(&p, &len, &sz, buf, i);
889 /* target object size */
890 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
891 n = o->size >> GOT_DELTA_SIZE_SHIFT;
892 for (i = 1; n > 0; i++) {
893 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
894 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
895 n >>= GOT_DELTA_SIZE_SHIFT;
897 err = append(&p, &len, &sz, buf, i);
900 for (j = 0; j < m->ndeltas; j++) {
905 buf[0] = GOT_DELTA_BASE_COPY;
906 for (i = 0; i < 4; i++) {
907 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
916 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
917 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
918 for (i = 0; i < 3 && n > 0; i++) {
919 buf[0] |= 1 << (i + 4);
924 err = append(&p, &len, &sz, buf, bp - buf);
930 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
931 return got_error_from_errno("fseeko");
933 while (n != d->len) {
934 buf[0] = (d->len - n < 127) ? d->len - n : 127;
935 err = append(&p, &len, &sz, buf, 1);
938 r = fread(content, 1, buf[0], o->f);
940 return got_ferror(o->f, GOT_ERR_IO);
941 err = append(&p, &len, &sz, content, buf[0]);
954 packoff(char *hdr, off_t off)
959 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
960 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
961 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
967 hdr[j++] = rbuf[--i];
971 static const struct got_error *
972 genpack(uint8_t *pack_sha1, FILE *packfile,
973 struct got_pack_meta **meta, int nmeta, int use_offset_deltas,
974 struct got_repository *repo)
976 const struct got_error *err = NULL;
979 struct got_pack_meta *m;
980 struct got_raw_object *raw;
981 char *p = NULL, buf[32];
983 struct got_deflate_checksum csum;
986 csum.output_sha1 = &ctx;
987 csum.output_crc = NULL;
990 fprintf(stderr, "generating pack\n");
992 err = hwrite(packfile, "PACK", 4, &ctx);
995 putbe32(buf, GOT_PACKFILE_VERSION);
996 err = hwrite(packfile, buf, 4, &ctx);
1000 err = hwrite(packfile, buf, 4, &ctx);
1003 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1004 fprintf(stderr, "writing %d objects: 0%%", nmeta);
1005 for (i = 0; i < nmeta; i++) {
1006 pct = showprogress((i*100)/nmeta, pct);
1008 m->off = ftello(packfile);
1009 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1012 if (m->deltas == NULL) {
1013 err = packhdr(&nh, buf, sizeof(buf),
1014 m->obj_type, raw->size);
1017 err = hwrite(packfile, buf, nh, &ctx);
1020 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1021 err = got_error_from_errno("fseeko");
1024 err = got_deflate_to_file(&outlen, raw->f, packfile,
1030 struct got_raw_object *base_raw;
1031 err = got_object_raw_open(&base_raw, repo,
1032 &m->prev->id, 8192);
1035 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1038 got_object_raw_close(base_raw);
1039 if (use_offset_deltas && m->prev->off != 0) {
1040 err = packhdr(&nh, buf, sizeof(buf),
1041 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1044 nh += packoff(buf + nh,
1045 m->off - m->prev->off);
1046 err = hwrite(packfile, buf, nh, &ctx);
1050 err = packhdr(&nh, buf, sizeof(buf),
1051 GOT_OBJ_TYPE_REF_DELTA, nd);
1052 err = hwrite(packfile, buf, nh, &ctx);
1055 err = hwrite(packfile, m->prev->id.sha1,
1056 sizeof(m->prev->id.sha1), &ctx);
1060 /* XXX need got_deflate_from_mem() */
1061 delta_file = fmemopen(p, nd, "r");
1062 if (delta_file == NULL) {
1063 err = got_error_from_errno("fmemopen");
1066 err = got_deflate_to_file(&outlen, delta_file,
1074 got_object_raw_close(raw);
1077 fprintf(stderr, "\b\b\b\b100%%\n");
1078 SHA1Final(pack_sha1, &ctx);
1079 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1080 if (n != SHA1_DIGEST_LENGTH)
1081 err = got_ferror(packfile, GOT_ERR_IO);
1087 const struct got_error *
1088 got_pack_create(uint8_t *packsha1, FILE *packfile,
1089 struct got_object_id **theirs, int ntheirs,
1090 struct got_object_id **ours, int nours,
1091 struct got_repository *repo, got_cancel_cb cancel_cb, void *cancel_arg)
1093 const struct got_error *err;
1094 struct got_pack_meta **meta;
1097 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1098 cancel_cb, cancel_arg);
1102 err = pick_deltas(meta, nmeta, repo, cancel_cb, cancel_arg);
1106 err = genpack(packsha1, packfile, meta, nmeta, 1, repo);
1110 free_nmeta(meta, nmeta);