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