]>
Commit | Line | Data |
---|---|---|
58e60dd2 NH |
1 | #include "cache.h" |
2 | #include "commit.h" | |
58e60dd2 NH |
3 | #include "tag.h" |
4 | #include "blob.h" | |
29508e1e | 5 | #include "http.h" |
aa1dbc98 | 6 | #include "refs.h" |
c4e05b1a | 7 | #include "diff.h" |
aa1dbc98 | 8 | #include "revision.h" |
3dfaf7bc | 9 | #include "exec_cmd.h" |
6b62816c | 10 | #include "remote.h" |
d633c882 | 11 | #include "list-objects.h" |
4a16d072 | 12 | #include "sigchain.h" |
29508e1e | 13 | |
bee8e79d | 14 | #include <expat.h> |
58e60dd2 NH |
15 | |
16 | static const char http_push_usage[] = | |
1b1dd23f | 17 | "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n"; |
58e60dd2 | 18 | |
92e2eb9c JS |
19 | #ifndef XML_STATUS_OK |
20 | enum XML_Status { | |
21 | XML_STATUS_OK = 1, | |
22 | XML_STATUS_ERROR = 0 | |
23 | }; | |
24 | #define XML_STATUS_OK 1 | |
25 | #define XML_STATUS_ERROR 0 | |
26 | #endif | |
27 | ||
197e8951 | 28 | #define PREV_BUF_SIZE 4096 |
58e60dd2 | 29 | |
acf59575 | 30 | /* DAV methods */ |
58e60dd2 NH |
31 | #define DAV_LOCK "LOCK" |
32 | #define DAV_MKCOL "MKCOL" | |
33 | #define DAV_MOVE "MOVE" | |
34 | #define DAV_PROPFIND "PROPFIND" | |
35 | #define DAV_PUT "PUT" | |
36 | #define DAV_UNLOCK "UNLOCK" | |
3dfaf7bc | 37 | #define DAV_DELETE "DELETE" |
acf59575 NH |
38 | |
39 | /* DAV lock flags */ | |
40 | #define DAV_PROP_LOCKWR (1u << 0) | |
41 | #define DAV_PROP_LOCKEX (1u << 1) | |
42 | #define DAV_LOCK_OK (1u << 2) | |
43 | ||
44 | /* DAV XML properties */ | |
45 | #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry" | |
46 | #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write" | |
47 | #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive" | |
48 | #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href" | |
49 | #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout" | |
50 | #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href" | |
aa1dbc98 NH |
51 | #define DAV_PROPFIND_RESP ".multistatus.response" |
52 | #define DAV_PROPFIND_NAME ".multistatus.response.href" | |
53 | #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection" | |
acf59575 NH |
54 | |
55 | /* DAV request body templates */ | |
aa1dbc98 NH |
56 | #define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>" |
57 | #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>" | |
58e60dd2 NH |
58 | #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>" |
59 | ||
75187c9d NH |
60 | #define LOCK_TIME 600 |
61 | #define LOCK_REFRESH 30 | |
62 | ||
1b65a5aa | 63 | /* bits #0-15 in revision.h */ |
aa1dbc98 | 64 | |
1b65a5aa JH |
65 | #define LOCAL (1u<<16) |
66 | #define REMOTE (1u<<17) | |
67 | #define FETCHING (1u<<18) | |
68 | #define PUSHING (1u<<19) | |
aa1dbc98 | 69 | |
3dfaf7bc NH |
70 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
71 | #define MAXDEPTH 5 | |
72 | ||
96f1e58f DR |
73 | static int pushing; |
74 | static int aborted; | |
a3c57c9a | 75 | static signed char remote_dir_exists[256]; |
58e60dd2 | 76 | |
96f1e58f | 77 | static int push_verbosely; |
28b9d6e5 | 78 | static int push_all = MATCH_REFS_NONE; |
96f1e58f | 79 | static int force_all; |
fe5d1d3e | 80 | static int dry_run; |
ae4efe19 | 81 | static int helper_status; |
58e60dd2 | 82 | |
96f1e58f | 83 | static struct object_list *objects; |
aa1dbc98 | 84 | |
58e60dd2 NH |
85 | struct repo |
86 | { | |
87 | char *url; | |
e1f33efe | 88 | char *path; |
aa1dbc98 | 89 | int path_len; |
197e8951 NH |
90 | int has_info_refs; |
91 | int can_update_info_refs; | |
92 | int has_info_packs; | |
58e60dd2 | 93 | struct packed_git *packs; |
512d632c | 94 | struct remote_lock *locks; |
58e60dd2 NH |
95 | }; |
96 | ||
7b5201a6 | 97 | static struct repo *repo; |
58e60dd2 NH |
98 | |
99 | enum transfer_state { | |
197e8951 NH |
100 | NEED_FETCH, |
101 | RUN_FETCH_LOOSE, | |
102 | RUN_FETCH_PACKED, | |
58e60dd2 NH |
103 | NEED_PUSH, |
104 | RUN_MKCOL, | |
105 | RUN_PUT, | |
106 | RUN_MOVE, | |
107 | ABORTED, | |
108 | COMPLETE, | |
109 | }; | |
110 | ||
111 | struct transfer_request | |
112 | { | |
aa1dbc98 | 113 | struct object *obj; |
58e60dd2 NH |
114 | char *url; |
115 | char *dest; | |
aa1dbc98 | 116 | struct remote_lock *lock; |
58e60dd2 NH |
117 | struct curl_slist *headers; |
118 | struct buffer buffer; | |
58e60dd2 NH |
119 | enum transfer_state state; |
120 | CURLcode curl_result; | |
121 | char errorstr[CURL_ERROR_SIZE]; | |
122 | long http_code; | |
197e8951 | 123 | void *userData; |
58e60dd2 NH |
124 | struct active_request_slot *slot; |
125 | struct transfer_request *next; | |
126 | }; | |
127 | ||
96f1e58f | 128 | static struct transfer_request *request_queue_head; |
58e60dd2 | 129 | |
acf59575 NH |
130 | struct xml_ctx |
131 | { | |
132 | char *name; | |
133 | int len; | |
134 | char *cdata; | |
135 | void (*userFunc)(struct xml_ctx *ctx, int tag_closed); | |
136 | void *userData; | |
137 | }; | |
138 | ||
aa1dbc98 | 139 | struct remote_lock |
26349b2e | 140 | { |
75187c9d | 141 | char *url; |
26349b2e | 142 | char *owner; |
75187c9d | 143 | char *token; |
dfab7c14 | 144 | char tmpfile_suffix[41]; |
26349b2e NH |
145 | time_t start_time; |
146 | long timeout; | |
75187c9d | 147 | int refreshing; |
aa1dbc98 NH |
148 | struct remote_lock *next; |
149 | }; | |
150 | ||
3030baa7 NH |
151 | /* Flags that control remote_ls processing */ |
152 | #define PROCESS_FILES (1u << 0) | |
153 | #define PROCESS_DIRS (1u << 1) | |
154 | #define RECURSIVE (1u << 2) | |
155 | ||
156 | /* Flags that remote_ls passes to callback functions */ | |
157 | #define IS_DIR (1u << 0) | |
158 | ||
159 | struct remote_ls_ctx | |
aa1dbc98 | 160 | { |
3030baa7 NH |
161 | char *path; |
162 | void (*userFunc)(struct remote_ls_ctx *ls); | |
163 | void *userData; | |
164 | int flags; | |
165 | char *dentry_name; | |
166 | int dentry_flags; | |
167 | struct remote_ls_ctx *parent; | |
26349b2e NH |
168 | }; |
169 | ||
b1c7d4aa TRC |
170 | /* get_dav_token_headers options */ |
171 | enum dav_header_flag { | |
172 | DAV_HEADER_IF = (1u << 0), | |
173 | DAV_HEADER_LOCK = (1u << 1), | |
174 | DAV_HEADER_TIMEOUT = (1u << 2) | |
175 | }; | |
176 | ||
519d05be MH |
177 | static char *xml_entities(char *s) |
178 | { | |
179 | struct strbuf buf = STRBUF_INIT; | |
180 | while (*s) { | |
181 | size_t len = strcspn(s, "\"<>&"); | |
182 | strbuf_add(&buf, s, len); | |
183 | s += len; | |
184 | switch (*s) { | |
185 | case '"': | |
186 | strbuf_addstr(&buf, """); | |
187 | break; | |
188 | case '<': | |
189 | strbuf_addstr(&buf, "<"); | |
190 | break; | |
191 | case '>': | |
192 | strbuf_addstr(&buf, ">"); | |
193 | break; | |
194 | case '&': | |
195 | strbuf_addstr(&buf, "&"); | |
196 | break; | |
c8400d9e HS |
197 | case 0: |
198 | return strbuf_detach(&buf, NULL); | |
519d05be MH |
199 | } |
200 | s++; | |
201 | } | |
202 | return strbuf_detach(&buf, NULL); | |
203 | } | |
204 | ||
d456c9fd JH |
205 | static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options) |
206 | { | |
b1c7d4aa TRC |
207 | struct strbuf buf = STRBUF_INIT; |
208 | struct curl_slist *dav_headers = NULL; | |
209 | ||
d456c9fd | 210 | if (options & DAV_HEADER_IF) { |
b1c7d4aa TRC |
211 | strbuf_addf(&buf, "If: (<%s>)", lock->token); |
212 | dav_headers = curl_slist_append(dav_headers, buf.buf); | |
213 | strbuf_reset(&buf); | |
214 | } | |
d456c9fd | 215 | if (options & DAV_HEADER_LOCK) { |
b1c7d4aa TRC |
216 | strbuf_addf(&buf, "Lock-Token: <%s>", lock->token); |
217 | dav_headers = curl_slist_append(dav_headers, buf.buf); | |
218 | strbuf_reset(&buf); | |
219 | } | |
d456c9fd | 220 | if (options & DAV_HEADER_TIMEOUT) { |
b1c7d4aa TRC |
221 | strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout); |
222 | dav_headers = curl_slist_append(dav_headers, buf.buf); | |
223 | strbuf_reset(&buf); | |
224 | } | |
225 | strbuf_release(&buf); | |
226 | ||
227 | return dav_headers; | |
228 | } | |
229 | ||
29508e1e | 230 | static void finish_request(struct transfer_request *request); |
197e8951 | 231 | static void release_request(struct transfer_request *request); |
58e60dd2 | 232 | |
29508e1e | 233 | static void process_response(void *callback_data) |
58e60dd2 | 234 | { |
29508e1e NH |
235 | struct transfer_request *request = |
236 | (struct transfer_request *)callback_data; | |
58e60dd2 | 237 | |
29508e1e | 238 | finish_request(request); |
58e60dd2 NH |
239 | } |
240 | ||
dd8239f9 | 241 | #ifdef USE_CURL_MULTI |
dcdb3335 | 242 | |
197e8951 NH |
243 | static void start_fetch_loose(struct transfer_request *request) |
244 | { | |
197e8951 | 245 | struct active_request_slot *slot; |
5424bc55 | 246 | struct http_object_request *obj_req; |
197e8951 | 247 | |
5424bc55 TRC |
248 | obj_req = new_http_object_request(repo->url, request->obj->sha1); |
249 | if (obj_req == NULL) { | |
197e8951 | 250 | request->state = ABORTED; |
197e8951 NH |
251 | return; |
252 | } | |
253 | ||
5424bc55 | 254 | slot = obj_req->slot; |
197e8951 NH |
255 | slot->callback_func = process_response; |
256 | slot->callback_data = request; | |
257 | request->slot = slot; | |
5424bc55 | 258 | request->userData = obj_req; |
197e8951 NH |
259 | |
260 | /* Try to get the request started, abort the request on error */ | |
261 | request->state = RUN_FETCH_LOOSE; | |
262 | if (!start_active_slot(slot)) { | |
263 | fprintf(stderr, "Unable to start GET request\n"); | |
7b5201a6 | 264 | repo->can_update_info_refs = 0; |
5424bc55 | 265 | release_http_object_request(obj_req); |
197e8951 NH |
266 | release_request(request); |
267 | } | |
268 | } | |
269 | ||
dd8239f9 JH |
270 | static void start_mkcol(struct transfer_request *request) |
271 | { | |
272 | char *hex = sha1_to_hex(request->obj->sha1); | |
273 | struct active_request_slot *slot; | |
dd8239f9 | 274 | |
7b5201a6 | 275 | request->url = get_remote_object_url(repo->url, hex, 1); |
dd8239f9 JH |
276 | |
277 | slot = get_active_slot(); | |
278 | slot->callback_func = process_response; | |
279 | slot->callback_data = request; | |
280 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */ | |
281 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); | |
282 | curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr); | |
283 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL); | |
284 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
285 | ||
286 | if (start_active_slot(slot)) { | |
287 | request->slot = slot; | |
288 | request->state = RUN_MKCOL; | |
289 | } else { | |
290 | request->state = ABORTED; | |
291 | free(request->url); | |
292 | request->url = NULL; | |
293 | } | |
294 | } | |
295 | #endif | |
296 | ||
197e8951 NH |
297 | static void start_fetch_packed(struct transfer_request *request) |
298 | { | |
197e8951 | 299 | struct packed_git *target; |
197e8951 NH |
300 | |
301 | struct transfer_request *check_request = request_queue_head; | |
2264dfa5 | 302 | struct http_pack_request *preq; |
197e8951 | 303 | |
7b5201a6 | 304 | target = find_sha1_pack(request->obj->sha1, repo->packs); |
197e8951 NH |
305 | if (!target) { |
306 | fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", sha1_to_hex(request->obj->sha1)); | |
7b5201a6 | 307 | repo->can_update_info_refs = 0; |
197e8951 NH |
308 | release_request(request); |
309 | return; | |
310 | } | |
311 | ||
312 | fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1)); | |
313 | fprintf(stderr, " which contains %s\n", sha1_to_hex(request->obj->sha1)); | |
314 | ||
2264dfa5 TRC |
315 | preq = new_http_pack_request(target, repo->url); |
316 | if (preq == NULL) { | |
317 | release_http_pack_request(preq); | |
318 | repo->can_update_info_refs = 0; | |
319 | return; | |
320 | } | |
321 | preq->lst = &repo->packs; | |
197e8951 NH |
322 | |
323 | /* Make sure there isn't another open request for this pack */ | |
324 | while (check_request) { | |
325 | if (check_request->state == RUN_FETCH_PACKED && | |
2264dfa5 TRC |
326 | !strcmp(check_request->url, preq->url)) { |
327 | release_http_pack_request(preq); | |
197e8951 NH |
328 | release_request(request); |
329 | return; | |
330 | } | |
331 | check_request = check_request->next; | |
332 | } | |
333 | ||
2264dfa5 TRC |
334 | preq->slot->callback_func = process_response; |
335 | preq->slot->callback_data = request; | |
336 | request->slot = preq->slot; | |
337 | request->userData = preq; | |
197e8951 NH |
338 | |
339 | /* Try to get the request started, abort the request on error */ | |
340 | request->state = RUN_FETCH_PACKED; | |
2264dfa5 | 341 | if (!start_active_slot(preq->slot)) { |
197e8951 | 342 | fprintf(stderr, "Unable to start GET request\n"); |
2264dfa5 | 343 | release_http_pack_request(preq); |
7b5201a6 | 344 | repo->can_update_info_refs = 0; |
197e8951 NH |
345 | release_request(request); |
346 | } | |
347 | } | |
348 | ||
58e60dd2 NH |
349 | static void start_put(struct transfer_request *request) |
350 | { | |
aa1dbc98 | 351 | char *hex = sha1_to_hex(request->obj->sha1); |
58e60dd2 | 352 | struct active_request_slot *slot; |
817d14a8 | 353 | struct strbuf buf = STRBUF_INIT; |
21666f1a | 354 | enum object_type type; |
58e60dd2 NH |
355 | char hdr[50]; |
356 | void *unpacked; | |
357 | unsigned long len; | |
358 | int hdrlen; | |
359 | ssize_t size; | |
360 | z_stream stream; | |
361 | ||
21666f1a NP |
362 | unpacked = read_sha1_file(request->obj->sha1, &type, &len); |
363 | hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1; | |
58e60dd2 NH |
364 | |
365 | /* Set it up */ | |
366 | memset(&stream, 0, sizeof(stream)); | |
12f6c308 | 367 | deflateInit(&stream, zlib_compression_level); |
58e60dd2 | 368 | size = deflateBound(&stream, len + hdrlen); |
028c2976 MH |
369 | strbuf_init(&request->buffer.buf, size); |
370 | request->buffer.posn = 0; | |
58e60dd2 NH |
371 | |
372 | /* Compress it */ | |
028c2976 | 373 | stream.next_out = (unsigned char *)request->buffer.buf.buf; |
58e60dd2 NH |
374 | stream.avail_out = size; |
375 | ||
376 | /* First header.. */ | |
377 | stream.next_in = (void *)hdr; | |
378 | stream.avail_in = hdrlen; | |
379 | while (deflate(&stream, 0) == Z_OK) | |
380 | /* nothing */; | |
381 | ||
382 | /* Then the data itself.. */ | |
383 | stream.next_in = unpacked; | |
384 | stream.avail_in = len; | |
385 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
386 | /* nothing */; | |
387 | deflateEnd(&stream); | |
388 | free(unpacked); | |
389 | ||
028c2976 | 390 | request->buffer.buf.len = stream.total_out; |
58e60dd2 | 391 | |
817d14a8 | 392 | strbuf_addstr(&buf, "Destination: "); |
7b5201a6 | 393 | append_remote_object_url(&buf, repo->url, hex, 0); |
817d14a8 TRC |
394 | request->dest = strbuf_detach(&buf, NULL); |
395 | ||
7b5201a6 | 396 | append_remote_object_url(&buf, repo->url, hex, 0); |
dfab7c14 | 397 | strbuf_add(&buf, request->lock->tmpfile_suffix, 41); |
817d14a8 | 398 | request->url = strbuf_detach(&buf, NULL); |
58e60dd2 NH |
399 | |
400 | slot = get_active_slot(); | |
29508e1e NH |
401 | slot->callback_func = process_response; |
402 | slot->callback_data = request; | |
58e60dd2 | 403 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &request->buffer); |
028c2976 | 404 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, request->buffer.buf.len); |
58e60dd2 | 405 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); |
3944ba0c MS |
406 | #ifndef NO_CURL_IOCTL |
407 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer); | |
408 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &request->buffer); | |
409 | #endif | |
58e60dd2 | 410 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); |
d21f9794 | 411 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
58e60dd2 NH |
412 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); |
413 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
414 | curl_easy_setopt(slot->curl, CURLOPT_PUT, 1); | |
58e60dd2 NH |
415 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); |
416 | ||
417 | if (start_active_slot(slot)) { | |
418 | request->slot = slot; | |
419 | request->state = RUN_PUT; | |
420 | } else { | |
421 | request->state = ABORTED; | |
422 | free(request->url); | |
7b899967 | 423 | request->url = NULL; |
58e60dd2 NH |
424 | } |
425 | } | |
426 | ||
427 | static void start_move(struct transfer_request *request) | |
428 | { | |
429 | struct active_request_slot *slot; | |
430 | struct curl_slist *dav_headers = NULL; | |
431 | ||
432 | slot = get_active_slot(); | |
29508e1e NH |
433 | slot->callback_func = process_response; |
434 | slot->callback_data = request; | |
58e60dd2 NH |
435 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */ |
436 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MOVE); | |
437 | dav_headers = curl_slist_append(dav_headers, request->dest); | |
438 | dav_headers = curl_slist_append(dav_headers, "Overwrite: T"); | |
439 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
440 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
441 | curl_easy_setopt(slot->curl, CURLOPT_URL, request->url); | |
442 | ||
443 | if (start_active_slot(slot)) { | |
444 | request->slot = slot; | |
445 | request->state = RUN_MOVE; | |
446 | } else { | |
447 | request->state = ABORTED; | |
448 | free(request->url); | |
7b899967 | 449 | request->url = NULL; |
58e60dd2 NH |
450 | } |
451 | } | |
452 | ||
512d632c | 453 | static int refresh_lock(struct remote_lock *lock) |
75187c9d NH |
454 | { |
455 | struct active_request_slot *slot; | |
baa7b67d | 456 | struct slot_results results; |
b1c7d4aa | 457 | struct curl_slist *dav_headers; |
512d632c | 458 | int rc = 0; |
75187c9d | 459 | |
512d632c | 460 | lock->refreshing = 1; |
75187c9d | 461 | |
b1c7d4aa | 462 | dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT); |
75187c9d | 463 | |
512d632c NH |
464 | slot = get_active_slot(); |
465 | slot->results = &results; | |
466 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); | |
467 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
468 | curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url); | |
469 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK); | |
470 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
75187c9d | 471 | |
512d632c NH |
472 | if (start_active_slot(slot)) { |
473 | run_active_slot(slot); | |
474 | if (results.curl_result != CURLE_OK) { | |
475 | fprintf(stderr, "LOCK HTTP error %ld\n", | |
476 | results.http_code); | |
477 | } else { | |
478 | lock->start_time = time(NULL); | |
479 | rc = 1; | |
480 | } | |
481 | } | |
aa1dbc98 | 482 | |
512d632c NH |
483 | lock->refreshing = 0; |
484 | curl_slist_free_all(dav_headers); | |
aa1dbc98 | 485 | |
512d632c NH |
486 | return rc; |
487 | } | |
488 | ||
ec26b4d6 | 489 | static void check_locks(void) |
512d632c | 490 | { |
7b5201a6 | 491 | struct remote_lock *lock = repo->locks; |
512d632c NH |
492 | time_t current_time = time(NULL); |
493 | int time_remaining; | |
494 | ||
495 | while (lock) { | |
496 | time_remaining = lock->start_time + lock->timeout - | |
497 | current_time; | |
498 | if (!lock->refreshing && time_remaining < LOCK_REFRESH) { | |
499 | if (!refresh_lock(lock)) { | |
500 | fprintf(stderr, | |
501 | "Unable to refresh lock for %s\n", | |
502 | lock->url); | |
503 | aborted = 1; | |
504 | return; | |
aa1dbc98 | 505 | } |
75187c9d | 506 | } |
512d632c | 507 | lock = lock->next; |
75187c9d | 508 | } |
aa1dbc98 | 509 | } |
75187c9d | 510 | |
aa1dbc98 NH |
511 | static void release_request(struct transfer_request *request) |
512 | { | |
513 | struct transfer_request *entry = request_queue_head; | |
514 | ||
515 | if (request == request_queue_head) { | |
516 | request_queue_head = request->next; | |
517 | } else { | |
518 | while (entry->next != NULL && entry->next != request) | |
519 | entry = entry->next; | |
520 | if (entry->next == request) | |
521 | entry->next = entry->next->next; | |
522 | } | |
523 | ||
8e0f7003 | 524 | free(request->url); |
aa1dbc98 | 525 | free(request); |
75187c9d NH |
526 | } |
527 | ||
58e60dd2 NH |
528 | static void finish_request(struct transfer_request *request) |
529 | { | |
2264dfa5 | 530 | struct http_pack_request *preq; |
5424bc55 | 531 | struct http_object_request *obj_req; |
197e8951 NH |
532 | |
533 | request->curl_result = request->slot->curl_result; | |
58e60dd2 NH |
534 | request->http_code = request->slot->http_code; |
535 | request->slot = NULL; | |
75187c9d | 536 | |
aa1dbc98 | 537 | /* Keep locks active */ |
512d632c | 538 | check_locks(); |
75187c9d | 539 | |
58e60dd2 NH |
540 | if (request->headers != NULL) |
541 | curl_slist_free_all(request->headers); | |
7b899967 NH |
542 | |
543 | /* URL is reused for MOVE after PUT */ | |
544 | if (request->state != RUN_PUT) { | |
545 | free(request->url); | |
546 | request->url = NULL; | |
aa1dbc98 | 547 | } |
7b899967 | 548 | |
aa1dbc98 | 549 | if (request->state == RUN_MKCOL) { |
58e60dd2 NH |
550 | if (request->curl_result == CURLE_OK || |
551 | request->http_code == 405) { | |
aa1dbc98 | 552 | remote_dir_exists[request->obj->sha1[0]] = 1; |
58e60dd2 NH |
553 | start_put(request); |
554 | } else { | |
555 | fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n", | |
aa1dbc98 | 556 | sha1_to_hex(request->obj->sha1), |
58e60dd2 NH |
557 | request->curl_result, request->http_code); |
558 | request->state = ABORTED; | |
559 | aborted = 1; | |
560 | } | |
561 | } else if (request->state == RUN_PUT) { | |
562 | if (request->curl_result == CURLE_OK) { | |
563 | start_move(request); | |
564 | } else { | |
565 | fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n", | |
aa1dbc98 | 566 | sha1_to_hex(request->obj->sha1), |
58e60dd2 NH |
567 | request->curl_result, request->http_code); |
568 | request->state = ABORTED; | |
569 | aborted = 1; | |
570 | } | |
571 | } else if (request->state == RUN_MOVE) { | |
572 | if (request->curl_result == CURLE_OK) { | |
1a703cba NH |
573 | if (push_verbosely) |
574 | fprintf(stderr, " sent %s\n", | |
575 | sha1_to_hex(request->obj->sha1)); | |
aa1dbc98 NH |
576 | request->obj->flags |= REMOTE; |
577 | release_request(request); | |
58e60dd2 NH |
578 | } else { |
579 | fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n", | |
aa1dbc98 | 580 | sha1_to_hex(request->obj->sha1), |
58e60dd2 NH |
581 | request->curl_result, request->http_code); |
582 | request->state = ABORTED; | |
583 | aborted = 1; | |
584 | } | |
197e8951 | 585 | } else if (request->state == RUN_FETCH_LOOSE) { |
5424bc55 | 586 | obj_req = (struct http_object_request *)request->userData; |
197e8951 | 587 | |
5424bc55 TRC |
588 | if (finish_http_object_request(obj_req) == 0) |
589 | if (obj_req->rename == 0) | |
590 | request->obj->flags |= (LOCAL | REMOTE); | |
197e8951 NH |
591 | |
592 | /* Try fetching packed if necessary */ | |
5424bc55 TRC |
593 | if (request->obj->flags & LOCAL) { |
594 | release_http_object_request(obj_req); | |
197e8951 | 595 | release_request(request); |
5424bc55 | 596 | } else |
197e8951 NH |
597 | start_fetch_packed(request); |
598 | ||
599 | } else if (request->state == RUN_FETCH_PACKED) { | |
2264dfa5 | 600 | int fail = 1; |
197e8951 NH |
601 | if (request->curl_result != CURLE_OK) { |
602 | fprintf(stderr, "Unable to get pack file %s\n%s", | |
603 | request->url, curl_errorstr); | |
197e8951 | 604 | } else { |
2264dfa5 TRC |
605 | preq = (struct http_pack_request *)request->userData; |
606 | ||
607 | if (preq) { | |
609621a4 | 608 | if (finish_http_pack_request(preq) == 0) |
2264dfa5 TRC |
609 | fail = 0; |
610 | release_http_pack_request(preq); | |
197e8951 NH |
611 | } |
612 | } | |
2264dfa5 TRC |
613 | if (fail) |
614 | repo->can_update_info_refs = 0; | |
197e8951 | 615 | release_request(request); |
58e60dd2 NH |
616 | } |
617 | } | |
618 | ||
b3ca4e4e | 619 | #ifdef USE_CURL_MULTI |
4f66250d | 620 | static int is_running_queue; |
fc57b6aa | 621 | static int fill_active_slot(void *unused) |
58e60dd2 | 622 | { |
8e24cbae | 623 | struct transfer_request *request; |
58e60dd2 | 624 | |
4f66250d | 625 | if (aborted || !is_running_queue) |
45c17412 | 626 | return 0; |
58e60dd2 | 627 | |
45c17412 | 628 | for (request = request_queue_head; request; request = request->next) { |
197e8951 NH |
629 | if (request->state == NEED_FETCH) { |
630 | start_fetch_loose(request); | |
45c17412 | 631 | return 1; |
197e8951 | 632 | } else if (pushing && request->state == NEED_PUSH) { |
aa1dbc98 | 633 | if (remote_dir_exists[request->obj->sha1[0]] == 1) { |
0dd276b8 | 634 | start_put(request); |
aa1dbc98 | 635 | } else { |
0dd276b8 | 636 | start_mkcol(request); |
aa1dbc98 | 637 | } |
45c17412 | 638 | return 1; |
58e60dd2 | 639 | } |
aa1dbc98 | 640 | } |
45c17412 | 641 | return 0; |
58e60dd2 | 642 | } |
b3ca4e4e | 643 | #endif |
58e60dd2 | 644 | |
aa1dbc98 NH |
645 | static void get_remote_object_list(unsigned char parent); |
646 | ||
197e8951 NH |
647 | static void add_fetch_request(struct object *obj) |
648 | { | |
649 | struct transfer_request *request; | |
650 | ||
651 | check_locks(); | |
652 | ||
653 | /* | |
654 | * Don't fetch the object if it's known to exist locally | |
655 | * or is already in the request queue | |
656 | */ | |
657 | if (remote_dir_exists[obj->sha1[0]] == -1) | |
658 | get_remote_object_list(obj->sha1[0]); | |
659 | if (obj->flags & (LOCAL | FETCHING)) | |
660 | return; | |
661 | ||
662 | obj->flags |= FETCHING; | |
663 | request = xmalloc(sizeof(*request)); | |
664 | request->obj = obj; | |
665 | request->url = NULL; | |
666 | request->lock = NULL; | |
667 | request->headers = NULL; | |
197e8951 NH |
668 | request->state = NEED_FETCH; |
669 | request->next = request_queue_head; | |
670 | request_queue_head = request; | |
671 | ||
b3ca4e4e | 672 | #ifdef USE_CURL_MULTI |
197e8951 NH |
673 | fill_active_slots(); |
674 | step_active_slots(); | |
b3ca4e4e | 675 | #endif |
197e8951 NH |
676 | } |
677 | ||
1a703cba | 678 | static int add_send_request(struct object *obj, struct remote_lock *lock) |
58e60dd2 NH |
679 | { |
680 | struct transfer_request *request = request_queue_head; | |
58e60dd2 | 681 | struct packed_git *target; |
58e60dd2 | 682 | |
512d632c NH |
683 | /* Keep locks active */ |
684 | check_locks(); | |
685 | ||
aa1dbc98 NH |
686 | /* |
687 | * Don't push the object if it's known to exist on the remote | |
688 | * or is already in the request queue | |
689 | */ | |
690 | if (remote_dir_exists[obj->sha1[0]] == -1) | |
691 | get_remote_object_list(obj->sha1[0]); | |
692 | if (obj->flags & (REMOTE | PUSHING)) | |
1a703cba | 693 | return 0; |
7b5201a6 | 694 | target = find_sha1_pack(obj->sha1, repo->packs); |
aa1dbc98 NH |
695 | if (target) { |
696 | obj->flags |= REMOTE; | |
1a703cba | 697 | return 0; |
aa1dbc98 | 698 | } |
58e60dd2 | 699 | |
aa1dbc98 | 700 | obj->flags |= PUSHING; |
58e60dd2 | 701 | request = xmalloc(sizeof(*request)); |
aa1dbc98 | 702 | request->obj = obj; |
58e60dd2 | 703 | request->url = NULL; |
26349b2e | 704 | request->lock = lock; |
58e60dd2 | 705 | request->headers = NULL; |
aa1dbc98 | 706 | request->state = NEED_PUSH; |
c17fb6ee NH |
707 | request->next = request_queue_head; |
708 | request_queue_head = request; | |
29508e1e | 709 | |
b3ca4e4e | 710 | #ifdef USE_CURL_MULTI |
29508e1e NH |
711 | fill_active_slots(); |
712 | step_active_slots(); | |
b3ca4e4e | 713 | #endif |
1a703cba NH |
714 | |
715 | return 1; | |
58e60dd2 NH |
716 | } |
717 | ||
f4f440a0 | 718 | static int fetch_indices(void) |
58e60dd2 | 719 | { |
b8caac2b | 720 | int ret; |
58e60dd2 | 721 | |
58e60dd2 NH |
722 | if (push_verbosely) |
723 | fprintf(stderr, "Getting pack list\n"); | |
1a703cba | 724 | |
b8caac2b TRC |
725 | switch (http_get_info_packs(repo->url, &repo->packs)) { |
726 | case HTTP_OK: | |
727 | case HTTP_MISSING_TARGET: | |
728 | ret = 0; | |
729 | break; | |
730 | default: | |
731 | ret = -1; | |
58e60dd2 NH |
732 | } |
733 | ||
b8caac2b | 734 | return ret; |
58e60dd2 NH |
735 | } |
736 | ||
aa1dbc98 NH |
737 | static void one_remote_object(const char *hex) |
738 | { | |
739 | unsigned char sha1[20]; | |
740 | struct object *obj; | |
741 | ||
742 | if (get_sha1_hex(hex, sha1) != 0) | |
743 | return; | |
744 | ||
745 | obj = lookup_object(sha1); | |
746 | if (!obj) | |
747 | obj = parse_object(sha1); | |
748 | ||
749 | /* Ignore remote objects that don't exist locally */ | |
750 | if (!obj) | |
751 | return; | |
752 | ||
753 | obj->flags |= REMOTE; | |
754 | if (!object_list_contains(objects, obj)) | |
1f1e895f | 755 | object_list_insert(obj, &objects); |
aa1dbc98 NH |
756 | } |
757 | ||
acf59575 | 758 | static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed) |
26349b2e | 759 | { |
acf59575 NH |
760 | int *lock_flags = (int *)ctx->userData; |
761 | ||
762 | if (tag_closed) { | |
763 | if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) { | |
764 | if ((*lock_flags & DAV_PROP_LOCKEX) && | |
765 | (*lock_flags & DAV_PROP_LOCKWR)) { | |
766 | *lock_flags |= DAV_LOCK_OK; | |
767 | } | |
768 | *lock_flags &= DAV_LOCK_OK; | |
769 | } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) { | |
770 | *lock_flags |= DAV_PROP_LOCKWR; | |
771 | } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) { | |
772 | *lock_flags |= DAV_PROP_LOCKEX; | |
773 | } | |
774 | } | |
26349b2e NH |
775 | } |
776 | ||
acf59575 | 777 | static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed) |
26349b2e | 778 | { |
aa1dbc98 | 779 | struct remote_lock *lock = (struct remote_lock *)ctx->userData; |
dfab7c14 TRC |
780 | git_SHA_CTX sha_ctx; |
781 | unsigned char lock_token_sha1[20]; | |
acf59575 NH |
782 | |
783 | if (tag_closed && ctx->cdata) { | |
784 | if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) { | |
785 | lock->owner = xmalloc(strlen(ctx->cdata) + 1); | |
786 | strcpy(lock->owner, ctx->cdata); | |
787 | } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) { | |
cc44c765 | 788 | if (!prefixcmp(ctx->cdata, "Second-")) |
acf59575 NH |
789 | lock->timeout = |
790 | strtol(ctx->cdata + 7, NULL, 10); | |
791 | } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) { | |
753bc911 KK |
792 | lock->token = xmalloc(strlen(ctx->cdata) + 1); |
793 | strcpy(lock->token, ctx->cdata); | |
dfab7c14 TRC |
794 | |
795 | git_SHA1_Init(&sha_ctx); | |
796 | git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token)); | |
797 | git_SHA1_Final(lock_token_sha1, &sha_ctx); | |
798 | ||
799 | lock->tmpfile_suffix[0] = '_'; | |
800 | memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40); | |
acf59575 | 801 | } |
26349b2e NH |
802 | } |
803 | } | |
804 | ||
aa1dbc98 | 805 | static void one_remote_ref(char *refname); |
aa1dbc98 | 806 | |
26349b2e | 807 | static void |
acf59575 | 808 | xml_start_tag(void *userData, const char *name, const char **atts) |
26349b2e | 809 | { |
acf59575 | 810 | struct xml_ctx *ctx = (struct xml_ctx *)userData; |
ef9e58c8 | 811 | const char *c = strchr(name, ':'); |
acf59575 NH |
812 | int new_len; |
813 | ||
814 | if (c == NULL) | |
815 | c = name; | |
816 | else | |
817 | c++; | |
818 | ||
819 | new_len = strlen(ctx->name) + strlen(c) + 2; | |
820 | ||
821 | if (new_len > ctx->len) { | |
822 | ctx->name = xrealloc(ctx->name, new_len); | |
823 | ctx->len = new_len; | |
26349b2e | 824 | } |
acf59575 NH |
825 | strcat(ctx->name, "."); |
826 | strcat(ctx->name, c); | |
26349b2e | 827 | |
4cac42b1 JH |
828 | free(ctx->cdata); |
829 | ctx->cdata = NULL; | |
acf59575 NH |
830 | |
831 | ctx->userFunc(ctx, 0); | |
26349b2e NH |
832 | } |
833 | ||
58e60dd2 | 834 | static void |
acf59575 | 835 | xml_end_tag(void *userData, const char *name) |
58e60dd2 | 836 | { |
acf59575 | 837 | struct xml_ctx *ctx = (struct xml_ctx *)userData; |
ef9e58c8 | 838 | const char *c = strchr(name, ':'); |
acf59575 | 839 | char *ep; |
58e60dd2 | 840 | |
acf59575 NH |
841 | ctx->userFunc(ctx, 1); |
842 | ||
843 | if (c == NULL) | |
844 | c = name; | |
845 | else | |
846 | c++; | |
847 | ||
848 | ep = ctx->name + strlen(ctx->name) - strlen(c) - 1; | |
849 | *ep = 0; | |
58e60dd2 NH |
850 | } |
851 | ||
852 | static void | |
acf59575 | 853 | xml_cdata(void *userData, const XML_Char *s, int len) |
58e60dd2 | 854 | { |
acf59575 | 855 | struct xml_ctx *ctx = (struct xml_ctx *)userData; |
4cac42b1 | 856 | free(ctx->cdata); |
182af834 | 857 | ctx->cdata = xmemdupz(s, len); |
58e60dd2 NH |
858 | } |
859 | ||
554fe20d | 860 | static struct remote_lock *lock_remote(const char *path, long timeout) |
58e60dd2 NH |
861 | { |
862 | struct active_request_slot *slot; | |
baa7b67d | 863 | struct slot_results results; |
028c2976 MH |
864 | struct buffer out_buffer = { STRBUF_INIT, 0 }; |
865 | struct strbuf in_buffer = STRBUF_INIT; | |
58e60dd2 | 866 | char *url; |
0772b9a6 | 867 | char *ep; |
58e60dd2 | 868 | char timeout_header[25]; |
512d632c | 869 | struct remote_lock *lock = NULL; |
58e60dd2 | 870 | struct curl_slist *dav_headers = NULL; |
acf59575 | 871 | struct xml_ctx ctx; |
519d05be | 872 | char *escaped; |
58e60dd2 | 873 | |
7b5201a6 AK |
874 | url = xmalloc(strlen(repo->url) + strlen(path) + 1); |
875 | sprintf(url, "%s%s", repo->url, path); | |
aa1dbc98 | 876 | |
0772b9a6 | 877 | /* Make sure leading directories exist for the remote ref */ |
7b5201a6 | 878 | ep = strchr(url + strlen(repo->url) + 1, '/'); |
0772b9a6 | 879 | while (ep) { |
466ddf90 JS |
880 | char saved_character = ep[1]; |
881 | ep[1] = '\0'; | |
0772b9a6 | 882 | slot = get_active_slot(); |
baa7b67d | 883 | slot->results = &results; |
0772b9a6 NH |
884 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); |
885 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
886 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL); | |
887 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
888 | if (start_active_slot(slot)) { | |
889 | run_active_slot(slot); | |
baa7b67d NH |
890 | if (results.curl_result != CURLE_OK && |
891 | results.http_code != 405) { | |
0772b9a6 NH |
892 | fprintf(stderr, |
893 | "Unable to create branch path %s\n", | |
894 | url); | |
895 | free(url); | |
896 | return NULL; | |
897 | } | |
898 | } else { | |
1a703cba | 899 | fprintf(stderr, "Unable to start MKCOL request\n"); |
0772b9a6 NH |
900 | free(url); |
901 | return NULL; | |
902 | } | |
466ddf90 | 903 | ep[1] = saved_character; |
0772b9a6 NH |
904 | ep = strchr(ep + 1, '/'); |
905 | } | |
906 | ||
519d05be MH |
907 | escaped = xml_entities(git_default_email); |
908 | strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped); | |
909 | free(escaped); | |
26349b2e | 910 | |
75187c9d | 911 | sprintf(timeout_header, "Timeout: Second-%ld", timeout); |
58e60dd2 NH |
912 | dav_headers = curl_slist_append(dav_headers, timeout_header); |
913 | dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); | |
914 | ||
915 | slot = get_active_slot(); | |
baa7b67d | 916 | slot->results = &results; |
58e60dd2 | 917 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); |
028c2976 | 918 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len); |
58e60dd2 | 919 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); |
3944ba0c MS |
920 | #ifndef NO_CURL_IOCTL |
921 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer); | |
922 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer); | |
923 | #endif | |
26349b2e | 924 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); |
29508e1e | 925 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
58e60dd2 NH |
926 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); |
927 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
928 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK); | |
929 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
930 | ||
aa1dbc98 | 931 | lock = xcalloc(1, sizeof(*lock)); |
aa1dbc98 | 932 | lock->timeout = -1; |
acf59575 | 933 | |
58e60dd2 NH |
934 | if (start_active_slot(slot)) { |
935 | run_active_slot(slot); | |
baa7b67d | 936 | if (results.curl_result == CURLE_OK) { |
472b2570 MH |
937 | XML_Parser parser = XML_ParserCreate(NULL); |
938 | enum XML_Status result; | |
acf59575 NH |
939 | ctx.name = xcalloc(10, 1); |
940 | ctx.len = 0; | |
941 | ctx.cdata = NULL; | |
942 | ctx.userFunc = handle_new_lock_ctx; | |
aa1dbc98 | 943 | ctx.userData = lock; |
acf59575 NH |
944 | XML_SetUserData(parser, &ctx); |
945 | XML_SetElementHandler(parser, xml_start_tag, | |
946 | xml_end_tag); | |
947 | XML_SetCharacterDataHandler(parser, xml_cdata); | |
028c2976 MH |
948 | result = XML_Parse(parser, in_buffer.buf, |
949 | in_buffer.len, 1); | |
acf59575 NH |
950 | free(ctx.name); |
951 | if (result != XML_STATUS_OK) { | |
952 | fprintf(stderr, "XML error: %s\n", | |
953 | XML_ErrorString( | |
954 | XML_GetErrorCode(parser))); | |
aa1dbc98 | 955 | lock->timeout = -1; |
acf59575 | 956 | } |
472b2570 | 957 | XML_ParserFree(parser); |
58e60dd2 NH |
958 | } |
959 | } else { | |
1a703cba | 960 | fprintf(stderr, "Unable to start LOCK request\n"); |
58e60dd2 NH |
961 | } |
962 | ||
acf59575 | 963 | curl_slist_free_all(dav_headers); |
028c2976 MH |
964 | strbuf_release(&out_buffer.buf); |
965 | strbuf_release(&in_buffer); | |
26349b2e | 966 | |
aa1dbc98 | 967 | if (lock->token == NULL || lock->timeout <= 0) { |
8e0f7003 JM |
968 | free(lock->token); |
969 | free(lock->owner); | |
75187c9d | 970 | free(url); |
aa1dbc98 NH |
971 | free(lock); |
972 | lock = NULL; | |
acf59575 | 973 | } else { |
aa1dbc98 | 974 | lock->url = url; |
aa1dbc98 | 975 | lock->start_time = time(NULL); |
7b5201a6 AK |
976 | lock->next = repo->locks; |
977 | repo->locks = lock; | |
26349b2e NH |
978 | } |
979 | ||
aa1dbc98 | 980 | return lock; |
58e60dd2 NH |
981 | } |
982 | ||
aa1dbc98 | 983 | static int unlock_remote(struct remote_lock *lock) |
58e60dd2 NH |
984 | { |
985 | struct active_request_slot *slot; | |
baa7b67d | 986 | struct slot_results results; |
7b5201a6 | 987 | struct remote_lock *prev = repo->locks; |
b1c7d4aa | 988 | struct curl_slist *dav_headers; |
58e60dd2 NH |
989 | int rc = 0; |
990 | ||
b1c7d4aa | 991 | dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK); |
58e60dd2 NH |
992 | |
993 | slot = get_active_slot(); | |
baa7b67d | 994 | slot->results = &results; |
58e60dd2 | 995 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); |
75187c9d | 996 | curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url); |
58e60dd2 NH |
997 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK); |
998 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
999 | ||
1000 | if (start_active_slot(slot)) { | |
1001 | run_active_slot(slot); | |
baa7b67d | 1002 | if (results.curl_result == CURLE_OK) |
58e60dd2 NH |
1003 | rc = 1; |
1004 | else | |
512d632c | 1005 | fprintf(stderr, "UNLOCK HTTP error %ld\n", |
baa7b67d | 1006 | results.http_code); |
58e60dd2 | 1007 | } else { |
512d632c | 1008 | fprintf(stderr, "Unable to start UNLOCK request\n"); |
58e60dd2 NH |
1009 | } |
1010 | ||
1011 | curl_slist_free_all(dav_headers); | |
75187c9d | 1012 | |
7b5201a6 AK |
1013 | if (repo->locks == lock) { |
1014 | repo->locks = lock->next; | |
512d632c NH |
1015 | } else { |
1016 | while (prev && prev->next != lock) | |
1017 | prev = prev->next; | |
1018 | if (prev) | |
1019 | prev->next = prev->next->next; | |
1020 | } | |
1021 | ||
8e0f7003 | 1022 | free(lock->owner); |
512d632c NH |
1023 | free(lock->url); |
1024 | free(lock->token); | |
1025 | free(lock); | |
58e60dd2 NH |
1026 | |
1027 | return rc; | |
1028 | } | |
1029 | ||
6a491a17 CB |
1030 | static void remove_locks(void) |
1031 | { | |
7b5201a6 | 1032 | struct remote_lock *lock = repo->locks; |
6a491a17 CB |
1033 | |
1034 | fprintf(stderr, "Removing remote locks...\n"); | |
1035 | while (lock) { | |
6589ebf1 | 1036 | struct remote_lock *next = lock->next; |
6a491a17 | 1037 | unlock_remote(lock); |
6589ebf1 | 1038 | lock = next; |
6a491a17 CB |
1039 | } |
1040 | } | |
1041 | ||
1042 | static void remove_locks_on_signal(int signo) | |
1043 | { | |
1044 | remove_locks(); | |
4a16d072 | 1045 | sigchain_pop(signo); |
6a491a17 CB |
1046 | raise(signo); |
1047 | } | |
1048 | ||
3030baa7 NH |
1049 | static void remote_ls(const char *path, int flags, |
1050 | void (*userFunc)(struct remote_ls_ctx *ls), | |
1051 | void *userData); | |
aa1dbc98 | 1052 | |
3030baa7 NH |
1053 | static void process_ls_object(struct remote_ls_ctx *ls) |
1054 | { | |
1055 | unsigned int *parent = (unsigned int *)ls->userData; | |
1056 | char *path = ls->dentry_name; | |
1057 | char *obj_hex; | |
aa1dbc98 | 1058 | |
3030baa7 NH |
1059 | if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) { |
1060 | remote_dir_exists[*parent] = 1; | |
1061 | return; | |
1062 | } | |
aa1dbc98 | 1063 | |
3030baa7 NH |
1064 | if (strlen(path) != 49) |
1065 | return; | |
1066 | path += 8; | |
1067 | obj_hex = xmalloc(strlen(path)); | |
eecc8367 ER |
1068 | /* NB: path is not null-terminated, can not use strlcpy here */ |
1069 | memcpy(obj_hex, path, 2); | |
3030baa7 NH |
1070 | strcpy(obj_hex + 2, path + 3); |
1071 | one_remote_object(obj_hex); | |
1072 | free(obj_hex); | |
1073 | } | |
aa1dbc98 | 1074 | |
3030baa7 NH |
1075 | static void process_ls_ref(struct remote_ls_ctx *ls) |
1076 | { | |
1077 | if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) { | |
1078 | fprintf(stderr, " %s\n", ls->dentry_name); | |
1079 | return; | |
1080 | } | |
aa1dbc98 | 1081 | |
3030baa7 NH |
1082 | if (!(ls->dentry_flags & IS_DIR)) |
1083 | one_remote_ref(ls->dentry_name); | |
1084 | } | |
aa1dbc98 | 1085 | |
3030baa7 NH |
1086 | static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed) |
1087 | { | |
1088 | struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData; | |
aa1dbc98 | 1089 | |
3030baa7 NH |
1090 | if (tag_closed) { |
1091 | if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) { | |
1092 | if (ls->dentry_flags & IS_DIR) { | |
1093 | if (ls->flags & PROCESS_DIRS) { | |
1094 | ls->userFunc(ls); | |
1095 | } | |
1096 | if (strcmp(ls->dentry_name, ls->path) && | |
1097 | ls->flags & RECURSIVE) { | |
1098 | remote_ls(ls->dentry_name, | |
1099 | ls->flags, | |
1100 | ls->userFunc, | |
1101 | ls->userData); | |
1102 | } | |
1103 | } else if (ls->flags & PROCESS_FILES) { | |
1104 | ls->userFunc(ls); | |
aa1dbc98 | 1105 | } |
3030baa7 | 1106 | } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) { |
e1f33efe KK |
1107 | char *path = ctx->cdata; |
1108 | if (*ctx->cdata == 'h') { | |
1109 | path = strstr(path, "//"); | |
1110 | if (path) { | |
1111 | path = strchr(path+2, '/'); | |
1112 | } | |
1113 | } | |
1114 | if (path) { | |
7b5201a6 | 1115 | path += repo->path_len; |
20642801 | 1116 | ls->dentry_name = xstrdup(path); |
e1f33efe | 1117 | } |
3030baa7 NH |
1118 | } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) { |
1119 | ls->dentry_flags |= IS_DIR; | |
aa1dbc98 | 1120 | } |
3030baa7 | 1121 | } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) { |
4cac42b1 | 1122 | free(ls->dentry_name); |
3030baa7 NH |
1123 | ls->dentry_name = NULL; |
1124 | ls->dentry_flags = 0; | |
aa1dbc98 | 1125 | } |
aa1dbc98 NH |
1126 | } |
1127 | ||
20642801 JS |
1128 | /* |
1129 | * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it | |
1130 | * should _only_ heed the information from that file, instead of trying to | |
1131 | * determine the refs from the remote file system (badly: it does not even | |
1132 | * know about packed-refs). | |
1133 | */ | |
3030baa7 NH |
1134 | static void remote_ls(const char *path, int flags, |
1135 | void (*userFunc)(struct remote_ls_ctx *ls), | |
1136 | void *userData) | |
aa1dbc98 | 1137 | { |
7b5201a6 | 1138 | char *url = xmalloc(strlen(repo->url) + strlen(path) + 1); |
aa1dbc98 | 1139 | struct active_request_slot *slot; |
baa7b67d | 1140 | struct slot_results results; |
028c2976 MH |
1141 | struct strbuf in_buffer = STRBUF_INIT; |
1142 | struct buffer out_buffer = { STRBUF_INIT, 0 }; | |
aa1dbc98 NH |
1143 | struct curl_slist *dav_headers = NULL; |
1144 | struct xml_ctx ctx; | |
3030baa7 NH |
1145 | struct remote_ls_ctx ls; |
1146 | ||
1147 | ls.flags = flags; | |
9befac47 | 1148 | ls.path = xstrdup(path); |
3030baa7 NH |
1149 | ls.dentry_name = NULL; |
1150 | ls.dentry_flags = 0; | |
1151 | ls.userData = userData; | |
1152 | ls.userFunc = userFunc; | |
aa1dbc98 | 1153 | |
7b5201a6 | 1154 | sprintf(url, "%s%s", repo->url, path); |
aa1dbc98 | 1155 | |
028c2976 | 1156 | strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST); |
aa1dbc98 NH |
1157 | |
1158 | dav_headers = curl_slist_append(dav_headers, "Depth: 1"); | |
1159 | dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); | |
1160 | ||
1161 | slot = get_active_slot(); | |
baa7b67d | 1162 | slot->results = &results; |
aa1dbc98 | 1163 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); |
028c2976 | 1164 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len); |
aa1dbc98 | 1165 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); |
3944ba0c MS |
1166 | #ifndef NO_CURL_IOCTL |
1167 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer); | |
1168 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer); | |
1169 | #endif | |
aa1dbc98 NH |
1170 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); |
1171 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
1172 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
1173 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
1174 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND); | |
1175 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1176 | ||
1177 | if (start_active_slot(slot)) { | |
1178 | run_active_slot(slot); | |
baa7b67d | 1179 | if (results.curl_result == CURLE_OK) { |
472b2570 MH |
1180 | XML_Parser parser = XML_ParserCreate(NULL); |
1181 | enum XML_Status result; | |
aa1dbc98 NH |
1182 | ctx.name = xcalloc(10, 1); |
1183 | ctx.len = 0; | |
1184 | ctx.cdata = NULL; | |
3030baa7 NH |
1185 | ctx.userFunc = handle_remote_ls_ctx; |
1186 | ctx.userData = &ls; | |
aa1dbc98 NH |
1187 | XML_SetUserData(parser, &ctx); |
1188 | XML_SetElementHandler(parser, xml_start_tag, | |
1189 | xml_end_tag); | |
1190 | XML_SetCharacterDataHandler(parser, xml_cdata); | |
028c2976 MH |
1191 | result = XML_Parse(parser, in_buffer.buf, |
1192 | in_buffer.len, 1); | |
aa1dbc98 NH |
1193 | free(ctx.name); |
1194 | ||
1195 | if (result != XML_STATUS_OK) { | |
1196 | fprintf(stderr, "XML error: %s\n", | |
1197 | XML_ErrorString( | |
1198 | XML_GetErrorCode(parser))); | |
1199 | } | |
472b2570 | 1200 | XML_ParserFree(parser); |
aa1dbc98 NH |
1201 | } |
1202 | } else { | |
3030baa7 | 1203 | fprintf(stderr, "Unable to start PROPFIND request\n"); |
aa1dbc98 NH |
1204 | } |
1205 | ||
3030baa7 | 1206 | free(ls.path); |
aa1dbc98 | 1207 | free(url); |
028c2976 MH |
1208 | strbuf_release(&out_buffer.buf); |
1209 | strbuf_release(&in_buffer); | |
aa1dbc98 NH |
1210 | curl_slist_free_all(dav_headers); |
1211 | } | |
1212 | ||
3030baa7 NH |
1213 | static void get_remote_object_list(unsigned char parent) |
1214 | { | |
1215 | char path[] = "objects/XX/"; | |
1216 | static const char hex[] = "0123456789abcdef"; | |
1217 | unsigned int val = parent; | |
1218 | ||
1219 | path[8] = hex[val >> 4]; | |
1220 | path[9] = hex[val & 0xf]; | |
1221 | remote_dir_exists[val] = 0; | |
1222 | remote_ls(path, (PROCESS_FILES | PROCESS_DIRS), | |
1223 | process_ls_object, &val); | |
1224 | } | |
1225 | ||
acf59575 | 1226 | static int locking_available(void) |
58e60dd2 NH |
1227 | { |
1228 | struct active_request_slot *slot; | |
baa7b67d | 1229 | struct slot_results results; |
028c2976 MH |
1230 | struct strbuf in_buffer = STRBUF_INIT; |
1231 | struct buffer out_buffer = { STRBUF_INIT, 0 }; | |
58e60dd2 | 1232 | struct curl_slist *dav_headers = NULL; |
acf59575 NH |
1233 | struct xml_ctx ctx; |
1234 | int lock_flags = 0; | |
519d05be | 1235 | char *escaped; |
58e60dd2 | 1236 | |
519d05be MH |
1237 | escaped = xml_entities(repo->url); |
1238 | strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped); | |
1239 | free(escaped); | |
58e60dd2 NH |
1240 | |
1241 | dav_headers = curl_slist_append(dav_headers, "Depth: 0"); | |
1242 | dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml"); | |
baa7b67d | 1243 | |
58e60dd2 | 1244 | slot = get_active_slot(); |
baa7b67d | 1245 | slot->results = &results; |
58e60dd2 | 1246 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); |
028c2976 | 1247 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len); |
58e60dd2 | 1248 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); |
3944ba0c MS |
1249 | #ifndef NO_CURL_IOCTL |
1250 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer); | |
1251 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer); | |
1252 | #endif | |
58e60dd2 | 1253 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer); |
29508e1e | 1254 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
7b5201a6 | 1255 | curl_easy_setopt(slot->curl, CURLOPT_URL, repo->url); |
58e60dd2 NH |
1256 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); |
1257 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PROPFIND); | |
1258 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1259 | ||
1260 | if (start_active_slot(slot)) { | |
1261 | run_active_slot(slot); | |
baa7b67d | 1262 | if (results.curl_result == CURLE_OK) { |
472b2570 MH |
1263 | XML_Parser parser = XML_ParserCreate(NULL); |
1264 | enum XML_Status result; | |
acf59575 NH |
1265 | ctx.name = xcalloc(10, 1); |
1266 | ctx.len = 0; | |
1267 | ctx.cdata = NULL; | |
1268 | ctx.userFunc = handle_lockprop_ctx; | |
1269 | ctx.userData = &lock_flags; | |
1270 | XML_SetUserData(parser, &ctx); | |
1271 | XML_SetElementHandler(parser, xml_start_tag, | |
1272 | xml_end_tag); | |
028c2976 MH |
1273 | result = XML_Parse(parser, in_buffer.buf, |
1274 | in_buffer.len, 1); | |
acf59575 NH |
1275 | free(ctx.name); |
1276 | ||
1277 | if (result != XML_STATUS_OK) { | |
1278 | fprintf(stderr, "XML error: %s\n", | |
1279 | XML_ErrorString( | |
1280 | XML_GetErrorCode(parser))); | |
1281 | lock_flags = 0; | |
1282 | } | |
472b2570 | 1283 | XML_ParserFree(parser); |
325ce395 | 1284 | if (!lock_flags) |
d5c87cb4 | 1285 | error("no DAV locking support on %s", |
7b5201a6 | 1286 | repo->url); |
325ce395 JH |
1287 | |
1288 | } else { | |
1289 | error("Cannot access URL %s, return code %d", | |
7b5201a6 | 1290 | repo->url, results.curl_result); |
325ce395 | 1291 | lock_flags = 0; |
58e60dd2 | 1292 | } |
58e60dd2 | 1293 | } else { |
7b5201a6 | 1294 | error("Unable to start PROPFIND request on %s", repo->url); |
58e60dd2 NH |
1295 | } |
1296 | ||
028c2976 MH |
1297 | strbuf_release(&out_buffer.buf); |
1298 | strbuf_release(&in_buffer); | |
acf59575 NH |
1299 | curl_slist_free_all(dav_headers); |
1300 | ||
1301 | return lock_flags; | |
58e60dd2 NH |
1302 | } |
1303 | ||
b5bf7cd6 | 1304 | static struct object_list **add_one_object(struct object *obj, struct object_list **p) |
1f1e895f LT |
1305 | { |
1306 | struct object_list *entry = xmalloc(sizeof(struct object_list)); | |
1307 | entry->item = obj; | |
1308 | entry->next = *p; | |
1309 | *p = entry; | |
1310 | return &entry->next; | |
1311 | } | |
1312 | ||
aa1dbc98 NH |
1313 | static struct object_list **process_blob(struct blob *blob, |
1314 | struct object_list **p, | |
1315 | struct name_path *path, | |
1316 | const char *name) | |
58e60dd2 | 1317 | { |
aa1dbc98 | 1318 | struct object *obj = &blob->object; |
58e60dd2 | 1319 | |
aa1dbc98 NH |
1320 | obj->flags |= LOCAL; |
1321 | ||
1322 | if (obj->flags & (UNINTERESTING | SEEN)) | |
1323 | return p; | |
1324 | ||
1325 | obj->flags |= SEEN; | |
1f1e895f | 1326 | return add_one_object(obj, p); |
aa1dbc98 NH |
1327 | } |
1328 | ||
1329 | static struct object_list **process_tree(struct tree *tree, | |
1330 | struct object_list **p, | |
1331 | struct name_path *path, | |
1332 | const char *name) | |
1333 | { | |
1334 | struct object *obj = &tree->object; | |
2d9c58c6 | 1335 | struct tree_desc desc; |
4c068a98 | 1336 | struct name_entry entry; |
aa1dbc98 NH |
1337 | struct name_path me; |
1338 | ||
1339 | obj->flags |= LOCAL; | |
1340 | ||
1341 | if (obj->flags & (UNINTERESTING | SEEN)) | |
1342 | return p; | |
1343 | if (parse_tree(tree) < 0) | |
1344 | die("bad tree object %s", sha1_to_hex(obj->sha1)); | |
1345 | ||
1346 | obj->flags |= SEEN; | |
9befac47 | 1347 | name = xstrdup(name); |
1f1e895f | 1348 | p = add_one_object(obj, p); |
aa1dbc98 NH |
1349 | me.up = path; |
1350 | me.elem = name; | |
1351 | me.elem_len = strlen(name); | |
2d9c58c6 | 1352 | |
6fda5e51 | 1353 | init_tree_desc(&desc, tree->buffer, tree->size); |
2d9c58c6 | 1354 | |
1bbeb4c6 JS |
1355 | while (tree_entry(&desc, &entry)) |
1356 | switch (object_type(entry.mode)) { | |
1357 | case OBJ_TREE: | |
4c068a98 | 1358 | p = process_tree(lookup_tree(entry.sha1), p, &me, name); |
1bbeb4c6 JS |
1359 | break; |
1360 | case OBJ_BLOB: | |
4c068a98 | 1361 | p = process_blob(lookup_blob(entry.sha1), p, &me, name); |
1bbeb4c6 JS |
1362 | break; |
1363 | default: | |
1364 | /* Subproject commit - not in this repository */ | |
1365 | break; | |
1366 | } | |
1367 | ||
2d9c58c6 LT |
1368 | free(tree->buffer); |
1369 | tree->buffer = NULL; | |
aa1dbc98 | 1370 | return p; |
58e60dd2 NH |
1371 | } |
1372 | ||
1a703cba | 1373 | static int get_delta(struct rev_info *revs, struct remote_lock *lock) |
58e60dd2 | 1374 | { |
1f1e895f | 1375 | int i; |
58e60dd2 | 1376 | struct commit *commit; |
1f1e895f | 1377 | struct object_list **p = &objects; |
1a703cba | 1378 | int count = 0; |
58e60dd2 | 1379 | |
aa1dbc98 NH |
1380 | while ((commit = get_revision(revs)) != NULL) { |
1381 | p = process_tree(commit->tree, p, NULL, ""); | |
1382 | commit->object.flags |= LOCAL; | |
1383 | if (!(commit->object.flags & UNINTERESTING)) | |
1a703cba | 1384 | count += add_send_request(&commit->object, lock); |
aa1dbc98 | 1385 | } |
58e60dd2 | 1386 | |
1f1e895f LT |
1387 | for (i = 0; i < revs->pending.nr; i++) { |
1388 | struct object_array_entry *entry = revs->pending.objects + i; | |
1389 | struct object *obj = entry->item; | |
1390 | const char *name = entry->name; | |
58e60dd2 | 1391 | |
aa1dbc98 NH |
1392 | if (obj->flags & (UNINTERESTING | SEEN)) |
1393 | continue; | |
1974632c | 1394 | if (obj->type == OBJ_TAG) { |
aa1dbc98 | 1395 | obj->flags |= SEEN; |
1f1e895f | 1396 | p = add_one_object(obj, p); |
aa1dbc98 | 1397 | continue; |
58e60dd2 | 1398 | } |
1974632c | 1399 | if (obj->type == OBJ_TREE) { |
aa1dbc98 NH |
1400 | p = process_tree((struct tree *)obj, p, NULL, name); |
1401 | continue; | |
58e60dd2 | 1402 | } |
1974632c | 1403 | if (obj->type == OBJ_BLOB) { |
aa1dbc98 NH |
1404 | p = process_blob((struct blob *)obj, p, NULL, name); |
1405 | continue; | |
58e60dd2 | 1406 | } |
aa1dbc98 NH |
1407 | die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name); |
1408 | } | |
1409 | ||
1410 | while (objects) { | |
1411 | if (!(objects->item->flags & UNINTERESTING)) | |
1a703cba | 1412 | count += add_send_request(objects->item, lock); |
aa1dbc98 | 1413 | objects = objects->next; |
58e60dd2 | 1414 | } |
1a703cba NH |
1415 | |
1416 | return count; | |
58e60dd2 NH |
1417 | } |
1418 | ||
aa1dbc98 | 1419 | static int update_remote(unsigned char *sha1, struct remote_lock *lock) |
58e60dd2 NH |
1420 | { |
1421 | struct active_request_slot *slot; | |
baa7b67d | 1422 | struct slot_results results; |
028c2976 | 1423 | struct buffer out_buffer = { STRBUF_INIT, 0 }; |
b1c7d4aa | 1424 | struct curl_slist *dav_headers; |
58e60dd2 | 1425 | |
b1c7d4aa | 1426 | dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF); |
58e60dd2 | 1427 | |
028c2976 | 1428 | strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1)); |
58e60dd2 NH |
1429 | |
1430 | slot = get_active_slot(); | |
baa7b67d | 1431 | slot->results = &results; |
58e60dd2 | 1432 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &out_buffer); |
028c2976 | 1433 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, out_buffer.buf.len); |
58e60dd2 | 1434 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); |
3944ba0c MS |
1435 | #ifndef NO_CURL_IOCTL |
1436 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer); | |
1437 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &out_buffer); | |
1438 | #endif | |
58e60dd2 NH |
1439 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); |
1440 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); | |
1441 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1442 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
1443 | curl_easy_setopt(slot->curl, CURLOPT_PUT, 1); | |
75187c9d | 1444 | curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url); |
58e60dd2 NH |
1445 | |
1446 | if (start_active_slot(slot)) { | |
1447 | run_active_slot(slot); | |
028c2976 | 1448 | strbuf_release(&out_buffer.buf); |
baa7b67d | 1449 | if (results.curl_result != CURLE_OK) { |
58e60dd2 NH |
1450 | fprintf(stderr, |
1451 | "PUT error: curl result=%d, HTTP code=%ld\n", | |
baa7b67d | 1452 | results.curl_result, results.http_code); |
58e60dd2 NH |
1453 | /* We should attempt recovery? */ |
1454 | return 0; | |
1455 | } | |
1456 | } else { | |
028c2976 | 1457 | strbuf_release(&out_buffer.buf); |
58e60dd2 NH |
1458 | fprintf(stderr, "Unable to start PUT request\n"); |
1459 | return 0; | |
1460 | } | |
1461 | ||
1462 | return 1; | |
1463 | } | |
1464 | ||
6d2bf96e | 1465 | static struct ref *remote_refs; |
aa1dbc98 | 1466 | |
aa1dbc98 NH |
1467 | static void one_remote_ref(char *refname) |
1468 | { | |
1469 | struct ref *ref; | |
197e8951 | 1470 | struct object *obj; |
aa1dbc98 | 1471 | |
59c69c0c | 1472 | ref = alloc_ref(refname); |
c13b2633 | 1473 | |
7b5201a6 | 1474 | if (http_fetch_ref(repo->url, ref) != 0) { |
aa1dbc98 NH |
1475 | fprintf(stderr, |
1476 | "Unable to fetch ref %s from %s\n", | |
7b5201a6 | 1477 | refname, repo->url); |
c13b2633 | 1478 | free(ref); |
aa1dbc98 NH |
1479 | return; |
1480 | } | |
1481 | ||
197e8951 NH |
1482 | /* |
1483 | * Fetch a copy of the object if it doesn't exist locally - it | |
1484 | * may be required for updating server info later. | |
1485 | */ | |
7b5201a6 | 1486 | if (repo->can_update_info_refs && !has_sha1_file(ref->old_sha1)) { |
c13b2633 | 1487 | obj = lookup_unknown_object(ref->old_sha1); |
197e8951 NH |
1488 | if (obj) { |
1489 | fprintf(stderr, " fetch %s for %s\n", | |
c13b2633 | 1490 | sha1_to_hex(ref->old_sha1), refname); |
197e8951 NH |
1491 | add_fetch_request(obj); |
1492 | } | |
1493 | } | |
1494 | ||
6d2bf96e CB |
1495 | ref->next = remote_refs; |
1496 | remote_refs = ref; | |
aa1dbc98 NH |
1497 | } |
1498 | ||
aa1dbc98 NH |
1499 | static void get_dav_remote_heads(void) |
1500 | { | |
3030baa7 | 1501 | remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL); |
aa1dbc98 NH |
1502 | } |
1503 | ||
197e8951 NH |
1504 | static void add_remote_info_ref(struct remote_ls_ctx *ls) |
1505 | { | |
028c2976 | 1506 | struct strbuf *buf = (struct strbuf *)ls->userData; |
197e8951 NH |
1507 | struct object *o; |
1508 | int len; | |
1509 | char *ref_info; | |
c13b2633 DB |
1510 | struct ref *ref; |
1511 | ||
59c69c0c | 1512 | ref = alloc_ref(ls->dentry_name); |
197e8951 | 1513 | |
7b5201a6 | 1514 | if (http_fetch_ref(repo->url, ref) != 0) { |
197e8951 NH |
1515 | fprintf(stderr, |
1516 | "Unable to fetch ref %s from %s\n", | |
7b5201a6 | 1517 | ls->dentry_name, repo->url); |
197e8951 | 1518 | aborted = 1; |
c13b2633 | 1519 | free(ref); |
197e8951 NH |
1520 | return; |
1521 | } | |
1522 | ||
c13b2633 | 1523 | o = parse_object(ref->old_sha1); |
197e8951 NH |
1524 | if (!o) { |
1525 | fprintf(stderr, | |
1526 | "Unable to parse object %s for remote ref %s\n", | |
c13b2633 | 1527 | sha1_to_hex(ref->old_sha1), ls->dentry_name); |
197e8951 | 1528 | aborted = 1; |
c13b2633 | 1529 | free(ref); |
197e8951 NH |
1530 | return; |
1531 | } | |
1532 | ||
1533 | len = strlen(ls->dentry_name) + 42; | |
1534 | ref_info = xcalloc(len + 1, 1); | |
1535 | sprintf(ref_info, "%s %s\n", | |
c13b2633 | 1536 | sha1_to_hex(ref->old_sha1), ls->dentry_name); |
197e8951 NH |
1537 | fwrite_buffer(ref_info, 1, len, buf); |
1538 | free(ref_info); | |
1539 | ||
1974632c | 1540 | if (o->type == OBJ_TAG) { |
197e8951 NH |
1541 | o = deref_tag(o, ls->dentry_name, 0); |
1542 | if (o) { | |
1543 | len = strlen(ls->dentry_name) + 45; | |
1544 | ref_info = xcalloc(len + 1, 1); | |
1545 | sprintf(ref_info, "%s %s^{}\n", | |
1546 | sha1_to_hex(o->sha1), ls->dentry_name); | |
1547 | fwrite_buffer(ref_info, 1, len, buf); | |
1548 | free(ref_info); | |
1549 | } | |
1550 | } | |
c13b2633 | 1551 | free(ref); |
197e8951 NH |
1552 | } |
1553 | ||
1554 | static void update_remote_info_refs(struct remote_lock *lock) | |
1555 | { | |
028c2976 | 1556 | struct buffer buffer = { STRBUF_INIT, 0 }; |
197e8951 NH |
1557 | struct active_request_slot *slot; |
1558 | struct slot_results results; | |
b1c7d4aa | 1559 | struct curl_slist *dav_headers; |
197e8951 | 1560 | |
197e8951 | 1561 | remote_ls("refs/", (PROCESS_FILES | RECURSIVE), |
028c2976 | 1562 | add_remote_info_ref, &buffer.buf); |
197e8951 | 1563 | if (!aborted) { |
b1c7d4aa | 1564 | dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF); |
197e8951 NH |
1565 | |
1566 | slot = get_active_slot(); | |
1567 | slot->results = &results; | |
1568 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, &buffer); | |
028c2976 | 1569 | curl_easy_setopt(slot->curl, CURLOPT_INFILESIZE, buffer.buf.len); |
197e8951 | 1570 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, fread_buffer); |
3944ba0c MS |
1571 | #ifndef NO_CURL_IOCTL |
1572 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer); | |
1573 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, &buffer); | |
1574 | #endif | |
197e8951 NH |
1575 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); |
1576 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_PUT); | |
1577 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers); | |
1578 | curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1); | |
1579 | curl_easy_setopt(slot->curl, CURLOPT_PUT, 1); | |
1580 | curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url); | |
1581 | ||
197e8951 NH |
1582 | if (start_active_slot(slot)) { |
1583 | run_active_slot(slot); | |
1584 | if (results.curl_result != CURLE_OK) { | |
1585 | fprintf(stderr, | |
1586 | "PUT error: curl result=%d, HTTP code=%ld\n", | |
1587 | results.curl_result, results.http_code); | |
1588 | } | |
1589 | } | |
197e8951 | 1590 | } |
028c2976 | 1591 | strbuf_release(&buffer.buf); |
197e8951 NH |
1592 | } |
1593 | ||
1594 | static int remote_exists(const char *path) | |
1595 | { | |
7b5201a6 | 1596 | char *url = xmalloc(strlen(repo->url) + strlen(path) + 1); |
446b941a | 1597 | int ret; |
197e8951 | 1598 | |
7b5201a6 | 1599 | sprintf(url, "%s%s", repo->url, path); |
197e8951 | 1600 | |
446b941a MH |
1601 | switch (http_get_strbuf(url, NULL, 0)) { |
1602 | case HTTP_OK: | |
1603 | ret = 1; | |
1604 | break; | |
1605 | case HTTP_MISSING_TARGET: | |
1606 | ret = 0; | |
1607 | break; | |
1608 | case HTTP_ERROR: | |
1609 | http_error(url, HTTP_ERROR); | |
1610 | default: | |
1611 | ret = -1; | |
197e8951 | 1612 | } |
3a462bc9 MH |
1613 | free(url); |
1614 | return ret; | |
197e8951 NH |
1615 | } |
1616 | ||
554fe20d | 1617 | static void fetch_symref(const char *path, char **symref, unsigned char *sha1) |
3dfaf7bc NH |
1618 | { |
1619 | char *url; | |
028c2976 | 1620 | struct strbuf buffer = STRBUF_INIT; |
3dfaf7bc | 1621 | |
7b5201a6 AK |
1622 | url = xmalloc(strlen(repo->url) + strlen(path) + 1); |
1623 | sprintf(url, "%s%s", repo->url, path); | |
3dfaf7bc | 1624 | |
9af5abd9 MH |
1625 | if (http_get_strbuf(url, &buffer, 0) != HTTP_OK) |
1626 | die("Couldn't get %s for remote symref\n%s", url, | |
1627 | curl_errorstr); | |
3dfaf7bc NH |
1628 | free(url); |
1629 | ||
8e0f7003 | 1630 | free(*symref); |
3dfaf7bc | 1631 | *symref = NULL; |
a8e0d16d | 1632 | hashclr(sha1); |
3dfaf7bc | 1633 | |
028c2976 | 1634 | if (buffer.len == 0) |
3dfaf7bc NH |
1635 | return; |
1636 | ||
1637 | /* If it's a symref, set the refname; otherwise try for a sha1 */ | |
028c2976 MH |
1638 | if (!prefixcmp((char *)buffer.buf, "ref: ")) { |
1639 | *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6); | |
3dfaf7bc | 1640 | } else { |
028c2976 | 1641 | get_sha1_hex(buffer.buf, sha1); |
3dfaf7bc NH |
1642 | } |
1643 | ||
028c2976 | 1644 | strbuf_release(&buffer); |
3dfaf7bc NH |
1645 | } |
1646 | ||
1647 | static int verify_merge_base(unsigned char *head_sha1, unsigned char *branch_sha1) | |
1648 | { | |
61ffbcb9 JS |
1649 | struct commit *head = lookup_commit(head_sha1); |
1650 | struct commit *branch = lookup_commit(branch_sha1); | |
1651 | struct commit_list *merge_bases = get_merge_bases(head, branch, 1); | |
3dfaf7bc | 1652 | |
0bc87ffb | 1653 | return (merge_bases && !merge_bases->next && merge_bases->item == branch); |
3dfaf7bc NH |
1654 | } |
1655 | ||
1656 | static int delete_remote_branch(char *pattern, int force) | |
1657 | { | |
1658 | struct ref *refs = remote_refs; | |
1659 | struct ref *remote_ref = NULL; | |
1660 | unsigned char head_sha1[20]; | |
1661 | char *symref = NULL; | |
1662 | int match; | |
1663 | int patlen = strlen(pattern); | |
1664 | int i; | |
1665 | struct active_request_slot *slot; | |
1666 | struct slot_results results; | |
1667 | char *url; | |
1668 | ||
1669 | /* Find the remote branch(es) matching the specified branch name */ | |
1670 | for (match = 0; refs; refs = refs->next) { | |
1671 | char *name = refs->name; | |
1672 | int namelen = strlen(name); | |
1673 | if (namelen < patlen || | |
1674 | memcmp(name + namelen - patlen, pattern, patlen)) | |
1675 | continue; | |
1676 | if (namelen != patlen && name[namelen - patlen - 1] != '/') | |
1677 | continue; | |
1678 | match++; | |
1679 | remote_ref = refs; | |
1680 | } | |
1681 | if (match == 0) | |
1682 | return error("No remote branch matches %s", pattern); | |
1683 | if (match != 1) | |
1684 | return error("More than one remote branch matches %s", | |
1685 | pattern); | |
1686 | ||
1687 | /* | |
1688 | * Remote HEAD must be a symref (not exactly foolproof; a remote | |
1689 | * symlink to a symref will look like a symref) | |
1690 | */ | |
1691 | fetch_symref("HEAD", &symref, head_sha1); | |
1692 | if (!symref) | |
1693 | return error("Remote HEAD is not a symref"); | |
1694 | ||
1695 | /* Remote branch must not be the remote HEAD */ | |
1696 | for (i=0; symref && i<MAXDEPTH; i++) { | |
1697 | if (!strcmp(remote_ref->name, symref)) | |
1698 | return error("Remote branch %s is the current HEAD", | |
1699 | remote_ref->name); | |
1700 | fetch_symref(symref, &symref, head_sha1); | |
1701 | } | |
1702 | ||
1703 | /* Run extra sanity checks if delete is not forced */ | |
1704 | if (!force) { | |
1705 | /* Remote HEAD must resolve to a known object */ | |
1706 | if (symref) | |
1707 | return error("Remote HEAD symrefs too deep"); | |
0bf8c1f9 | 1708 | if (is_null_sha1(head_sha1)) |
3dfaf7bc NH |
1709 | return error("Unable to resolve remote HEAD"); |
1710 | if (!has_sha1_file(head_sha1)) | |
1711 | return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", sha1_to_hex(head_sha1)); | |
1712 | ||
1713 | /* Remote branch must resolve to a known object */ | |
0bf8c1f9 | 1714 | if (is_null_sha1(remote_ref->old_sha1)) |
3dfaf7bc NH |
1715 | return error("Unable to resolve remote branch %s", |
1716 | remote_ref->name); | |
1717 | if (!has_sha1_file(remote_ref->old_sha1)) | |
1718 | return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, sha1_to_hex(remote_ref->old_sha1)); | |
1719 | ||
1720 | /* Remote branch must be an ancestor of remote HEAD */ | |
1721 | if (!verify_merge_base(head_sha1, remote_ref->old_sha1)) { | |
00ae8289 BF |
1722 | return error("The branch '%s' is not an ancestor " |
1723 | "of your current HEAD.\n" | |
1724 | "If you are sure you want to delete it," | |
1725 | " run:\n\t'git http-push -D %s %s'", | |
7b5201a6 | 1726 | remote_ref->name, repo->url, pattern); |
3dfaf7bc NH |
1727 | } |
1728 | } | |
1729 | ||
1730 | /* Send delete request */ | |
1731 | fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name); | |
6eaf4060 CB |
1732 | if (dry_run) |
1733 | return 0; | |
7b5201a6 AK |
1734 | url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1); |
1735 | sprintf(url, "%s%s", repo->url, remote_ref->name); | |
3dfaf7bc NH |
1736 | slot = get_active_slot(); |
1737 | slot->results = &results; | |
1738 | curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); | |
1739 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null); | |
1740 | curl_easy_setopt(slot->curl, CURLOPT_URL, url); | |
1741 | curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_DELETE); | |
1742 | if (start_active_slot(slot)) { | |
1743 | run_active_slot(slot); | |
1744 | free(url); | |
1745 | if (results.curl_result != CURLE_OK) | |
1746 | return error("DELETE request failed (%d/%ld)\n", | |
1747 | results.curl_result, results.http_code); | |
1748 | } else { | |
1749 | free(url); | |
1750 | return error("Unable to start DELETE request"); | |
1751 | } | |
1752 | ||
1753 | return 0; | |
1754 | } | |
1755 | ||
2af202be | 1756 | static void run_request_queue(void) |
4f66250d TRC |
1757 | { |
1758 | #ifdef USE_CURL_MULTI | |
1759 | is_running_queue = 1; | |
1760 | fill_active_slots(); | |
1761 | add_fill_function(NULL, fill_active_slot); | |
1762 | #endif | |
1763 | do { | |
1764 | finish_all_active_slots(); | |
1765 | #ifdef USE_CURL_MULTI | |
1766 | fill_active_slots(); | |
1767 | #endif | |
1768 | } while (request_queue_head && !aborted); | |
1769 | ||
1770 | #ifdef USE_CURL_MULTI | |
1771 | is_running_queue = 0; | |
1772 | #endif | |
1773 | } | |
1774 | ||
58e60dd2 NH |
1775 | int main(int argc, char **argv) |
1776 | { | |
58e60dd2 NH |
1777 | struct transfer_request *request; |
1778 | struct transfer_request *next_request; | |
1779 | int nr_refspec = 0; | |
1780 | char **refspec = NULL; | |
512d632c | 1781 | struct remote_lock *ref_lock = NULL; |
197e8951 | 1782 | struct remote_lock *info_ref_lock = NULL; |
aa1dbc98 | 1783 | struct rev_info revs; |
3dfaf7bc NH |
1784 | int delete_branch = 0; |
1785 | int force_delete = 0; | |
1a703cba | 1786 | int objects_to_send; |
58e60dd2 NH |
1787 | int rc = 0; |
1788 | int i; | |
8c9e7947 | 1789 | int new_refs; |
454e2025 | 1790 | struct ref *ref, *local_refs; |
44d808c2 | 1791 | struct remote *remote; |
3057ded0 | 1792 | char *rewritten_url = NULL; |
58e60dd2 | 1793 | |
2fb3f6db SP |
1794 | git_extract_argv0_path(argv[0]); |
1795 | ||
7b5201a6 | 1796 | repo = xcalloc(sizeof(*repo), 1); |
58e60dd2 NH |
1797 | |
1798 | argv++; | |
1799 | for (i = 1; i < argc; i++, argv++) { | |
1800 | char *arg = *argv; | |
1801 | ||
1802 | if (*arg == '-') { | |
aa1dbc98 | 1803 | if (!strcmp(arg, "--all")) { |
28b9d6e5 | 1804 | push_all = MATCH_REFS_ALL; |
58e60dd2 NH |
1805 | continue; |
1806 | } | |
1807 | if (!strcmp(arg, "--force")) { | |
1808 | force_all = 1; | |
1809 | continue; | |
1810 | } | |
fe5d1d3e SP |
1811 | if (!strcmp(arg, "--dry-run")) { |
1812 | dry_run = 1; | |
1813 | continue; | |
1814 | } | |
ae4efe19 SP |
1815 | if (!strcmp(arg, "--helper-status")) { |
1816 | helper_status = 1; | |
1817 | continue; | |
1818 | } | |
58e60dd2 NH |
1819 | if (!strcmp(arg, "--verbose")) { |
1820 | push_verbosely = 1; | |
e9176745 | 1821 | http_is_verbose = 1; |
58e60dd2 NH |
1822 | continue; |
1823 | } | |
3dfaf7bc NH |
1824 | if (!strcmp(arg, "-d")) { |
1825 | delete_branch = 1; | |
1826 | continue; | |
1827 | } | |
1828 | if (!strcmp(arg, "-D")) { | |
1829 | delete_branch = 1; | |
1830 | force_delete = 1; | |
1831 | continue; | |
1832 | } | |
548d3464 JN |
1833 | if (!strcmp(arg, "-h")) |
1834 | usage(http_push_usage); | |
58e60dd2 | 1835 | } |
7b5201a6 | 1836 | if (!repo->url) { |
aa1dbc98 | 1837 | char *path = strstr(arg, "//"); |
7b5201a6 AK |
1838 | repo->url = arg; |
1839 | repo->path_len = strlen(arg); | |
aa1dbc98 | 1840 | if (path) { |
7b5201a6 AK |
1841 | repo->path = strchr(path+2, '/'); |
1842 | if (repo->path) | |
1843 | repo->path_len = strlen(repo->path); | |
aa1dbc98 | 1844 | } |
58e60dd2 NH |
1845 | continue; |
1846 | } | |
1847 | refspec = argv; | |
1848 | nr_refspec = argc - i; | |
1849 | break; | |
1850 | } | |
1851 | ||
f854824b GB |
1852 | #ifndef USE_CURL_MULTI |
1853 | die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI"); | |
1854 | #endif | |
1855 | ||
7b5201a6 | 1856 | if (!repo->url) |
3e9fabc8 NH |
1857 | usage(http_push_usage); |
1858 | ||
3dfaf7bc NH |
1859 | if (delete_branch && nr_refspec != 1) |
1860 | die("You must specify only one branch name when deleting a remote branch"); | |
1861 | ||
548d3464 JN |
1862 | setup_git_directory(); |
1863 | ||
aa1dbc98 | 1864 | memset(remote_dir_exists, -1, 256); |
0dd276b8 | 1865 | |
44d808c2 AK |
1866 | /* |
1867 | * Create a minimum remote by hand to give to http_init(), | |
1868 | * primarily to allow it to look at the URL. | |
1869 | */ | |
1870 | remote = xcalloc(sizeof(*remote), 1); | |
1871 | ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc); | |
1872 | remote->url[remote->url_nr++] = repo->url; | |
1873 | http_init(remote); | |
58e60dd2 | 1874 | |
7b5201a6 AK |
1875 | if (repo->url && repo->url[strlen(repo->url)-1] != '/') { |
1876 | rewritten_url = xmalloc(strlen(repo->url)+2); | |
1877 | strcpy(rewritten_url, repo->url); | |
3057ded0 | 1878 | strcat(rewritten_url, "/"); |
7b5201a6 AK |
1879 | repo->path = rewritten_url + (repo->path - repo->url); |
1880 | repo->path_len++; | |
1881 | repo->url = rewritten_url; | |
3057ded0 GB |
1882 | } |
1883 | ||
68862a31 | 1884 | #ifdef USE_CURL_MULTI |
4f66250d | 1885 | is_running_queue = 0; |
68862a31 | 1886 | #endif |
4f66250d | 1887 | |
58e60dd2 | 1888 | /* Verify DAV compliance/lock support */ |
acf59575 | 1889 | if (!locking_available()) { |
58e60dd2 NH |
1890 | rc = 1; |
1891 | goto cleanup; | |
1892 | } | |
1893 | ||
57b235a4 | 1894 | sigchain_push_common(remove_locks_on_signal); |
6a491a17 | 1895 | |
197e8951 | 1896 | /* Check whether the remote has server info files */ |
7b5201a6 AK |
1897 | repo->can_update_info_refs = 0; |
1898 | repo->has_info_refs = remote_exists("info/refs"); | |
1899 | repo->has_info_packs = remote_exists("objects/info/packs"); | |
1900 | if (repo->has_info_refs) { | |
197e8951 NH |
1901 | info_ref_lock = lock_remote("info/refs", LOCK_TIME); |
1902 | if (info_ref_lock) | |
7b5201a6 | 1903 | repo->can_update_info_refs = 1; |
9bdbabad | 1904 | else { |
d5c87cb4 | 1905 | error("cannot lock existing info/refs"); |
9bdbabad GB |
1906 | rc = 1; |
1907 | goto cleanup; | |
1908 | } | |
197e8951 | 1909 | } |
7b5201a6 | 1910 | if (repo->has_info_packs) |
197e8951 NH |
1911 | fetch_indices(); |
1912 | ||
aa1dbc98 | 1913 | /* Get a list of all local and remote heads to validate refspecs */ |
454e2025 | 1914 | local_refs = get_local_heads(); |
aa1dbc98 NH |
1915 | fprintf(stderr, "Fetching remote heads...\n"); |
1916 | get_dav_remote_heads(); | |
4f66250d | 1917 | run_request_queue(); |
aa1dbc98 | 1918 | |
3dfaf7bc NH |
1919 | /* Remove a remote branch if -d or -D was specified */ |
1920 | if (delete_branch) { | |
ae4efe19 | 1921 | if (delete_remote_branch(refspec[0], force_delete) == -1) { |
3dfaf7bc NH |
1922 | fprintf(stderr, "Unable to delete remote branch %s\n", |
1923 | refspec[0]); | |
ae4efe19 SP |
1924 | if (helper_status) |
1925 | printf("error %s cannot remove\n", refspec[0]); | |
1926 | } | |
3dfaf7bc NH |
1927 | goto cleanup; |
1928 | } | |
1929 | ||
aa1dbc98 | 1930 | /* match them up */ |
6d2bf96e | 1931 | if (match_refs(local_refs, &remote_refs, |
9116de59 GB |
1932 | nr_refspec, (const char **) refspec, push_all)) { |
1933 | rc = -1; | |
1934 | goto cleanup; | |
1935 | } | |
aa1dbc98 NH |
1936 | if (!remote_refs) { |
1937 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); | |
ae4efe19 SP |
1938 | if (helper_status) |
1939 | printf("error null no match\n"); | |
9116de59 GB |
1940 | rc = 0; |
1941 | goto cleanup; | |
aa1dbc98 NH |
1942 | } |
1943 | ||
8c9e7947 | 1944 | new_refs = 0; |
aa1dbc98 NH |
1945 | for (ref = remote_refs; ref; ref = ref->next) { |
1946 | char old_hex[60], *new_hex; | |
a80aad7b | 1947 | const char *commit_argv[5]; |
8c9e7947 JH |
1948 | int commit_argc; |
1949 | char *new_sha1_hex, *old_sha1_hex; | |
1950 | ||
aa1dbc98 NH |
1951 | if (!ref->peer_ref) |
1952 | continue; | |
6eaf4060 | 1953 | |
0bf8c1f9 | 1954 | if (is_null_sha1(ref->peer_ref->new_sha1)) { |
6eaf4060 CB |
1955 | if (delete_remote_branch(ref->name, 1) == -1) { |
1956 | error("Could not remove %s", ref->name); | |
ae4efe19 SP |
1957 | if (helper_status) |
1958 | printf("error %s cannot remove\n", ref->name); | |
6eaf4060 CB |
1959 | rc = -4; |
1960 | } | |
ae4efe19 SP |
1961 | else if (helper_status) |
1962 | printf("ok %s\n", ref->name); | |
6eaf4060 CB |
1963 | new_refs++; |
1964 | continue; | |
1965 | } | |
1966 | ||
a89fccd2 | 1967 | if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { |
aa1dbc98 NH |
1968 | if (push_verbosely || 1) |
1969 | fprintf(stderr, "'%s': up-to-date\n", ref->name); | |
ae4efe19 SP |
1970 | if (helper_status) |
1971 | printf("ok %s up to date\n", ref->name); | |
aa1dbc98 NH |
1972 | continue; |
1973 | } | |
1974 | ||
1975 | if (!force_all && | |
0bf8c1f9 | 1976 | !is_null_sha1(ref->old_sha1) && |
aa1dbc98 NH |
1977 | !ref->force) { |
1978 | if (!has_sha1_file(ref->old_sha1) || | |
1979 | !ref_newer(ref->peer_ref->new_sha1, | |
1980 | ref->old_sha1)) { | |
00ae8289 BF |
1981 | /* |
1982 | * We do not have the remote ref, or | |
aa1dbc98 NH |
1983 | * we know that the remote ref is not |
1984 | * an ancestor of what we are trying to | |
1985 | * push. Either way this can be losing | |
1986 | * commits at the remote end and likely | |
1987 | * we were not up to date to begin with. | |
1988 | */ | |
00ae8289 BF |
1989 | error("remote '%s' is not an ancestor of\n" |
1990 | "local '%s'.\n" | |
1991 | "Maybe you are not up-to-date and " | |
aa1dbc98 NH |
1992 | "need to pull first?", |
1993 | ref->name, | |
1994 | ref->peer_ref->name); | |
ae4efe19 SP |
1995 | if (helper_status) |
1996 | printf("error %s non-fast forward\n", ref->name); | |
1a703cba | 1997 | rc = -2; |
aa1dbc98 NH |
1998 | continue; |
1999 | } | |
58e60dd2 | 2000 | } |
e702496e | 2001 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); |
aa1dbc98 NH |
2002 | new_refs++; |
2003 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); | |
2004 | new_hex = sha1_to_hex(ref->new_sha1); | |
2005 | ||
2006 | fprintf(stderr, "updating '%s'", ref->name); | |
2007 | if (strcmp(ref->name, ref->peer_ref->name)) | |
2008 | fprintf(stderr, " using '%s'", ref->peer_ref->name); | |
2009 | fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex); | |
ae4efe19 SP |
2010 | if (dry_run) { |
2011 | if (helper_status) | |
2012 | printf("ok %s\n", ref->name); | |
fe5d1d3e | 2013 | continue; |
ae4efe19 | 2014 | } |
58e60dd2 NH |
2015 | |
2016 | /* Lock remote branch ref */ | |
aa1dbc98 NH |
2017 | ref_lock = lock_remote(ref->name, LOCK_TIME); |
2018 | if (ref_lock == NULL) { | |
58e60dd2 | 2019 | fprintf(stderr, "Unable to lock remote branch %s\n", |
aa1dbc98 | 2020 | ref->name); |
ae4efe19 SP |
2021 | if (helper_status) |
2022 | printf("error %s lock error\n", ref->name); | |
58e60dd2 NH |
2023 | rc = 1; |
2024 | continue; | |
2025 | } | |
2026 | ||
aa1dbc98 | 2027 | /* Set up revision info for this refspec */ |
8c9e7947 | 2028 | commit_argc = 3; |
9befac47 | 2029 | new_sha1_hex = xstrdup(sha1_to_hex(ref->new_sha1)); |
8c9e7947 | 2030 | old_sha1_hex = NULL; |
5241bfe6 NH |
2031 | commit_argv[1] = "--objects"; |
2032 | commit_argv[2] = new_sha1_hex; | |
0bf8c1f9 | 2033 | if (!push_all && !is_null_sha1(ref->old_sha1)) { |
aa1dbc98 NH |
2034 | old_sha1_hex = xmalloc(42); |
2035 | sprintf(old_sha1_hex, "^%s", | |
2036 | sha1_to_hex(ref->old_sha1)); | |
5241bfe6 | 2037 | commit_argv[3] = old_sha1_hex; |
aa1dbc98 | 2038 | commit_argc++; |
58e60dd2 | 2039 | } |
a80aad7b | 2040 | commit_argv[commit_argc] = NULL; |
db6296a5 | 2041 | init_revisions(&revs, setup_git_directory()); |
aa1dbc98 | 2042 | setup_revisions(commit_argc, commit_argv, &revs, NULL); |
d633c882 | 2043 | revs.edge_hint = 0; /* just in case */ |
aa1dbc98 NH |
2044 | free(new_sha1_hex); |
2045 | if (old_sha1_hex) { | |
2046 | free(old_sha1_hex); | |
2047 | commit_argv[1] = NULL; | |
58e60dd2 NH |
2048 | } |
2049 | ||
aa1dbc98 | 2050 | /* Generate a list of objects that need to be pushed */ |
58e60dd2 | 2051 | pushing = 0; |
3d51e1b5 MK |
2052 | if (prepare_revision_walk(&revs)) |
2053 | die("revision walk setup failed"); | |
d633c882 | 2054 | mark_edges_uninteresting(revs.commits, &revs, NULL); |
1a703cba | 2055 | objects_to_send = get_delta(&revs, ref_lock); |
29508e1e | 2056 | finish_all_active_slots(); |
58e60dd2 NH |
2057 | |
2058 | /* Push missing objects to remote, this would be a | |
2059 | convenient time to pack them first if appropriate. */ | |
2060 | pushing = 1; | |
1a703cba NH |
2061 | if (objects_to_send) |
2062 | fprintf(stderr, " sending %d objects\n", | |
2063 | objects_to_send); | |
4f66250d TRC |
2064 | |
2065 | run_request_queue(); | |
58e60dd2 NH |
2066 | |
2067 | /* Update the remote branch if all went well */ | |
7fea9c55 | 2068 | if (aborted || !update_remote(ref->new_sha1, ref_lock)) |
aa1dbc98 | 2069 | rc = 1; |
58e60dd2 | 2070 | |
aa1dbc98 NH |
2071 | if (!rc) |
2072 | fprintf(stderr, " done\n"); | |
ae4efe19 SP |
2073 | if (helper_status) |
2074 | printf("%s %s\n", !rc ? "ok" : "error", ref->name); | |
aa1dbc98 | 2075 | unlock_remote(ref_lock); |
512d632c | 2076 | check_locks(); |
58e60dd2 NH |
2077 | } |
2078 | ||
197e8951 | 2079 | /* Update remote server info if appropriate */ |
7b5201a6 AK |
2080 | if (repo->has_info_refs && new_refs) { |
2081 | if (info_ref_lock && repo->can_update_info_refs) { | |
197e8951 | 2082 | fprintf(stderr, "Updating remote server info\n"); |
fe5d1d3e SP |
2083 | if (!dry_run) |
2084 | update_remote_info_refs(info_ref_lock); | |
197e8951 NH |
2085 | } else { |
2086 | fprintf(stderr, "Unable to update server info\n"); | |
2087 | } | |
2088 | } | |
197e8951 | 2089 | |
58e60dd2 | 2090 | cleanup: |
8e0f7003 | 2091 | free(rewritten_url); |
9116de59 GB |
2092 | if (info_ref_lock) |
2093 | unlock_remote(info_ref_lock); | |
7b5201a6 | 2094 | free(repo); |
58e60dd2 | 2095 | |
29508e1e | 2096 | http_cleanup(); |
58e60dd2 NH |
2097 | |
2098 | request = request_queue_head; | |
2099 | while (request != NULL) { | |
2100 | next_request = request->next; | |
2101 | release_request(request); | |
58e60dd2 NH |
2102 | request = next_request; |
2103 | } | |
2104 | ||
58e60dd2 NH |
2105 | return rc; |
2106 | } |