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 int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
52 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
53 argv
[0] = "http-push";
55 if (flags
& TRANSPORT_PUSH_ALL
)
56 argv
[argc
++] = "--all";
57 if (flags
& TRANSPORT_PUSH_FORCE
)
58 argv
[argc
++] = "--force";
59 argv
[argc
++] = transport
->url
;
61 argv
[argc
++] = *refspec
++;
63 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
65 case -ERR_RUN_COMMAND_FORK
:
66 error("unable to fork for %s", argv
[0]);
67 case -ERR_RUN_COMMAND_EXEC
:
68 error("unable to exec %s", argv
[0]);
70 case -ERR_RUN_COMMAND_WAITPID
:
71 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
72 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
73 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
74 error("%s died with strange error", argv
[0]);
80 static int missing__target(int code
, int result
)
82 return /* file:// URL -- do we ever use one??? */
83 (result
== CURLE_FILE_COULDNT_READ_FILE
) ||
84 /* http:// and https:// URL */
85 (code
== 404 && result
== CURLE_HTTP_RETURNED_ERROR
) ||
87 (code
== 550 && result
== CURLE_FTP_COULDNT_RETR_FILE
)
91 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
93 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
96 char *data
, *start
, *mid
;
101 struct active_request_slot
*slot
;
102 struct slot_results results
;
104 struct ref
*refs
= NULL
;
105 struct ref
*ref
= NULL
;
106 struct ref
*last_ref
= NULL
;
108 data
= xmalloc(4096);
111 buffer
.buffer
= data
;
113 refs_url
= xmalloc(strlen(transport
->url
) + 11);
114 sprintf(refs_url
, "%s/info/refs", transport
->url
);
118 slot
= get_active_slot();
119 slot
->results
= &results
;
120 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
121 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
122 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
123 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
124 if (start_active_slot(slot
)) {
125 run_active_slot(slot
);
126 if (results
.curl_result
!= CURLE_OK
) {
127 if (missing_target(&results
)) {
132 error("%s", curl_errorstr
);
138 error("Unable to start request");
144 data
= buffer
.buffer
;
147 while (i
< buffer
.posn
) {
152 if (data
[i
] == '\n') {
155 ref
= xmalloc(sizeof(struct ref
) +
156 strlen(ref_name
) + 1);
157 memset(ref
, 0, sizeof(struct ref
));
158 strcpy(ref
->name
, ref_name
);
159 get_sha1_hex(start
, ref
->old_sha1
);
163 last_ref
->next
= ref
;
175 static int fetch_objs_via_curl(struct transport
*transport
,
176 int nr_objs
, struct ref
**to_fetch
)
178 if (!transport
->data
)
179 transport
->data
= get_http_walker(transport
->url
);
180 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
185 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
187 die("Cannot fetch from '%s' without curl ...", transport
->url
);
191 static int fetch_objs_via_curl(struct transport
*transport
,
192 int nr_objs
, struct ref
**to_fetch
)
194 die("Cannot fetch from '%s' without curl ...", transport
->url
);
200 struct bundle_transport_data
{
202 struct bundle_header header
;
205 static struct ref
*get_refs_from_bundle(const struct transport
*transport
)
207 struct bundle_transport_data
*data
= transport
->data
;
208 struct ref
*result
= NULL
;
213 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
215 die ("Could not read bundle '%s'.", transport
->url
);
216 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
217 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
218 struct ref
*ref
= alloc_ref(strlen(e
->name
) + 1);
219 hashcpy(ref
->old_sha1
, e
->sha1
);
220 strcpy(ref
->name
, e
->name
);
227 static int fetch_refs_from_bundle(struct transport
*transport
,
228 int nr_heads
, struct ref
**to_fetch
)
230 struct bundle_transport_data
*data
= transport
->data
;
231 return unbundle(&data
->header
, data
->fd
);
234 static int close_bundle(struct transport
*transport
)
236 struct bundle_transport_data
*data
= transport
->data
;
243 struct git_transport_data
{
247 const char *uploadpack
;
248 const char *receivepack
;
251 static int set_git_option(struct transport
*connection
,
252 const char *name
, const char *value
)
254 struct git_transport_data
*data
= connection
->data
;
255 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
256 data
->uploadpack
= value
;
258 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
259 data
->receivepack
= value
;
261 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
262 data
->thin
= !!value
;
264 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
265 data
->keep
= !!value
;
267 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
271 data
->depth
= atoi(value
);
277 static struct ref
*get_refs_via_connect(const struct transport
*transport
)
279 struct git_transport_data
*data
= transport
->data
;
283 char *dest
= xstrdup(transport
->url
);
285 pid
= git_connect(fd
, dest
, data
->uploadpack
, 0);
288 die("Failed to connect to \"%s\"", transport
->url
);
290 get_remote_heads(fd
[0], &refs
, 0, NULL
, 0);
300 static int fetch_refs_via_pack(struct transport
*transport
,
301 int nr_heads
, struct ref
**to_fetch
)
303 struct git_transport_data
*data
= transport
->data
;
304 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
305 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
307 char *dest
= xstrdup(transport
->url
);
308 struct fetch_pack_args args
;
311 memset(&args
, 0, sizeof(args
));
312 args
.uploadpack
= data
->uploadpack
;
313 args
.keep_pack
= data
->keep
;
315 args
.use_thin_pack
= data
->thin
;
316 args
.verbose
= transport
->verbose
;
317 args
.depth
= data
->depth
;
319 for (i
= 0; i
< nr_heads
; i
++)
320 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
321 refs
= fetch_pack(&args
, dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
323 for (i
= 0; i
< nr_heads
; i
++)
332 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
333 struct git_transport_data
*data
= transport
->data
;
339 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
340 argv
[0] = "send-pack";
342 if (flags
& TRANSPORT_PUSH_ALL
)
343 argv
[argc
++] = "--all";
344 if (flags
& TRANSPORT_PUSH_FORCE
)
345 argv
[argc
++] = "--force";
346 if (data
->receivepack
) {
347 char *rp
= xmalloc(strlen(data
->receivepack
) + 16);
348 sprintf(rp
, "--receive-pack=%s", data
->receivepack
);
352 argv
[argc
++] = "--thin";
353 rem
= xmalloc(strlen(transport
->remote
->name
) + 10);
354 sprintf(rem
, "--remote=%s", transport
->remote
->name
);
356 argv
[argc
++] = transport
->url
;
358 argv
[argc
++] = *refspec
++;
360 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
362 case -ERR_RUN_COMMAND_FORK
:
363 error("unable to fork for %s", argv
[0]);
364 case -ERR_RUN_COMMAND_EXEC
:
365 error("unable to exec %s", argv
[0]);
367 case -ERR_RUN_COMMAND_WAITPID
:
368 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
369 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
370 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
371 error("%s died with strange error", argv
[0]);
376 static int disconnect_git(struct transport
*transport
)
378 free(transport
->data
);
382 static int is_local(const char *url
)
384 const char *colon
= strchr(url
, ':');
385 const char *slash
= strchr(url
, '/');
386 return !colon
|| (slash
&& slash
< colon
);
389 static int is_file(const char *url
)
394 return S_ISREG(buf
.st_mode
);
397 struct transport
*transport_get(struct remote
*remote
, const char *url
)
399 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
401 ret
->remote
= remote
;
404 if (!prefixcmp(url
, "rsync://")) {
405 /* not supported; don't populate any ops */
407 } else if (!prefixcmp(url
, "http://")
408 || !prefixcmp(url
, "https://")
409 || !prefixcmp(url
, "ftp://")) {
410 ret
->get_refs_list
= get_refs_via_curl
;
411 ret
->fetch
= fetch_objs_via_curl
;
412 ret
->push
= curl_transport_push
;
413 ret
->disconnect
= disconnect_walker
;
415 } else if (is_local(url
) && is_file(url
)) {
416 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
418 ret
->get_refs_list
= get_refs_from_bundle
;
419 ret
->fetch
= fetch_refs_from_bundle
;
420 ret
->disconnect
= close_bundle
;
423 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
425 ret
->set_option
= set_git_option
;
426 ret
->get_refs_list
= get_refs_via_connect
;
427 ret
->fetch
= fetch_refs_via_pack
;
428 ret
->push
= git_transport_push
;
429 ret
->disconnect
= disconnect_git
;
432 data
->uploadpack
= "git-upload-pack";
433 if (remote
&& remote
->uploadpack
)
434 data
->uploadpack
= remote
->uploadpack
;
435 data
->receivepack
= "git-receive-pack";
436 if (remote
&& remote
->receivepack
)
437 data
->receivepack
= remote
->receivepack
;
443 int transport_set_option(struct transport
*transport
,
444 const char *name
, const char *value
)
446 if (transport
->set_option
)
447 return transport
->set_option(transport
, name
, value
);
451 int transport_push(struct transport
*transport
,
452 int refspec_nr
, const char **refspec
, int flags
)
454 if (!transport
->push
)
456 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
459 struct ref
*transport_get_remote_refs(struct transport
*transport
)
461 if (!transport
->remote_refs
)
462 transport
->remote_refs
= transport
->get_refs_list(transport
);
463 return transport
->remote_refs
;
466 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
469 int nr_heads
= 0, nr_alloc
= 0;
470 struct ref
**heads
= NULL
;
473 for (rm
= refs
; rm
; rm
= rm
->next
) {
475 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
477 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
478 heads
[nr_heads
++] = rm
;
481 rc
= transport
->fetch(transport
, nr_heads
, heads
);
486 void transport_unlock_pack(struct transport
*transport
)
488 if (transport
->pack_lockfile
) {
489 unlink(transport
->pack_lockfile
);
490 free(transport
->pack_lockfile
);
491 transport
->pack_lockfile
= NULL
;
495 int transport_disconnect(struct transport
*transport
)
498 if (transport
->disconnect
)
499 ret
= transport
->disconnect(transport
);