Blame


1 f8a36e22 2021-08-26 stsp /*
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>
4 f8a36e22 2021-08-26 stsp *
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.
8 f8a36e22 2021-08-26 stsp *
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.
16 f8a36e22 2021-08-26 stsp */
17 f8a36e22 2021-08-26 stsp
18 f8a36e22 2021-08-26 stsp #include <sys/types.h>
19 f8a36e22 2021-08-26 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 f8a36e22 2021-08-26 stsp #include <sys/uio.h>
22 f8a36e22 2021-08-26 stsp #include <sys/socket.h>
23 f8a36e22 2021-08-26 stsp #include <sys/wait.h>
24 f8a36e22 2021-08-26 stsp #include <sys/resource.h>
25 f8a36e22 2021-08-26 stsp #include <sys/socket.h>
26 f8a36e22 2021-08-26 stsp
27 f8a36e22 2021-08-26 stsp #include <errno.h>
28 f8a36e22 2021-08-26 stsp #include <err.h>
29 f8a36e22 2021-08-26 stsp #include <fcntl.h>
30 f8a36e22 2021-08-26 stsp #include <stdio.h>
31 f8a36e22 2021-08-26 stsp #include <stdlib.h>
32 f8a36e22 2021-08-26 stsp #include <string.h>
33 f8a36e22 2021-08-26 stsp #include <stdint.h>
34 f8a36e22 2021-08-26 stsp #include <unistd.h>
35 f8a36e22 2021-08-26 stsp #include <zlib.h>
36 f8a36e22 2021-08-26 stsp #include <ctype.h>
37 f8a36e22 2021-08-26 stsp #include <limits.h>
38 f8a36e22 2021-08-26 stsp #include <time.h>
39 f8a36e22 2021-08-26 stsp
40 f8a36e22 2021-08-26 stsp #include "got_error.h"
41 f8a36e22 2021-08-26 stsp #include "got_reference.h"
42 f8a36e22 2021-08-26 stsp #include "got_repository.h"
43 f8a36e22 2021-08-26 stsp #include "got_path.h"
44 f8a36e22 2021-08-26 stsp #include "got_cancel.h"
45 f8a36e22 2021-08-26 stsp #include "got_worktree.h"
46 f8a36e22 2021-08-26 stsp #include "got_object.h"
47 f8a36e22 2021-08-26 stsp #include "got_opentemp.h"
48 f8a36e22 2021-08-26 stsp #include "got_send.h"
49 f8a36e22 2021-08-26 stsp #include "got_repository_admin.h"
50 f8a36e22 2021-08-26 stsp #include "got_commit_graph.h"
51 f8a36e22 2021-08-26 stsp
52 f8a36e22 2021-08-26 stsp #include "got_lib_delta.h"
53 f8a36e22 2021-08-26 stsp #include "got_lib_inflate.h"
54 f8a36e22 2021-08-26 stsp #include "got_lib_object.h"
55 f8a36e22 2021-08-26 stsp #include "got_lib_object_parse.h"
56 f8a36e22 2021-08-26 stsp #include "got_lib_object_create.h"
57 f8a36e22 2021-08-26 stsp #include "got_lib_pack.h"
58 f8a36e22 2021-08-26 stsp #include "got_lib_sha1.h"
59 f8a36e22 2021-08-26 stsp #include "got_lib_privsep.h"
60 f8a36e22 2021-08-26 stsp #include "got_lib_object_cache.h"
61 f8a36e22 2021-08-26 stsp #include "got_lib_repository.h"
62 f8a36e22 2021-08-26 stsp #include "got_lib_pack_create.h"
63 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
64 f8a36e22 2021-08-26 stsp
65 f8a36e22 2021-08-26 stsp #ifndef nitems
66 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 f8a36e22 2021-08-26 stsp #endif
68 f8a36e22 2021-08-26 stsp
69 f8a36e22 2021-08-26 stsp #ifndef ssizeof
70 f8a36e22 2021-08-26 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
71 f8a36e22 2021-08-26 stsp #endif
72 f8a36e22 2021-08-26 stsp
73 f8a36e22 2021-08-26 stsp #ifndef MIN
74 f8a36e22 2021-08-26 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
75 f8a36e22 2021-08-26 stsp #endif
76 f8a36e22 2021-08-26 stsp
77 f8a36e22 2021-08-26 stsp const struct got_error *
78 f8a36e22 2021-08-26 stsp got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
79 f8a36e22 2021-08-26 stsp const char *host, const char *port, const char *server_path, int verbosity)
80 f8a36e22 2021-08-26 stsp {
81 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
82 f8a36e22 2021-08-26 stsp
83 f8a36e22 2021-08-26 stsp *sendpid = -1;
84 f8a36e22 2021-08-26 stsp *sendfd = -1;
85 f8a36e22 2021-08-26 stsp
86 f8a36e22 2021-08-26 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
87 d65a88a2 2021-09-05 stsp err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
88 d65a88a2 2021-09-05 stsp GOT_DIAL_DIRECTION_SEND, verbosity);
89 f8a36e22 2021-08-26 stsp else if (strcmp(proto, "git") == 0)
90 d65a88a2 2021-09-05 stsp err = got_dial_git(sendfd, host, port, server_path,
91 d65a88a2 2021-09-05 stsp GOT_DIAL_DIRECTION_SEND);
92 f8a36e22 2021-08-26 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
93 f8a36e22 2021-08-26 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
94 f8a36e22 2021-08-26 stsp else
95 f8a36e22 2021-08-26 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
96 f8a36e22 2021-08-26 stsp return err;
97 f8a36e22 2021-08-26 stsp }
98 f8a36e22 2021-08-26 stsp
99 f8a36e22 2021-08-26 stsp struct pack_progress_arg {
100 f8a36e22 2021-08-26 stsp got_send_progress_cb progress_cb;
101 f8a36e22 2021-08-26 stsp void *progress_arg;
102 f8a36e22 2021-08-26 stsp
103 85220b0e 2022-03-22 thomas int ncolored;
104 85220b0e 2022-03-22 thomas int nfound;
105 85220b0e 2022-03-22 thomas int ntrees;
106 f8a36e22 2021-08-26 stsp off_t packfile_size;
107 f8a36e22 2021-08-26 stsp int ncommits;
108 f8a36e22 2021-08-26 stsp int nobj_total;
109 f8a36e22 2021-08-26 stsp int nobj_deltify;
110 f8a36e22 2021-08-26 stsp int nobj_written;
111 f8a36e22 2021-08-26 stsp };
112 f8a36e22 2021-08-26 stsp
113 f8a36e22 2021-08-26 stsp static const struct got_error *
114 85220b0e 2022-03-22 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
115 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
116 85220b0e 2022-03-22 thomas int nobj_written)
117 f8a36e22 2021-08-26 stsp {
118 f8a36e22 2021-08-26 stsp const struct got_error *err;
119 f8a36e22 2021-08-26 stsp struct pack_progress_arg *a = arg;
120 f8a36e22 2021-08-26 stsp
121 85220b0e 2022-03-22 thomas err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
122 85220b0e 2022-03-22 thomas packfile_size, ncommits, nobj_total, nobj_deltify,
123 85220b0e 2022-03-22 thomas nobj_written, 0, NULL, 0);
124 f8a36e22 2021-08-26 stsp if (err)
125 f8a36e22 2021-08-26 stsp return err;
126 f8a36e22 2021-08-26 stsp
127 85220b0e 2022-03-22 thomas a->ncolored= ncolored;
128 85220b0e 2022-03-22 thomas a->nfound = nfound;
129 85220b0e 2022-03-22 thomas a->ntrees = ntrees;
130 f8a36e22 2021-08-26 stsp a->packfile_size = packfile_size;
131 f8a36e22 2021-08-26 stsp a->ncommits = ncommits;
132 f8a36e22 2021-08-26 stsp a->nobj_total = nobj_total;
133 f8a36e22 2021-08-26 stsp a->nobj_deltify = nobj_deltify;
134 f8a36e22 2021-08-26 stsp a->nobj_written = nobj_written;
135 f8a36e22 2021-08-26 stsp return NULL;
136 f8a36e22 2021-08-26 stsp }
137 f8a36e22 2021-08-26 stsp
138 f8a36e22 2021-08-26 stsp static const struct got_error *
139 f8a36e22 2021-08-26 stsp insert_ref(struct got_reflist_head *refs, const char *refname,
140 f8a36e22 2021-08-26 stsp struct got_repository *repo)
141 f8a36e22 2021-08-26 stsp {
142 f8a36e22 2021-08-26 stsp const struct got_error *err;
143 f8a36e22 2021-08-26 stsp struct got_reference *ref;
144 f8a36e22 2021-08-26 stsp struct got_reflist_entry *new;
145 f8a36e22 2021-08-26 stsp
146 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, refname, 0);
147 f8a36e22 2021-08-26 stsp if (err)
148 f8a36e22 2021-08-26 stsp return err;
149 f8a36e22 2021-08-26 stsp
150 f8a36e22 2021-08-26 stsp err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
151 f8a36e22 2021-08-26 stsp if (err || new == NULL /* duplicate */)
152 f8a36e22 2021-08-26 stsp got_ref_close(ref);
153 f8a36e22 2021-08-26 stsp
154 f8a36e22 2021-08-26 stsp return err;
155 f8a36e22 2021-08-26 stsp }
156 f8a36e22 2021-08-26 stsp
157 f8a36e22 2021-08-26 stsp static const struct got_error *
158 f8a36e22 2021-08-26 stsp check_linear_ancestry(const char *refname, struct got_object_id *my_id,
159 f8a36e22 2021-08-26 stsp struct got_object_id *their_id, struct got_repository *repo,
160 f8a36e22 2021-08-26 stsp got_cancel_cb cancel_cb, void *cancel_arg)
161 f8a36e22 2021-08-26 stsp {
162 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
163 f8a36e22 2021-08-26 stsp struct got_object_id *yca_id;
164 f8a36e22 2021-08-26 stsp int obj_type;
165 f8a36e22 2021-08-26 stsp
166 f8a36e22 2021-08-26 stsp err = got_object_get_type(&obj_type, repo, their_id);
167 f8a36e22 2021-08-26 stsp if (err)
168 f8a36e22 2021-08-26 stsp return err;
169 f8a36e22 2021-08-26 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 f8a36e22 2021-08-26 stsp return got_error_fmt(GOT_ERR_OBJ_TYPE,
171 f8a36e22 2021-08-26 stsp "bad object type on server for %s", refname);
172 f8a36e22 2021-08-26 stsp
173 f8a36e22 2021-08-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
174 768705e3 2021-09-27 thomas my_id, their_id, 1, repo, cancel_cb, cancel_arg);
175 f8a36e22 2021-08-26 stsp if (err)
176 f8a36e22 2021-08-26 stsp return err;
177 f8a36e22 2021-08-26 stsp if (yca_id == NULL)
178 f8a36e22 2021-08-26 stsp return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
179 f8a36e22 2021-08-26 stsp
180 f8a36e22 2021-08-26 stsp /*
181 f8a36e22 2021-08-26 stsp * Require a straight line of history between the two commits,
182 f8a36e22 2021-08-26 stsp * with their commit being older than my commit.
183 f8a36e22 2021-08-26 stsp *
184 f8a36e22 2021-08-26 stsp * Non-linear situations such as this require a rebase:
185 f8a36e22 2021-08-26 stsp *
186 f8a36e22 2021-08-26 stsp * (theirs) D F (mine)
187 f8a36e22 2021-08-26 stsp * \ /
188 f8a36e22 2021-08-26 stsp * C E
189 f8a36e22 2021-08-26 stsp * \ /
190 f8a36e22 2021-08-26 stsp * B (yca)
191 f8a36e22 2021-08-26 stsp * |
192 f8a36e22 2021-08-26 stsp * A
193 f8a36e22 2021-08-26 stsp */
194 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(their_id, yca_id) != 0)
195 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
196 f8a36e22 2021-08-26 stsp
197 f8a36e22 2021-08-26 stsp free(yca_id);
198 f8a36e22 2021-08-26 stsp return err;
199 f8a36e22 2021-08-26 stsp }
200 f8a36e22 2021-08-26 stsp
201 f8a36e22 2021-08-26 stsp static const struct got_error *
202 f8a36e22 2021-08-26 stsp realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
203 f8a36e22 2021-08-26 stsp {
204 f8a36e22 2021-08-26 stsp struct got_object_id **new;
205 f8a36e22 2021-08-26 stsp const size_t alloc_chunksz = 256;
206 f8a36e22 2021-08-26 stsp
207 535e07c7 2021-08-29 naddy if (*nalloc >= n)
208 f8a36e22 2021-08-26 stsp return NULL;
209 f8a36e22 2021-08-26 stsp
210 f8a36e22 2021-08-26 stsp new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
211 f8a36e22 2021-08-26 stsp sizeof(struct got_object_id));
212 f8a36e22 2021-08-26 stsp if (new == NULL)
213 f8a36e22 2021-08-26 stsp return got_error_from_errno("recallocarray");
214 f8a36e22 2021-08-26 stsp
215 f8a36e22 2021-08-26 stsp *ids = new;
216 f8a36e22 2021-08-26 stsp *nalloc += alloc_chunksz;
217 f8a36e22 2021-08-26 stsp return NULL;
218 f8a36e22 2021-08-26 stsp }
219 f8a36e22 2021-08-26 stsp
220 f8a36e22 2021-08-26 stsp static struct got_reference *
221 f8a36e22 2021-08-26 stsp find_ref(struct got_reflist_head *refs, const char *refname)
222 f8a36e22 2021-08-26 stsp {
223 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
224 f8a36e22 2021-08-26 stsp
225 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, refs, entry) {
226 f8a36e22 2021-08-26 stsp if (got_path_cmp(got_ref_get_name(re->ref), refname,
227 f8a36e22 2021-08-26 stsp strlen(got_ref_get_name(re->ref)),
228 f8a36e22 2021-08-26 stsp strlen(refname)) == 0) {
229 f8a36e22 2021-08-26 stsp return re->ref;
230 f8a36e22 2021-08-26 stsp }
231 f8a36e22 2021-08-26 stsp }
232 f8a36e22 2021-08-26 stsp
233 f8a36e22 2021-08-26 stsp return NULL;
234 f8a36e22 2021-08-26 stsp }
235 f8a36e22 2021-08-26 stsp
236 f8a36e22 2021-08-26 stsp static struct got_pathlist_entry *
237 f8a36e22 2021-08-26 stsp find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
238 f8a36e22 2021-08-26 stsp {
239 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
240 f8a36e22 2021-08-26 stsp
241 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, their_refs, entry) {
242 f8a36e22 2021-08-26 stsp const char *their_refname = pe->path;
243 f8a36e22 2021-08-26 stsp if (got_path_cmp(their_refname, refname,
244 f8a36e22 2021-08-26 stsp strlen(their_refname), strlen(refname)) == 0) {
245 f8a36e22 2021-08-26 stsp return pe;
246 f8a36e22 2021-08-26 stsp }
247 f8a36e22 2021-08-26 stsp }
248 f8a36e22 2021-08-26 stsp
249 f8a36e22 2021-08-26 stsp return NULL;
250 f8a36e22 2021-08-26 stsp }
251 f8a36e22 2021-08-26 stsp
252 f8a36e22 2021-08-26 stsp static const struct got_error *
253 f8a36e22 2021-08-26 stsp get_remote_refname(char **remote_refname, const char *remote_name,
254 f8a36e22 2021-08-26 stsp const char *refname)
255 f8a36e22 2021-08-26 stsp {
256 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/", 5) == 0)
257 f8a36e22 2021-08-26 stsp refname += 5;
258 f8a36e22 2021-08-26 stsp if (strncmp(refname, "heads/", 6) == 0)
259 f8a36e22 2021-08-26 stsp refname += 6;
260 f8a36e22 2021-08-26 stsp
261 f8a36e22 2021-08-26 stsp if (asprintf(remote_refname, "refs/remotes/%s/%s",
262 f8a36e22 2021-08-26 stsp remote_name, refname) == -1)
263 f8a36e22 2021-08-26 stsp return got_error_from_errno("asprintf");
264 f8a36e22 2021-08-26 stsp
265 f8a36e22 2021-08-26 stsp return NULL;
266 f8a36e22 2021-08-26 stsp }
267 f8a36e22 2021-08-26 stsp
268 f8a36e22 2021-08-26 stsp static const struct got_error *
269 f8a36e22 2021-08-26 stsp update_remote_ref(struct got_reference *my_ref, const char *remote_name,
270 f8a36e22 2021-08-26 stsp struct got_repository *repo)
271 f8a36e22 2021-08-26 stsp {
272 f8a36e22 2021-08-26 stsp const struct got_error *err, *unlock_err;
273 f8a36e22 2021-08-26 stsp struct got_object_id *my_id;
274 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
275 f8a36e22 2021-08-26 stsp char *remote_refname = NULL;
276 f8a36e22 2021-08-26 stsp int ref_locked = 0;
277 f8a36e22 2021-08-26 stsp
278 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&my_id, repo, my_ref);
279 f8a36e22 2021-08-26 stsp if (err)
280 f8a36e22 2021-08-26 stsp return err;
281 f8a36e22 2021-08-26 stsp
282 f8a36e22 2021-08-26 stsp err = get_remote_refname(&remote_refname, remote_name,
283 f8a36e22 2021-08-26 stsp got_ref_get_name(my_ref));
284 f8a36e22 2021-08-26 stsp if (err)
285 f8a36e22 2021-08-26 stsp goto done;
286 f8a36e22 2021-08-26 stsp
287 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
288 f8a36e22 2021-08-26 stsp if (err) {
289 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NOT_REF)
290 f8a36e22 2021-08-26 stsp goto done;
291 f8a36e22 2021-08-26 stsp err = got_ref_alloc(&ref, remote_refname, my_id);
292 f8a36e22 2021-08-26 stsp if (err)
293 f8a36e22 2021-08-26 stsp goto done;
294 f8a36e22 2021-08-26 stsp } else {
295 f8a36e22 2021-08-26 stsp ref_locked = 1;
296 f8a36e22 2021-08-26 stsp err = got_ref_change_ref(ref, my_id);
297 f8a36e22 2021-08-26 stsp if (err)
298 f8a36e22 2021-08-26 stsp goto done;
299 f8a36e22 2021-08-26 stsp }
300 f8a36e22 2021-08-26 stsp
301 f8a36e22 2021-08-26 stsp err = got_ref_write(ref, repo);
302 f8a36e22 2021-08-26 stsp done:
303 f8a36e22 2021-08-26 stsp if (ref) {
304 f8a36e22 2021-08-26 stsp if (ref_locked) {
305 f8a36e22 2021-08-26 stsp unlock_err = got_ref_unlock(ref);
306 f8a36e22 2021-08-26 stsp if (unlock_err && err == NULL)
307 f8a36e22 2021-08-26 stsp err = unlock_err;
308 f8a36e22 2021-08-26 stsp }
309 f8a36e22 2021-08-26 stsp got_ref_close(ref);
310 f8a36e22 2021-08-26 stsp }
311 f8a36e22 2021-08-26 stsp free(my_id);
312 f8a36e22 2021-08-26 stsp free(remote_refname);
313 f8a36e22 2021-08-26 stsp return err;
314 f8a36e22 2021-08-26 stsp }
315 f8a36e22 2021-08-26 stsp
316 f8a36e22 2021-08-26 stsp const struct got_error*
317 f8a36e22 2021-08-26 stsp got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
318 f8a36e22 2021-08-26 stsp struct got_pathlist_head *tag_names,
319 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches,
320 f8a36e22 2021-08-26 stsp int verbosity, int overwrite_refs, int sendfd,
321 f8a36e22 2021-08-26 stsp struct got_repository *repo, got_send_progress_cb progress_cb,
322 f8a36e22 2021-08-26 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
323 f8a36e22 2021-08-26 stsp {
324 f8a36e22 2021-08-26 stsp int imsg_sendfds[2];
325 f8a36e22 2021-08-26 stsp int npackfd = -1, nsendfd = -1;
326 f8a36e22 2021-08-26 stsp int sendstatus, done = 0;
327 f8a36e22 2021-08-26 stsp const struct got_error *err;
328 f8a36e22 2021-08-26 stsp struct imsgbuf sendibuf;
329 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
330 f8a36e22 2021-08-26 stsp struct got_reflist_head refs;
331 f8a36e22 2021-08-26 stsp struct got_pathlist_head have_refs;
332 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
333 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
334 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
335 f8a36e22 2021-08-26 stsp struct got_object_id **our_ids = NULL;
336 f8a36e22 2021-08-26 stsp struct got_object_id **their_ids = NULL;
337 f8a36e22 2021-08-26 stsp int i, nours = 0, ntheirs = 0;
338 f8a36e22 2021-08-26 stsp size_t nalloc_ours = 0, nalloc_theirs = 0;
339 27b75514 2021-08-28 stsp int refs_to_send = 0, refs_to_delete = 0;
340 46cd7462 2022-07-21 thomas off_t bytes_sent = 0, bytes_sent_cur = 0;
341 f8a36e22 2021-08-26 stsp struct pack_progress_arg ppa;
342 f8a36e22 2021-08-26 stsp uint8_t packsha1[SHA1_DIGEST_LENGTH];
343 f8a36e22 2021-08-26 stsp FILE *packfile = NULL;
344 f8a36e22 2021-08-26 stsp
345 f8a36e22 2021-08-26 stsp TAILQ_INIT(&refs);
346 f8a36e22 2021-08-26 stsp TAILQ_INIT(&have_refs);
347 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
348 f8a36e22 2021-08-26 stsp
349 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, branch_names, entry) {
350 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
351 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
352 f8a36e22 2021-08-26 stsp char *s;
353 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
354 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
355 f8a36e22 2021-08-26 stsp goto done;
356 f8a36e22 2021-08-26 stsp }
357 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, s, repo);
358 f8a36e22 2021-08-26 stsp free(s);
359 f8a36e22 2021-08-26 stsp } else {
360 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, branchname, repo);
361 f8a36e22 2021-08-26 stsp }
362 f8a36e22 2021-08-26 stsp if (err)
363 f8a36e22 2021-08-26 stsp goto done;
364 f8a36e22 2021-08-26 stsp }
365 f8a36e22 2021-08-26 stsp
366 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_branches, entry) {
367 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
368 f8a36e22 2021-08-26 stsp struct got_reference *ref;
369 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
370 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
371 f8a36e22 2021-08-26 stsp branchname);
372 f8a36e22 2021-08-26 stsp goto done;
373 f8a36e22 2021-08-26 stsp }
374 f8a36e22 2021-08-26 stsp ref = find_ref(&refs, branchname);
375 f8a36e22 2021-08-26 stsp if (ref) {
376 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
377 f8a36e22 2021-08-26 stsp "changes on %s will be sent to server",
378 f8a36e22 2021-08-26 stsp branchname);
379 f8a36e22 2021-08-26 stsp goto done;
380 f8a36e22 2021-08-26 stsp }
381 f8a36e22 2021-08-26 stsp }
382 f8a36e22 2021-08-26 stsp
383 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, tag_names, entry) {
384 f8a36e22 2021-08-26 stsp const char *tagname = pe->path;
385 f8a36e22 2021-08-26 stsp if (strncmp(tagname, "refs/tags/", 10) != 0) {
386 f8a36e22 2021-08-26 stsp char *s;
387 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
388 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
389 f8a36e22 2021-08-26 stsp goto done;
390 f8a36e22 2021-08-26 stsp }
391 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, s, repo);
392 f8a36e22 2021-08-26 stsp free(s);
393 f8a36e22 2021-08-26 stsp } else {
394 f8a36e22 2021-08-26 stsp err = insert_ref(&refs, tagname, repo);
395 f8a36e22 2021-08-26 stsp }
396 f8a36e22 2021-08-26 stsp if (err)
397 f8a36e22 2021-08-26 stsp goto done;
398 f8a36e22 2021-08-26 stsp }
399 f8a36e22 2021-08-26 stsp
400 f8a36e22 2021-08-26 stsp if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
401 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_SEND_EMPTY);
402 f8a36e22 2021-08-26 stsp goto done;
403 f8a36e22 2021-08-26 stsp }
404 f8a36e22 2021-08-26 stsp
405 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &refs, entry) {
406 f8a36e22 2021-08-26 stsp struct got_object_id *id;
407 f8a36e22 2021-08-26 stsp int obj_type;
408 f8a36e22 2021-08-26 stsp
409 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(re->ref)) {
410 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
411 f8a36e22 2021-08-26 stsp "cannot send symbolic reference %s",
412 f8a36e22 2021-08-26 stsp got_ref_get_name(re->ref));
413 f8a36e22 2021-08-26 stsp goto done;
414 f8a36e22 2021-08-26 stsp }
415 f8a36e22 2021-08-26 stsp
416 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&id, repo, re->ref);
417 f8a36e22 2021-08-26 stsp if (err)
418 f8a36e22 2021-08-26 stsp goto done;
419 f8a36e22 2021-08-26 stsp err = got_object_get_type(&obj_type, repo, id);
420 f8a36e22 2021-08-26 stsp free(id);
421 f8a36e22 2021-08-26 stsp if (err)
422 f8a36e22 2021-08-26 stsp goto done;
423 f8a36e22 2021-08-26 stsp switch (obj_type) {
424 f8a36e22 2021-08-26 stsp case GOT_OBJ_TYPE_COMMIT:
425 f8a36e22 2021-08-26 stsp case GOT_OBJ_TYPE_TAG:
426 f8a36e22 2021-08-26 stsp break;
427 f8a36e22 2021-08-26 stsp default:
428 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
429 f8a36e22 2021-08-26 stsp "cannot send %s", got_ref_get_name(re->ref));
430 f8a36e22 2021-08-26 stsp goto done;
431 f8a36e22 2021-08-26 stsp }
432 f8a36e22 2021-08-26 stsp }
433 f8a36e22 2021-08-26 stsp
434 f8a36e22 2021-08-26 stsp packfile = got_opentemp();
435 f8a36e22 2021-08-26 stsp if (packfile == NULL) {
436 f8a36e22 2021-08-26 stsp err = got_error_from_errno("got_opentemp");
437 f8a36e22 2021-08-26 stsp goto done;
438 f8a36e22 2021-08-26 stsp }
439 f8a36e22 2021-08-26 stsp
440 f8a36e22 2021-08-26 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
441 f8a36e22 2021-08-26 stsp err = got_error_from_errno("socketpair");
442 f8a36e22 2021-08-26 stsp goto done;
443 f8a36e22 2021-08-26 stsp }
444 f8a36e22 2021-08-26 stsp
445 f8a36e22 2021-08-26 stsp sendpid = fork();
446 f8a36e22 2021-08-26 stsp if (sendpid == -1) {
447 f8a36e22 2021-08-26 stsp err = got_error_from_errno("fork");
448 f8a36e22 2021-08-26 stsp goto done;
449 f8a36e22 2021-08-26 stsp } else if (sendpid == 0){
450 f8a36e22 2021-08-26 stsp got_privsep_exec_child(imsg_sendfds,
451 f8a36e22 2021-08-26 stsp GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
452 f8a36e22 2021-08-26 stsp }
453 f8a36e22 2021-08-26 stsp
454 f8a36e22 2021-08-26 stsp if (close(imsg_sendfds[1]) == -1) {
455 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
456 f8a36e22 2021-08-26 stsp goto done;
457 f8a36e22 2021-08-26 stsp }
458 f8a36e22 2021-08-26 stsp imsg_init(&sendibuf, imsg_sendfds[0]);
459 f8a36e22 2021-08-26 stsp nsendfd = dup(sendfd);
460 f8a36e22 2021-08-26 stsp if (nsendfd == -1) {
461 f8a36e22 2021-08-26 stsp err = got_error_from_errno("dup");
462 f8a36e22 2021-08-26 stsp goto done;
463 f8a36e22 2021-08-26 stsp }
464 f8a36e22 2021-08-26 stsp
465 f8a36e22 2021-08-26 stsp /*
466 f8a36e22 2021-08-26 stsp * Convert reflist to pathlist since the privsep layer
467 f8a36e22 2021-08-26 stsp * is linked into helper programs which lack reference.c.
468 f8a36e22 2021-08-26 stsp */
469 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &refs, entry) {
470 f8a36e22 2021-08-26 stsp struct got_object_id *id;
471 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&id, repo, re->ref);
472 f8a36e22 2021-08-26 stsp if (err)
473 f8a36e22 2021-08-26 stsp goto done;
474 f8a36e22 2021-08-26 stsp err = got_pathlist_append(&have_refs,
475 f8a36e22 2021-08-26 stsp got_ref_get_name(re->ref), id);
476 f8a36e22 2021-08-26 stsp if (err)
477 f8a36e22 2021-08-26 stsp goto done;
478 f8a36e22 2021-08-26 stsp /*
479 f8a36e22 2021-08-26 stsp * Also prepare the array of our object IDs which
480 f8a36e22 2021-08-26 stsp * will be needed for generating a pack file.
481 f8a36e22 2021-08-26 stsp */
482 535e07c7 2021-08-29 naddy err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
483 f8a36e22 2021-08-26 stsp if (err)
484 f8a36e22 2021-08-26 stsp goto done;
485 f8a36e22 2021-08-26 stsp our_ids[nours] = id;
486 f8a36e22 2021-08-26 stsp nours++;
487 f8a36e22 2021-08-26 stsp }
488 f8a36e22 2021-08-26 stsp
489 f8a36e22 2021-08-26 stsp err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
490 f8a36e22 2021-08-26 stsp delete_branches, verbosity);
491 f8a36e22 2021-08-26 stsp if (err)
492 f8a36e22 2021-08-26 stsp goto done;
493 f8a36e22 2021-08-26 stsp nsendfd = -1;
494 f8a36e22 2021-08-26 stsp
495 f8a36e22 2021-08-26 stsp err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
496 f8a36e22 2021-08-26 stsp if (err)
497 f8a36e22 2021-08-26 stsp goto done;
498 f8a36e22 2021-08-26 stsp
499 f8a36e22 2021-08-26 stsp /*
500 f8a36e22 2021-08-26 stsp * Process references reported by the server.
501 f8a36e22 2021-08-26 stsp * Push appropriate object IDs onto the "their IDs" array.
502 f8a36e22 2021-08-26 stsp * This array will be used to exclude objects which already
503 f8a36e22 2021-08-26 stsp * exist on the server from our pack file.
504 f8a36e22 2021-08-26 stsp */
505 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
506 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
507 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = pe->data;
508 f8a36e22 2021-08-26 stsp int have_their_id;
509 f8a36e22 2021-08-26 stsp struct got_object *obj;
510 f8a36e22 2021-08-26 stsp struct got_reference *my_ref = NULL;
511 f8a36e22 2021-08-26 stsp int is_tag = 0;
512 f8a36e22 2021-08-26 stsp
513 f8a36e22 2021-08-26 stsp /* Don't blindly trust the server to send us valid names. */
514 f8a36e22 2021-08-26 stsp if (!got_ref_name_is_valid(refname))
515 f8a36e22 2021-08-26 stsp continue;
516 f8a36e22 2021-08-26 stsp
517 4064588e 2021-10-17 thomas if (strncmp(refname, "refs/tags/", 10) == 0)
518 4064588e 2021-10-17 thomas is_tag = 1;
519 f8a36e22 2021-08-26 stsp /*
520 f8a36e22 2021-08-26 stsp * Find out whether this is a reference we want to upload.
521 f8a36e22 2021-08-26 stsp * Otherwise we can still use this reference as a hint to
522 f8a36e22 2021-08-26 stsp * avoid uploading any objects the server already has.
523 f8a36e22 2021-08-26 stsp */
524 f8a36e22 2021-08-26 stsp my_ref = find_ref(&refs, refname);
525 f8a36e22 2021-08-26 stsp if (my_ref) {
526 c2105d00 2021-09-14 stsp struct got_object_id *my_id;
527 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&my_id, repo, my_ref);
528 f8a36e22 2021-08-26 stsp if (err)
529 f8a36e22 2021-08-26 stsp goto done;
530 4064588e 2021-10-17 thomas if (got_object_id_cmp(my_id, their_id) != 0) {
531 4064588e 2021-10-17 thomas if (!overwrite_refs && is_tag) {
532 4064588e 2021-10-17 thomas err = got_error_fmt(
533 4064588e 2021-10-17 thomas GOT_ERR_SEND_TAG_EXISTS,
534 4064588e 2021-10-17 thomas "%s", refname);
535 4064588e 2021-10-17 thomas free(my_id);
536 4064588e 2021-10-17 thomas goto done;
537 4064588e 2021-10-17 thomas }
538 c2105d00 2021-09-14 stsp refs_to_send++;
539 4064588e 2021-10-17 thomas }
540 c2105d00 2021-09-14 stsp free(my_id);
541 f8a36e22 2021-08-26 stsp }
542 f8a36e22 2021-08-26 stsp
543 f8a36e22 2021-08-26 stsp /* Check if their object exists locally. */
544 f8a36e22 2021-08-26 stsp err = got_object_open(&obj, repo, their_id);
545 f8a36e22 2021-08-26 stsp if (err) {
546 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NO_OBJ)
547 f8a36e22 2021-08-26 stsp goto done;
548 f8a36e22 2021-08-26 stsp if (!overwrite_refs && my_ref != NULL) {
549 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
550 f8a36e22 2021-08-26 stsp "%s", refname);
551 f8a36e22 2021-08-26 stsp goto done;
552 f8a36e22 2021-08-26 stsp }
553 f8a36e22 2021-08-26 stsp have_their_id = 0;
554 f8a36e22 2021-08-26 stsp } else {
555 f8a36e22 2021-08-26 stsp got_object_close(obj);
556 f8a36e22 2021-08-26 stsp have_their_id = 1;
557 f8a36e22 2021-08-26 stsp }
558 f8a36e22 2021-08-26 stsp
559 535e07c7 2021-08-29 naddy err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
560 f8a36e22 2021-08-26 stsp if (err)
561 f8a36e22 2021-08-26 stsp goto done;
562 f8a36e22 2021-08-26 stsp
563 f8a36e22 2021-08-26 stsp if (have_their_id) {
564 f8a36e22 2021-08-26 stsp /* Enforce linear ancestry if required. */
565 f8a36e22 2021-08-26 stsp if (!overwrite_refs && my_ref && !is_tag) {
566 f8a36e22 2021-08-26 stsp struct got_object_id *my_id;
567 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&my_id, repo, my_ref);
568 f8a36e22 2021-08-26 stsp if (err)
569 f8a36e22 2021-08-26 stsp goto done;
570 f8a36e22 2021-08-26 stsp err = check_linear_ancestry(refname, my_id,
571 f8a36e22 2021-08-26 stsp their_id, repo, cancel_cb, cancel_arg);
572 f8a36e22 2021-08-26 stsp free(my_id);
573 f8a36e22 2021-08-26 stsp my_id = NULL;
574 f8a36e22 2021-08-26 stsp if (err)
575 f8a36e22 2021-08-26 stsp goto done;
576 f8a36e22 2021-08-26 stsp }
577 f8a36e22 2021-08-26 stsp /* Exclude any objects reachable via their ID. */
578 f8a36e22 2021-08-26 stsp their_ids[ntheirs] = got_object_id_dup(their_id);
579 f8a36e22 2021-08-26 stsp if (their_ids[ntheirs] == NULL) {
580 f8a36e22 2021-08-26 stsp err = got_error_from_errno("got_object_id_dup");
581 f8a36e22 2021-08-26 stsp goto done;
582 f8a36e22 2021-08-26 stsp }
583 f8a36e22 2021-08-26 stsp ntheirs++;
584 f8a36e22 2021-08-26 stsp } else if (!is_tag) {
585 f8a36e22 2021-08-26 stsp char *remote_refname;
586 f8a36e22 2021-08-26 stsp struct got_reference *ref;
587 f8a36e22 2021-08-26 stsp /*
588 f8a36e22 2021-08-26 stsp * Exclude any objects which exist on the server
589 f8a36e22 2021-08-26 stsp * according to a locally cached remote reference.
590 f8a36e22 2021-08-26 stsp */
591 f8a36e22 2021-08-26 stsp err = get_remote_refname(&remote_refname,
592 f8a36e22 2021-08-26 stsp remote_name, refname);
593 f8a36e22 2021-08-26 stsp if (err)
594 f8a36e22 2021-08-26 stsp goto done;
595 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, remote_refname, 0);
596 f8a36e22 2021-08-26 stsp free(remote_refname);
597 f8a36e22 2021-08-26 stsp if (err) {
598 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NOT_REF)
599 f8a36e22 2021-08-26 stsp goto done;
600 f8a36e22 2021-08-26 stsp } else {
601 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&their_ids[ntheirs],
602 f8a36e22 2021-08-26 stsp repo, ref);
603 f8a36e22 2021-08-26 stsp got_ref_close(ref);
604 f8a36e22 2021-08-26 stsp if (err)
605 f8a36e22 2021-08-26 stsp goto done;
606 f8a36e22 2021-08-26 stsp ntheirs++;
607 f8a36e22 2021-08-26 stsp }
608 f8a36e22 2021-08-26 stsp }
609 f8a36e22 2021-08-26 stsp }
610 f8a36e22 2021-08-26 stsp
611 f8a36e22 2021-08-26 stsp /* Account for any new references we are going to upload. */
612 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &refs, entry) {
613 f8a36e22 2021-08-26 stsp if (find_their_ref(&their_refs,
614 f8a36e22 2021-08-26 stsp got_ref_get_name(re->ref)) == NULL)
615 f8a36e22 2021-08-26 stsp refs_to_send++;
616 f8a36e22 2021-08-26 stsp }
617 f8a36e22 2021-08-26 stsp
618 1bd76734 2021-08-26 stsp /* Account for any existing references we are going to delete. */
619 1bd76734 2021-08-26 stsp TAILQ_FOREACH(pe, delete_branches, entry) {
620 1bd76734 2021-08-26 stsp const char *branchname = pe->path;
621 1bd76734 2021-08-26 stsp if (find_their_ref(&their_refs, branchname))
622 27b75514 2021-08-28 stsp refs_to_delete++;
623 1bd76734 2021-08-26 stsp }
624 1bd76734 2021-08-26 stsp
625 27b75514 2021-08-28 stsp if (refs_to_send == 0 && refs_to_delete == 0) {
626 f8a36e22 2021-08-26 stsp got_privsep_send_stop(imsg_sendfds[0]);
627 f8a36e22 2021-08-26 stsp goto done;
628 f8a36e22 2021-08-26 stsp }
629 f8a36e22 2021-08-26 stsp
630 27b75514 2021-08-28 stsp if (refs_to_send > 0) {
631 27b75514 2021-08-28 stsp memset(&ppa, 0, sizeof(ppa));
632 27b75514 2021-08-28 stsp ppa.progress_cb = progress_cb;
633 27b75514 2021-08-28 stsp ppa.progress_arg = progress_arg;
634 27b75514 2021-08-28 stsp err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
635 27b75514 2021-08-28 stsp our_ids, nours, repo, 0, 1, pack_progress, &ppa,
636 27b75514 2021-08-28 stsp cancel_cb, cancel_arg);
637 27b75514 2021-08-28 stsp if (err)
638 27b75514 2021-08-28 stsp goto done;
639 f8a36e22 2021-08-26 stsp
640 27b75514 2021-08-28 stsp if (fflush(packfile) == -1) {
641 27b75514 2021-08-28 stsp err = got_error_from_errno("fflush");
642 27b75514 2021-08-28 stsp goto done;
643 27b75514 2021-08-28 stsp }
644 27b75514 2021-08-28 stsp
645 27b75514 2021-08-28 stsp npackfd = dup(fileno(packfile));
646 27b75514 2021-08-28 stsp if (npackfd == -1) {
647 27b75514 2021-08-28 stsp err = got_error_from_errno("dup");
648 27b75514 2021-08-28 stsp goto done;
649 27b75514 2021-08-28 stsp }
650 27b75514 2021-08-28 stsp err = got_privsep_send_packfd(&sendibuf, npackfd);
651 27b75514 2021-08-28 stsp if (err != NULL)
652 27b75514 2021-08-28 stsp goto done;
653 27b75514 2021-08-28 stsp npackfd = -1;
654 27b75514 2021-08-28 stsp } else {
655 27b75514 2021-08-28 stsp err = got_privsep_send_packfd(&sendibuf, -1);
656 27b75514 2021-08-28 stsp if (err != NULL)
657 27b75514 2021-08-28 stsp goto done;
658 f8a36e22 2021-08-26 stsp }
659 f8a36e22 2021-08-26 stsp
660 f8a36e22 2021-08-26 stsp while (!done) {
661 f8a36e22 2021-08-26 stsp int success = 0;
662 f8a36e22 2021-08-26 stsp char *refname = NULL;
663 f8a36e22 2021-08-26 stsp if (cancel_cb) {
664 f8a36e22 2021-08-26 stsp err = (*cancel_cb)(cancel_arg);
665 f8a36e22 2021-08-26 stsp if (err)
666 f8a36e22 2021-08-26 stsp goto done;
667 f8a36e22 2021-08-26 stsp }
668 f8a36e22 2021-08-26 stsp err = got_privsep_recv_send_progress(&done, &bytes_sent,
669 f8a36e22 2021-08-26 stsp &success, &refname, &sendibuf);
670 f8a36e22 2021-08-26 stsp if (err)
671 f8a36e22 2021-08-26 stsp goto done;
672 f8a36e22 2021-08-26 stsp if (refname && got_ref_name_is_valid(refname) && success &&
673 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/tags/", 10) != 0) {
674 f8a36e22 2021-08-26 stsp struct got_reference *my_ref;
675 f8a36e22 2021-08-26 stsp /*
676 f8a36e22 2021-08-26 stsp * The server has accepted our changes.
677 f8a36e22 2021-08-26 stsp * Update our reference in refs/remotes/ accordingly.
678 f8a36e22 2021-08-26 stsp */
679 f8a36e22 2021-08-26 stsp my_ref = find_ref(&refs, refname);
680 f8a36e22 2021-08-26 stsp if (my_ref) {
681 f8a36e22 2021-08-26 stsp err = update_remote_ref(my_ref, remote_name,
682 f8a36e22 2021-08-26 stsp repo);
683 f8a36e22 2021-08-26 stsp if (err)
684 f8a36e22 2021-08-26 stsp goto done;
685 f8a36e22 2021-08-26 stsp }
686 f8a36e22 2021-08-26 stsp }
687 f8a36e22 2021-08-26 stsp if (refname != NULL ||
688 f8a36e22 2021-08-26 stsp bytes_sent_cur != bytes_sent) {
689 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ppa.ncolored,
690 85220b0e 2022-03-22 thomas ppa.nfound, ppa.ntrees, ppa.packfile_size,
691 f8a36e22 2021-08-26 stsp ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
692 f8a36e22 2021-08-26 stsp ppa.nobj_written, bytes_sent,
693 f8a36e22 2021-08-26 stsp refname, success);
694 f8a36e22 2021-08-26 stsp if (err) {
695 f8a36e22 2021-08-26 stsp free(refname);
696 f8a36e22 2021-08-26 stsp goto done;
697 f8a36e22 2021-08-26 stsp }
698 f8a36e22 2021-08-26 stsp bytes_sent_cur = bytes_sent;
699 f8a36e22 2021-08-26 stsp }
700 f8a36e22 2021-08-26 stsp free(refname);
701 f8a36e22 2021-08-26 stsp }
702 f8a36e22 2021-08-26 stsp done:
703 f8a36e22 2021-08-26 stsp if (sendpid != -1) {
704 f8a36e22 2021-08-26 stsp if (err)
705 f8a36e22 2021-08-26 stsp got_privsep_send_stop(imsg_sendfds[0]);
706 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
707 f8a36e22 2021-08-26 stsp err = got_error_from_errno("waitpid");
708 f8a36e22 2021-08-26 stsp }
709 f8a36e22 2021-08-26 stsp if (packfile && fclose(packfile) == EOF && err == NULL)
710 f8a36e22 2021-08-26 stsp err = got_error_from_errno("fclose");
711 f8a36e22 2021-08-26 stsp if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
712 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
713 f8a36e22 2021-08-26 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
714 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
715 f8a36e22 2021-08-26 stsp
716 f8a36e22 2021-08-26 stsp got_ref_list_free(&refs);
717 f8a36e22 2021-08-26 stsp got_pathlist_free(&have_refs);
718 f8a36e22 2021-08-26 stsp got_pathlist_free(&their_refs);
719 f8a36e22 2021-08-26 stsp for (i = 0; i < nours; i++)
720 f8a36e22 2021-08-26 stsp free(our_ids[i]);
721 f8a36e22 2021-08-26 stsp free(our_ids);
722 f8a36e22 2021-08-26 stsp for (i = 0; i < ntheirs; i++)
723 f8a36e22 2021-08-26 stsp free(their_ids[i]);
724 f8a36e22 2021-08-26 stsp free(their_ids);
725 f8a36e22 2021-08-26 stsp return err;
726 f8a36e22 2021-08-26 stsp }