git add -p: demonstrate failure when staging both mode and hunk
[git/jnareb-git.git] / transport.c
blob3dfb03c06ed82102f60045d6889db97cf036651e
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 */
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).
23 * Appears refactoring this from refs.c is too cumbersome.
26 static int str_cmp(const void *a, const void *b)
28 const char *s1 = a;
29 const char *s2 = b;
31 return strcmp(s1, s2);
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)
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);
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;
83 close(fd);
84 (*tail)->next = next;
85 *tail = next;
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;
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;
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;
134 next->next = (*list)->next;
135 (*list)->next = next;
136 list = &(*list)->next;
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 ("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;
278 free(ref);
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 ("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;
399 int err;
401 if (flags & TRANSPORT_PUSH_MIRROR)
402 return error("http transport does not support mirror mode");
404 argv = xmalloc((refspec_nr + 12) * sizeof(char *));
405 argv[0] = "http-push";
406 argc = 1;
407 if (flags & TRANSPORT_PUSH_ALL)
408 argv[argc++] = "--all";
409 if (flags & TRANSPORT_PUSH_FORCE)
410 argv[argc++] = "--force";
411 if (flags & TRANSPORT_PUSH_DRY_RUN)
412 argv[argc++] = "--dry-run";
413 if (flags & TRANSPORT_PUSH_VERBOSE)
414 argv[argc++] = "--verbose";
415 argv[argc++] = transport->url;
416 while (refspec_nr--)
417 argv[argc++] = *refspec++;
418 argv[argc] = NULL;
419 err = run_command_v_opt(argv, RUN_GIT_CMD);
420 switch (err) {
421 case -ERR_RUN_COMMAND_FORK:
422 error("unable to fork for %s", argv[0]);
423 case -ERR_RUN_COMMAND_EXEC:
424 error("unable to exec %s", argv[0]);
425 break;
426 case -ERR_RUN_COMMAND_WAITPID:
427 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
428 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
429 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
430 error("%s died with strange error", argv[0]);
432 return !!err;
435 static struct ref *get_refs_via_curl(struct transport *transport, int for_push)
437 struct strbuf buffer = STRBUF_INIT;
438 char *data, *start, *mid;
439 char *ref_name;
440 char *refs_url;
441 int i = 0;
443 struct active_request_slot *slot;
444 struct slot_results results;
446 struct ref *refs = NULL;
447 struct ref *ref = NULL;
448 struct ref *last_ref = NULL;
450 struct walker *walker;
452 if (for_push)
453 return NULL;
455 if (!transport->data)
456 transport->data = get_http_walker(transport->url,
457 transport->remote);
459 walker = transport->data;
461 refs_url = xmalloc(strlen(transport->url) + 11);
462 sprintf(refs_url, "%s/info/refs", transport->url);
464 slot = get_active_slot();
465 slot->results = &results;
466 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
467 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
468 curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
469 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
471 if (start_active_slot(slot)) {
472 run_active_slot(slot);
473 if (results.curl_result != CURLE_OK) {
474 strbuf_release(&buffer);
475 if (missing_target(&results))
476 die("%s not found: did you run git update-server-info on the server?", refs_url);
477 else
478 die("%s download error - %s", refs_url, curl_errorstr);
480 } else {
481 strbuf_release(&buffer);
482 die("Unable to start HTTP request");
485 data = buffer.buf;
486 start = NULL;
487 mid = data;
488 while (i < buffer.len) {
489 if (!start)
490 start = &data[i];
491 if (data[i] == '\t')
492 mid = &data[i];
493 if (data[i] == '\n') {
494 data[i] = 0;
495 ref_name = mid + 1;
496 ref = xmalloc(sizeof(struct ref) +
497 strlen(ref_name) + 1);
498 memset(ref, 0, sizeof(struct ref));
499 strcpy(ref->name, ref_name);
500 get_sha1_hex(start, ref->old_sha1);
501 if (!refs)
502 refs = ref;
503 if (last_ref)
504 last_ref->next = ref;
505 last_ref = ref;
506 start = NULL;
508 i++;
511 strbuf_release(&buffer);
513 ref = alloc_ref("HEAD");
514 if (!walker->fetch_ref(walker, ref) &&
515 !resolve_remote_symref(ref, refs)) {
516 ref->next = refs;
517 refs = ref;
518 } else {
519 free(ref);
522 return refs;
525 static int fetch_objs_via_curl(struct transport *transport,
526 int nr_objs, const struct ref **to_fetch)
528 if (!transport->data)
529 transport->data = get_http_walker(transport->url,
530 transport->remote);
531 return fetch_objs_via_walker(transport, nr_objs, to_fetch);
534 #endif
536 struct bundle_transport_data {
537 int fd;
538 struct bundle_header header;
541 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
543 struct bundle_transport_data *data = transport->data;
544 struct ref *result = NULL;
545 int i;
547 if (for_push)
548 return NULL;
550 if (data->fd > 0)
551 close(data->fd);
552 data->fd = read_bundle_header(transport->url, &data->header);
553 if (data->fd < 0)
554 die ("Could not read bundle '%s'.", transport->url);
555 for (i = 0; i < data->header.references.nr; i++) {
556 struct ref_list_entry *e = data->header.references.list + i;
557 struct ref *ref = alloc_ref(e->name);
558 hashcpy(ref->old_sha1, e->sha1);
559 ref->next = result;
560 result = ref;
562 return result;
565 static int fetch_refs_from_bundle(struct transport *transport,
566 int nr_heads, const struct ref **to_fetch)
568 struct bundle_transport_data *data = transport->data;
569 return unbundle(&data->header, data->fd);
572 static int close_bundle(struct transport *transport)
574 struct bundle_transport_data *data = transport->data;
575 if (data->fd > 0)
576 close(data->fd);
577 free(data);
578 return 0;
581 struct git_transport_data {
582 unsigned thin : 1;
583 unsigned keep : 1;
584 unsigned followtags : 1;
585 int depth;
586 struct child_process *conn;
587 int fd[2];
588 const char *uploadpack;
589 const char *receivepack;
590 struct extra_have_objects extra_have;
593 static int set_git_option(struct transport *connection,
594 const char *name, const char *value)
596 struct git_transport_data *data = connection->data;
597 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
598 data->uploadpack = value;
599 return 0;
600 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
601 data->receivepack = value;
602 return 0;
603 } else if (!strcmp(name, TRANS_OPT_THIN)) {
604 data->thin = !!value;
605 return 0;
606 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
607 data->followtags = !!value;
608 return 0;
609 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
610 data->keep = !!value;
611 return 0;
612 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
613 if (!value)
614 data->depth = 0;
615 else
616 data->depth = atoi(value);
617 return 0;
619 return 1;
622 static int connect_setup(struct transport *transport, int for_push, int verbose)
624 struct git_transport_data *data = transport->data;
625 data->conn = git_connect(data->fd, transport->url,
626 for_push ? data->receivepack : data->uploadpack,
627 verbose ? CONNECT_VERBOSE : 0);
628 return 0;
631 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
633 struct git_transport_data *data = transport->data;
634 struct ref *refs;
636 connect_setup(transport, for_push, 0);
637 get_remote_heads(data->fd[0], &refs, 0, NULL,
638 for_push ? REF_NORMAL : 0, &data->extra_have);
640 return refs;
643 static int fetch_refs_via_pack(struct transport *transport,
644 int nr_heads, const struct ref **to_fetch)
646 struct git_transport_data *data = transport->data;
647 char **heads = xmalloc(nr_heads * sizeof(*heads));
648 char **origh = xmalloc(nr_heads * sizeof(*origh));
649 const struct ref *refs;
650 char *dest = xstrdup(transport->url);
651 struct fetch_pack_args args;
652 int i;
653 struct ref *refs_tmp = NULL;
655 memset(&args, 0, sizeof(args));
656 args.uploadpack = data->uploadpack;
657 args.keep_pack = data->keep;
658 args.lock_pack = 1;
659 args.use_thin_pack = data->thin;
660 args.include_tag = data->followtags;
661 args.verbose = (transport->verbose > 0);
662 args.quiet = (transport->verbose < 0);
663 args.no_progress = args.quiet || (!transport->progress && !isatty(1));
664 args.depth = data->depth;
666 for (i = 0; i < nr_heads; i++)
667 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
669 if (!data->conn) {
670 connect_setup(transport, 0, 0);
671 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
674 refs = fetch_pack(&args, data->fd, data->conn,
675 refs_tmp ? refs_tmp : transport->remote_refs,
676 dest, nr_heads, heads, &transport->pack_lockfile);
677 close(data->fd[0]);
678 close(data->fd[1]);
679 if (finish_connect(data->conn))
680 refs = NULL;
681 data->conn = NULL;
683 free_refs(refs_tmp);
685 for (i = 0; i < nr_heads; i++)
686 free(origh[i]);
687 free(origh);
688 free(heads);
689 free(dest);
690 return (refs ? 0 : -1);
693 static int refs_pushed(struct ref *ref)
695 for (; ref; ref = ref->next) {
696 switch(ref->status) {
697 case REF_STATUS_NONE:
698 case REF_STATUS_UPTODATE:
699 break;
700 default:
701 return 1;
704 return 0;
707 static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
709 struct refspec rs;
711 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
712 return;
714 rs.src = ref->name;
715 rs.dst = NULL;
717 if (!remote_find_tracking(remote, &rs)) {
718 if (verbose)
719 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
720 if (ref->deletion) {
721 delete_ref(rs.dst, NULL, 0);
722 } else
723 update_ref("update by push", rs.dst,
724 ref->new_sha1, NULL, 0, 0);
725 free(rs.dst);
729 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
731 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
733 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
734 if (from)
735 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
736 else
737 fputs(prettify_ref(to), stderr);
738 if (msg) {
739 fputs(" (", stderr);
740 fputs(msg, stderr);
741 fputc(')', stderr);
743 fputc('\n', stderr);
746 static const char *status_abbrev(unsigned char sha1[20])
748 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
751 static void print_ok_ref_status(struct ref *ref)
753 if (ref->deletion)
754 print_ref_status('-', "[deleted]", ref, NULL, NULL);
755 else if (is_null_sha1(ref->old_sha1))
756 print_ref_status('*',
757 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
758 "[new branch]"),
759 ref, ref->peer_ref, NULL);
760 else {
761 char quickref[84];
762 char type;
763 const char *msg;
765 strcpy(quickref, status_abbrev(ref->old_sha1));
766 if (ref->nonfastforward) {
767 strcat(quickref, "...");
768 type = '+';
769 msg = "forced update";
770 } else {
771 strcat(quickref, "..");
772 type = ' ';
773 msg = NULL;
775 strcat(quickref, status_abbrev(ref->new_sha1));
777 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
781 static int print_one_push_status(struct ref *ref, const char *dest, int count)
783 if (!count)
784 fprintf(stderr, "To %s\n", dest);
786 switch(ref->status) {
787 case REF_STATUS_NONE:
788 print_ref_status('X', "[no match]", ref, NULL, NULL);
789 break;
790 case REF_STATUS_REJECT_NODELETE:
791 print_ref_status('!', "[rejected]", ref, NULL,
792 "remote does not support deleting refs");
793 break;
794 case REF_STATUS_UPTODATE:
795 print_ref_status('=', "[up to date]", ref,
796 ref->peer_ref, NULL);
797 break;
798 case REF_STATUS_REJECT_NONFASTFORWARD:
799 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
800 "non-fast forward");
801 break;
802 case REF_STATUS_REMOTE_REJECT:
803 print_ref_status('!', "[remote rejected]", ref,
804 ref->deletion ? NULL : ref->peer_ref,
805 ref->remote_status);
806 break;
807 case REF_STATUS_EXPECTING_REPORT:
808 print_ref_status('!', "[remote failure]", ref,
809 ref->deletion ? NULL : ref->peer_ref,
810 "remote failed to report status");
811 break;
812 case REF_STATUS_OK:
813 print_ok_ref_status(ref);
814 break;
817 return 1;
820 static void print_push_status(const char *dest, struct ref *refs, int verbose)
822 struct ref *ref;
823 int n = 0;
825 if (verbose) {
826 for (ref = refs; ref; ref = ref->next)
827 if (ref->status == REF_STATUS_UPTODATE)
828 n += print_one_push_status(ref, dest, n);
831 for (ref = refs; ref; ref = ref->next)
832 if (ref->status == REF_STATUS_OK)
833 n += print_one_push_status(ref, dest, n);
835 for (ref = refs; ref; ref = ref->next) {
836 if (ref->status != REF_STATUS_NONE &&
837 ref->status != REF_STATUS_UPTODATE &&
838 ref->status != REF_STATUS_OK)
839 n += print_one_push_status(ref, dest, n);
843 static void verify_remote_names(int nr_heads, const char **heads)
845 int i;
847 for (i = 0; i < nr_heads; i++) {
848 const char *local = heads[i];
849 const char *remote = strrchr(heads[i], ':');
851 if (*local == '+')
852 local++;
854 /* A matching refspec is okay. */
855 if (remote == local && remote[1] == '\0')
856 continue;
858 remote = remote ? (remote + 1) : local;
859 switch (check_ref_format(remote)) {
860 case 0: /* ok */
861 case CHECK_REF_FORMAT_ONELEVEL:
862 /* ok but a single level -- that is fine for
863 * a match pattern.
865 case CHECK_REF_FORMAT_WILDCARD:
866 /* ok but ends with a pattern-match character */
867 continue;
869 die("remote part of refspec is not a valid name in %s",
870 heads[i]);
874 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
876 struct git_transport_data *data = transport->data;
877 struct send_pack_args args;
878 int ret;
880 if (!data->conn) {
881 struct ref *tmp_refs;
882 connect_setup(transport, 1, 0);
884 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
885 NULL);
888 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
889 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
890 args.use_thin_pack = data->thin;
891 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
892 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
894 ret = send_pack(&args, data->fd, data->conn, remote_refs,
895 &data->extra_have);
897 close(data->fd[1]);
898 close(data->fd[0]);
899 ret |= finish_connect(data->conn);
900 data->conn = NULL;
902 return ret;
905 static int disconnect_git(struct transport *transport)
907 struct git_transport_data *data = transport->data;
908 if (data->conn) {
909 packet_flush(data->fd[1]);
910 close(data->fd[0]);
911 close(data->fd[1]);
912 finish_connect(data->conn);
915 free(data);
916 return 0;
919 static int is_local(const char *url)
921 const char *colon = strchr(url, ':');
922 const char *slash = strchr(url, '/');
923 return !colon || (slash && slash < colon) ||
924 has_dos_drive_prefix(url);
927 static int is_file(const char *url)
929 struct stat buf;
930 if (stat(url, &buf))
931 return 0;
932 return S_ISREG(buf.st_mode);
935 struct transport *transport_get(struct remote *remote, const char *url)
937 struct transport *ret = xcalloc(1, sizeof(*ret));
939 ret->remote = remote;
940 ret->url = url;
942 if (!prefixcmp(url, "rsync:")) {
943 ret->get_refs_list = get_refs_via_rsync;
944 ret->fetch = fetch_objs_via_rsync;
945 ret->push = rsync_transport_push;
947 } else if (!prefixcmp(url, "http://")
948 || !prefixcmp(url, "https://")
949 || !prefixcmp(url, "ftp://")) {
950 #ifdef NO_CURL
951 error("git was compiled without libcurl support.");
952 #else
953 ret->get_refs_list = get_refs_via_curl;
954 ret->fetch = fetch_objs_via_curl;
955 ret->push = curl_transport_push;
956 #endif
957 ret->disconnect = disconnect_walker;
959 } else if (is_local(url) && is_file(url)) {
960 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
961 ret->data = data;
962 ret->get_refs_list = get_refs_from_bundle;
963 ret->fetch = fetch_refs_from_bundle;
964 ret->disconnect = close_bundle;
966 } else {
967 struct git_transport_data *data = xcalloc(1, sizeof(*data));
968 ret->data = data;
969 ret->set_option = set_git_option;
970 ret->get_refs_list = get_refs_via_connect;
971 ret->fetch = fetch_refs_via_pack;
972 ret->push_refs = git_transport_push;
973 ret->disconnect = disconnect_git;
975 data->thin = 1;
976 data->conn = NULL;
977 data->uploadpack = "git-upload-pack";
978 if (remote && remote->uploadpack)
979 data->uploadpack = remote->uploadpack;
980 data->receivepack = "git-receive-pack";
981 if (remote && remote->receivepack)
982 data->receivepack = remote->receivepack;
985 return ret;
988 int transport_set_option(struct transport *transport,
989 const char *name, const char *value)
991 if (transport->set_option)
992 return transport->set_option(transport, name, value);
993 return 1;
996 int transport_push(struct transport *transport,
997 int refspec_nr, const char **refspec, int flags)
999 verify_remote_names(refspec_nr, refspec);
1001 if (transport->push)
1002 return transport->push(transport, refspec_nr, refspec, flags);
1003 if (transport->push_refs) {
1004 struct ref *remote_refs =
1005 transport->get_refs_list(transport, 1);
1006 struct ref **remote_tail;
1007 struct ref *local_refs = get_local_heads();
1008 int match_flags = MATCH_REFS_NONE;
1009 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
1010 int ret;
1012 if (flags & TRANSPORT_PUSH_ALL)
1013 match_flags |= MATCH_REFS_ALL;
1014 if (flags & TRANSPORT_PUSH_MIRROR)
1015 match_flags |= MATCH_REFS_MIRROR;
1017 remote_tail = &remote_refs;
1018 while (*remote_tail)
1019 remote_tail = &((*remote_tail)->next);
1020 if (match_refs(local_refs, remote_refs, &remote_tail,
1021 refspec_nr, refspec, match_flags)) {
1022 return -1;
1025 ret = transport->push_refs(transport, remote_refs, flags);
1027 print_push_status(transport->url, remote_refs, verbose);
1029 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1030 struct ref *ref;
1031 for (ref = remote_refs; ref; ref = ref->next)
1032 update_tracking_ref(transport->remote, ref, verbose);
1035 if (!ret && !refs_pushed(remote_refs))
1036 fprintf(stderr, "Everything up-to-date\n");
1037 return ret;
1039 return 1;
1042 const struct ref *transport_get_remote_refs(struct transport *transport)
1044 if (!transport->remote_refs)
1045 transport->remote_refs = transport->get_refs_list(transport, 0);
1046 return transport->remote_refs;
1049 int transport_fetch_refs(struct transport *transport, const struct ref *refs)
1051 int rc;
1052 int nr_heads = 0, nr_alloc = 0;
1053 const struct ref **heads = NULL;
1054 const struct ref *rm;
1056 for (rm = refs; rm; rm = rm->next) {
1057 if (rm->peer_ref &&
1058 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1059 continue;
1060 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1061 heads[nr_heads++] = rm;
1064 rc = transport->fetch(transport, nr_heads, heads);
1065 free(heads);
1066 return rc;
1069 void transport_unlock_pack(struct transport *transport)
1071 if (transport->pack_lockfile) {
1072 unlink(transport->pack_lockfile);
1073 free(transport->pack_lockfile);
1074 transport->pack_lockfile = NULL;
1078 int transport_disconnect(struct transport *transport)
1080 int ret = 0;
1081 if (transport->disconnect)
1082 ret = transport->disconnect(transport);
1083 free(transport);
1084 return ret;