3 #include "run-command.h"
8 #include "fetch-pack.h"
18 * We copy packed-refs and refs/ into a temporary file, then read the
19 * loose refs recursively (sorting whenever possible), and then inserting
20 * those packed refs that are not yet in the list (not validating, but
21 * assuming that the file is sorted).
23 * Appears refactoring this from refs.c is too cumbersome.
26 static int str_cmp(const void *a
, const void *b
)
31 return strcmp(s1
, s2
);
34 /* path->buf + name_offset is expected to point to "refs/" */
36 static int read_loose_refs(struct strbuf
*path
, int name_offset
,
39 DIR *dir
= opendir(path
->buf
);
50 memset (&list
, 0, sizeof(list
));
52 while ((de
= readdir(dir
))) {
53 if (de
->d_name
[0] == '.' && (de
->d_name
[1] == '\0' ||
54 (de
->d_name
[1] == '.' &&
55 de
->d_name
[2] == '\0')))
57 ALLOC_GROW(list
.entries
, list
.nr
+ 1, list
.alloc
);
58 list
.entries
[list
.nr
++] = xstrdup(de
->d_name
);
64 qsort(list
.entries
, list
.nr
, sizeof(char *), str_cmp
);
67 strbuf_addch(path
, '/');
69 for (i
= 0; i
< list
.nr
; i
++, strbuf_setlen(path
, pathlen
+ 1)) {
70 strbuf_addstr(path
, list
.entries
[i
]);
71 if (read_loose_refs(path
, name_offset
, tail
)) {
72 int fd
= open(path
->buf
, O_RDONLY
);
78 next
= alloc_ref(path
->len
- name_offset
+ 1);
79 if (read_in_full(fd
, buffer
, 40) != 40 ||
80 get_sha1_hex(buffer
, next
->old_sha1
)) {
86 strcpy(next
->name
, path
->buf
+ name_offset
);
91 strbuf_setlen(path
, pathlen
);
93 for (i
= 0; i
< list
.nr
; i
++)
94 free(list
.entries
[i
]);
100 /* insert the packed refs for which no loose refs were found */
102 static void insert_packed_refs(const char *packed_refs
, struct ref
**list
)
104 FILE *f
= fopen(packed_refs
, "r");
105 static char buffer
[PATH_MAX
];
113 if (!fgets(buffer
, sizeof(buffer
), f
)) {
118 if (hexval(buffer
[0]) > 0xf)
120 len
= strlen(buffer
);
121 if (buffer
[len
- 1] == '\n')
122 buffer
[--len
] = '\0';
125 while ((*list
)->next
&&
126 (cmp
= strcmp(buffer
+ 41,
127 (*list
)->next
->name
)) > 0)
128 list
= &(*list
)->next
;
129 if (!(*list
)->next
|| cmp
< 0) {
130 struct ref
*next
= alloc_ref(len
- 40);
132 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
133 warning ("invalid SHA-1: %s", buffer
);
137 strcpy(next
->name
, buffer
+ 41);
138 next
->next
= (*list
)->next
;
139 (*list
)->next
= next
;
140 list
= &(*list
)->next
;
145 static struct ref
*get_refs_via_rsync(struct transport
*transport
)
147 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
148 struct ref dummy
, *tail
= &dummy
;
149 struct child_process rsync
;
153 /* copy the refs to the temporary directory */
155 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
156 if (!mkdtemp(temp_dir
.buf
))
157 die ("Could not make temporary directory");
158 temp_dir_len
= temp_dir
.len
;
160 strbuf_addstr(&buf
, transport
->url
);
161 strbuf_addstr(&buf
, "/refs");
163 memset(&rsync
, 0, sizeof(rsync
));
165 rsync
.stdout_to_stderr
= 1;
167 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
169 args
[3] = temp_dir
.buf
;
172 if (run_command(&rsync
))
173 die ("Could not run rsync to get refs");
176 strbuf_addstr(&buf
, transport
->url
);
177 strbuf_addstr(&buf
, "/packed-refs");
181 if (run_command(&rsync
))
182 die ("Could not run rsync to get refs");
184 /* read the copied refs */
186 strbuf_addstr(&temp_dir
, "/refs");
187 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
188 strbuf_setlen(&temp_dir
, temp_dir_len
);
191 strbuf_addstr(&temp_dir
, "/packed-refs");
192 insert_packed_refs(temp_dir
.buf
, &tail
);
193 strbuf_setlen(&temp_dir
, temp_dir_len
);
195 if (remove_dir_recursively(&temp_dir
, 0))
196 warning ("Error removing temporary directory %s.",
199 strbuf_release(&buf
);
200 strbuf_release(&temp_dir
);
205 static int fetch_objs_via_rsync(struct transport
*transport
,
206 int nr_objs
, struct ref
**to_fetch
)
208 struct strbuf buf
= STRBUF_INIT
;
209 struct child_process rsync
;
213 strbuf_addstr(&buf
, transport
->url
);
214 strbuf_addstr(&buf
, "/objects/");
216 memset(&rsync
, 0, sizeof(rsync
));
218 rsync
.stdout_to_stderr
= 1;
220 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
221 args
[2] = "--ignore-existing";
222 args
[3] = "--exclude";
225 args
[6] = get_object_directory();
228 /* NEEDSWORK: handle one level of alternates */
229 result
= run_command(&rsync
);
231 strbuf_release(&buf
);
236 static int write_one_ref(const char *name
, const unsigned char *sha1
,
237 int flags
, void *data
)
239 struct strbuf
*buf
= data
;
243 /* when called via for_each_ref(), flags is non-zero */
244 if (flags
&& prefixcmp(name
, "refs/heads/") &&
245 prefixcmp(name
, "refs/tags/"))
248 strbuf_addstr(buf
, name
);
249 if (safe_create_leading_directories(buf
->buf
) ||
250 !(f
= fopen(buf
->buf
, "w")) ||
251 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
253 return error("problems writing temporary file %s", buf
->buf
);
254 strbuf_setlen(buf
, len
);
258 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
259 int refspec_nr
, const char **refspec
)
263 for (i
= 0; i
< refspec_nr
; i
++) {
264 unsigned char sha1
[20];
267 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
268 return error("Could not get ref %s", refspec
[i
]);
270 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
279 static int rsync_transport_push(struct transport
*transport
,
280 int refspec_nr
, const char **refspec
, int flags
)
282 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
284 struct child_process rsync
;
285 const char *args
[10];
287 /* first push the objects */
289 strbuf_addstr(&buf
, transport
->url
);
290 strbuf_addch(&buf
, '/');
292 memset(&rsync
, 0, sizeof(rsync
));
294 rsync
.stdout_to_stderr
= 1;
298 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
299 args
[i
++] = "--dry-run";
300 if (transport
->verbose
> 0)
302 args
[i
++] = "--ignore-existing";
303 args
[i
++] = "--exclude";
305 args
[i
++] = get_object_directory();
309 if (run_command(&rsync
))
310 return error("Could not push objects to %s", transport
->url
);
312 /* copy the refs to the temporary directory; they could be packed. */
314 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
315 if (!mkdtemp(temp_dir
.buf
))
316 die ("Could not make temporary directory");
317 strbuf_addch(&temp_dir
, '/');
319 if (flags
& TRANSPORT_PUSH_ALL
) {
320 if (for_each_ref(write_one_ref
, &temp_dir
))
322 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
326 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
327 args
[i
++] = "--dry-run";
328 if (!(flags
& TRANSPORT_PUSH_FORCE
))
329 args
[i
++] = "--ignore-existing";
330 args
[i
++] = temp_dir
.buf
;
331 args
[i
++] = transport
->url
;
333 if (run_command(&rsync
))
334 result
= error("Could not push to %s", transport
->url
);
336 if (remove_dir_recursively(&temp_dir
, 0))
337 warning ("Could not remove temporary directory %s.",
340 strbuf_release(&buf
);
341 strbuf_release(&temp_dir
);
346 /* Generic functions for using commit walkers */
348 static int fetch_objs_via_walker(struct transport
*transport
,
349 int nr_objs
, struct ref
**to_fetch
)
351 char *dest
= xstrdup(transport
->url
);
352 struct walker
*walker
= transport
->data
;
353 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
357 walker
->get_tree
= 1;
358 walker
->get_history
= 1;
359 walker
->get_verbosely
= transport
->verbose
>= 0;
360 walker
->get_recover
= 0;
362 for (i
= 0; i
< nr_objs
; i
++)
363 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
365 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
366 die("Fetch failed.");
368 for (i
= 0; i
< nr_objs
; i
++)
375 static int disconnect_walker(struct transport
*transport
)
377 struct walker
*walker
= transport
->data
;
384 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
390 argv
= xmalloc((refspec_nr
+ 12) * sizeof(char *));
391 argv
[0] = "http-push";
393 if (flags
& TRANSPORT_PUSH_ALL
)
394 argv
[argc
++] = "--all";
395 if (flags
& TRANSPORT_PUSH_FORCE
)
396 argv
[argc
++] = "--force";
397 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
398 argv
[argc
++] = "--dry-run";
399 if (flags
& TRANSPORT_PUSH_VERBOSE
)
400 argv
[argc
++] = "--verbose";
401 argv
[argc
++] = transport
->url
;
403 argv
[argc
++] = *refspec
++;
405 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
407 case -ERR_RUN_COMMAND_FORK
:
408 error("unable to fork for %s", argv
[0]);
409 case -ERR_RUN_COMMAND_EXEC
:
410 error("unable to exec %s", argv
[0]);
412 case -ERR_RUN_COMMAND_WAITPID
:
413 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
414 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
415 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
416 error("%s died with strange error", argv
[0]);
421 static int missing__target(int code
, int result
)
423 return /* file:// URL -- do we ever use one??? */
424 (result
== CURLE_FILE_COULDNT_READ_FILE
) ||
425 /* http:// and https:// URL */
426 (code
== 404 && result
== CURLE_HTTP_RETURNED_ERROR
) ||
428 (code
== 550 && result
== CURLE_FTP_COULDNT_RETR_FILE
)
432 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
434 static struct ref
*get_refs_via_curl(struct transport
*transport
)
436 struct buffer buffer
;
437 char *data
, *start
, *mid
;
442 struct active_request_slot
*slot
;
443 struct slot_results results
;
445 struct ref
*refs
= NULL
;
446 struct ref
*ref
= NULL
;
447 struct ref
*last_ref
= NULL
;
449 data
= xmalloc(4096);
452 buffer
.buffer
= data
;
454 refs_url
= xmalloc(strlen(transport
->url
) + 11);
455 sprintf(refs_url
, "%s/info/refs", transport
->url
);
459 slot
= get_active_slot();
460 slot
->results
= &results
;
461 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
462 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
463 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
464 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
465 if (start_active_slot(slot
)) {
466 run_active_slot(slot
);
467 if (results
.curl_result
!= CURLE_OK
) {
468 if (missing_target(&results
)) {
473 error("%s", curl_errorstr
);
479 error("Unable to start request");
485 data
= buffer
.buffer
;
488 while (i
< buffer
.posn
) {
493 if (data
[i
] == '\n') {
496 ref
= xmalloc(sizeof(struct ref
) +
497 strlen(ref_name
) + 1);
498 memset(ref
, 0, sizeof(struct ref
));
499 strcpy(ref
->name
, ref_name
);
500 get_sha1_hex(start
, ref
->old_sha1
);
504 last_ref
->next
= ref
;
516 static int fetch_objs_via_curl(struct transport
*transport
,
517 int nr_objs
, struct ref
**to_fetch
)
519 if (!transport
->data
)
520 transport
->data
= get_http_walker(transport
->url
);
521 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
526 struct bundle_transport_data
{
528 struct bundle_header header
;
531 static struct ref
*get_refs_from_bundle(struct transport
*transport
)
533 struct bundle_transport_data
*data
= transport
->data
;
534 struct ref
*result
= NULL
;
539 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
541 die ("Could not read bundle '%s'.", transport
->url
);
542 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
543 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
544 struct ref
*ref
= alloc_ref(strlen(e
->name
) + 1);
545 hashcpy(ref
->old_sha1
, e
->sha1
);
546 strcpy(ref
->name
, e
->name
);
553 static int fetch_refs_from_bundle(struct transport
*transport
,
554 int nr_heads
, struct ref
**to_fetch
)
556 struct bundle_transport_data
*data
= transport
->data
;
557 return unbundle(&data
->header
, data
->fd
);
560 static int close_bundle(struct transport
*transport
)
562 struct bundle_transport_data
*data
= transport
->data
;
569 struct git_transport_data
{
573 const char *uploadpack
;
574 const char *receivepack
;
577 static int set_git_option(struct transport
*connection
,
578 const char *name
, const char *value
)
580 struct git_transport_data
*data
= connection
->data
;
581 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
582 data
->uploadpack
= value
;
584 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
585 data
->receivepack
= value
;
587 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
588 data
->thin
= !!value
;
590 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
591 data
->keep
= !!value
;
593 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
597 data
->depth
= atoi(value
);
603 static struct ref
*get_refs_via_connect(struct transport
*transport
)
605 struct git_transport_data
*data
= transport
->data
;
608 char *dest
= xstrdup(transport
->url
);
609 struct child_process
*conn
= git_connect(fd
, dest
, data
->uploadpack
, 0);
611 get_remote_heads(fd
[0], &refs
, 0, NULL
, 0);
614 finish_connect(conn
);
621 static int fetch_refs_via_pack(struct transport
*transport
,
622 int nr_heads
, struct ref
**to_fetch
)
624 struct git_transport_data
*data
= transport
->data
;
625 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
626 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
628 char *dest
= xstrdup(transport
->url
);
629 struct fetch_pack_args args
;
632 memset(&args
, 0, sizeof(args
));
633 args
.uploadpack
= data
->uploadpack
;
634 args
.keep_pack
= data
->keep
;
636 args
.use_thin_pack
= data
->thin
;
637 args
.verbose
= transport
->verbose
> 0;
638 args
.depth
= data
->depth
;
640 for (i
= 0; i
< nr_heads
; i
++)
641 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
642 refs
= fetch_pack(&args
, dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
644 for (i
= 0; i
< nr_heads
; i
++)
653 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
655 struct git_transport_data
*data
= transport
->data
;
656 struct send_pack_args args
;
658 args
.receivepack
= data
->receivepack
;
659 args
.send_all
= !!(flags
& TRANSPORT_PUSH_ALL
);
660 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
661 args
.use_thin_pack
= data
->thin
;
662 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
663 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
665 return send_pack(&args
, transport
->url
, transport
->remote
, refspec_nr
, refspec
);
668 static int disconnect_git(struct transport
*transport
)
670 free(transport
->data
);
674 static int is_local(const char *url
)
676 const char *colon
= strchr(url
, ':');
677 const char *slash
= strchr(url
, '/');
678 return !colon
|| (slash
&& slash
< colon
);
681 static int is_file(const char *url
)
686 return S_ISREG(buf
.st_mode
);
689 struct transport
*transport_get(struct remote
*remote
, const char *url
)
691 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
693 ret
->remote
= remote
;
696 if (!prefixcmp(url
, "rsync://")) {
697 ret
->get_refs_list
= get_refs_via_rsync
;
698 ret
->fetch
= fetch_objs_via_rsync
;
699 ret
->push
= rsync_transport_push
;
701 } else if (!prefixcmp(url
, "http://")
702 || !prefixcmp(url
, "https://")
703 || !prefixcmp(url
, "ftp://")) {
705 error("git was compiled without libcurl support.");
707 ret
->get_refs_list
= get_refs_via_curl
;
708 ret
->fetch
= fetch_objs_via_curl
;
709 ret
->push
= curl_transport_push
;
711 ret
->disconnect
= disconnect_walker
;
713 } else if (is_local(url
) && is_file(url
)) {
714 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
716 ret
->get_refs_list
= get_refs_from_bundle
;
717 ret
->fetch
= fetch_refs_from_bundle
;
718 ret
->disconnect
= close_bundle
;
721 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
723 ret
->set_option
= set_git_option
;
724 ret
->get_refs_list
= get_refs_via_connect
;
725 ret
->fetch
= fetch_refs_via_pack
;
726 ret
->push
= git_transport_push
;
727 ret
->disconnect
= disconnect_git
;
730 data
->uploadpack
= "git-upload-pack";
731 if (remote
&& remote
->uploadpack
)
732 data
->uploadpack
= remote
->uploadpack
;
733 data
->receivepack
= "git-receive-pack";
734 if (remote
&& remote
->receivepack
)
735 data
->receivepack
= remote
->receivepack
;
741 int transport_set_option(struct transport
*transport
,
742 const char *name
, const char *value
)
744 if (transport
->set_option
)
745 return transport
->set_option(transport
, name
, value
);
749 int transport_push(struct transport
*transport
,
750 int refspec_nr
, const char **refspec
, int flags
)
752 if (!transport
->push
)
754 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
757 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
759 if (!transport
->remote_refs
)
760 transport
->remote_refs
= transport
->get_refs_list(transport
);
761 return transport
->remote_refs
;
764 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
767 int nr_heads
= 0, nr_alloc
= 0;
768 struct ref
**heads
= NULL
;
771 for (rm
= refs
; rm
; rm
= rm
->next
) {
773 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
775 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
776 heads
[nr_heads
++] = rm
;
779 rc
= transport
->fetch(transport
, nr_heads
, heads
);
784 void transport_unlock_pack(struct transport
*transport
)
786 if (transport
->pack_lockfile
) {
787 unlink(transport
->pack_lockfile
);
788 free(transport
->pack_lockfile
);
789 transport
->pack_lockfile
= NULL
;
793 int transport_disconnect(struct transport
*transport
)
796 if (transport
->disconnect
)
797 ret
= transport
->disconnect(transport
);