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
->buf
+ name_offset
);
79 if (read_in_full(fd
, buffer
, 40) != 40 ||
80 get_sha1_hex(buffer
, next
->old_sha1
)) {
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 (len
&& 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(buffer
+ 41);
131 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
132 warning ("invalid SHA-1: %s", buffer
);
136 next
->next
= (*list
)->next
;
137 (*list
)->next
= next
;
138 list
= &(*list
)->next
;
143 static struct ref
*get_refs_via_rsync(struct transport
*transport
)
145 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
146 struct ref dummy
, *tail
= &dummy
;
147 struct child_process rsync
;
151 /* copy the refs to the temporary directory */
153 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
154 if (!mkdtemp(temp_dir
.buf
))
155 die ("Could not make temporary directory");
156 temp_dir_len
= temp_dir
.len
;
158 strbuf_addstr(&buf
, transport
->url
);
159 strbuf_addstr(&buf
, "/refs");
161 memset(&rsync
, 0, sizeof(rsync
));
163 rsync
.stdout_to_stderr
= 1;
165 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
167 args
[3] = temp_dir
.buf
;
170 if (run_command(&rsync
))
171 die ("Could not run rsync to get refs");
174 strbuf_addstr(&buf
, transport
->url
);
175 strbuf_addstr(&buf
, "/packed-refs");
179 if (run_command(&rsync
))
180 die ("Could not run rsync to get refs");
182 /* read the copied refs */
184 strbuf_addstr(&temp_dir
, "/refs");
185 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
186 strbuf_setlen(&temp_dir
, temp_dir_len
);
189 strbuf_addstr(&temp_dir
, "/packed-refs");
190 insert_packed_refs(temp_dir
.buf
, &tail
);
191 strbuf_setlen(&temp_dir
, temp_dir_len
);
193 if (remove_dir_recursively(&temp_dir
, 0))
194 warning ("Error removing temporary directory %s.",
197 strbuf_release(&buf
);
198 strbuf_release(&temp_dir
);
203 static int fetch_objs_via_rsync(struct transport
*transport
,
204 int nr_objs
, const struct ref
**to_fetch
)
206 struct strbuf buf
= STRBUF_INIT
;
207 struct child_process rsync
;
211 strbuf_addstr(&buf
, transport
->url
);
212 strbuf_addstr(&buf
, "/objects/");
214 memset(&rsync
, 0, sizeof(rsync
));
216 rsync
.stdout_to_stderr
= 1;
218 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
219 args
[2] = "--ignore-existing";
220 args
[3] = "--exclude";
223 args
[6] = get_object_directory();
226 /* NEEDSWORK: handle one level of alternates */
227 result
= run_command(&rsync
);
229 strbuf_release(&buf
);
234 static int write_one_ref(const char *name
, const unsigned char *sha1
,
235 int flags
, void *data
)
237 struct strbuf
*buf
= data
;
241 /* when called via for_each_ref(), flags is non-zero */
242 if (flags
&& prefixcmp(name
, "refs/heads/") &&
243 prefixcmp(name
, "refs/tags/"))
246 strbuf_addstr(buf
, name
);
247 if (safe_create_leading_directories(buf
->buf
) ||
248 !(f
= fopen(buf
->buf
, "w")) ||
249 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
251 return error("problems writing temporary file %s", buf
->buf
);
252 strbuf_setlen(buf
, len
);
256 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
257 int refspec_nr
, const char **refspec
)
261 for (i
= 0; i
< refspec_nr
; i
++) {
262 unsigned char sha1
[20];
265 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
266 return error("Could not get ref %s", refspec
[i
]);
268 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
277 static int rsync_transport_push(struct transport
*transport
,
278 int refspec_nr
, const char **refspec
, int flags
)
280 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
282 struct child_process rsync
;
283 const char *args
[10];
285 if (flags
& TRANSPORT_PUSH_MIRROR
)
286 return error("rsync transport does not support mirror mode");
288 /* first push the objects */
290 strbuf_addstr(&buf
, transport
->url
);
291 strbuf_addch(&buf
, '/');
293 memset(&rsync
, 0, sizeof(rsync
));
295 rsync
.stdout_to_stderr
= 1;
299 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
300 args
[i
++] = "--dry-run";
301 if (transport
->verbose
> 0)
303 args
[i
++] = "--ignore-existing";
304 args
[i
++] = "--exclude";
306 args
[i
++] = get_object_directory();
310 if (run_command(&rsync
))
311 return error("Could not push objects to %s", transport
->url
);
313 /* copy the refs to the temporary directory; they could be packed. */
315 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
316 if (!mkdtemp(temp_dir
.buf
))
317 die ("Could not make temporary directory");
318 strbuf_addch(&temp_dir
, '/');
320 if (flags
& TRANSPORT_PUSH_ALL
) {
321 if (for_each_ref(write_one_ref
, &temp_dir
))
323 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
327 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
328 args
[i
++] = "--dry-run";
329 if (!(flags
& TRANSPORT_PUSH_FORCE
))
330 args
[i
++] = "--ignore-existing";
331 args
[i
++] = temp_dir
.buf
;
332 args
[i
++] = transport
->url
;
334 if (run_command(&rsync
))
335 result
= error("Could not push to %s", transport
->url
);
337 if (remove_dir_recursively(&temp_dir
, 0))
338 warning ("Could not remove temporary directory %s.",
341 strbuf_release(&buf
);
342 strbuf_release(&temp_dir
);
347 /* Generic functions for using commit walkers */
349 #ifndef NO_CURL /* http fetch is the only user */
350 static int fetch_objs_via_walker(struct transport
*transport
,
351 int nr_objs
, const struct ref
**to_fetch
)
353 char *dest
= xstrdup(transport
->url
);
354 struct walker
*walker
= transport
->data
;
355 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
359 walker
->get_tree
= 1;
360 walker
->get_history
= 1;
361 walker
->get_verbosely
= transport
->verbose
>= 0;
362 walker
->get_recover
= 0;
364 for (i
= 0; i
< nr_objs
; i
++)
365 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
367 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
368 die("Fetch failed.");
370 for (i
= 0; i
< nr_objs
; i
++)
378 static int disconnect_walker(struct transport
*transport
)
380 struct walker
*walker
= transport
->data
;
387 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
393 if (flags
& TRANSPORT_PUSH_MIRROR
)
394 return error("http transport does not support mirror mode");
396 argv
= xmalloc((refspec_nr
+ 12) * sizeof(char *));
397 argv
[0] = "http-push";
399 if (flags
& TRANSPORT_PUSH_ALL
)
400 argv
[argc
++] = "--all";
401 if (flags
& TRANSPORT_PUSH_FORCE
)
402 argv
[argc
++] = "--force";
403 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
404 argv
[argc
++] = "--dry-run";
405 if (flags
& TRANSPORT_PUSH_VERBOSE
)
406 argv
[argc
++] = "--verbose";
407 argv
[argc
++] = transport
->url
;
409 argv
[argc
++] = *refspec
++;
411 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
413 case -ERR_RUN_COMMAND_FORK
:
414 error("unable to fork for %s", argv
[0]);
415 case -ERR_RUN_COMMAND_EXEC
:
416 error("unable to exec %s", argv
[0]);
418 case -ERR_RUN_COMMAND_WAITPID
:
419 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
420 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
421 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
422 error("%s died with strange error", argv
[0]);
427 static struct ref
*get_refs_via_curl(struct transport
*transport
)
429 struct strbuf buffer
= STRBUF_INIT
;
430 char *data
, *start
, *mid
;
435 struct active_request_slot
*slot
;
436 struct slot_results results
;
438 struct ref
*refs
= NULL
;
439 struct ref
*ref
= NULL
;
440 struct ref
*last_ref
= NULL
;
442 struct walker
*walker
;
444 if (!transport
->data
)
445 transport
->data
= get_http_walker(transport
->url
,
448 walker
= transport
->data
;
450 refs_url
= xmalloc(strlen(transport
->url
) + 11);
451 sprintf(refs_url
, "%s/info/refs", transport
->url
);
453 slot
= get_active_slot();
454 slot
->results
= &results
;
455 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
456 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
457 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
458 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
460 if (start_active_slot(slot
)) {
461 run_active_slot(slot
);
462 if (results
.curl_result
!= CURLE_OK
) {
463 strbuf_release(&buffer
);
464 if (missing_target(&results
))
465 die("%s not found: did you run git update-server-info on the server?", refs_url
);
467 die("%s download error - %s", refs_url
, curl_errorstr
);
470 strbuf_release(&buffer
);
471 die("Unable to start HTTP request");
477 while (i
< buffer
.len
) {
482 if (data
[i
] == '\n') {
485 ref
= xmalloc(sizeof(struct ref
) +
486 strlen(ref_name
) + 1);
487 memset(ref
, 0, sizeof(struct ref
));
488 strcpy(ref
->name
, ref_name
);
489 get_sha1_hex(start
, ref
->old_sha1
);
493 last_ref
->next
= ref
;
500 strbuf_release(&buffer
);
502 ref
= alloc_ref("HEAD");
503 if (!walker
->fetch_ref(walker
, ref
) &&
504 !resolve_remote_symref(ref
, refs
)) {
514 static int fetch_objs_via_curl(struct transport
*transport
,
515 int nr_objs
, const struct ref
**to_fetch
)
517 if (!transport
->data
)
518 transport
->data
= get_http_walker(transport
->url
,
520 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
525 struct bundle_transport_data
{
527 struct bundle_header header
;
530 static struct ref
*get_refs_from_bundle(struct transport
*transport
)
532 struct bundle_transport_data
*data
= transport
->data
;
533 struct ref
*result
= NULL
;
538 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
540 die ("Could not read bundle '%s'.", transport
->url
);
541 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
542 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
543 struct ref
*ref
= alloc_ref(e
->name
);
544 hashcpy(ref
->old_sha1
, e
->sha1
);
551 static int fetch_refs_from_bundle(struct transport
*transport
,
552 int nr_heads
, const struct ref
**to_fetch
)
554 struct bundle_transport_data
*data
= transport
->data
;
555 return unbundle(&data
->header
, data
->fd
);
558 static int close_bundle(struct transport
*transport
)
560 struct bundle_transport_data
*data
= transport
->data
;
567 struct git_transport_data
{
570 unsigned followtags
: 1;
572 struct child_process
*conn
;
574 const char *uploadpack
;
575 const char *receivepack
;
578 static int set_git_option(struct transport
*connection
,
579 const char *name
, const char *value
)
581 struct git_transport_data
*data
= connection
->data
;
582 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
583 data
->uploadpack
= value
;
585 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
586 data
->receivepack
= value
;
588 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
589 data
->thin
= !!value
;
591 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
592 data
->followtags
= !!value
;
594 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
595 data
->keep
= !!value
;
597 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
601 data
->depth
= atoi(value
);
607 static int connect_setup(struct transport
*transport
)
609 struct git_transport_data
*data
= transport
->data
;
610 data
->conn
= git_connect(data
->fd
, transport
->url
, data
->uploadpack
, 0);
614 static struct ref
*get_refs_via_connect(struct transport
*transport
)
616 struct git_transport_data
*data
= transport
->data
;
619 connect_setup(transport
);
620 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
, 0, NULL
);
625 static int fetch_refs_via_pack(struct transport
*transport
,
626 int nr_heads
, const struct ref
**to_fetch
)
628 struct git_transport_data
*data
= transport
->data
;
629 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
630 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
631 const struct ref
*refs
;
632 char *dest
= xstrdup(transport
->url
);
633 struct fetch_pack_args args
;
635 struct ref
*refs_tmp
= NULL
;
637 memset(&args
, 0, sizeof(args
));
638 args
.uploadpack
= data
->uploadpack
;
639 args
.keep_pack
= data
->keep
;
641 args
.use_thin_pack
= data
->thin
;
642 args
.include_tag
= data
->followtags
;
643 args
.verbose
= (transport
->verbose
> 0);
644 args
.quiet
= (transport
->verbose
< 0);
645 args
.no_progress
= args
.quiet
|| (!transport
->progress
&& !isatty(1));
646 args
.depth
= data
->depth
;
648 for (i
= 0; i
< nr_heads
; i
++)
649 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
652 connect_setup(transport
);
653 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
656 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
657 refs_tmp
? refs_tmp
: transport
->remote_refs
,
658 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
661 if (finish_connect(data
->conn
))
667 for (i
= 0; i
< nr_heads
; i
++)
672 return (refs
? 0 : -1);
675 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
677 struct git_transport_data
*data
= transport
->data
;
678 struct send_pack_args args
;
680 args
.receivepack
= data
->receivepack
;
681 args
.send_all
= !!(flags
& TRANSPORT_PUSH_ALL
);
682 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
683 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
684 args
.use_thin_pack
= data
->thin
;
685 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
686 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
688 return send_pack(&args
, transport
->url
, transport
->remote
, refspec_nr
, refspec
);
691 static int disconnect_git(struct transport
*transport
)
693 struct git_transport_data
*data
= transport
->data
;
695 packet_flush(data
->fd
[1]);
698 finish_connect(data
->conn
);
705 static int is_local(const char *url
)
707 const char *colon
= strchr(url
, ':');
708 const char *slash
= strchr(url
, '/');
709 return !colon
|| (slash
&& slash
< colon
) ||
710 has_dos_drive_prefix(url
);
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
;
763 data
->uploadpack
= "git-upload-pack";
764 if (remote
&& remote
->uploadpack
)
765 data
->uploadpack
= remote
->uploadpack
;
766 data
->receivepack
= "git-receive-pack";
767 if (remote
&& remote
->receivepack
)
768 data
->receivepack
= remote
->receivepack
;
774 int transport_set_option(struct transport
*transport
,
775 const char *name
, const char *value
)
777 if (transport
->set_option
)
778 return transport
->set_option(transport
, name
, value
);
782 int transport_push(struct transport
*transport
,
783 int refspec_nr
, const char **refspec
, int flags
)
785 if (!transport
->push
)
787 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
790 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
792 if (!transport
->remote_refs
)
793 transport
->remote_refs
= transport
->get_refs_list(transport
);
794 return transport
->remote_refs
;
797 int transport_fetch_refs(struct transport
*transport
, const struct ref
*refs
)
800 int nr_heads
= 0, nr_alloc
= 0;
801 const struct ref
**heads
= NULL
;
802 const struct ref
*rm
;
804 for (rm
= refs
; rm
; rm
= rm
->next
) {
806 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
808 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
809 heads
[nr_heads
++] = rm
;
812 rc
= transport
->fetch(transport
, nr_heads
, heads
);
817 void transport_unlock_pack(struct transport
*transport
)
819 if (transport
->pack_lockfile
) {
820 unlink(transport
->pack_lockfile
);
821 free(transport
->pack_lockfile
);
822 transport
->pack_lockfile
= NULL
;
826 int transport_disconnect(struct transport
*transport
)
829 if (transport
->disconnect
)
830 ret
= transport
->disconnect(transport
);