]>
Commit | Line | Data |
---|---|---|
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 | ||
8 | #include "git2.h" | |
9 | ||
10 | #include "common.h" | |
11 | #include "pack.h" | |
12 | #include "pack-objects.h" | |
13 | #include "remote.h" | |
14 | #include "vector.h" | |
15 | #include "push.h" | |
799f9a04 | 16 | #include "tree.h" |
613d5eb9 | 17 | |
df93a681 PK |
18 | static 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 | ||
22 | return strcmp(push_spec_a->rref, push_spec_b->rref); | |
23 | } | |
24 | ||
25 | static 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 |
32 | int 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)); | |
39 | GITERR_CHECK_ALLOC(p); | |
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 | ||
57 | *out = p; | |
58 | return 0; | |
59 | } | |
60 | ||
b8b897bb PK |
61 | int git_push_set_options(git_push *push, const git_push_options *opts) |
62 | { | |
63 | if (!push || !opts) | |
64 | return -1; | |
65 | ||
66 | GITERR_CHECK_VERSION(opts, GIT_PUSH_OPTIONS_VERSION, "git_push_options"); | |
67 | ||
68 | push->pb_parallelism = opts->pb_parallelism; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
b176eded JM |
73 | int git_push_set_callbacks( |
74 | git_push *push, | |
75 | git_packbuilder_progress pack_progress_cb, | |
76 | void *pack_progress_cb_payload, | |
77 | git_push_transfer_progress transfer_progress_cb, | |
78 | void *transfer_progress_cb_payload) | |
79 | { | |
80 | if (!push) | |
81 | return -1; | |
82 | ||
83 | push->pack_progress_cb = pack_progress_cb; | |
84 | push->pack_progress_cb_payload = pack_progress_cb_payload; | |
85 | ||
86 | push->transfer_progress_cb = transfer_progress_cb; | |
87 | push->transfer_progress_cb_payload = transfer_progress_cb_payload; | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
613d5eb9 PK |
92 | static void free_refspec(push_spec *spec) |
93 | { | |
94 | if (spec == NULL) | |
95 | return; | |
96 | ||
97 | if (spec->lref) | |
98 | git__free(spec->lref); | |
99 | ||
100 | if (spec->rref) | |
101 | git__free(spec->rref); | |
102 | ||
103 | git__free(spec); | |
104 | } | |
105 | ||
087f64d3 | 106 | static int check_rref(char *ref) |
613d5eb9 | 107 | { |
087f64d3 JM |
108 | if (git__prefixcmp(ref, "refs/")) { |
109 | giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref); | |
613d5eb9 PK |
110 | return -1; |
111 | } | |
087f64d3 | 112 | |
613d5eb9 PK |
113 | return 0; |
114 | } | |
115 | ||
087f64d3 JM |
116 | static int check_lref(git_push *push, char *ref) |
117 | { | |
118 | /* lref must be resolvable to an existing object */ | |
119 | git_object *obj; | |
120 | int error = git_revparse_single(&obj, push->repo, ref); | |
2ebc3c66 | 121 | git_object_free(obj); |
087f64d3 | 122 | |
1aa21fe3 BS |
123 | if (!error) |
124 | return 0; | |
087f64d3 | 125 | |
1aa21fe3 BS |
126 | if (error == GIT_ENOTFOUND) |
127 | giterr_set(GITERR_REFERENCE, | |
128 | "src refspec '%s' does not match any existing object", ref); | |
129 | else | |
130 | giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref); | |
131 | return -1; | |
087f64d3 JM |
132 | } |
133 | ||
134 | static int parse_refspec(git_push *push, push_spec **spec, const char *str) | |
613d5eb9 PK |
135 | { |
136 | push_spec *s; | |
137 | char *delim; | |
138 | ||
139 | *spec = NULL; | |
140 | ||
141 | s = git__calloc(1, sizeof(*s)); | |
142 | GITERR_CHECK_ALLOC(s); | |
143 | ||
144 | if (str[0] == '+') { | |
145 | s->force = true; | |
146 | str++; | |
147 | } | |
148 | ||
613d5eb9 PK |
149 | delim = strchr(str, ':'); |
150 | if (delim == NULL) { | |
151 | s->lref = git__strdup(str); | |
087f64d3 JM |
152 | if (!s->lref || check_lref(push, s->lref) < 0) |
153 | goto on_error; | |
613d5eb9 PK |
154 | } else { |
155 | if (delim - str) { | |
156 | s->lref = git__strndup(str, delim - str); | |
087f64d3 JM |
157 | if (!s->lref || check_lref(push, s->lref) < 0) |
158 | goto on_error; | |
4128f5aa | 159 | } |
613d5eb9 PK |
160 | |
161 | if (strlen(delim + 1)) { | |
162 | s->rref = git__strdup(delim + 1); | |
087f64d3 JM |
163 | if (!s->rref || check_rref(s->rref) < 0) |
164 | goto on_error; | |
4128f5aa | 165 | } |
613d5eb9 PK |
166 | } |
167 | ||
168 | if (!s->lref && !s->rref) | |
169 | goto on_error; | |
170 | ||
4128f5aa CW |
171 | /* If rref is ommitted, use the same ref name as lref */ |
172 | if (!s->rref) { | |
173 | s->rref = git__strdup(s->lref); | |
abeefbbe MS |
174 | if (!s->rref || check_rref(s->rref) < 0) |
175 | goto on_error; | |
4128f5aa CW |
176 | } |
177 | ||
613d5eb9 PK |
178 | *spec = s; |
179 | return 0; | |
180 | ||
181 | on_error: | |
182 | free_refspec(s); | |
183 | return -1; | |
184 | } | |
185 | ||
186 | int git_push_add_refspec(git_push *push, const char *refspec) | |
187 | { | |
188 | push_spec *spec; | |
189 | ||
087f64d3 | 190 | if (parse_refspec(push, &spec, refspec) < 0 || |
613d5eb9 PK |
191 | git_vector_insert(&push->specs, spec) < 0) |
192 | return -1; | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
491cecfe BS |
197 | int git_push_update_tips( |
198 | git_push *push, | |
199 | const git_signature *signature, | |
200 | const char *reflog_message) | |
1d645aab | 201 | { |
1d645aab JM |
202 | git_buf remote_ref_name = GIT_BUF_INIT; |
203 | size_t i, j; | |
4330ab26 | 204 | git_refspec *fetch_spec; |
e583334c | 205 | push_spec *push_spec = NULL; |
1d645aab JM |
206 | git_reference *remote_ref; |
207 | push_status *status; | |
208 | int error = 0; | |
209 | ||
210 | git_vector_foreach(&push->status, i, status) { | |
321d377a | 211 | int fire_callback = 1; |
1d645aab JM |
212 | |
213 | /* Find the corresponding remote ref */ | |
4330ab26 CMN |
214 | fetch_spec = git_remote__matching_refspec(push->remote, status->ref); |
215 | if (!fetch_spec) | |
1d645aab JM |
216 | continue; |
217 | ||
bf522e08 | 218 | if ((error = git_refspec_transform(&remote_ref_name, fetch_spec, status->ref)) < 0) |
1d645aab JM |
219 | goto on_error; |
220 | ||
221 | /* Find matching push ref spec */ | |
222 | git_vector_foreach(&push->specs, j, push_spec) { | |
223 | if (!strcmp(push_spec->rref, status->ref)) | |
224 | break; | |
225 | } | |
226 | ||
227 | /* Could not find the corresponding push ref spec for this push update */ | |
228 | if (j == push->specs.length) | |
229 | continue; | |
230 | ||
321d377a JG |
231 | /* If this ref update was successful (ok, not ng), it will have an empty message */ |
232 | if (status->msg == NULL) { | |
233 | /* Update the remote ref */ | |
234 | if (git_oid_iszero(&push_spec->loid)) { | |
235 | error = git_reference_lookup(&remote_ref, push->remote->repo, git_buf_cstr(&remote_ref_name)); | |
1d645aab | 236 | |
321d377a JG |
237 | if (error >= 0) { |
238 | error = git_reference_delete(remote_ref); | |
20858f6e | 239 | git_reference_free(remote_ref); |
20858f6e | 240 | } |
321d377a JG |
241 | } else { |
242 | error = git_reference_create(NULL, push->remote->repo, | |
243 | git_buf_cstr(&remote_ref_name), &push_spec->loid, 1, signature, | |
244 | reflog_message ? reflog_message : "update by push"); | |
245 | } | |
246 | } | |
247 | ||
248 | if (error < 0) { | |
249 | if (error != GIT_ENOTFOUND) | |
1d645aab | 250 | goto on_error; |
321d377a JG |
251 | |
252 | giterr_clear(); | |
253 | fire_callback = 0; | |
254 | } | |
255 | ||
256 | if (fire_callback && push->remote->callbacks.update_tips) { | |
257 | error = push->remote->callbacks.update_tips(git_buf_cstr(&remote_ref_name), | |
258 | &push_spec->roid, &push_spec->loid, push->remote->callbacks.payload); | |
259 | ||
260 | if (error < 0) | |
261 | goto on_error; | |
262 | } | |
1d645aab JM |
263 | } |
264 | ||
265 | error = 0; | |
266 | ||
267 | on_error: | |
268 | git_buf_free(&remote_ref_name); | |
269 | return error; | |
270 | } | |
271 | ||
5ce6c1e9 CMN |
272 | /** |
273 | * Insert all tags until we find a non-tag object, which is returned | |
274 | * in `out`. | |
275 | */ | |
276 | static int enqueue_tag(git_object **out, git_push *push, git_oid *id) | |
277 | { | |
278 | git_object *obj = NULL, *target = NULL; | |
279 | int error; | |
280 | ||
281 | if ((error = git_object_lookup(&obj, push->repo, id, GIT_OBJ_TAG)) < 0) | |
282 | return error; | |
283 | ||
284 | while (git_object_type(obj) == GIT_OBJ_TAG) { | |
285 | if ((error = git_packbuilder_insert(push->pb, git_object_id(obj), NULL)) < 0) | |
286 | break; | |
287 | ||
288 | if ((error = git_tag_target(&target, (git_tag *) obj)) < 0) | |
289 | break; | |
290 | ||
291 | git_object_free(obj); | |
292 | obj = target; | |
293 | } | |
294 | ||
295 | if (error < 0) | |
296 | git_object_free(obj); | |
297 | else | |
298 | *out = obj; | |
299 | ||
300 | return error; | |
301 | } | |
302 | ||
613d5eb9 PK |
303 | static int revwalk(git_vector *commits, git_push *push) |
304 | { | |
305 | git_remote_head *head; | |
306 | push_spec *spec; | |
307 | git_revwalk *rw; | |
308 | git_oid oid; | |
309 | unsigned int i; | |
310 | int error = -1; | |
311 | ||
312 | if (git_revwalk_new(&rw, push->repo) < 0) | |
313 | return -1; | |
314 | ||
315 | git_revwalk_sorting(rw, GIT_SORT_TIME); | |
316 | ||
317 | git_vector_foreach(&push->specs, i, spec) { | |
abeefbbe MS |
318 | git_otype type; |
319 | size_t size; | |
320 | ||
613d5eb9 PK |
321 | if (git_oid_iszero(&spec->loid)) |
322 | /* | |
323 | * Delete reference on remote side; | |
324 | * nothing to do here. | |
325 | */ | |
326 | continue; | |
327 | ||
328 | if (git_oid_equal(&spec->loid, &spec->roid)) | |
329 | continue; /* up-to-date */ | |
330 | ||
abeefbbe MS |
331 | if (git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid) < 0) |
332 | goto on_error; | |
333 | ||
334 | if (type == GIT_OBJ_TAG) { | |
abeefbbe MS |
335 | git_object *target; |
336 | ||
5ce6c1e9 | 337 | if ((error = enqueue_tag(&target, push, &spec->loid)) < 0) |
abeefbbe | 338 | goto on_error; |
abeefbbe MS |
339 | |
340 | if (git_object_type(target) == GIT_OBJ_COMMIT) { | |
341 | if (git_revwalk_push(rw, git_object_id(target)) < 0) { | |
342 | git_object_free(target); | |
343 | goto on_error; | |
344 | } | |
345 | } else { | |
346 | if (git_packbuilder_insert( | |
347 | push->pb, git_object_id(target), NULL) < 0) { | |
348 | git_object_free(target); | |
349 | goto on_error; | |
350 | } | |
351 | } | |
352 | git_object_free(target); | |
353 | } else if (git_revwalk_push(rw, &spec->loid) < 0) | |
613d5eb9 PK |
354 | goto on_error; |
355 | ||
356 | if (!spec->force) { | |
357 | git_oid base; | |
358 | ||
359 | if (git_oid_iszero(&spec->roid)) | |
360 | continue; | |
361 | ||
362 | if (!git_odb_exists(push->repo->_odb, &spec->roid)) { | |
62caf3f3 | 363 | giterr_set(GITERR_REFERENCE, "Cannot push missing reference"); |
613d5eb9 PK |
364 | error = GIT_ENONFASTFORWARD; |
365 | goto on_error; | |
366 | } | |
367 | ||
368 | error = git_merge_base(&base, push->repo, | |
369 | &spec->loid, &spec->roid); | |
370 | ||
371 | if (error == GIT_ENOTFOUND || | |
372 | (!error && !git_oid_equal(&base, &spec->roid))) { | |
62caf3f3 RB |
373 | giterr_set(GITERR_REFERENCE, |
374 | "Cannot push non-fastforwardable reference"); | |
613d5eb9 PK |
375 | error = GIT_ENONFASTFORWARD; |
376 | goto on_error; | |
377 | } | |
378 | ||
379 | if (error < 0) | |
380 | goto on_error; | |
381 | } | |
382 | } | |
383 | ||
384 | git_vector_foreach(&push->remote->refs, i, head) { | |
385 | if (git_oid_iszero(&head->oid)) | |
386 | continue; | |
387 | ||
388 | /* TODO */ | |
389 | git_revwalk_hide(rw, &head->oid); | |
390 | } | |
391 | ||
392 | while ((error = git_revwalk_next(&oid, rw)) == 0) { | |
393 | git_oid *o = git__malloc(GIT_OID_RAWSZ); | |
62caf3f3 | 394 | if (!o) { |
613d5eb9 PK |
395 | error = -1; |
396 | goto on_error; | |
397 | } | |
62caf3f3 RB |
398 | git_oid_cpy(o, &oid); |
399 | if ((error = git_vector_insert(commits, o)) < 0) | |
400 | goto on_error; | |
613d5eb9 PK |
401 | } |
402 | ||
403 | on_error: | |
404 | git_revwalk_free(rw); | |
405 | return error == GIT_ITEROVER ? 0 : error; | |
406 | } | |
407 | ||
bef2a12c PK |
408 | static int enqueue_object( |
409 | const git_tree_entry *entry, | |
410 | git_packbuilder *pb) | |
411 | { | |
412 | switch (git_tree_entry_type(entry)) { | |
413 | case GIT_OBJ_COMMIT: | |
414 | return 0; | |
415 | case GIT_OBJ_TREE: | |
416 | return git_packbuilder_insert_tree(pb, &entry->oid); | |
417 | default: | |
418 | return git_packbuilder_insert(pb, &entry->oid, entry->filename); | |
419 | } | |
420 | } | |
421 | ||
799f9a04 PK |
422 | static int queue_differences( |
423 | git_tree *base, | |
424 | git_tree *delta, | |
425 | git_packbuilder *pb) | |
613d5eb9 | 426 | { |
799f9a04 PK |
427 | git_tree *b_child = NULL, *d_child = NULL; |
428 | size_t b_length = git_tree_entrycount(base); | |
429 | size_t d_length = git_tree_entrycount(delta); | |
430 | size_t i = 0, j = 0; | |
613d5eb9 PK |
431 | int error; |
432 | ||
799f9a04 PK |
433 | while (i < b_length && j < d_length) { |
434 | const git_tree_entry *b_entry = git_tree_entry_byindex(base, i); | |
435 | const git_tree_entry *d_entry = git_tree_entry_byindex(delta, j); | |
436 | int cmp = 0; | |
437 | ||
b7f167da | 438 | if (!git_oid__cmp(&b_entry->oid, &d_entry->oid)) |
799f9a04 PK |
439 | goto loop; |
440 | ||
cd01dd5d | 441 | cmp = strcmp(b_entry->filename, d_entry->filename); |
799f9a04 PK |
442 | |
443 | /* If the entries are both trees and they have the same name but are | |
444 | * different, then we'll recurse after adding the right-hand entry */ | |
445 | if (!cmp && | |
446 | git_tree_entry__is_tree(b_entry) && | |
447 | git_tree_entry__is_tree(d_entry)) { | |
448 | /* Add the right-hand entry */ | |
449 | if ((error = git_packbuilder_insert(pb, &d_entry->oid, | |
450 | d_entry->filename)) < 0) | |
451 | goto on_error; | |
452 | ||
453 | /* Acquire the subtrees and recurse */ | |
454 | if ((error = git_tree_lookup(&b_child, | |
455 | git_tree_owner(base), &b_entry->oid)) < 0 || | |
456 | (error = git_tree_lookup(&d_child, | |
457 | git_tree_owner(delta), &d_entry->oid)) < 0 || | |
458 | (error = queue_differences(b_child, d_child, pb)) < 0) | |
459 | goto on_error; | |
460 | ||
461 | git_tree_free(b_child); b_child = NULL; | |
462 | git_tree_free(d_child); d_child = NULL; | |
463 | } | |
464 | /* If the object is new or different in the right-hand tree, | |
465 | * then enumerate it */ | |
bef2a12c PK |
466 | else if (cmp >= 0 && |
467 | (error = enqueue_object(d_entry, pb)) < 0) | |
468 | goto on_error; | |
799f9a04 PK |
469 | |
470 | loop: | |
471 | if (cmp <= 0) i++; | |
472 | if (cmp >= 0) j++; | |
473 | } | |
474 | ||
475 | /* Drain the right-hand tree of entries */ | |
476 | for (; j < d_length; j++) | |
bef2a12c PK |
477 | if ((error = enqueue_object(git_tree_entry_byindex(delta, j), pb)) < 0) |
478 | goto on_error; | |
799f9a04 PK |
479 | |
480 | error = 0; | |
481 | ||
482 | on_error: | |
483 | if (b_child) | |
484 | git_tree_free(b_child); | |
485 | ||
486 | if (d_child) | |
487 | git_tree_free(d_child); | |
488 | ||
489 | return error; | |
490 | } | |
491 | ||
492 | static int queue_objects(git_push *push) | |
493 | { | |
494 | git_vector commits = GIT_VECTOR_INIT; | |
495 | git_oid *oid; | |
496 | size_t i; | |
497 | unsigned j; | |
498 | int error; | |
613d5eb9 PK |
499 | |
500 | if ((error = revwalk(&commits, push)) < 0) | |
501 | goto on_error; | |
502 | ||
799f9a04 PK |
503 | git_vector_foreach(&commits, i, oid) { |
504 | git_commit *parent = NULL, *commit; | |
505 | git_tree *tree = NULL, *ptree = NULL; | |
506 | size_t parentcount; | |
613d5eb9 | 507 | |
799f9a04 | 508 | if ((error = git_commit_lookup(&commit, push->repo, oid)) < 0) |
613d5eb9 | 509 | goto on_error; |
613d5eb9 | 510 | |
799f9a04 PK |
511 | /* Insert the commit */ |
512 | if ((error = git_packbuilder_insert(push->pb, oid, NULL)) < 0) | |
513 | goto loop_error; | |
613d5eb9 | 514 | |
799f9a04 | 515 | parentcount = git_commit_parentcount(commit); |
613d5eb9 | 516 | |
799f9a04 | 517 | if (!parentcount) { |
613d5eb9 | 518 | if ((error = git_packbuilder_insert_tree(push->pb, |
799f9a04 PK |
519 | git_commit_tree_id(commit))) < 0) |
520 | goto loop_error; | |
521 | } else { | |
522 | if ((error = git_tree_lookup(&tree, push->repo, | |
523 | git_commit_tree_id(commit))) < 0 || | |
524 | (error = git_packbuilder_insert(push->pb, | |
525 | git_commit_tree_id(commit), NULL)) < 0) | |
526 | goto loop_error; | |
527 | ||
528 | /* For each parent, add the items which are different */ | |
529 | for (j = 0; j < parentcount; j++) { | |
530 | if ((error = git_commit_parent(&parent, commit, j)) < 0 || | |
531 | (error = git_commit_tree(&ptree, parent)) < 0 || | |
532 | (error = queue_differences(ptree, tree, push->pb)) < 0) | |
533 | goto loop_error; | |
534 | ||
535 | git_tree_free(ptree); ptree = NULL; | |
536 | git_commit_free(parent); parent = NULL; | |
613d5eb9 | 537 | } |
613d5eb9 | 538 | } |
799f9a04 PK |
539 | |
540 | error = 0; | |
541 | ||
542 | loop_error: | |
543 | if (tree) | |
544 | git_tree_free(tree); | |
545 | ||
546 | if (ptree) | |
547 | git_tree_free(ptree); | |
548 | ||
549 | if (parent) | |
550 | git_commit_free(parent); | |
551 | ||
552 | git_commit_free(commit); | |
553 | ||
554 | if (error < 0) | |
555 | goto on_error; | |
613d5eb9 | 556 | } |
799f9a04 | 557 | |
613d5eb9 PK |
558 | error = 0; |
559 | ||
560 | on_error: | |
9cfce273 | 561 | git_vector_free_deep(&commits); |
613d5eb9 PK |
562 | return error; |
563 | } | |
564 | ||
565 | static int calculate_work(git_push *push) | |
566 | { | |
567 | git_remote_head *head; | |
568 | push_spec *spec; | |
569 | unsigned int i, j; | |
570 | ||
4128f5aa CW |
571 | /* Update local and remote oids*/ |
572 | ||
613d5eb9 PK |
573 | git_vector_foreach(&push->specs, i, spec) { |
574 | if (spec->lref) { | |
4128f5aa | 575 | /* This is a create or update. Local ref must exist. */ |
613d5eb9 PK |
576 | if (git_reference_name_to_id( |
577 | &spec->loid, push->repo, spec->lref) < 0) { | |
62caf3f3 | 578 | giterr_set(GITERR_REFERENCE, "No such reference '%s'", spec->lref); |
613d5eb9 PK |
579 | return -1; |
580 | } | |
4128f5aa | 581 | } |
613d5eb9 | 582 | |
4128f5aa CW |
583 | if (spec->rref) { |
584 | /* Remote ref may or may not (e.g. during create) already exist. */ | |
585 | git_vector_foreach(&push->remote->refs, j, head) { | |
586 | if (!strcmp(spec->rref, head->name)) { | |
587 | git_oid_cpy(&spec->roid, &head->oid); | |
588 | break; | |
613d5eb9 PK |
589 | } |
590 | } | |
591 | } | |
592 | } | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
597 | static int do_push(git_push *push) | |
598 | { | |
5b188225 | 599 | int error = 0; |
613d5eb9 PK |
600 | git_transport *transport = push->remote->transport; |
601 | ||
9bf56c7b SB |
602 | if (!transport->push) { |
603 | giterr_set(GITERR_NET, "Remote transport doesn't support push"); | |
604 | error = -1; | |
605 | goto on_error; | |
606 | } | |
607 | ||
613d5eb9 PK |
608 | /* |
609 | * A pack-file MUST be sent if either create or update command | |
610 | * is used, even if the server already has all the necessary | |
611 | * objects. In this case the client MUST send an empty pack-file. | |
612 | */ | |
613 | ||
b8b897bb PK |
614 | if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0) |
615 | goto on_error; | |
616 | ||
617 | git_packbuilder_set_threads(push->pb, push->pb_parallelism); | |
618 | ||
b176eded JM |
619 | if (push->pack_progress_cb) |
620 | if ((error = git_packbuilder_set_callbacks(push->pb, push->pack_progress_cb, push->pack_progress_cb_payload)) < 0) | |
621 | goto on_error; | |
622 | ||
b8b897bb | 623 | if ((error = calculate_work(push)) < 0 || |
613d5eb9 PK |
624 | (error = queue_objects(push)) < 0 || |
625 | (error = transport->push(transport, push)) < 0) | |
626 | goto on_error; | |
627 | ||
613d5eb9 PK |
628 | on_error: |
629 | git_packbuilder_free(push->pb); | |
630 | return error; | |
631 | } | |
632 | ||
613d5eb9 PK |
633 | static int filter_refs(git_remote *remote) |
634 | { | |
98eaf39a | 635 | const git_remote_head **heads; |
359dce72 CMN |
636 | size_t heads_len, i; |
637 | ||
613d5eb9 | 638 | git_vector_clear(&remote->refs); |
359dce72 CMN |
639 | |
640 | if (git_remote_ls(&heads, &heads_len, remote) < 0) | |
641 | return -1; | |
642 | ||
643 | for (i = 0; i < heads_len; i++) { | |
98eaf39a | 644 | if (git_vector_insert(&remote->refs, (void *)heads[i]) < 0) |
359dce72 CMN |
645 | return -1; |
646 | } | |
647 | ||
648 | return 0; | |
613d5eb9 PK |
649 | } |
650 | ||
651 | int git_push_finish(git_push *push) | |
652 | { | |
653 | int error; | |
654 | ||
655 | if (!git_remote_connected(push->remote) && | |
656 | (error = git_remote_connect(push->remote, GIT_DIRECTION_PUSH)) < 0) | |
657 | return error; | |
658 | ||
659 | if ((error = filter_refs(push->remote)) < 0 || | |
660 | (error = do_push(push)) < 0) | |
661 | return error; | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
3b4ba278 | 666 | int git_push_unpack_ok(const git_push *push) |
613d5eb9 PK |
667 | { |
668 | return push->unpack_ok; | |
669 | } | |
670 | ||
671 | int git_push_status_foreach(git_push *push, | |
672 | int (*cb)(const char *ref, const char *msg, void *data), | |
673 | void *data) | |
674 | { | |
675 | push_status *status; | |
676 | unsigned int i; | |
677 | ||
678 | git_vector_foreach(&push->status, i, status) { | |
c7b3e1b3 RB |
679 | int error = cb(status->ref, status->msg, data); |
680 | if (error) | |
26c1cb91 | 681 | return giterr_set_after_callback(error); |
613d5eb9 PK |
682 | } |
683 | ||
684 | return 0; | |
685 | } | |
686 | ||
20858f6e | 687 | void git_push_status_free(push_status *status) |
688 | { | |
689 | if (status == NULL) | |
690 | return; | |
691 | ||
be6996b7 | 692 | git__free(status->msg); |
20858f6e | 693 | git__free(status->ref); |
694 | git__free(status); | |
695 | } | |
696 | ||
613d5eb9 PK |
697 | void git_push_free(git_push *push) |
698 | { | |
699 | push_spec *spec; | |
700 | push_status *status; | |
701 | unsigned int i; | |
702 | ||
703 | if (push == NULL) | |
704 | return; | |
705 | ||
706 | git_vector_foreach(&push->specs, i, spec) { | |
707 | free_refspec(spec); | |
708 | } | |
709 | git_vector_free(&push->specs); | |
710 | ||
711 | git_vector_foreach(&push->status, i, status) { | |
20858f6e | 712 | git_push_status_free(status); |
613d5eb9 PK |
713 | } |
714 | git_vector_free(&push->status); | |
715 | ||
716 | git__free(push); | |
717 | } | |
b9f81997 | 718 | |
702efc89 | 719 | int git_push_init_options(git_push_options *opts, unsigned int version) |
b9f81997 | 720 | { |
702efc89 RB |
721 | GIT_INIT_STRUCTURE_FROM_TEMPLATE( |
722 | opts, version, git_push_options, GIT_PUSH_OPTIONS_INIT); | |
723 | return 0; | |
b9f81997 | 724 | } |