Code

Move push matching and reporting logic into transport.c
[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 struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
143         struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
144         struct ref dummy, *tail = &dummy;
145         struct child_process rsync;
146         const char *args[5];
147         int temp_dir_len;
149         if (for_push)
150                 return NULL;
152         /* copy the refs to the temporary directory */
154         strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
155         if (!mkdtemp(temp_dir.buf))
156                 die ("Could not make temporary directory");
157         temp_dir_len = temp_dir.len;
159         strbuf_addstr(&buf, transport->url);
160         strbuf_addstr(&buf, "/refs");
162         memset(&rsync, 0, sizeof(rsync));
163         rsync.argv = args;
164         rsync.stdout_to_stderr = 1;
165         args[0] = "rsync";
166         args[1] = (transport->verbose > 0) ? "-rv" : "-r";
167         args[2] = buf.buf;
168         args[3] = temp_dir.buf;
169         args[4] = NULL;
171         if (run_command(&rsync))
172                 die ("Could not run rsync to get refs");
174         strbuf_reset(&buf);
175         strbuf_addstr(&buf, transport->url);
176         strbuf_addstr(&buf, "/packed-refs");
178         args[2] = buf.buf;
180         if (run_command(&rsync))
181                 die ("Could not run rsync to get refs");
183         /* read the copied refs */
185         strbuf_addstr(&temp_dir, "/refs");
186         read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
187         strbuf_setlen(&temp_dir, temp_dir_len);
189         tail = &dummy;
190         strbuf_addstr(&temp_dir, "/packed-refs");
191         insert_packed_refs(temp_dir.buf, &tail);
192         strbuf_setlen(&temp_dir, temp_dir_len);
194         if (remove_dir_recursively(&temp_dir, 0))
195                 warning ("Error removing temporary directory %s.",
196                                 temp_dir.buf);
198         strbuf_release(&buf);
199         strbuf_release(&temp_dir);
201         return dummy.next;
204 static int fetch_objs_via_rsync(struct transport *transport,
205                                 int nr_objs, const struct ref **to_fetch)
207         struct strbuf buf = STRBUF_INIT;
208         struct child_process rsync;
209         const char *args[8];
210         int result;
212         strbuf_addstr(&buf, transport->url);
213         strbuf_addstr(&buf, "/objects/");
215         memset(&rsync, 0, sizeof(rsync));
216         rsync.argv = args;
217         rsync.stdout_to_stderr = 1;
218         args[0] = "rsync";
219         args[1] = (transport->verbose > 0) ? "-rv" : "-r";
220         args[2] = "--ignore-existing";
221         args[3] = "--exclude";
222         args[4] = "info";
223         args[5] = buf.buf;
224         args[6] = get_object_directory();
225         args[7] = NULL;
227         /* NEEDSWORK: handle one level of alternates */
228         result = run_command(&rsync);
230         strbuf_release(&buf);
232         return result;
235 static int write_one_ref(const char *name, const unsigned char *sha1,
236                 int flags, void *data)
238         struct strbuf *buf = data;
239         int len = buf->len;
240         FILE *f;
242         /* when called via for_each_ref(), flags is non-zero */
243         if (flags && prefixcmp(name, "refs/heads/") &&
244                         prefixcmp(name, "refs/tags/"))
245                 return 0;
247         strbuf_addstr(buf, name);
248         if (safe_create_leading_directories(buf->buf) ||
249                         !(f = fopen(buf->buf, "w")) ||
250                         fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
251                         fclose(f))
252                 return error("problems writing temporary file %s", buf->buf);
253         strbuf_setlen(buf, len);
254         return 0;
257 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
258                 int refspec_nr, const char **refspec)
260         int i;
262         for (i = 0; i < refspec_nr; i++) {
263                 unsigned char sha1[20];
264                 char *ref;
266                 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
267                         return error("Could not get ref %s", refspec[i]);
269                 if (write_one_ref(ref, sha1, 0, temp_dir)) {
270                         free(ref);
271                         return -1;
272                 }
273                 free(ref);
274         }
275         return 0;
278 static int rsync_transport_push(struct transport *transport,
279                 int refspec_nr, const char **refspec, int flags)
281         struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
282         int result = 0, i;
283         struct child_process rsync;
284         const char *args[10];
286         if (flags & TRANSPORT_PUSH_MIRROR)
287                 return error("rsync transport does not support mirror mode");
289         /* first push the objects */
291         strbuf_addstr(&buf, transport->url);
292         strbuf_addch(&buf, '/');
294         memset(&rsync, 0, sizeof(rsync));
295         rsync.argv = args;
296         rsync.stdout_to_stderr = 1;
297         i = 0;
298         args[i++] = "rsync";
299         args[i++] = "-a";
300         if (flags & TRANSPORT_PUSH_DRY_RUN)
301                 args[i++] = "--dry-run";
302         if (transport->verbose > 0)
303                 args[i++] = "-v";
304         args[i++] = "--ignore-existing";
305         args[i++] = "--exclude";
306         args[i++] = "info";
307         args[i++] = get_object_directory();
308         args[i++] = buf.buf;
309         args[i++] = NULL;
311         if (run_command(&rsync))
312                 return error("Could not push objects to %s", transport->url);
314         /* copy the refs to the temporary directory; they could be packed. */
316         strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
317         if (!mkdtemp(temp_dir.buf))
318                 die ("Could not make temporary directory");
319         strbuf_addch(&temp_dir, '/');
321         if (flags & TRANSPORT_PUSH_ALL) {
322                 if (for_each_ref(write_one_ref, &temp_dir))
323                         return -1;
324         } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
325                 return -1;
327         i = 2;
328         if (flags & TRANSPORT_PUSH_DRY_RUN)
329                 args[i++] = "--dry-run";
330         if (!(flags & TRANSPORT_PUSH_FORCE))
331                 args[i++] = "--ignore-existing";
332         args[i++] = temp_dir.buf;
333         args[i++] = transport->url;
334         args[i++] = NULL;
335         if (run_command(&rsync))
336                 result = error("Could not push to %s", transport->url);
338         if (remove_dir_recursively(&temp_dir, 0))
339                 warning ("Could not remove temporary directory %s.",
340                                 temp_dir.buf);
342         strbuf_release(&buf);
343         strbuf_release(&temp_dir);
345         return result;
348 /* Generic functions for using commit walkers */
350 #ifndef NO_CURL /* http fetch is the only user */
351 static int fetch_objs_via_walker(struct transport *transport,
352                                  int nr_objs, const struct ref **to_fetch)
354         char *dest = xstrdup(transport->url);
355         struct walker *walker = transport->data;
356         char **objs = xmalloc(nr_objs * sizeof(*objs));
357         int i;
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;
365         for (i = 0; i < nr_objs; i++)
366                 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
368         if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
369                 die("Fetch failed.");
371         for (i = 0; i < nr_objs; i++)
372                 free(objs[i]);
373         free(objs);
374         free(dest);
375         return 0;
377 #endif /* NO_CURL */
379 static int disconnect_walker(struct transport *transport)
381         struct walker *walker = transport->data;
382         if (walker)
383                 walker_free(walker);
384         return 0;
387 #ifndef NO_CURL
388 static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
390         const char **argv;
391         int argc;
392         int err;
394         if (flags & TRANSPORT_PUSH_MIRROR)
395                 return error("http transport does not support mirror mode");
397         argv = xmalloc((refspec_nr + 12) * sizeof(char *));
398         argv[0] = "http-push";
399         argc = 1;
400         if (flags & TRANSPORT_PUSH_ALL)
401                 argv[argc++] = "--all";
402         if (flags & TRANSPORT_PUSH_FORCE)
403                 argv[argc++] = "--force";
404         if (flags & TRANSPORT_PUSH_DRY_RUN)
405                 argv[argc++] = "--dry-run";
406         if (flags & TRANSPORT_PUSH_VERBOSE)
407                 argv[argc++] = "--verbose";
408         argv[argc++] = transport->url;
409         while (refspec_nr--)
410                 argv[argc++] = *refspec++;
411         argv[argc] = NULL;
412         err = run_command_v_opt(argv, RUN_GIT_CMD);
413         switch (err) {
414         case -ERR_RUN_COMMAND_FORK:
415                 error("unable to fork for %s", argv[0]);
416         case -ERR_RUN_COMMAND_EXEC:
417                 error("unable to exec %s", argv[0]);
418                 break;
419         case -ERR_RUN_COMMAND_WAITPID:
420         case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
421         case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
422         case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
423                 error("%s died with strange error", argv[0]);
424         }
425         return !!err;
428 static struct ref *get_refs_via_curl(struct transport *transport, int for_push)
430         struct strbuf buffer = STRBUF_INIT;
431         char *data, *start, *mid;
432         char *ref_name;
433         char *refs_url;
434         int i = 0;
436         struct active_request_slot *slot;
437         struct slot_results results;
439         struct ref *refs = NULL;
440         struct ref *ref = NULL;
441         struct ref *last_ref = NULL;
443         struct walker *walker;
445         if (for_push)
446                 return NULL;
448         if (!transport->data)
449                 transport->data = get_http_walker(transport->url,
450                                                 transport->remote);
452         walker = transport->data;
454         refs_url = xmalloc(strlen(transport->url) + 11);
455         sprintf(refs_url, "%s/info/refs", transport->url);
457         slot = get_active_slot();
458         slot->results = &results;
459         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
460         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
461         curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
462         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
464         if (start_active_slot(slot)) {
465                 run_active_slot(slot);
466                 if (results.curl_result != CURLE_OK) {
467                         strbuf_release(&buffer);
468                         if (missing_target(&results))
469                                 die("%s not found: did you run git update-server-info on the server?", refs_url);
470                         else
471                                 die("%s download error - %s", refs_url, curl_errorstr);
472                 }
473         } else {
474                 strbuf_release(&buffer);
475                 die("Unable to start HTTP request");
476         }
478         data = buffer.buf;
479         start = NULL;
480         mid = data;
481         while (i < buffer.len) {
482                 if (!start)
483                         start = &data[i];
484                 if (data[i] == '\t')
485                         mid = &data[i];
486                 if (data[i] == '\n') {
487                         data[i] = 0;
488                         ref_name = mid + 1;
489                         ref = xmalloc(sizeof(struct ref) +
490                                       strlen(ref_name) + 1);
491                         memset(ref, 0, sizeof(struct ref));
492                         strcpy(ref->name, ref_name);
493                         get_sha1_hex(start, ref->old_sha1);
494                         if (!refs)
495                                 refs = ref;
496                         if (last_ref)
497                                 last_ref->next = ref;
498                         last_ref = ref;
499                         start = NULL;
500                 }
501                 i++;
502         }
504         strbuf_release(&buffer);
506         ref = alloc_ref("HEAD");
507         if (!walker->fetch_ref(walker, ref) &&
508             !resolve_remote_symref(ref, refs)) {
509                 ref->next = refs;
510                 refs = ref;
511         } else {
512                 free(ref);
513         }
515         return refs;
518 static int fetch_objs_via_curl(struct transport *transport,
519                                  int nr_objs, const struct ref **to_fetch)
521         if (!transport->data)
522                 transport->data = get_http_walker(transport->url,
523                                                 transport->remote);
524         return fetch_objs_via_walker(transport, nr_objs, to_fetch);
527 #endif
529 struct bundle_transport_data {
530         int fd;
531         struct bundle_header header;
532 };
534 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
536         struct bundle_transport_data *data = transport->data;
537         struct ref *result = NULL;
538         int i;
540         if (for_push)
541                 return NULL;
543         if (data->fd > 0)
544                 close(data->fd);
545         data->fd = read_bundle_header(transport->url, &data->header);
546         if (data->fd < 0)
547                 die ("Could not read bundle '%s'.", transport->url);
548         for (i = 0; i < data->header.references.nr; i++) {
549                 struct ref_list_entry *e = data->header.references.list + i;
550                 struct ref *ref = alloc_ref(e->name);
551                 hashcpy(ref->old_sha1, e->sha1);
552                 ref->next = result;
553                 result = ref;
554         }
555         return result;
558 static int fetch_refs_from_bundle(struct transport *transport,
559                                int nr_heads, const struct ref **to_fetch)
561         struct bundle_transport_data *data = transport->data;
562         return unbundle(&data->header, data->fd);
565 static int close_bundle(struct transport *transport)
567         struct bundle_transport_data *data = transport->data;
568         if (data->fd > 0)
569                 close(data->fd);
570         free(data);
571         return 0;
574 struct git_transport_data {
575         unsigned thin : 1;
576         unsigned keep : 1;
577         unsigned followtags : 1;
578         int depth;
579         struct child_process *conn;
580         int fd[2];
581         const char *uploadpack;
582         const char *receivepack;
583         struct extra_have_objects extra_have;
584 };
586 static int set_git_option(struct transport *connection,
587                           const char *name, const char *value)
589         struct git_transport_data *data = connection->data;
590         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
591                 data->uploadpack = value;
592                 return 0;
593         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
594                 data->receivepack = value;
595                 return 0;
596         } else if (!strcmp(name, TRANS_OPT_THIN)) {
597                 data->thin = !!value;
598                 return 0;
599         } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
600                 data->followtags = !!value;
601                 return 0;
602         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
603                 data->keep = !!value;
604                 return 0;
605         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
606                 if (!value)
607                         data->depth = 0;
608                 else
609                         data->depth = atoi(value);
610                 return 0;
611         }
612         return 1;
615 static int connect_setup(struct transport *transport, int for_push, int verbose)
617         struct git_transport_data *data = transport->data;
618         data->conn = git_connect(data->fd, transport->url,
619                                  for_push ? data->receivepack : data->uploadpack,
620                                  verbose ? CONNECT_VERBOSE : 0);
621         return 0;
624 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
626         struct git_transport_data *data = transport->data;
627         struct ref *refs;
629         connect_setup(transport, for_push, 0);
630         get_remote_heads(data->fd[0], &refs, 0, NULL,
631                          for_push ? REF_NORMAL : 0, &data->extra_have);
633         return refs;
636 static int fetch_refs_via_pack(struct transport *transport,
637                                int nr_heads, const struct ref **to_fetch)
639         struct git_transport_data *data = transport->data;
640         char **heads = xmalloc(nr_heads * sizeof(*heads));
641         char **origh = xmalloc(nr_heads * sizeof(*origh));
642         const struct ref *refs;
643         char *dest = xstrdup(transport->url);
644         struct fetch_pack_args args;
645         int i;
646         struct ref *refs_tmp = NULL;
648         memset(&args, 0, sizeof(args));
649         args.uploadpack = data->uploadpack;
650         args.keep_pack = data->keep;
651         args.lock_pack = 1;
652         args.use_thin_pack = data->thin;
653         args.include_tag = data->followtags;
654         args.verbose = (transport->verbose > 0);
655         args.quiet = (transport->verbose < 0);
656         args.no_progress = args.quiet || (!transport->progress && !isatty(1));
657         args.depth = data->depth;
659         for (i = 0; i < nr_heads; i++)
660                 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
662         if (!data->conn) {
663                 connect_setup(transport, 0, 0);
664                 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
665         }
667         refs = fetch_pack(&args, data->fd, data->conn,
668                           refs_tmp ? refs_tmp : transport->remote_refs,
669                           dest, nr_heads, heads, &transport->pack_lockfile);
670         close(data->fd[0]);
671         close(data->fd[1]);
672         if (finish_connect(data->conn))
673                 refs = NULL;
674         data->conn = NULL;
676         free_refs(refs_tmp);
678         for (i = 0; i < nr_heads; i++)
679                 free(origh[i]);
680         free(origh);
681         free(heads);
682         free(dest);
683         return (refs ? 0 : -1);
686 static int refs_pushed(struct ref *ref)
688         for (; ref; ref = ref->next) {
689                 switch(ref->status) {
690                 case REF_STATUS_NONE:
691                 case REF_STATUS_UPTODATE:
692                         break;
693                 default:
694                         return 1;
695                 }
696         }
697         return 0;
700 static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
702         struct refspec rs;
704         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
705                 return;
707         rs.src = ref->name;
708         rs.dst = NULL;
710         if (!remote_find_tracking(remote, &rs)) {
711                 if (verbose)
712                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
713                 if (ref->deletion) {
714                         delete_ref(rs.dst, NULL, 0);
715                 } else
716                         update_ref("update by push", rs.dst,
717                                         ref->new_sha1, NULL, 0, 0);
718                 free(rs.dst);
719         }
722 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
724 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
726         fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
727         if (from)
728                 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
729         else
730                 fputs(prettify_ref(to), stderr);
731         if (msg) {
732                 fputs(" (", stderr);
733                 fputs(msg, stderr);
734                 fputc(')', stderr);
735         }
736         fputc('\n', stderr);
739 static const char *status_abbrev(unsigned char sha1[20])
741         return find_unique_abbrev(sha1, DEFAULT_ABBREV);
744 static void print_ok_ref_status(struct ref *ref)
746         if (ref->deletion)
747                 print_ref_status('-', "[deleted]", ref, NULL, NULL);
748         else if (is_null_sha1(ref->old_sha1))
749                 print_ref_status('*',
750                         (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
751                           "[new branch]"),
752                         ref, ref->peer_ref, NULL);
753         else {
754                 char quickref[84];
755                 char type;
756                 const char *msg;
758                 strcpy(quickref, status_abbrev(ref->old_sha1));
759                 if (ref->nonfastforward) {
760                         strcat(quickref, "...");
761                         type = '+';
762                         msg = "forced update";
763                 } else {
764                         strcat(quickref, "..");
765                         type = ' ';
766                         msg = NULL;
767                 }
768                 strcat(quickref, status_abbrev(ref->new_sha1));
770                 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
771         }
774 static int print_one_push_status(struct ref *ref, const char *dest, int count)
776         if (!count)
777                 fprintf(stderr, "To %s\n", dest);
779         switch(ref->status) {
780         case REF_STATUS_NONE:
781                 print_ref_status('X', "[no match]", ref, NULL, NULL);
782                 break;
783         case REF_STATUS_REJECT_NODELETE:
784                 print_ref_status('!', "[rejected]", ref, NULL,
785                                 "remote does not support deleting refs");
786                 break;
787         case REF_STATUS_UPTODATE:
788                 print_ref_status('=', "[up to date]", ref,
789                                 ref->peer_ref, NULL);
790                 break;
791         case REF_STATUS_REJECT_NONFASTFORWARD:
792                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
793                                 "non-fast forward");
794                 break;
795         case REF_STATUS_REMOTE_REJECT:
796                 print_ref_status('!', "[remote rejected]", ref,
797                                 ref->deletion ? NULL : ref->peer_ref,
798                                 ref->remote_status);
799                 break;
800         case REF_STATUS_EXPECTING_REPORT:
801                 print_ref_status('!', "[remote failure]", ref,
802                                 ref->deletion ? NULL : ref->peer_ref,
803                                 "remote failed to report status");
804                 break;
805         case REF_STATUS_OK:
806                 print_ok_ref_status(ref);
807                 break;
808         }
810         return 1;
813 static void print_push_status(const char *dest, struct ref *refs, int verbose)
815         struct ref *ref;
816         int n = 0;
818         if (verbose) {
819                 for (ref = refs; ref; ref = ref->next)
820                         if (ref->status == REF_STATUS_UPTODATE)
821                                 n += print_one_push_status(ref, dest, n);
822         }
824         for (ref = refs; ref; ref = ref->next)
825                 if (ref->status == REF_STATUS_OK)
826                         n += print_one_push_status(ref, dest, n);
828         for (ref = refs; ref; ref = ref->next) {
829                 if (ref->status != REF_STATUS_NONE &&
830                     ref->status != REF_STATUS_UPTODATE &&
831                     ref->status != REF_STATUS_OK)
832                         n += print_one_push_status(ref, dest, n);
833         }
836 static void verify_remote_names(int nr_heads, const char **heads)
838         int i;
840         for (i = 0; i < nr_heads; i++) {
841                 const char *local = heads[i];
842                 const char *remote = strrchr(heads[i], ':');
844                 if (*local == '+')
845                         local++;
847                 /* A matching refspec is okay.  */
848                 if (remote == local && remote[1] == '\0')
849                         continue;
851                 remote = remote ? (remote + 1) : local;
852                 switch (check_ref_format(remote)) {
853                 case 0: /* ok */
854                 case CHECK_REF_FORMAT_ONELEVEL:
855                         /* ok but a single level -- that is fine for
856                          * a match pattern.
857                          */
858                 case CHECK_REF_FORMAT_WILDCARD:
859                         /* ok but ends with a pattern-match character */
860                         continue;
861                 }
862                 die("remote part of refspec is not a valid name in %s",
863                     heads[i]);
864         }
867 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
869         struct git_transport_data *data = transport->data;
870         struct send_pack_args args;
871         int ret;
873         if (!data->conn) {
874                 struct ref *tmp_refs;
875                 connect_setup(transport, 1, 0);
877                 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
878                                  NULL);
879         }
881         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
882         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
883         args.use_thin_pack = data->thin;
884         args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
885         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
887         ret = send_pack(&args, data->fd, data->conn, remote_refs,
888                         &data->extra_have);
890         close(data->fd[1]);
891         close(data->fd[0]);
892         ret |= finish_connect(data->conn);
893         data->conn = NULL;
895         return ret;
898 static int disconnect_git(struct transport *transport)
900         struct git_transport_data *data = transport->data;
901         if (data->conn) {
902                 packet_flush(data->fd[1]);
903                 close(data->fd[0]);
904                 close(data->fd[1]);
905                 finish_connect(data->conn);
906         }
908         free(data);
909         return 0;
912 static int is_local(const char *url)
914         const char *colon = strchr(url, ':');
915         const char *slash = strchr(url, '/');
916         return !colon || (slash && slash < colon) ||
917                 has_dos_drive_prefix(url);
920 static int is_file(const char *url)
922         struct stat buf;
923         if (stat(url, &buf))
924                 return 0;
925         return S_ISREG(buf.st_mode);
928 struct transport *transport_get(struct remote *remote, const char *url)
930         struct transport *ret = xcalloc(1, sizeof(*ret));
932         ret->remote = remote;
933         ret->url = url;
935         if (!prefixcmp(url, "rsync://")) {
936                 ret->get_refs_list = get_refs_via_rsync;
937                 ret->fetch = fetch_objs_via_rsync;
938                 ret->push = rsync_transport_push;
940         } else if (!prefixcmp(url, "http://")
941                 || !prefixcmp(url, "https://")
942                 || !prefixcmp(url, "ftp://")) {
943 #ifdef NO_CURL
944                 error("git was compiled without libcurl support.");
945 #else
946                 ret->get_refs_list = get_refs_via_curl;
947                 ret->fetch = fetch_objs_via_curl;
948                 ret->push = curl_transport_push;
949 #endif
950                 ret->disconnect = disconnect_walker;
952         } else if (is_local(url) && is_file(url)) {
953                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
954                 ret->data = data;
955                 ret->get_refs_list = get_refs_from_bundle;
956                 ret->fetch = fetch_refs_from_bundle;
957                 ret->disconnect = close_bundle;
959         } else {
960                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
961                 ret->data = data;
962                 ret->set_option = set_git_option;
963                 ret->get_refs_list = get_refs_via_connect;
964                 ret->fetch = fetch_refs_via_pack;
965                 ret->push_refs = git_transport_push;
966                 ret->disconnect = disconnect_git;
968                 data->thin = 1;
969                 data->conn = NULL;
970                 data->uploadpack = "git-upload-pack";
971                 if (remote && remote->uploadpack)
972                         data->uploadpack = remote->uploadpack;
973                 data->receivepack = "git-receive-pack";
974                 if (remote && remote->receivepack)
975                         data->receivepack = remote->receivepack;
976         }
978         return ret;
981 int transport_set_option(struct transport *transport,
982                          const char *name, const char *value)
984         if (transport->set_option)
985                 return transport->set_option(transport, name, value);
986         return 1;
989 int transport_push(struct transport *transport,
990                    int refspec_nr, const char **refspec, int flags)
992         verify_remote_names(refspec_nr, refspec);
994         if (transport->push)
995                 return transport->push(transport, refspec_nr, refspec, flags);
996         if (transport->push_refs) {
997                 struct ref *remote_refs =
998                         transport->get_refs_list(transport, 1);
999                 struct ref **remote_tail;
1000                 struct ref *local_refs = get_local_heads();
1001                 int match_flags = MATCH_REFS_NONE;
1002                 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
1003                 int ret;
1005                 if (flags & TRANSPORT_PUSH_ALL)
1006                         match_flags |= MATCH_REFS_ALL;
1007                 if (flags & TRANSPORT_PUSH_MIRROR)
1008                         match_flags |= MATCH_REFS_MIRROR;
1010                 remote_tail = &remote_refs;
1011                 while (*remote_tail)
1012                         remote_tail = &((*remote_tail)->next);
1013                 if (match_refs(local_refs, remote_refs, &remote_tail,
1014                                refspec_nr, refspec, match_flags)) {
1015                         return -1;
1016                 }
1018                 ret = transport->push_refs(transport, remote_refs, flags);
1020                 print_push_status(transport->url, remote_refs, verbose);
1022                 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1023                         struct ref *ref;
1024                         for (ref = remote_refs; ref; ref = ref->next)
1025                                 update_tracking_ref(transport->remote, ref, verbose);
1026                 }
1028                 if (!ret && !refs_pushed(remote_refs))
1029                         fprintf(stderr, "Everything up-to-date\n");
1030                 return ret;
1031         }
1032         return 1;
1035 const struct ref *transport_get_remote_refs(struct transport *transport)
1037         if (!transport->remote_refs)
1038                 transport->remote_refs = transport->get_refs_list(transport, 0);
1039         return transport->remote_refs;
1042 int transport_fetch_refs(struct transport *transport, const struct ref *refs)
1044         int rc;
1045         int nr_heads = 0, nr_alloc = 0;
1046         const struct ref **heads = NULL;
1047         const struct ref *rm;
1049         for (rm = refs; rm; rm = rm->next) {
1050                 if (rm->peer_ref &&
1051                     !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1052                         continue;
1053                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1054                 heads[nr_heads++] = rm;
1055         }
1057         rc = transport->fetch(transport, nr_heads, heads);
1058         free(heads);
1059         return rc;
1062 void transport_unlock_pack(struct transport *transport)
1064         if (transport->pack_lockfile) {
1065                 unlink(transport->pack_lockfile);
1066                 free(transport->pack_lockfile);
1067                 transport->pack_lockfile = NULL;
1068         }
1071 int transport_disconnect(struct transport *transport)
1073         int ret = 0;
1074         if (transport->disconnect)
1075                 ret = transport->disconnect(transport);
1076         free(transport);
1077         return ret;