commit 14dbbf48885998b6db226d20b27b77b094ceea23 from: Stefan Sperling date: Sun Apr 10 12:15:46 2022 UTC for clarity, move the coloring loop from findtwixt() into a separate function commit - 1d765da334af12ddd26b666c0294e0d39f45cbdf commit + 14dbbf48885998b6db226d20b27b77b094ceea23 blob - bae2f1f3f3e47feb4dc99c8c49dd89349df27dac blob + 5ad9119523e5995b68797f05b79ea98c1f8efd3f --- lib/pack_create.c +++ lib/pack_create.c @@ -1237,73 +1237,26 @@ done: } static const struct got_error * -findtwixt(struct got_object_id ***res, int *nres, int *ncolored, - struct got_object_id **head, int nhead, - struct got_object_id **tail, int ntail, +color_commits(int *ncolored, struct got_object_id_queue *ids, + struct got_object_idset *keep, struct got_object_idset *drop, struct got_repository *repo, got_pack_progress_cb progress_cb, void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg) { const struct got_error *err = NULL; - struct got_object_id_queue ids; - struct got_object_idset *keep, *drop; + struct got_commit_object *commit = NULL; struct got_object_qid *qid; - int i, ncolor, nkeep; - - STAILQ_INIT(&ids); - *res = NULL; - *nres = 0; - *ncolored = 0; - - keep = got_object_idset_alloc(); - if (keep == NULL) - return got_error_from_errno("got_object_idset_alloc"); - - drop = got_object_idset_alloc(); - if (drop == NULL) { - err = got_error_from_errno("got_object_idset_alloc"); - goto done; - } - for (i = 0; i < nhead; i++) { - struct got_object_id *id = head[i]; - int j, color = COLOR_KEEP; - - if (id == NULL) - continue; - - for (j = 0; j < ntail; j++) { - if (tail[j] != NULL && - got_object_id_cmp(id, tail[j]) == 0) { - color = COLOR_DROP; - break; - } - } + while (!STAILQ_EMPTY(ids)) { + int qcolor, ncolor; - err = queue_commit_or_tag_id(id, color, &ids, repo); - if (err) - goto done; - } - - for (i = 0; i < ntail; i++) { - struct got_object_id *id = tail[i]; - if (id == NULL) - continue; - err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo); - if (err) - goto done; - } - - while (!STAILQ_EMPTY(&ids)) { - int qcolor; - if (cancel_cb) { err = cancel_cb(cancel_arg); if (err) - goto done; + break; } - qid = STAILQ_FIRST(&ids); + qid = STAILQ_FIRST(ids); qcolor = *((int *)qid->data); if (got_object_idset_contains(drop, qid->id)) @@ -1317,11 +1270,11 @@ findtwixt(struct got_object_id ***res, int *nres, int err = report_progress(progress_cb, progress_arg, rl, *ncolored, 0, 0, 0L, 0, 0, 0, 0); if (err) - goto done; + break; if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP && qcolor == COLOR_KEEP)) { - STAILQ_REMOVE_HEAD(&ids, entry); + STAILQ_REMOVE_HEAD(ids, entry); got_object_qid_free(qid); continue; } @@ -1330,7 +1283,7 @@ findtwixt(struct got_object_id ***res, int *nres, int err = drop_commit(keep, drop, qid->id, repo, cancel_cb, cancel_arg); if (err) - goto done; + break; } else if (ncolor == COLOR_BLANK) { struct got_commit_object *commit; const struct got_object_id_queue *parents; @@ -1341,19 +1294,19 @@ findtwixt(struct got_object_id ***res, int *nres, int else err = got_object_idset_add(drop, qid->id, NULL); if (err) - goto done; + break; err = got_object_open_as_commit(&commit, repo, qid->id); if (err) - goto done; + break; parents = got_object_commit_get_parent_ids(commit); if (parents) { STAILQ_FOREACH(pid, parents, entry) { - err = queue_commit_id(&ids, pid->id, + err = queue_commit_id(ids, pid->id, qcolor, repo); if (err) - goto done; + break; } } got_object_commit_close(commit); @@ -1362,13 +1315,80 @@ findtwixt(struct got_object_id ***res, int *nres, int /* should not happen */ err = got_error_fmt(GOT_ERR_NOT_IMPL, "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor); - goto done; + break; } - STAILQ_REMOVE_HEAD(&ids, entry); + STAILQ_REMOVE_HEAD(ids, entry); got_object_qid_free(qid); } + if (commit) + got_object_commit_close(commit); + return err; +} + +static const struct got_error * +findtwixt(struct got_object_id ***res, int *nres, int *ncolored, + struct got_object_id **head, int nhead, + struct got_object_id **tail, int ntail, + struct got_repository *repo, + got_pack_progress_cb progress_cb, void *progress_arg, + struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg) +{ + const struct got_error *err = NULL; + struct got_object_id_queue ids; + struct got_object_idset *keep, *drop; + int i, nkeep; + + STAILQ_INIT(&ids); + *res = NULL; + *nres = 0; + *ncolored = 0; + + keep = got_object_idset_alloc(); + if (keep == NULL) + return got_error_from_errno("got_object_idset_alloc"); + + drop = got_object_idset_alloc(); + if (drop == NULL) { + err = got_error_from_errno("got_object_idset_alloc"); + goto done; + } + + for (i = 0; i < nhead; i++) { + struct got_object_id *id = head[i]; + int j, color = COLOR_KEEP; + + if (id == NULL) + continue; + + for (j = 0; j < ntail; j++) { + if (tail[j] != NULL && + got_object_id_cmp(id, tail[j]) == 0) { + color = COLOR_DROP; + break; + } + } + + err = queue_commit_or_tag_id(id, color, &ids, repo); + if (err) + goto done; + } + + for (i = 0; i < ntail; i++) { + struct got_object_id *id = tail[i]; + if (id == NULL) + continue; + err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo); + if (err) + goto done; + } + + err = color_commits(ncolored, &ids, keep, drop, repo, + progress_cb, progress_arg, rl, cancel_cb, cancel_arg); + if (err) + goto done; + nkeep = got_object_idset_num_elements(keep); if (nkeep > 0) { struct append_id_arg arg;