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