3 #include "run-command.h"
8 #include "fetch-pack.h"
17 * We copy packed-refs and refs/ into a temporary file, then read the
18 * loose refs recursively (sorting whenever possible), and then inserting
19 * those packed refs that are not yet in the list (not validating, but
20 * assuming that the file is sorted).
22 * Appears refactoring this from refs.c is too cumbersome.
25 static int str_cmp(const void *a
, const void *b
)
30 return strcmp(s1
, s2
);
33 /* path->buf + name_offset is expected to point to "refs/" */
35 static int read_loose_refs(struct strbuf
*path
, int name_offset
,
38 DIR *dir
= opendir(path
->buf
);
49 memset (&list
, 0, sizeof(list
));
51 while ((de
= readdir(dir
))) {
52 if (de
->d_name
[0] == '.' && (de
->d_name
[1] == '\0' ||
53 (de
->d_name
[1] == '.' &&
54 de
->d_name
[2] == '\0')))
56 ALLOC_GROW(list
.entries
, list
.nr
+ 1, list
.alloc
);
57 list
.entries
[list
.nr
++] = xstrdup(de
->d_name
);
63 qsort(list
.entries
, list
.nr
, sizeof(char *), str_cmp
);
66 strbuf_addch(path
, '/');
68 for (i
= 0; i
< list
.nr
; i
++, strbuf_setlen(path
, pathlen
+ 1)) {
69 strbuf_addstr(path
, list
.entries
[i
]);
70 if (read_loose_refs(path
, name_offset
, tail
)) {
71 int fd
= open(path
->buf
, O_RDONLY
);
77 next
= alloc_ref(path
->len
- name_offset
+ 1);
78 if (read_in_full(fd
, buffer
, 40) != 40 ||
79 get_sha1_hex(buffer
, next
->old_sha1
)) {
85 strcpy(next
->name
, path
->buf
+ name_offset
);
90 strbuf_setlen(path
, pathlen
);
92 for (i
= 0; i
< list
.nr
; i
++)
93 free(list
.entries
[i
]);
99 /* insert the packed refs for which no loose refs were found */
101 static void insert_packed_refs(const char *packed_refs
, struct ref
**list
)
103 FILE *f
= fopen(packed_refs
, "r");
104 static char buffer
[PATH_MAX
];
112 if (!fgets(buffer
, sizeof(buffer
), f
)) {
117 if (hexval(buffer
[0]) > 0xf)
119 len
= strlen(buffer
);
120 if (buffer
[len
- 1] == '\n')
121 buffer
[--len
] = '\0';
124 while ((*list
)->next
&&
125 (cmp
= strcmp(buffer
+ 41,
126 (*list
)->next
->name
)) > 0)
127 list
= &(*list
)->next
;
128 if (!(*list
)->next
|| cmp
< 0) {
129 struct ref
*next
= alloc_ref(len
- 40);
131 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
132 warning ("invalid SHA-1: %s", buffer
);
136 strcpy(next
->name
, buffer
+ 41);
137 next
->next
= (*list
)->next
;
138 (*list
)->next
= next
;
139 list
= &(*list
)->next
;
144 static struct ref
*get_refs_via_rsync(const struct transport
*transport
)
146 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
147 struct ref dummy
, *tail
= &dummy
;
148 struct child_process rsync
;
152 /* copy the refs to the temporary directory */
154 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
155 if (!mkdtemp(temp_dir
.buf
))
156 die ("Could not make temporary directory");
157 temp_dir_len
= temp_dir
.len
;
159 strbuf_addstr(&buf
, transport
->url
);
160 strbuf_addstr(&buf
, "/refs");
162 memset(&rsync
, 0, sizeof(rsync
));
164 rsync
.stdout_to_stderr
= 1;
166 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
168 args
[3] = temp_dir
.buf
;
171 if (run_command(&rsync
))
172 die ("Could not run rsync to get refs");
175 strbuf_addstr(&buf
, transport
->url
);
176 strbuf_addstr(&buf
, "/packed-refs");
180 if (run_command(&rsync
))
181 die ("Could not run rsync to get refs");
183 /* read the copied refs */
185 strbuf_addstr(&temp_dir
, "/refs");
186 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
187 strbuf_setlen(&temp_dir
, temp_dir_len
);
190 strbuf_addstr(&temp_dir
, "/packed-refs");
191 insert_packed_refs(temp_dir
.buf
, &tail
);
192 strbuf_setlen(&temp_dir
, temp_dir_len
);
194 if (remove_dir_recursively(&temp_dir
, 0))
195 warning ("Error removing temporary directory %s.",
198 strbuf_release(&buf
);
199 strbuf_release(&temp_dir
);
204 static int fetch_objs_via_rsync(struct transport
*transport
,
205 int nr_objs
, struct ref
**to_fetch
)
207 struct strbuf buf
= STRBUF_INIT
;
208 struct child_process rsync
;
212 strbuf_addstr(&buf
, transport
->url
);
213 strbuf_addstr(&buf
, "/objects/");
215 memset(&rsync
, 0, sizeof(rsync
));
217 rsync
.stdout_to_stderr
= 1;
219 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
220 args
[2] = "--ignore-existing";
221 args
[3] = "--exclude";
224 args
[6] = get_object_directory();
227 /* NEEDSWORK: handle one level of alternates */
228 result
= run_command(&rsync
);
230 strbuf_release(&buf
);
235 static int write_one_ref(const char *name
, const unsigned char *sha1
,
236 int flags
, void *data
)
238 struct strbuf
*buf
= data
;
242 /* when called via for_each_ref(), flags is non-zero */
243 if (flags
&& prefixcmp(name
, "refs/heads/") &&
244 prefixcmp(name
, "refs/tags/"))
247 strbuf_addstr(buf
, name
);
248 if (safe_create_leading_directories(buf
->buf
) ||
249 !(f
= fopen(buf
->buf
, "w")) ||
250 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
252 return error("problems writing temporary file %s", buf
->buf
);
253 strbuf_setlen(buf
, len
);
257 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
258 int refspec_nr
, const char **refspec
)
262 for (i
= 0; i
< refspec_nr
; i
++) {
263 unsigned char sha1
[20];
266 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
267 return error("Could not get ref %s", refspec
[i
]);
269 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
278 static int rsync_transport_push(struct transport
*transport
,
279 int refspec_nr
, const char **refspec
, int flags
)
281 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
283 struct child_process rsync
;
284 const char *args
[10];
286 /* first push the objects */
288 strbuf_addstr(&buf
, transport
->url
);
289 strbuf_addch(&buf
, '/');
291 memset(&rsync
, 0, sizeof(rsync
));
293 rsync
.stdout_to_stderr
= 1;
297 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
298 args
[i
++] = "--dry-run";
299 if (transport
->verbose
> 0)
301 args
[i
++] = "--ignore-existing";
302 args
[i
++] = "--exclude";
304 args
[i
++] = get_object_directory();
308 if (run_command(&rsync
))
309 return error("Could not push objects to %s", transport
->url
);
311 /* copy the refs to the temporary directory; they could be packed. */
313 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
314 if (!mkdtemp(temp_dir
.buf
))
315 die ("Could not make temporary directory");
316 strbuf_addch(&temp_dir
, '/');
318 if (flags
& TRANSPORT_PUSH_ALL
) {
319 if (for_each_ref(write_one_ref
, &temp_dir
))
321 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
325 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
326 args
[i
++] = "--dry-run";
327 if (!(flags
& TRANSPORT_PUSH_FORCE
))
328 args
[i
++] = "--ignore-existing";
329 args
[i
++] = temp_dir
.buf
;
330 args
[i
++] = transport
->url
;
332 if (run_command(&rsync
))
333 result
= error("Could not push to %s", transport
->url
);
335 if (remove_dir_recursively(&temp_dir
, 0))
336 warning ("Could not remove temporary directory %s.",
339 strbuf_release(&buf
);
340 strbuf_release(&temp_dir
);
345 /* Generic functions for using commit walkers */
347 static int fetch_objs_via_walker(struct transport
*transport
,
348 int nr_objs
, struct ref
**to_fetch
)
350 char *dest
= xstrdup(transport
->url
);
351 struct walker
*walker
= transport
->data
;
352 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
356 walker
->get_tree
= 1;
357 walker
->get_history
= 1;
358 walker
->get_verbosely
= transport
->verbose
>= 0;
359 walker
->get_recover
= 0;
361 for (i
= 0; i
< nr_objs
; i
++)
362 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
364 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
365 die("Fetch failed.");
367 for (i
= 0; i
< nr_objs
; i
++)
374 static int disconnect_walker(struct transport
*transport
)
376 struct walker
*walker
= transport
->data
;
383 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
388 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
389 argv
[0] = "http-push";
391 if (flags
& TRANSPORT_PUSH_ALL
)
392 argv
[argc
++] = "--all";
393 if (flags
& TRANSPORT_PUSH_FORCE
)
394 argv
[argc
++] = "--force";
395 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
396 argv
[argc
++] = "--dry-run";
397 argv
[argc
++] = transport
->url
;
399 argv
[argc
++] = *refspec
++;
401 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
403 case -ERR_RUN_COMMAND_FORK
:
404 error("unable to fork for %s", argv
[0]);
405 case -ERR_RUN_COMMAND_EXEC
:
406 error("unable to exec %s", argv
[0]);
408 case -ERR_RUN_COMMAND_WAITPID
:
409 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
410 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
411 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
412 error("%s died with strange error", argv
[0]);
417 static int missing__target(int code
, int result
)
419 return /* file:// URL -- do we ever use one??? */
420 (result
== CURLE_FILE_COULDNT_READ_FILE
) ||
421 /* http:// and https:// URL */
422 (code
== 404 && result
== CURLE_HTTP_RETURNED_ERROR
) ||
424 (code
== 550 && result
== CURLE_FTP_COULDNT_RETR_FILE
)
428 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
430 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
432 struct buffer buffer
;
433 char *data
, *start
, *mid
;
438 struct active_request_slot
*slot
;
439 struct slot_results results
;
441 struct ref
*refs
= NULL
;
442 struct ref
*ref
= NULL
;
443 struct ref
*last_ref
= NULL
;
445 data
= xmalloc(4096);
448 buffer
.buffer
= data
;
450 refs_url
= xmalloc(strlen(transport
->url
) + 11);
451 sprintf(refs_url
, "%s/info/refs", transport
->url
);
455 slot
= get_active_slot();
456 slot
->results
= &results
;
457 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
458 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
459 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
460 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
461 if (start_active_slot(slot
)) {
462 run_active_slot(slot
);
463 if (results
.curl_result
!= CURLE_OK
) {
464 if (missing_target(&results
)) {
469 error("%s", curl_errorstr
);
475 error("Unable to start request");
481 data
= buffer
.buffer
;
484 while (i
< buffer
.posn
) {
489 if (data
[i
] == '\n') {
492 ref
= xmalloc(sizeof(struct ref
) +
493 strlen(ref_name
) + 1);
494 memset(ref
, 0, sizeof(struct ref
));
495 strcpy(ref
->name
, ref_name
);
496 get_sha1_hex(start
, ref
->old_sha1
);
500 last_ref
->next
= ref
;
512 static int fetch_objs_via_curl(struct transport
*transport
,
513 int nr_objs
, struct ref
**to_fetch
)
515 if (!transport
->data
)
516 transport
->data
= get_http_walker(transport
->url
);
517 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
522 struct bundle_transport_data
{
524 struct bundle_header header
;
527 static struct ref
*get_refs_from_bundle(const struct transport
*transport
)
529 struct bundle_transport_data
*data
= transport
->data
;
530 struct ref
*result
= NULL
;
535 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
537 die ("Could not read bundle '%s'.", transport
->url
);
538 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
539 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
540 struct ref
*ref
= alloc_ref(strlen(e
->name
) + 1);
541 hashcpy(ref
->old_sha1
, e
->sha1
);
542 strcpy(ref
->name
, e
->name
);
549 static int fetch_refs_from_bundle(struct transport
*transport
,
550 int nr_heads
, struct ref
**to_fetch
)
552 struct bundle_transport_data
*data
= transport
->data
;
553 return unbundle(&data
->header
, data
->fd
);
556 static int close_bundle(struct transport
*transport
)
558 struct bundle_transport_data
*data
= transport
->data
;
565 struct git_transport_data
{
569 const char *uploadpack
;
570 const char *receivepack
;
573 static int set_git_option(struct transport
*connection
,
574 const char *name
, const char *value
)
576 struct git_transport_data
*data
= connection
->data
;
577 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
578 data
->uploadpack
= value
;
580 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
581 data
->receivepack
= value
;
583 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
584 data
->thin
= !!value
;
586 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
587 data
->keep
= !!value
;
589 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
593 data
->depth
= atoi(value
);
599 static struct ref
*get_refs_via_connect(const struct transport
*transport
)
601 struct git_transport_data
*data
= transport
->data
;
605 char *dest
= xstrdup(transport
->url
);
607 pid
= git_connect(fd
, dest
, data
->uploadpack
, 0);
610 die("Failed to connect to \"%s\"", transport
->url
);
612 get_remote_heads(fd
[0], &refs
, 0, NULL
, 0);
622 static int fetch_refs_via_pack(struct transport
*transport
,
623 int nr_heads
, struct ref
**to_fetch
)
625 struct git_transport_data
*data
= transport
->data
;
626 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
627 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
629 char *dest
= xstrdup(transport
->url
);
630 struct fetch_pack_args args
;
633 memset(&args
, 0, sizeof(args
));
634 args
.uploadpack
= data
->uploadpack
;
635 args
.keep_pack
= data
->keep
;
637 args
.use_thin_pack
= data
->thin
;
638 args
.verbose
= transport
->verbose
> 0;
639 args
.depth
= data
->depth
;
641 for (i
= 0; i
< nr_heads
; i
++)
642 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
643 refs
= fetch_pack(&args
, dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
645 for (i
= 0; i
< nr_heads
; i
++)
654 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
655 struct git_transport_data
*data
= transport
->data
;
661 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
662 argv
[0] = "send-pack";
664 if (flags
& TRANSPORT_PUSH_ALL
)
665 argv
[argc
++] = "--all";
666 if (flags
& TRANSPORT_PUSH_FORCE
)
667 argv
[argc
++] = "--force";
668 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
669 argv
[argc
++] = "--dry-run";
670 if (data
->receivepack
) {
671 char *rp
= xmalloc(strlen(data
->receivepack
) + 16);
672 sprintf(rp
, "--receive-pack=%s", data
->receivepack
);
676 argv
[argc
++] = "--thin";
677 rem
= xmalloc(strlen(transport
->remote
->name
) + 10);
678 sprintf(rem
, "--remote=%s", transport
->remote
->name
);
680 argv
[argc
++] = transport
->url
;
682 argv
[argc
++] = *refspec
++;
684 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
686 case -ERR_RUN_COMMAND_FORK
:
687 error("unable to fork for %s", argv
[0]);
688 case -ERR_RUN_COMMAND_EXEC
:
689 error("unable to exec %s", argv
[0]);
691 case -ERR_RUN_COMMAND_WAITPID
:
692 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
693 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
694 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
695 error("%s died with strange error", argv
[0]);
700 static int disconnect_git(struct transport
*transport
)
702 free(transport
->data
);
706 static int is_local(const char *url
)
708 const char *colon
= strchr(url
, ':');
709 const char *slash
= strchr(url
, '/');
710 return !colon
|| (slash
&& slash
< colon
);
713 static int is_file(const char *url
)
718 return S_ISREG(buf
.st_mode
);
721 struct transport
*transport_get(struct remote
*remote
, const char *url
)
723 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
725 ret
->remote
= remote
;
728 if (!prefixcmp(url
, "rsync://")) {
729 ret
->get_refs_list
= get_refs_via_rsync
;
730 ret
->fetch
= fetch_objs_via_rsync
;
731 ret
->push
= rsync_transport_push
;
733 } else if (!prefixcmp(url
, "http://")
734 || !prefixcmp(url
, "https://")
735 || !prefixcmp(url
, "ftp://")) {
737 error("git was compiled without libcurl support.");
739 ret
->get_refs_list
= get_refs_via_curl
;
740 ret
->fetch
= fetch_objs_via_curl
;
741 ret
->push
= curl_transport_push
;
743 ret
->disconnect
= disconnect_walker
;
745 } else if (is_local(url
) && is_file(url
)) {
746 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
748 ret
->get_refs_list
= get_refs_from_bundle
;
749 ret
->fetch
= fetch_refs_from_bundle
;
750 ret
->disconnect
= close_bundle
;
753 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
755 ret
->set_option
= set_git_option
;
756 ret
->get_refs_list
= get_refs_via_connect
;
757 ret
->fetch
= fetch_refs_via_pack
;
758 ret
->push
= git_transport_push
;
759 ret
->disconnect
= disconnect_git
;
762 data
->uploadpack
= "git-upload-pack";
763 if (remote
&& remote
->uploadpack
)
764 data
->uploadpack
= remote
->uploadpack
;
765 data
->receivepack
= "git-receive-pack";
766 if (remote
&& remote
->receivepack
)
767 data
->receivepack
= remote
->receivepack
;
773 int transport_set_option(struct transport
*transport
,
774 const char *name
, const char *value
)
776 if (transport
->set_option
)
777 return transport
->set_option(transport
, name
, value
);
781 int transport_push(struct transport
*transport
,
782 int refspec_nr
, const char **refspec
, int flags
)
784 if (!transport
->push
)
786 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
789 struct ref
*transport_get_remote_refs(struct transport
*transport
)
791 if (!transport
->remote_refs
)
792 transport
->remote_refs
= transport
->get_refs_list(transport
);
793 return transport
->remote_refs
;
796 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
799 int nr_heads
= 0, nr_alloc
= 0;
800 struct ref
**heads
= NULL
;
803 for (rm
= refs
; rm
; rm
= rm
->next
) {
805 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
807 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
808 heads
[nr_heads
++] = rm
;
811 rc
= transport
->fetch(transport
, nr_heads
, heads
);
816 void transport_unlock_pack(struct transport
*transport
)
818 if (transport
->pack_lockfile
) {
819 unlink(transport
->pack_lockfile
);
820 free(transport
->pack_lockfile
);
821 transport
->pack_lockfile
= NULL
;
825 int transport_disconnect(struct transport
*transport
)
828 if (transport
->disconnect
)
829 ret
= transport
->disconnect(transport
);