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