add a mini-commit object for use by commit graph
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index d0e80bc..be5add9 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -163,12 +163,12 @@ is_merge_point(struct got_commit_graph_node *node)
}
static const struct got_error *
-detect_changed_path(int *changed, struct got_commit_object *commit,
+detect_changed_path(int *changed, struct got_commit_object_mini *commit,
struct got_object_id *commit_id, const char *path,
struct got_repository *repo)
{
const struct got_error *err = NULL;
- struct got_commit_object *pcommit = NULL;
+ struct got_commit_object_mini *pcommit = NULL;
struct got_tree_object *tree = NULL, *ptree = NULL;
struct got_object_qid *pid;
@@ -194,7 +194,7 @@ detect_changed_path(int *changed, struct got_commit_object *commit,
*changed = 1; /* The path was created in this commit. */
free(obj_id);
} else {
- err = got_object_open_as_commit(&pcommit, repo, pid->id);
+ err = got_object_open_mini_commit(&pcommit, repo, pid->id);
if (err)
goto done;
@@ -211,7 +211,7 @@ done:
if (ptree)
got_object_tree_close(ptree);
if (pcommit)
- got_object_commit_close(pcommit);
+ got_object_mini_commit_close(pcommit);
return err;
}
@@ -275,7 +275,7 @@ close_branch(struct got_commit_graph *graph, struct got_object_id *commit_id)
static const struct got_error *
advance_branch(struct got_commit_graph *graph,
struct got_commit_graph_node *node,
- struct got_object_id *commit_id, struct got_commit_object *commit,
+ struct got_object_id *commit_id, struct got_commit_object_mini *commit,
struct got_repository *repo)
{
const struct got_error *err;
@@ -382,8 +382,8 @@ free_node(struct got_commit_graph_node *node)
static const struct got_error *
add_node(struct got_commit_graph_node **new_node,
struct got_commit_graph *graph, struct got_object_id *commit_id,
- struct got_commit_object *commit, struct got_commit_graph_node *child_node,
- struct got_repository *repo)
+ struct got_commit_object_mini *commit,
+ struct got_commit_graph_node *child_node, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_commit_graph_node *node;
@@ -456,11 +456,11 @@ got_commit_graph_open(struct got_commit_graph **graph,
int first_parent_traversal, struct got_repository *repo)
{
const struct got_error *err = NULL;
- struct got_commit_object *commit;
+ struct got_commit_object_mini *commit;
*graph = NULL;
- err = got_object_open_as_commit(&commit, repo, commit_id);
+ err = got_object_open_mini_commit(&commit, repo, commit_id);
if (err)
return err;
@@ -475,7 +475,7 @@ got_commit_graph_open(struct got_commit_graph **graph,
*graph = alloc_graph(path);
if (*graph == NULL) {
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
return got_error_from_errno();
}
@@ -484,7 +484,7 @@ got_commit_graph_open(struct got_commit_graph **graph,
err = add_node(&(*graph)->head_node, *graph, commit_id, commit, NULL,
repo);
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
if (err) {
got_commit_graph_close(*graph);
*graph = NULL;
@@ -547,20 +547,20 @@ fetch_commits_from_open_branches(int *ncommits,
for (i = 0; i < arg.ntips; i++) {
struct got_object_id *commit_id;
struct got_commit_graph_node *child_node, *new_node;
- struct got_commit_object *commit;
+ struct got_commit_object_mini *commit;
int changed;
commit_id = &graph->tips[i].id;
child_node = graph->tips[i].node;
- err = got_object_open_as_commit(&commit, repo, commit_id);
+ err = got_object_open_mini_commit(&commit, repo, commit_id);
if (err)
break;
err = detect_changed_path(&changed, commit, commit_id,
graph->path, repo);
if (err) {
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
if (err->code != GOT_ERR_NO_OBJ)
break;
err = close_branch(graph, commit_id);
@@ -572,7 +572,7 @@ fetch_commits_from_open_branches(int *ncommits,
*changed_id = commit_id;
err = add_node(&new_node, graph, commit_id, commit, child_node,
repo);
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
if (err)
break;
if (new_node)
@@ -628,21 +628,21 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
{
const struct got_error *err = NULL;
struct got_commit_graph_node *start_node;
- struct got_commit_object *commit;
+ struct got_commit_object_mini *commit;
int changed;
start_node = got_object_idset_get(graph->node_ids, id);
if (start_node == NULL)
return got_error(GOT_ERR_NO_OBJ);
- err = got_object_open_as_commit(&commit, repo, &start_node->id);
+ err = got_object_open_mini_commit(&commit, repo, &start_node->id);
if (err)
return err;
err = detect_changed_path(&changed, commit, &start_node->id,
graph->path, repo);
if (err) {
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
return err;
}
@@ -654,13 +654,13 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
err = fetch_commits_from_open_branches(&ncommits,
&changed_id, graph, repo);
if (err) {
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
return err;
}
}
start_node = got_object_idset_get(graph->node_ids, changed_id);
}
- got_object_commit_close(commit);
+ got_object_mini_commit_close(commit);
graph->iter_node = start_node;
return NULL;
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index fc8b2ee..95f019e 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -48,3 +48,16 @@ struct got_blob_object {
uint8_t *read_buf;
struct got_object_id id;
};
+
+/* Small version of got_commit_object. Used by commit graph. */
+struct got_commit_object_mini {
+ struct got_object_id *tree_id;
+ unsigned int nparents;
+ struct got_object_id_queue parent_ids;
+ struct tm tm_committer; /* UTC */
+};
+
+const struct got_error *
+got_object_open_mini_commit(struct got_commit_object_mini **,
+ struct got_repository *, struct got_object_id *);
+void got_object_mini_commit_close(struct got_commit_object_mini *);
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index b427544..99f5e6b 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -14,7 +14,11 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+const struct got_error *got_object_qid_alloc_partial(struct got_object_qid **);
struct got_commit_object *got_object_commit_alloc_partial(void);
+struct got_commit_object_mini *got_object_mini_commit_alloc_partial(void);
+const struct got_error *got_object_commit_add_parent(struct got_commit_object *,
+ const char *);
struct got_tree_entry *got_alloc_tree_entry_partial(void);
const struct got_error *got_object_read_header_privsep(struct got_object**,
struct got_repository *repo, int);
@@ -23,11 +27,16 @@ const struct got_error *got_object_read_blob_privsep(size_t *, int, int,
const struct got_error *got_object_read_commit_privsep(
struct got_commit_object **, struct got_object *, int,
struct got_repository *);
+const struct got_error *got_object_read_mini_commit_privsep(
+ struct got_commit_object_mini **, struct got_object *, int,
+ struct got_repository *);
const struct got_error *got_object_read_tree_privsep(struct got_tree_object **,
struct got_object *, int, struct got_repository *);
const struct got_error *got_object_parse_commit(struct got_commit_object **,
char *, size_t);
+const struct got_error *got_object_parse_mini_commit(
+ struct got_commit_object_mini **, char *, size_t);
const struct got_error *got_object_parse_tree(struct got_tree_object **,
uint8_t *, size_t);
const struct got_error *got_read_file_to_mem(uint8_t **, size_t *, FILE *);
@@ -42,5 +51,7 @@ const struct got_error *got_object_packed_read_privsep(struct got_object **,
struct got_object_id *);
const struct got_error *got_object_read_packed_commit_privsep(
struct got_commit_object **, struct got_object *, struct got_pack *);
+const struct got_error *got_object_read_packed_mini_commit_privsep(
+ struct got_commit_object_mini **, struct got_object *, struct got_pack *);
const struct got_error *got_object_read_packed_tree_privsep(
struct got_tree_object **, struct got_object *, struct got_pack *);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index b39f11f..e343c37 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -82,6 +82,8 @@ enum got_imsg_type {
GOT_IMSG_COMMIT_REQUEST,
GOT_IMSG_COMMIT,
GOT_IMSG_COMMIT_LOGMSG,
+ GOT_IMSG_MINI_COMMIT_REQUEST,
+ GOT_IMSG_MINI_COMMIT,
GOT_IMSG_TREE_REQUEST,
GOT_IMSG_TREE,
GOT_IMSG_TREE_ENTRY,
@@ -142,6 +144,14 @@ struct got_imsg_commit_object {
*/
} __attribute__((__packed__));
+/* Structure for GOT_IMSG_MINI_COMMIT data. */
+struct got_imsg_commit_object_mini {
+ uint8_t tree_id[SHA1_DIGEST_LENGTH];
+ struct tm tm_committer;
+ int nparents;
+
+ /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
+} __attribute__((__packed__));
/* Structure for GOT_IMSG_TREE_ENTRY. */
struct got_imsg_tree_entry {
@@ -202,8 +212,12 @@ const struct got_error *got_privsep_recv_obj(struct got_object **,
struct imsgbuf *);
const struct got_error *got_privsep_send_commit(struct imsgbuf *,
struct got_commit_object *);
+const struct got_error *got_privsep_send_mini_commit(struct imsgbuf *,
+ struct got_commit_object_mini *);
const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
struct imsgbuf *);
+const struct got_error *got_privsep_recv_mini_commit(
+ struct got_commit_object_mini **, struct imsgbuf *);
const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
struct imsgbuf *);
const struct got_error *got_privsep_send_tree(struct imsgbuf *,
@@ -214,4 +228,5 @@ const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
struct got_pack *, struct got_packidx *);
const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int,
struct got_object_id *);
-const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);
+const struct got_error *got_privsep_send_mini_commit_req(struct imsgbuf *, int,
+ struct got_object *);
diff --git a/lib/object.c b/lib/object.c
index c58c241..de22dce 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -316,6 +316,41 @@ open_commit(struct got_commit_object **commit,
return err;
}
+static const struct got_error *
+open_mini_commit(struct got_commit_object_mini **commit,
+ struct got_repository *repo, struct got_object *obj)
+{
+ const struct got_error *err = NULL;
+
+ *commit = NULL;
+
+ if (obj->type != GOT_OBJ_TYPE_COMMIT)
+ return got_error(GOT_ERR_OBJ_TYPE);
+
+ if (obj->flags & GOT_OBJ_FLAG_PACKED) {
+ struct got_pack *pack;
+ pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+ if (pack == NULL) {
+ err = got_repo_cache_pack(&pack, repo,
+ obj->path_packfile, NULL);
+ if (err)
+ return err;
+ }
+ err = got_object_read_packed_mini_commit_privsep(commit, obj,
+ pack);
+ } else {
+ int fd;
+ err = open_loose_object(&fd, obj, repo);
+ if (err)
+ return err;
+ err = got_object_read_mini_commit_privsep(commit, obj, fd,
+ repo);
+ close(fd);
+ }
+
+ return err;
+}
+
const struct got_error *
got_object_open_as_commit(struct got_commit_object **commit,
struct got_repository *repo, struct got_object_id *id)
@@ -351,6 +386,27 @@ got_object_commit_open(struct got_commit_object **commit,
}
const struct got_error *
+got_object_open_mini_commit(struct got_commit_object_mini **commit,
+ struct got_repository *repo, struct got_object_id *id)
+{
+ const struct got_error *err;
+ struct got_object *obj;
+
+ err = got_object_open(&obj, repo, id);
+ if (err)
+ return err;
+ if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
+ err = got_error(GOT_ERR_OBJ_TYPE);
+ goto done;
+ }
+
+ err = open_mini_commit(commit, repo, obj);
+done:
+ got_object_close(obj);
+ return err;
+}
+
+const struct got_error *
got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
{
const struct got_error *err = NULL;
@@ -1074,7 +1130,6 @@ done:
return err;
}
-
static const struct got_error *
request_commit(struct got_commit_object **commit, struct got_repository *repo,
struct got_object *obj, int fd)
@@ -1091,6 +1146,22 @@ request_commit(struct got_commit_object **commit, struct got_repository *repo,
return got_privsep_recv_commit(commit, ibuf);
}
+static const struct got_error *
+request_mini_commit(struct got_commit_object_mini **commit,
+ struct got_repository *repo, struct got_object *obj, int fd)
+{
+ const struct got_error *err = NULL;
+ struct imsgbuf *ibuf;
+
+ ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
+
+ err = got_privsep_send_mini_commit_req(ibuf, fd, obj);
+ if (err)
+ return err;
+
+ return got_privsep_recv_mini_commit(commit, ibuf);
+}
+
const struct got_error *
got_object_read_packed_commit_privsep(struct got_commit_object **commit,
struct got_object *obj, struct got_pack *pack)
@@ -1105,6 +1176,20 @@ got_object_read_packed_commit_privsep(struct got_commit_object **commit,
}
const struct got_error *
+got_object_read_packed_mini_commit_privsep(struct got_commit_object_mini **commit,
+ struct got_object *obj, struct got_pack *pack)
+{
+ const struct got_error *err = NULL;
+
+ err = got_privsep_send_mini_commit_req(pack->privsep_child->ibuf, -1,
+ obj);
+ if (err)
+ return err;
+
+ return got_privsep_recv_mini_commit(commit, pack->privsep_child->ibuf);
+}
+
+const struct got_error *
got_object_read_commit_privsep(struct got_commit_object **commit,
struct got_object *obj, int obj_fd, struct got_repository *repo)
{
@@ -1141,6 +1226,43 @@ got_object_read_commit_privsep(struct got_commit_object **commit,
return request_commit(commit, repo, obj, obj_fd);
}
+const struct got_error *
+got_object_read_mini_commit_privsep(struct got_commit_object_mini **commit,
+ struct got_object *obj, int obj_fd, struct got_repository *repo)
+{
+ int imsg_fds[2];
+ pid_t pid;
+ struct imsgbuf *ibuf;
+
+ if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
+ return request_mini_commit(commit, repo, obj, obj_fd);
+
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL)
+ return got_error_from_errno();
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
+ return got_error_from_errno();
+
+ pid = fork();
+ if (pid == -1)
+ return got_error_from_errno();
+ else if (pid == 0) {
+ exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
+ repo->path);
+ /* not reached */
+ }
+
+ close(imsg_fds[1]);
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
+ imsg_fds[0];
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
+ imsg_init(ibuf, imsg_fds[0]);
+ repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
+
+ return request_mini_commit(commit, repo, obj, obj_fd);
+}
+
static const struct got_error *
request_tree(struct got_tree_object **tree, struct got_repository *repo,
struct got_object *obj, int fd)
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 07d5265..ebf4613 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -42,10 +42,10 @@
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
-#include "got_lib_privsep.h"
-#include "got_lib_pack.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
+#include "got_lib_privsep.h"
+#include "got_lib_pack.h"
#include "got_lib_object_cache.h"
#include "got_lib_repository.h"
@@ -102,6 +102,24 @@ got_object_close(struct got_object *obj)
free(obj);
}
+const struct got_error *
+got_object_qid_alloc_partial(struct got_object_qid **qid)
+{
+ const struct got_error *err = NULL;
+
+ *qid = malloc(sizeof(**qid));
+ if (*qid == NULL)
+ return got_error_from_errno();
+
+ (*qid)->id = malloc(sizeof(*((*qid)->id)));
+ if ((*qid)->id == NULL) {
+ err = got_error_from_errno();
+ got_object_qid_free(*qid);
+ *qid = NULL;
+ }
+ return err;
+}
+
void
got_object_qid_free(struct got_object_qid *qid)
{
@@ -128,6 +146,25 @@ got_object_commit_alloc_partial(void)
return commit;
}
+struct got_commit_object_mini *
+got_object_mini_commit_alloc_partial(void)
+{
+ struct got_commit_object_mini *commit;
+
+ commit = calloc(1, sizeof(*commit));
+ if (commit == NULL)
+ return NULL;
+ commit->tree_id = calloc(1, sizeof(*commit->tree_id));
+ if (commit->tree_id == NULL) {
+ free(commit);
+ return NULL;
+ }
+
+ SIMPLEQ_INIT(&commit->parent_ids);
+
+ return commit;
+}
+
const struct got_error *
got_object_commit_add_parent(struct got_commit_object *commit,
const char *id_str)
@@ -135,17 +172,34 @@ got_object_commit_add_parent(struct got_commit_object *commit,
const struct got_error *err = NULL;
struct got_object_qid *qid;
- qid = malloc(sizeof(*qid));
- if (qid == NULL)
- return got_error_from_errno();
+ err = got_object_qid_alloc_partial(&qid);
+ if (err)
+ return err;
- qid->id = malloc(sizeof(*qid->id));
- if (qid->id == NULL) {
- err = got_error_from_errno();
- got_object_qid_free(qid);
+ if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ free(qid->id);
+ free(qid);
return err;
}
+ SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
+ commit->nparents++;
+
+ return NULL;
+}
+
+const struct got_error *
+got_object_mini_commit_add_parent(struct got_commit_object_mini *commit,
+ const char *id_str)
+{
+ const struct got_error *err = NULL;
+ struct got_object_qid *qid;
+
+ err = got_object_qid_alloc_partial(&qid);
+ if (err)
+ return err;
+
if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
err = got_error(GOT_ERR_BAD_OBJ_DATA);
free(qid->id);
@@ -255,14 +309,30 @@ got_object_commit_close(struct got_commit_object *commit)
free(commit);
}
+void
+got_object_mini_commit_close(struct got_commit_object_mini *commit)
+{
+ struct got_object_qid *qid;
+
+ while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
+ qid = SIMPLEQ_FIRST(&commit->parent_ids);
+ SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
+ got_object_qid_free(qid);
+ }
+
+ free(commit->tree_id);
+ free(commit);
+}
+
const struct got_error *
-got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
+got_object_parse_commit(struct got_commit_object **commit, char *buf,
+ size_t len)
{
const struct got_error *err = NULL;
char *s = buf;
size_t tlen;
ssize_t remain = (ssize_t)len;
-
+
*commit = got_object_commit_alloc_partial();
if (*commit == NULL)
return got_error_from_errno();
@@ -375,6 +445,109 @@ done:
return err;
}
+const struct got_error *
+got_object_parse_mini_commit(struct got_commit_object_mini **commit, char *buf,
+ size_t len)
+{
+ const struct got_error *err = NULL;
+ char *s = buf;
+ size_t tlen;
+ ssize_t remain = (ssize_t)len;
+
+ *commit = got_object_mini_commit_alloc_partial();
+ if (*commit == NULL)
+ return got_error_from_errno();
+
+ tlen = strlen(GOT_COMMIT_TAG_TREE);
+ if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
+ remain -= tlen;
+ if (remain < SHA1_DIGEST_STRING_LENGTH) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ s += tlen;
+ if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ remain -= SHA1_DIGEST_STRING_LENGTH;
+ s += SHA1_DIGEST_STRING_LENGTH;
+ } else {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+
+ tlen = strlen(GOT_COMMIT_TAG_PARENT);
+ while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
+ remain -= tlen;
+ if (remain < SHA1_DIGEST_STRING_LENGTH) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ s += tlen;
+ err = got_object_mini_commit_add_parent(*commit, s);
+ if (err)
+ goto done;
+
+ remain -= SHA1_DIGEST_STRING_LENGTH;
+ s += SHA1_DIGEST_STRING_LENGTH;
+ }
+
+ tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
+ if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
+ char *p;
+ size_t slen;
+
+ remain -= tlen;
+ if (remain <= 0) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ s += tlen;
+ p = strchr(s, '\n');
+ if (p == NULL) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ *p = '\0';
+ slen = strlen(s);
+ s += slen + 1;
+ remain -= slen + 1;
+ }
+
+ tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
+ if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
+ char *p;
+ size_t slen;
+
+ remain -= tlen;
+ if (remain <= 0) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ s += tlen;
+ p = strchr(s, '\n');
+ if (p == NULL) {
+ err = got_error(GOT_ERR_BAD_OBJ_DATA);
+ goto done;
+ }
+ *p = '\0';
+ slen = strlen(s);
+ err = parse_commit_time(&(*commit)->tm_committer, s);
+ if (err)
+ goto done;
+ s += slen + 1;
+ remain -= slen + 1;
+ }
+
+done:
+ if (err) {
+ got_object_mini_commit_close(*commit);
+ *commit = NULL;
+ }
+ return err;
+}
+
void
got_object_tree_entry_close(struct got_tree_entry *te)
{
diff --git a/lib/privsep.c b/lib/privsep.c
index 2429a94..e0d4a53 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -214,6 +214,20 @@ got_privsep_send_stop(int fd)
return err;
}
+static void
+init_imsg_object(struct got_imsg_object *iobj, struct got_object *obj)
+{
+ memcpy(iobj->id, obj->id.sha1, sizeof(iobj->id));
+ iobj->type = obj->type;
+ iobj->flags = obj->flags;
+ iobj->hdrlen = obj->hdrlen;
+ iobj->size = obj->size;
+ if (iobj->flags & GOT_OBJ_FLAG_PACKED) {
+ iobj->pack_offset = obj->pack_offset;
+ iobj->pack_idx = obj->pack_idx;
+ }
+}
+
const struct got_error *
got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
{
@@ -236,15 +250,7 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
return got_error(GOT_ERR_OBJ_TYPE);
}
- memcpy(iobj.id, obj->id.sha1, sizeof(iobj.id));
- iobj.type = obj->type;
- iobj.flags = obj->flags;
- iobj.hdrlen = obj->hdrlen;
- iobj.size = obj->size;
- if (iobj.flags & GOT_OBJ_FLAG_PACKED) {
- iobj.pack_offset = obj->pack_offset;
- iobj.pack_idx = obj->pack_idx;
- }
+ init_imsg_object(&iobj, obj);
iobjp = &iobj;
iobj_size = sizeof(iobj);
@@ -257,6 +263,28 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
}
const struct got_error *
+got_privsep_send_mini_commit_req(struct imsgbuf *ibuf, int fd,
+ struct got_object *obj)
+{
+ struct got_imsg_object iobj, *iobjp;
+ size_t iobj_size;
+
+ if (obj->type != GOT_OBJ_TYPE_COMMIT)
+ return got_error(GOT_ERR_OBJ_TYPE);
+
+ init_imsg_object(&iobj, obj);
+
+ iobjp = &iobj;
+ iobj_size = sizeof(iobj);
+
+ if (imsg_compose(ibuf, GOT_IMSG_MINI_COMMIT_REQUEST, 0, 0, fd,
+ iobjp, iobj_size) == -1)
+ return got_error_from_errno();
+
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
{
if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
@@ -459,6 +487,46 @@ done:
}
const struct got_error *
+got_privsep_send_mini_commit(struct imsgbuf *ibuf,
+ struct got_commit_object_mini *commit)
+{
+ const struct got_error *err = NULL;
+ struct got_imsg_commit_object_mini icommit;
+ uint8_t *buf;
+ size_t len, total;
+ struct got_object_qid *qid;
+
+ memcpy(icommit.tree_id, commit->tree_id->sha1, sizeof(icommit.tree_id));
+ memcpy(&icommit.tm_committer, &commit->tm_committer,
+ sizeof(icommit.tm_committer));
+ icommit.nparents = commit->nparents;
+
+ total = sizeof(icommit) + icommit.nparents * SHA1_DIGEST_LENGTH;
+
+ buf = malloc(total);
+ if (buf == NULL)
+ return got_error_from_errno();
+
+ len = 0;
+ memcpy(buf + len, &icommit, sizeof(icommit));
+ len += sizeof(icommit);
+ SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
+ memcpy(buf + len, qid->id, SHA1_DIGEST_LENGTH);
+ len += SHA1_DIGEST_LENGTH;
+ }
+
+ if (imsg_compose(ibuf, GOT_IMSG_MINI_COMMIT, 0, 0, -1,
+ buf, len) == -1) {
+ err = got_error_from_errno();
+ }
+
+ free(buf);
+ if (err)
+ return err;
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
@@ -592,17 +660,89 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
for (i = 0; i < icommit.nparents; i++) {
struct got_object_qid *qid;
- qid = calloc(1, sizeof(*qid));
- if (qid == NULL) {
- err = got_error_from_errno();
+ err = got_object_qid_alloc_partial(&qid);
+ if (err)
break;
- }
- qid->id = calloc(1, sizeof(*qid->id));
- if (qid->id == NULL) {
- err = got_error_from_errno();
- free(qid);
+
+ memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
+ sizeof(*qid->id));
+ SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);
+ (*commit)->nparents++;
+ }
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+
+ imsg_free(&imsg);
+
+ return err;
+}
+
+const struct got_error *
+got_privsep_recv_mini_commit(struct got_commit_object_mini **commit,
+ struct imsgbuf *ibuf)
+{
+ const struct got_error *err = NULL;
+ struct imsg imsg;
+ struct got_imsg_commit_object_mini icommit;
+ size_t len, datalen;
+ int i;
+ const size_t min_datalen =
+ MIN(sizeof(struct got_imsg_error),
+ sizeof(struct got_imsg_commit_object_mini));
+ uint8_t *data;
+
+ *commit = NULL;
+
+ err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
+ if (err)
+ return err;
+
+ data = imsg.data;
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+ len = 0;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_ERROR:
+ err = recv_imsg_error(&imsg, datalen);
+ break;
+ case GOT_IMSG_MINI_COMMIT:
+ if (datalen < sizeof(icommit)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+
+ memcpy(&icommit, data, sizeof(icommit));
+ if (datalen != sizeof(icommit) +
+ icommit.nparents * SHA1_DIGEST_LENGTH) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ if (icommit.nparents < 0) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ len += sizeof(icommit);
+
+ *commit = got_object_mini_commit_alloc_partial();
+ if (*commit == NULL) {
+ err = got_error_from_errno();
+ break;
+ }
+
+ memcpy((*commit)->tree_id->sha1, icommit.tree_id,
+ SHA1_DIGEST_LENGTH);
+ memcpy(&(*commit)->tm_committer, &icommit.tm_committer,
+ sizeof((*commit)->tm_committer));
+
+ for (i = 0; i < icommit.nparents; i++) {
+ struct got_object_qid *qid;
+
+ err = got_object_qid_alloc_partial(&qid);
+ if (err)
break;
- }
memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
sizeof(*qid->id));
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index f6b5435..49187ef 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -39,30 +39,60 @@
#include "got_lib_privsep.h"
static const struct got_error *
-read_commit_object(struct got_commit_object **commit, struct got_object *obj,
- FILE *f)
+read_commit_data(uint8_t **p, size_t *len, struct got_object *obj, FILE *f)
{
- const struct got_error *err = NULL;
- size_t len;
- uint8_t *p;
+ const struct got_error *err;
if (obj->flags & GOT_OBJ_FLAG_PACKED)
- err = got_read_file_to_mem(&p, &len, f);
+ err = got_read_file_to_mem(p, len, f);
else
- err = got_inflate_to_mem(&p, &len, f);
+ err = got_inflate_to_mem(p, len, f);
if (err)
return err;
- if (len < obj->hdrlen + obj->size) {
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
- goto done;
+ if (*len < obj->hdrlen + obj->size) {
+ free(*p);
+ *p = NULL;
+ *len = 0;
+ return got_error(GOT_ERR_BAD_OBJ_DATA);
}
/* Skip object header. */
- len -= obj->hdrlen;
+ *len -= obj->hdrlen;
+ return NULL;
+}
+
+static const struct got_error *
+read_commit_object(struct got_commit_object **commit, struct got_object *obj,
+ FILE *f)
+{
+ const struct got_error *err;
+ uint8_t *p;
+ size_t len;
+
+ err = read_commit_data(&p, &len, obj, f);
+ if (err)
+ return err;
+
err = got_object_parse_commit(commit, p + obj->hdrlen, len);
free(p);
-done:
+ return err;
+}
+
+static const struct got_error *
+read_commit_object_mini(struct got_commit_object_mini **commit,
+ struct got_object *obj, FILE *f)
+{
+ const struct got_error *err;
+ size_t len;
+ uint8_t *p;
+
+ err = read_commit_data(&p, &len, obj, f);
+ if (err)
+ return err;
+
+ err = got_object_parse_mini_commit(commit, p + obj->hdrlen, len);
+ free(p);
return err;
}
@@ -70,7 +100,6 @@ int
main(int argc, char *argv[])
{
const struct got_error *err = NULL;
- struct got_commit_object *commit = NULL;
struct imsgbuf ibuf;
size_t datalen;
@@ -90,6 +119,7 @@ main(int argc, char *argv[])
struct got_imsg_object iobj;
FILE *f = NULL;
struct got_object *obj = NULL;
+ int mini = 0;
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
@@ -101,9 +131,16 @@ main(int argc, char *argv[])
if (imsg.hdr.type == GOT_IMSG_STOP)
break;
- if (imsg.hdr.type != GOT_IMSG_COMMIT_REQUEST) {
- err = got_error(GOT_ERR_PRIVSEP_MSG);
- goto done;
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_COMMIT_REQUEST:
+ mini = 0;
+ break;
+ case GOT_IMSG_MINI_COMMIT_REQUEST:
+ mini = 1;
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ goto done;
}
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
@@ -139,11 +176,21 @@ main(int argc, char *argv[])
goto done;
}
- err = read_commit_object(&commit, obj, f);
- if (err)
- goto done;
-
- err = got_privsep_send_commit(&ibuf, commit);
+ if (mini) {
+ struct got_commit_object_mini *commit;
+ err = read_commit_object_mini(&commit, obj, f);
+ if (err)
+ goto done;
+ err = got_privsep_send_mini_commit(&ibuf, commit);
+ got_object_mini_commit_close(commit);
+ } else {
+ struct got_commit_object *commit;
+ err = read_commit_object(&commit, obj, f);
+ if (err)
+ goto done;
+ err = got_privsep_send_commit(&ibuf, commit);
+ got_object_commit_close(commit);
+ }
done:
if (f)
fclose(f);
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 010b14b..1a4046e 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -145,6 +145,44 @@ commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
}
static const struct got_error *
+mini_commit_request(struct imsg *imsg, struct imsgbuf *ibuf,
+ struct got_pack *pack, struct got_packidx *packidx,
+ struct got_object_cache *objcache)
+{
+ const struct got_error *err = NULL;
+ struct got_object *obj = NULL;
+ struct got_commit_object_mini *commit = NULL;
+ uint8_t *buf;
+ size_t len;
+
+ err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
+ GOT_OBJ_TYPE_COMMIT);
+ if (err)
+ return err;
+
+ err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
+ if (err)
+ return err;
+
+ obj->size = len;
+ err = got_object_parse_mini_commit(&commit, buf, len);
+ free(buf);
+
+ err = got_privsep_send_mini_commit(ibuf, commit);
+ if (obj)
+ got_object_close(obj);
+ got_object_mini_commit_close(commit);
+ if (err) {
+ if (err->code == GOT_ERR_PRIVSEP_PIPE)
+ err = NULL;
+ else
+ got_privsep_send_error(ibuf, err);
+ }
+
+ return err;
+}
+
+static const struct got_error *
tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
struct got_packidx *packidx, struct got_object_cache *objcache)
{
@@ -464,6 +502,10 @@ main(int argc, char *argv[])
err = commit_request(&imsg, &ibuf, pack, packidx,
&objcache);
break;
+ case GOT_IMSG_MINI_COMMIT_REQUEST:
+ err = mini_commit_request(&imsg, &ibuf, pack, packidx,
+ &objcache);
+ break;
case GOT_IMSG_TREE_REQUEST:
err = tree_request(&imsg, &ibuf, pack, packidx,
&objcache);