3 #include "run-command.h"
6 #include "fetch-pack.h"
10 /* Generic functions for using commit walkers */
12 static int fetch_objs_via_walker(struct transport
*transport
,
13 int nr_objs
, struct ref
**to_fetch
)
15 char *dest
= xstrdup(transport
->url
);
16 struct walker
*walker
= transport
->data
;
17 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
22 walker
->get_history
= 1;
23 walker
->get_verbosely
= transport
->verbose
;
24 walker
->get_recover
= 0;
26 for (i
= 0; i
< nr_objs
; i
++)
27 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
29 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
32 for (i
= 0; i
< nr_objs
; i
++)
39 static int disconnect_walker(struct transport
*transport
)
41 struct walker
*walker
= transport
->data
;
47 static const struct transport_ops rsync_transport
;
49 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
54 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
55 argv
[0] = "http-push";
57 if (flags
& TRANSPORT_PUSH_ALL
)
58 argv
[argc
++] = "--all";
59 if (flags
& TRANSPORT_PUSH_FORCE
)
60 argv
[argc
++] = "--force";
61 argv
[argc
++] = transport
->url
;
63 argv
[argc
++] = *refspec
++;
65 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
67 case -ERR_RUN_COMMAND_FORK
:
68 error("unable to fork for %s", argv
[0]);
69 case -ERR_RUN_COMMAND_EXEC
:
70 error("unable to exec %s", argv
[0]);
72 case -ERR_RUN_COMMAND_WAITPID
:
73 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
74 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
75 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
76 error("%s died with strange error", argv
[0]);
82 static int missing__target(int code
, int result
)
84 return /* file:// URL -- do we ever use one??? */
85 (result
== CURLE_FILE_COULDNT_READ_FILE
) ||
86 /* http:// and https:// URL */
87 (code
== 404 && result
== CURLE_HTTP_RETURNED_ERROR
) ||
89 (code
== 550 && result
== CURLE_FTP_COULDNT_RETR_FILE
)
93 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
95 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
98 char *data
, *start
, *mid
;
103 struct active_request_slot
*slot
;
104 struct slot_results results
;
106 struct ref
*refs
= NULL
;
107 struct ref
*ref
= NULL
;
108 struct ref
*last_ref
= NULL
;
110 data
= xmalloc(4096);
113 buffer
.buffer
= data
;
115 refs_url
= xmalloc(strlen(transport
->url
) + 11);
116 sprintf(refs_url
, "%s/info/refs", transport
->url
);
120 slot
= get_active_slot();
121 slot
->results
= &results
;
122 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
123 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
124 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
125 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
126 if (start_active_slot(slot
)) {
127 run_active_slot(slot
);
128 if (results
.curl_result
!= CURLE_OK
) {
129 if (missing_target(&results
)) {
134 error("%s", curl_errorstr
);
140 error("Unable to start request");
146 data
= buffer
.buffer
;
149 while (i
< buffer
.posn
) {
154 if (data
[i
] == '\n') {
157 ref
= xmalloc(sizeof(struct ref
) +
158 strlen(ref_name
) + 1);
159 memset(ref
, 0, sizeof(struct ref
));
160 strcpy(ref
->name
, ref_name
);
161 get_sha1_hex(start
, ref
->old_sha1
);
165 last_ref
->next
= ref
;
179 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
181 die("Cannot fetch from '%s' without curl ...", transport
->url
);
187 static const struct transport_ops curl_transport
= {
188 /* set_option */ NULL
,
189 /* get_refs_list */ get_refs_via_curl
,
190 /* fetch */ fetch_objs_via_walker
,
191 /* push */ curl_transport_push
,
192 /* disconnect */ disconnect_walker
195 struct bundle_transport_data
{
197 struct bundle_header header
;
200 static struct ref
*get_refs_from_bundle(const struct transport
*transport
)
202 struct bundle_transport_data
*data
= transport
->data
;
203 struct ref
*result
= NULL
;
208 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
210 die ("Could not read bundle '%s'.", transport
->url
);
211 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
212 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
213 struct ref
*ref
= alloc_ref(strlen(e
->name
));
214 hashcpy(ref
->old_sha1
, e
->sha1
);
215 strcpy(ref
->name
, e
->name
);
222 static int fetch_refs_from_bundle(struct transport
*transport
,
223 int nr_heads
, struct ref
**to_fetch
)
225 struct bundle_transport_data
*data
= transport
->data
;
226 return unbundle(&data
->header
, data
->fd
);
229 static int close_bundle(struct transport
*transport
)
231 struct bundle_transport_data
*data
= transport
->data
;
237 static const struct transport_ops bundle_transport
= {
238 /* set_option */ NULL
,
239 /* get_refs_list */ get_refs_from_bundle
,
240 /* fetch */ fetch_refs_from_bundle
,
242 /* disconnect */ close_bundle
245 struct git_transport_data
{
253 const char *uploadpack
;
254 const char *receivepack
;
257 static int set_git_option(struct transport
*connection
,
258 const char *name
, const char *value
)
260 struct git_transport_data
*data
= connection
->data
;
261 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
262 data
->uploadpack
= value
;
264 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
265 data
->receivepack
= value
;
267 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
268 data
->thin
= !!value
;
270 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
271 data
->keep
= !!value
;
273 } else if (!strcmp(name
, TRANS_OPT_UNPACKLIMIT
)) {
274 data
->unpacklimit
= atoi(value
);
276 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
280 data
->depth
= atoi(value
);
286 static struct ref
*get_refs_via_connect(const struct transport
*transport
)
288 struct git_transport_data
*data
= transport
->data
;
292 char *dest
= xstrdup(transport
->url
);
294 pid
= git_connect(fd
, dest
, data
->uploadpack
, 0);
297 die("Failed to connect to \"%s\"", transport
->url
);
299 get_remote_heads(fd
[0], &refs
, 0, NULL
, 0);
309 static int fetch_refs_via_pack(struct transport
*transport
,
310 int nr_heads
, struct ref
**to_fetch
)
312 struct git_transport_data
*data
= transport
->data
;
313 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
314 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
316 char *dest
= xstrdup(transport
->url
);
317 struct fetch_pack_args args
;
320 args
.uploadpack
= data
->uploadpack
;
322 args
.keep_pack
= data
->keep
;
323 args
.unpacklimit
= data
->unpacklimit
;
324 args
.use_thin_pack
= data
->thin
;
326 args
.verbose
= transport
->verbose
;
327 args
.depth
= data
->depth
;
328 args
.no_progress
= 0;
330 setup_fetch_pack(&args
);
332 for (i
= 0; i
< nr_heads
; i
++)
333 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
334 refs
= fetch_pack(dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
336 for (i
= 0; i
< nr_heads
; i
++)
345 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
346 struct git_transport_data
*data
= transport
->data
;
352 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
353 argv
[0] = "send-pack";
355 if (flags
& TRANSPORT_PUSH_ALL
)
356 argv
[argc
++] = "--all";
357 if (flags
& TRANSPORT_PUSH_FORCE
)
358 argv
[argc
++] = "--force";
359 if (data
->receivepack
) {
360 char *rp
= xmalloc(strlen(data
->receivepack
) + 16);
361 sprintf(rp
, "--receive-pack=%s", data
->receivepack
);
365 argv
[argc
++] = "--thin";
366 rem
= xmalloc(strlen(transport
->remote
->name
) + 10);
367 sprintf(rem
, "--remote=%s", transport
->remote
->name
);
369 argv
[argc
++] = transport
->url
;
371 argv
[argc
++] = *refspec
++;
373 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
375 case -ERR_RUN_COMMAND_FORK
:
376 error("unable to fork for %s", argv
[0]);
377 case -ERR_RUN_COMMAND_EXEC
:
378 error("unable to exec %s", argv
[0]);
380 case -ERR_RUN_COMMAND_WAITPID
:
381 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
382 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
383 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
384 error("%s died with strange error", argv
[0]);
389 static const struct transport_ops git_transport
= {
390 /* set_option */ set_git_option
,
391 /* get_refs_list */ get_refs_via_connect
,
392 /* fetch */ fetch_refs_via_pack
,
393 /* push */ git_transport_push
396 static int is_local(const char *url
)
398 const char *colon
= strchr(url
, ':');
399 const char *slash
= strchr(url
, '/');
400 return !colon
|| (slash
&& slash
< colon
);
403 static int is_file(const char *url
)
408 return S_ISREG(buf
.st_mode
);
411 struct transport
*transport_get(struct remote
*remote
, const char *url
,
414 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
416 ret
->remote
= remote
;
418 ret
->fetch
= !!fetch
;
420 if (!prefixcmp(url
, "rsync://")) {
421 ret
->ops
= &rsync_transport
;
422 } else if (!prefixcmp(url
, "http://")
423 || !prefixcmp(url
, "https://")
424 || !prefixcmp(url
, "ftp://")) {
425 ret
->ops
= &curl_transport
;
427 ret
->data
= get_http_walker(url
);
428 } else if (is_local(url
) && is_file(url
)) {
429 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
431 ret
->ops
= &bundle_transport
;
433 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
436 data
->uploadpack
= "git-upload-pack";
437 if (remote
&& remote
->uploadpack
)
438 data
->uploadpack
= remote
->uploadpack
;
439 data
->receivepack
= "git-receive-pack";
440 if (remote
&& remote
->receivepack
)
441 data
->receivepack
= remote
->receivepack
;
442 data
->unpacklimit
= -1;
443 ret
->ops
= &git_transport
;
449 int transport_set_option(struct transport
*transport
,
450 const char *name
, const char *value
)
453 if (transport
->ops
->set_option
)
454 ret
= transport
->ops
->set_option(transport
, name
, value
);
456 fprintf(stderr
, "For '%s' option %s cannot be set to '%s'\n",
457 transport
->url
, name
, value
);
459 fprintf(stderr
, "For '%s' option %s is ignored\n",
460 transport
->url
, name
);
464 int transport_push(struct transport
*transport
,
465 int refspec_nr
, const char **refspec
, int flags
)
467 if (!transport
->ops
->push
)
469 return transport
->ops
->push(transport
, refspec_nr
, refspec
, flags
);
472 struct ref
*transport_get_remote_refs(struct transport
*transport
)
474 if (!transport
->remote_refs
)
475 transport
->remote_refs
=
476 transport
->ops
->get_refs_list(transport
);
477 return transport
->remote_refs
;
480 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
483 int nr_heads
= 0, nr_alloc
= 0;
484 struct ref
**heads
= NULL
;
487 for (rm
= refs
; rm
; rm
= rm
->next
) {
489 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
491 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
492 heads
[nr_heads
++] = rm
;
495 rc
= transport
->ops
->fetch(transport
, nr_heads
, heads
);
500 void transport_unlock_pack(struct transport
*transport
)
502 if (transport
->pack_lockfile
) {
503 unlink(transport
->pack_lockfile
);
504 free(transport
->pack_lockfile
);
505 transport
->pack_lockfile
= NULL
;
509 int transport_disconnect(struct transport
*transport
)
512 if (transport
->ops
->disconnect
)
513 ret
= transport
->ops
->disconnect(transport
);