2 f8a36e22 2021-08-26 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 f8a36e22 2021-08-26 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 f8a36e22 2021-08-26 stsp * Permission to use, copy, modify, and distribute this software for any
6 f8a36e22 2021-08-26 stsp * purpose with or without fee is hereby granted, provided that the above
7 f8a36e22 2021-08-26 stsp * copyright notice and this permission notice appear in all copies.
9 f8a36e22 2021-08-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 f8a36e22 2021-08-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 f8a36e22 2021-08-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 f8a36e22 2021-08-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 f8a36e22 2021-08-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 f8a36e22 2021-08-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 f8a36e22 2021-08-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 f8a36e22 2021-08-26 stsp #include <sys/types.h>
19 f8a36e22 2021-08-26 stsp #include <sys/stat.h>
20 f8a36e22 2021-08-26 stsp #include <sys/queue.h>
21 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
22 f8a36e22 2021-08-26 stsp #include <sys/uio.h>
23 f8a36e22 2021-08-26 stsp #include <sys/socket.h>
24 f8a36e22 2021-08-26 stsp #include <sys/wait.h>
25 f8a36e22 2021-08-26 stsp #include <sys/resource.h>
26 f8a36e22 2021-08-26 stsp #include <sys/socket.h>
28 f8a36e22 2021-08-26 stsp #include <endian.h>
29 f8a36e22 2021-08-26 stsp #include <errno.h>
30 f8a36e22 2021-08-26 stsp #include <err.h>
31 f8a36e22 2021-08-26 stsp #include <fcntl.h>
32 f8a36e22 2021-08-26 stsp #include <stdio.h>
33 f8a36e22 2021-08-26 stsp #include <stdlib.h>
34 f8a36e22 2021-08-26 stsp #include <string.h>
35 f8a36e22 2021-08-26 stsp #include <stdint.h>
36 f8a36e22 2021-08-26 stsp #include <sha1.h>
37 f8a36e22 2021-08-26 stsp #include <unistd.h>
38 f8a36e22 2021-08-26 stsp #include <zlib.h>
39 f8a36e22 2021-08-26 stsp #include <ctype.h>
40 f8a36e22 2021-08-26 stsp #include <limits.h>
41 f8a36e22 2021-08-26 stsp #include <imsg.h>
42 f8a36e22 2021-08-26 stsp #include <time.h>
43 f8a36e22 2021-08-26 stsp #include <uuid.h>
45 f8a36e22 2021-08-26 stsp #include "got_error.h"
46 f8a36e22 2021-08-26 stsp #include "got_reference.h"
47 f8a36e22 2021-08-26 stsp #include "got_repository.h"
48 f8a36e22 2021-08-26 stsp #include "got_path.h"
49 f8a36e22 2021-08-26 stsp #include "got_cancel.h"
50 f8a36e22 2021-08-26 stsp #include "got_worktree.h"
51 f8a36e22 2021-08-26 stsp #include "got_object.h"
52 f8a36e22 2021-08-26 stsp #include "got_opentemp.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.h"
54 f8a36e22 2021-08-26 stsp #include "got_repository_admin.h"
55 f8a36e22 2021-08-26 stsp #include "got_commit_graph.h"
57 f8a36e22 2021-08-26 stsp #include "got_lib_delta.h"
58 f8a36e22 2021-08-26 stsp #include "got_lib_inflate.h"
59 f8a36e22 2021-08-26 stsp #include "got_lib_object.h"
60 f8a36e22 2021-08-26 stsp #include "got_lib_object_parse.h"
61 f8a36e22 2021-08-26 stsp #include "got_lib_object_create.h"
62 f8a36e22 2021-08-26 stsp #include "got_lib_pack.h"
63 f8a36e22 2021-08-26 stsp #include "got_lib_sha1.h"
64 f8a36e22 2021-08-26 stsp #include "got_lib_privsep.h"
65 f8a36e22 2021-08-26 stsp #include "got_lib_object_cache.h"
66 f8a36e22 2021-08-26 stsp #include "got_lib_repository.h"
67 cae60ab8 2022-10-18 stsp #include "got_lib_ratelimit.h"
68 f8a36e22 2021-08-26 stsp #include "got_lib_pack_create.h"
69 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
71 f8a36e22 2021-08-26 stsp #ifndef nitems
72 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 f8a36e22 2021-08-26 stsp #ifndef ssizeof
76 f8a36e22 2021-08-26 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
80 f8a36e22 2021-08-26 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
83 f8a36e22 2021-08-26 stsp const struct got_error *
84 f8a36e22 2021-08-26 stsp got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
85 f8a36e22 2021-08-26 stsp const char *host, const char *port, const char *server_path, int verbosity)
87 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
89 f8a36e22 2021-08-26 stsp *sendpid = -1;
90 f8a36e22 2021-08-26 stsp *sendfd = -1;
92 f8a36e22 2021-08-26 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
93 d65a88a2 2021-09-05 stsp err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
94 d65a88a2 2021-09-05 stsp GOT_DIAL_DIRECTION_SEND, verbosity);
95 f8a36e22 2021-08-26 stsp else if (strcmp(proto, "git") == 0)
96 d65a88a2 2021-09-05 stsp err = got_dial_git(sendfd, host, port, server_path,
97 d65a88a2 2021-09-05 stsp GOT_DIAL_DIRECTION_SEND);
98 f8a36e22 2021-08-26 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
99 f8a36e22 2021-08-26 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
101 f8a36e22 2021-08-26 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
102 f8a36e22 2021-08-26 stsp return err;
105 f8a36e22 2021-08-26 stsp struct pack_progress_arg {
106 f8a36e22 2021-08-26 stsp got_send_progress_cb progress_cb;
107 f8a36e22 2021-08-26 stsp void *progress_arg;
109 b8af7c06 2022-03-15 stsp int ncolored;
110 b8af7c06 2022-03-15 stsp int nfound;
111 b8af7c06 2022-03-15 stsp int ntrees;
112 f8a36e22 2021-08-26 stsp off_t packfile_size;
113 f8a36e22 2021-08-26 stsp int ncommits;
114 f8a36e22 2021-08-26 stsp int nobj_total;
115 f8a36e22 2021-08-26 stsp int nobj_deltify;
116 f8a36e22 2021-08-26 stsp int nobj_written;
119 f8a36e22 2021-08-26 stsp static const struct got_error *
120 b8af7c06 2022-03-15 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
121 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
122 b8af7c06 2022-03-15 stsp int nobj_written)
124 f8a36e22 2021-08-26 stsp const struct got_error *err;
125 f8a36e22 2021-08-26 stsp struct pack_progress_arg *a = arg;
127 b8af7c06 2022-03-15 stsp err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
128 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, nobj_deltify,
129 6242c45b 2022-11-14 op nobj_written, 0, NULL, NULL, 0);
131 f8a36e22 2021-08-26 stsp return err;
133 b8af7c06 2022-03-15 stsp a->ncolored= ncolored;
134 b8af7c06 2022-03-15 stsp a->nfound = nfound;
135 b8af7c06 2022-03-15 stsp a->ntrees = ntrees;
136 f8a36e22 2021-08-26 stsp a->packfile_size = packfile_size;
137 f8a36e22 2021-08-26 stsp a->ncommits = ncommits;
138 f8a36e22 2021-08-26 stsp a->nobj_total = nobj_total;
139 f8a36e22 2021-08-26 stsp a->nobj_deltify = nobj_deltify;
140 f8a36e22 2021-08-26 stsp a->nobj_written = nobj_written;
141 f8a36e22 2021-08-26 stsp return NULL;
144 f8a36e22 2021-08-26 stsp static const struct got_error *
145 f8a36e22 2021-08-26 stsp insert_ref(struct got_reflist_head *refs, const char *refname,
146 f8a36e22 2021-08-26 stsp struct got_repository *repo)
148 f8a36e22 2021-08-26 stsp const struct got_error *err;
149 f8a36e22 2021-08-26 stsp struct got_reference *ref;
150 f8a36e22 2021-08-26 stsp struct got_reflist_entry *new;
152 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, refname, 0);
154 f8a36e22 2021-08-26 stsp return err;
156 f8a36e22 2021-08-26 stsp err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
157 f8a36e22 2021-08-26 stsp if (err || new == NULL /* duplicate */)
158 f8a36e22 2021-08-26 stsp got_ref_close(ref);
160 f8a36e22 2021-08-26 stsp return err;
163 f8a36e22 2021-08-26 stsp static const struct got_error *
164 f8a36e22 2021-08-26 stsp check_linear_ancestry(const char *refname, struct got_object_id *my_id,
165 f8a36e22 2021-08-26 stsp struct got_object_id *their_id, struct got_repository *repo,
166 f8a36e22 2021-08-26 stsp got_cancel_cb cancel_cb, void *cancel_arg)
168 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
169 f8a36e22 2021-08-26 stsp struct got_object_id *yca_id;
170 f8a36e22 2021-08-26 stsp int obj_type;
172 f8a36e22 2021-08-26 stsp err = got_object_get_type(&obj_type, repo, their_id);
174 f8a36e22 2021-08-26 stsp return err;
175 f8a36e22 2021-08-26 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
176 f8a36e22 2021-08-26 stsp return got_error_fmt(GOT_ERR_OBJ_TYPE,
177 f8a36e22 2021-08-26 stsp "bad object type on server for %s", refname);
179 f8a36e22 2021-08-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
180 4e91ef15 2021-09-26 stsp my_id, their_id, 1, repo, cancel_cb, cancel_arg);
182 f8a36e22 2021-08-26 stsp return err;
183 f8a36e22 2021-08-26 stsp if (yca_id == NULL)
184 f8a36e22 2021-08-26 stsp return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
187 f8a36e22 2021-08-26 stsp * Require a straight line of history between the two commits,
188 f8a36e22 2021-08-26 stsp * with their commit being older than my commit.
190 f8a36e22 2021-08-26 stsp * Non-linear situations such as this require a rebase:
192 f8a36e22 2021-08-26 stsp * (theirs) D F (mine)
200 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(their_id, yca_id) != 0)
201 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
203 f8a36e22 2021-08-26 stsp free(yca_id);
204 f8a36e22 2021-08-26 stsp return err;
207 f8a36e22 2021-08-26 stsp static const struct got_error *
208 f8a36e22 2021-08-26 stsp realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
210 f8a36e22 2021-08-26 stsp struct got_object_id **new;
211 f8a36e22 2021-08-26 stsp const size_t alloc_chunksz = 256;
213 535e07c7 2021-08-29 naddy if (*nalloc >= n)
214 f8a36e22 2021-08-26 stsp return NULL;
216 f8a36e22 2021-08-26 stsp new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
217 f8a36e22 2021-08-26 stsp sizeof(struct got_object_id));
218 f8a36e22 2021-08-26 stsp if (new == NULL)
219 f8a36e22 2021-08-26 stsp return got_error_from_errno("recallocarray");
221 f8a36e22 2021-08-26 stsp *ids = new;
222 f8a36e22 2021-08-26 stsp *nalloc += alloc_chunksz;
223 f8a36e22 2021-08-26 stsp return NULL;
226 f8a36e22 2021-08-26 stsp static struct got_reference *
227 f8a36e22 2021-08-26 stsp find_ref(struct got_reflist_head *refs, const char *refname)
229 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
231 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, refs, entry) {
232 f8a36e22 2021-08-26 stsp if (got_path_cmp(got_ref_get_name(re->ref), refname,
233 f8a36e22 2021-08-26 stsp strlen(got_ref_get_name(re->ref)),
234 f8a36e22 2021-08-26 stsp strlen(refname)) == 0) {
235 f8a36e22 2021-08-26 stsp return re->ref;
239 f8a36e22 2021-08-26 stsp return NULL;
242 f8a36e22 2021-08-26 stsp static struct got_pathlist_entry *
243 f8a36e22 2021-08-26 stsp find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
245 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
247 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, their_refs, entry) {
248 f8a36e22 2021-08-26 stsp const char *their_refname = pe->path;
249 f8a36e22 2021-08-26 stsp if (got_path_cmp(their_refname, refname,
250 f8a36e22 2021-08-26 stsp strlen(their_refname), strlen(refname)) == 0) {
255 f8a36e22 2021-08-26 stsp return NULL;
258 f8a36e22 2021-08-26 stsp static const struct got_error *
259 f8a36e22 2021-08-26 stsp get_remote_refname(char **remote_refname, const char *remote_name,
260 f8a36e22 2021-08-26 stsp const char *refname)
262 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/", 5) == 0)
263 f8a36e22 2021-08-26 stsp refname += 5;
264 f8a36e22 2021-08-26 stsp if (strncmp(refname, "heads/", 6) == 0)
265 f8a36e22 2021-08-26 stsp refname += 6;
267 f8a36e22 2021-08-26 stsp if (asprintf(remote_refname, "refs/remotes/%s/%s",
268 f8a36e22 2021-08-26 stsp remote_name, refname) == -1)
269 f8a36e22 2021-08-26 stsp return got_error_from_errno("asprintf");
271 f8a36e22 2021-08-26 stsp return NULL;
274 f8a36e22 2021-08-26 stsp static const struct got_error *
275 f8a36e22 2021-08-26 stsp update_remote_ref(struct got_reference *my_ref, const char *remote_name,
276 f8a36e22 2021-08-26 stsp struct got_repository *repo)
278 f8a36e22 2021-08-26 stsp const struct got_error *err, *unlock_err;
279 f8a36e22 2021-08-26 stsp struct got_object_id *my_id;
280 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
281 f8a36e22 2021-08-26 stsp char *remote_refname = NULL;
282 f8a36e22 2021-08-26 stsp int ref_locked = 0;
284 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&my_id, repo, my_ref);
286 f8a36e22 2021-08-26 stsp return err;
288 f8a36e22 2021-08-26 stsp err = get_remote_refname(&remote_refname, remote_name,
289 f8a36e22 2021-08-26 stsp got_ref_get_name(my_ref));
293 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
295 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NOT_REF)
297 f8a36e22 2021-08-26 stsp err = got_ref_alloc(&ref, remote_refname, my_id);
301 f8a36e22 2021-08-26 stsp ref_locked = 1;
302 f8a36e22 2021-08-26 stsp err = got_ref_change_ref(ref, my_id);
307 f8a36e22 2021-08-26 stsp err = got_ref_write(ref, repo);
310 f8a36e22 2021-08-26 stsp if (ref_locked) {
311 f8a36e22 2021-08-26 stsp unlock_err = got_ref_unlock(ref);
312 f8a36e22 2021-08-26 stsp if (unlock_err && err == NULL)
313 f8a36e22 2021-08-26 stsp err = unlock_err;
315 f8a36e22 2021-08-26 stsp got_ref_close(ref);
317 f8a36e22 2021-08-26 stsp free(my_id);
318 f8a36e22 2021-08-26 stsp free(remote_refname);
319 f8a36e22 2021-08-26 stsp return err;
322 f8a36e22 2021-08-26 stsp const struct got_error*
323 f8a36e22 2021-08-26 stsp got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
324 f8a36e22 2021-08-26 stsp struct got_pathlist_head *tag_names,
325 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches,
326 f8a36e22 2021-08-26 stsp int verbosity, int overwrite_refs, int sendfd,
327 f8a36e22 2021-08-26 stsp struct got_repository *repo, got_send_progress_cb progress_cb,
328 f8a36e22 2021-08-26 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
330 f8a36e22 2021-08-26 stsp int imsg_sendfds[2];
331 f8a36e22 2021-08-26 stsp int npackfd = -1, nsendfd = -1;
332 f8a36e22 2021-08-26 stsp int sendstatus, done = 0;
333 f8a36e22 2021-08-26 stsp const struct got_error *err;
334 f8a36e22 2021-08-26 stsp struct imsgbuf sendibuf;
335 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
336 f8a36e22 2021-08-26 stsp struct got_reflist_head refs;
337 f8a36e22 2021-08-26 stsp struct got_pathlist_head have_refs;
338 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
339 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
340 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
341 f8a36e22 2021-08-26 stsp struct got_object_id **our_ids = NULL;
342 f8a36e22 2021-08-26 stsp struct got_object_id **their_ids = NULL;
343 f8a36e22 2021-08-26 stsp int i, nours = 0, ntheirs = 0;
344 f8a36e22 2021-08-26 stsp size_t nalloc_ours = 0, nalloc_theirs = 0;
345 27b75514 2021-08-28 stsp int refs_to_send = 0, refs_to_delete = 0;
346 c46eda88 2022-07-21 florian off_t bytes_sent = 0, bytes_sent_cur = 0;
347 f8a36e22 2021-08-26 stsp struct pack_progress_arg ppa;
348 f8a36e22 2021-08-26 stsp uint8_t packsha1[SHA1_DIGEST_LENGTH];
349 894e4711 2022-10-15 stsp int packfd = -1;
350 a32780aa 2022-10-15 stsp FILE *delta_cache = NULL;
352 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
353 f8a36e22 2021-08-26 stsp TAILQ_INIT(&have_refs);
354 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
356 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, branch_names, entry) {
357 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
358 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
360 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
361 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
364 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, s, repo);
367 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, branchname, repo);
373 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_branches, entry) {
374 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
375 f8a36e22 2021-08-26 stsp struct got_reference *ref;
376 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
377 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
378 f8a36e22 2021-08-26 stsp branchname);
381 f8a36e22 2021-08-26 stsp ref = find_ref(&refs, branchname);
383 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
384 f8a36e22 2021-08-26 stsp "changes on %s will be sent to server",
385 f8a36e22 2021-08-26 stsp branchname);
390 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, tag_names, entry) {
391 f8a36e22 2021-08-26 stsp const char *tagname = pe->path;
392 f8a36e22 2021-08-26 stsp if (strncmp(tagname, "refs/tags/", 10) != 0) {
394 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
395 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
398 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, s, repo);
401 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, tagname, repo);
407 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
408 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_SEND_EMPTY);
412 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &refs, entry) {
413 f8a36e22 2021-08-26 stsp struct got_object_id *id;
414 f8a36e22 2021-08-26 stsp int obj_type;
416 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(re->ref)) {
417 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
418 f8a36e22 2021-08-26 stsp "cannot send symbolic reference %s",
419 f8a36e22 2021-08-26 stsp got_ref_get_name(re->ref));
423 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&id, repo, re->ref);
426 f8a36e22 2021-08-26 stsp err = got_object_get_type(&obj_type, repo, id);
430 f8a36e22 2021-08-26 stsp switch (obj_type) {
431 f8a36e22 2021-08-26 stsp case GOT_OBJ_TYPE_COMMIT:
432 f8a36e22 2021-08-26 stsp case GOT_OBJ_TYPE_TAG:
435 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
436 f8a36e22 2021-08-26 stsp "cannot send %s", got_ref_get_name(re->ref));
441 894e4711 2022-10-15 stsp packfd = got_opentempfd();
442 894e4711 2022-10-15 stsp if (packfd == -1) {
443 894e4711 2022-10-15 stsp err = got_error_from_errno("got_opentempfd");
447 a32780aa 2022-10-15 stsp delta_cache = got_opentemp();
448 a32780aa 2022-10-15 stsp if (delta_cache == NULL) {
449 a32780aa 2022-10-15 stsp err = got_error_from_errno("got_opentemp");
453 f8a36e22 2021-08-26 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
454 f8a36e22 2021-08-26 stsp err = got_error_from_errno("socketpair");
458 f8a36e22 2021-08-26 stsp sendpid = fork();
459 f8a36e22 2021-08-26 stsp if (sendpid == -1) {
460 f8a36e22 2021-08-26 stsp err = got_error_from_errno("fork");
462 f8a36e22 2021-08-26 stsp } else if (sendpid == 0){
463 f8a36e22 2021-08-26 stsp got_privsep_exec_child(imsg_sendfds,
464 f8a36e22 2021-08-26 stsp GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
467 f8a36e22 2021-08-26 stsp if (close(imsg_sendfds[1]) == -1) {
468 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
471 f8a36e22 2021-08-26 stsp imsg_init(&sendibuf, imsg_sendfds[0]);
472 f8a36e22 2021-08-26 stsp nsendfd = dup(sendfd);
473 f8a36e22 2021-08-26 stsp if (nsendfd == -1) {
474 f8a36e22 2021-08-26 stsp err = got_error_from_errno("dup");
479 f8a36e22 2021-08-26 stsp * Convert reflist to pathlist since the privsep layer
480 f8a36e22 2021-08-26 stsp * is linked into helper programs which lack reference.c.
482 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &refs, entry) {
483 f8a36e22 2021-08-26 stsp struct got_object_id *id;
484 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&id, repo, re->ref);
487 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&have_refs,
488 f8a36e22 2021-08-26 stsp got_ref_get_name(re->ref), id);
492 f8a36e22 2021-08-26 stsp * Also prepare the array of our object IDs which
493 f8a36e22 2021-08-26 stsp * will be needed for generating a pack file.
495 535e07c7 2021-08-29 naddy err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
498 f8a36e22 2021-08-26 stsp our_ids[nours] = id;
502 f8a36e22 2021-08-26 stsp err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
503 f8a36e22 2021-08-26 stsp delete_branches, verbosity);
506 f8a36e22 2021-08-26 stsp nsendfd = -1;
508 f8a36e22 2021-08-26 stsp err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
513 f8a36e22 2021-08-26 stsp * Process references reported by the server.
514 f8a36e22 2021-08-26 stsp * Push appropriate object IDs onto the "their IDs" array.
515 f8a36e22 2021-08-26 stsp * This array will be used to exclude objects which already
516 f8a36e22 2021-08-26 stsp * exist on the server from our pack file.
518 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
519 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
520 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = pe->data;
521 f8a36e22 2021-08-26 stsp int have_their_id;
522 f8a36e22 2021-08-26 stsp struct got_object *obj;
523 f8a36e22 2021-08-26 stsp struct got_reference *my_ref = NULL;
524 f8a36e22 2021-08-26 stsp int is_tag = 0;
526 f8a36e22 2021-08-26 stsp /* Don't blindly trust the server to send us valid names. */
527 f8a36e22 2021-08-26 stsp if (!got_ref_name_is_valid(refname))
530 92410079 2021-10-16 stsp if (strncmp(refname, "refs/tags/", 10) == 0)
531 92410079 2021-10-16 stsp is_tag = 1;
533 f8a36e22 2021-08-26 stsp * Find out whether this is a reference we want to upload.
534 f8a36e22 2021-08-26 stsp * Otherwise we can still use this reference as a hint to
535 f8a36e22 2021-08-26 stsp * avoid uploading any objects the server already has.
537 f8a36e22 2021-08-26 stsp my_ref = find_ref(&refs, refname);
538 f8a36e22 2021-08-26 stsp if (my_ref) {
539 c2105d00 2021-09-14 stsp struct got_object_id *my_id;
540 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&my_id, repo, my_ref);
543 92410079 2021-10-16 stsp if (got_object_id_cmp(my_id, their_id) != 0) {
544 92410079 2021-10-16 stsp if (!overwrite_refs && is_tag) {
545 92410079 2021-10-16 stsp err = got_error_fmt(
546 92410079 2021-10-16 stsp GOT_ERR_SEND_TAG_EXISTS,
547 92410079 2021-10-16 stsp "%s", refname);
548 92410079 2021-10-16 stsp free(my_id);
551 c2105d00 2021-09-14 stsp refs_to_send++;
553 c2105d00 2021-09-14 stsp free(my_id);
556 f8a36e22 2021-08-26 stsp /* Check if their object exists locally. */
557 f8a36e22 2021-08-26 stsp err = got_object_open(&obj, repo, their_id);
559 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NO_OBJ)
561 f8a36e22 2021-08-26 stsp if (!overwrite_refs && my_ref != NULL) {
562 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
563 f8a36e22 2021-08-26 stsp "%s", refname);
566 f8a36e22 2021-08-26 stsp have_their_id = 0;
568 f8a36e22 2021-08-26 stsp got_object_close(obj);
569 f8a36e22 2021-08-26 stsp have_their_id = 1;
572 535e07c7 2021-08-29 naddy err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
576 f8a36e22 2021-08-26 stsp if (have_their_id) {
577 f8a36e22 2021-08-26 stsp /* Enforce linear ancestry if required. */
578 f8a36e22 2021-08-26 stsp if (!overwrite_refs && my_ref && !is_tag) {
579 f8a36e22 2021-08-26 stsp struct got_object_id *my_id;
580 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&my_id, repo, my_ref);
583 f8a36e22 2021-08-26 stsp err = check_linear_ancestry(refname, my_id,
584 f8a36e22 2021-08-26 stsp their_id, repo, cancel_cb, cancel_arg);
585 f8a36e22 2021-08-26 stsp free(my_id);
586 f8a36e22 2021-08-26 stsp my_id = NULL;
590 f8a36e22 2021-08-26 stsp /* Exclude any objects reachable via their ID. */
591 f8a36e22 2021-08-26 stsp their_ids[ntheirs] = got_object_id_dup(their_id);
592 f8a36e22 2021-08-26 stsp if (their_ids[ntheirs] == NULL) {
593 f8a36e22 2021-08-26 stsp err = got_error_from_errno("got_object_id_dup");
597 f8a36e22 2021-08-26 stsp } else if (!is_tag) {
598 f8a36e22 2021-08-26 stsp char *remote_refname;
599 f8a36e22 2021-08-26 stsp struct got_reference *ref;
601 f8a36e22 2021-08-26 stsp * Exclude any objects which exist on the server
602 f8a36e22 2021-08-26 stsp * according to a locally cached remote reference.
604 f8a36e22 2021-08-26 stsp err = get_remote_refname(&remote_refname,
605 f8a36e22 2021-08-26 stsp remote_name, refname);
608 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, remote_refname, 0);
609 f8a36e22 2021-08-26 stsp free(remote_refname);
611 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NOT_REF)
614 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&their_ids[ntheirs],
615 f8a36e22 2021-08-26 stsp repo, ref);
616 f8a36e22 2021-08-26 stsp got_ref_close(ref);
624 f8a36e22 2021-08-26 stsp /* Account for any new references we are going to upload. */
625 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &refs, entry) {
626 f8a36e22 2021-08-26 stsp if (find_their_ref(&their_refs,
627 f8a36e22 2021-08-26 stsp got_ref_get_name(re->ref)) == NULL)
628 f8a36e22 2021-08-26 stsp refs_to_send++;
631 1bd76734 2021-08-26 stsp /* Account for any existing references we are going to delete. */
632 1bd76734 2021-08-26 stsp TAILQ_FOREACH(pe, delete_branches, entry) {
633 1bd76734 2021-08-26 stsp const char *branchname = pe->path;
634 1bd76734 2021-08-26 stsp if (find_their_ref(&their_refs, branchname))
635 27b75514 2021-08-28 stsp refs_to_delete++;
638 27b75514 2021-08-28 stsp if (refs_to_send == 0 && refs_to_delete == 0) {
639 f8a36e22 2021-08-26 stsp got_privsep_send_stop(imsg_sendfds[0]);
643 27b75514 2021-08-28 stsp if (refs_to_send > 0) {
644 cae60ab8 2022-10-18 stsp struct got_ratelimit rl;
645 cae60ab8 2022-10-18 stsp got_ratelimit_init(&rl, 0, 500);
646 27b75514 2021-08-28 stsp memset(&ppa, 0, sizeof(ppa));
647 27b75514 2021-08-28 stsp ppa.progress_cb = progress_cb;
648 27b75514 2021-08-28 stsp ppa.progress_arg = progress_arg;
649 a32780aa 2022-10-15 stsp err = got_pack_create(packsha1, packfd, delta_cache,
650 a32780aa 2022-10-15 stsp their_ids, ntheirs, our_ids, nours, repo, 0, 1,
651 cae60ab8 2022-10-18 stsp pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
655 894e4711 2022-10-15 stsp npackfd = dup(packfd);
656 27b75514 2021-08-28 stsp if (npackfd == -1) {
657 27b75514 2021-08-28 stsp err = got_error_from_errno("dup");
660 27b75514 2021-08-28 stsp err = got_privsep_send_packfd(&sendibuf, npackfd);
661 27b75514 2021-08-28 stsp if (err != NULL)
663 27b75514 2021-08-28 stsp npackfd = -1;
665 27b75514 2021-08-28 stsp err = got_privsep_send_packfd(&sendibuf, -1);
666 27b75514 2021-08-28 stsp if (err != NULL)
670 f8a36e22 2021-08-26 stsp while (!done) {
671 f8a36e22 2021-08-26 stsp int success = 0;
672 f8a36e22 2021-08-26 stsp char *refname = NULL;
673 6242c45b 2022-11-14 op char *errmsg = NULL;
675 f8a36e22 2021-08-26 stsp if (cancel_cb) {
676 f8a36e22 2021-08-26 stsp err = (*cancel_cb)(cancel_arg);
680 f8a36e22 2021-08-26 stsp err = got_privsep_recv_send_progress(&done, &bytes_sent,
681 6242c45b 2022-11-14 op &success, &refname, &errmsg, &sendibuf);
684 f8a36e22 2021-08-26 stsp if (refname && got_ref_name_is_valid(refname) && success &&
685 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/tags/", 10) != 0) {
686 f8a36e22 2021-08-26 stsp struct got_reference *my_ref;
688 f8a36e22 2021-08-26 stsp * The server has accepted our changes.
689 f8a36e22 2021-08-26 stsp * Update our reference in refs/remotes/ accordingly.
691 f8a36e22 2021-08-26 stsp my_ref = find_ref(&refs, refname);
692 f8a36e22 2021-08-26 stsp if (my_ref) {
693 f8a36e22 2021-08-26 stsp err = update_remote_ref(my_ref, remote_name,
699 f8a36e22 2021-08-26 stsp if (refname != NULL ||
700 f8a36e22 2021-08-26 stsp bytes_sent_cur != bytes_sent) {
701 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ppa.ncolored,
702 b8af7c06 2022-03-15 stsp ppa.nfound, ppa.ntrees, ppa.packfile_size,
703 f8a36e22 2021-08-26 stsp ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
704 f8a36e22 2021-08-26 stsp ppa.nobj_written, bytes_sent,
705 6242c45b 2022-11-14 op refname, errmsg, success);
707 f8a36e22 2021-08-26 stsp free(refname);
708 6242c45b 2022-11-14 op free(errmsg);
711 f8a36e22 2021-08-26 stsp bytes_sent_cur = bytes_sent;
713 f8a36e22 2021-08-26 stsp free(refname);
714 6242c45b 2022-11-14 op free(errmsg);
717 f8a36e22 2021-08-26 stsp if (sendpid != -1) {
719 f8a36e22 2021-08-26 stsp got_privsep_send_stop(imsg_sendfds[0]);
720 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
721 f8a36e22 2021-08-26 stsp err = got_error_from_errno("waitpid");
723 894e4711 2022-10-15 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
724 894e4711 2022-10-15 stsp err = got_error_from_errno("close");
725 a32780aa 2022-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
726 a32780aa 2022-10-15 stsp err = got_error_from_errno("fclose");
727 f8a36e22 2021-08-26 stsp if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
728 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
729 f8a36e22 2021-08-26 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
730 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
732 f8a36e22 2021-08-26 stsp got_ref_list_free(&refs);
733 d8bacb93 2023-01-10 mark got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_NONE);
734 d8bacb93 2023-01-10 mark got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_NONE);
735 f8a36e22 2021-08-26 stsp for (i = 0; i < nours; i++)
736 f8a36e22 2021-08-26 stsp free(our_ids[i]);
737 f8a36e22 2021-08-26 stsp free(our_ids);
738 f8a36e22 2021-08-26 stsp for (i = 0; i < ntheirs; i++)
739 f8a36e22 2021-08-26 stsp free(their_ids[i]);
740 f8a36e22 2021-08-26 stsp free(their_ids);
741 f8a36e22 2021-08-26 stsp return err;