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 "got_compat.h"
20 #include <sys/types.h>
22 #include <sys/queue.h>
24 #include <sys/socket.h>
26 #include <sys/resource.h>
27 #include <sys/socket.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
49 #include "got_opentemp.h"
51 #include "got_repository_admin.h"
52 #include "got_commit_graph.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_hash.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_ratelimit.h"
65 #include "got_lib_pack_create.h"
66 #include "got_lib_dial.h"
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
77 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
80 const struct got_error
*
81 got_send_connect(pid_t
*sendpid
, int *sendfd
, const char *proto
,
82 const char *host
, const char *port
, const char *server_path
, int verbosity
)
84 const struct got_error
*err
= NULL
;
89 if (strcmp(proto
, "ssh") == 0 || strcmp(proto
, "git+ssh") == 0)
90 err
= got_dial_ssh(sendpid
, sendfd
, host
, port
, server_path
,
91 GOT_DIAL_DIRECTION_SEND
, verbosity
);
92 else if (strcmp(proto
, "git") == 0)
93 err
= got_dial_git(sendfd
, host
, port
, server_path
,
94 GOT_DIAL_DIRECTION_SEND
);
95 else if (strcmp(proto
, "http") == 0 || strcmp(proto
, "git+http") == 0)
96 err
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
98 err
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
102 struct pack_progress_arg
{
103 got_send_progress_cb progress_cb
;
116 static const struct got_error
*
117 pack_progress(void *arg
, int ncolored
, int nfound
, int ntrees
,
118 off_t packfile_size
, int ncommits
, int nobj_total
, int nobj_deltify
,
121 const struct got_error
*err
;
122 struct pack_progress_arg
*a
= arg
;
124 err
= a
->progress_cb(a
->progress_arg
, ncolored
, nfound
, ntrees
,
125 packfile_size
, ncommits
, nobj_total
, nobj_deltify
,
126 nobj_written
, 0, NULL
, NULL
, 0);
130 a
->ncolored
= ncolored
;
133 a
->packfile_size
= packfile_size
;
134 a
->ncommits
= ncommits
;
135 a
->nobj_total
= nobj_total
;
136 a
->nobj_deltify
= nobj_deltify
;
137 a
->nobj_written
= nobj_written
;
141 static const struct got_error
*
142 insert_ref(struct got_reflist_head
*refs
, const char *refname
,
143 struct got_repository
*repo
)
145 const struct got_error
*err
;
146 struct got_reference
*ref
;
147 struct got_reflist_entry
*new;
149 err
= got_ref_open(&ref
, repo
, refname
, 0);
153 err
= got_reflist_insert(&new, refs
, ref
, got_ref_cmp_by_name
, NULL
);
154 if (err
|| new == NULL
/* duplicate */)
160 static const struct got_error
*
161 check_linear_ancestry(const char *refname
, struct got_object_id
*my_id
,
162 struct got_object_id
*their_id
, struct got_repository
*repo
,
163 got_cancel_cb cancel_cb
, void *cancel_arg
)
165 const struct got_error
*err
= NULL
;
166 struct got_object_id
*yca_id
;
169 err
= got_object_get_type(&obj_type
, repo
, their_id
);
172 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
)
173 return got_error_fmt(GOT_ERR_OBJ_TYPE
,
174 "bad object type on server for %s", refname
);
176 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
177 my_id
, their_id
, 1, repo
, cancel_cb
, cancel_arg
);
181 return got_error_fmt(GOT_ERR_SEND_ANCESTRY
, "%s", refname
);
184 * Require a straight line of history between the two commits,
185 * with their commit being older than my commit.
187 * Non-linear situations such as this require a rebase:
189 * (theirs) D F (mine)
197 if (got_object_id_cmp(their_id
, yca_id
) != 0)
198 err
= got_error_fmt(GOT_ERR_SEND_ANCESTRY
, "%s", refname
);
204 static const struct got_error
*
205 realloc_ids(struct got_object_id
***ids
, size_t *nalloc
, size_t n
)
207 struct got_object_id
**new;
208 const size_t alloc_chunksz
= 256;
213 new = recallocarray(*ids
, *nalloc
, *nalloc
+ alloc_chunksz
,
214 sizeof(struct got_object_id
));
216 return got_error_from_errno("recallocarray");
219 *nalloc
+= alloc_chunksz
;
223 static struct got_reference
*
224 find_ref(struct got_reflist_head
*refs
, const char *refname
)
226 struct got_reflist_entry
*re
;
228 TAILQ_FOREACH(re
, refs
, entry
) {
229 if (got_path_cmp(got_ref_get_name(re
->ref
), refname
,
230 strlen(got_ref_get_name(re
->ref
)),
231 strlen(refname
)) == 0) {
239 static struct got_pathlist_entry
*
240 find_their_ref(struct got_pathlist_head
*their_refs
, const char *refname
)
242 struct got_pathlist_entry
*pe
;
244 TAILQ_FOREACH(pe
, their_refs
, entry
) {
245 const char *their_refname
= pe
->path
;
246 if (got_path_cmp(their_refname
, refname
,
247 strlen(their_refname
), strlen(refname
)) == 0) {
255 static const struct got_error
*
256 get_remote_refname(char **remote_refname
, const char *remote_name
,
259 if (strncmp(refname
, "refs/", 5) == 0)
261 if (strncmp(refname
, "heads/", 6) == 0)
264 if (asprintf(remote_refname
, "refs/remotes/%s/%s",
265 remote_name
, refname
) == -1)
266 return got_error_from_errno("asprintf");
271 static const struct got_error
*
272 update_remote_ref(struct got_reference
*my_ref
, const char *remote_name
,
273 struct got_repository
*repo
)
275 const struct got_error
*err
, *unlock_err
;
276 struct got_object_id
*my_id
;
277 struct got_reference
*ref
= NULL
;
278 char *remote_refname
= NULL
;
281 err
= got_ref_resolve(&my_id
, repo
, my_ref
);
285 err
= get_remote_refname(&remote_refname
, remote_name
,
286 got_ref_get_name(my_ref
));
290 err
= got_ref_open(&ref
, repo
, remote_refname
, 1 /* lock */);
292 if (err
->code
!= GOT_ERR_NOT_REF
)
294 err
= got_ref_alloc(&ref
, remote_refname
, my_id
);
299 err
= got_ref_change_ref(ref
, my_id
);
304 err
= got_ref_write(ref
, repo
);
308 unlock_err
= got_ref_unlock(ref
);
309 if (unlock_err
&& err
== NULL
)
315 free(remote_refname
);
319 const struct got_error
*
320 got_send_pack(const char *remote_name
, struct got_pathlist_head
*branch_names
,
321 struct got_pathlist_head
*tag_names
,
322 struct got_pathlist_head
*delete_branches
,
323 int verbosity
, int overwrite_refs
, int sendfd
,
324 struct got_repository
*repo
, got_send_progress_cb progress_cb
,
325 void *progress_arg
, got_cancel_cb cancel_cb
, void *cancel_arg
)
328 int npackfd
= -1, nsendfd
= -1;
329 int sendstatus
, done
= 0;
330 const struct got_error
*err
;
331 struct imsgbuf sendibuf
;
333 struct got_reflist_head refs
;
334 struct got_pathlist_head have_refs
;
335 struct got_pathlist_head their_refs
;
336 struct got_pathlist_entry
*pe
;
337 struct got_reflist_entry
*re
;
338 struct got_object_id
**our_ids
= NULL
;
339 struct got_object_id
**their_ids
= NULL
;
340 int i
, nours
= 0, ntheirs
= 0;
341 size_t nalloc_ours
= 0, nalloc_theirs
= 0;
342 int refs_to_send
= 0, refs_to_delete
= 0;
343 off_t bytes_sent
= 0, bytes_sent_cur
= 0;
344 struct pack_progress_arg ppa
;
345 uint8_t packsha1
[SHA1_DIGEST_LENGTH
];
347 FILE *delta_cache
= NULL
;
350 TAILQ_INIT(&have_refs
);
351 TAILQ_INIT(&their_refs
);
353 TAILQ_FOREACH(pe
, branch_names
, entry
) {
354 const char *branchname
= pe
->path
;
355 if (strncmp(branchname
, "refs/heads/", 11) != 0) {
357 if (asprintf(&s
, "refs/heads/%s", branchname
) == -1) {
358 err
= got_error_from_errno("asprintf");
361 err
= insert_ref(&refs
, s
, repo
);
364 err
= insert_ref(&refs
, branchname
, repo
);
370 TAILQ_FOREACH(pe
, delete_branches
, entry
) {
371 const char *branchname
= pe
->path
;
372 struct got_reference
*ref
;
373 if (strncmp(branchname
, "refs/heads/", 11) != 0) {
374 err
= got_error_fmt(GOT_ERR_SEND_DELETE_REF
, "%s",
378 ref
= find_ref(&refs
, branchname
);
380 err
= got_error_fmt(GOT_ERR_SEND_DELETE_REF
,
381 "changes on %s will be sent to server",
387 TAILQ_FOREACH(pe
, tag_names
, entry
) {
388 const char *tagname
= pe
->path
;
389 if (strncmp(tagname
, "refs/tags/", 10) != 0) {
391 if (asprintf(&s
, "refs/tags/%s", tagname
) == -1) {
392 err
= got_error_from_errno("asprintf");
395 err
= insert_ref(&refs
, s
, repo
);
398 err
= insert_ref(&refs
, tagname
, repo
);
404 if (TAILQ_EMPTY(&refs
) && TAILQ_EMPTY(delete_branches
)) {
405 err
= got_error(GOT_ERR_SEND_EMPTY
);
409 TAILQ_FOREACH(re
, &refs
, entry
) {
410 struct got_object_id
*id
;
413 if (got_ref_is_symbolic(re
->ref
)) {
414 err
= got_error_fmt(GOT_ERR_BAD_REF_TYPE
,
415 "cannot send symbolic reference %s",
416 got_ref_get_name(re
->ref
));
420 err
= got_ref_resolve(&id
, repo
, re
->ref
);
423 err
= got_object_get_type(&obj_type
, repo
, id
);
428 case GOT_OBJ_TYPE_COMMIT
:
429 case GOT_OBJ_TYPE_TAG
:
432 err
= got_error_fmt(GOT_ERR_OBJ_TYPE
,
433 "cannot send %s", got_ref_get_name(re
->ref
));
438 packfd
= got_opentempfd();
440 err
= got_error_from_errno("got_opentempfd");
444 delta_cache
= got_opentemp();
445 if (delta_cache
== NULL
) {
446 err
= got_error_from_errno("got_opentemp");
450 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_sendfds
) == -1) {
451 err
= got_error_from_errno("socketpair");
457 err
= got_error_from_errno("fork");
459 } else if (sendpid
== 0){
460 got_privsep_exec_child(imsg_sendfds
,
461 GOT_PATH_PROG_SEND_PACK
, got_repo_get_path(repo
));
464 if (close(imsg_sendfds
[1]) == -1) {
465 err
= got_error_from_errno("close");
468 imsg_init(&sendibuf
, imsg_sendfds
[0]);
469 nsendfd
= dup(sendfd
);
471 err
= got_error_from_errno("dup");
476 * Convert reflist to pathlist since the privsep layer
477 * is linked into helper programs which lack reference.c.
479 TAILQ_FOREACH(re
, &refs
, entry
) {
480 struct got_object_id
*id
;
481 err
= got_ref_resolve(&id
, repo
, re
->ref
);
484 err
= got_pathlist_append(&have_refs
,
485 got_ref_get_name(re
->ref
), id
);
489 * Also prepare the array of our object IDs which
490 * will be needed for generating a pack file.
492 err
= realloc_ids(&our_ids
, &nalloc_ours
, nours
+ 1);
499 err
= got_privsep_send_send_req(&sendibuf
, nsendfd
, &have_refs
,
500 delete_branches
, verbosity
);
505 err
= got_privsep_recv_send_remote_refs(&their_refs
, &sendibuf
);
510 * Process references reported by the server.
511 * Push appropriate object IDs onto the "their IDs" array.
512 * This array will be used to exclude objects which already
513 * exist on the server from our pack file.
515 TAILQ_FOREACH(pe
, &their_refs
, entry
) {
516 const char *refname
= pe
->path
;
517 struct got_object_id
*their_id
= pe
->data
;
519 struct got_object
*obj
;
520 struct got_reference
*my_ref
= NULL
;
523 /* Don't blindly trust the server to send us valid names. */
524 if (!got_ref_name_is_valid(refname
))
527 if (strncmp(refname
, "refs/tags/", 10) == 0)
530 * Find out whether this is a reference we want to upload.
531 * Otherwise we can still use this reference as a hint to
532 * avoid uploading any objects the server already has.
534 my_ref
= find_ref(&refs
, refname
);
536 struct got_object_id
*my_id
;
537 err
= got_ref_resolve(&my_id
, repo
, my_ref
);
540 if (got_object_id_cmp(my_id
, their_id
) != 0) {
541 if (!overwrite_refs
&& is_tag
) {
543 GOT_ERR_SEND_TAG_EXISTS
,
553 /* Check if their object exists locally. */
554 err
= got_object_open(&obj
, repo
, their_id
);
556 if (err
->code
!= GOT_ERR_NO_OBJ
)
558 if (!overwrite_refs
&& my_ref
!= NULL
) {
559 err
= got_error_fmt(GOT_ERR_SEND_ANCESTRY
,
565 got_object_close(obj
);
569 err
= realloc_ids(&their_ids
, &nalloc_theirs
, ntheirs
+ 1);
574 /* Enforce linear ancestry if required. */
575 if (!overwrite_refs
&& my_ref
&& !is_tag
) {
576 struct got_object_id
*my_id
;
577 err
= got_ref_resolve(&my_id
, repo
, my_ref
);
580 err
= check_linear_ancestry(refname
, my_id
,
581 their_id
, repo
, cancel_cb
, cancel_arg
);
587 /* Exclude any objects reachable via their ID. */
588 their_ids
[ntheirs
] = got_object_id_dup(their_id
);
589 if (their_ids
[ntheirs
] == NULL
) {
590 err
= got_error_from_errno("got_object_id_dup");
594 } else if (!is_tag
) {
595 char *remote_refname
;
596 struct got_reference
*ref
;
598 * Exclude any objects which exist on the server
599 * according to a locally cached remote reference.
601 err
= get_remote_refname(&remote_refname
,
602 remote_name
, refname
);
605 err
= got_ref_open(&ref
, repo
, remote_refname
, 0);
606 free(remote_refname
);
608 if (err
->code
!= GOT_ERR_NOT_REF
)
611 err
= got_ref_resolve(&their_ids
[ntheirs
],
621 /* Account for any new references we are going to upload. */
622 TAILQ_FOREACH(re
, &refs
, entry
) {
623 if (find_their_ref(&their_refs
,
624 got_ref_get_name(re
->ref
)) == NULL
)
628 /* Account for any existing references we are going to delete. */
629 TAILQ_FOREACH(pe
, delete_branches
, entry
) {
630 const char *branchname
= pe
->path
;
631 if (find_their_ref(&their_refs
, branchname
))
635 if (refs_to_send
== 0 && refs_to_delete
== 0) {
636 got_privsep_send_stop(imsg_sendfds
[0]);
640 if (refs_to_send
> 0) {
641 struct got_ratelimit rl
;
642 got_ratelimit_init(&rl
, 0, 500);
643 memset(&ppa
, 0, sizeof(ppa
));
644 ppa
.progress_cb
= progress_cb
;
645 ppa
.progress_arg
= progress_arg
;
646 err
= got_pack_create(packsha1
, packfd
, delta_cache
,
647 their_ids
, ntheirs
, our_ids
, nours
, repo
, 0, 1, 0,
648 pack_progress
, &ppa
, &rl
, cancel_cb
, cancel_arg
);
652 npackfd
= dup(packfd
);
654 err
= got_error_from_errno("dup");
657 err
= got_privsep_send_packfd(&sendibuf
, npackfd
);
662 err
= got_privsep_send_packfd(&sendibuf
, -1);
669 char *refname
= NULL
;
673 err
= (*cancel_cb
)(cancel_arg
);
677 err
= got_privsep_recv_send_progress(&done
, &bytes_sent
,
678 &success
, &refname
, &errmsg
, &sendibuf
);
681 if (refname
&& got_ref_name_is_valid(refname
) && success
&&
682 strncmp(refname
, "refs/tags/", 10) != 0) {
683 struct got_reference
*my_ref
;
685 * The server has accepted our changes.
686 * Update our reference in refs/remotes/ accordingly.
688 my_ref
= find_ref(&refs
, refname
);
690 err
= update_remote_ref(my_ref
, remote_name
,
696 if (refname
!= NULL
||
697 bytes_sent_cur
!= bytes_sent
) {
698 err
= progress_cb(progress_arg
, ppa
.ncolored
,
699 ppa
.nfound
, ppa
.ntrees
, ppa
.packfile_size
,
700 ppa
.ncommits
, ppa
.nobj_total
, ppa
.nobj_deltify
,
701 ppa
.nobj_written
, bytes_sent
,
702 refname
, errmsg
, success
);
708 bytes_sent_cur
= bytes_sent
;
716 got_privsep_send_stop(imsg_sendfds
[0]);
717 if (waitpid(sendpid
, &sendstatus
, 0) == -1 && err
== NULL
)
718 err
= got_error_from_errno("waitpid");
720 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
721 err
= got_error_from_errno("close");
722 if (delta_cache
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
723 err
= got_error_from_errno("fclose");
724 if (nsendfd
!= -1 && close(nsendfd
) == -1 && err
== NULL
)
725 err
= got_error_from_errno("close");
726 if (npackfd
!= -1 && close(npackfd
) == -1 && err
== NULL
)
727 err
= got_error_from_errno("close");
729 got_ref_list_free(&refs
);
730 got_pathlist_free(&have_refs
, GOT_PATHLIST_FREE_NONE
);
731 got_pathlist_free(&their_refs
, GOT_PATHLIST_FREE_NONE
);
732 for (i
= 0; i
< nours
; i
++)
735 for (i
= 0; i
< ntheirs
; i
++)