13 static const char http_push_usage
[] =
14 "git-http-push [--complete] [--force] [--verbose] <url> <ref> [<ref>...]\n";
21 #define XML_STATUS_OK 1
22 #define XML_STATUS_ERROR 0
25 #define RANGE_HEADER_SIZE 30
27 /* DAV method names and request body templates */
28 #define DAV_LOCK "LOCK"
29 #define DAV_MKCOL "MKCOL"
30 #define DAV_MOVE "MOVE"
31 #define DAV_PROPFIND "PROPFIND"
33 #define DAV_UNLOCK "UNLOCK"
34 #define PROPFIND_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>"
35 #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>"
38 #define LOCK_REFRESH 30
40 static int pushing
= 0;
41 static int aborted
= 0;
42 static char remote_dir_exists
[256];
44 static struct curl_slist
*no_pragma_header
;
45 static struct curl_slist
*default_headers
;
47 static int push_verbosely
= 0;
48 static int push_all
= 0;
49 static int force_all
= 0;
54 struct packed_git
*packs
;
57 static struct repo
*remote
= NULL
;
70 struct transfer_request
72 unsigned char sha1
[20];
75 struct active_lock
*lock
;
76 struct curl_slist
*headers
;
78 char filename
[PATH_MAX
];
79 char tmpfile
[PATH_MAX
];
80 enum transfer_state state
;
82 char errorstr
[CURL_ERROR_SIZE
];
84 unsigned char real_sha1
[20];
89 struct active_request_slot
*slot
;
90 struct transfer_request
*next
;
93 static struct transfer_request
*request_queue_head
= NULL
;
102 int ctx_locktoken_href
;
118 int lock_exclusive_write
;
121 static void finish_request(struct transfer_request
*request
);
123 static void process_response(void *callback_data
)
125 struct transfer_request
*request
=
126 (struct transfer_request
*)callback_data
;
128 finish_request(request
);
131 static void start_check(struct transfer_request
*request
)
133 char *hex
= sha1_to_hex(request
->sha1
);
134 struct active_request_slot
*slot
;
137 request
->url
= xmalloc(strlen(remote
->url
) + 55);
138 strcpy(request
->url
, remote
->url
);
139 posn
= request
->url
+ strlen(remote
->url
);
140 strcpy(posn
, "objects/");
142 memcpy(posn
, hex
, 2);
145 strcpy(posn
, hex
+ 2);
147 slot
= get_active_slot();
148 slot
->callback_func
= process_response
;
149 slot
->callback_data
= request
;
150 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
151 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
152 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
154 if (start_active_slot(slot
)) {
155 request
->slot
= slot
;
156 request
->state
= RUN_HEAD
;
158 request
->state
= ABORTED
;
163 static void start_mkcol(struct transfer_request
*request
)
165 char *hex
= sha1_to_hex(request
->sha1
);
166 struct active_request_slot
*slot
;
169 request
->url
= xmalloc(strlen(remote
->url
) + 13);
170 strcpy(request
->url
, remote
->url
);
171 posn
= request
->url
+ strlen(remote
->url
);
172 strcpy(posn
, "objects/");
174 memcpy(posn
, hex
, 2);
178 slot
= get_active_slot();
179 slot
->callback_func
= process_response
;
180 slot
->callback_data
= request
;
181 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
182 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
183 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
184 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
185 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
187 if (start_active_slot(slot
)) {
188 request
->slot
= slot
;
189 request
->state
= RUN_MKCOL
;
191 request
->state
= ABORTED
;
196 static void start_put(struct transfer_request
*request
)
198 char *hex
= sha1_to_hex(request
->sha1
);
199 struct active_request_slot
*slot
;
209 unpacked
= read_sha1_file(request
->sha1
, type
, &len
);
210 hdrlen
= sprintf(hdr
, "%s %lu", type
, len
) + 1;
213 memset(&stream
, 0, sizeof(stream
));
214 deflateInit(&stream
, Z_BEST_COMPRESSION
);
215 size
= deflateBound(&stream
, len
+ hdrlen
);
216 request
->buffer
.buffer
= xmalloc(size
);
219 stream
.next_out
= request
->buffer
.buffer
;
220 stream
.avail_out
= size
;
223 stream
.next_in
= (void *)hdr
;
224 stream
.avail_in
= hdrlen
;
225 while (deflate(&stream
, 0) == Z_OK
)
228 /* Then the data itself.. */
229 stream
.next_in
= unpacked
;
230 stream
.avail_in
= len
;
231 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
236 request
->buffer
.size
= stream
.total_out
;
237 request
->buffer
.posn
= 0;
239 if (request
->url
!= NULL
)
241 request
->url
= xmalloc(strlen(remote
->url
) +
242 strlen(request
->lock
->token
) + 51);
243 strcpy(request
->url
, remote
->url
);
244 posn
= request
->url
+ strlen(remote
->url
);
245 strcpy(posn
, "objects/");
247 memcpy(posn
, hex
, 2);
250 strcpy(posn
, hex
+ 2);
251 request
->dest
= xmalloc(strlen(request
->url
) + 14);
252 sprintf(request
->dest
, "Destination: %s", request
->url
);
255 strcpy(posn
, request
->lock
->token
);
257 slot
= get_active_slot();
258 slot
->callback_func
= process_response
;
259 slot
->callback_data
= request
;
260 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &request
->buffer
);
261 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, request
->buffer
.size
);
262 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
263 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
264 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
265 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
266 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
267 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
268 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
270 if (start_active_slot(slot
)) {
271 request
->slot
= slot
;
272 request
->state
= RUN_PUT
;
274 request
->state
= ABORTED
;
279 static void start_move(struct transfer_request
*request
)
281 struct active_request_slot
*slot
;
282 struct curl_slist
*dav_headers
= NULL
;
284 slot
= get_active_slot();
285 slot
->callback_func
= process_response
;
286 slot
->callback_data
= request
;
287 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1); /* undo PUT setup */
288 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MOVE
);
289 dav_headers
= curl_slist_append(dav_headers
, request
->dest
);
290 dav_headers
= curl_slist_append(dav_headers
, "Overwrite: T");
291 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
292 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
293 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, request
->url
);
295 if (start_active_slot(slot
)) {
296 request
->slot
= slot
;
297 request
->state
= RUN_MOVE
;
299 request
->state
= ABORTED
;
304 static int refresh_lock(struct active_lock
*lock
)
306 struct active_request_slot
*slot
;
308 char timeout_header
[25];
309 struct curl_slist
*dav_headers
= NULL
;
312 lock
->refreshing
= 1;
314 if_header
= xmalloc(strlen(lock
->token
) + 25);
315 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
316 sprintf(timeout_header
, "Timeout: Second-%ld", lock
->timeout
);
317 dav_headers
= curl_slist_append(dav_headers
, if_header
);
318 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
320 slot
= get_active_slot();
321 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
322 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
323 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
324 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
325 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
327 if (start_active_slot(slot
)) {
328 run_active_slot(slot
);
329 if (slot
->curl_result
!= CURLE_OK
) {
330 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
332 lock
->start_time
= time(NULL
);
337 lock
->refreshing
= 0;
338 curl_slist_free_all(dav_headers
);
344 static void finish_request(struct transfer_request
*request
)
346 time_t current_time
= time(NULL
);
349 request
->curl_result
= request
->slot
->curl_result
;
350 request
->http_code
= request
->slot
->http_code
;
351 request
->slot
= NULL
;
353 /* Refresh the lock if it is close to timing out */
354 time_remaining
= request
->lock
->start_time
+ request
->lock
->timeout
356 if (time_remaining
< LOCK_REFRESH
&& !request
->lock
->refreshing
) {
357 if (!refresh_lock(request
->lock
)) {
358 fprintf(stderr
, "Unable to refresh remote lock\n");
363 if (request
->headers
!= NULL
)
364 curl_slist_free_all(request
->headers
);
365 if (request
->state
== RUN_HEAD
) {
366 if (request
->http_code
== 404) {
367 request
->state
= NEED_PUSH
;
368 } else if (request
->curl_result
== CURLE_OK
) {
369 remote_dir_exists
[request
->sha1
[0]] = 1;
370 request
->state
= COMPLETE
;
372 fprintf(stderr
, "HEAD %s failed, aborting (%d/%ld)\n",
373 sha1_to_hex(request
->sha1
),
374 request
->curl_result
, request
->http_code
);
375 request
->state
= ABORTED
;
378 } else if (request
->state
== RUN_MKCOL
) {
379 if (request
->curl_result
== CURLE_OK
||
380 request
->http_code
== 405) {
381 remote_dir_exists
[request
->sha1
[0]] = 1;
384 fprintf(stderr
, "MKCOL %s failed, aborting (%d/%ld)\n",
385 sha1_to_hex(request
->sha1
),
386 request
->curl_result
, request
->http_code
);
387 request
->state
= ABORTED
;
390 } else if (request
->state
== RUN_PUT
) {
391 if (request
->curl_result
== CURLE_OK
) {
394 fprintf(stderr
, "PUT %s failed, aborting (%d/%ld)\n",
395 sha1_to_hex(request
->sha1
),
396 request
->curl_result
, request
->http_code
);
397 request
->state
= ABORTED
;
400 } else if (request
->state
== RUN_MOVE
) {
401 if (request
->curl_result
== CURLE_OK
) {
405 sha1_to_hex(request
->sha1
));
406 request
->state
= COMPLETE
;
408 fprintf(stderr
, "MOVE %s failed, aborting (%d/%ld)\n",
409 sha1_to_hex(request
->sha1
),
410 request
->curl_result
, request
->http_code
);
411 request
->state
= ABORTED
;
417 static void release_request(struct transfer_request
*request
)
419 struct transfer_request
*entry
= request_queue_head
;
421 if (request
== request_queue_head
) {
422 request_queue_head
= request
->next
;
424 while (entry
->next
!= NULL
&& entry
->next
!= request
)
426 if (entry
->next
== request
)
427 entry
->next
= entry
->next
->next
;
434 void fill_active_slots(void)
436 struct transfer_request
*request
= request_queue_head
;
437 struct active_request_slot
*slot
= active_queue_head
;
443 while (active_requests
< max_requests
&& request
!= NULL
) {
444 if (!pushing
&& request
->state
== NEED_CHECK
) {
445 start_check(request
);
446 curl_multi_perform(curlm
, &num_transfers
);
447 } else if (pushing
&& request
->state
== NEED_PUSH
) {
448 if (remote_dir_exists
[request
->sha1
[0]])
451 start_mkcol(request
);
452 curl_multi_perform(curlm
, &num_transfers
);
454 request
= request
->next
;
457 while (slot
!= NULL
) {
458 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
459 curl_easy_cleanup(slot
->curl
);
466 static void add_request(unsigned char *sha1
, struct active_lock
*lock
)
468 struct transfer_request
*request
= request_queue_head
;
469 struct packed_git
*target
;
471 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
472 request
= request
->next
;
476 target
= find_sha1_pack(sha1
, remote
->packs
);
480 request
= xmalloc(sizeof(*request
));
481 memcpy(request
->sha1
, sha1
, 20);
483 request
->lock
= lock
;
484 request
->headers
= NULL
;
485 request
->state
= NEED_CHECK
;
486 request
->next
= request_queue_head
;
487 request_queue_head
= request
;
493 static int fetch_index(unsigned char *sha1
)
495 char *hex
= sha1_to_hex(sha1
);
498 char tmpfile
[PATH_MAX
];
500 char range
[RANGE_HEADER_SIZE
];
501 struct curl_slist
*range_header
= NULL
;
504 struct active_request_slot
*slot
;
506 /* Don't use the index if the pack isn't there */
507 url
= xmalloc(strlen(remote
->url
) + 65);
508 sprintf(url
, "%s/objects/pack/pack-%s.pack", remote
->url
, hex
);
509 slot
= get_active_slot();
510 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
511 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
512 if (start_active_slot(slot
)) {
513 run_active_slot(slot
);
514 if (slot
->curl_result
!= CURLE_OK
) {
516 return error("Unable to verify pack %s is available",
520 return error("Unable to start request");
523 if (has_pack_index(sha1
))
527 fprintf(stderr
, "Getting index for pack %s\n", hex
);
529 sprintf(url
, "%s/objects/pack/pack-%s.idx", remote
->url
, hex
);
531 filename
= sha1_pack_index_name(sha1
);
532 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
533 indexfile
= fopen(tmpfile
, "a");
535 return error("Unable to open local file %s for pack index",
538 slot
= get_active_slot();
539 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
540 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
541 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
542 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
543 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
544 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
545 slot
->local
= indexfile
;
547 /* If there is data present from a previous transfer attempt,
548 resume where it left off */
549 prev_posn
= ftell(indexfile
);
553 "Resuming fetch of index for pack %s at byte %ld\n",
555 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
556 range_header
= curl_slist_append(range_header
, range
);
557 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
560 if (start_active_slot(slot
)) {
561 run_active_slot(slot
);
562 if (slot
->curl_result
!= CURLE_OK
) {
565 return error("Unable to get pack index %s\n%s", url
,
570 return error("Unable to start request");
576 return move_temp_to_file(tmpfile
, filename
);
579 static int setup_index(unsigned char *sha1
)
581 struct packed_git
*new_pack
;
583 if (fetch_index(sha1
))
586 new_pack
= parse_pack_index(sha1
);
587 new_pack
->next
= remote
->packs
;
588 remote
->packs
= new_pack
;
592 static int fetch_indices(void)
594 unsigned char sha1
[20];
596 struct buffer buffer
;
600 struct active_request_slot
*slot
;
602 data
= xmalloc(4096);
603 memset(data
, 0, 4096);
606 buffer
.buffer
= data
;
609 fprintf(stderr
, "Getting pack list\n");
611 url
= xmalloc(strlen(remote
->url
) + 21);
612 sprintf(url
, "%s/objects/info/packs", remote
->url
);
614 slot
= get_active_slot();
615 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
616 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
617 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
618 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
619 if (start_active_slot(slot
)) {
620 run_active_slot(slot
);
621 if (slot
->curl_result
!= CURLE_OK
) {
624 if (slot
->http_code
== 404)
627 return error("%s", curl_errorstr
);
632 return error("Unable to start request");
636 data
= buffer
.buffer
;
637 while (i
< buffer
.posn
) {
641 if (i
+ 52 < buffer
.posn
&&
642 !strncmp(data
+ i
, " pack-", 6) &&
643 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
644 get_sha1_hex(data
+ i
+ 6, sha1
);
650 while (data
[i
] != '\n')
660 static inline int needs_quote(int ch
)
663 case '/': case '-': case '.':
664 case 'A'...'Z': case 'a'...'z': case '0'...'9':
671 static inline int hex(int v
)
673 if (v
< 10) return '0' + v
;
674 else return 'A' + v
- 10;
677 static char *quote_ref_url(const char *base
, const char *ref
)
681 int len
, baselen
, ch
;
683 baselen
= strlen(base
);
684 len
= baselen
+ 12; /* "refs/heads/" + NUL */
685 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
687 len
+= 2; /* extra two hex plus replacement % */
689 memcpy(qref
, base
, baselen
);
690 memcpy(qref
+ baselen
, "refs/heads/", 11);
691 for (cp
= ref
, dp
= qref
+ baselen
+ 11; (ch
= *cp
) != 0; cp
++) {
692 if (needs_quote(ch
)) {
694 *dp
++ = hex((ch
>> 4) & 0xF);
695 *dp
++ = hex(ch
& 0xF);
705 int fetch_ref(char *ref
, unsigned char *sha1
)
709 struct buffer buffer
;
710 char *base
= remote
->url
;
711 struct active_request_slot
*slot
;
717 url
= quote_ref_url(base
, ref
);
718 slot
= get_active_slot();
719 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
720 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
721 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
722 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
723 if (start_active_slot(slot
)) {
724 run_active_slot(slot
);
725 if (slot
->curl_result
!= CURLE_OK
)
726 return error("Couldn't get %s for %s\n%s",
727 url
, ref
, curl_errorstr
);
729 return error("Unable to start request");
733 get_sha1_hex(hex
, sha1
);
738 start_activelock_element(void *userData
, const char *name
, const char **atts
)
740 struct active_lock
*lock
= (struct active_lock
*)userData
;
742 if (lock
->ctx_activelock
&& !strcmp(name
, "D:timeout"))
743 lock
->ctx_timeout
= 1;
744 else if (lock
->ctx_owner
&& strstr(name
, "href"))
745 lock
->ctx_owner_href
= 1;
746 else if (lock
->ctx_activelock
&& strstr(name
, "owner"))
748 else if (lock
->ctx_locktoken
&& !strcmp(name
, "D:href"))
749 lock
->ctx_locktoken_href
= 1;
750 else if (lock
->ctx_activelock
&& !strcmp(name
, "D:locktoken"))
751 lock
->ctx_locktoken
= 1;
752 else if (!strcmp(name
, "D:activelock"))
753 lock
->ctx_activelock
= 1;
757 end_activelock_element(void *userData
, const char *name
)
759 struct active_lock
*lock
= (struct active_lock
*)userData
;
761 if (lock
->ctx_timeout
&& !strcmp(name
, "D:timeout")) {
762 lock
->ctx_timeout
= 0;
763 } else if (lock
->ctx_owner_href
&& strstr(name
, "href")) {
764 lock
->ctx_owner_href
= 0;
765 } else if (lock
->ctx_owner
&& strstr(name
, "owner")) {
767 } else if (lock
->ctx_locktoken_href
&& !strcmp(name
, "D:href")) {
768 lock
->ctx_locktoken_href
= 0;
769 } else if (lock
->ctx_locktoken
&& !strcmp(name
, "D:locktoken")) {
770 lock
->ctx_locktoken
= 0;
771 } else if (lock
->ctx_activelock
&& !strcmp(name
, "D:activelock")) {
772 lock
->ctx_activelock
= 0;
777 activelock_cdata(void *userData
, const XML_Char
*s
, int len
)
779 struct active_lock
*lock
= (struct active_lock
*)userData
;
780 char *this = malloc(len
+1);
781 strncpy(this, s
, len
);
783 if (lock
->ctx_owner_href
) {
784 lock
->owner
= malloc(len
+1);
785 strcpy(lock
->owner
, this);
786 } else if (lock
->ctx_locktoken_href
) {
787 if (!strncmp(this, "opaquelocktoken:", 16)) {
788 lock
->token
= malloc(len
-15);
789 strcpy(lock
->token
, this+16);
791 } else if (lock
->ctx_timeout
) {
792 if (!strncmp(this, "Second-", 7))
793 lock
->timeout
= strtol(this+7, NULL
, 10);
800 start_lockprop_element(void *userData
, const char *name
, const char **atts
)
802 struct lockprop
*prop
= (struct lockprop
*)userData
;
804 if (prop
->lock_type
&& !strcmp(name
, "D:write")) {
805 if (prop
->lock_exclusive
) {
806 prop
->lock_exclusive_write
= 1;
808 } else if (prop
->lock_scope
&& !strcmp(name
, "D:exclusive")) {
809 prop
->lock_exclusive
= 1;
810 } else if (prop
->lock_entry
) {
811 if (!strcmp(name
, "D:lockscope")) {
812 prop
->lock_scope
= 1;
813 } else if (!strcmp(name
, "D:locktype")) {
816 } else if (prop
->supported_lock
) {
817 if (!strcmp(name
, "D:lockentry")) {
818 prop
->lock_entry
= 1;
820 } else if (!strcmp(name
, "D:supportedlock")) {
821 prop
->supported_lock
= 1;
826 end_lockprop_element(void *userData
, const char *name
)
828 struct lockprop
*prop
= (struct lockprop
*)userData
;
830 if (!strcmp(name
, "D:lockentry")) {
831 prop
->lock_entry
= 0;
832 prop
->lock_scope
= 0;
834 prop
->lock_exclusive
= 0;
835 } else if (!strcmp(name
, "D:supportedlock")) {
836 prop
->supported_lock
= 0;
840 static struct active_lock
*lock_remote(char *file
, long timeout
)
842 struct active_request_slot
*slot
;
843 struct buffer out_buffer
;
844 struct buffer in_buffer
;
849 char timeout_header
[25];
850 struct active_lock
*new_lock
;
851 XML_Parser parser
= XML_ParserCreate(NULL
);
852 enum XML_Status result
;
853 struct curl_slist
*dav_headers
= NULL
;
855 url
= xmalloc(strlen(remote
->url
) + strlen(file
) + 1);
856 sprintf(url
, "%s%s", remote
->url
, file
);
858 /* Make sure leading directories exist for the remote ref */
859 ep
= strchr(url
+ strlen(remote
->url
) + 11, '/');
862 slot
= get_active_slot();
863 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
864 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
865 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_MKCOL
);
866 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
867 if (start_active_slot(slot
)) {
868 run_active_slot(slot
);
869 if (slot
->curl_result
!= CURLE_OK
&&
870 slot
->http_code
!= 405) {
872 "Unable to create branch path %s\n",
878 fprintf(stderr
, "Unable to start request\n");
883 ep
= strchr(ep
+ 1, '/');
886 out_buffer
.size
= strlen(LOCK_REQUEST
) + strlen(git_default_email
) - 2;
887 out_data
= xmalloc(out_buffer
.size
+ 1);
888 snprintf(out_data
, out_buffer
.size
+ 1, LOCK_REQUEST
, git_default_email
);
890 out_buffer
.buffer
= out_data
;
892 in_buffer
.size
= 4096;
893 in_data
= xmalloc(in_buffer
.size
);
895 in_buffer
.buffer
= in_data
;
897 new_lock
= xcalloc(1, sizeof(*new_lock
));
898 new_lock
->owner
= NULL
;
899 new_lock
->token
= NULL
;
900 new_lock
->timeout
= -1;
901 new_lock
->refreshing
= 0;
903 sprintf(timeout_header
, "Timeout: Second-%ld", timeout
);
904 dav_headers
= curl_slist_append(dav_headers
, timeout_header
);
905 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
907 slot
= get_active_slot();
908 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
909 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
910 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
911 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
912 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
913 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
914 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
915 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_LOCK
);
916 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
918 if (start_active_slot(slot
)) {
919 run_active_slot(slot
);
920 if (slot
->curl_result
!= CURLE_OK
) {
921 fprintf(stderr
, "Got HTTP error %ld\n", slot
->http_code
);
933 fprintf(stderr
, "Unable to start request\n");
939 XML_SetUserData(parser
, new_lock
);
940 XML_SetElementHandler(parser
, start_activelock_element
,
941 end_activelock_element
);
942 XML_SetCharacterDataHandler(parser
, activelock_cdata
);
943 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
945 if (result
!= XML_STATUS_OK
) {
946 fprintf(stderr
, "%s", XML_ErrorString(
947 XML_GetErrorCode(parser
)));
953 if (new_lock
->token
== NULL
|| new_lock
->timeout
<= 0) {
954 if (new_lock
->token
!= NULL
)
955 free(new_lock
->token
);
956 if (new_lock
->owner
!= NULL
)
957 free(new_lock
->owner
);
964 new_lock
->start_time
= time(NULL
);
968 static int unlock_remote(struct active_lock
*lock
)
970 struct active_request_slot
*slot
;
971 char *lock_token_header
;
972 struct curl_slist
*dav_headers
= NULL
;
975 lock_token_header
= xmalloc(strlen(lock
->token
) + 31);
976 sprintf(lock_token_header
, "Lock-Token: <opaquelocktoken:%s>",
978 dav_headers
= curl_slist_append(dav_headers
, lock_token_header
);
980 slot
= get_active_slot();
981 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
982 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
983 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_UNLOCK
);
984 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
986 if (start_active_slot(slot
)) {
987 run_active_slot(slot
);
988 if (slot
->curl_result
== CURLE_OK
)
991 fprintf(stderr
, "Got HTTP error %ld\n",
994 fprintf(stderr
, "Unable to start request\n");
997 curl_slist_free_all(dav_headers
);
998 free(lock_token_header
);
1000 if (lock
->owner
!= NULL
)
1009 static int check_locking(void)
1011 struct active_request_slot
*slot
;
1012 struct buffer in_buffer
;
1013 struct buffer out_buffer
;
1016 XML_Parser parser
= XML_ParserCreate(NULL
);
1017 enum XML_Status result
;
1018 struct lockprop supported_lock
;
1019 struct curl_slist
*dav_headers
= NULL
;
1021 out_buffer
.size
= strlen(PROPFIND_REQUEST
) + strlen(remote
->url
) - 2;
1022 out_data
= xmalloc(out_buffer
.size
+ 1);
1023 snprintf(out_data
, out_buffer
.size
+ 1, PROPFIND_REQUEST
, remote
->url
);
1024 out_buffer
.posn
= 0;
1025 out_buffer
.buffer
= out_data
;
1027 in_buffer
.size
= 4096;
1028 in_data
= xmalloc(in_buffer
.size
);
1030 in_buffer
.buffer
= in_data
;
1032 dav_headers
= curl_slist_append(dav_headers
, "Depth: 0");
1033 dav_headers
= curl_slist_append(dav_headers
, "Content-Type: text/xml");
1035 slot
= get_active_slot();
1036 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1037 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1038 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1039 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &in_buffer
);
1040 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1041 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, remote
->url
);
1042 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1043 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PROPFIND
);
1044 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1046 if (start_active_slot(slot
)) {
1047 run_active_slot(slot
);
1049 if (slot
->curl_result
!= CURLE_OK
) {
1050 free(in_buffer
.buffer
);
1054 XML_SetUserData(parser
, &supported_lock
);
1055 XML_SetElementHandler(parser
, start_lockprop_element
,
1056 end_lockprop_element
);
1057 result
= XML_Parse(parser
, in_buffer
.buffer
, in_buffer
.posn
, 1);
1058 free(in_buffer
.buffer
);
1059 if (result
!= XML_STATUS_OK
)
1060 return error("%s", XML_ErrorString(
1061 XML_GetErrorCode(parser
)));
1064 free(in_buffer
.buffer
);
1065 return error("Unable to start request");
1068 if (supported_lock
.lock_exclusive_write
)
1074 static int is_ancestor(unsigned char *sha1
, struct commit
*commit
)
1076 struct commit_list
*parents
;
1078 if (parse_commit(commit
))
1080 parents
= commit
->parents
;
1081 for (; parents
; parents
= parents
->next
) {
1082 if (!memcmp(sha1
, parents
->item
->object
.sha1
, 20)) {
1084 } else if (parents
->item
->object
.type
== commit_type
) {
1087 (struct commit
*)&parents
->item
->object
1095 static void get_delta(unsigned char *sha1
, struct object
*obj
,
1096 struct active_lock
*lock
)
1098 struct commit
*commit
;
1099 struct commit_list
*parents
;
1101 struct tree_entry_list
*entry
;
1103 if (sha1
&& !memcmp(sha1
, obj
->sha1
, 20))
1109 if (obj
->type
== commit_type
) {
1111 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1112 add_request(obj
->sha1
, lock
);
1113 commit
= (struct commit
*)obj
;
1114 if (parse_commit(commit
)) {
1115 fprintf(stderr
, "Error parsing commit %s\n",
1116 sha1_to_hex(obj
->sha1
));
1120 parents
= commit
->parents
;
1121 for (; parents
; parents
= parents
->next
)
1123 memcmp(sha1
, parents
->item
->object
.sha1
, 20))
1124 get_delta(sha1
, &parents
->item
->object
,
1126 get_delta(sha1
, &commit
->tree
->object
, lock
);
1127 } else if (obj
->type
== tree_type
) {
1129 fprintf(stderr
, "walk %s\n", sha1_to_hex(obj
->sha1
));
1130 add_request(obj
->sha1
, lock
);
1131 tree
= (struct tree
*)obj
;
1132 if (parse_tree(tree
)) {
1133 fprintf(stderr
, "Error parsing tree %s\n",
1134 sha1_to_hex(obj
->sha1
));
1138 entry
= tree
->entries
;
1139 tree
->entries
= NULL
;
1141 struct tree_entry_list
*next
= entry
->next
;
1142 get_delta(sha1
, entry
->item
.any
, lock
);
1147 } else if (obj
->type
== blob_type
|| obj
->type
== tag_type
) {
1148 add_request(obj
->sha1
, lock
);
1152 static int update_remote(unsigned char *sha1
, struct active_lock
*lock
)
1154 struct active_request_slot
*slot
;
1157 struct buffer out_buffer
;
1158 struct curl_slist
*dav_headers
= NULL
;
1161 if_header
= xmalloc(strlen(lock
->token
) + 25);
1162 sprintf(if_header
, "If: (<opaquelocktoken:%s>)", lock
->token
);
1163 dav_headers
= curl_slist_append(dav_headers
, if_header
);
1165 out_buffer
.size
= 41;
1166 out_data
= xmalloc(out_buffer
.size
+ 1);
1167 i
= snprintf(out_data
, out_buffer
.size
+ 1, "%s\n", sha1_to_hex(sha1
));
1168 if (i
!= out_buffer
.size
) {
1169 fprintf(stderr
, "Unable to initialize PUT request body\n");
1172 out_buffer
.posn
= 0;
1173 out_buffer
.buffer
= out_data
;
1175 slot
= get_active_slot();
1176 curl_easy_setopt(slot
->curl
, CURLOPT_INFILE
, &out_buffer
);
1177 curl_easy_setopt(slot
->curl
, CURLOPT_INFILESIZE
, out_buffer
.size
);
1178 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, fread_buffer
);
1179 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_null
);
1180 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, DAV_PUT
);
1181 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, dav_headers
);
1182 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 1);
1183 curl_easy_setopt(slot
->curl
, CURLOPT_PUT
, 1);
1184 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, lock
->url
);
1186 if (start_active_slot(slot
)) {
1187 run_active_slot(slot
);
1190 if (slot
->curl_result
!= CURLE_OK
) {
1192 "PUT error: curl result=%d, HTTP code=%ld\n",
1193 slot
->curl_result
, slot
->http_code
);
1194 /* We should attempt recovery? */
1200 fprintf(stderr
, "Unable to start PUT request\n");
1207 int main(int argc
, char **argv
)
1209 struct transfer_request
*request
;
1210 struct transfer_request
*next_request
;
1212 char **refspec
= NULL
;
1213 int do_remote_update
;
1217 unsigned char local_sha1
[20];
1218 struct object
*local_object
= NULL
;
1219 char *remote_ref
= NULL
;
1220 unsigned char remote_sha1
[20];
1221 struct active_lock
*remote_lock
;
1222 char *remote_path
= NULL
;
1228 remote
= xmalloc(sizeof(*remote
));
1230 remote
->packs
= NULL
;
1233 for (i
= 1; i
< argc
; i
++, argv
++) {
1237 if (!strcmp(arg
, "--complete")) {
1241 if (!strcmp(arg
, "--force")) {
1245 if (!strcmp(arg
, "--verbose")) {
1249 usage(http_push_usage
);
1256 nr_refspec
= argc
- i
;
1260 memset(remote_dir_exists
, 0, 256);
1264 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1265 default_headers
= curl_slist_append(default_headers
, "Range:");
1266 default_headers
= curl_slist_append(default_headers
, "Destination:");
1267 default_headers
= curl_slist_append(default_headers
, "If:");
1268 default_headers
= curl_slist_append(default_headers
,
1269 "Pragma: no-cache");
1271 /* Verify DAV compliance/lock support */
1272 if (check_locking() != 0) {
1273 fprintf(stderr
, "Error: no DAV locking support on remote repo %s\n", remote
->url
);
1278 /* Process each refspec */
1279 for (i
= 0; i
< nr_refspec
; i
++) {
1282 do_remote_update
= 0;
1284 local_ref
= refspec
[i
];
1285 if (*local_ref
== '+') {
1289 ep
= strchr(local_ref
, ':');
1291 remote_ref
= ep
+ 1;
1295 remote_ref
= local_ref
;
1297 /* Lock remote branch ref */
1300 remote_path
= xmalloc(strlen(remote_ref
) + 12);
1301 sprintf(remote_path
, "refs/heads/%s", remote_ref
);
1302 remote_lock
= lock_remote(remote_path
, LOCK_TIME
);
1303 if (remote_lock
== NULL
) {
1304 fprintf(stderr
, "Unable to lock remote branch %s\n",
1310 /* Resolve local and remote refs */
1311 if (fetch_ref(remote_ref
, remote_sha1
) != 0) {
1313 "Remote branch %s does not exist on %s\n",
1314 remote_ref
, remote
->url
);
1317 if (get_sha1(local_ref
, local_sha1
) != 0) {
1318 fprintf(stderr
, "Error resolving local branch %s\n",
1324 /* Find relationship between local and remote */
1325 local_object
= parse_object(local_sha1
);
1326 if (!local_object
) {
1327 fprintf(stderr
, "Unable to parse local object %s\n",
1328 sha1_to_hex(local_sha1
));
1331 } else if (new_branch
) {
1332 do_remote_update
= 1;
1334 if (!memcmp(local_sha1
, remote_sha1
, 20)) {
1336 "* %s: same as branch '%s' of %s\n",
1337 local_ref
, remote_ref
, remote
->url
);
1338 } else if (is_ancestor(remote_sha1
,
1339 (struct commit
*)local_object
)) {
1341 "Remote %s will fast-forward to local %s\n",
1342 remote_ref
, local_ref
);
1343 do_remote_update
= 1;
1344 } else if (force_all
|| force_this
) {
1346 "* %s on %s does not fast forward to local branch '%s', overwriting\n",
1347 remote_ref
, remote
->url
, local_ref
);
1348 do_remote_update
= 1;
1351 "* %s on %s does not fast forward to local branch '%s'\n",
1352 remote_ref
, remote
->url
, local_ref
);
1358 /* Generate and check list of required objects */
1360 if (do_remote_update
|| push_all
)
1362 get_delta(push_all
? NULL
: remote_sha1
,
1363 local_object
, remote_lock
);
1364 finish_all_active_slots();
1366 /* Push missing objects to remote, this would be a
1367 convenient time to pack them first if appropriate. */
1369 fill_active_slots();
1370 finish_all_active_slots();
1372 /* Update the remote branch if all went well */
1373 if (do_remote_update
) {
1374 if (!aborted
&& update_remote(local_sha1
,
1376 fprintf(stderr
, "%s remote branch %s\n",
1377 new_branch
? "Created" : "Updated",
1381 "Unable to %s remote branch %s\n",
1382 new_branch
? "create" : "update",
1390 unlock_remote(remote_lock
);
1397 curl_slist_free_all(no_pragma_header
);
1398 curl_slist_free_all(default_headers
);
1402 request
= request_queue_head
;
1403 while (request
!= NULL
) {
1404 next_request
= request
->next
;
1405 release_request(request
);
1406 request
= next_request
;
1411 #else /* ifdef USE_CURL_MULTI */
1412 int main(int argc
, char **argv
)
1414 fprintf(stderr
, "http-push requires curl 7.9.8 or higher.\n");