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>
44 #include <netinet/in.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
55 #include "got_repository_admin.h"
56 #include "got_commit_graph.h"
58 #include "got_lib_delta.h"
59 #include "got_lib_inflate.h"
60 #include "got_lib_object.h"
61 #include "got_lib_object_parse.h"
62 #include "got_lib_object_create.h"
63 #include "got_lib_pack.h"
64 #include "got_lib_sha1.h"
65 #include "got_lib_privsep.h"
66 #include "got_lib_object_cache.h"
67 #include "got_lib_repository.h"
68 #include "got_lib_pack_create.h"
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
79 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
82 static const struct got_error *
83 dial_ssh(pid_t *sendpid, int *sendfd, const char *host, const char *port,
84 const char *path, const char *direction, int verbosity)
86 const struct got_error *error = NULL;
95 argv[i++] = GOT_SEND_PATH_SSH;
98 argv[i++] = (char *)port;
100 if (verbosity == -1) {
103 /* ssh(1) allows up to 3 "-v" options. */
104 for (j = 0; j < MIN(3, verbosity); j++)
108 argv[i++] = (char *)host;
109 argv[i++] = (char *)cmd;
110 argv[i++] = (char *)path;
113 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
114 return got_error_from_errno("socketpair");
118 error = got_error_from_errno("fork");
122 } else if (pid == 0) {
124 if (close(pfd[1]) == -1)
126 if (dup2(pfd[0], 0) == -1)
128 if (dup2(pfd[0], 1) == -1)
130 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
131 if (n < 0 || n >= ssizeof(cmd))
133 if (execv(GOT_SEND_PATH_SSH, argv) == -1)
135 abort(); /* not reached */
137 if (close(pfd[0]) == -1)
138 return got_error_from_errno("close");
145 static const struct got_error *
146 dial_git(int *sendfd, const char *host, const char *port, const char *path,
147 const char *direction)
149 const struct got_error *err = NULL;
150 struct addrinfo hints, *servinfo, *p;
152 int fd = -1, len, r, eaicode;
157 port = GOT_DEFAULT_GIT_PORT_STR;
159 memset(&hints, 0, sizeof hints);
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
162 eaicode = getaddrinfo(host, port, &hints, &servinfo);
165 snprintf(msg, sizeof(msg), "%s: %s", host,
166 gai_strerror(eaicode));
167 return got_error_msg(GOT_ERR_ADDRINFO, msg);
170 for (p = servinfo; p != NULL; p = p->ai_next) {
171 if ((fd = socket(p->ai_family, p->ai_socktype,
172 p->ai_protocol)) == -1)
174 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
178 err = got_error_from_errno("connect");
184 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
185 err = got_error_from_errno("asprintf");
188 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
189 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
191 err = got_error_from_errno("dprintf");
202 const struct got_error *
203 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
204 const char *host, const char *port, const char *server_path, int verbosity)
206 const struct got_error *err = NULL;
211 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
212 err = dial_ssh(sendpid, sendfd, host, port, server_path,
213 "receive", verbosity);
214 else if (strcmp(proto, "git") == 0)
215 err = dial_git(sendfd, host, port, server_path, "receive");
216 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
217 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
219 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
223 struct pack_progress_arg {
224 got_send_progress_cb progress_cb;
234 static const struct got_error *
235 pack_progress(void *arg, off_t packfile_size, int ncommits,
236 int nobj_total, int nobj_deltify, int nobj_written)
238 const struct got_error *err;
239 struct pack_progress_arg *a = arg;
241 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
242 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
246 a->packfile_size = packfile_size;
247 a->ncommits = ncommits;
248 a->nobj_total = nobj_total;
249 a->nobj_deltify = nobj_deltify;
250 a->nobj_written = nobj_written;
254 static const struct got_error *
255 insert_ref(struct got_reflist_head *refs, const char *refname,
256 struct got_repository *repo)
258 const struct got_error *err;
259 struct got_reference *ref;
260 struct got_reflist_entry *new;
262 err = got_ref_open(&ref, repo, refname, 0);
266 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
267 if (err || new == NULL /* duplicate */)
273 static const struct got_error *
274 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
275 struct got_object_id *their_id, struct got_repository *repo,
276 got_cancel_cb cancel_cb, void *cancel_arg)
278 const struct got_error *err = NULL;
279 struct got_object_id *yca_id;
282 err = got_object_get_type(&obj_type, repo, their_id);
285 if (obj_type != GOT_OBJ_TYPE_COMMIT)
286 return got_error_fmt(GOT_ERR_OBJ_TYPE,
287 "bad object type on server for %s", refname);
289 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
290 my_id, their_id, repo, cancel_cb, cancel_arg);
294 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
297 * Require a straight line of history between the two commits,
298 * with their commit being older than my commit.
300 * Non-linear situations such as this require a rebase:
302 * (theirs) D F (mine)
310 if (got_object_id_cmp(their_id, yca_id) != 0)
311 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
317 static const struct got_error *
318 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
320 struct got_object_id **new;
321 const size_t alloc_chunksz = 256;
326 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
327 sizeof(struct got_object_id));
329 return got_error_from_errno("recallocarray");
332 *nalloc += alloc_chunksz;
336 static struct got_reference *
337 find_ref(struct got_reflist_head *refs, const char *refname)
339 struct got_reflist_entry *re;
341 TAILQ_FOREACH(re, refs, entry) {
342 if (got_path_cmp(got_ref_get_name(re->ref), refname,
343 strlen(got_ref_get_name(re->ref)),
344 strlen(refname)) == 0) {
352 static struct got_pathlist_entry *
353 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
355 struct got_pathlist_entry *pe;
357 TAILQ_FOREACH(pe, their_refs, entry) {
358 const char *their_refname = pe->path;
359 if (got_path_cmp(their_refname, refname,
360 strlen(their_refname), strlen(refname)) == 0) {
368 static const struct got_error *
369 get_remote_refname(char **remote_refname, const char *remote_name,
372 if (strncmp(refname, "refs/", 5) == 0)
374 if (strncmp(refname, "heads/", 6) == 0)
377 if (asprintf(remote_refname, "refs/remotes/%s/%s",
378 remote_name, refname) == -1)
379 return got_error_from_errno("asprintf");
384 static const struct got_error *
385 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
386 struct got_repository *repo)
388 const struct got_error *err, *unlock_err;
389 struct got_object_id *my_id;
390 struct got_reference *ref = NULL;
391 char *remote_refname = NULL;
394 err = got_ref_resolve(&my_id, repo, my_ref);
398 err = get_remote_refname(&remote_refname, remote_name,
399 got_ref_get_name(my_ref));
403 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
405 if (err->code != GOT_ERR_NOT_REF)
407 err = got_ref_alloc(&ref, remote_refname, my_id);
412 err = got_ref_change_ref(ref, my_id);
417 err = got_ref_write(ref, repo);
421 unlock_err = got_ref_unlock(ref);
422 if (unlock_err && err == NULL)
428 free(remote_refname);
432 const struct got_error*
433 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
434 struct got_pathlist_head *tag_names,
435 struct got_pathlist_head *delete_branches,
436 int verbosity, int overwrite_refs, int sendfd,
437 struct got_repository *repo, got_send_progress_cb progress_cb,
438 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
441 int npackfd = -1, nsendfd = -1;
442 int sendstatus, done = 0;
443 const struct got_error *err;
444 struct imsgbuf sendibuf;
446 struct got_reflist_head refs;
447 struct got_pathlist_head have_refs;
448 struct got_pathlist_head their_refs;
449 struct got_pathlist_entry *pe;
450 struct got_reflist_entry *re;
451 struct got_object_id **our_ids = NULL;
452 struct got_object_id **their_ids = NULL;
453 struct got_object_id *my_id = NULL;
454 int i, nours = 0, ntheirs = 0;
455 size_t nalloc_ours = 0, nalloc_theirs = 0;
456 int refs_to_send = 0, refs_to_delete = 0;
457 off_t bytes_sent = 0;
458 struct pack_progress_arg ppa;
459 uint8_t packsha1[SHA1_DIGEST_LENGTH];
460 FILE *packfile = NULL;
463 TAILQ_INIT(&have_refs);
464 TAILQ_INIT(&their_refs);
466 TAILQ_FOREACH(pe, branch_names, entry) {
467 const char *branchname = pe->path;
468 if (strncmp(branchname, "refs/heads/", 11) != 0) {
470 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
471 err = got_error_from_errno("asprintf");
474 err = insert_ref(&refs, s, repo);
477 err = insert_ref(&refs, branchname, repo);
483 TAILQ_FOREACH(pe, delete_branches, entry) {
484 const char *branchname = pe->path;
485 struct got_reference *ref;
486 if (strncmp(branchname, "refs/heads/", 11) != 0) {
487 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
491 ref = find_ref(&refs, branchname);
493 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
494 "changes on %s will be sent to server",
500 TAILQ_FOREACH(pe, tag_names, entry) {
501 const char *tagname = pe->path;
502 if (strncmp(tagname, "refs/tags/", 10) != 0) {
504 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
505 err = got_error_from_errno("asprintf");
508 err = insert_ref(&refs, s, repo);
511 err = insert_ref(&refs, tagname, repo);
517 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
518 err = got_error(GOT_ERR_SEND_EMPTY);
522 TAILQ_FOREACH(re, &refs, entry) {
523 struct got_object_id *id;
526 if (got_ref_is_symbolic(re->ref)) {
527 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
528 "cannot send symbolic reference %s",
529 got_ref_get_name(re->ref));
533 err = got_ref_resolve(&id, repo, re->ref);
536 err = got_object_get_type(&obj_type, repo, id);
541 case GOT_OBJ_TYPE_COMMIT:
542 case GOT_OBJ_TYPE_TAG:
545 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
546 "cannot send %s", got_ref_get_name(re->ref));
551 packfile = got_opentemp();
552 if (packfile == NULL) {
553 err = got_error_from_errno("got_opentemp");
557 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
558 err = got_error_from_errno("socketpair");
564 err = got_error_from_errno("fork");
566 } else if (sendpid == 0){
567 got_privsep_exec_child(imsg_sendfds,
568 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
571 if (close(imsg_sendfds[1]) == -1) {
572 err = got_error_from_errno("close");
575 imsg_init(&sendibuf, imsg_sendfds[0]);
576 nsendfd = dup(sendfd);
578 err = got_error_from_errno("dup");
583 * Convert reflist to pathlist since the privsep layer
584 * is linked into helper programs which lack reference.c.
586 TAILQ_FOREACH(re, &refs, entry) {
587 struct got_object_id *id;
588 err = got_ref_resolve(&id, repo, re->ref);
591 err = got_pathlist_append(&have_refs,
592 got_ref_get_name(re->ref), id);
596 * Also prepare the array of our object IDs which
597 * will be needed for generating a pack file.
599 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
606 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
607 delete_branches, verbosity);
612 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
617 * Process references reported by the server.
618 * Push appropriate object IDs onto the "their IDs" array.
619 * This array will be used to exclude objects which already
620 * exist on the server from our pack file.
622 TAILQ_FOREACH(pe, &their_refs, entry) {
623 const char *refname = pe->path;
624 struct got_object_id *their_id = pe->data;
626 struct got_object *obj;
627 struct got_reference *my_ref = NULL;
630 /* Don't blindly trust the server to send us valid names. */
631 if (!got_ref_name_is_valid(refname))
635 * Find out whether this is a reference we want to upload.
636 * Otherwise we can still use this reference as a hint to
637 * avoid uploading any objects the server already has.
639 my_ref = find_ref(&refs, refname);
641 err = got_ref_resolve(&my_id, repo, my_ref);
644 if (got_object_id_cmp(my_id, their_id) == 0) {
653 if (strncmp(refname, "refs/tags/", 10) == 0)
656 /* Prevent tags from being overwritten by default. */
657 if (!overwrite_refs && my_ref && is_tag) {
658 err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
663 /* Check if their object exists locally. */
664 err = got_object_open(&obj, repo, their_id);
666 if (err->code != GOT_ERR_NO_OBJ)
668 if (!overwrite_refs && my_ref != NULL) {
669 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
675 got_object_close(obj);
679 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
684 /* Enforce linear ancestry if required. */
685 if (!overwrite_refs && my_ref && !is_tag) {
686 struct got_object_id *my_id;
687 err = got_ref_resolve(&my_id, repo, my_ref);
690 err = check_linear_ancestry(refname, my_id,
691 their_id, repo, cancel_cb, cancel_arg);
697 /* Exclude any objects reachable via their ID. */
698 their_ids[ntheirs] = got_object_id_dup(their_id);
699 if (their_ids[ntheirs] == NULL) {
700 err = got_error_from_errno("got_object_id_dup");
704 } else if (!is_tag) {
705 char *remote_refname;
706 struct got_reference *ref;
708 * Exclude any objects which exist on the server
709 * according to a locally cached remote reference.
711 err = get_remote_refname(&remote_refname,
712 remote_name, refname);
715 err = got_ref_open(&ref, repo, remote_refname, 0);
716 free(remote_refname);
718 if (err->code != GOT_ERR_NOT_REF)
721 err = got_ref_resolve(&their_ids[ntheirs],
731 /* Account for any new references we are going to upload. */
732 TAILQ_FOREACH(re, &refs, entry) {
733 if (find_their_ref(&their_refs,
734 got_ref_get_name(re->ref)) == NULL)
738 /* Account for any existing references we are going to delete. */
739 TAILQ_FOREACH(pe, delete_branches, entry) {
740 const char *branchname = pe->path;
741 if (find_their_ref(&their_refs, branchname))
745 if (refs_to_send == 0 && refs_to_delete == 0) {
746 got_privsep_send_stop(imsg_sendfds[0]);
750 if (refs_to_send > 0) {
751 memset(&ppa, 0, sizeof(ppa));
752 ppa.progress_cb = progress_cb;
753 ppa.progress_arg = progress_arg;
754 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
755 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
756 cancel_cb, cancel_arg);
760 if (fflush(packfile) == -1) {
761 err = got_error_from_errno("fflush");
765 npackfd = dup(fileno(packfile));
767 err = got_error_from_errno("dup");
770 err = got_privsep_send_packfd(&sendibuf, npackfd);
775 err = got_privsep_send_packfd(&sendibuf, -1);
782 char *refname = NULL;
783 off_t bytes_sent_cur = 0;
785 err = (*cancel_cb)(cancel_arg);
789 err = got_privsep_recv_send_progress(&done, &bytes_sent,
790 &success, &refname, &sendibuf);
793 if (refname && got_ref_name_is_valid(refname) && success &&
794 strncmp(refname, "refs/tags/", 10) != 0) {
795 struct got_reference *my_ref;
797 * The server has accepted our changes.
798 * Update our reference in refs/remotes/ accordingly.
800 my_ref = find_ref(&refs, refname);
802 err = update_remote_ref(my_ref, remote_name,
808 if (refname != NULL ||
809 bytes_sent_cur != bytes_sent) {
810 err = progress_cb(progress_arg, ppa.packfile_size,
811 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
812 ppa.nobj_written, bytes_sent,
818 bytes_sent_cur = bytes_sent;
825 got_privsep_send_stop(imsg_sendfds[0]);
826 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
827 err = got_error_from_errno("waitpid");
829 if (packfile && fclose(packfile) == EOF && err == NULL)
830 err = got_error_from_errno("fclose");
831 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
832 err = got_error_from_errno("close");
833 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
834 err = got_error_from_errno("close");
836 got_ref_list_free(&refs);
837 got_pathlist_free(&have_refs);
838 got_pathlist_free(&their_refs);
839 for (i = 0; i < nours; i++)
842 for (i = 0; i < ntheirs; i++)