]> git.proxmox.com Git - libgit2.git/blame - src/push.c
New upstream version 0.28.1+dfsg.1
[libgit2.git] / src / push.c
CommitLineData
613d5eb9 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
613d5eb9
PK
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
eae0bfdc
PP
8#include "push.h"
9
613d5eb9
PK
10#include "git2.h"
11
613d5eb9
PK
12#include "pack.h"
13#include "pack-objects.h"
14#include "remote.h"
15#include "vector.h"
799f9a04 16#include "tree.h"
613d5eb9 17
df93a681
PK
18static int push_spec_rref_cmp(const void *a, const void *b)
19{
20 const push_spec *push_spec_a = a, *push_spec_b = b;
21
aad638f3 22 return strcmp(push_spec_a->refspec.dst, push_spec_b->refspec.dst);
df93a681
PK
23}
24
25static int push_status_ref_cmp(const void *a, const void *b)
26{
27 const push_status *push_status_a = a, *push_status_b = b;
28
29 return strcmp(push_status_a->ref, push_status_b->ref);
30}
31
613d5eb9
PK
32int git_push_new(git_push **out, git_remote *remote)
33{
34 git_push *p;
35
36 *out = NULL;
37
38 p = git__calloc(1, sizeof(*p));
ac3d33df 39 GIT_ERROR_CHECK_ALLOC(p);
613d5eb9
PK
40
41 p->repo = remote->repo;
42 p->remote = remote;
43 p->report_status = 1;
b8b897bb 44 p->pb_parallelism = 1;
613d5eb9 45
df93a681 46 if (git_vector_init(&p->specs, 0, push_spec_rref_cmp) < 0) {
613d5eb9
PK
47 git__free(p);
48 return -1;
49 }
50
df93a681 51 if (git_vector_init(&p->status, 0, push_status_ref_cmp) < 0) {
613d5eb9
PK
52 git_vector_free(&p->specs);
53 git__free(p);
54 return -1;
55 }
56
efc2fec5
CMN
57 if (git_vector_init(&p->updates, 0, NULL) < 0) {
58 git_vector_free(&p->status);
59 git_vector_free(&p->specs);
60 git__free(p);
61 return -1;
62 }
63
613d5eb9
PK
64 *out = p;
65 return 0;
66}
67
b8b897bb
PK
68int git_push_set_options(git_push *push, const git_push_options *opts)
69{
70 if (!push || !opts)
71 return -1;
72
ac3d33df 73 GIT_ERROR_CHECK_VERSION(opts, GIT_PUSH_OPTIONS_VERSION, "git_push_options");
b8b897bb
PK
74
75 push->pb_parallelism = opts->pb_parallelism;
ac3d33df
JK
76 push->connection.custom_headers = &opts->custom_headers;
77 push->connection.proxy = &opts->proxy_opts;
b8b897bb
PK
78
79 return 0;
80}
81
613d5eb9
PK
82static void free_refspec(push_spec *spec)
83{
84 if (spec == NULL)
85 return;
86
ac3d33df 87 git_refspec__dispose(&spec->refspec);
613d5eb9
PK
88 git__free(spec);
89}
90
087f64d3 91static int check_rref(char *ref)
613d5eb9 92{
087f64d3 93 if (git__prefixcmp(ref, "refs/")) {
ac3d33df 94 git_error_set(GIT_ERROR_INVALID, "not a valid reference '%s'", ref);
613d5eb9
PK
95 return -1;
96 }
087f64d3 97
613d5eb9
PK
98 return 0;
99}
100
087f64d3
JM
101static int check_lref(git_push *push, char *ref)
102{
103 /* lref must be resolvable to an existing object */
104 git_object *obj;
105 int error = git_revparse_single(&obj, push->repo, ref);
2ebc3c66 106 git_object_free(obj);
087f64d3 107
1aa21fe3
BS
108 if (!error)
109 return 0;
087f64d3 110
1aa21fe3 111 if (error == GIT_ENOTFOUND)
ac3d33df 112 git_error_set(GIT_ERROR_REFERENCE,
1aa21fe3
BS
113 "src refspec '%s' does not match any existing object", ref);
114 else
ac3d33df 115 git_error_set(GIT_ERROR_INVALID, "not a valid reference '%s'", ref);
1aa21fe3 116 return -1;
087f64d3
JM
117}
118
119static int parse_refspec(git_push *push, push_spec **spec, const char *str)
613d5eb9
PK
120{
121 push_spec *s;
613d5eb9
PK
122
123 *spec = NULL;
124
125 s = git__calloc(1, sizeof(*s));
ac3d33df 126 GIT_ERROR_CHECK_ALLOC(s);
613d5eb9 127
aad638f3 128 if (git_refspec__parse(&s->refspec, str, false) < 0) {
ac3d33df 129 git_error_set(GIT_ERROR_INVALID, "invalid refspec %s", str);
aad638f3 130 goto on_error;
613d5eb9
PK
131 }
132
aad638f3
CMN
133 if (s->refspec.src && s->refspec.src[0] != '\0' &&
134 check_lref(push, s->refspec.src) < 0) {
135 goto on_error;
613d5eb9
PK
136 }
137
aad638f3 138 if (check_rref(s->refspec.dst) < 0)
613d5eb9
PK
139 goto on_error;
140
613d5eb9
PK
141 *spec = s;
142 return 0;
143
144on_error:
145 free_refspec(s);
146 return -1;
147}
148
149int git_push_add_refspec(git_push *push, const char *refspec)
150{
151 push_spec *spec;
152
087f64d3 153 if (parse_refspec(push, &spec, refspec) < 0 ||
613d5eb9
PK
154 git_vector_insert(&push->specs, spec) < 0)
155 return -1;
156
157 return 0;
158}
159
8f0104ec 160int git_push_update_tips(git_push *push, const git_remote_callbacks *callbacks)
1d645aab 161{
1d645aab
JM
162 git_buf remote_ref_name = GIT_BUF_INIT;
163 size_t i, j;
4330ab26 164 git_refspec *fetch_spec;
e583334c 165 push_spec *push_spec = NULL;
1d645aab
JM
166 git_reference *remote_ref;
167 push_status *status;
168 int error = 0;
169
170 git_vector_foreach(&push->status, i, status) {
321d377a 171 int fire_callback = 1;
1d645aab 172
e22ffb42
POL
173 /* Skip unsuccessful updates which have non-empty messages */
174 if (status->msg)
175 continue;
176
1d645aab 177 /* Find the corresponding remote ref */
4330ab26
CMN
178 fetch_spec = git_remote__matching_refspec(push->remote, status->ref);
179 if (!fetch_spec)
1d645aab
JM
180 continue;
181
eae0bfdc
PP
182 /* Clear the buffer which can be dirty from previous iteration */
183 git_buf_clear(&remote_ref_name);
184
bf522e08 185 if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0)
1d645aab
JM
186 goto on_error;
187
188 /* Find matching push ref spec */
189 git_vector_foreach(&push->specs, j, push_spec) {
aad638f3 190 if (!strcmp(push_spec->refspec.dst, status->ref))
1d645aab
JM
191 break;
192 }
193
194 /* Could not find the corresponding push ref spec for this push update */
195 if (j == push->specs.length)
196 continue;
197
e22ffb42
POL
198 /* Update the remote ref */
199 if (git_oid_iszero(&push_spec->loid)) {
200 error = git_reference_lookup(&remote_ref, push->remote->repo, git_buf_cstr(&remote_ref_name));
201
202 if (error >= 0) {
203 error = git_reference_delete(remote_ref);
204 git_reference_free(remote_ref);
321d377a 205 }
e22ffb42
POL
206 } else {
207 error = git_reference_create(NULL, push->remote->repo,
208 git_buf_cstr(&remote_ref_name), &push_spec->loid, 1,
209 "update by push");
321d377a
JG
210 }
211
212 if (error < 0) {
213 if (error != GIT_ENOTFOUND)
1d645aab 214 goto on_error;
321d377a 215
ac3d33df 216 git_error_clear();
321d377a
JG
217 fire_callback = 0;
218 }
219
8f0104ec
CMN
220 if (fire_callback && callbacks && callbacks->update_tips) {
221 error = callbacks->update_tips(git_buf_cstr(&remote_ref_name),
222 &push_spec->roid, &push_spec->loid, callbacks->payload);
321d377a
JG
223
224 if (error < 0)
225 goto on_error;
226 }
1d645aab
JM
227 }
228
229 error = 0;
230
231on_error:
ac3d33df 232 git_buf_dispose(&remote_ref_name);
1d645aab
JM
233 return error;
234}
235
5ce6c1e9
CMN
236/**
237 * Insert all tags until we find a non-tag object, which is returned
238 * in `out`.
239 */
240static int enqueue_tag(git_object **out, git_push *push, git_oid *id)
241{
242 git_object *obj = NULL, *target = NULL;
243 int error;
244
ac3d33df 245 if ((error = git_object_lookup(&obj, push->repo, id, GIT_OBJECT_TAG)) < 0)
5ce6c1e9
CMN
246 return error;
247
ac3d33df 248 while (git_object_type(obj) == GIT_OBJECT_TAG) {
5ce6c1e9
CMN
249 if ((error = git_packbuilder_insert(push->pb, git_object_id(obj), NULL)) < 0)
250 break;
251
252 if ((error = git_tag_target(&target, (git_tag *) obj)) < 0)
253 break;
254
255 git_object_free(obj);
256 obj = target;
257 }
258
259 if (error < 0)
260 git_object_free(obj);
261 else
262 *out = obj;
263
264 return error;
265}
266
eae0bfdc 267static int queue_objects(git_push *push)
613d5eb9
PK
268{
269 git_remote_head *head;
270 push_spec *spec;
271 git_revwalk *rw;
613d5eb9
PK
272 unsigned int i;
273 int error = -1;
274
275 if (git_revwalk_new(&rw, push->repo) < 0)
276 return -1;
277
278 git_revwalk_sorting(rw, GIT_SORT_TIME);
279
280 git_vector_foreach(&push->specs, i, spec) {
ac3d33df 281 git_object_t type;
abeefbbe
MS
282 size_t size;
283
613d5eb9
PK
284 if (git_oid_iszero(&spec->loid))
285 /*
286 * Delete reference on remote side;
287 * nothing to do here.
288 */
289 continue;
290
291 if (git_oid_equal(&spec->loid, &spec->roid))
292 continue; /* up-to-date */
293
abeefbbe
MS
294 if (git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid) < 0)
295 goto on_error;
296
ac3d33df 297 if (type == GIT_OBJECT_TAG) {
abeefbbe
MS
298 git_object *target;
299
5ce6c1e9 300 if ((error = enqueue_tag(&target, push, &spec->loid)) < 0)
abeefbbe 301 goto on_error;
abeefbbe 302
ac3d33df 303 if (git_object_type(target) == GIT_OBJECT_COMMIT) {
abeefbbe
MS
304 if (git_revwalk_push(rw, git_object_id(target)) < 0) {
305 git_object_free(target);
306 goto on_error;
307 }
308 } else {
309 if (git_packbuilder_insert(
310 push->pb, git_object_id(target), NULL) < 0) {
311 git_object_free(target);
312 goto on_error;
313 }
314 }
315 git_object_free(target);
316 } else if (git_revwalk_push(rw, &spec->loid) < 0)
613d5eb9
PK
317 goto on_error;
318
aad638f3 319 if (!spec->refspec.force) {
613d5eb9
PK
320 git_oid base;
321
322 if (git_oid_iszero(&spec->roid))
323 continue;
324
325 if (!git_odb_exists(push->repo->_odb, &spec->roid)) {
ac3d33df 326 git_error_set(GIT_ERROR_REFERENCE,
909d5494 327 "cannot push because a reference that you are trying to update on the remote contains commits that are not present locally.");
613d5eb9
PK
328 error = GIT_ENONFASTFORWARD;
329 goto on_error;
330 }
331
332 error = git_merge_base(&base, push->repo,
333 &spec->loid, &spec->roid);
334
335 if (error == GIT_ENOTFOUND ||
336 (!error && !git_oid_equal(&base, &spec->roid))) {
ac3d33df 337 git_error_set(GIT_ERROR_REFERENCE,
909d5494 338 "cannot push non-fastforwardable reference");
613d5eb9
PK
339 error = GIT_ENONFASTFORWARD;
340 goto on_error;
341 }
342
343 if (error < 0)
344 goto on_error;
345 }
346 }
347
348 git_vector_foreach(&push->remote->refs, i, head) {
349 if (git_oid_iszero(&head->oid))
350 continue;
351
352 /* TODO */
353 git_revwalk_hide(rw, &head->oid);
354 }
355
eae0bfdc 356 error = git_packbuilder_insert_walk(push->pb, rw);
613d5eb9
PK
357
358on_error:
359 git_revwalk_free(rw);
613d5eb9
PK
360 return error;
361}
362
efc2fec5
CMN
363static int add_update(git_push *push, push_spec *spec)
364{
365 git_push_update *u = git__calloc(1, sizeof(git_push_update));
ac3d33df 366 GIT_ERROR_CHECK_ALLOC(u);
efc2fec5
CMN
367
368 u->src_refname = git__strdup(spec->refspec.src);
ac3d33df 369 GIT_ERROR_CHECK_ALLOC(u->src_refname);
254ff3e9
CMN
370
371 u->dst_refname = git__strdup(spec->refspec.dst);
ac3d33df 372 GIT_ERROR_CHECK_ALLOC(u->dst_refname);
efc2fec5 373
254ff3e9
CMN
374 git_oid_cpy(&u->src, &spec->roid);
375 git_oid_cpy(&u->dst, &spec->loid);
efc2fec5
CMN
376
377 return git_vector_insert(&push->updates, u);
378}
379
613d5eb9
PK
380static int calculate_work(git_push *push)
381{
382 git_remote_head *head;
383 push_spec *spec;
384 unsigned int i, j;
385
4128f5aa
CW
386 /* Update local and remote oids*/
387
613d5eb9 388 git_vector_foreach(&push->specs, i, spec) {
aad638f3 389 if (spec->refspec.src && spec->refspec.src[0]!= '\0') {
4128f5aa 390 /* This is a create or update. Local ref must exist. */
613d5eb9 391 if (git_reference_name_to_id(
aad638f3 392 &spec->loid, push->repo, spec->refspec.src) < 0) {
ac3d33df 393 git_error_set(GIT_ERROR_REFERENCE, "no such reference '%s'", spec->refspec.src);
613d5eb9
PK
394 return -1;
395 }
4128f5aa 396 }
613d5eb9 397
aad638f3
CMN
398 /* Remote ref may or may not (e.g. during create) already exist. */
399 git_vector_foreach(&push->remote->refs, j, head) {
400 if (!strcmp(spec->refspec.dst, head->name)) {
401 git_oid_cpy(&spec->roid, &head->oid);
402 break;
613d5eb9
PK
403 }
404 }
efc2fec5
CMN
405
406 if (add_update(push, spec) < 0)
407 return -1;
613d5eb9
PK
408 }
409
410 return 0;
411}
412
8f0104ec 413static int do_push(git_push *push, const git_remote_callbacks *callbacks)
613d5eb9 414{
5b188225 415 int error = 0;
613d5eb9
PK
416 git_transport *transport = push->remote->transport;
417
9bf56c7b 418 if (!transport->push) {
ac3d33df 419 git_error_set(GIT_ERROR_NET, "remote transport doesn't support push");
9bf56c7b
SB
420 error = -1;
421 goto on_error;
422 }
423
613d5eb9
PK
424 /*
425 * A pack-file MUST be sent if either create or update command
426 * is used, even if the server already has all the necessary
427 * objects. In this case the client MUST send an empty pack-file.
428 */
429
b8b897bb
PK
430 if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0)
431 goto on_error;
432
433 git_packbuilder_set_threads(push->pb, push->pb_parallelism);
434
8f0104ec
CMN
435 if (callbacks && callbacks->pack_progress)
436 if ((error = git_packbuilder_set_callbacks(push->pb, callbacks->pack_progress, callbacks->payload)) < 0)
b176eded
JM
437 goto on_error;
438
efc2fec5
CMN
439 if ((error = calculate_work(push)) < 0)
440 goto on_error;
441
8f0104ec
CMN
442 if (callbacks && callbacks->push_negotiation &&
443 (error = callbacks->push_negotiation((const git_push_update **) push->updates.contents,
444 push->updates.length, callbacks->payload)) < 0)
efc2fec5
CMN
445 goto on_error;
446
447 if ((error = queue_objects(push)) < 0 ||
8f0104ec 448 (error = transport->push(transport, push, callbacks)) < 0)
613d5eb9
PK
449 goto on_error;
450
613d5eb9
PK
451on_error:
452 git_packbuilder_free(push->pb);
453 return error;
454}
455
613d5eb9
PK
456static int filter_refs(git_remote *remote)
457{
98eaf39a 458 const git_remote_head **heads;
359dce72
CMN
459 size_t heads_len, i;
460
613d5eb9 461 git_vector_clear(&remote->refs);
359dce72
CMN
462
463 if (git_remote_ls(&heads, &heads_len, remote) < 0)
464 return -1;
465
466 for (i = 0; i < heads_len; i++) {
98eaf39a 467 if (git_vector_insert(&remote->refs, (void *)heads[i]) < 0)
359dce72
CMN
468 return -1;
469 }
470
471 return 0;
613d5eb9
PK
472}
473
8f0104ec 474int git_push_finish(git_push *push, const git_remote_callbacks *callbacks)
613d5eb9
PK
475{
476 int error;
477
478 if (!git_remote_connected(push->remote) &&
ac3d33df 479 (error = git_remote__connect(push->remote, GIT_DIRECTION_PUSH, callbacks, &push->connection)) < 0)
613d5eb9
PK
480 return error;
481
482 if ((error = filter_refs(push->remote)) < 0 ||
8f0104ec 483 (error = do_push(push, callbacks)) < 0)
613d5eb9
PK
484 return error;
485
d524b2d3
CMN
486 if (!push->unpack_ok) {
487 error = -1;
ac3d33df 488 git_error_set(GIT_ERROR_NET, "unpacking the sent packfile failed on the remote");
d524b2d3 489 }
613d5eb9 490
d524b2d3 491 return error;
613d5eb9
PK
492}
493
494int git_push_status_foreach(git_push *push,
495 int (*cb)(const char *ref, const char *msg, void *data),
496 void *data)
497{
498 push_status *status;
499 unsigned int i;
500
501 git_vector_foreach(&push->status, i, status) {
c7b3e1b3
RB
502 int error = cb(status->ref, status->msg, data);
503 if (error)
ac3d33df 504 return git_error_set_after_callback(error);
613d5eb9
PK
505 }
506
507 return 0;
508}
509
20858f6e 510void git_push_status_free(push_status *status)
511{
512 if (status == NULL)
513 return;
514
be6996b7 515 git__free(status->msg);
20858f6e 516 git__free(status->ref);
517 git__free(status);
518}
519
613d5eb9
PK
520void git_push_free(git_push *push)
521{
522 push_spec *spec;
523 push_status *status;
3251972e 524 git_push_update *update;
613d5eb9
PK
525 unsigned int i;
526
527 if (push == NULL)
528 return;
529
530 git_vector_foreach(&push->specs, i, spec) {
531 free_refspec(spec);
532 }
533 git_vector_free(&push->specs);
534
535 git_vector_foreach(&push->status, i, status) {
20858f6e 536 git_push_status_free(status);
613d5eb9
PK
537 }
538 git_vector_free(&push->status);
539
3251972e
CMN
540 git_vector_foreach(&push->updates, i, update) {
541 git__free(update->src_refname);
542 git__free(update->dst_refname);
543 git__free(update);
544 }
545 git_vector_free(&push->updates);
546
613d5eb9
PK
547 git__free(push);
548}
b9f81997 549
702efc89 550int git_push_init_options(git_push_options *opts, unsigned int version)
b9f81997 551{
702efc89
RB
552 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
553 opts, version, git_push_options, GIT_PUSH_OPTIONS_INIT);
554 return 0;
b9f81997 555}