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>
21 #include <sys/socket.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
39 #include "got_error.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_object.h"
46 #include "got_opentemp.h"
48 #include "got_repository_admin.h"
49 #include "got_commit_graph.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_pack_create.h"
62 #include "got_lib_dial.h"
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
73 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
76 const struct got_error *
77 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
78 const char *host, const char *port, const char *server_path, int verbosity)
80 const struct got_error *err = NULL;
85 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
86 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
87 GOT_DIAL_DIRECTION_SEND, verbosity);
88 else if (strcmp(proto, "git") == 0)
89 err = got_dial_git(sendfd, host, port, server_path,
90 GOT_DIAL_DIRECTION_SEND);
91 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
92 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
94 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
98 struct pack_progress_arg {
99 got_send_progress_cb progress_cb;
112 static const struct got_error *
113 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
114 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
117 const struct got_error *err;
118 struct pack_progress_arg *a = arg;
120 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
121 packfile_size, ncommits, nobj_total, nobj_deltify,
122 nobj_written, 0, NULL, 0);
126 a->ncolored= ncolored;
129 a->packfile_size = packfile_size;
130 a->ncommits = ncommits;
131 a->nobj_total = nobj_total;
132 a->nobj_deltify = nobj_deltify;
133 a->nobj_written = nobj_written;
137 static const struct got_error *
138 insert_ref(struct got_reflist_head *refs, const char *refname,
139 struct got_repository *repo)
141 const struct got_error *err;
142 struct got_reference *ref;
143 struct got_reflist_entry *new;
145 err = got_ref_open(&ref, repo, refname, 0);
149 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
150 if (err || new == NULL /* duplicate */)
156 static const struct got_error *
157 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
158 struct got_object_id *their_id, struct got_repository *repo,
159 got_cancel_cb cancel_cb, void *cancel_arg)
161 const struct got_error *err = NULL;
162 struct got_object_id *yca_id;
165 err = got_object_get_type(&obj_type, repo, their_id);
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error_fmt(GOT_ERR_OBJ_TYPE,
170 "bad object type on server for %s", refname);
172 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
173 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
177 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
180 * Require a straight line of history between the two commits,
181 * with their commit being older than my commit.
183 * Non-linear situations such as this require a rebase:
185 * (theirs) D F (mine)
193 if (got_object_id_cmp(their_id, yca_id) != 0)
194 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
200 static const struct got_error *
201 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
203 struct got_object_id **new;
204 const size_t alloc_chunksz = 256;
209 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
210 sizeof(struct got_object_id));
212 return got_error_from_errno("recallocarray");
215 *nalloc += alloc_chunksz;
219 static struct got_reference *
220 find_ref(struct got_reflist_head *refs, const char *refname)
222 struct got_reflist_entry *re;
224 TAILQ_FOREACH(re, refs, entry) {
225 if (got_path_cmp(got_ref_get_name(re->ref), refname,
226 strlen(got_ref_get_name(re->ref)),
227 strlen(refname)) == 0) {
235 static struct got_pathlist_entry *
236 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
238 struct got_pathlist_entry *pe;
240 TAILQ_FOREACH(pe, their_refs, entry) {
241 const char *their_refname = pe->path;
242 if (got_path_cmp(their_refname, refname,
243 strlen(their_refname), strlen(refname)) == 0) {
251 static const struct got_error *
252 get_remote_refname(char **remote_refname, const char *remote_name,
255 if (strncmp(refname, "refs/", 5) == 0)
257 if (strncmp(refname, "heads/", 6) == 0)
260 if (asprintf(remote_refname, "refs/remotes/%s/%s",
261 remote_name, refname) == -1)
262 return got_error_from_errno("asprintf");
267 static const struct got_error *
268 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
269 struct got_repository *repo)
271 const struct got_error *err, *unlock_err;
272 struct got_object_id *my_id;
273 struct got_reference *ref = NULL;
274 char *remote_refname = NULL;
277 err = got_ref_resolve(&my_id, repo, my_ref);
281 err = get_remote_refname(&remote_refname, remote_name,
282 got_ref_get_name(my_ref));
286 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
288 if (err->code != GOT_ERR_NOT_REF)
290 err = got_ref_alloc(&ref, remote_refname, my_id);
295 err = got_ref_change_ref(ref, my_id);
300 err = got_ref_write(ref, repo);
304 unlock_err = got_ref_unlock(ref);
305 if (unlock_err && err == NULL)
311 free(remote_refname);
315 const struct got_error*
316 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
317 struct got_pathlist_head *tag_names,
318 struct got_pathlist_head *delete_branches,
319 int verbosity, int overwrite_refs, int sendfd,
320 struct got_repository *repo, got_send_progress_cb progress_cb,
321 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
324 int npackfd = -1, nsendfd = -1;
325 int sendstatus, done = 0;
326 const struct got_error *err;
327 struct imsgbuf sendibuf;
329 struct got_reflist_head refs;
330 struct got_pathlist_head have_refs;
331 struct got_pathlist_head their_refs;
332 struct got_pathlist_entry *pe;
333 struct got_reflist_entry *re;
334 struct got_object_id **our_ids = NULL;
335 struct got_object_id **their_ids = NULL;
336 int i, nours = 0, ntheirs = 0;
337 size_t nalloc_ours = 0, nalloc_theirs = 0;
338 int refs_to_send = 0, refs_to_delete = 0;
339 off_t bytes_sent = 0;
340 struct pack_progress_arg ppa;
341 uint8_t packsha1[SHA1_DIGEST_LENGTH];
342 FILE *packfile = NULL;
345 TAILQ_INIT(&have_refs);
346 TAILQ_INIT(&their_refs);
348 TAILQ_FOREACH(pe, branch_names, entry) {
349 const char *branchname = pe->path;
350 if (strncmp(branchname, "refs/heads/", 11) != 0) {
352 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
353 err = got_error_from_errno("asprintf");
356 err = insert_ref(&refs, s, repo);
359 err = insert_ref(&refs, branchname, repo);
365 TAILQ_FOREACH(pe, delete_branches, entry) {
366 const char *branchname = pe->path;
367 struct got_reference *ref;
368 if (strncmp(branchname, "refs/heads/", 11) != 0) {
369 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
373 ref = find_ref(&refs, branchname);
375 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
376 "changes on %s will be sent to server",
382 TAILQ_FOREACH(pe, tag_names, entry) {
383 const char *tagname = pe->path;
384 if (strncmp(tagname, "refs/tags/", 10) != 0) {
386 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
387 err = got_error_from_errno("asprintf");
390 err = insert_ref(&refs, s, repo);
393 err = insert_ref(&refs, tagname, repo);
399 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
400 err = got_error(GOT_ERR_SEND_EMPTY);
404 TAILQ_FOREACH(re, &refs, entry) {
405 struct got_object_id *id;
408 if (got_ref_is_symbolic(re->ref)) {
409 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
410 "cannot send symbolic reference %s",
411 got_ref_get_name(re->ref));
415 err = got_ref_resolve(&id, repo, re->ref);
418 err = got_object_get_type(&obj_type, repo, id);
423 case GOT_OBJ_TYPE_COMMIT:
424 case GOT_OBJ_TYPE_TAG:
427 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
428 "cannot send %s", got_ref_get_name(re->ref));
433 packfile = got_opentemp();
434 if (packfile == NULL) {
435 err = got_error_from_errno("got_opentemp");
439 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
440 err = got_error_from_errno("socketpair");
446 err = got_error_from_errno("fork");
448 } else if (sendpid == 0){
449 got_privsep_exec_child(imsg_sendfds,
450 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
453 if (close(imsg_sendfds[1]) == -1) {
454 err = got_error_from_errno("close");
457 imsg_init(&sendibuf, imsg_sendfds[0]);
458 nsendfd = dup(sendfd);
460 err = got_error_from_errno("dup");
465 * Convert reflist to pathlist since the privsep layer
466 * is linked into helper programs which lack reference.c.
468 TAILQ_FOREACH(re, &refs, entry) {
469 struct got_object_id *id;
470 err = got_ref_resolve(&id, repo, re->ref);
473 err = got_pathlist_append(&have_refs,
474 got_ref_get_name(re->ref), id);
478 * Also prepare the array of our object IDs which
479 * will be needed for generating a pack file.
481 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
488 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
489 delete_branches, verbosity);
494 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
499 * Process references reported by the server.
500 * Push appropriate object IDs onto the "their IDs" array.
501 * This array will be used to exclude objects which already
502 * exist on the server from our pack file.
504 TAILQ_FOREACH(pe, &their_refs, entry) {
505 const char *refname = pe->path;
506 struct got_object_id *their_id = pe->data;
508 struct got_object *obj;
509 struct got_reference *my_ref = NULL;
512 /* Don't blindly trust the server to send us valid names. */
513 if (!got_ref_name_is_valid(refname))
516 if (strncmp(refname, "refs/tags/", 10) == 0)
519 * Find out whether this is a reference we want to upload.
520 * Otherwise we can still use this reference as a hint to
521 * avoid uploading any objects the server already has.
523 my_ref = find_ref(&refs, refname);
525 struct got_object_id *my_id;
526 err = got_ref_resolve(&my_id, repo, my_ref);
529 if (got_object_id_cmp(my_id, their_id) != 0) {
530 if (!overwrite_refs && is_tag) {
532 GOT_ERR_SEND_TAG_EXISTS,
542 /* Check if their object exists locally. */
543 err = got_object_open(&obj, repo, their_id);
545 if (err->code != GOT_ERR_NO_OBJ)
547 if (!overwrite_refs && my_ref != NULL) {
548 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
554 got_object_close(obj);
558 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
563 /* Enforce linear ancestry if required. */
564 if (!overwrite_refs && my_ref && !is_tag) {
565 struct got_object_id *my_id;
566 err = got_ref_resolve(&my_id, repo, my_ref);
569 err = check_linear_ancestry(refname, my_id,
570 their_id, repo, cancel_cb, cancel_arg);
576 /* Exclude any objects reachable via their ID. */
577 their_ids[ntheirs] = got_object_id_dup(their_id);
578 if (their_ids[ntheirs] == NULL) {
579 err = got_error_from_errno("got_object_id_dup");
583 } else if (!is_tag) {
584 char *remote_refname;
585 struct got_reference *ref;
587 * Exclude any objects which exist on the server
588 * according to a locally cached remote reference.
590 err = get_remote_refname(&remote_refname,
591 remote_name, refname);
594 err = got_ref_open(&ref, repo, remote_refname, 0);
595 free(remote_refname);
597 if (err->code != GOT_ERR_NOT_REF)
600 err = got_ref_resolve(&their_ids[ntheirs],
610 /* Account for any new references we are going to upload. */
611 TAILQ_FOREACH(re, &refs, entry) {
612 if (find_their_ref(&their_refs,
613 got_ref_get_name(re->ref)) == NULL)
617 /* Account for any existing references we are going to delete. */
618 TAILQ_FOREACH(pe, delete_branches, entry) {
619 const char *branchname = pe->path;
620 if (find_their_ref(&their_refs, branchname))
624 if (refs_to_send == 0 && refs_to_delete == 0) {
625 got_privsep_send_stop(imsg_sendfds[0]);
629 if (refs_to_send > 0) {
630 memset(&ppa, 0, sizeof(ppa));
631 ppa.progress_cb = progress_cb;
632 ppa.progress_arg = progress_arg;
633 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
634 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
635 cancel_cb, cancel_arg);
639 if (fflush(packfile) == -1) {
640 err = got_error_from_errno("fflush");
644 npackfd = dup(fileno(packfile));
646 err = got_error_from_errno("dup");
649 err = got_privsep_send_packfd(&sendibuf, npackfd);
654 err = got_privsep_send_packfd(&sendibuf, -1);
661 char *refname = NULL;
662 off_t bytes_sent_cur = 0;
664 err = (*cancel_cb)(cancel_arg);
668 err = got_privsep_recv_send_progress(&done, &bytes_sent,
669 &success, &refname, &sendibuf);
672 if (refname && got_ref_name_is_valid(refname) && success &&
673 strncmp(refname, "refs/tags/", 10) != 0) {
674 struct got_reference *my_ref;
676 * The server has accepted our changes.
677 * Update our reference in refs/remotes/ accordingly.
679 my_ref = find_ref(&refs, refname);
681 err = update_remote_ref(my_ref, remote_name,
687 if (refname != NULL ||
688 bytes_sent_cur != bytes_sent) {
689 err = progress_cb(progress_arg, ppa.ncolored,
690 ppa.nfound, ppa.ntrees, ppa.packfile_size,
691 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
692 ppa.nobj_written, bytes_sent,
698 bytes_sent_cur = bytes_sent;
705 got_privsep_send_stop(imsg_sendfds[0]);
706 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
707 err = got_error_from_errno("waitpid");
709 if (packfile && fclose(packfile) == EOF && err == NULL)
710 err = got_error_from_errno("fclose");
711 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
712 err = got_error_from_errno("close");
713 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
714 err = got_error_from_errno("close");
716 got_ref_list_free(&refs);
717 got_pathlist_free(&have_refs);
718 got_pathlist_free(&their_refs);
719 for (i = 0; i < nours; i++)
722 for (i = 0; i < ntheirs; i++)