]>
Commit | Line | Data |
---|---|---|
613d5eb9 PK |
1 | /* |
2 | * Copyright (C) 2009-2012 the libgit2 contributors | |
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 | ||
17 | int git_push_new(git_push **out, git_remote *remote) | |
18 | { | |
19 | git_push *p; | |
20 | ||
21 | *out = NULL; | |
22 | ||
23 | p = git__calloc(1, sizeof(*p)); | |
24 | GITERR_CHECK_ALLOC(p); | |
25 | ||
26 | p->repo = remote->repo; | |
27 | p->remote = remote; | |
28 | p->report_status = 1; | |
29 | ||
30 | if (git_vector_init(&p->specs, 0, NULL) < 0) { | |
31 | git__free(p); | |
32 | return -1; | |
33 | } | |
34 | ||
35 | if (git_vector_init(&p->status, 0, NULL) < 0) { | |
36 | git_vector_free(&p->specs); | |
37 | git__free(p); | |
38 | return -1; | |
39 | } | |
40 | ||
41 | *out = p; | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static void free_refspec(push_spec *spec) | |
46 | { | |
47 | if (spec == NULL) | |
48 | return; | |
49 | ||
50 | if (spec->lref) | |
51 | git__free(spec->lref); | |
52 | ||
53 | if (spec->rref) | |
54 | git__free(spec->rref); | |
55 | ||
56 | git__free(spec); | |
57 | } | |
58 | ||
59 | static void free_status(push_status *status) | |
60 | { | |
61 | if (status == NULL) | |
62 | return; | |
63 | ||
64 | if (status->msg) | |
65 | git__free(status->msg); | |
66 | ||
67 | git__free(status->ref); | |
68 | git__free(status); | |
69 | } | |
70 | ||
71 | static int check_ref(char *ref) | |
72 | { | |
73 | if (strcmp(ref, "HEAD") && | |
74 | git__prefixcmp(ref, "refs/heads/") && | |
75 | git__prefixcmp(ref, "refs/tags/")) { | |
76 | giterr_set(GITERR_INVALID, "No valid reference '%s'", ref); | |
77 | return -1; | |
78 | } | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static int parse_refspec(push_spec **spec, const char *str) | |
83 | { | |
84 | push_spec *s; | |
85 | char *delim; | |
86 | ||
87 | *spec = NULL; | |
88 | ||
89 | s = git__calloc(1, sizeof(*s)); | |
90 | GITERR_CHECK_ALLOC(s); | |
91 | ||
92 | if (str[0] == '+') { | |
93 | s->force = true; | |
94 | str++; | |
95 | } | |
96 | ||
97 | #define check(ref) \ | |
98 | if (!ref || check_ref(ref) < 0) goto on_error | |
99 | ||
100 | delim = strchr(str, ':'); | |
101 | if (delim == NULL) { | |
102 | s->lref = git__strdup(str); | |
103 | check(s->lref); | |
613d5eb9 PK |
104 | } else { |
105 | if (delim - str) { | |
106 | s->lref = git__strndup(str, delim - str); | |
107 | check(s->lref); | |
4128f5aa | 108 | } |
613d5eb9 PK |
109 | |
110 | if (strlen(delim + 1)) { | |
111 | s->rref = git__strdup(delim + 1); | |
112 | check(s->rref); | |
4128f5aa | 113 | } |
613d5eb9 PK |
114 | } |
115 | ||
116 | if (!s->lref && !s->rref) | |
117 | goto on_error; | |
118 | ||
4128f5aa CW |
119 | /* If rref is ommitted, use the same ref name as lref */ |
120 | if (!s->rref) { | |
121 | s->rref = git__strdup(s->lref); | |
122 | check(s->rref); | |
123 | } | |
124 | ||
613d5eb9 PK |
125 | #undef check |
126 | ||
127 | *spec = s; | |
128 | return 0; | |
129 | ||
130 | on_error: | |
131 | free_refspec(s); | |
132 | return -1; | |
133 | } | |
134 | ||
135 | int git_push_add_refspec(git_push *push, const char *refspec) | |
136 | { | |
137 | push_spec *spec; | |
138 | ||
139 | if (parse_refspec(&spec, refspec) < 0 || | |
140 | git_vector_insert(&push->specs, spec) < 0) | |
141 | return -1; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static int revwalk(git_vector *commits, git_push *push) | |
147 | { | |
148 | git_remote_head *head; | |
149 | push_spec *spec; | |
150 | git_revwalk *rw; | |
151 | git_oid oid; | |
152 | unsigned int i; | |
153 | int error = -1; | |
154 | ||
155 | if (git_revwalk_new(&rw, push->repo) < 0) | |
156 | return -1; | |
157 | ||
158 | git_revwalk_sorting(rw, GIT_SORT_TIME); | |
159 | ||
160 | git_vector_foreach(&push->specs, i, spec) { | |
161 | if (git_oid_iszero(&spec->loid)) | |
162 | /* | |
163 | * Delete reference on remote side; | |
164 | * nothing to do here. | |
165 | */ | |
166 | continue; | |
167 | ||
168 | if (git_oid_equal(&spec->loid, &spec->roid)) | |
169 | continue; /* up-to-date */ | |
170 | ||
171 | if (git_revwalk_push(rw, &spec->loid) < 0) | |
172 | goto on_error; | |
173 | ||
174 | if (!spec->force) { | |
175 | git_oid base; | |
176 | ||
177 | if (git_oid_iszero(&spec->roid)) | |
178 | continue; | |
179 | ||
180 | if (!git_odb_exists(push->repo->_odb, &spec->roid)) { | |
181 | giterr_clear(); | |
182 | error = GIT_ENONFASTFORWARD; | |
183 | goto on_error; | |
184 | } | |
185 | ||
186 | error = git_merge_base(&base, push->repo, | |
187 | &spec->loid, &spec->roid); | |
188 | ||
189 | if (error == GIT_ENOTFOUND || | |
190 | (!error && !git_oid_equal(&base, &spec->roid))) { | |
191 | giterr_clear(); | |
192 | error = GIT_ENONFASTFORWARD; | |
193 | goto on_error; | |
194 | } | |
195 | ||
196 | if (error < 0) | |
197 | goto on_error; | |
198 | } | |
199 | } | |
200 | ||
201 | git_vector_foreach(&push->remote->refs, i, head) { | |
202 | if (git_oid_iszero(&head->oid)) | |
203 | continue; | |
204 | ||
205 | /* TODO */ | |
206 | git_revwalk_hide(rw, &head->oid); | |
207 | } | |
208 | ||
209 | while ((error = git_revwalk_next(&oid, rw)) == 0) { | |
210 | git_oid *o = git__malloc(GIT_OID_RAWSZ); | |
211 | GITERR_CHECK_ALLOC(o); | |
212 | git_oid_cpy(o, &oid); | |
213 | if (git_vector_insert(commits, o) < 0) { | |
214 | error = -1; | |
215 | goto on_error; | |
216 | } | |
217 | } | |
218 | ||
219 | on_error: | |
220 | git_revwalk_free(rw); | |
221 | return error == GIT_ITEROVER ? 0 : error; | |
222 | } | |
223 | ||
224 | static int queue_objects(git_push *push) | |
225 | { | |
226 | git_vector commits; | |
227 | git_oid *o; | |
228 | unsigned int i; | |
229 | int error; | |
230 | ||
231 | if (git_vector_init(&commits, 0, NULL) < 0) | |
232 | return -1; | |
233 | ||
234 | if ((error = revwalk(&commits, push)) < 0) | |
235 | goto on_error; | |
236 | ||
237 | if (!commits.length) { | |
238 | git_vector_free(&commits); | |
239 | return 0; /* nothing to do */ | |
240 | } | |
241 | ||
242 | git_vector_foreach(&commits, i, o) { | |
243 | if ((error = git_packbuilder_insert(push->pb, o, NULL)) < 0) | |
244 | goto on_error; | |
245 | } | |
246 | ||
247 | git_vector_foreach(&commits, i, o) { | |
248 | git_object *obj; | |
249 | ||
250 | if ((error = git_object_lookup(&obj, push->repo, o, GIT_OBJ_ANY)) < 0) | |
251 | goto on_error; | |
252 | ||
253 | switch (git_object_type(obj)) { | |
254 | case GIT_OBJ_TAG: /* TODO: expect tags */ | |
255 | case GIT_OBJ_COMMIT: | |
256 | if ((error = git_packbuilder_insert_tree(push->pb, | |
257 | git_commit_tree_id((git_commit *)obj))) < 0) { | |
258 | git_object_free(obj); | |
259 | goto on_error; | |
260 | } | |
261 | break; | |
262 | case GIT_OBJ_TREE: | |
263 | case GIT_OBJ_BLOB: | |
264 | default: | |
265 | git_object_free(obj); | |
266 | giterr_set(GITERR_INVALID, "Given object type invalid"); | |
267 | error = -1; | |
268 | goto on_error; | |
269 | } | |
270 | git_object_free(obj); | |
271 | } | |
272 | error = 0; | |
273 | ||
274 | on_error: | |
275 | git_vector_foreach(&commits, i, o) { | |
276 | git__free(o); | |
277 | } | |
278 | git_vector_free(&commits); | |
279 | return error; | |
280 | } | |
281 | ||
282 | static int calculate_work(git_push *push) | |
283 | { | |
284 | git_remote_head *head; | |
285 | push_spec *spec; | |
286 | unsigned int i, j; | |
287 | ||
4128f5aa CW |
288 | /* Update local and remote oids*/ |
289 | ||
613d5eb9 PK |
290 | git_vector_foreach(&push->specs, i, spec) { |
291 | if (spec->lref) { | |
4128f5aa | 292 | /* This is a create or update. Local ref must exist. */ |
613d5eb9 PK |
293 | if (git_reference_name_to_id( |
294 | &spec->loid, push->repo, spec->lref) < 0) { | |
295 | giterr_set(GIT_ENOTFOUND, "No such reference '%s'", spec->lref); | |
296 | return -1; | |
297 | } | |
4128f5aa | 298 | } |
613d5eb9 | 299 | |
4128f5aa CW |
300 | if (spec->rref) { |
301 | /* Remote ref may or may not (e.g. during create) already exist. */ | |
302 | git_vector_foreach(&push->remote->refs, j, head) { | |
303 | if (!strcmp(spec->rref, head->name)) { | |
304 | git_oid_cpy(&spec->roid, &head->oid); | |
305 | break; | |
613d5eb9 PK |
306 | } |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
314 | static int do_push(git_push *push) | |
315 | { | |
316 | int error; | |
317 | git_transport *transport = push->remote->transport; | |
318 | ||
319 | /* | |
320 | * A pack-file MUST be sent if either create or update command | |
321 | * is used, even if the server already has all the necessary | |
322 | * objects. In this case the client MUST send an empty pack-file. | |
323 | */ | |
324 | ||
325 | if ((error = git_packbuilder_new(&push->pb, push->repo)) < 0 || | |
326 | (error = calculate_work(push)) < 0 || | |
327 | (error = queue_objects(push)) < 0 || | |
328 | (error = transport->push(transport, push)) < 0) | |
329 | goto on_error; | |
330 | ||
331 | error = 0; | |
332 | ||
333 | on_error: | |
334 | git_packbuilder_free(push->pb); | |
335 | return error; | |
336 | } | |
337 | ||
338 | static int cb_filter_refs(git_remote_head *ref, void *data) | |
339 | { | |
340 | git_remote *remote = (git_remote *) data; | |
341 | return git_vector_insert(&remote->refs, ref); | |
342 | } | |
343 | ||
344 | static int filter_refs(git_remote *remote) | |
345 | { | |
346 | git_vector_clear(&remote->refs); | |
347 | return git_remote_ls(remote, cb_filter_refs, remote); | |
348 | } | |
349 | ||
350 | int git_push_finish(git_push *push) | |
351 | { | |
352 | int error; | |
353 | ||
354 | if (!git_remote_connected(push->remote) && | |
355 | (error = git_remote_connect(push->remote, GIT_DIRECTION_PUSH)) < 0) | |
356 | return error; | |
357 | ||
358 | if ((error = filter_refs(push->remote)) < 0 || | |
359 | (error = do_push(push)) < 0) | |
360 | return error; | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
365 | int git_push_unpack_ok(git_push *push) | |
366 | { | |
367 | return push->unpack_ok; | |
368 | } | |
369 | ||
370 | int git_push_status_foreach(git_push *push, | |
371 | int (*cb)(const char *ref, const char *msg, void *data), | |
372 | void *data) | |
373 | { | |
374 | push_status *status; | |
375 | unsigned int i; | |
376 | ||
377 | git_vector_foreach(&push->status, i, status) { | |
378 | if (cb(status->ref, status->msg, data) < 0) | |
379 | return GIT_EUSER; | |
380 | } | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
385 | void git_push_free(git_push *push) | |
386 | { | |
387 | push_spec *spec; | |
388 | push_status *status; | |
389 | unsigned int i; | |
390 | ||
391 | if (push == NULL) | |
392 | return; | |
393 | ||
394 | git_vector_foreach(&push->specs, i, spec) { | |
395 | free_refspec(spec); | |
396 | } | |
397 | git_vector_free(&push->specs); | |
398 | ||
399 | git_vector_foreach(&push->status, i, status) { | |
400 | free_status(status); | |
401 | } | |
402 | git_vector_free(&push->status); | |
403 | ||
404 | git__free(push); | |
405 | } |