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