git-push: fix an advice message so it goes to stderr
[git/git.git] / transport.c
CommitLineData
9b288516
DB
1#include "cache.h"
2#include "transport.h"
3#include "run-command.h"
c29727d5
DB
4#include "pkt-line.h"
5#include "fetch-pack.h"
40cb4fab 6#include "send-pack.h"
c29727d5 7#include "walker.h"
c7a8a162 8#include "bundle.h"
cd547b48
JS
9#include "dir.h"
10#include "refs.h"
e9fcd1e2 11#include "branch.h"
cd547b48
JS
12
13/* rsync support */
14
15/*
16 * We copy packed-refs and refs/ into a temporary file, then read the
17 * loose refs recursively (sorting whenever possible), and then inserting
18 * those packed refs that are not yet in the list (not validating, but
19 * assuming that the file is sorted).
20 *
21 * Appears refactoring this from refs.c is too cumbersome.
22 */
23
24static int str_cmp(const void *a, const void *b)
25{
26 const char *s1 = a;
27 const char *s2 = b;
28
29 return strcmp(s1, s2);
30}
31
32/* path->buf + name_offset is expected to point to "refs/" */
33
34static int read_loose_refs(struct strbuf *path, int name_offset,
35 struct ref **tail)
36{
37 DIR *dir = opendir(path->buf);
38 struct dirent *de;
39 struct {
40 char **entries;
41 int nr, alloc;
42 } list;
43 int i, pathlen;
44
45 if (!dir)
46 return -1;
47
48 memset (&list, 0, sizeof(list));
49
50 while ((de = readdir(dir))) {
8ca12c0d 51 if (is_dot_or_dotdot(de->d_name))
cd547b48
JS
52 continue;
53 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
54 list.entries[list.nr++] = xstrdup(de->d_name);
55 }
56 closedir(dir);
57
58 /* sort the list */
59
60 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
61
62 pathlen = path->len;
63 strbuf_addch(path, '/');
64
65 for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
66 strbuf_addstr(path, list.entries[i]);
67 if (read_loose_refs(path, name_offset, tail)) {
68 int fd = open(path->buf, O_RDONLY);
69 char buffer[40];
70 struct ref *next;
71
72 if (fd < 0)
73 continue;
59c69c0c 74 next = alloc_ref(path->buf + name_offset);
cd547b48
JS
75 if (read_in_full(fd, buffer, 40) != 40 ||
76 get_sha1_hex(buffer, next->old_sha1)) {
77 close(fd);
78 free(next);
79 continue;
80 }
81 close(fd);
cd547b48
JS
82 (*tail)->next = next;
83 *tail = next;
84 }
85 }
86 strbuf_setlen(path, pathlen);
87
88 for (i = 0; i < list.nr; i++)
89 free(list.entries[i]);
90 free(list.entries);
91
92 return 0;
93}
94
95/* insert the packed refs for which no loose refs were found */
96
97static void insert_packed_refs(const char *packed_refs, struct ref **list)
98{
99 FILE *f = fopen(packed_refs, "r");
100 static char buffer[PATH_MAX];
101
102 if (!f)
103 return;
104
105 for (;;) {
47ec7943 106 int cmp = cmp, len;
cd547b48
JS
107
108 if (!fgets(buffer, sizeof(buffer), f)) {
109 fclose(f);
110 return;
111 }
112
113 if (hexval(buffer[0]) > 0xf)
114 continue;
115 len = strlen(buffer);
872c930d 116 if (len && buffer[len - 1] == '\n')
cd547b48
JS
117 buffer[--len] = '\0';
118 if (len < 41)
119 continue;
120 while ((*list)->next &&
121 (cmp = strcmp(buffer + 41,
122 (*list)->next->name)) > 0)
123 list = &(*list)->next;
124 if (!(*list)->next || cmp < 0) {
59c69c0c 125 struct ref *next = alloc_ref(buffer + 41);
cd547b48
JS
126 buffer[40] = '\0';
127 if (get_sha1_hex(buffer, next->old_sha1)) {
128 warning ("invalid SHA-1: %s", buffer);
129 free(next);
130 continue;
131 }
cd547b48
JS
132 next->next = (*list)->next;
133 (*list)->next = next;
134 list = &(*list)->next;
135 }
136 }
137}
138
e9fcd1e2
IL
139static void set_upstreams(struct transport *transport, struct ref *refs,
140 int pretend)
141{
142 struct ref *ref;
143 for (ref = refs; ref; ref = ref->next) {
144 const char *localname;
145 const char *tmp;
146 const char *remotename;
147 unsigned char sha[20];
148 int flag = 0;
149 /*
150 * Check suitability for tracking. Must be successful /
151 * already up-to-date ref create/modify (not delete).
152 */
153 if (ref->status != REF_STATUS_OK &&
154 ref->status != REF_STATUS_UPTODATE)
155 continue;
156 if (!ref->peer_ref)
157 continue;
158 if (!ref->new_sha1 || is_null_sha1(ref->new_sha1))
159 continue;
160
161 /* Follow symbolic refs (mainly for HEAD). */
162 localname = ref->peer_ref->name;
163 remotename = ref->name;
164 tmp = resolve_ref(localname, sha, 1, &flag);
165 if (tmp && flag & REF_ISSYMREF &&
166 !prefixcmp(tmp, "refs/heads/"))
167 localname = tmp;
168
169 /* Both source and destination must be local branches. */
170 if (!localname || prefixcmp(localname, "refs/heads/"))
171 continue;
172 if (!remotename || prefixcmp(remotename, "refs/heads/"))
173 continue;
174
175 if (!pretend)
176 install_branch_config(BRANCH_CONFIG_VERBOSE,
177 localname + 11, transport->remote->name,
178 remotename);
179 else
180 printf("Would set upstream of '%s' to '%s' of '%s'\n",
181 localname + 11, remotename + 11,
182 transport->remote->name);
183 }
184}
185
7efaeba2
JS
186static const char *rsync_url(const char *url)
187{
188 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
189}
190
64fcef2d 191static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
cd547b48
JS
192{
193 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
b9cb0772 194 struct ref dummy = {0}, *tail = &dummy;
cd547b48
JS
195 struct child_process rsync;
196 const char *args[5];
197 int temp_dir_len;
198
64fcef2d
DB
199 if (for_push)
200 return NULL;
201
cd547b48
JS
202 /* copy the refs to the temporary directory */
203
204 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
205 if (!mkdtemp(temp_dir.buf))
0721c314 206 die_errno ("Could not make temporary directory");
cd547b48
JS
207 temp_dir_len = temp_dir.len;
208
7efaeba2 209 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
210 strbuf_addstr(&buf, "/refs");
211
212 memset(&rsync, 0, sizeof(rsync));
213 rsync.argv = args;
214 rsync.stdout_to_stderr = 1;
215 args[0] = "rsync";
2b5a06ed 216 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
cd547b48
JS
217 args[2] = buf.buf;
218 args[3] = temp_dir.buf;
219 args[4] = NULL;
220
221 if (run_command(&rsync))
222 die ("Could not run rsync to get refs");
223
224 strbuf_reset(&buf);
7efaeba2 225 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
226 strbuf_addstr(&buf, "/packed-refs");
227
228 args[2] = buf.buf;
229
230 if (run_command(&rsync))
231 die ("Could not run rsync to get refs");
232
233 /* read the copied refs */
234
235 strbuf_addstr(&temp_dir, "/refs");
236 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
237 strbuf_setlen(&temp_dir, temp_dir_len);
238
239 tail = &dummy;
240 strbuf_addstr(&temp_dir, "/packed-refs");
241 insert_packed_refs(temp_dir.buf, &tail);
242 strbuf_setlen(&temp_dir, temp_dir_len);
243
244 if (remove_dir_recursively(&temp_dir, 0))
245 warning ("Error removing temporary directory %s.",
246 temp_dir.buf);
247
248 strbuf_release(&buf);
249 strbuf_release(&temp_dir);
250
251 return dummy.next;
252}
253
254static int fetch_objs_via_rsync(struct transport *transport,
37148311 255 int nr_objs, struct ref **to_fetch)
cd547b48
JS
256{
257 struct strbuf buf = STRBUF_INIT;
258 struct child_process rsync;
259 const char *args[8];
260 int result;
261
7efaeba2 262 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
263 strbuf_addstr(&buf, "/objects/");
264
265 memset(&rsync, 0, sizeof(rsync));
266 rsync.argv = args;
267 rsync.stdout_to_stderr = 1;
268 args[0] = "rsync";
2b5a06ed 269 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
cd547b48
JS
270 args[2] = "--ignore-existing";
271 args[3] = "--exclude";
272 args[4] = "info";
273 args[5] = buf.buf;
274 args[6] = get_object_directory();
275 args[7] = NULL;
276
277 /* NEEDSWORK: handle one level of alternates */
278 result = run_command(&rsync);
279
280 strbuf_release(&buf);
281
282 return result;
283}
284
285static int write_one_ref(const char *name, const unsigned char *sha1,
286 int flags, void *data)
287{
288 struct strbuf *buf = data;
289 int len = buf->len;
290 FILE *f;
291
292 /* when called via for_each_ref(), flags is non-zero */
293 if (flags && prefixcmp(name, "refs/heads/") &&
294 prefixcmp(name, "refs/tags/"))
295 return 0;
296
297 strbuf_addstr(buf, name);
298 if (safe_create_leading_directories(buf->buf) ||
299 !(f = fopen(buf->buf, "w")) ||
300 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
301 fclose(f))
302 return error("problems writing temporary file %s", buf->buf);
303 strbuf_setlen(buf, len);
304 return 0;
305}
306
307static int write_refs_to_temp_dir(struct strbuf *temp_dir,
308 int refspec_nr, const char **refspec)
309{
310 int i;
311
312 for (i = 0; i < refspec_nr; i++) {
313 unsigned char sha1[20];
314 char *ref;
315
316 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
317 return error("Could not get ref %s", refspec[i]);
318
319 if (write_one_ref(ref, sha1, 0, temp_dir)) {
320 free(ref);
321 return -1;
322 }
323 free(ref);
324 }
325 return 0;
326}
327
328static int rsync_transport_push(struct transport *transport,
329 int refspec_nr, const char **refspec, int flags)
330{
331 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
332 int result = 0, i;
333 struct child_process rsync;
ee020f35 334 const char *args[10];
cd547b48 335
94c89ba6
AW
336 if (flags & TRANSPORT_PUSH_MIRROR)
337 return error("rsync transport does not support mirror mode");
338
cd547b48
JS
339 /* first push the objects */
340
7efaeba2 341 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
342 strbuf_addch(&buf, '/');
343
344 memset(&rsync, 0, sizeof(rsync));
345 rsync.argv = args;
346 rsync.stdout_to_stderr = 1;
ee020f35
SP
347 i = 0;
348 args[i++] = "rsync";
349 args[i++] = "-a";
350 if (flags & TRANSPORT_PUSH_DRY_RUN)
351 args[i++] = "--dry-run";
352 if (transport->verbose > 0)
353 args[i++] = "-v";
354 args[i++] = "--ignore-existing";
355 args[i++] = "--exclude";
356 args[i++] = "info";
357 args[i++] = get_object_directory();
358 args[i++] = buf.buf;
359 args[i++] = NULL;
cd547b48
JS
360
361 if (run_command(&rsync))
7efaeba2
JS
362 return error("Could not push objects to %s",
363 rsync_url(transport->url));
cd547b48
JS
364
365 /* copy the refs to the temporary directory; they could be packed. */
366
367 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
368 if (!mkdtemp(temp_dir.buf))
0721c314 369 die_errno ("Could not make temporary directory");
cd547b48
JS
370 strbuf_addch(&temp_dir, '/');
371
372 if (flags & TRANSPORT_PUSH_ALL) {
373 if (for_each_ref(write_one_ref, &temp_dir))
374 return -1;
375 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
376 return -1;
377
ee020f35
SP
378 i = 2;
379 if (flags & TRANSPORT_PUSH_DRY_RUN)
380 args[i++] = "--dry-run";
381 if (!(flags & TRANSPORT_PUSH_FORCE))
382 args[i++] = "--ignore-existing";
cd547b48 383 args[i++] = temp_dir.buf;
7efaeba2 384 args[i++] = rsync_url(transport->url);
cd547b48
JS
385 args[i++] = NULL;
386 if (run_command(&rsync))
7efaeba2
JS
387 result = error("Could not push to %s",
388 rsync_url(transport->url));
cd547b48
JS
389
390 if (remove_dir_recursively(&temp_dir, 0))
391 warning ("Could not remove temporary directory %s.",
392 temp_dir.buf);
393
394 strbuf_release(&buf);
395 strbuf_release(&temp_dir);
396
397 return result;
398}
c29727d5 399
c7a8a162
JS
400struct bundle_transport_data {
401 int fd;
402 struct bundle_header header;
403};
404
64fcef2d 405static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
c7a8a162
JS
406{
407 struct bundle_transport_data *data = transport->data;
408 struct ref *result = NULL;
409 int i;
410
64fcef2d
DB
411 if (for_push)
412 return NULL;
413
c7a8a162
JS
414 if (data->fd > 0)
415 close(data->fd);
416 data->fd = read_bundle_header(transport->url, &data->header);
417 if (data->fd < 0)
418 die ("Could not read bundle '%s'.", transport->url);
419 for (i = 0; i < data->header.references.nr; i++) {
420 struct ref_list_entry *e = data->header.references.list + i;
59c69c0c 421 struct ref *ref = alloc_ref(e->name);
c7a8a162 422 hashcpy(ref->old_sha1, e->sha1);
c7a8a162
JS
423 ref->next = result;
424 result = ref;
425 }
426 return result;
427}
428
1788c39c 429static int fetch_refs_from_bundle(struct transport *transport,
37148311 430 int nr_heads, struct ref **to_fetch)
c7a8a162
JS
431{
432 struct bundle_transport_data *data = transport->data;
433 return unbundle(&data->header, data->fd);
434}
435
436static int close_bundle(struct transport *transport)
437{
438 struct bundle_transport_data *data = transport->data;
439 if (data->fd > 0)
440 close(data->fd);
f4e95765 441 free(data);
c7a8a162
JS
442 return 0;
443}
444
9b288516 445struct git_transport_data {
aa5af974 446 struct git_transport_options options;
ba227857
DB
447 struct child_process *conn;
448 int fd[2];
61b075bd 449 unsigned got_remote_heads : 1;
64fcef2d 450 struct extra_have_objects extra_have;
9b288516
DB
451};
452
aa5af974 453static int set_git_option(struct git_transport_options *opts,
9b288516
DB
454 const char *name, const char *value)
455{
c29727d5 456 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
aa5af974 457 opts->uploadpack = value;
c29727d5
DB
458 return 0;
459 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
aa5af974 460 opts->receivepack = value;
9b288516
DB
461 return 0;
462 } else if (!strcmp(name, TRANS_OPT_THIN)) {
aa5af974 463 opts->thin = !!value;
9b288516 464 return 0;
41fa7d2e 465 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
aa5af974 466 opts->followtags = !!value;
41fa7d2e 467 return 0;
c29727d5 468 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
aa5af974 469 opts->keep = !!value;
c29727d5 470 return 0;
c29727d5
DB
471 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
472 if (!value)
aa5af974 473 opts->depth = 0;
c29727d5 474 else
aa5af974 475 opts->depth = atoi(value);
c29727d5 476 return 0;
9b288516
DB
477 }
478 return 1;
479}
480
64fcef2d 481static int connect_setup(struct transport *transport, int for_push, int verbose)
ba227857
DB
482{
483 struct git_transport_data *data = transport->data;
61b075bd
IL
484
485 if (data->conn)
486 return 0;
487
64fcef2d 488 data->conn = git_connect(data->fd, transport->url,
aa5af974
IL
489 for_push ? data->options.receivepack :
490 data->options.uploadpack,
64fcef2d 491 verbose ? CONNECT_VERBOSE : 0);
61b075bd 492
ba227857
DB
493 return 0;
494}
495
64fcef2d 496static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
c29727d5
DB
497{
498 struct git_transport_data *data = transport->data;
499 struct ref *refs;
c29727d5 500
64fcef2d
DB
501 connect_setup(transport, for_push, 0);
502 get_remote_heads(data->fd[0], &refs, 0, NULL,
503 for_push ? REF_NORMAL : 0, &data->extra_have);
61b075bd 504 data->got_remote_heads = 1;
c29727d5
DB
505
506 return refs;
507}
508
1788c39c 509static int fetch_refs_via_pack(struct transport *transport,
37148311 510 int nr_heads, struct ref **to_fetch)
c29727d5
DB
511{
512 struct git_transport_data *data = transport->data;
425b1393 513 char **heads = xmalloc(nr_heads * sizeof(*heads));
e4cd6c7a 514 char **origh = xmalloc(nr_heads * sizeof(*origh));
ba227857 515 const struct ref *refs;
c29727d5
DB
516 char *dest = xstrdup(transport->url);
517 struct fetch_pack_args args;
425b1393 518 int i;
00183cbb 519 struct ref *refs_tmp = NULL;
c29727d5 520
fa740529 521 memset(&args, 0, sizeof(args));
aa5af974
IL
522 args.uploadpack = data->options.uploadpack;
523 args.keep_pack = data->options.keep;
fa740529 524 args.lock_pack = 1;
aa5af974
IL
525 args.use_thin_pack = data->options.thin;
526 args.include_tag = data->options.followtags;
9b373229 527 args.verbose = (transport->verbose > 0);
fe8aa148 528 args.quiet = (transport->verbose < 0);
486a3d71 529 args.no_progress = args.quiet || (!transport->progress && !isatty(2));
aa5af974 530 args.depth = data->options.depth;
c29727d5 531
425b1393 532 for (i = 0; i < nr_heads; i++)
e4cd6c7a 533 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
ba227857 534
61b075bd 535 if (!data->got_remote_heads) {
64fcef2d 536 connect_setup(transport, 0, 0);
40c155ff 537 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
61b075bd 538 data->got_remote_heads = 1;
ba227857
DB
539 }
540
00183cbb
DB
541 refs = fetch_pack(&args, data->fd, data->conn,
542 refs_tmp ? refs_tmp : transport->remote_refs,
ba227857
DB
543 dest, nr_heads, heads, &transport->pack_lockfile);
544 close(data->fd[0]);
545 close(data->fd[1]);
546 if (finish_connect(data->conn))
547 refs = NULL;
548 data->conn = NULL;
61b075bd 549 data->got_remote_heads = 0;
c29727d5 550
00183cbb
DB
551 free_refs(refs_tmp);
552
425b1393 553 for (i = 0; i < nr_heads; i++)
e4cd6c7a
SP
554 free(origh[i]);
555 free(origh);
556 free(heads);
c29727d5 557 free(dest);
c6bc4005 558 return (refs ? 0 : -1);
c29727d5
DB
559}
560
481c7a6d
JK
561static int push_had_errors(struct ref *ref)
562{
563 for (; ref; ref = ref->next) {
564 switch (ref->status) {
565 case REF_STATUS_NONE:
566 case REF_STATUS_UPTODATE:
567 case REF_STATUS_OK:
568 break;
569 default:
570 return 1;
571 }
572 }
573 return 0;
574}
575
64fcef2d
DB
576static int refs_pushed(struct ref *ref)
577{
578 for (; ref; ref = ref->next) {
579 switch(ref->status) {
580 case REF_STATUS_NONE:
581 case REF_STATUS_UPTODATE:
582 break;
583 default:
584 return 1;
585 }
586 }
587 return 0;
588}
589
590static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
591{
592 struct refspec rs;
593
594 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
595 return;
596
597 rs.src = ref->name;
598 rs.dst = NULL;
599
600 if (!remote_find_tracking(remote, &rs)) {
601 if (verbose)
602 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
603 if (ref->deletion) {
604 delete_ref(rs.dst, NULL, 0);
605 } else
606 update_ref("update by push", rs.dst,
607 ref->new_sha1, NULL, 0, 0);
608 free(rs.dst);
609 }
610}
611
612#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
613
1965ff74 614static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
64fcef2d 615{
1965ff74
LA
616 if (porcelain) {
617 if (from)
618 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
619 else
620 fprintf(stdout, "%c\t:%s\t", flag, to->name);
621 if (msg)
622 fprintf(stdout, "%s (%s)\n", summary, msg);
623 else
624 fprintf(stdout, "%s\n", summary);
625 } else {
626 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
627 if (from)
628 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
629 else
630 fputs(prettify_refname(to->name), stderr);
631 if (msg) {
632 fputs(" (", stderr);
633 fputs(msg, stderr);
634 fputc(')', stderr);
635 }
636 fputc('\n', stderr);
64fcef2d 637 }
64fcef2d
DB
638}
639
640static const char *status_abbrev(unsigned char sha1[20])
641{
642 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
643}
644
1965ff74 645static void print_ok_ref_status(struct ref *ref, int porcelain)
64fcef2d
DB
646{
647 if (ref->deletion)
1965ff74 648 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
64fcef2d
DB
649 else if (is_null_sha1(ref->old_sha1))
650 print_ref_status('*',
651 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
1965ff74
LA
652 "[new branch]"),
653 ref, ref->peer_ref, NULL, porcelain);
64fcef2d
DB
654 else {
655 char quickref[84];
656 char type;
657 const char *msg;
658
659 strcpy(quickref, status_abbrev(ref->old_sha1));
660 if (ref->nonfastforward) {
661 strcat(quickref, "...");
662 type = '+';
663 msg = "forced update";
664 } else {
665 strcat(quickref, "..");
666 type = ' ';
667 msg = NULL;
668 }
669 strcat(quickref, status_abbrev(ref->new_sha1));
670
1965ff74 671 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
64fcef2d
DB
672 }
673}
674
1965ff74 675static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
64fcef2d
DB
676{
677 if (!count)
678 fprintf(stderr, "To %s\n", dest);
679
680 switch(ref->status) {
681 case REF_STATUS_NONE:
1965ff74 682 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
64fcef2d
DB
683 break;
684 case REF_STATUS_REJECT_NODELETE:
685 print_ref_status('!', "[rejected]", ref, NULL,
1965ff74 686 "remote does not support deleting refs", porcelain);
64fcef2d
DB
687 break;
688 case REF_STATUS_UPTODATE:
689 print_ref_status('=', "[up to date]", ref,
1965ff74 690 ref->peer_ref, NULL, porcelain);
64fcef2d
DB
691 break;
692 case REF_STATUS_REJECT_NONFASTFORWARD:
693 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
a75d7b54 694 "non-fast-forward", porcelain);
64fcef2d
DB
695 break;
696 case REF_STATUS_REMOTE_REJECT:
697 print_ref_status('!', "[remote rejected]", ref,
1965ff74
LA
698 ref->deletion ? NULL : ref->peer_ref,
699 ref->remote_status, porcelain);
64fcef2d
DB
700 break;
701 case REF_STATUS_EXPECTING_REPORT:
702 print_ref_status('!', "[remote failure]", ref,
1965ff74
LA
703 ref->deletion ? NULL : ref->peer_ref,
704 "remote failed to report status", porcelain);
64fcef2d
DB
705 break;
706 case REF_STATUS_OK:
1965ff74 707 print_ok_ref_status(ref, porcelain);
64fcef2d
DB
708 break;
709 }
710
711 return 1;
712}
713
1965ff74 714static void print_push_status(const char *dest, struct ref *refs,
07436e43 715 int verbose, int porcelain, int * nonfastforward)
64fcef2d
DB
716{
717 struct ref *ref;
718 int n = 0;
719
720 if (verbose) {
721 for (ref = refs; ref; ref = ref->next)
722 if (ref->status == REF_STATUS_UPTODATE)
1965ff74 723 n += print_one_push_status(ref, dest, n, porcelain);
64fcef2d
DB
724 }
725
726 for (ref = refs; ref; ref = ref->next)
727 if (ref->status == REF_STATUS_OK)
1965ff74 728 n += print_one_push_status(ref, dest, n, porcelain);
64fcef2d 729
07436e43 730 *nonfastforward = 0;
64fcef2d
DB
731 for (ref = refs; ref; ref = ref->next) {
732 if (ref->status != REF_STATUS_NONE &&
733 ref->status != REF_STATUS_UPTODATE &&
734 ref->status != REF_STATUS_OK)
1965ff74 735 n += print_one_push_status(ref, dest, n, porcelain);
07436e43
MM
736 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
737 *nonfastforward = 1;
64fcef2d
DB
738 }
739}
740
741static void verify_remote_names(int nr_heads, const char **heads)
742{
743 int i;
744
745 for (i = 0; i < nr_heads; i++) {
746 const char *local = heads[i];
747 const char *remote = strrchr(heads[i], ':');
748
749 if (*local == '+')
750 local++;
751
752 /* A matching refspec is okay. */
753 if (remote == local && remote[1] == '\0')
754 continue;
755
756 remote = remote ? (remote + 1) : local;
757 switch (check_ref_format(remote)) {
758 case 0: /* ok */
759 case CHECK_REF_FORMAT_ONELEVEL:
760 /* ok but a single level -- that is fine for
761 * a match pattern.
762 */
763 case CHECK_REF_FORMAT_WILDCARD:
764 /* ok but ends with a pattern-match character */
765 continue;
766 }
767 die("remote part of refspec is not a valid name in %s",
768 heads[i]);
769 }
770}
771
772static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
f3fa1838 773{
9b288516 774 struct git_transport_data *data = transport->data;
40cb4fab 775 struct send_pack_args args;
64fcef2d
DB
776 int ret;
777
61b075bd 778 if (!data->got_remote_heads) {
64fcef2d
DB
779 struct ref *tmp_refs;
780 connect_setup(transport, 1, 0);
781
782 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
783 NULL);
61b075bd 784 data->got_remote_heads = 1;
64fcef2d 785 }
9b288516 786
de1a2fdd 787 memset(&args, 0, sizeof(args));
94c89ba6 788 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
40cb4fab 789 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
aa5af974 790 args.use_thin_pack = data->options.thin;
a108e538 791 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
12070329 792 args.quiet = !!(flags & TRANSPORT_PUSH_QUIET);
40cb4fab
DB
793 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
794
64fcef2d
DB
795 ret = send_pack(&args, data->fd, data->conn, remote_refs,
796 &data->extra_have);
797
798 close(data->fd[1]);
799 close(data->fd[0]);
800 ret |= finish_connect(data->conn);
801 data->conn = NULL;
61b075bd 802 data->got_remote_heads = 0;
64fcef2d
DB
803
804 return ret;
9b288516
DB
805}
806
b236752a
IL
807static int connect_git(struct transport *transport, const char *name,
808 const char *executable, int fd[2])
809{
810 struct git_transport_data *data = transport->data;
811 data->conn = git_connect(data->fd, transport->url,
812 executable, 0);
813 fd[0] = data->fd[0];
814 fd[1] = data->fd[1];
815 return 0;
816}
817
f4e95765
SP
818static int disconnect_git(struct transport *transport)
819{
ba227857
DB
820 struct git_transport_data *data = transport->data;
821 if (data->conn) {
61b075bd
IL
822 if (data->got_remote_heads)
823 packet_flush(data->fd[1]);
ba227857
DB
824 close(data->fd[0]);
825 close(data->fd[1]);
826 finish_connect(data->conn);
827 }
828
829 free(data);
f4e95765
SP
830 return 0;
831}
832
61b075bd
IL
833void transport_take_over(struct transport *transport,
834 struct child_process *child)
835{
836 struct git_transport_data *data;
837
838 if (!transport->smart_options)
839 die("Bug detected: Taking over transport requires non-NULL "
840 "smart_options field.");
841
842 data = xcalloc(1, sizeof(*data));
843 data->options = *transport->smart_options;
844 data->conn = child;
845 data->fd[0] = data->conn->out;
846 data->fd[1] = data->conn->in;
847 data->got_remote_heads = 0;
848 transport->data = data;
849
850 transport->set_option = NULL;
851 transport->get_refs_list = get_refs_via_connect;
852 transport->fetch = fetch_refs_via_pack;
853 transport->push = NULL;
854 transport->push_refs = git_transport_push;
855 transport->disconnect = disconnect_git;
856 transport->smart_options = &(data->options);
857}
858
9b288516
DB
859static int is_local(const char *url)
860{
861 const char *colon = strchr(url, ':');
862 const char *slash = strchr(url, '/');
be501813
JS
863 return !colon || (slash && slash < colon) ||
864 has_dos_drive_prefix(url);
9b288516
DB
865}
866
867static int is_file(const char *url)
868{
869 struct stat buf;
870 if (stat(url, &buf))
871 return 0;
872 return S_ISREG(buf.st_mode);
873}
874
25d5cc48
IL
875static int is_url(const char *url)
876{
877 const char *url2, *first_slash;
878
879 if (!url)
880 return 0;
881 url2 = url;
882 first_slash = strchr(url, '/');
883
884 /* Input with no slash at all or slash first can't be URL. */
885 if (!first_slash || first_slash == url)
886 return 0;
887 /* Character before must be : and next must be /. */
888 if (first_slash[-1] != ':' || first_slash[1] != '/')
889 return 0;
890 /* There must be something before the :// */
891 if (first_slash == url + 1)
892 return 0;
893 /*
894 * Check all characters up to first slash - 1. Only alphanum
895 * is allowed.
896 */
897 url2 = url;
898 while (url2 < first_slash - 1) {
899 if (!isalnum((unsigned char)*url2))
900 return 0;
901 url2++;
902 }
903
904 /* Valid enough. */
905 return 1;
906}
907
908static int external_specification_len(const char *url)
909{
910 return strchr(url, ':') - url;
911}
912
e5f4e214 913struct transport *transport_get(struct remote *remote, const char *url)
9b288516 914{
4da50460 915 const char *helper;
8eb554ae
SP
916 struct transport *ret = xcalloc(1, sizeof(*ret));
917
c1d45cf7
DB
918 if (!remote)
919 die("No remote provided to transport_get()");
920
8eb554ae 921 ret->remote = remote;
4da50460 922 helper = remote->foreign_vcs;
fb0cc87e 923
cb21d8f0 924 if (!url && remote->url)
fb0cc87e 925 url = remote->url[0];
8eb554ae 926 ret->url = url;
8eb554ae 927
87422439
JS
928 /* maybe it is a foreign URL? */
929 if (url) {
930 const char *p = url;
931
932 while (isalnum(*p))
933 p++;
934 if (!prefixcmp(p, "::"))
4da50460 935 helper = xstrndup(url, p - url);
87422439
JS
936 }
937
4da50460
IL
938 if (helper) {
939 transport_helper_init(ret, helper);
25d5cc48 940 } else if (!prefixcmp(url, "rsync:")) {
cd547b48
JS
941 ret->get_refs_list = get_refs_via_rsync;
942 ret->fetch = fetch_objs_via_rsync;
943 ret->push = rsync_transport_push;
aa5af974 944 ret->smart_options = NULL;
9b288516 945 } else if (is_local(url) && is_file(url)) {
c7a8a162 946 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
c7a8a162 947 ret->data = data;
824d5776
SP
948 ret->get_refs_list = get_refs_from_bundle;
949 ret->fetch = fetch_refs_from_bundle;
950 ret->disconnect = close_bundle;
aa5af974 951 ret->smart_options = NULL;
25d5cc48
IL
952 } else if (!is_url(url)
953 || !prefixcmp(url, "file://")
954 || !prefixcmp(url, "git://")
955 || !prefixcmp(url, "ssh://")
956 || !prefixcmp(url, "git+ssh://")
957 || !prefixcmp(url, "ssh+git://")) {
958 /* These are builtin smart transports. */
9b288516 959 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 960 ret->data = data;
aa5af974 961 ret->set_option = NULL;
824d5776
SP
962 ret->get_refs_list = get_refs_via_connect;
963 ret->fetch = fetch_refs_via_pack;
64fcef2d 964 ret->push_refs = git_transport_push;
b236752a 965 ret->connect = connect_git;
f4e95765 966 ret->disconnect = disconnect_git;
aa5af974 967 ret->smart_options = &(data->options);
824d5776 968
ba227857 969 data->conn = NULL;
61b075bd 970 data->got_remote_heads = 0;
25d5cc48
IL
971 } else {
972 /* Unknown protocol in URL. Pass to external handler. */
973 int len = external_specification_len(url);
974 char *handler = xmalloc(len + 1);
975 handler[len] = 0;
976 strncpy(handler, url, len);
977 transport_helper_init(ret, handler);
9b288516 978 }
8eb554ae 979
aa5af974
IL
980 if (ret->smart_options) {
981 ret->smart_options->thin = 1;
982 ret->smart_options->uploadpack = "git-upload-pack";
983 if (remote->uploadpack)
984 ret->smart_options->uploadpack = remote->uploadpack;
985 ret->smart_options->receivepack = "git-receive-pack";
986 if (remote->receivepack)
987 ret->smart_options->receivepack = remote->receivepack;
988 }
989
9b288516
DB
990 return ret;
991}
992
993int transport_set_option(struct transport *transport,
994 const char *name, const char *value)
995{
aa5af974
IL
996 int git_reports = 1, protocol_reports = 1;
997
998 if (transport->smart_options)
999 git_reports = set_git_option(transport->smart_options,
1000 name, value);
1001
824d5776 1002 if (transport->set_option)
aa5af974
IL
1003 protocol_reports = transport->set_option(transport, name,
1004 value);
1005
1006 /* If either report is 0, report 0 (success). */
1007 if (!git_reports || !protocol_reports)
1008 return 0;
1009 /* If either reports -1 (invalid value), report -1. */
1010 if ((git_reports == -1) || (protocol_reports == -1))
1011 return -1;
1012 /* Otherwise if both report unknown, report unknown. */
ab865e6e 1013 return 1;
9b288516
DB
1014}
1015
1016int transport_push(struct transport *transport,
07436e43 1017 int refspec_nr, const char **refspec, int flags,
bb8cccd0 1018 int *nonfastforward)
9b288516 1019{
bb8cccd0 1020 *nonfastforward = 0;
64fcef2d
DB
1021 verify_remote_names(refspec_nr, refspec);
1022
61b075bd 1023 if (transport->push) {
e9fcd1e2
IL
1024 /* Maybe FIXME. But no important transport uses this case. */
1025 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1026 die("This transport does not support using --set-upstream");
1027
64fcef2d 1028 return transport->push(transport, refspec_nr, refspec, flags);
61b075bd 1029 } else if (transport->push_refs) {
64fcef2d
DB
1030 struct ref *remote_refs =
1031 transport->get_refs_list(transport, 1);
64fcef2d
DB
1032 struct ref *local_refs = get_local_heads();
1033 int match_flags = MATCH_REFS_NONE;
1034 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
481c7a6d 1035 int quiet = flags & TRANSPORT_PUSH_QUIET;
1965ff74 1036 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
e9fcd1e2 1037 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
42328267 1038 int ret, err;
64fcef2d
DB
1039
1040 if (flags & TRANSPORT_PUSH_ALL)
1041 match_flags |= MATCH_REFS_ALL;
1042 if (flags & TRANSPORT_PUSH_MIRROR)
1043 match_flags |= MATCH_REFS_MIRROR;
1044
6d2bf96e 1045 if (match_refs(local_refs, &remote_refs,
64fcef2d
DB
1046 refspec_nr, refspec, match_flags)) {
1047 return -1;
1048 }
1049
20e8b465
TRC
1050 set_ref_status_for_push(remote_refs,
1051 flags & TRANSPORT_PUSH_MIRROR,
1052 flags & TRANSPORT_PUSH_FORCE);
1053
64fcef2d 1054 ret = transport->push_refs(transport, remote_refs, flags);
42328267 1055 err = push_had_errors(remote_refs);
64fcef2d 1056
42328267 1057 ret |= err;
64fcef2d 1058
42328267 1059 if (!quiet || err)
481c7a6d 1060 print_push_status(transport->url, remote_refs,
6ffd7812
JH
1061 verbose | porcelain, porcelain,
1062 nonfastforward);
64fcef2d 1063
e9fcd1e2
IL
1064 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1065 set_upstreams(transport, remote_refs, pretend);
1066
64fcef2d
DB
1067 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1068 struct ref *ref;
1069 for (ref = remote_refs; ref; ref = ref->next)
1070 update_tracking_ref(transport->remote, ref, verbose);
1071 }
1072
2e5ed5f2 1073 if (!quiet && !ret && !refs_pushed(remote_refs))
64fcef2d
DB
1074 fprintf(stderr, "Everything up-to-date\n");
1075 return ret;
1076 }
1077 return 1;
9b288516
DB
1078}
1079
4577370e 1080const struct ref *transport_get_remote_refs(struct transport *transport)
c29727d5
DB
1081{
1082 if (!transport->remote_refs)
64fcef2d 1083 transport->remote_refs = transport->get_refs_list(transport, 0);
61b075bd 1084
c29727d5
DB
1085 return transport->remote_refs;
1086}
1087
37148311 1088int transport_fetch_refs(struct transport *transport, struct ref *refs)
c29727d5 1089{
425b1393 1090 int rc;
86386829 1091 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
37148311
DB
1092 struct ref **heads = NULL;
1093 struct ref *rm;
c29727d5
DB
1094
1095 for (rm = refs; rm; rm = rm->next) {
86386829 1096 nr_refs++;
c29727d5 1097 if (rm->peer_ref &&
37148311 1098 !is_null_sha1(rm->old_sha1) &&
c29727d5
DB
1099 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1100 continue;
7a2bff45 1101 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 1102 heads[nr_heads++] = rm;
c29727d5
DB
1103 }
1104
86386829
NP
1105 if (!nr_heads) {
1106 /*
1107 * When deepening of a shallow repository is requested,
1108 * then local and remote refs are likely to still be equal.
1109 * Just feed them all to the fetch method in that case.
1110 * This condition shouldn't be met in a non-deepening fetch
1111 * (see builtin-fetch.c:quickfetch()).
1112 */
1113 heads = xmalloc(nr_refs * sizeof(*heads));
1114 for (rm = refs; rm; rm = rm->next)
1115 heads[nr_heads++] = rm;
1116 }
1117
824d5776 1118 rc = transport->fetch(transport, nr_heads, heads);
61b075bd 1119
c29727d5 1120 free(heads);
425b1393 1121 return rc;
c29727d5
DB
1122}
1123
1788c39c
SP
1124void transport_unlock_pack(struct transport *transport)
1125{
1126 if (transport->pack_lockfile) {
691f1a28 1127 unlink_or_warn(transport->pack_lockfile);
1788c39c
SP
1128 free(transport->pack_lockfile);
1129 transport->pack_lockfile = NULL;
1130 }
1131}
1132
b236752a
IL
1133int transport_connect(struct transport *transport, const char *name,
1134 const char *exec, int fd[2])
1135{
1136 if (transport->connect)
1137 return transport->connect(transport, name, exec, fd);
1138 else
1139 die("Operation not supported by protocol");
1140}
1141
9b288516
DB
1142int transport_disconnect(struct transport *transport)
1143{
1144 int ret = 0;
824d5776
SP
1145 if (transport->disconnect)
1146 ret = transport->disconnect(transport);
9b288516
DB
1147 free(transport);
1148 return ret;
1149}
47abd85b
AE
1150
1151/*
1152 * Strip username (and password) from an url and return
1153 * it in a newly allocated string.
1154 */
1155char *transport_anonymize_url(const char *url)
1156{
1157 char *anon_url, *scheme_prefix, *anon_part;
1158 size_t anon_len, prefix_len = 0;
1159
1160 anon_part = strchr(url, '@');
1161 if (is_local(url) || !anon_part)
1162 goto literal_copy;
1163
1164 anon_len = strlen(++anon_part);
1165 scheme_prefix = strstr(url, "://");
1166 if (!scheme_prefix) {
1167 if (!strchr(anon_part, ':'))
1168 /* cannot be "me@there:/path/name" */
1169 goto literal_copy;
1170 } else {
1171 const char *cp;
1172 /* make sure scheme is reasonable */
1173 for (cp = url; cp < scheme_prefix; cp++) {
1174 switch (*cp) {
1175 /* RFC 1738 2.1 */
1176 case '+': case '.': case '-':
1177 break; /* ok */
1178 default:
1179 if (isalnum(*cp))
1180 break;
1181 /* it isn't */
1182 goto literal_copy;
1183 }
1184 }
1185 /* @ past the first slash does not count */
1186 cp = strchr(scheme_prefix + 3, '/');
1187 if (cp && cp < anon_part)
1188 goto literal_copy;
1189 prefix_len = scheme_prefix - url + 3;
1190 }
1191 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1192 memcpy(anon_url, url, prefix_len);
1193 memcpy(anon_url + prefix_len, anon_part, anon_len);
1194 return anon_url;
1195literal_copy:
1196 return xstrdup(url);
1197}