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