git-push: plumb in --mirror mode
[git] / transport.c
1 #include "cache.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #ifndef NO_CURL
5 #include "http.h"
6 #endif
7 #include "pkt-line.h"
8 #include "fetch-pack.h"
9 #include "send-pack.h"
10 #include "walker.h"
11 #include "bundle.h"
12 #include "dir.h"
13 #include "refs.h"
14
15 /* rsync support */
16
17 /*
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).
22  *
23  * Appears refactoring this from refs.c is too cumbersome.
24  */
25
26 static int str_cmp(const void *a, const void *b)
27 {
28         const char *s1 = a;
29         const char *s2 = b;
30
31         return strcmp(s1, s2);
32 }
33
34 /* path->buf + name_offset is expected to point to "refs/" */
35
36 static int read_loose_refs(struct strbuf *path, int name_offset,
37                 struct ref **tail)
38 {
39         DIR *dir = opendir(path->buf);
40         struct dirent *de;
41         struct {
42                 char **entries;
43                 int nr, alloc;
44         } list;
45         int i, pathlen;
46
47         if (!dir)
48                 return -1;
49
50         memset (&list, 0, sizeof(list));
51
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')))
56                         continue;
57                 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
58                 list.entries[list.nr++] = xstrdup(de->d_name);
59         }
60         closedir(dir);
61
62         /* sort the list */
63
64         qsort(list.entries, list.nr, sizeof(char *), str_cmp);
65
66         pathlen = path->len;
67         strbuf_addch(path, '/');
68
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);
73                         char buffer[40];
74                         struct ref *next;
75
76                         if (fd < 0)
77                                 continue;
78                         next = alloc_ref(path->len - name_offset + 1);
79                         if (read_in_full(fd, buffer, 40) != 40 ||
80                                         get_sha1_hex(buffer, next->old_sha1)) {
81                                 close(fd);
82                                 free(next);
83                                 continue;
84                         }
85                         close(fd);
86                         strcpy(next->name, path->buf + name_offset);
87                         (*tail)->next = next;
88                         *tail = next;
89                 }
90         }
91         strbuf_setlen(path, pathlen);
92
93         for (i = 0; i < list.nr; i++)
94                 free(list.entries[i]);
95         free(list.entries);
96
97         return 0;
98 }
99
100 /* insert the packed refs for which no loose refs were found */
101
102 static void insert_packed_refs(const char *packed_refs, struct ref **list)
103 {
104         FILE *f = fopen(packed_refs, "r");
105         static char buffer[PATH_MAX];
106
107         if (!f)
108                 return;
109
110         for (;;) {
111                 int cmp = cmp, len;
112
113                 if (!fgets(buffer, sizeof(buffer), f)) {
114                         fclose(f);
115                         return;
116                 }
117
118                 if (hexval(buffer[0]) > 0xf)
119                         continue;
120                 len = strlen(buffer);
121                 if (buffer[len - 1] == '\n')
122                         buffer[--len] = '\0';
123                 if (len < 41)
124                         continue;
125                 while ((*list)->next &&
126                                 (cmp = strcmp(buffer + 41,
127                                       (*list)->next->name)) > 0)
128                         list = &(*list)->next;
129                 if (!(*list)->next || cmp < 0) {
130                         struct ref *next = alloc_ref(len - 40);
131                         buffer[40] = '\0';
132                         if (get_sha1_hex(buffer, next->old_sha1)) {
133                                 warning ("invalid SHA-1: %s", buffer);
134                                 free(next);
135                                 continue;
136                         }
137                         strcpy(next->name, buffer + 41);
138                         next->next = (*list)->next;
139                         (*list)->next = next;
140                         list = &(*list)->next;
141                 }
142         }
143 }
144
145 static struct ref *get_refs_via_rsync(struct transport *transport)
146 {
147         struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
148         struct ref dummy, *tail = &dummy;
149         struct child_process rsync;
150         const char *args[5];
151         int temp_dir_len;
152
153         /* copy the refs to the temporary directory */
154
155         strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
156         if (!mkdtemp(temp_dir.buf))
157                 die ("Could not make temporary directory");
158         temp_dir_len = temp_dir.len;
159
160         strbuf_addstr(&buf, transport->url);
161         strbuf_addstr(&buf, "/refs");
162
163         memset(&rsync, 0, sizeof(rsync));
164         rsync.argv = args;
165         rsync.stdout_to_stderr = 1;
166         args[0] = "rsync";
167         args[1] = (transport->verbose > 0) ? "-rv" : "-r";
168         args[2] = buf.buf;
169         args[3] = temp_dir.buf;
170         args[4] = NULL;
171
172         if (run_command(&rsync))
173                 die ("Could not run rsync to get refs");
174
175         strbuf_reset(&buf);
176         strbuf_addstr(&buf, transport->url);
177         strbuf_addstr(&buf, "/packed-refs");
178
179         args[2] = buf.buf;
180
181         if (run_command(&rsync))
182                 die ("Could not run rsync to get refs");
183
184         /* read the copied refs */
185
186         strbuf_addstr(&temp_dir, "/refs");
187         read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
188         strbuf_setlen(&temp_dir, temp_dir_len);
189
190         tail = &dummy;
191         strbuf_addstr(&temp_dir, "/packed-refs");
192         insert_packed_refs(temp_dir.buf, &tail);
193         strbuf_setlen(&temp_dir, temp_dir_len);
194
195         if (remove_dir_recursively(&temp_dir, 0))
196                 warning ("Error removing temporary directory %s.",
197                                 temp_dir.buf);
198
199         strbuf_release(&buf);
200         strbuf_release(&temp_dir);
201
202         return dummy.next;
203 }
204
205 static int fetch_objs_via_rsync(struct transport *transport,
206                                  int nr_objs, struct ref **to_fetch)
207 {
208         struct strbuf buf = STRBUF_INIT;
209         struct child_process rsync;
210         const char *args[8];
211         int result;
212
213         strbuf_addstr(&buf, transport->url);
214         strbuf_addstr(&buf, "/objects/");
215
216         memset(&rsync, 0, sizeof(rsync));
217         rsync.argv = args;
218         rsync.stdout_to_stderr = 1;
219         args[0] = "rsync";
220         args[1] = (transport->verbose > 0) ? "-rv" : "-r";
221         args[2] = "--ignore-existing";
222         args[3] = "--exclude";
223         args[4] = "info";
224         args[5] = buf.buf;
225         args[6] = get_object_directory();
226         args[7] = NULL;
227
228         /* NEEDSWORK: handle one level of alternates */
229         result = run_command(&rsync);
230
231         strbuf_release(&buf);
232
233         return result;
234 }
235
236 static int write_one_ref(const char *name, const unsigned char *sha1,
237                 int flags, void *data)
238 {
239         struct strbuf *buf = data;
240         int len = buf->len;
241         FILE *f;
242
243         /* when called via for_each_ref(), flags is non-zero */
244         if (flags && prefixcmp(name, "refs/heads/") &&
245                         prefixcmp(name, "refs/tags/"))
246                 return 0;
247
248         strbuf_addstr(buf, name);
249         if (safe_create_leading_directories(buf->buf) ||
250                         !(f = fopen(buf->buf, "w")) ||
251                         fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
252                         fclose(f))
253                 return error("problems writing temporary file %s", buf->buf);
254         strbuf_setlen(buf, len);
255         return 0;
256 }
257
258 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
259                 int refspec_nr, const char **refspec)
260 {
261         int i;
262
263         for (i = 0; i < refspec_nr; i++) {
264                 unsigned char sha1[20];
265                 char *ref;
266
267                 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
268                         return error("Could not get ref %s", refspec[i]);
269
270                 if (write_one_ref(ref, sha1, 0, temp_dir)) {
271                         free(ref);
272                         return -1;
273                 }
274                 free(ref);
275         }
276         return 0;
277 }
278
279 static int rsync_transport_push(struct transport *transport,
280                 int refspec_nr, const char **refspec, int flags)
281 {
282         struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
283         int result = 0, i;
284         struct child_process rsync;
285         const char *args[10];
286
287         if (flags & TRANSPORT_PUSH_MIRROR)
288                 return error("rsync transport does not support mirror mode");
289
290         /* first push the objects */
291
292         strbuf_addstr(&buf, transport->url);
293         strbuf_addch(&buf, '/');
294
295         memset(&rsync, 0, sizeof(rsync));
296         rsync.argv = args;
297         rsync.stdout_to_stderr = 1;
298         i = 0;
299         args[i++] = "rsync";
300         args[i++] = "-a";
301         if (flags & TRANSPORT_PUSH_DRY_RUN)
302                 args[i++] = "--dry-run";
303         if (transport->verbose > 0)
304                 args[i++] = "-v";
305         args[i++] = "--ignore-existing";
306         args[i++] = "--exclude";
307         args[i++] = "info";
308         args[i++] = get_object_directory();
309         args[i++] = buf.buf;
310         args[i++] = NULL;
311
312         if (run_command(&rsync))
313                 return error("Could not push objects to %s", transport->url);
314
315         /* copy the refs to the temporary directory; they could be packed. */
316
317         strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
318         if (!mkdtemp(temp_dir.buf))
319                 die ("Could not make temporary directory");
320         strbuf_addch(&temp_dir, '/');
321
322         if (flags & TRANSPORT_PUSH_ALL) {
323                 if (for_each_ref(write_one_ref, &temp_dir))
324                         return -1;
325         } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
326                 return -1;
327
328         i = 2;
329         if (flags & TRANSPORT_PUSH_DRY_RUN)
330                 args[i++] = "--dry-run";
331         if (!(flags & TRANSPORT_PUSH_FORCE))
332                 args[i++] = "--ignore-existing";
333         args[i++] = temp_dir.buf;
334         args[i++] = transport->url;
335         args[i++] = NULL;
336         if (run_command(&rsync))
337                 result = error("Could not push to %s", transport->url);
338
339         if (remove_dir_recursively(&temp_dir, 0))
340                 warning ("Could not remove temporary directory %s.",
341                                 temp_dir.buf);
342
343         strbuf_release(&buf);
344         strbuf_release(&temp_dir);
345
346         return result;
347 }
348
349 /* Generic functions for using commit walkers */
350
351 static int fetch_objs_via_walker(struct transport *transport,
352                                  int nr_objs, struct ref **to_fetch)
353 {
354         char *dest = xstrdup(transport->url);
355         struct walker *walker = transport->data;
356         char **objs = xmalloc(nr_objs * sizeof(*objs));
357         int i;
358
359         walker->get_all = 1;
360         walker->get_tree = 1;
361         walker->get_history = 1;
362         walker->get_verbosely = transport->verbose >= 0;
363         walker->get_recover = 0;
364
365         for (i = 0; i < nr_objs; i++)
366                 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
367
368         if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
369                 die("Fetch failed.");
370
371         for (i = 0; i < nr_objs; i++)
372                 free(objs[i]);
373         free(objs);
374         free(dest);
375         return 0;
376 }
377
378 static int disconnect_walker(struct transport *transport)
379 {
380         struct walker *walker = transport->data;
381         if (walker)
382                 walker_free(walker);
383         return 0;
384 }
385
386 #ifndef NO_CURL
387 static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
388 {
389         const char **argv;
390         int argc;
391         int err;
392
393         if (flags & TRANSPORT_PUSH_MIRROR)
394                 return error("http transport does not support mirror mode");
395
396         argv = xmalloc((refspec_nr + 11) * sizeof(char *));
397         argv[0] = "http-push";
398         argc = 1;
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         argv[argc++] = transport->url;
406         while (refspec_nr--)
407                 argv[argc++] = *refspec++;
408         argv[argc] = NULL;
409         err = run_command_v_opt(argv, RUN_GIT_CMD);
410         switch (err) {
411         case -ERR_RUN_COMMAND_FORK:
412                 error("unable to fork for %s", argv[0]);
413         case -ERR_RUN_COMMAND_EXEC:
414                 error("unable to exec %s", argv[0]);
415                 break;
416         case -ERR_RUN_COMMAND_WAITPID:
417         case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
418         case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
419         case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
420                 error("%s died with strange error", argv[0]);
421         }
422         return !!err;
423 }
424
425 static int missing__target(int code, int result)
426 {
427         return  /* file:// URL -- do we ever use one??? */
428                 (result == CURLE_FILE_COULDNT_READ_FILE) ||
429                 /* http:// and https:// URL */
430                 (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
431                 /* ftp:// URL */
432                 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
433                 ;
434 }
435
436 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
437
438 static struct ref *get_refs_via_curl(struct transport *transport)
439 {
440         struct buffer buffer;
441         char *data, *start, *mid;
442         char *ref_name;
443         char *refs_url;
444         int i = 0;
445
446         struct active_request_slot *slot;
447         struct slot_results results;
448
449         struct ref *refs = NULL;
450         struct ref *ref = NULL;
451         struct ref *last_ref = NULL;
452
453         data = xmalloc(4096);
454         buffer.size = 4096;
455         buffer.posn = 0;
456         buffer.buffer = data;
457
458         refs_url = xmalloc(strlen(transport->url) + 11);
459         sprintf(refs_url, "%s/info/refs", transport->url);
460
461         http_init();
462
463         slot = get_active_slot();
464         slot->results = &results;
465         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
466         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
467         curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
468         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
469         if (start_active_slot(slot)) {
470                 run_active_slot(slot);
471                 if (results.curl_result != CURLE_OK) {
472                         if (missing_target(&results)) {
473                                 free(buffer.buffer);
474                                 return NULL;
475                         } else {
476                                 free(buffer.buffer);
477                                 error("%s", curl_errorstr);
478                                 return NULL;
479                         }
480                 }
481         } else {
482                 free(buffer.buffer);
483                 error("Unable to start request");
484                 return NULL;
485         }
486
487         http_cleanup();
488
489         data = buffer.buffer;
490         start = NULL;
491         mid = data;
492         while (i < buffer.posn) {
493                 if (!start)
494                         start = &data[i];
495                 if (data[i] == '\t')
496                         mid = &data[i];
497                 if (data[i] == '\n') {
498                         data[i] = 0;
499                         ref_name = mid + 1;
500                         ref = xmalloc(sizeof(struct ref) +
501                                       strlen(ref_name) + 1);
502                         memset(ref, 0, sizeof(struct ref));
503                         strcpy(ref->name, ref_name);
504                         get_sha1_hex(start, ref->old_sha1);
505                         if (!refs)
506                                 refs = ref;
507                         if (last_ref)
508                                 last_ref->next = ref;
509                         last_ref = ref;
510                         start = NULL;
511                 }
512                 i++;
513         }
514
515         free(buffer.buffer);
516
517         return refs;
518 }
519
520 static int fetch_objs_via_curl(struct transport *transport,
521                                  int nr_objs, struct ref **to_fetch)
522 {
523         if (!transport->data)
524                 transport->data = get_http_walker(transport->url);
525         return fetch_objs_via_walker(transport, nr_objs, to_fetch);
526 }
527
528 #endif
529
530 struct bundle_transport_data {
531         int fd;
532         struct bundle_header header;
533 };
534
535 static struct ref *get_refs_from_bundle(struct transport *transport)
536 {
537         struct bundle_transport_data *data = transport->data;
538         struct ref *result = NULL;
539         int i;
540
541         if (data->fd > 0)
542                 close(data->fd);
543         data->fd = read_bundle_header(transport->url, &data->header);
544         if (data->fd < 0)
545                 die ("Could not read bundle '%s'.", transport->url);
546         for (i = 0; i < data->header.references.nr; i++) {
547                 struct ref_list_entry *e = data->header.references.list + i;
548                 struct ref *ref = alloc_ref(strlen(e->name) + 1);
549                 hashcpy(ref->old_sha1, e->sha1);
550                 strcpy(ref->name, e->name);
551                 ref->next = result;
552                 result = ref;
553         }
554         return result;
555 }
556
557 static int fetch_refs_from_bundle(struct transport *transport,
558                                int nr_heads, struct ref **to_fetch)
559 {
560         struct bundle_transport_data *data = transport->data;
561         return unbundle(&data->header, data->fd);
562 }
563
564 static int close_bundle(struct transport *transport)
565 {
566         struct bundle_transport_data *data = transport->data;
567         if (data->fd > 0)
568                 close(data->fd);
569         free(data);
570         return 0;
571 }
572
573 struct git_transport_data {
574         unsigned thin : 1;
575         unsigned keep : 1;
576         int depth;
577         const char *uploadpack;
578         const char *receivepack;
579 };
580
581 static int set_git_option(struct transport *connection,
582                           const char *name, const char *value)
583 {
584         struct git_transport_data *data = connection->data;
585         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
586                 data->uploadpack = value;
587                 return 0;
588         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
589                 data->receivepack = value;
590                 return 0;
591         } else if (!strcmp(name, TRANS_OPT_THIN)) {
592                 data->thin = !!value;
593                 return 0;
594         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
595                 data->keep = !!value;
596                 return 0;
597         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
598                 if (!value)
599                         data->depth = 0;
600                 else
601                         data->depth = atoi(value);
602                 return 0;
603         }
604         return 1;
605 }
606
607 static struct ref *get_refs_via_connect(struct transport *transport)
608 {
609         struct git_transport_data *data = transport->data;
610         struct ref *refs;
611         int fd[2];
612         char *dest = xstrdup(transport->url);
613         struct child_process *conn = git_connect(fd, dest, data->uploadpack, 0);
614
615         get_remote_heads(fd[0], &refs, 0, NULL, 0);
616         packet_flush(fd[1]);
617
618         finish_connect(conn);
619
620         free(dest);
621
622         return refs;
623 }
624
625 static int fetch_refs_via_pack(struct transport *transport,
626                                int nr_heads, struct ref **to_fetch)
627 {
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         struct ref *refs;
632         char *dest = xstrdup(transport->url);
633         struct fetch_pack_args args;
634         int i;
635
636         memset(&args, 0, sizeof(args));
637         args.uploadpack = data->uploadpack;
638         args.keep_pack = data->keep;
639         args.lock_pack = 1;
640         args.use_thin_pack = data->thin;
641         args.verbose = transport->verbose > 0;
642         args.depth = data->depth;
643
644         for (i = 0; i < nr_heads; i++)
645                 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
646         refs = fetch_pack(&args, dest, nr_heads, heads, &transport->pack_lockfile);
647
648         for (i = 0; i < nr_heads; i++)
649                 free(origh[i]);
650         free(origh);
651         free(heads);
652         free_refs(refs);
653         free(dest);
654         return 0;
655 }
656
657 static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
658 {
659         struct git_transport_data *data = transport->data;
660         struct send_pack_args args;
661
662         args.receivepack = data->receivepack;
663         args.send_all = !!(flags & TRANSPORT_PUSH_ALL);
664         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
665         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
666         args.use_thin_pack = data->thin;
667         args.verbose = transport->verbose;
668         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
669
670         return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec);
671 }
672
673 static int disconnect_git(struct transport *transport)
674 {
675         free(transport->data);
676         return 0;
677 }
678
679 static int is_local(const char *url)
680 {
681         const char *colon = strchr(url, ':');
682         const char *slash = strchr(url, '/');
683         return !colon || (slash && slash < colon);
684 }
685
686 static int is_file(const char *url)
687 {
688         struct stat buf;
689         if (stat(url, &buf))
690                 return 0;
691         return S_ISREG(buf.st_mode);
692 }
693
694 struct transport *transport_get(struct remote *remote, const char *url)
695 {
696         struct transport *ret = xcalloc(1, sizeof(*ret));
697
698         ret->remote = remote;
699         ret->url = url;
700
701         if (!prefixcmp(url, "rsync://")) {
702                 ret->get_refs_list = get_refs_via_rsync;
703                 ret->fetch = fetch_objs_via_rsync;
704                 ret->push = rsync_transport_push;
705
706         } else if (!prefixcmp(url, "http://")
707                 || !prefixcmp(url, "https://")
708                 || !prefixcmp(url, "ftp://")) {
709 #ifdef NO_CURL
710                 error("git was compiled without libcurl support.");
711 #else
712                 ret->get_refs_list = get_refs_via_curl;
713                 ret->fetch = fetch_objs_via_curl;
714                 ret->push = curl_transport_push;
715 #endif
716                 ret->disconnect = disconnect_walker;
717
718         } else if (is_local(url) && is_file(url)) {
719                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
720                 ret->data = data;
721                 ret->get_refs_list = get_refs_from_bundle;
722                 ret->fetch = fetch_refs_from_bundle;
723                 ret->disconnect = close_bundle;
724
725         } else {
726                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
727                 ret->data = data;
728                 ret->set_option = set_git_option;
729                 ret->get_refs_list = get_refs_via_connect;
730                 ret->fetch = fetch_refs_via_pack;
731                 ret->push = git_transport_push;
732                 ret->disconnect = disconnect_git;
733
734                 data->thin = 1;
735                 data->uploadpack = "git-upload-pack";
736                 if (remote && remote->uploadpack)
737                         data->uploadpack = remote->uploadpack;
738                 data->receivepack = "git-receive-pack";
739                 if (remote && remote->receivepack)
740                         data->receivepack = remote->receivepack;
741         }
742
743         return ret;
744 }
745
746 int transport_set_option(struct transport *transport,
747                          const char *name, const char *value)
748 {
749         if (transport->set_option)
750                 return transport->set_option(transport, name, value);
751         return 1;
752 }
753
754 int transport_push(struct transport *transport,
755                    int refspec_nr, const char **refspec, int flags)
756 {
757         if (!transport->push)
758                 return 1;
759         return transport->push(transport, refspec_nr, refspec, flags);
760 }
761
762 const struct ref *transport_get_remote_refs(struct transport *transport)
763 {
764         if (!transport->remote_refs)
765                 transport->remote_refs = transport->get_refs_list(transport);
766         return transport->remote_refs;
767 }
768
769 int transport_fetch_refs(struct transport *transport, struct ref *refs)
770 {
771         int rc;
772         int nr_heads = 0, nr_alloc = 0;
773         struct ref **heads = NULL;
774         struct ref *rm;
775
776         for (rm = refs; rm; rm = rm->next) {
777                 if (rm->peer_ref &&
778                     !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
779                         continue;
780                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
781                 heads[nr_heads++] = rm;
782         }
783
784         rc = transport->fetch(transport, nr_heads, heads);
785         free(heads);
786         return rc;
787 }
788
789 void transport_unlock_pack(struct transport *transport)
790 {
791         if (transport->pack_lockfile) {
792                 unlink(transport->pack_lockfile);
793                 free(transport->pack_lockfile);
794                 transport->pack_lockfile = NULL;
795         }
796 }
797
798 int transport_disconnect(struct transport *transport)
799 {
800         int ret = 0;
801         if (transport->disconnect)
802                 ret = transport->disconnect(transport);
803         free(transport);
804         return ret;
805 }