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