]> git.proxmox.com Git - libgit2.git/blame - src/push.c
Fix some incorrect MSVC #ifdef's. Fixes #1305
[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
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"
16
df93a681
PK
17static int push_spec_rref_cmp(const void *a, const void *b)
18{
19 const push_spec *push_spec_a = a, *push_spec_b = b;
20
21 return strcmp(push_spec_a->rref, push_spec_b->rref);
22}
23
24static int push_status_ref_cmp(const void *a, const void *b)
25{
26 const push_status *push_status_a = a, *push_status_b = b;
27
28 return strcmp(push_status_a->ref, push_status_b->ref);
29}
30
613d5eb9
PK
31int git_push_new(git_push **out, git_remote *remote)
32{
33 git_push *p;
34
35 *out = NULL;
36
37 p = git__calloc(1, sizeof(*p));
38 GITERR_CHECK_ALLOC(p);
39
40 p->repo = remote->repo;
41 p->remote = remote;
42 p->report_status = 1;
43
df93a681 44 if (git_vector_init(&p->specs, 0, push_spec_rref_cmp) < 0) {
613d5eb9
PK
45 git__free(p);
46 return -1;
47 }
48
df93a681 49 if (git_vector_init(&p->status, 0, push_status_ref_cmp) < 0) {
613d5eb9
PK
50 git_vector_free(&p->specs);
51 git__free(p);
52 return -1;
53 }
54
55 *out = p;
56 return 0;
57}
58
59static void free_refspec(push_spec *spec)
60{
61 if (spec == NULL)
62 return;
63
64 if (spec->lref)
65 git__free(spec->lref);
66
67 if (spec->rref)
68 git__free(spec->rref);
69
70 git__free(spec);
71}
72
73static void free_status(push_status *status)
74{
75 if (status == NULL)
76 return;
77
78 if (status->msg)
79 git__free(status->msg);
80
81 git__free(status->ref);
82 git__free(status);
83}
84
087f64d3 85static int check_rref(char *ref)
613d5eb9 86{
087f64d3
JM
87 if (git__prefixcmp(ref, "refs/")) {
88 giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref);
613d5eb9
PK
89 return -1;
90 }
087f64d3 91
613d5eb9
PK
92 return 0;
93}
94
087f64d3
JM
95static int check_lref(git_push *push, char *ref)
96{
97 /* lref must be resolvable to an existing object */
98 git_object *obj;
99 int error = git_revparse_single(&obj, push->repo, ref);
100
101 if (error) {
abeefbbe
MS
102 if (error == GIT_ENOTFOUND)
103 giterr_set(GITERR_REFERENCE,
104 "src refspec '%s' does not match any existing object", ref);
087f64d3
JM
105 else
106 giterr_set(GITERR_INVALID, "Not a valid reference '%s'", ref);
107
108 return -1;
abeefbbe 109 } else
087f64d3 110 git_object_free(obj);
087f64d3
JM
111
112 return 0;
113}
114
115static int parse_refspec(git_push *push, push_spec **spec, const char *str)
613d5eb9
PK
116{
117 push_spec *s;
118 char *delim;
119
120 *spec = NULL;
121
122 s = git__calloc(1, sizeof(*s));
123 GITERR_CHECK_ALLOC(s);
124
125 if (str[0] == '+') {
126 s->force = true;
127 str++;
128 }
129
613d5eb9
PK
130 delim = strchr(str, ':');
131 if (delim == NULL) {
132 s->lref = git__strdup(str);
087f64d3
JM
133 if (!s->lref || check_lref(push, s->lref) < 0)
134 goto on_error;
613d5eb9
PK
135 } else {
136 if (delim - str) {
137 s->lref = git__strndup(str, delim - str);
087f64d3
JM
138 if (!s->lref || check_lref(push, s->lref) < 0)
139 goto on_error;
4128f5aa 140 }
613d5eb9
PK
141
142 if (strlen(delim + 1)) {
143 s->rref = git__strdup(delim + 1);
087f64d3
JM
144 if (!s->rref || check_rref(s->rref) < 0)
145 goto on_error;
4128f5aa 146 }
613d5eb9
PK
147 }
148
149 if (!s->lref && !s->rref)
150 goto on_error;
151
4128f5aa
CW
152 /* If rref is ommitted, use the same ref name as lref */
153 if (!s->rref) {
154 s->rref = git__strdup(s->lref);
abeefbbe
MS
155 if (!s->rref || check_rref(s->rref) < 0)
156 goto on_error;
4128f5aa
CW
157 }
158
613d5eb9
PK
159 *spec = s;
160 return 0;
161
162on_error:
163 free_refspec(s);
164 return -1;
165}
166
167int git_push_add_refspec(git_push *push, const char *refspec)
168{
169 push_spec *spec;
170
087f64d3 171 if (parse_refspec(push, &spec, refspec) < 0 ||
613d5eb9
PK
172 git_vector_insert(&push->specs, spec) < 0)
173 return -1;
174
175 return 0;
176}
177
1d645aab
JM
178int git_push_update_tips(git_push *push)
179{
180 git_refspec *fetch_spec = &push->remote->fetch;
181 git_buf remote_ref_name = GIT_BUF_INIT;
182 size_t i, j;
183 push_spec *push_spec;
184 git_reference *remote_ref;
185 push_status *status;
186 int error = 0;
187
188 git_vector_foreach(&push->status, i, status) {
189 /* If this ref update was successful (ok, not ng), it will have an empty message */
190 if (status->msg)
191 continue;
192
193 /* Find the corresponding remote ref */
194 if (!git_refspec_src_matches(fetch_spec, status->ref))
195 continue;
196
197 if ((error = git_refspec_transform_r(&remote_ref_name, fetch_spec, status->ref)) < 0)
198 goto on_error;
199
200 /* Find matching push ref spec */
201 git_vector_foreach(&push->specs, j, push_spec) {
202 if (!strcmp(push_spec->rref, status->ref))
203 break;
204 }
205
206 /* Could not find the corresponding push ref spec for this push update */
207 if (j == push->specs.length)
208 continue;
209
210 /* Update the remote ref */
211 if (git_oid_iszero(&push_spec->loid)) {
212 error = git_reference_lookup(&remote_ref, push->remote->repo, git_buf_cstr(&remote_ref_name));
213
214 if (!error) {
215 if ((error = git_reference_delete(remote_ref)) < 0)
216 goto on_error;
217 } else if (error == GIT_ENOTFOUND)
218 giterr_clear();
219 else
220 goto on_error;
221 } else if ((error = git_reference_create(NULL, push->remote->repo, git_buf_cstr(&remote_ref_name), &push_spec->loid, 1)) < 0)
222 goto on_error;
223 }
224
225 error = 0;
226
227on_error:
228 git_buf_free(&remote_ref_name);
229 return error;
230}
231
613d5eb9
PK
232static int revwalk(git_vector *commits, git_push *push)
233{
234 git_remote_head *head;
235 push_spec *spec;
236 git_revwalk *rw;
237 git_oid oid;
238 unsigned int i;
239 int error = -1;
240
241 if (git_revwalk_new(&rw, push->repo) < 0)
242 return -1;
243
244 git_revwalk_sorting(rw, GIT_SORT_TIME);
245
246 git_vector_foreach(&push->specs, i, spec) {
abeefbbe
MS
247 git_otype type;
248 size_t size;
249
613d5eb9
PK
250 if (git_oid_iszero(&spec->loid))
251 /*
252 * Delete reference on remote side;
253 * nothing to do here.
254 */
255 continue;
256
257 if (git_oid_equal(&spec->loid, &spec->roid))
258 continue; /* up-to-date */
259
abeefbbe
MS
260 if (git_odb_read_header(&size, &type, push->repo->_odb, &spec->loid) < 0)
261 goto on_error;
262
263 if (type == GIT_OBJ_TAG) {
264 git_tag *tag;
265 git_object *target;
266
267 if (git_packbuilder_insert(push->pb, &spec->loid, NULL) < 0)
268 goto on_error;
269
270 if (git_tag_lookup(&tag, push->repo, &spec->loid) < 0)
271 goto on_error;
272
273 if (git_tag_peel(&target, tag) < 0) {
274 git_tag_free(tag);
275 goto on_error;
276 }
277 git_tag_free(tag);
278
279 if (git_object_type(target) == GIT_OBJ_COMMIT) {
280 if (git_revwalk_push(rw, git_object_id(target)) < 0) {
281 git_object_free(target);
282 goto on_error;
283 }
284 } else {
285 if (git_packbuilder_insert(
286 push->pb, git_object_id(target), NULL) < 0) {
287 git_object_free(target);
288 goto on_error;
289 }
290 }
291 git_object_free(target);
292 } else if (git_revwalk_push(rw, &spec->loid) < 0)
613d5eb9
PK
293 goto on_error;
294
295 if (!spec->force) {
296 git_oid base;
297
298 if (git_oid_iszero(&spec->roid))
299 continue;
300
301 if (!git_odb_exists(push->repo->_odb, &spec->roid)) {
302 giterr_clear();
303 error = GIT_ENONFASTFORWARD;
304 goto on_error;
305 }
306
307 error = git_merge_base(&base, push->repo,
308 &spec->loid, &spec->roid);
309
310 if (error == GIT_ENOTFOUND ||
311 (!error && !git_oid_equal(&base, &spec->roid))) {
312 giterr_clear();
313 error = GIT_ENONFASTFORWARD;
314 goto on_error;
315 }
316
317 if (error < 0)
318 goto on_error;
319 }
320 }
321
322 git_vector_foreach(&push->remote->refs, i, head) {
323 if (git_oid_iszero(&head->oid))
324 continue;
325
326 /* TODO */
327 git_revwalk_hide(rw, &head->oid);
328 }
329
330 while ((error = git_revwalk_next(&oid, rw)) == 0) {
331 git_oid *o = git__malloc(GIT_OID_RAWSZ);
332 GITERR_CHECK_ALLOC(o);
333 git_oid_cpy(o, &oid);
334 if (git_vector_insert(commits, o) < 0) {
335 error = -1;
336 goto on_error;
337 }
338 }
339
340on_error:
341 git_revwalk_free(rw);
342 return error == GIT_ITEROVER ? 0 : error;
343}
344
345static int queue_objects(git_push *push)
346{
347 git_vector commits;
348 git_oid *o;
349 unsigned int i;
350 int error;
351
352 if (git_vector_init(&commits, 0, NULL) < 0)
353 return -1;
354
355 if ((error = revwalk(&commits, push)) < 0)
356 goto on_error;
357
358 if (!commits.length) {
359 git_vector_free(&commits);
360 return 0; /* nothing to do */
361 }
362
363 git_vector_foreach(&commits, i, o) {
364 if ((error = git_packbuilder_insert(push->pb, o, NULL)) < 0)
365 goto on_error;
366 }
367
368 git_vector_foreach(&commits, i, o) {
369 git_object *obj;
370
371 if ((error = git_object_lookup(&obj, push->repo, o, GIT_OBJ_ANY)) < 0)
372 goto on_error;
373
374 switch (git_object_type(obj)) {
375 case GIT_OBJ_TAG: /* TODO: expect tags */
376 case GIT_OBJ_COMMIT:
377 if ((error = git_packbuilder_insert_tree(push->pb,
378 git_commit_tree_id((git_commit *)obj))) < 0) {
379 git_object_free(obj);
380 goto on_error;
381 }
382 break;
383 case GIT_OBJ_TREE:
384 case GIT_OBJ_BLOB:
385 default:
386 git_object_free(obj);
387 giterr_set(GITERR_INVALID, "Given object type invalid");
388 error = -1;
389 goto on_error;
390 }
391 git_object_free(obj);
392 }
393 error = 0;
394
395on_error:
396 git_vector_foreach(&commits, i, o) {
397 git__free(o);
398 }
399 git_vector_free(&commits);
400 return error;
401}
402
403static int calculate_work(git_push *push)
404{
405 git_remote_head *head;
406 push_spec *spec;
407 unsigned int i, j;
408
4128f5aa
CW
409 /* Update local and remote oids*/
410
613d5eb9
PK
411 git_vector_foreach(&push->specs, i, spec) {
412 if (spec->lref) {
4128f5aa 413 /* This is a create or update. Local ref must exist. */
613d5eb9
PK
414 if (git_reference_name_to_id(
415 &spec->loid, push->repo, spec->lref) < 0) {
416 giterr_set(GIT_ENOTFOUND, "No such reference '%s'", spec->lref);
417 return -1;
418 }
4128f5aa 419 }
613d5eb9 420
4128f5aa
CW
421 if (spec->rref) {
422 /* Remote ref may or may not (e.g. during create) already exist. */
423 git_vector_foreach(&push->remote->refs, j, head) {
424 if (!strcmp(spec->rref, head->name)) {
425 git_oid_cpy(&spec->roid, &head->oid);
426 break;
613d5eb9
PK
427 }
428 }
429 }
430 }
431
432 return 0;
433}
434
435static int do_push(git_push *push)
436{
437 int error;
438 git_transport *transport = push->remote->transport;
439
9bf56c7b
SB
440 if (!transport->push) {
441 giterr_set(GITERR_NET, "Remote transport doesn't support push");
442 error = -1;
443 goto on_error;
444 }
445
613d5eb9
PK
446 /*
447 * A pack-file MUST be sent if either create or update command
448 * is used, even if the server already has all the necessary
449 * objects. In this case the client MUST send an empty pack-file.
450 */
451
452 if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0 ||
453 (error = calculate_work(push)) < 0 ||
454 (error = queue_objects(push)) < 0 ||
455 (error = transport->push(transport, push)) < 0)
456 goto on_error;
457
458 error = 0;
459
460on_error:
461 git_packbuilder_free(push->pb);
462 return error;
463}
464
465static int cb_filter_refs(git_remote_head *ref, void *data)
466{
467 git_remote *remote = (git_remote *) data;
468 return git_vector_insert(&remote->refs, ref);
469}
470
471static int filter_refs(git_remote *remote)
472{
473 git_vector_clear(&remote->refs);
474 return git_remote_ls(remote, cb_filter_refs, remote);
475}
476
477int git_push_finish(git_push *push)
478{
479 int error;
480
481 if (!git_remote_connected(push->remote) &&
482 (error = git_remote_connect(push->remote, GIT_DIRECTION_PUSH)) < 0)
483 return error;
484
485 if ((error = filter_refs(push->remote)) < 0 ||
486 (error = do_push(push)) < 0)
487 return error;
488
489 return 0;
490}
491
492int git_push_unpack_ok(git_push *push)
493{
494 return push->unpack_ok;
495}
496
497int git_push_status_foreach(git_push *push,
498 int (*cb)(const char *ref, const char *msg, void *data),
499 void *data)
500{
501 push_status *status;
502 unsigned int i;
503
504 git_vector_foreach(&push->status, i, status) {
505 if (cb(status->ref, status->msg, data) < 0)
506 return GIT_EUSER;
507 }
508
509 return 0;
510}
511
512void git_push_free(git_push *push)
513{
514 push_spec *spec;
515 push_status *status;
516 unsigned int i;
517
518 if (push == NULL)
519 return;
520
521 git_vector_foreach(&push->specs, i, spec) {
522 free_refspec(spec);
523 }
524 git_vector_free(&push->specs);
525
526 git_vector_foreach(&push->status, i, status) {
527 free_status(status);
528 }
529 git_vector_free(&push->status);
530
531 git__free(push);
532}