Remove pack.keep after ref updates in git-fetch
[git/git.git] / transport.c
CommitLineData
9b288516
DB
1#include "cache.h"
2#include "transport.h"
3#include "run-command.h"
c29727d5
DB
4#include "http.h"
5#include "pkt-line.h"
6#include "fetch-pack.h"
7#include "walker.h"
c7a8a162 8#include "bundle.h"
c29727d5
DB
9
10/* Generic functions for using commit walkers */
11
1788c39c 12static int fetch_objs_via_walker(struct transport *transport,
425b1393 13 int nr_objs, struct ref **to_fetch)
c29727d5
DB
14{
15 char *dest = xstrdup(transport->url);
16 struct walker *walker = transport->data;
425b1393
SP
17 char **objs = xmalloc(nr_objs * sizeof(*objs));
18 int i;
c29727d5
DB
19
20 walker->get_all = 1;
21 walker->get_tree = 1;
22 walker->get_history = 1;
23 walker->get_verbosely = transport->verbose;
24 walker->get_recover = 0;
25
425b1393
SP
26 for (i = 0; i < nr_objs; i++)
27 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
28
c29727d5
DB
29 if (walker_fetch(walker, nr_objs, objs, NULL, dest))
30 die("Fetch failed.");
31
425b1393
SP
32 for (i = 0; i < nr_objs; i++)
33 free(objs[i]);
34 free(objs);
c29727d5
DB
35 free(dest);
36 return 0;
37}
38
39static int disconnect_walker(struct transport *transport)
40{
41 struct walker *walker = transport->data;
42 if (walker)
43 walker_free(walker);
44 return 0;
45}
9b288516
DB
46
47static const struct transport_ops rsync_transport;
48
49static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
50 const char **argv;
51 int argc;
52 int err;
53
54 argv = xmalloc((refspec_nr + 11) * sizeof(char *));
55 argv[0] = "http-push";
56 argc = 1;
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;
62 while (refspec_nr--)
63 argv[argc++] = *refspec++;
64 argv[argc] = NULL;
65 err = run_command_v_opt(argv, RUN_GIT_CMD);
66 switch (err) {
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]);
71 break;
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]);
77 }
78 return !!err;
79}
80
c29727d5
DB
81#ifndef NO_CURL
82static int missing__target(int code, int result)
83{
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) ||
88 /* ftp:// URL */
89 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
90 ;
91}
92
93#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
94
95static struct ref *get_refs_via_curl(const struct transport *transport)
96{
97 struct buffer buffer;
98 char *data, *start, *mid;
99 char *ref_name;
100 char *refs_url;
101 int i = 0;
102
103 struct active_request_slot *slot;
104 struct slot_results results;
105
106 struct ref *refs = NULL;
107 struct ref *ref = NULL;
108 struct ref *last_ref = NULL;
109
110 data = xmalloc(4096);
111 buffer.size = 4096;
112 buffer.posn = 0;
113 buffer.buffer = data;
114
115 refs_url = xmalloc(strlen(transport->url) + 11);
116 sprintf(refs_url, "%s/info/refs", transport->url);
117
118 http_init();
119
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)) {
130 free(buffer.buffer);
131 return NULL;
132 } else {
133 free(buffer.buffer);
134 error("%s", curl_errorstr);
135 return NULL;
136 }
137 }
138 } else {
139 free(buffer.buffer);
140 error("Unable to start request");
141 return NULL;
142 }
143
144 http_cleanup();
145
146 data = buffer.buffer;
147 start = NULL;
148 mid = data;
149 while (i < buffer.posn) {
150 if (!start)
151 start = &data[i];
152 if (data[i] == '\t')
153 mid = &data[i];
154 if (data[i] == '\n') {
155 data[i] = 0;
156 ref_name = mid + 1;
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);
162 if (!refs)
163 refs = ref;
164 if (last_ref)
165 last_ref->next = ref;
166 last_ref = ref;
167 start = NULL;
168 }
169 i++;
170 }
171
172 free(buffer.buffer);
173
174 return refs;
175}
176
177#else
178
179static struct ref *get_refs_via_curl(const struct transport *transport)
180{
181 die("Cannot fetch from '%s' without curl ...", transport->url);
182 return NULL;
183}
184
185#endif
186
9b288516
DB
187static const struct transport_ops curl_transport = {
188 /* set_option */ NULL,
c29727d5 189 /* get_refs_list */ get_refs_via_curl,
425b1393 190 /* fetch */ fetch_objs_via_walker,
c29727d5
DB
191 /* push */ curl_transport_push,
192 /* disconnect */ disconnect_walker
9b288516
DB
193};
194
c7a8a162
JS
195struct bundle_transport_data {
196 int fd;
197 struct bundle_header header;
198};
199
200static struct ref *get_refs_from_bundle(const struct transport *transport)
201{
202 struct bundle_transport_data *data = transport->data;
203 struct ref *result = NULL;
204 int i;
205
206 if (data->fd > 0)
207 close(data->fd);
208 data->fd = read_bundle_header(transport->url, &data->header);
209 if (data->fd < 0)
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);
216 ref->next = result;
217 result = ref;
218 }
219 return result;
220}
221
1788c39c 222static int fetch_refs_from_bundle(struct transport *transport,
425b1393 223 int nr_heads, struct ref **to_fetch)
c7a8a162
JS
224{
225 struct bundle_transport_data *data = transport->data;
226 return unbundle(&data->header, data->fd);
227}
228
229static int close_bundle(struct transport *transport)
230{
231 struct bundle_transport_data *data = transport->data;
232 if (data->fd > 0)
233 close(data->fd);
234 return 0;
235}
236
9b288516 237static const struct transport_ops bundle_transport = {
c7a8a162
JS
238 /* set_option */ NULL,
239 /* get_refs_list */ get_refs_from_bundle,
425b1393 240 /* fetch */ fetch_refs_from_bundle,
c7a8a162
JS
241 /* push */ NULL,
242 /* disconnect */ close_bundle
9b288516
DB
243};
244
245struct git_transport_data {
246 unsigned thin : 1;
c29727d5
DB
247 unsigned keep : 1;
248
249 int unpacklimit;
9b288516 250
c29727d5
DB
251 int depth;
252
253 const char *uploadpack;
9b288516
DB
254 const char *receivepack;
255};
256
257static int set_git_option(struct transport *connection,
258 const char *name, const char *value)
259{
260 struct git_transport_data *data = connection->data;
c29727d5
DB
261 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
262 data->uploadpack = value;
263 return 0;
264 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
9b288516
DB
265 data->receivepack = value;
266 return 0;
267 } else if (!strcmp(name, TRANS_OPT_THIN)) {
268 data->thin = !!value;
269 return 0;
c29727d5
DB
270 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
271 data->keep = !!value;
272 return 0;
273 } else if (!strcmp(name, TRANS_OPT_UNPACKLIMIT)) {
274 data->unpacklimit = atoi(value);
275 return 0;
276 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
277 if (!value)
278 data->depth = 0;
279 else
280 data->depth = atoi(value);
281 return 0;
9b288516
DB
282 }
283 return 1;
284}
285
c29727d5
DB
286static struct ref *get_refs_via_connect(const struct transport *transport)
287{
288 struct git_transport_data *data = transport->data;
289 struct ref *refs;
290 int fd[2];
291 pid_t pid;
292 char *dest = xstrdup(transport->url);
293
294 pid = git_connect(fd, dest, data->uploadpack, 0);
295
296 if (pid < 0)
297 die("Failed to connect to \"%s\"", transport->url);
298
299 get_remote_heads(fd[0], &refs, 0, NULL, 0);
300 packet_flush(fd[1]);
301
302 finish_connect(pid);
303
304 free(dest);
305
306 return refs;
307}
308
1788c39c 309static int fetch_refs_via_pack(struct transport *transport,
425b1393 310 int nr_heads, struct ref **to_fetch)
c29727d5
DB
311{
312 struct git_transport_data *data = transport->data;
425b1393 313 char **heads = xmalloc(nr_heads * sizeof(*heads));
c29727d5
DB
314 struct ref *refs;
315 char *dest = xstrdup(transport->url);
316 struct fetch_pack_args args;
425b1393 317 int i;
c29727d5
DB
318
319 args.uploadpack = data->uploadpack;
320 args.quiet = 0;
321 args.keep_pack = data->keep;
322 args.unpacklimit = data->unpacklimit;
323 args.use_thin_pack = data->thin;
324 args.fetch_all = 0;
325 args.verbose = transport->verbose;
326 args.depth = data->depth;
327 args.no_progress = 0;
328
329 setup_fetch_pack(&args);
330
425b1393
SP
331 for (i = 0; i < nr_heads; i++)
332 heads[i] = xstrdup(to_fetch[i]->name);
1788c39c 333 refs = fetch_pack(dest, nr_heads, heads, &transport->pack_lockfile);
c29727d5 334
425b1393
SP
335 for (i = 0; i < nr_heads; i++)
336 free(heads[i]);
c29727d5 337 free_refs(refs);
c29727d5
DB
338 free(dest);
339 return 0;
340}
341
9b288516
DB
342static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
343 struct git_transport_data *data = transport->data;
344 const char **argv;
345 char *rem;
346 int argc;
347 int err;
348
349 argv = xmalloc((refspec_nr + 11) * sizeof(char *));
350 argv[0] = "send-pack";
351 argc = 1;
352 if (flags & TRANSPORT_PUSH_ALL)
353 argv[argc++] = "--all";
354 if (flags & TRANSPORT_PUSH_FORCE)
355 argv[argc++] = "--force";
356 if (data->receivepack) {
357 char *rp = xmalloc(strlen(data->receivepack) + 16);
358 sprintf(rp, "--receive-pack=%s", data->receivepack);
359 argv[argc++] = rp;
360 }
361 if (data->thin)
362 argv[argc++] = "--thin";
363 rem = xmalloc(strlen(transport->remote->name) + 10);
364 sprintf(rem, "--remote=%s", transport->remote->name);
365 argv[argc++] = rem;
366 argv[argc++] = transport->url;
367 while (refspec_nr--)
368 argv[argc++] = *refspec++;
369 argv[argc] = NULL;
370 err = run_command_v_opt(argv, RUN_GIT_CMD);
371 switch (err) {
372 case -ERR_RUN_COMMAND_FORK:
373 error("unable to fork for %s", argv[0]);
374 case -ERR_RUN_COMMAND_EXEC:
375 error("unable to exec %s", argv[0]);
376 break;
377 case -ERR_RUN_COMMAND_WAITPID:
378 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
379 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
380 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
381 error("%s died with strange error", argv[0]);
382 }
383 return !!err;
384}
385
386static const struct transport_ops git_transport = {
387 /* set_option */ set_git_option,
c29727d5 388 /* get_refs_list */ get_refs_via_connect,
425b1393 389 /* fetch */ fetch_refs_via_pack,
9b288516
DB
390 /* push */ git_transport_push
391};
392
393static int is_local(const char *url)
394{
395 const char *colon = strchr(url, ':');
396 const char *slash = strchr(url, '/');
397 return !colon || (slash && slash < colon);
398}
399
400static int is_file(const char *url)
401{
402 struct stat buf;
403 if (stat(url, &buf))
404 return 0;
405 return S_ISREG(buf.st_mode);
406}
407
408struct transport *transport_get(struct remote *remote, const char *url,
409 int fetch)
410{
411 struct transport *ret = NULL;
412 if (!prefixcmp(url, "rsync://")) {
413 ret = xmalloc(sizeof(*ret));
414 ret->data = NULL;
415 ret->ops = &rsync_transport;
416 } else if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://") ||
417 !prefixcmp(url, "ftp://")) {
418 ret = xmalloc(sizeof(*ret));
419 ret->ops = &curl_transport;
c29727d5
DB
420 if (fetch)
421 ret->data = get_http_walker(url);
422 else
423 ret->data = NULL;
9b288516 424 } else if (is_local(url) && is_file(url)) {
c7a8a162 425 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 426 ret = xmalloc(sizeof(*ret));
c7a8a162 427 ret->data = data;
9b288516
DB
428 ret->ops = &bundle_transport;
429 } else {
430 struct git_transport_data *data = xcalloc(1, sizeof(*data));
431 ret = xcalloc(1, sizeof(*ret));
432 ret->data = data;
433 data->thin = 1;
c29727d5
DB
434 data->uploadpack = "git-upload-pack";
435 if (remote && remote->uploadpack)
436 data->uploadpack = remote->uploadpack;
9b288516
DB
437 data->receivepack = "git-receive-pack";
438 if (remote && remote->receivepack)
439 data->receivepack = remote->receivepack;
c29727d5 440 data->unpacklimit = -1;
9b288516
DB
441 ret->ops = &git_transport;
442 }
443 if (ret) {
444 ret->remote = remote;
445 ret->url = url;
c29727d5 446 ret->remote_refs = NULL;
9b288516 447 ret->fetch = !!fetch;
1788c39c 448 ret->pack_lockfile = NULL;
9b288516
DB
449 }
450 return ret;
451}
452
453int transport_set_option(struct transport *transport,
454 const char *name, const char *value)
455{
456 int ret = 1;
457 if (transport->ops->set_option)
458 ret = transport->ops->set_option(transport, name, value);
459 if (ret < 0)
460 fprintf(stderr, "For '%s' option %s cannot be set to '%s'\n",
461 transport->url, name, value);
462 if (ret > 0)
463 fprintf(stderr, "For '%s' option %s is ignored\n",
464 transport->url, name);
465 return ret;
466}
467
468int transport_push(struct transport *transport,
469 int refspec_nr, const char **refspec, int flags)
470{
471 if (!transport->ops->push)
472 return 1;
473 return transport->ops->push(transport, refspec_nr, refspec, flags);
474}
475
c29727d5
DB
476struct ref *transport_get_remote_refs(struct transport *transport)
477{
478 if (!transport->remote_refs)
479 transport->remote_refs =
480 transport->ops->get_refs_list(transport);
481 return transport->remote_refs;
482}
483
c29727d5
DB
484int transport_fetch_refs(struct transport *transport, struct ref *refs)
485{
425b1393 486 int rc;
7a2bff45 487 int nr_heads = 0, nr_alloc = 0;
425b1393 488 struct ref **heads = NULL;
c29727d5 489 struct ref *rm;
c29727d5
DB
490
491 for (rm = refs; rm; rm = rm->next) {
492 if (rm->peer_ref &&
493 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
494 continue;
7a2bff45 495 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 496 heads[nr_heads++] = rm;
c29727d5
DB
497 }
498
425b1393 499 rc = transport->ops->fetch(transport, nr_heads, heads);
c29727d5 500 free(heads);
425b1393 501 return rc;
c29727d5
DB
502}
503
1788c39c
SP
504void transport_unlock_pack(struct transport *transport)
505{
506 if (transport->pack_lockfile) {
507 unlink(transport->pack_lockfile);
508 free(transport->pack_lockfile);
509 transport->pack_lockfile = NULL;
510 }
511}
512
9b288516
DB
513int transport_disconnect(struct transport *transport)
514{
515 int ret = 0;
516 if (transport->ops->disconnect)
517 ret = transport->ops->disconnect(transport);
518 free(transport);
519 return ret;
520}