Code

Merge branch 'zf/maint-gitweb-acname'
[git.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"
15 /* rsync support */
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  */
26 static int str_cmp(const void *a, const void *b)
27 {
28         const char *s1 = a;
29         const char *s2 = b;
31         return strcmp(s1, s2);
32 }
34 /* path->buf + name_offset is expected to point to "refs/" */
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;
47         if (!dir)
48                 return -1;
50         memset (&list, 0, sizeof(list));
52         while ((de = readdir(dir))) {
53                 if (is_dot_or_dotdot(de->d_name))
54                         continue;
55                 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
56                 list.entries[list.nr++] = xstrdup(de->d_name);
57         }
58         closedir(dir);
60         /* sort the list */
62         qsort(list.entries, list.nr, sizeof(char *), str_cmp);
64         pathlen = path->len;
65         strbuf_addch(path, '/');
67         for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
68                 strbuf_addstr(path, list.entries[i]);
69                 if (read_loose_refs(path, name_offset, tail)) {
70                         int fd = open(path->buf, O_RDONLY);
71                         char buffer[40];
72                         struct ref *next;
74                         if (fd < 0)
75                                 continue;
76                         next = alloc_ref(path->buf + name_offset);
77                         if (read_in_full(fd, buffer, 40) != 40 ||
78                                         get_sha1_hex(buffer, next->old_sha1)) {
79                                 close(fd);
80                                 free(next);
81                                 continue;
82                         }
83                         close(fd);
84                         (*tail)->next = next;
85                         *tail = next;
86                 }
87         }
88         strbuf_setlen(path, pathlen);
90         for (i = 0; i < list.nr; i++)
91                 free(list.entries[i]);
92         free(list.entries);
94         return 0;
95 }
97 /* insert the packed refs for which no loose refs were found */
99 static void insert_packed_refs(const char *packed_refs, struct ref **list)
101         FILE *f = fopen(packed_refs, "r");
102         static char buffer[PATH_MAX];
104         if (!f)
105                 return;
107         for (;;) {
108                 int cmp = cmp, len;
110                 if (!fgets(buffer, sizeof(buffer), f)) {
111                         fclose(f);
112                         return;
113                 }
115                 if (hexval(buffer[0]) > 0xf)
116                         continue;
117                 len = strlen(buffer);
118                 if (len && buffer[len - 1] == '\n')
119                         buffer[--len] = '\0';
120                 if (len < 41)
121                         continue;
122                 while ((*list)->next &&
123                                 (cmp = strcmp(buffer + 41,
124                                       (*list)->next->name)) > 0)
125                         list = &(*list)->next;
126                 if (!(*list)->next || cmp < 0) {
127                         struct ref *next = alloc_ref(buffer + 41);
128                         buffer[40] = '\0';
129                         if (get_sha1_hex(buffer, next->old_sha1)) {
130                                 warning ("invalid SHA-1: %s", buffer);
131                                 free(next);
132                                 continue;
133                         }
134                         next->next = (*list)->next;
135                         (*list)->next = next;
136                         list = &(*list)->next;
137                 }
138         }
141 static const char *rsync_url(const char *url)
143         return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
146 static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
148         struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
149         struct ref dummy, *tail = &dummy;
150         struct child_process rsync;
151         const char *args[5];
152         int temp_dir_len;
154         if (for_push)
155                 return NULL;
157         /* copy the refs to the temporary directory */
159         strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
160         if (!mkdtemp(temp_dir.buf))
161                 die_errno ("Could not make temporary directory");
162         temp_dir_len = temp_dir.len;
164         strbuf_addstr(&buf, rsync_url(transport->url));
165         strbuf_addstr(&buf, "/refs");
167         memset(&rsync, 0, sizeof(rsync));
168         rsync.argv = args;
169         rsync.stdout_to_stderr = 1;
170         args[0] = "rsync";
171         args[1] = (transport->verbose > 0) ? "-rv" : "-r";
172         args[2] = buf.buf;
173         args[3] = temp_dir.buf;
174         args[4] = NULL;
176         if (run_command(&rsync))
177                 die ("Could not run rsync to get refs");
179         strbuf_reset(&buf);
180         strbuf_addstr(&buf, rsync_url(transport->url));
181         strbuf_addstr(&buf, "/packed-refs");
183         args[2] = buf.buf;
185         if (run_command(&rsync))
186                 die ("Could not run rsync to get refs");
188         /* read the copied refs */
190         strbuf_addstr(&temp_dir, "/refs");
191         read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
192         strbuf_setlen(&temp_dir, temp_dir_len);
194         tail = &dummy;
195         strbuf_addstr(&temp_dir, "/packed-refs");
196         insert_packed_refs(temp_dir.buf, &tail);
197         strbuf_setlen(&temp_dir, temp_dir_len);
199         if (remove_dir_recursively(&temp_dir, 0))
200                 warning ("Error removing temporary directory %s.",
201                                 temp_dir.buf);
203         strbuf_release(&buf);
204         strbuf_release(&temp_dir);
206         return dummy.next;
209 static int fetch_objs_via_rsync(struct transport *transport,
210                                 int nr_objs, const struct ref **to_fetch)
212         struct strbuf buf = STRBUF_INIT;
213         struct child_process rsync;
214         const char *args[8];
215         int result;
217         strbuf_addstr(&buf, rsync_url(transport->url));
218         strbuf_addstr(&buf, "/objects/");
220         memset(&rsync, 0, sizeof(rsync));
221         rsync.argv = args;
222         rsync.stdout_to_stderr = 1;
223         args[0] = "rsync";
224         args[1] = (transport->verbose > 0) ? "-rv" : "-r";
225         args[2] = "--ignore-existing";
226         args[3] = "--exclude";
227         args[4] = "info";
228         args[5] = buf.buf;
229         args[6] = get_object_directory();
230         args[7] = NULL;
232         /* NEEDSWORK: handle one level of alternates */
233         result = run_command(&rsync);
235         strbuf_release(&buf);
237         return result;
240 static int write_one_ref(const char *name, const unsigned char *sha1,
241                 int flags, void *data)
243         struct strbuf *buf = data;
244         int len = buf->len;
245         FILE *f;
247         /* when called via for_each_ref(), flags is non-zero */
248         if (flags && prefixcmp(name, "refs/heads/") &&
249                         prefixcmp(name, "refs/tags/"))
250                 return 0;
252         strbuf_addstr(buf, name);
253         if (safe_create_leading_directories(buf->buf) ||
254                         !(f = fopen(buf->buf, "w")) ||
255                         fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
256                         fclose(f))
257                 return error("problems writing temporary file %s", buf->buf);
258         strbuf_setlen(buf, len);
259         return 0;
262 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
263                 int refspec_nr, const char **refspec)
265         int i;
267         for (i = 0; i < refspec_nr; i++) {
268                 unsigned char sha1[20];
269                 char *ref;
271                 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
272                         return error("Could not get ref %s", refspec[i]);
274                 if (write_one_ref(ref, sha1, 0, temp_dir)) {
275                         free(ref);
276                         return -1;
277                 }
278                 free(ref);
279         }
280         return 0;
283 static int rsync_transport_push(struct transport *transport,
284                 int refspec_nr, const char **refspec, int flags)
286         struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
287         int result = 0, i;
288         struct child_process rsync;
289         const char *args[10];
291         if (flags & TRANSPORT_PUSH_MIRROR)
292                 return error("rsync transport does not support mirror mode");
294         /* first push the objects */
296         strbuf_addstr(&buf, rsync_url(transport->url));
297         strbuf_addch(&buf, '/');
299         memset(&rsync, 0, sizeof(rsync));
300         rsync.argv = args;
301         rsync.stdout_to_stderr = 1;
302         i = 0;
303         args[i++] = "rsync";
304         args[i++] = "-a";
305         if (flags & TRANSPORT_PUSH_DRY_RUN)
306                 args[i++] = "--dry-run";
307         if (transport->verbose > 0)
308                 args[i++] = "-v";
309         args[i++] = "--ignore-existing";
310         args[i++] = "--exclude";
311         args[i++] = "info";
312         args[i++] = get_object_directory();
313         args[i++] = buf.buf;
314         args[i++] = NULL;
316         if (run_command(&rsync))
317                 return error("Could not push objects to %s",
318                                 rsync_url(transport->url));
320         /* copy the refs to the temporary directory; they could be packed. */
322         strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
323         if (!mkdtemp(temp_dir.buf))
324                 die_errno ("Could not make temporary directory");
325         strbuf_addch(&temp_dir, '/');
327         if (flags & TRANSPORT_PUSH_ALL) {
328                 if (for_each_ref(write_one_ref, &temp_dir))
329                         return -1;
330         } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
331                 return -1;
333         i = 2;
334         if (flags & TRANSPORT_PUSH_DRY_RUN)
335                 args[i++] = "--dry-run";
336         if (!(flags & TRANSPORT_PUSH_FORCE))
337                 args[i++] = "--ignore-existing";
338         args[i++] = temp_dir.buf;
339         args[i++] = rsync_url(transport->url);
340         args[i++] = NULL;
341         if (run_command(&rsync))
342                 result = error("Could not push to %s",
343                                 rsync_url(transport->url));
345         if (remove_dir_recursively(&temp_dir, 0))
346                 warning ("Could not remove temporary directory %s.",
347                                 temp_dir.buf);
349         strbuf_release(&buf);
350         strbuf_release(&temp_dir);
352         return result;
355 /* Generic functions for using commit walkers */
357 #ifndef NO_CURL /* http fetch is the only user */
358 static int fetch_objs_via_walker(struct transport *transport,
359                                  int nr_objs, const struct ref **to_fetch)
361         char *dest = xstrdup(transport->url);
362         struct walker *walker = transport->data;
363         char **objs = xmalloc(nr_objs * sizeof(*objs));
364         int i;
366         walker->get_all = 1;
367         walker->get_tree = 1;
368         walker->get_history = 1;
369         walker->get_verbosely = transport->verbose >= 0;
370         walker->get_recover = 0;
372         for (i = 0; i < nr_objs; i++)
373                 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
375         if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
376                 die("Fetch failed.");
378         for (i = 0; i < nr_objs; i++)
379                 free(objs[i]);
380         free(objs);
381         free(dest);
382         return 0;
384 #endif /* NO_CURL */
386 static int disconnect_walker(struct transport *transport)
388         struct walker *walker = transport->data;
389         if (walker)
390                 walker_free(walker);
391         return 0;
394 #ifndef NO_CURL
395 static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
397         const char **argv;
398         int argc;
400         if (flags & TRANSPORT_PUSH_MIRROR)
401                 return error("http transport does not support mirror mode");
403         argv = xmalloc((refspec_nr + 12) * sizeof(char *));
404         argv[0] = "http-push";
405         argc = 1;
406         if (flags & TRANSPORT_PUSH_ALL)
407                 argv[argc++] = "--all";
408         if (flags & TRANSPORT_PUSH_FORCE)
409                 argv[argc++] = "--force";
410         if (flags & TRANSPORT_PUSH_DRY_RUN)
411                 argv[argc++] = "--dry-run";
412         if (flags & TRANSPORT_PUSH_VERBOSE)
413                 argv[argc++] = "--verbose";
414         argv[argc++] = transport->url;
415         while (refspec_nr--)
416                 argv[argc++] = *refspec++;
417         argv[argc] = NULL;
418         return !!run_command_v_opt(argv, RUN_GIT_CMD);
421 static struct ref *get_refs_via_curl(struct transport *transport, int for_push)
423         struct strbuf buffer = STRBUF_INIT;
424         char *data, *start, *mid;
425         char *ref_name;
426         char *refs_url;
427         int i = 0;
428         int http_ret;
430         struct ref *refs = NULL;
431         struct ref *ref = NULL;
432         struct ref *last_ref = NULL;
434         struct walker *walker;
436         if (for_push)
437                 return NULL;
439         if (!transport->data)
440                 transport->data = get_http_walker(transport->url,
441                                                 transport->remote);
443         walker = transport->data;
445         refs_url = xmalloc(strlen(transport->url) + 11);
446         sprintf(refs_url, "%s/info/refs", transport->url);
448         http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
449         switch (http_ret) {
450         case HTTP_OK:
451                 break;
452         case HTTP_MISSING_TARGET:
453                 die("%s not found: did you run git update-server-info on the"
454                     " server?", refs_url);
455         default:
456                 http_error(refs_url, http_ret);
457                 die("HTTP request failed");
458         }
460         data = buffer.buf;
461         start = NULL;
462         mid = data;
463         while (i < buffer.len) {
464                 if (!start)
465                         start = &data[i];
466                 if (data[i] == '\t')
467                         mid = &data[i];
468                 if (data[i] == '\n') {
469                         data[i] = 0;
470                         ref_name = mid + 1;
471                         ref = xmalloc(sizeof(struct ref) +
472                                       strlen(ref_name) + 1);
473                         memset(ref, 0, sizeof(struct ref));
474                         strcpy(ref->name, ref_name);
475                         get_sha1_hex(start, ref->old_sha1);
476                         if (!refs)
477                                 refs = ref;
478                         if (last_ref)
479                                 last_ref->next = ref;
480                         last_ref = ref;
481                         start = NULL;
482                 }
483                 i++;
484         }
486         strbuf_release(&buffer);
488         ref = alloc_ref("HEAD");
489         if (!walker->fetch_ref(walker, ref) &&
490             !resolve_remote_symref(ref, refs)) {
491                 ref->next = refs;
492                 refs = ref;
493         } else {
494                 free(ref);
495         }
497         strbuf_release(&buffer);
498         free(refs_url);
499         return refs;
502 static int fetch_objs_via_curl(struct transport *transport,
503                                  int nr_objs, const struct ref **to_fetch)
505         if (!transport->data)
506                 transport->data = get_http_walker(transport->url,
507                                                 transport->remote);
508         return fetch_objs_via_walker(transport, nr_objs, to_fetch);
511 #endif
513 struct bundle_transport_data {
514         int fd;
515         struct bundle_header header;
516 };
518 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
520         struct bundle_transport_data *data = transport->data;
521         struct ref *result = NULL;
522         int i;
524         if (for_push)
525                 return NULL;
527         if (data->fd > 0)
528                 close(data->fd);
529         data->fd = read_bundle_header(transport->url, &data->header);
530         if (data->fd < 0)
531                 die ("Could not read bundle '%s'.", transport->url);
532         for (i = 0; i < data->header.references.nr; i++) {
533                 struct ref_list_entry *e = data->header.references.list + i;
534                 struct ref *ref = alloc_ref(e->name);
535                 hashcpy(ref->old_sha1, e->sha1);
536                 ref->next = result;
537                 result = ref;
538         }
539         return result;
542 static int fetch_refs_from_bundle(struct transport *transport,
543                                int nr_heads, const struct ref **to_fetch)
545         struct bundle_transport_data *data = transport->data;
546         return unbundle(&data->header, data->fd);
549 static int close_bundle(struct transport *transport)
551         struct bundle_transport_data *data = transport->data;
552         if (data->fd > 0)
553                 close(data->fd);
554         free(data);
555         return 0;
558 struct git_transport_data {
559         unsigned thin : 1;
560         unsigned keep : 1;
561         unsigned followtags : 1;
562         int depth;
563         struct child_process *conn;
564         int fd[2];
565         const char *uploadpack;
566         const char *receivepack;
567         struct extra_have_objects extra_have;
568 };
570 static int set_git_option(struct transport *connection,
571                           const char *name, const char *value)
573         struct git_transport_data *data = connection->data;
574         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
575                 data->uploadpack = value;
576                 return 0;
577         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
578                 data->receivepack = value;
579                 return 0;
580         } else if (!strcmp(name, TRANS_OPT_THIN)) {
581                 data->thin = !!value;
582                 return 0;
583         } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
584                 data->followtags = !!value;
585                 return 0;
586         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
587                 data->keep = !!value;
588                 return 0;
589         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
590                 if (!value)
591                         data->depth = 0;
592                 else
593                         data->depth = atoi(value);
594                 return 0;
595         }
596         return 1;
599 static int connect_setup(struct transport *transport, int for_push, int verbose)
601         struct git_transport_data *data = transport->data;
602         data->conn = git_connect(data->fd, transport->url,
603                                  for_push ? data->receivepack : data->uploadpack,
604                                  verbose ? CONNECT_VERBOSE : 0);
605         return 0;
608 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
610         struct git_transport_data *data = transport->data;
611         struct ref *refs;
613         connect_setup(transport, for_push, 0);
614         get_remote_heads(data->fd[0], &refs, 0, NULL,
615                          for_push ? REF_NORMAL : 0, &data->extra_have);
617         return refs;
620 static int fetch_refs_via_pack(struct transport *transport,
621                                int nr_heads, const struct ref **to_fetch)
623         struct git_transport_data *data = transport->data;
624         char **heads = xmalloc(nr_heads * sizeof(*heads));
625         char **origh = xmalloc(nr_heads * sizeof(*origh));
626         const struct ref *refs;
627         char *dest = xstrdup(transport->url);
628         struct fetch_pack_args args;
629         int i;
630         struct ref *refs_tmp = NULL;
632         memset(&args, 0, sizeof(args));
633         args.uploadpack = data->uploadpack;
634         args.keep_pack = data->keep;
635         args.lock_pack = 1;
636         args.use_thin_pack = data->thin;
637         args.include_tag = data->followtags;
638         args.verbose = (transport->verbose > 0);
639         args.quiet = (transport->verbose < 0);
640         args.no_progress = args.quiet || (!transport->progress && !isatty(1));
641         args.depth = data->depth;
643         for (i = 0; i < nr_heads; i++)
644                 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
646         if (!data->conn) {
647                 connect_setup(transport, 0, 0);
648                 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
649         }
651         refs = fetch_pack(&args, data->fd, data->conn,
652                           refs_tmp ? refs_tmp : transport->remote_refs,
653                           dest, nr_heads, heads, &transport->pack_lockfile);
654         close(data->fd[0]);
655         close(data->fd[1]);
656         if (finish_connect(data->conn))
657                 refs = NULL;
658         data->conn = NULL;
660         free_refs(refs_tmp);
662         for (i = 0; i < nr_heads; i++)
663                 free(origh[i]);
664         free(origh);
665         free(heads);
666         free(dest);
667         return (refs ? 0 : -1);
670 static int refs_pushed(struct ref *ref)
672         for (; ref; ref = ref->next) {
673                 switch(ref->status) {
674                 case REF_STATUS_NONE:
675                 case REF_STATUS_UPTODATE:
676                         break;
677                 default:
678                         return 1;
679                 }
680         }
681         return 0;
684 static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
686         struct refspec rs;
688         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
689                 return;
691         rs.src = ref->name;
692         rs.dst = NULL;
694         if (!remote_find_tracking(remote, &rs)) {
695                 if (verbose)
696                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
697                 if (ref->deletion) {
698                         delete_ref(rs.dst, NULL, 0);
699                 } else
700                         update_ref("update by push", rs.dst,
701                                         ref->new_sha1, NULL, 0, 0);
702                 free(rs.dst);
703         }
706 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
708 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
710         if (porcelain) {
711                 if (from)
712                         fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
713                 else
714                         fprintf(stdout, "%c\t:%s\t", flag, to->name);
715                 if (msg)
716                         fprintf(stdout, "%s (%s)\n", summary, msg);
717                 else
718                         fprintf(stdout, "%s\n", summary);
719         } else {
720                 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
721                 if (from)
722                         fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
723                 else
724                         fputs(prettify_refname(to->name), stderr);
725                 if (msg) {
726                         fputs(" (", stderr);
727                         fputs(msg, stderr);
728                         fputc(')', stderr);
729                 }
730                 fputc('\n', stderr);
731         }
734 static const char *status_abbrev(unsigned char sha1[20])
736         return find_unique_abbrev(sha1, DEFAULT_ABBREV);
739 static void print_ok_ref_status(struct ref *ref, int porcelain)
741         if (ref->deletion)
742                 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
743         else if (is_null_sha1(ref->old_sha1))
744                 print_ref_status('*',
745                         (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
746                         "[new branch]"),
747                         ref, ref->peer_ref, NULL, porcelain);
748         else {
749                 char quickref[84];
750                 char type;
751                 const char *msg;
753                 strcpy(quickref, status_abbrev(ref->old_sha1));
754                 if (ref->nonfastforward) {
755                         strcat(quickref, "...");
756                         type = '+';
757                         msg = "forced update";
758                 } else {
759                         strcat(quickref, "..");
760                         type = ' ';
761                         msg = NULL;
762                 }
763                 strcat(quickref, status_abbrev(ref->new_sha1));
765                 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
766         }
769 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
771         if (!count)
772                 fprintf(stderr, "To %s\n", dest);
774         switch(ref->status) {
775         case REF_STATUS_NONE:
776                 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
777                 break;
778         case REF_STATUS_REJECT_NODELETE:
779                 print_ref_status('!', "[rejected]", ref, NULL,
780                                                  "remote does not support deleting refs", porcelain);
781                 break;
782         case REF_STATUS_UPTODATE:
783                 print_ref_status('=', "[up to date]", ref,
784                                                  ref->peer_ref, NULL, porcelain);
785                 break;
786         case REF_STATUS_REJECT_NONFASTFORWARD:
787                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
788                                                  "non-fast forward", porcelain);
789                 break;
790         case REF_STATUS_REMOTE_REJECT:
791                 print_ref_status('!', "[remote rejected]", ref,
792                                                  ref->deletion ? NULL : ref->peer_ref,
793                                                  ref->remote_status, porcelain);
794                 break;
795         case REF_STATUS_EXPECTING_REPORT:
796                 print_ref_status('!', "[remote failure]", ref,
797                                                  ref->deletion ? NULL : ref->peer_ref,
798                                                  "remote failed to report status", porcelain);
799                 break;
800         case REF_STATUS_OK:
801                 print_ok_ref_status(ref, porcelain);
802                 break;
803         }
805         return 1;
808 static void print_push_status(const char *dest, struct ref *refs,
809                                                           int verbose, int porcelain)
811         struct ref *ref;
812         int n = 0;
814         if (verbose) {
815                 for (ref = refs; ref; ref = ref->next)
816                         if (ref->status == REF_STATUS_UPTODATE)
817                                 n += print_one_push_status(ref, dest, n, porcelain);
818         }
820         for (ref = refs; ref; ref = ref->next)
821                 if (ref->status == REF_STATUS_OK)
822                         n += print_one_push_status(ref, dest, n, porcelain);
824         for (ref = refs; ref; ref = ref->next) {
825                 if (ref->status != REF_STATUS_NONE &&
826                     ref->status != REF_STATUS_UPTODATE &&
827                     ref->status != REF_STATUS_OK)
828                         n += print_one_push_status(ref, dest, n, porcelain);
829         }
832 static void verify_remote_names(int nr_heads, const char **heads)
834         int i;
836         for (i = 0; i < nr_heads; i++) {
837                 const char *local = heads[i];
838                 const char *remote = strrchr(heads[i], ':');
840                 if (*local == '+')
841                         local++;
843                 /* A matching refspec is okay.  */
844                 if (remote == local && remote[1] == '\0')
845                         continue;
847                 remote = remote ? (remote + 1) : local;
848                 switch (check_ref_format(remote)) {
849                 case 0: /* ok */
850                 case CHECK_REF_FORMAT_ONELEVEL:
851                         /* ok but a single level -- that is fine for
852                          * a match pattern.
853                          */
854                 case CHECK_REF_FORMAT_WILDCARD:
855                         /* ok but ends with a pattern-match character */
856                         continue;
857                 }
858                 die("remote part of refspec is not a valid name in %s",
859                     heads[i]);
860         }
863 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
865         struct git_transport_data *data = transport->data;
866         struct send_pack_args args;
867         int ret;
869         if (!data->conn) {
870                 struct ref *tmp_refs;
871                 connect_setup(transport, 1, 0);
873                 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
874                                  NULL);
875         }
877         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
878         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
879         args.use_thin_pack = data->thin;
880         args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
881         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
883         ret = send_pack(&args, data->fd, data->conn, remote_refs,
884                         &data->extra_have);
886         close(data->fd[1]);
887         close(data->fd[0]);
888         ret |= finish_connect(data->conn);
889         data->conn = NULL;
891         return ret;
894 static int disconnect_git(struct transport *transport)
896         struct git_transport_data *data = transport->data;
897         if (data->conn) {
898                 packet_flush(data->fd[1]);
899                 close(data->fd[0]);
900                 close(data->fd[1]);
901                 finish_connect(data->conn);
902         }
904         free(data);
905         return 0;
908 static int is_local(const char *url)
910         const char *colon = strchr(url, ':');
911         const char *slash = strchr(url, '/');
912         return !colon || (slash && slash < colon) ||
913                 has_dos_drive_prefix(url);
916 static int is_file(const char *url)
918         struct stat buf;
919         if (stat(url, &buf))
920                 return 0;
921         return S_ISREG(buf.st_mode);
924 struct transport *transport_get(struct remote *remote, const char *url)
926         struct transport *ret = xcalloc(1, sizeof(*ret));
928         ret->remote = remote;
929         ret->url = url;
931         if (!prefixcmp(url, "rsync:")) {
932                 ret->get_refs_list = get_refs_via_rsync;
933                 ret->fetch = fetch_objs_via_rsync;
934                 ret->push = rsync_transport_push;
936         } else if (!prefixcmp(url, "http://")
937                 || !prefixcmp(url, "https://")
938                 || !prefixcmp(url, "ftp://")) {
939 #ifdef NO_CURL
940                 error("git was compiled without libcurl support.");
941 #else
942                 ret->get_refs_list = get_refs_via_curl;
943                 ret->fetch = fetch_objs_via_curl;
944                 ret->push = curl_transport_push;
945 #endif
946                 ret->disconnect = disconnect_walker;
948         } else if (is_local(url) && is_file(url)) {
949                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
950                 ret->data = data;
951                 ret->get_refs_list = get_refs_from_bundle;
952                 ret->fetch = fetch_refs_from_bundle;
953                 ret->disconnect = close_bundle;
955         } else {
956                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
957                 ret->data = data;
958                 ret->set_option = set_git_option;
959                 ret->get_refs_list = get_refs_via_connect;
960                 ret->fetch = fetch_refs_via_pack;
961                 ret->push_refs = git_transport_push;
962                 ret->disconnect = disconnect_git;
964                 data->thin = 1;
965                 data->conn = NULL;
966                 data->uploadpack = "git-upload-pack";
967                 if (remote && remote->uploadpack)
968                         data->uploadpack = remote->uploadpack;
969                 data->receivepack = "git-receive-pack";
970                 if (remote && remote->receivepack)
971                         data->receivepack = remote->receivepack;
972         }
974         return ret;
977 int transport_set_option(struct transport *transport,
978                          const char *name, const char *value)
980         if (transport->set_option)
981                 return transport->set_option(transport, name, value);
982         return 1;
985 int transport_push(struct transport *transport,
986                    int refspec_nr, const char **refspec, int flags)
988         verify_remote_names(refspec_nr, refspec);
990         if (transport->push)
991                 return transport->push(transport, refspec_nr, refspec, flags);
992         if (transport->push_refs) {
993                 struct ref *remote_refs =
994                         transport->get_refs_list(transport, 1);
995                 struct ref *local_refs = get_local_heads();
996                 int match_flags = MATCH_REFS_NONE;
997                 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
998                 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
999                 int ret;
1001                 if (flags & TRANSPORT_PUSH_ALL)
1002                         match_flags |= MATCH_REFS_ALL;
1003                 if (flags & TRANSPORT_PUSH_MIRROR)
1004                         match_flags |= MATCH_REFS_MIRROR;
1006                 if (match_refs(local_refs, &remote_refs,
1007                                refspec_nr, refspec, match_flags)) {
1008                         return -1;
1009                 }
1011                 ret = transport->push_refs(transport, remote_refs, flags);
1013                 print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain);
1015                 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1016                         struct ref *ref;
1017                         for (ref = remote_refs; ref; ref = ref->next)
1018                                 update_tracking_ref(transport->remote, ref, verbose);
1019                 }
1021                 if (!ret && !refs_pushed(remote_refs))
1022                         fprintf(stderr, "Everything up-to-date\n");
1023                 return ret;
1024         }
1025         return 1;
1028 const struct ref *transport_get_remote_refs(struct transport *transport)
1030         if (!transport->remote_refs)
1031                 transport->remote_refs = transport->get_refs_list(transport, 0);
1032         return transport->remote_refs;
1035 int transport_fetch_refs(struct transport *transport, const struct ref *refs)
1037         int rc;
1038         int nr_heads = 0, nr_alloc = 0;
1039         const struct ref **heads = NULL;
1040         const struct ref *rm;
1042         for (rm = refs; rm; rm = rm->next) {
1043                 if (rm->peer_ref &&
1044                     !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1045                         continue;
1046                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1047                 heads[nr_heads++] = rm;
1048         }
1050         rc = transport->fetch(transport, nr_heads, heads);
1051         free(heads);
1052         return rc;
1055 void transport_unlock_pack(struct transport *transport)
1057         if (transport->pack_lockfile) {
1058                 unlink_or_warn(transport->pack_lockfile);
1059                 free(transport->pack_lockfile);
1060                 transport->pack_lockfile = NULL;
1061         }
1064 int transport_disconnect(struct transport *transport)
1066         int ret = 0;
1067         if (transport->disconnect)
1068                 ret = transport->disconnect(transport);
1069         free(transport);
1070         return ret;
1073 /*
1074  * Strip username (and password) from an url and return
1075  * it in a newly allocated string.
1076  */
1077 char *transport_anonymize_url(const char *url)
1079         char *anon_url, *scheme_prefix, *anon_part;
1080         size_t anon_len, prefix_len = 0;
1082         anon_part = strchr(url, '@');
1083         if (is_local(url) || !anon_part)
1084                 goto literal_copy;
1086         anon_len = strlen(++anon_part);
1087         scheme_prefix = strstr(url, "://");
1088         if (!scheme_prefix) {
1089                 if (!strchr(anon_part, ':'))
1090                         /* cannot be "me@there:/path/name" */
1091                         goto literal_copy;
1092         } else {
1093                 const char *cp;
1094                 /* make sure scheme is reasonable */
1095                 for (cp = url; cp < scheme_prefix; cp++) {
1096                         switch (*cp) {
1097                                 /* RFC 1738 2.1 */
1098                         case '+': case '.': case '-':
1099                                 break; /* ok */
1100                         default:
1101                                 if (isalnum(*cp))
1102                                         break;
1103                                 /* it isn't */
1104                                 goto literal_copy;
1105                         }
1106                 }
1107                 /* @ past the first slash does not count */
1108                 cp = strchr(scheme_prefix + 3, '/');
1109                 if (cp && cp < anon_part)
1110                         goto literal_copy;
1111                 prefix_len = scheme_prefix - url + 3;
1112         }
1113         anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1114         memcpy(anon_url, url, prefix_len);
1115         memcpy(anon_url + prefix_len, anon_part, anon_len);
1116         return anon_url;
1117 literal_copy:
1118         return xstrdup(url);