]>
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 | ||
1d645aab JM |
197 | int git_push_update_tips(git_push *push) |
198 | { | |
1d645aab JM |
199 | git_buf remote_ref_name = GIT_BUF_INIT; |
200 | size_t i, j; | |
4330ab26 | 201 | git_refspec *fetch_spec; |
e583334c | 202 | push_spec *push_spec = NULL; |
1d645aab JM |
203 | git_reference *remote_ref; |
204 | push_status *status; | |
205 | int error = 0; | |
206 | ||
207 | git_vector_foreach(&push->status, i, status) { | |
208 | /* If this ref update was successful (ok, not ng), it will have an empty message */ | |
209 | if (status->msg) | |
210 | continue; | |
211 | ||
212 | /* Find the corresponding remote ref */ | |
4330ab26 CMN |
213 | fetch_spec = git_remote__matching_refspec(push->remote, status->ref); |
214 | if (!fetch_spec) | |
1d645aab JM |
215 | continue; |
216 | ||
217 | if ((error = git_refspec_transform_r(&remote_ref_name, fetch_spec, status->ref)) < 0) | |
218 | goto on_error; | |
219 | ||
220 | /* Find matching push ref spec */ | |
221 | git_vector_foreach(&push->specs, j, push_spec) { | |
222 | if (!strcmp(push_spec->rref, status->ref)) | |
223 | break; | |
224 | } | |
225 | ||
226 | /* Could not find the corresponding push ref spec for this push update */ | |
227 | if (j == push->specs.length) | |
228 | continue; | |
229 | ||
230 | /* Update the remote ref */ | |
231 | if (git_oid_iszero(&push_spec->loid)) { | |
232 | error = git_reference_lookup(&remote_ref, push->remote->repo, git_buf_cstr(&remote_ref_name)); | |
233 | ||
234 | if (!error) { | |
20858f6e | 235 | if ((error = git_reference_delete(remote_ref)) < 0) { |
236 | git_reference_free(remote_ref); | |
1d645aab | 237 | goto on_error; |
20858f6e | 238 | } |
239 | git_reference_free(remote_ref); | |
1d645aab JM |
240 | } else if (error == GIT_ENOTFOUND) |
241 | giterr_clear(); | |
242 | else | |
243 | goto on_error; | |
244 | } else if ((error = git_reference_create(NULL, push->remote->repo, git_buf_cstr(&remote_ref_name), &push_spec->loid, 1)) < 0) | |
245 | goto on_error; | |
246 | } | |
247 | ||
248 | error = 0; | |
249 | ||
250 | on_error: | |
251 | git_buf_free(&remote_ref_name); | |
252 | return error; | |
253 | } | |
254 | ||
5ce6c1e9 CMN |
255 | /** |
256 | * Insert all tags until we find a non-tag object, which is returned | |
257 | * in `out`. | |
258 | */ | |
259 | static int enqueue_tag(git_object **out, git_push *push, git_oid *id) | |
260 | { | |
261 | git_object *obj = NULL, *target = NULL; | |
262 | int error; | |
263 | ||
264 | if ((error = git_object_lookup(&obj, push->repo, id, GIT_OBJ_TAG)) < 0) | |
265 | return error; | |
266 | ||
267 | while (git_object_type(obj) == GIT_OBJ_TAG) { | |
268 | if ((error = git_packbuilder_insert(push->pb, git_object_id(obj), NULL)) < 0) | |
269 | break; | |
270 | ||
271 | if ((error = git_tag_target(&target, (git_tag *) obj)) < 0) | |
272 | break; | |
273 | ||
274 | git_object_free(obj); | |
275 | obj = target; | |
276 | } | |
277 | ||
278 | if (error < 0) | |
279 | git_object_free(obj); | |
280 | else | |
281 | *out = obj; | |
282 | ||
283 | return error; | |
284 | } | |
285 | ||
613d5eb9 PK |
286 | static int revwalk(git_vector *commits, git_push *push) |
287 | { | |
288 | git_remote_head *head; | |
289 | push_spec *spec; | |
290 | git_revwalk *rw; | |
291 | git_oid oid; | |
292 | unsigned int i; | |
293 | int error = -1; | |
294 | ||
295 | if (git_revwalk_new(&rw, push->repo) < 0) | |
296 | return -1; | |
297 | ||
298 | git_revwalk_sorting(rw, GIT_SORT_TIME); | |
299 | ||
300 | git_vector_foreach(&push->specs, i, spec) { | |
abeefbbe MS |
301 | git_otype type; |
302 | size_t size; | |
303 | ||
613d5eb9 PK |
304 | if (git_oid_iszero(&spec->loid)) |
305 | /* | |
306 | * Delete reference on remote side; | |
307 | * nothing to do here. | |
308 | */ | |
309 | continue; | |
310 | ||
311 | if (git_oid_equal(&spec->loid, &spec->roid)) | |
312 | continue; /* up-to-date */ | |
313 | ||
abeefbbe MS |
314 | if (git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid) < 0) |
315 | goto on_error; | |
316 | ||
317 | if (type == GIT_OBJ_TAG) { | |
abeefbbe MS |
318 | git_object *target; |
319 | ||
5ce6c1e9 | 320 | if ((error = enqueue_tag(&target, push, &spec->loid)) < 0) |
abeefbbe | 321 | goto on_error; |
abeefbbe MS |
322 | |
323 | if (git_object_type(target) == GIT_OBJ_COMMIT) { | |
324 | if (git_revwalk_push(rw, git_object_id(target)) < 0) { | |
325 | git_object_free(target); | |
326 | goto on_error; | |
327 | } | |
328 | } else { | |
329 | if (git_packbuilder_insert( | |
330 | push->pb, git_object_id(target), NULL) < 0) { | |
331 | git_object_free(target); | |
332 | goto on_error; | |
333 | } | |
334 | } | |
335 | git_object_free(target); | |
336 | } else if (git_revwalk_push(rw, &spec->loid) < 0) | |
613d5eb9 PK |
337 | goto on_error; |
338 | ||
339 | if (!spec->force) { | |
340 | git_oid base; | |
341 | ||
342 | if (git_oid_iszero(&spec->roid)) | |
343 | continue; | |
344 | ||
345 | if (!git_odb_exists(push->repo->_odb, &spec->roid)) { | |
62caf3f3 | 346 | giterr_set(GITERR_REFERENCE, "Cannot push missing reference"); |
613d5eb9 PK |
347 | error = GIT_ENONFASTFORWARD; |
348 | goto on_error; | |
349 | } | |
350 | ||
351 | error = git_merge_base(&base, push->repo, | |
352 | &spec->loid, &spec->roid); | |
353 | ||
354 | if (error == GIT_ENOTFOUND || | |
355 | (!error && !git_oid_equal(&base, &spec->roid))) { | |
62caf3f3 RB |
356 | giterr_set(GITERR_REFERENCE, |
357 | "Cannot push non-fastforwardable reference"); | |
613d5eb9 PK |
358 | error = GIT_ENONFASTFORWARD; |
359 | goto on_error; | |
360 | } | |
361 | ||
362 | if (error < 0) | |
363 | goto on_error; | |
364 | } | |
365 | } | |
366 | ||
367 | git_vector_foreach(&push->remote->refs, i, head) { | |
368 | if (git_oid_iszero(&head->oid)) | |
369 | continue; | |
370 | ||
371 | /* TODO */ | |
372 | git_revwalk_hide(rw, &head->oid); | |
373 | } | |
374 | ||
375 | while ((error = git_revwalk_next(&oid, rw)) == 0) { | |
376 | git_oid *o = git__malloc(GIT_OID_RAWSZ); | |
62caf3f3 | 377 | if (!o) { |
613d5eb9 PK |
378 | error = -1; |
379 | goto on_error; | |
380 | } | |
62caf3f3 RB |
381 | git_oid_cpy(o, &oid); |
382 | if ((error = git_vector_insert(commits, o)) < 0) | |
383 | goto on_error; | |
613d5eb9 PK |
384 | } |
385 | ||
386 | on_error: | |
387 | git_revwalk_free(rw); | |
388 | return error == GIT_ITEROVER ? 0 : error; | |
389 | } | |
390 | ||
bef2a12c PK |
391 | static int enqueue_object( |
392 | const git_tree_entry *entry, | |
393 | git_packbuilder *pb) | |
394 | { | |
395 | switch (git_tree_entry_type(entry)) { | |
396 | case GIT_OBJ_COMMIT: | |
397 | return 0; | |
398 | case GIT_OBJ_TREE: | |
399 | return git_packbuilder_insert_tree(pb, &entry->oid); | |
400 | default: | |
401 | return git_packbuilder_insert(pb, &entry->oid, entry->filename); | |
402 | } | |
403 | } | |
404 | ||
799f9a04 PK |
405 | static int queue_differences( |
406 | git_tree *base, | |
407 | git_tree *delta, | |
408 | git_packbuilder *pb) | |
613d5eb9 | 409 | { |
799f9a04 PK |
410 | git_tree *b_child = NULL, *d_child = NULL; |
411 | size_t b_length = git_tree_entrycount(base); | |
412 | size_t d_length = git_tree_entrycount(delta); | |
413 | size_t i = 0, j = 0; | |
613d5eb9 PK |
414 | int error; |
415 | ||
799f9a04 PK |
416 | while (i < b_length && j < d_length) { |
417 | const git_tree_entry *b_entry = git_tree_entry_byindex(base, i); | |
418 | const git_tree_entry *d_entry = git_tree_entry_byindex(delta, j); | |
419 | int cmp = 0; | |
420 | ||
b7f167da | 421 | if (!git_oid__cmp(&b_entry->oid, &d_entry->oid)) |
799f9a04 PK |
422 | goto loop; |
423 | ||
cd01dd5d | 424 | cmp = strcmp(b_entry->filename, d_entry->filename); |
799f9a04 PK |
425 | |
426 | /* If the entries are both trees and they have the same name but are | |
427 | * different, then we'll recurse after adding the right-hand entry */ | |
428 | if (!cmp && | |
429 | git_tree_entry__is_tree(b_entry) && | |
430 | git_tree_entry__is_tree(d_entry)) { | |
431 | /* Add the right-hand entry */ | |
432 | if ((error = git_packbuilder_insert(pb, &d_entry->oid, | |
433 | d_entry->filename)) < 0) | |
434 | goto on_error; | |
435 | ||
436 | /* Acquire the subtrees and recurse */ | |
437 | if ((error = git_tree_lookup(&b_child, | |
438 | git_tree_owner(base), &b_entry->oid)) < 0 || | |
439 | (error = git_tree_lookup(&d_child, | |
440 | git_tree_owner(delta), &d_entry->oid)) < 0 || | |
441 | (error = queue_differences(b_child, d_child, pb)) < 0) | |
442 | goto on_error; | |
443 | ||
444 | git_tree_free(b_child); b_child = NULL; | |
445 | git_tree_free(d_child); d_child = NULL; | |
446 | } | |
447 | /* If the object is new or different in the right-hand tree, | |
448 | * then enumerate it */ | |
bef2a12c PK |
449 | else if (cmp >= 0 && |
450 | (error = enqueue_object(d_entry, pb)) < 0) | |
451 | goto on_error; | |
799f9a04 PK |
452 | |
453 | loop: | |
454 | if (cmp <= 0) i++; | |
455 | if (cmp >= 0) j++; | |
456 | } | |
457 | ||
458 | /* Drain the right-hand tree of entries */ | |
459 | for (; j < d_length; j++) | |
bef2a12c PK |
460 | if ((error = enqueue_object(git_tree_entry_byindex(delta, j), pb)) < 0) |
461 | goto on_error; | |
799f9a04 PK |
462 | |
463 | error = 0; | |
464 | ||
465 | on_error: | |
466 | if (b_child) | |
467 | git_tree_free(b_child); | |
468 | ||
469 | if (d_child) | |
470 | git_tree_free(d_child); | |
471 | ||
472 | return error; | |
473 | } | |
474 | ||
475 | static int queue_objects(git_push *push) | |
476 | { | |
477 | git_vector commits = GIT_VECTOR_INIT; | |
478 | git_oid *oid; | |
479 | size_t i; | |
480 | unsigned j; | |
481 | int error; | |
613d5eb9 PK |
482 | |
483 | if ((error = revwalk(&commits, push)) < 0) | |
484 | goto on_error; | |
485 | ||
799f9a04 PK |
486 | git_vector_foreach(&commits, i, oid) { |
487 | git_commit *parent = NULL, *commit; | |
488 | git_tree *tree = NULL, *ptree = NULL; | |
489 | size_t parentcount; | |
613d5eb9 | 490 | |
799f9a04 | 491 | if ((error = git_commit_lookup(&commit, push->repo, oid)) < 0) |
613d5eb9 | 492 | goto on_error; |
613d5eb9 | 493 | |
799f9a04 PK |
494 | /* Insert the commit */ |
495 | if ((error = git_packbuilder_insert(push->pb, oid, NULL)) < 0) | |
496 | goto loop_error; | |
613d5eb9 | 497 | |
799f9a04 | 498 | parentcount = git_commit_parentcount(commit); |
613d5eb9 | 499 | |
799f9a04 | 500 | if (!parentcount) { |
613d5eb9 | 501 | if ((error = git_packbuilder_insert_tree(push->pb, |
799f9a04 PK |
502 | git_commit_tree_id(commit))) < 0) |
503 | goto loop_error; | |
504 | } else { | |
505 | if ((error = git_tree_lookup(&tree, push->repo, | |
506 | git_commit_tree_id(commit))) < 0 || | |
507 | (error = git_packbuilder_insert(push->pb, | |
508 | git_commit_tree_id(commit), NULL)) < 0) | |
509 | goto loop_error; | |
510 | ||
511 | /* For each parent, add the items which are different */ | |
512 | for (j = 0; j < parentcount; j++) { | |
513 | if ((error = git_commit_parent(&parent, commit, j)) < 0 || | |
514 | (error = git_commit_tree(&ptree, parent)) < 0 || | |
515 | (error = queue_differences(ptree, tree, push->pb)) < 0) | |
516 | goto loop_error; | |
517 | ||
518 | git_tree_free(ptree); ptree = NULL; | |
519 | git_commit_free(parent); parent = NULL; | |
613d5eb9 | 520 | } |
613d5eb9 | 521 | } |
799f9a04 PK |
522 | |
523 | error = 0; | |
524 | ||
525 | loop_error: | |
526 | if (tree) | |
527 | git_tree_free(tree); | |
528 | ||
529 | if (ptree) | |
530 | git_tree_free(ptree); | |
531 | ||
532 | if (parent) | |
533 | git_commit_free(parent); | |
534 | ||
535 | git_commit_free(commit); | |
536 | ||
537 | if (error < 0) | |
538 | goto on_error; | |
613d5eb9 | 539 | } |
799f9a04 | 540 | |
613d5eb9 PK |
541 | error = 0; |
542 | ||
543 | on_error: | |
799f9a04 PK |
544 | git_vector_foreach(&commits, i, oid) |
545 | git__free(oid); | |
546 | ||
613d5eb9 PK |
547 | git_vector_free(&commits); |
548 | return error; | |
549 | } | |
550 | ||
551 | static int calculate_work(git_push *push) | |
552 | { | |
553 | git_remote_head *head; | |
554 | push_spec *spec; | |
555 | unsigned int i, j; | |
556 | ||
4128f5aa CW |
557 | /* Update local and remote oids*/ |
558 | ||
613d5eb9 PK |
559 | git_vector_foreach(&push->specs, i, spec) { |
560 | if (spec->lref) { | |
4128f5aa | 561 | /* This is a create or update. Local ref must exist. */ |
613d5eb9 PK |
562 | if (git_reference_name_to_id( |
563 | &spec->loid, push->repo, spec->lref) < 0) { | |
62caf3f3 | 564 | giterr_set(GITERR_REFERENCE, "No such reference '%s'", spec->lref); |
613d5eb9 PK |
565 | return -1; |
566 | } | |
4128f5aa | 567 | } |
613d5eb9 | 568 | |
4128f5aa CW |
569 | if (spec->rref) { |
570 | /* Remote ref may or may not (e.g. during create) already exist. */ | |
571 | git_vector_foreach(&push->remote->refs, j, head) { | |
572 | if (!strcmp(spec->rref, head->name)) { | |
573 | git_oid_cpy(&spec->roid, &head->oid); | |
574 | break; | |
613d5eb9 PK |
575 | } |
576 | } | |
577 | } | |
578 | } | |
579 | ||
580 | return 0; | |
581 | } | |
582 | ||
583 | static int do_push(git_push *push) | |
584 | { | |
5b188225 | 585 | int error = 0; |
613d5eb9 PK |
586 | git_transport *transport = push->remote->transport; |
587 | ||
9bf56c7b SB |
588 | if (!transport->push) { |
589 | giterr_set(GITERR_NET, "Remote transport doesn't support push"); | |
590 | error = -1; | |
591 | goto on_error; | |
592 | } | |
593 | ||
613d5eb9 PK |
594 | /* |
595 | * A pack-file MUST be sent if either create or update command | |
596 | * is used, even if the server already has all the necessary | |
597 | * objects. In this case the client MUST send an empty pack-file. | |
598 | */ | |
599 | ||
b8b897bb PK |
600 | if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0) |
601 | goto on_error; | |
602 | ||
603 | git_packbuilder_set_threads(push->pb, push->pb_parallelism); | |
604 | ||
b176eded JM |
605 | if (push->pack_progress_cb) |
606 | if ((error = git_packbuilder_set_callbacks(push->pb, push->pack_progress_cb, push->pack_progress_cb_payload)) < 0) | |
607 | goto on_error; | |
608 | ||
b8b897bb | 609 | if ((error = calculate_work(push)) < 0 || |
613d5eb9 PK |
610 | (error = queue_objects(push)) < 0 || |
611 | (error = transport->push(transport, push)) < 0) | |
612 | goto on_error; | |
613 | ||
613d5eb9 PK |
614 | on_error: |
615 | git_packbuilder_free(push->pb); | |
616 | return error; | |
617 | } | |
618 | ||
619 | static int cb_filter_refs(git_remote_head *ref, void *data) | |
620 | { | |
621 | git_remote *remote = (git_remote *) data; | |
622 | return git_vector_insert(&remote->refs, ref); | |
623 | } | |
624 | ||
625 | static int filter_refs(git_remote *remote) | |
626 | { | |
627 | git_vector_clear(&remote->refs); | |
628 | return git_remote_ls(remote, cb_filter_refs, remote); | |
629 | } | |
630 | ||
631 | int git_push_finish(git_push *push) | |
632 | { | |
633 | int error; | |
634 | ||
635 | if (!git_remote_connected(push->remote) && | |
636 | (error = git_remote_connect(push->remote, GIT_DIRECTION_PUSH)) < 0) | |
637 | return error; | |
638 | ||
639 | if ((error = filter_refs(push->remote)) < 0 || | |
640 | (error = do_push(push)) < 0) | |
641 | return error; | |
642 | ||
643 | return 0; | |
644 | } | |
645 | ||
646 | int git_push_unpack_ok(git_push *push) | |
647 | { | |
648 | return push->unpack_ok; | |
649 | } | |
650 | ||
651 | int git_push_status_foreach(git_push *push, | |
652 | int (*cb)(const char *ref, const char *msg, void *data), | |
653 | void *data) | |
654 | { | |
655 | push_status *status; | |
656 | unsigned int i; | |
657 | ||
658 | git_vector_foreach(&push->status, i, status) { | |
659 | if (cb(status->ref, status->msg, data) < 0) | |
660 | return GIT_EUSER; | |
661 | } | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
20858f6e | 666 | void git_push_status_free(push_status *status) |
667 | { | |
668 | if (status == NULL) | |
669 | return; | |
670 | ||
671 | if (status->msg) | |
672 | git__free(status->msg); | |
673 | ||
674 | git__free(status->ref); | |
675 | git__free(status); | |
676 | } | |
677 | ||
613d5eb9 PK |
678 | void git_push_free(git_push *push) |
679 | { | |
680 | push_spec *spec; | |
681 | push_status *status; | |
682 | unsigned int i; | |
683 | ||
684 | if (push == NULL) | |
685 | return; | |
686 | ||
687 | git_vector_foreach(&push->specs, i, spec) { | |
688 | free_refspec(spec); | |
689 | } | |
690 | git_vector_free(&push->specs); | |
691 | ||
692 | git_vector_foreach(&push->status, i, status) { | |
20858f6e | 693 | git_push_status_free(status); |
613d5eb9 PK |
694 | } |
695 | git_vector_free(&push->status); | |
696 | ||
697 | git__free(push); | |
698 | } |