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