add support for fetching arbitrary references to 'got clone' and 'got fetch'
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
diff --git a/got/got.1 b/got/got.1
index 25d74be..c42f0af 100644
--- a/got/got.1
+++ b/got/got.1
@@ -136,7 +136,7 @@ follows the globbing rules documented in
.It Cm im
Short alias for
.Cm import .
-.It Cm clone Oo Fl a Oc Oo Fl b Ar branch Oc Oo Fl l Oc Oo Fl m Oc Oo Fl q Oc Oo Fl v Oc Ar repository-URL Op Ar directory
+.It Cm clone Oo Fl a Oc Oo Fl b Ar branch Oc Oo Fl l Oc Oo Fl m Oc Oo Fl q Oc Oo Fl v Oc Oo Fl R Ar reference Oc Ar repository-URL Op Ar directory
Clone a Git repository at the specified
.Ar repository-URL
into the specified
@@ -273,6 +273,29 @@ This option will be passed to
if applicable.
Multiple -v options increase the verbosity.
The maximum is 3.
+.It Fl R Ar reference
+In addition to the branches and tags that will be fetched, fetch an arbitrary
+.Ar reference
+from the remote repository's
+.Dq refs/
+namespace.
+This option may be specified multiple times to build a list of additional
+references to fetch.
+.Pp
+The references will be mapped into the cloned repository's
+.Dq refs/remotes/
+namespace, unless the
+.Fl m
+option is used to mirror references directly into the cloned repository's
+.Dq refs/
+namespace.
+.Pp
+.Cm got clone
+will refuse to fetch references from the remote repository's
+.Dq refs/remotes/
+or
+.Dq refs/got/
+namespace.
.El
.It Cm cl
Short alias for
@@ -385,6 +408,30 @@ The same option will be passed to
if applicable.
Multiple -v options increase the verbosity.
The maximum is 3.
+.It Fl R Ar reference
+In addition to the branches and tags that will be fetched, fetch an arbitrary
+.Ar reference
+from the remote repository's
+.Dq refs/
+namespace.
+This option may be specified multiple times to build a list of additional
+references to fetch.
+.Pp
+Each reference will be mapped into the local repository's
+.Dq refs/remotes/
+namespace, unless the local repository was created as a mirror with
+.Cm got clone -m .
+Once a reference has been fetched, a local branch based on it can be
+created with
+.Cm got branch
+if needed.
+.Pp
+.Cm got fetch
+will refuse to fetch references from the remote repository's
+.Dq refs/remotes/
+or
+.Dq refs/got/
+namespace.
.El
.It Cm fe
Short alias for
diff --git a/got/got.c b/got/got.c
index 989af2e..4f87cf6 100644
--- a/got/got.c
+++ b/got/got.c
@@ -812,7 +812,7 @@ __dead static void
usage_clone(void)
{
fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
- "repository-url [directory]\n", getprogname());
+ "[-R reference] repository-url [directory]\n", getprogname());
exit(1);
}
@@ -959,6 +959,65 @@ done:
return err;
}
+static int
+match_wanted_ref(const char *refname, const char *wanted_ref)
+{
+ if (strncmp(refname, "refs/", 5) != 0)
+ return 0;
+ refname += 5;
+
+ /*
+ * Prevent fetching of references that won't make any
+ * sense outside of the remote repository's context.
+ */
+ if (strncmp(refname, "got/", 4) == 0)
+ return 0;
+ if (strncmp(refname, "remotes/", 8) == 0)
+ return 0;
+
+ if (strncmp(wanted_ref, "refs/", 5) == 0)
+ wanted_ref += 5;
+
+ /* Allow prefix match. */
+ if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
+ return 1;
+
+ /* Allow exact match. */
+ return (strcmp(refname, wanted_ref) == 0);
+}
+
+static int
+is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
+{
+ struct got_pathlist_entry *pe;
+
+ TAILQ_FOREACH(pe, wanted_refs, entry) {
+ if (match_wanted_ref(refname, pe->path))
+ return 1;
+ }
+
+ return 0;
+}
+
+static const struct got_error *
+create_wanted_ref(const char *refname, struct got_object_id *id,
+ const char *remote_repo_name, int verbosity, struct got_repository *repo)
+{
+ const struct got_error *err;
+ char *remote_refname;
+
+ if (strncmp("refs/", refname, 5) == 0)
+ refname += 5;
+
+ if (asprintf(&remote_refname, "refs/remotes/%s/%s",
+ remote_repo_name, refname) == -1)
+ return got_error_from_errno("asprintf");
+
+ err = create_ref(remote_refname, id, verbosity, repo);
+ free(remote_refname);
+ return err;
+}
+
static const struct got_error *
cmd_clone(int argc, char *argv[])
{
@@ -968,7 +1027,7 @@ cmd_clone(int argc, char *argv[])
char *default_destdir = NULL, *id_str = NULL;
const char *repo_path;
struct got_repository *repo = NULL;
- struct got_pathlist_head refs, symrefs, wanted_branches;
+ struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
struct got_pathlist_entry *pe;
struct got_object_id *pack_hash = NULL;
int ch, fetchfd = -1, fetchstatus;
@@ -986,8 +1045,9 @@ cmd_clone(int argc, char *argv[])
TAILQ_INIT(&refs);
TAILQ_INIT(&symrefs);
TAILQ_INIT(&wanted_branches);
+ TAILQ_INIT(&wanted_refs);
- while ((ch = getopt(argc, argv, "ab:lmvq")) != -1) {
+ while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
switch (ch) {
case 'a':
fetch_all_branches = 1;
@@ -1013,6 +1073,12 @@ cmd_clone(int argc, char *argv[])
case 'q':
verbosity = -1;
break;
+ case 'R':
+ error = got_pathlist_append(&wanted_refs,
+ optarg, NULL);
+ if (error)
+ return error;
+ break;
default:
usage_clone();
break;
@@ -1032,6 +1098,8 @@ cmd_clone(int argc, char *argv[])
errx(1, "-l and -m options are mutually exclusive");
if (verbosity == -1)
errx(1, "-l and -q options are mutually exclusive");
+ if (!TAILQ_EMPTY(&wanted_refs))
+ errx(1, "-l and -R options are mutually exclusive");
}
uri = argv[0];
@@ -1124,8 +1192,9 @@ cmd_clone(int argc, char *argv[])
fpa.verbosity = verbosity;
error = got_fetch_pack(&pack_hash, &refs, &symrefs,
GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
- fetch_all_branches, &wanted_branches, list_refs_only,
- verbosity, fetchfd, repo, fetch_progress, &fpa);
+ fetch_all_branches, &wanted_branches, &wanted_refs,
+ list_refs_only, verbosity, fetchfd, repo,
+ fetch_progress, &fpa);
if (error)
goto done;
@@ -1147,6 +1216,16 @@ cmd_clone(int argc, char *argv[])
struct got_object_id *id = pe->data;
char *remote_refname;
+ if (is_wanted_ref(&wanted_refs, refname) &&
+ !mirror_references) {
+ error = create_wanted_ref(refname, id,
+ GOT_FETCH_DEFAULT_REMOTE_NAME,
+ verbosity - 1, repo);
+ if (error)
+ goto done;
+ continue;
+ }
+
error = create_ref(refname, id, verbosity - 1, repo);
if (error)
goto done;
@@ -1308,6 +1387,7 @@ done:
}
got_pathlist_free(&symrefs);
got_pathlist_free(&wanted_branches);
+ got_pathlist_free(&wanted_refs);
free(pack_hash);
free(proto);
free(host);
@@ -1385,7 +1465,8 @@ __dead static void
usage_fetch(void)
{
fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
- "[-r repository-path] [-t] [-q] [-v] [remote-repository-name]\n",
+ "[-r repository-path] [-t] [-q] [-v] [-R reference] "
+ "[remote-repository-name]\n",
getprogname());
exit(1);
}
@@ -1443,6 +1524,35 @@ delete_missing_refs(struct got_pathlist_head *their_refs,
}
static const struct got_error *
+update_wanted_ref(const char *refname, struct got_object_id *id,
+ const char *remote_repo_name, int verbosity, struct got_repository *repo)
+{
+ const struct got_error *err;
+ char *remote_refname;
+ struct got_reference *ref;
+
+ if (strncmp("refs/", refname, 5) == 0)
+ refname += 5;
+
+ if (asprintf(&remote_refname, "refs/remotes/%s/%s",
+ remote_repo_name, refname) == -1)
+ return got_error_from_errno("asprintf");
+
+ err = got_ref_open(&ref, repo, remote_refname, 0);
+ if (err) {
+ if (err->code != GOT_ERR_NOT_REF)
+ goto done;
+ err = create_ref(remote_refname, id, verbosity, repo);
+ } else {
+ err = update_ref(ref, id, 0, verbosity, repo);
+ got_ref_close(ref);
+ }
+done:
+ free(remote_refname);
+ return err;
+}
+
+static const struct got_error *
cmd_fetch(int argc, char *argv[])
{
const struct got_error *error = NULL;
@@ -1455,7 +1565,7 @@ cmd_fetch(int argc, char *argv[])
char *id_str = NULL;
struct got_repository *repo = NULL;
struct got_worktree *worktree = NULL;
- struct got_pathlist_head refs, symrefs, wanted_branches;
+ struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
struct got_pathlist_entry *pe;
struct got_object_id *pack_hash = NULL;
int i, ch, fetchfd = -1, fetchstatus;
@@ -1467,8 +1577,9 @@ cmd_fetch(int argc, char *argv[])
TAILQ_INIT(&refs);
TAILQ_INIT(&symrefs);
TAILQ_INIT(&wanted_branches);
+ TAILQ_INIT(&wanted_refs);
- while ((ch = getopt(argc, argv, "ab:dlr:tvq")) != -1) {
+ while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
switch (ch) {
case 'a':
fetch_all_branches = 1;
@@ -1504,6 +1615,12 @@ cmd_fetch(int argc, char *argv[])
case 'q':
verbosity = -1;
break;
+ case 'R':
+ error = got_pathlist_append(&wanted_refs,
+ optarg, NULL);
+ if (error)
+ return error;
+ break;
default:
usage_fetch();
break;
@@ -1628,7 +1745,8 @@ cmd_fetch(int argc, char *argv[])
fpa.verbosity = verbosity;
error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
remote->mirror_references, fetch_all_branches, &wanted_branches,
- list_refs_only, verbosity, fetchfd, repo, fetch_progress, &fpa);
+ &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
+ fetch_progress, &fpa);
if (error)
goto done;
@@ -1661,6 +1779,15 @@ cmd_fetch(int argc, char *argv[])
struct got_reference *ref;
char *remote_refname;
+ if (is_wanted_ref(&wanted_refs, refname) &&
+ !remote->mirror_references) {
+ error = update_wanted_ref(refname, id,
+ remote->name, verbosity, repo);
+ if (error)
+ goto done;
+ continue;
+ }
+
if (remote->mirror_references ||
strncmp("refs/tags/", refname, 10) == 0) {
error = got_ref_open(&ref, repo, refname, 0);
@@ -1740,6 +1867,7 @@ done:
}
got_pathlist_free(&symrefs);
got_pathlist_free(&wanted_branches);
+ got_pathlist_free(&wanted_refs);
free(id_str);
free(cwd);
free(repo_path);
diff --git a/include/got_error.h b/include/got_error.h
index 7d8cd87..dd38ef5 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -139,6 +139,7 @@
#define GOT_ERR_BAD_PACKET 122
#define GOT_ERR_NO_REMOTE 123
#define GOT_ERR_FETCH_NO_BRANCH 124
+#define GOT_ERR_FETCH_BAD_REF 125
static const struct got_error {
int code;
@@ -284,6 +285,7 @@ static const struct got_error {
{ GOT_ERR_BAD_PACKET, "bad packet received" },
{ GOT_ERR_NO_REMOTE, "remote repository not found" },
{ GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
+ { GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
};
/*
diff --git a/include/got_fetch.h b/include/got_fetch.h
index 42a3f63..ec4d2dc 100644
--- a/include/got_fetch.h
+++ b/include/got_fetch.h
@@ -66,5 +66,5 @@ typedef const struct got_error *(*got_fetch_progress_cb)(void *,
*/
const struct got_error *got_fetch_pack(struct got_object_id **,
struct got_pathlist_head *, struct got_pathlist_head *, const char *,
- int, int, struct got_pathlist_head *, int, int, int,
- struct got_repository *, got_fetch_progress_cb, void *);
+ int, int, struct got_pathlist_head *, struct got_pathlist_head *,
+ int, int, int, struct got_repository *, got_fetch_progress_cb, void *);
diff --git a/lib/fetch.c b/lib/fetch.c
index 4442a20..9cdf678 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -391,8 +391,9 @@ const struct got_error*
got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
struct got_pathlist_head *symrefs, const char *remote_name,
int mirror_references, int fetch_all_branches,
- struct got_pathlist_head *wanted_branches, int list_refs_only,
- int verbosity, int fetchfd, struct got_repository *repo,
+ struct got_pathlist_head *wanted_branches,
+ struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity,
+ int fetchfd, struct got_repository *repo,
got_fetch_progress_cb progress_cb, void *progress_arg)
{
int imsg_fetchfds[2], imsg_idxfds[2];
@@ -417,6 +418,19 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
char *path;
char *progress = NULL;
+ /*
+ * Prevent fetching of references that won't make any
+ * sense outside of the remote repository's context.
+ */
+ TAILQ_FOREACH(pe, wanted_refs, entry) {
+ const char *refname = pe->path;
+ if (strncmp(refname, "refs/got/", 9) == 0 ||
+ strncmp(refname, "got/", 4) == 0 ||
+ strncmp(refname, "refs/remotes/", 13) == 0 ||
+ strncmp(refname, "remotes/", 8) == 0)
+ return got_error_path(refname, GOT_ERR_FETCH_BAD_REF);
+ }
+
if (!list_refs_only)
repo_path = got_repo_get_path_git_dir(repo);
@@ -577,7 +591,8 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
goto done;
}
err = got_privsep_send_fetch_req(&fetchibuf, nfetchfd, &have_refs,
- fetch_all_branches, wanted_branches, list_refs_only, verbosity);
+ fetch_all_branches, wanted_branches, wanted_refs,
+ list_refs_only, verbosity);
if (err != NULL)
goto done;
nfetchfd = -1;
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 8c31f36..dcfb30c 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -112,6 +112,7 @@ enum got_imsg_type {
GOT_IMSG_FETCH_REQUEST,
GOT_IMSG_FETCH_HAVE_REF,
GOT_IMSG_FETCH_WANTED_BRANCH,
+ GOT_IMSG_FETCH_WANTED_REF,
GOT_IMSG_FETCH_OUTFD,
GOT_IMSG_FETCH_SYMREFS,
GOT_IMSG_FETCH_REF,
@@ -254,6 +255,12 @@ struct got_imsg_fetch_wanted_branch {
/* Followed by name_len data bytes. */
} __attribute__((__packed__));
+/* Structure for GOT_IMSG_FETCH_WANTED_REF data. */
+struct got_imsg_fetch_wanted_ref {
+ size_t name_len;
+ /* Followed by name_len data bytes. */
+} __attribute__((__packed__));
+
/* Structure for GOT_IMSG_FETCH_REQUEST data. */
struct got_imsg_fetch_request {
int fetch_all_branches;
@@ -261,8 +268,10 @@ struct got_imsg_fetch_request {
int verbosity;
size_t n_have_refs;
size_t n_wanted_branches;
+ size_t n_wanted_refs;
/* Followed by n_have_refs GOT_IMSG_FETCH_HAVE_REF messages. */
/* Followed by n_wanted_branches times GOT_IMSG_FETCH_WANTED_BRANCH. */
+ /* Followed by n_wanted_refs times GOT_IMSG_FETCH_WANTED_REF. */
} __attribute__((__packed__));
/* Structures for GOT_IMSG_FETCH_SYMREFS data. */
@@ -403,7 +412,8 @@ const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
int *, int *, struct imsgbuf *ibuf);
const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
- struct got_pathlist_head *, int, struct got_pathlist_head *, int, int);
+ struct got_pathlist_head *, int, struct got_pathlist_head *,
+ struct got_pathlist_head *, int, int);
const struct got_error *got_privsep_send_fetch_outfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_fetch_symrefs(struct imsgbuf *,
struct got_pathlist_head *);
diff --git a/lib/privsep.c b/lib/privsep.c
index 8c47b66..3e7631e 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -413,8 +413,8 @@ got_privsep_send_obj(struct imsgbuf *ibuf, struct got_object *obj)
const struct got_error *
got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
struct got_pathlist_head *have_refs, int fetch_all_branches,
- struct got_pathlist_head *wanted_branches, int list_refs_only,
- int verbosity)
+ struct got_pathlist_head *wanted_branches,
+ struct got_pathlist_head *wanted_refs, int list_refs_only, int verbosity)
{
const struct got_error *err = NULL;
struct ibuf *wbuf;
@@ -430,6 +430,8 @@ got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
fetchreq.n_have_refs++;
TAILQ_FOREACH(pe, wanted_branches, entry)
fetchreq.n_wanted_branches++;
+ TAILQ_FOREACH(pe, wanted_refs, entry)
+ fetchreq.n_wanted_refs++;
len = sizeof(struct got_imsg_fetch_request);
if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
close(fd);
@@ -514,6 +516,39 @@ got_privsep_send_fetch_req(struct imsgbuf *ibuf, int fd,
return err;
}
+ TAILQ_FOREACH(pe, wanted_refs, entry) {
+ const char *name = pe->path;
+ size_t name_len = pe->path_len;
+
+ len = sizeof(struct got_imsg_fetch_wanted_ref) + name_len;
+ wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_WANTED_REF, 0, 0,
+ len);
+ if (wbuf == NULL)
+ return got_error_from_errno(
+ "imsg_create FETCH_WANTED_REF");
+
+ /* Keep in sync with struct got_imsg_fetch_wanted_ref! */
+ if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
+ err = got_error_from_errno(
+ "imsg_add FETCH_WANTED_REF");
+ ibuf_free(wbuf);
+ return err;
+ }
+ if (imsg_add(wbuf, name, name_len) == -1) {
+ err = got_error_from_errno(
+ "imsg_add FETCH_WANTED_REF");
+ ibuf_free(wbuf);
+ return err;
+ }
+
+ wbuf->fd = -1;
+ imsg_close(ibuf, wbuf);
+ err = flush_imsg(ibuf);
+ if (err)
+ return err;
+ }
+
+
return NULL;
}
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index 70f8e79..8bedcea 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -251,6 +251,33 @@ match_branch(const char *branch, const char *wanted_branch)
return (strcmp(branch + 11, wanted_branch) == 0);
}
+static int
+match_wanted_ref(const char *refname, const char *wanted_ref)
+{
+ if (strncmp(refname, "refs/", 5) != 0)
+ return 0;
+ refname += 5;
+
+ /*
+ * Prevent fetching of references that won't make any
+ * sense outside of the remote repository's context.
+ */
+ if (strncmp(refname, "got/", 4) == 0)
+ return 0;
+ if (strncmp(refname, "remotes/", 8) == 0)
+ return 0;
+
+ if (strncmp(wanted_ref, "refs/", 5) == 0)
+ wanted_ref += 5;
+
+ /* Allow prefix match. */
+ if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
+ return 1;
+
+ /* Allow exact match. */
+ return (strcmp(refname, wanted_ref) == 0);
+}
+
static const struct got_error *
tokenize_refline(char **tokens, char *line, int len, int maxtokens)
{
@@ -488,7 +515,8 @@ fetch_error(const char *buf, size_t len)
static const struct got_error *
fetch_pack(int fd, int packfd, struct got_object_id *packid,
struct got_pathlist_head *have_refs, int fetch_all_branches,
- struct got_pathlist_head *wanted_branches, int list_refs_only,
+ struct got_pathlist_head *wanted_branches,
+ struct got_pathlist_head *wanted_refs, int list_refs_only,
struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
@@ -594,7 +622,21 @@ fetch_pack(int fd, int packfd, struct got_object_id *packid,
found_branch = 1;
}
} else if (strncmp(refname, "refs/tags/", 10) != 0) {
- if (!list_refs_only) {
+ if (!TAILQ_EMPTY(wanted_refs)) {
+ TAILQ_FOREACH(pe, wanted_refs, entry) {
+ if (match_wanted_ref(refname, pe->path))
+ break;
+ }
+ if (pe == NULL) {
+ if (chattygot) {
+ fprintf(stderr,
+ "%s: ignoring %s\n",
+ getprogname(), refname);
+ }
+ continue;
+ }
+ found_branch = 1;
+ } else if (!list_refs_only) {
if (chattygot) {
fprintf(stderr, "%s: ignoring %s\n",
getprogname(), refname);
@@ -871,10 +913,12 @@ main(int argc, char **argv)
struct imsg imsg;
struct got_pathlist_head have_refs;
struct got_pathlist_head wanted_branches;
+ struct got_pathlist_head wanted_refs;
struct got_pathlist_entry *pe;
struct got_imsg_fetch_request fetch_req;
struct got_imsg_fetch_have_ref href;
struct got_imsg_fetch_wanted_branch wbranch;
+ struct got_imsg_fetch_wanted_ref wref;
size_t datalen;
#if 0
static int attached;
@@ -884,6 +928,7 @@ main(int argc, char **argv)
TAILQ_INIT(&have_refs);
TAILQ_INIT(&wanted_branches);
+ TAILQ_INIT(&wanted_refs);
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
@@ -1008,6 +1053,47 @@ main(int argc, char **argv)
imsg_free(&imsg);
}
+ for (i = 0; i < fetch_req.n_wanted_refs; i++) {
+ char *refname;
+
+ if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
+ if (err->code == GOT_ERR_PRIVSEP_PIPE)
+ err = NULL;
+ goto done;
+ }
+ if (imsg.hdr.type == GOT_IMSG_STOP)
+ goto done;
+ if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ goto done;
+ }
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+ if (datalen < sizeof(wref)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ goto done;
+ }
+ memcpy(&wref, imsg.data, sizeof(wref));
+ if (datalen - sizeof(wref) < wref.name_len) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ goto done;
+ }
+ refname = malloc(wref.name_len + 1);
+ if (refname == NULL) {
+ err = got_error_from_errno("malloc");
+ goto done;
+ }
+ memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
+ refname[wref.name_len] = '\0';
+
+ err = got_pathlist_append(&wanted_refs, refname, NULL);
+ if (err) {
+ free(refname);
+ goto done;
+ }
+
+ imsg_free(&imsg);
+ }
+
if ((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
err = NULL;
@@ -1027,7 +1113,7 @@ main(int argc, char **argv)
err = fetch_pack(fetchfd, packfd, &packid, &have_refs,
fetch_req.fetch_all_branches, &wanted_branches,
- fetch_req.list_refs_only, &ibuf);
+ &wanted_refs, fetch_req.list_refs_only, &ibuf);
done:
TAILQ_FOREACH(pe, &have_refs, entry) {
free((char *)pe->path);
diff --git a/regress/cmdline/clone.sh b/regress/cmdline/clone.sh
index b408166..f3a8329 100755
--- a/regress/cmdline/clone.sh
+++ b/regress/cmdline/clone.sh
@@ -259,9 +259,120 @@ function test_clone_mirror_all {
test_done "$testroot" "$ret"
}
+function test_clone_reference {
+ local testroot=`test_init clone_reference`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got branch -r $testroot/repo -c $commit_id foo
+ got ref -r $testroot/repo refs/hoo/boo/zoo $commit_id
+ got tag -r $testroot/repo -c $commit_id -m tag "1.0" >/dev/null
+ local tag_id=`got ref -r $testroot/repo -l \
+ | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
+
+ got clone -q -R hoo $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/hoo/boo/zoo: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+function test_clone_branch_and_reference {
+ local testroot=`test_init clone_reference`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got branch -r $testroot/repo -c $commit_id foo
+ got ref -r $testroot/repo refs/hoo/boo/zoo $commit_id
+ got tag -r $testroot/repo -c $commit_id -m tag "1.0" >/dev/null
+ local tag_id=`got ref -r $testroot/repo -l \
+ | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
+
+ got clone -q -R hoo/boo/zoo -b foo $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/foo" > $testroot/stdout.expected
+ echo "refs/heads/foo: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/foo: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/hoo/boo/zoo: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+function test_clone_reference_mirror {
+ local testroot=`test_init clone_reference_mirror`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got branch -r $testroot/repo -c $commit_id foo
+ got ref -r $testroot/repo refs/hoo/boo/zoo $commit_id
+ got tag -r $testroot/repo -c $commit_id -m tag "1.0" >/dev/null
+ local tag_id=`got ref -r $testroot/repo -l \
+ | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
+
+ got clone -q -R hoo -m $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/hoo/boo/zoo: $commit_id" >> $testroot/stdout.expected
+ echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_clone_basic
run_test test_clone_list
run_test test_clone_branch
run_test test_clone_all
run_test test_clone_mirror
run_test test_clone_mirror_all
+run_test test_clone_reference
+run_test test_clone_branch_and_reference
+run_test test_clone_reference_mirror
diff --git a/regress/cmdline/fetch.sh b/regress/cmdline/fetch.sh
index 374586a..ce08ae3 100755
--- a/regress/cmdline/fetch.sh
+++ b/regress/cmdline/fetch.sh
@@ -642,6 +642,92 @@ function test_fetch_update_tag {
test_done "$testroot" "$ret"
}
+function test_fetch_reference {
+ local testroot=`test_init fetch_reference`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got clone -q $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got branch -r $testroot/repo -c $commit_id foo
+ got ref -r $testroot/repo refs/hoo/boo/zoo $commit_id
+ got tag -r $testroot/repo -c $commit_id -m tag "1.0" >/dev/null
+ local tag_id=`got ref -r $testroot/repo -l \
+ | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
+
+ echo "modified alpha on master" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "modified alpha"
+ local commit_id2=`git_show_head $testroot/repo`
+
+ (cd $testroot/repo && git checkout -q foo)
+ echo "modified alpha on foo" > $testroot/repo/alpha
+ git_commit $testroot/repo -m "modified alpha"
+ local commit_id3=`git_show_head $testroot/repo`
+ (cd $testroot/repo && git checkout -q master)
+
+ got fetch -q -r $testroot/repo-clone -R refs/remotes/origin/main \
+ > $testroot/stdout 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got fetch command succeeded unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo -n > $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: refs/remotes/origin/main: reference cannot be fetched" \
+ > $testroot/stderr.expected
+
+ cmp -s $testroot/stderr $testroot/stderr.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got fetch -q -r $testroot/repo-clone -R refs/hoo
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got fetch command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/hoo/boo/zoo: $commit_id" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id2" \
+ >> $testroot/stdout.expected
+ echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+
+}
+
run_test test_fetch_basic
run_test test_fetch_list
run_test test_fetch_branch
@@ -649,3 +735,4 @@ run_test test_fetch_all
run_test test_fetch_empty_packfile
run_test test_fetch_delete_branch
run_test test_fetch_update_tag
+run_test test_fetch_reference