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