3 #include "run-command.h"
6 #include "fetch-pack.h"
9 /* Generic functions for using commit walkers */
11 static int fetch_objs_via_walker(const struct transport
*transport
,
12 int nr_objs
, char **objs
)
14 char *dest
= xstrdup(transport
->url
);
15 struct walker
*walker
= transport
->data
;
19 walker
->get_history
= 1;
20 walker
->get_verbosely
= transport
->verbose
;
21 walker
->get_recover
= 0;
23 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, dest
))
30 static int disconnect_walker(struct transport
*transport
)
32 struct walker
*walker
= transport
->data
;
38 static const struct transport_ops rsync_transport
;
40 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
45 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
46 argv
[0] = "http-push";
48 if (flags
& TRANSPORT_PUSH_ALL
)
49 argv
[argc
++] = "--all";
50 if (flags
& TRANSPORT_PUSH_FORCE
)
51 argv
[argc
++] = "--force";
52 argv
[argc
++] = transport
->url
;
54 argv
[argc
++] = *refspec
++;
56 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
58 case -ERR_RUN_COMMAND_FORK
:
59 error("unable to fork for %s", argv
[0]);
60 case -ERR_RUN_COMMAND_EXEC
:
61 error("unable to exec %s", argv
[0]);
63 case -ERR_RUN_COMMAND_WAITPID
:
64 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
65 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
66 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
67 error("%s died with strange error", argv
[0]);
73 static int missing__target(int code
, int result
)
75 return /* file:// URL -- do we ever use one??? */
76 (result
== CURLE_FILE_COULDNT_READ_FILE
) ||
77 /* http:// and https:// URL */
78 (code
== 404 && result
== CURLE_HTTP_RETURNED_ERROR
) ||
80 (code
== 550 && result
== CURLE_FTP_COULDNT_RETR_FILE
)
84 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
86 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
89 char *data
, *start
, *mid
;
94 struct active_request_slot
*slot
;
95 struct slot_results results
;
97 struct ref
*refs
= NULL
;
98 struct ref
*ref
= NULL
;
99 struct ref
*last_ref
= NULL
;
101 data
= xmalloc(4096);
104 buffer
.buffer
= data
;
106 refs_url
= xmalloc(strlen(transport
->url
) + 11);
107 sprintf(refs_url
, "%s/info/refs", transport
->url
);
111 slot
= get_active_slot();
112 slot
->results
= &results
;
113 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
114 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
115 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
116 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
117 if (start_active_slot(slot
)) {
118 run_active_slot(slot
);
119 if (results
.curl_result
!= CURLE_OK
) {
120 if (missing_target(&results
)) {
125 error("%s", curl_errorstr
);
131 error("Unable to start request");
137 data
= buffer
.buffer
;
140 while (i
< buffer
.posn
) {
145 if (data
[i
] == '\n') {
148 ref
= xmalloc(sizeof(struct ref
) +
149 strlen(ref_name
) + 1);
150 memset(ref
, 0, sizeof(struct ref
));
151 strcpy(ref
->name
, ref_name
);
152 get_sha1_hex(start
, ref
->old_sha1
);
156 last_ref
->next
= ref
;
170 static struct ref
*get_refs_via_curl(const struct transport
*transport
)
172 die("Cannot fetch from '%s' without curl ...", transport
->url
);
178 static const struct transport_ops curl_transport
= {
179 /* set_option */ NULL
,
180 /* get_refs_list */ get_refs_via_curl
,
181 /* fetch_refs */ NULL
,
182 /* fetch_objs */ fetch_objs_via_walker
,
183 /* push */ curl_transport_push
,
184 /* disconnect */ disconnect_walker
187 static const struct transport_ops bundle_transport
= {
190 struct git_transport_data
{
198 const char *uploadpack
;
199 const char *receivepack
;
202 static int set_git_option(struct transport
*connection
,
203 const char *name
, const char *value
)
205 struct git_transport_data
*data
= connection
->data
;
206 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
207 data
->uploadpack
= value
;
209 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
210 data
->receivepack
= value
;
212 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
213 data
->thin
= !!value
;
215 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
216 data
->keep
= !!value
;
218 } else if (!strcmp(name
, TRANS_OPT_UNPACKLIMIT
)) {
219 data
->unpacklimit
= atoi(value
);
221 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
225 data
->depth
= atoi(value
);
231 static struct ref
*get_refs_via_connect(const struct transport
*transport
)
233 struct git_transport_data
*data
= transport
->data
;
237 char *dest
= xstrdup(transport
->url
);
239 pid
= git_connect(fd
, dest
, data
->uploadpack
, 0);
242 die("Failed to connect to \"%s\"", transport
->url
);
244 get_remote_heads(fd
[0], &refs
, 0, NULL
, 0);
254 static int fetch_refs_via_pack(const struct transport
*transport
,
255 int nr_heads
, char **heads
)
257 struct git_transport_data
*data
= transport
->data
;
259 char *dest
= xstrdup(transport
->url
);
260 struct fetch_pack_args args
;
262 args
.uploadpack
= data
->uploadpack
;
264 args
.keep_pack
= data
->keep
;
265 args
.unpacklimit
= data
->unpacklimit
;
266 args
.use_thin_pack
= data
->thin
;
268 args
.verbose
= transport
->verbose
;
269 args
.depth
= data
->depth
;
270 args
.no_progress
= 0;
272 setup_fetch_pack(&args
);
274 refs
= fetch_pack(dest
, nr_heads
, heads
);
276 // ???? check that refs got everything?
278 /* free the memory used for the refs list ... */
286 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
) {
287 struct git_transport_data
*data
= transport
->data
;
293 argv
= xmalloc((refspec_nr
+ 11) * sizeof(char *));
294 argv
[0] = "send-pack";
296 if (flags
& TRANSPORT_PUSH_ALL
)
297 argv
[argc
++] = "--all";
298 if (flags
& TRANSPORT_PUSH_FORCE
)
299 argv
[argc
++] = "--force";
300 if (data
->receivepack
) {
301 char *rp
= xmalloc(strlen(data
->receivepack
) + 16);
302 sprintf(rp
, "--receive-pack=%s", data
->receivepack
);
306 argv
[argc
++] = "--thin";
307 rem
= xmalloc(strlen(transport
->remote
->name
) + 10);
308 sprintf(rem
, "--remote=%s", transport
->remote
->name
);
310 argv
[argc
++] = transport
->url
;
312 argv
[argc
++] = *refspec
++;
314 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
316 case -ERR_RUN_COMMAND_FORK
:
317 error("unable to fork for %s", argv
[0]);
318 case -ERR_RUN_COMMAND_EXEC
:
319 error("unable to exec %s", argv
[0]);
321 case -ERR_RUN_COMMAND_WAITPID
:
322 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
323 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
324 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
325 error("%s died with strange error", argv
[0]);
330 static const struct transport_ops git_transport
= {
331 /* set_option */ set_git_option
,
332 /* get_refs_list */ get_refs_via_connect
,
333 /* fetch_refs */ fetch_refs_via_pack
,
334 /* fetch_objs */ NULL
,
335 /* push */ git_transport_push
338 static int is_local(const char *url
)
340 const char *colon
= strchr(url
, ':');
341 const char *slash
= strchr(url
, '/');
342 return !colon
|| (slash
&& slash
< colon
);
345 static int is_file(const char *url
)
350 return S_ISREG(buf
.st_mode
);
353 struct transport
*transport_get(struct remote
*remote
, const char *url
,
356 struct transport
*ret
= NULL
;
357 if (!prefixcmp(url
, "rsync://")) {
358 ret
= xmalloc(sizeof(*ret
));
360 ret
->ops
= &rsync_transport
;
361 } else if (!prefixcmp(url
, "http://") || !prefixcmp(url
, "https://") ||
362 !prefixcmp(url
, "ftp://")) {
363 ret
= xmalloc(sizeof(*ret
));
364 ret
->ops
= &curl_transport
;
366 ret
->data
= get_http_walker(url
);
369 } else if (is_local(url
) && is_file(url
)) {
370 ret
= xmalloc(sizeof(*ret
));
372 ret
->ops
= &bundle_transport
;
374 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
375 ret
= xcalloc(1, sizeof(*ret
));
378 data
->uploadpack
= "git-upload-pack";
379 if (remote
&& remote
->uploadpack
)
380 data
->uploadpack
= remote
->uploadpack
;
381 data
->receivepack
= "git-receive-pack";
382 if (remote
&& remote
->receivepack
)
383 data
->receivepack
= remote
->receivepack
;
384 data
->unpacklimit
= -1;
385 ret
->ops
= &git_transport
;
388 ret
->remote
= remote
;
390 ret
->remote_refs
= NULL
;
391 ret
->fetch
= !!fetch
;
396 int transport_set_option(struct transport
*transport
,
397 const char *name
, const char *value
)
400 if (transport
->ops
->set_option
)
401 ret
= transport
->ops
->set_option(transport
, name
, value
);
403 fprintf(stderr
, "For '%s' option %s cannot be set to '%s'\n",
404 transport
->url
, name
, value
);
406 fprintf(stderr
, "For '%s' option %s is ignored\n",
407 transport
->url
, name
);
411 int transport_push(struct transport
*transport
,
412 int refspec_nr
, const char **refspec
, int flags
)
414 if (!transport
->ops
->push
)
416 return transport
->ops
->push(transport
, refspec_nr
, refspec
, flags
);
419 struct ref
*transport_get_remote_refs(struct transport
*transport
)
421 if (!transport
->remote_refs
)
422 transport
->remote_refs
=
423 transport
->ops
->get_refs_list(transport
);
424 return transport
->remote_refs
;
427 #define PACK_HEADS_CHUNK_COUNT 256
429 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
433 char **heads
= xmalloc(PACK_HEADS_CHUNK_COUNT
* sizeof(char *));
435 int use_objs
= !transport
->ops
->fetch_refs
;
437 for (rm
= refs
; rm
; rm
= rm
->next
) {
439 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
442 heads
[nr_heads
++] = xstrdup(sha1_to_hex(rm
->old_sha1
));
444 heads
[nr_heads
++] = xstrdup(rm
->name
);
446 if (nr_heads
% PACK_HEADS_CHUNK_COUNT
== 0)
447 heads
= xrealloc(heads
,
448 (nr_heads
+ PACK_HEADS_CHUNK_COUNT
) *
453 if (transport
->ops
->fetch_objs(transport
, nr_heads
, heads
))
456 if (transport
->ops
->fetch_refs(transport
, nr_heads
, heads
))
460 /* free the memory used for the heads list ... */
461 for (i
= 0; i
< nr_heads
; i
++)
467 int transport_disconnect(struct transport
*transport
)
470 if (transport
->ops
->disconnect
)
471 ret
= transport
->ops
->disconnect(transport
);