2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
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/types.h>
20 #include <sys/queue.h>
22 #include <sys/socket.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
40 #include "got_error.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_object.h"
47 #include "got_opentemp.h"
49 #include "got_repository_admin.h"
50 #include "got_commit_graph.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_pack_create.h"
63 #include "got_lib_dial.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
74 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
77 const struct got_error *
78 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
79 const char *host, const char *port, const char *server_path, int verbosity)
81 const struct got_error *err = NULL;
86 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
87 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
88 GOT_DIAL_DIRECTION_SEND, verbosity);
89 else if (strcmp(proto, "git") == 0)
90 err = got_dial_git(sendfd, host, port, server_path,
91 GOT_DIAL_DIRECTION_SEND);
92 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
93 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
95 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
99 struct pack_progress_arg {
100 got_send_progress_cb progress_cb;
113 static const struct got_error *
114 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
115 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
118 const struct got_error *err;
119 struct pack_progress_arg *a = arg;
121 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
122 packfile_size, ncommits, nobj_total, nobj_deltify,
123 nobj_written, 0, NULL, 0);
127 a->ncolored= ncolored;
130 a->packfile_size = packfile_size;
131 a->ncommits = ncommits;
132 a->nobj_total = nobj_total;
133 a->nobj_deltify = nobj_deltify;
134 a->nobj_written = nobj_written;
138 static const struct got_error *
139 insert_ref(struct got_reflist_head *refs, const char *refname,
140 struct got_repository *repo)
142 const struct got_error *err;
143 struct got_reference *ref;
144 struct got_reflist_entry *new;
146 err = got_ref_open(&ref, repo, refname, 0);
150 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
151 if (err || new == NULL /* duplicate */)
157 static const struct got_error *
158 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
159 struct got_object_id *their_id, struct got_repository *repo,
160 got_cancel_cb cancel_cb, void *cancel_arg)
162 const struct got_error *err = NULL;
163 struct got_object_id *yca_id;
166 err = got_object_get_type(&obj_type, repo, their_id);
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error_fmt(GOT_ERR_OBJ_TYPE,
171 "bad object type on server for %s", refname);
173 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
174 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
178 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
181 * Require a straight line of history between the two commits,
182 * with their commit being older than my commit.
184 * Non-linear situations such as this require a rebase:
186 * (theirs) D F (mine)
194 if (got_object_id_cmp(their_id, yca_id) != 0)
195 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
201 static const struct got_error *
202 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
204 struct got_object_id **new;
205 const size_t alloc_chunksz = 256;
210 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
211 sizeof(struct got_object_id));
213 return got_error_from_errno("recallocarray");
216 *nalloc += alloc_chunksz;
220 static struct got_reference *
221 find_ref(struct got_reflist_head *refs, const char *refname)
223 struct got_reflist_entry *re;
225 TAILQ_FOREACH(re, refs, entry) {
226 if (got_path_cmp(got_ref_get_name(re->ref), refname,
227 strlen(got_ref_get_name(re->ref)),
228 strlen(refname)) == 0) {
236 static struct got_pathlist_entry *
237 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
239 struct got_pathlist_entry *pe;
241 TAILQ_FOREACH(pe, their_refs, entry) {
242 const char *their_refname = pe->path;
243 if (got_path_cmp(their_refname, refname,
244 strlen(their_refname), strlen(refname)) == 0) {
252 static const struct got_error *
253 get_remote_refname(char **remote_refname, const char *remote_name,
256 if (strncmp(refname, "refs/", 5) == 0)
258 if (strncmp(refname, "heads/", 6) == 0)
261 if (asprintf(remote_refname, "refs/remotes/%s/%s",
262 remote_name, refname) == -1)
263 return got_error_from_errno("asprintf");
268 static const struct got_error *
269 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
270 struct got_repository *repo)
272 const struct got_error *err, *unlock_err;
273 struct got_object_id *my_id;
274 struct got_reference *ref = NULL;
275 char *remote_refname = NULL;
278 err = got_ref_resolve(&my_id, repo, my_ref);
282 err = get_remote_refname(&remote_refname, remote_name,
283 got_ref_get_name(my_ref));
287 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
289 if (err->code != GOT_ERR_NOT_REF)
291 err = got_ref_alloc(&ref, remote_refname, my_id);
296 err = got_ref_change_ref(ref, my_id);
301 err = got_ref_write(ref, repo);
305 unlock_err = got_ref_unlock(ref);
306 if (unlock_err && err == NULL)
312 free(remote_refname);
316 const struct got_error*
317 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
318 struct got_pathlist_head *tag_names,
319 struct got_pathlist_head *delete_branches,
320 int verbosity, int overwrite_refs, int sendfd,
321 struct got_repository *repo, got_send_progress_cb progress_cb,
322 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
325 int npackfd = -1, nsendfd = -1;
326 int sendstatus, done = 0;
327 const struct got_error *err;
328 struct imsgbuf sendibuf;
330 struct got_reflist_head refs;
331 struct got_pathlist_head have_refs;
332 struct got_pathlist_head their_refs;
333 struct got_pathlist_entry *pe;
334 struct got_reflist_entry *re;
335 struct got_object_id **our_ids = NULL;
336 struct got_object_id **their_ids = NULL;
337 int i, nours = 0, ntheirs = 0;
338 size_t nalloc_ours = 0, nalloc_theirs = 0;
339 int refs_to_send = 0, refs_to_delete = 0;
340 off_t bytes_sent = 0;
341 struct pack_progress_arg ppa;
342 uint8_t packsha1[SHA1_DIGEST_LENGTH];
343 FILE *packfile = NULL;
346 TAILQ_INIT(&have_refs);
347 TAILQ_INIT(&their_refs);
349 TAILQ_FOREACH(pe, branch_names, entry) {
350 const char *branchname = pe->path;
351 if (strncmp(branchname, "refs/heads/", 11) != 0) {
353 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
354 err = got_error_from_errno("asprintf");
357 err = insert_ref(&refs, s, repo);
360 err = insert_ref(&refs, branchname, repo);
366 TAILQ_FOREACH(pe, delete_branches, entry) {
367 const char *branchname = pe->path;
368 struct got_reference *ref;
369 if (strncmp(branchname, "refs/heads/", 11) != 0) {
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
374 ref = find_ref(&refs, branchname);
376 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
377 "changes on %s will be sent to server",
383 TAILQ_FOREACH(pe, tag_names, entry) {
384 const char *tagname = pe->path;
385 if (strncmp(tagname, "refs/tags/", 10) != 0) {
387 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
388 err = got_error_from_errno("asprintf");
391 err = insert_ref(&refs, s, repo);
394 err = insert_ref(&refs, tagname, repo);
400 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
401 err = got_error(GOT_ERR_SEND_EMPTY);
405 TAILQ_FOREACH(re, &refs, entry) {
406 struct got_object_id *id;
409 if (got_ref_is_symbolic(re->ref)) {
410 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
411 "cannot send symbolic reference %s",
412 got_ref_get_name(re->ref));
416 err = got_ref_resolve(&id, repo, re->ref);
419 err = got_object_get_type(&obj_type, repo, id);
424 case GOT_OBJ_TYPE_COMMIT:
425 case GOT_OBJ_TYPE_TAG:
428 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
429 "cannot send %s", got_ref_get_name(re->ref));
434 packfile = got_opentemp();
435 if (packfile == NULL) {
436 err = got_error_from_errno("got_opentemp");
440 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
441 err = got_error_from_errno("socketpair");
447 err = got_error_from_errno("fork");
449 } else if (sendpid == 0){
450 got_privsep_exec_child(imsg_sendfds,
451 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
454 if (close(imsg_sendfds[1]) == -1) {
455 err = got_error_from_errno("close");
458 imsg_init(&sendibuf, imsg_sendfds[0]);
459 nsendfd = dup(sendfd);
461 err = got_error_from_errno("dup");
466 * Convert reflist to pathlist since the privsep layer
467 * is linked into helper programs which lack reference.c.
469 TAILQ_FOREACH(re, &refs, entry) {
470 struct got_object_id *id;
471 err = got_ref_resolve(&id, repo, re->ref);
474 err = got_pathlist_append(&have_refs,
475 got_ref_get_name(re->ref), id);
479 * Also prepare the array of our object IDs which
480 * will be needed for generating a pack file.
482 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
489 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
490 delete_branches, verbosity);
495 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
500 * Process references reported by the server.
501 * Push appropriate object IDs onto the "their IDs" array.
502 * This array will be used to exclude objects which already
503 * exist on the server from our pack file.
505 TAILQ_FOREACH(pe, &their_refs, entry) {
506 const char *refname = pe->path;
507 struct got_object_id *their_id = pe->data;
509 struct got_object *obj;
510 struct got_reference *my_ref = NULL;
513 /* Don't blindly trust the server to send us valid names. */
514 if (!got_ref_name_is_valid(refname))
517 if (strncmp(refname, "refs/tags/", 10) == 0)
520 * Find out whether this is a reference we want to upload.
521 * Otherwise we can still use this reference as a hint to
522 * avoid uploading any objects the server already has.
524 my_ref = find_ref(&refs, refname);
526 struct got_object_id *my_id;
527 err = got_ref_resolve(&my_id, repo, my_ref);
530 if (got_object_id_cmp(my_id, their_id) != 0) {
531 if (!overwrite_refs && is_tag) {
533 GOT_ERR_SEND_TAG_EXISTS,
543 /* Check if their object exists locally. */
544 err = got_object_open(&obj, repo, their_id);
546 if (err->code != GOT_ERR_NO_OBJ)
548 if (!overwrite_refs && my_ref != NULL) {
549 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
555 got_object_close(obj);
559 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
564 /* Enforce linear ancestry if required. */
565 if (!overwrite_refs && my_ref && !is_tag) {
566 struct got_object_id *my_id;
567 err = got_ref_resolve(&my_id, repo, my_ref);
570 err = check_linear_ancestry(refname, my_id,
571 their_id, repo, cancel_cb, cancel_arg);
577 /* Exclude any objects reachable via their ID. */
578 their_ids[ntheirs] = got_object_id_dup(their_id);
579 if (their_ids[ntheirs] == NULL) {
580 err = got_error_from_errno("got_object_id_dup");
584 } else if (!is_tag) {
585 char *remote_refname;
586 struct got_reference *ref;
588 * Exclude any objects which exist on the server
589 * according to a locally cached remote reference.
591 err = get_remote_refname(&remote_refname,
592 remote_name, refname);
595 err = got_ref_open(&ref, repo, remote_refname, 0);
596 free(remote_refname);
598 if (err->code != GOT_ERR_NOT_REF)
601 err = got_ref_resolve(&their_ids[ntheirs],
611 /* Account for any new references we are going to upload. */
612 TAILQ_FOREACH(re, &refs, entry) {
613 if (find_their_ref(&their_refs,
614 got_ref_get_name(re->ref)) == NULL)
618 /* Account for any existing references we are going to delete. */
619 TAILQ_FOREACH(pe, delete_branches, entry) {
620 const char *branchname = pe->path;
621 if (find_their_ref(&their_refs, branchname))
625 if (refs_to_send == 0 && refs_to_delete == 0) {
626 got_privsep_send_stop(imsg_sendfds[0]);
630 if (refs_to_send > 0) {
631 memset(&ppa, 0, sizeof(ppa));
632 ppa.progress_cb = progress_cb;
633 ppa.progress_arg = progress_arg;
634 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
635 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
636 cancel_cb, cancel_arg);
640 if (fflush(packfile) == -1) {
641 err = got_error_from_errno("fflush");
645 npackfd = dup(fileno(packfile));
647 err = got_error_from_errno("dup");
650 err = got_privsep_send_packfd(&sendibuf, npackfd);
655 err = got_privsep_send_packfd(&sendibuf, -1);
662 char *refname = NULL;
663 off_t bytes_sent_cur = 0;
665 err = (*cancel_cb)(cancel_arg);
669 err = got_privsep_recv_send_progress(&done, &bytes_sent,
670 &success, &refname, &sendibuf);
673 if (refname && got_ref_name_is_valid(refname) && success &&
674 strncmp(refname, "refs/tags/", 10) != 0) {
675 struct got_reference *my_ref;
677 * The server has accepted our changes.
678 * Update our reference in refs/remotes/ accordingly.
680 my_ref = find_ref(&refs, refname);
682 err = update_remote_ref(my_ref, remote_name,
688 if (refname != NULL ||
689 bytes_sent_cur != bytes_sent) {
690 err = progress_cb(progress_arg, ppa.ncolored,
691 ppa.nfound, ppa.ntrees, ppa.packfile_size,
692 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
693 ppa.nobj_written, bytes_sent,
699 bytes_sent_cur = bytes_sent;
706 got_privsep_send_stop(imsg_sendfds[0]);
707 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
708 err = got_error_from_errno("waitpid");
710 if (packfile && fclose(packfile) == EOF && err == NULL)
711 err = got_error_from_errno("fclose");
712 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
713 err = got_error_from_errno("close");
714 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
715 err = got_error_from_errno("close");
717 got_ref_list_free(&refs);
718 got_pathlist_free(&have_refs);
719 got_pathlist_free(&their_refs);
720 for (i = 0; i < nours; i++)
723 for (i = 0; i < ntheirs; i++)