http: create function to get curl allowed protocols
[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"
47a59185
JH
6#include "remote.h"
7#include "connect.h"
40cb4fab 8#include "send-pack.h"
c29727d5 9#include "walker.h"
c7a8a162 10#include "bundle.h"
cd547b48
JS
11#include "dir.h"
12#include "refs.h"
e9fcd1e2 13#include "branch.h"
638794cd 14#include "url.h"
d2b17b32 15#include "submodule.h"
a762e51e 16#include "string-list.h"
13eb4626 17#include "sha1-array.h"
af65f68c 18#include "sigchain.h"
cd547b48 19
e9fcd1e2
IL
20static void set_upstreams(struct transport *transport, struct ref *refs,
21 int pretend)
22{
23 struct ref *ref;
24 for (ref = refs; ref; ref = ref->next) {
25 const char *localname;
26 const char *tmp;
27 const char *remotename;
28 unsigned char sha[20];
29 int flag = 0;
30 /*
31 * Check suitability for tracking. Must be successful /
32 * already up-to-date ref create/modify (not delete).
33 */
34 if (ref->status != REF_STATUS_OK &&
35 ref->status != REF_STATUS_UPTODATE)
36 continue;
37 if (!ref->peer_ref)
38 continue;
f4e54d02 39 if (is_null_oid(&ref->new_oid))
e9fcd1e2
IL
40 continue;
41
42 /* Follow symbolic refs (mainly for HEAD). */
43 localname = ref->peer_ref->name;
44 remotename = ref->name;
7695d118
RS
45 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
46 sha, &flag);
e9fcd1e2 47 if (tmp && flag & REF_ISSYMREF &&
59556548 48 starts_with(tmp, "refs/heads/"))
e9fcd1e2
IL
49 localname = tmp;
50
51 /* Both source and destination must be local branches. */
59556548 52 if (!localname || !starts_with(localname, "refs/heads/"))
e9fcd1e2 53 continue;
59556548 54 if (!remotename || !starts_with(remotename, "refs/heads/"))
e9fcd1e2
IL
55 continue;
56
57 if (!pretend)
58 install_branch_config(BRANCH_CONFIG_VERBOSE,
59 localname + 11, transport->remote->name,
60 remotename);
61 else
62 printf("Would set upstream of '%s' to '%s' of '%s'\n",
63 localname + 11, remotename + 11,
64 transport->remote->name);
65 }
66}
67
c7a8a162
JS
68struct bundle_transport_data {
69 int fd;
70 struct bundle_header header;
71};
72
64fcef2d 73static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
c7a8a162
JS
74{
75 struct bundle_transport_data *data = transport->data;
76 struct ref *result = NULL;
77 int i;
78
64fcef2d
DB
79 if (for_push)
80 return NULL;
81
c7a8a162
JS
82 if (data->fd > 0)
83 close(data->fd);
84 data->fd = read_bundle_header(transport->url, &data->header);
85 if (data->fd < 0)
86 die ("Could not read bundle '%s'.", transport->url);
87 for (i = 0; i < data->header.references.nr; i++) {
88 struct ref_list_entry *e = data->header.references.list + i;
59c69c0c 89 struct ref *ref = alloc_ref(e->name);
f4e54d02 90 hashcpy(ref->old_oid.hash, e->sha1);
c7a8a162
JS
91 ref->next = result;
92 result = ref;
93 }
94 return result;
95}
96
1788c39c 97static int fetch_refs_from_bundle(struct transport *transport,
37148311 98 int nr_heads, struct ref **to_fetch)
c7a8a162
JS
99{
100 struct bundle_transport_data *data = transport->data;
be042aff
JH
101 return unbundle(&data->header, data->fd,
102 transport->progress ? BUNDLE_VERBOSE : 0);
c7a8a162
JS
103}
104
105static int close_bundle(struct transport *transport)
106{
107 struct bundle_transport_data *data = transport->data;
108 if (data->fd > 0)
109 close(data->fd);
f4e95765 110 free(data);
c7a8a162
JS
111 return 0;
112}
113
9b288516 114struct git_transport_data {
aa5af974 115 struct git_transport_options options;
ba227857
DB
116 struct child_process *conn;
117 int fd[2];
61b075bd 118 unsigned got_remote_heads : 1;
13eb4626 119 struct sha1_array extra_have;
beea4152 120 struct sha1_array shallow;
9b288516
DB
121};
122
aa5af974 123static int set_git_option(struct git_transport_options *opts,
9b288516
DB
124 const char *name, const char *value)
125{
c29727d5 126 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
aa5af974 127 opts->uploadpack = value;
c29727d5
DB
128 return 0;
129 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
aa5af974 130 opts->receivepack = value;
9b288516
DB
131 return 0;
132 } else if (!strcmp(name, TRANS_OPT_THIN)) {
aa5af974 133 opts->thin = !!value;
9b288516 134 return 0;
41fa7d2e 135 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
aa5af974 136 opts->followtags = !!value;
41fa7d2e 137 return 0;
c29727d5 138 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
aa5af974 139 opts->keep = !!value;
c29727d5 140 return 0;
48d25cae
NTND
141 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
142 opts->update_shallow = !!value;
143 return 0;
c29727d5
DB
144 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
145 if (!value)
aa5af974 146 opts->depth = 0;
e7622ce8
NTND
147 else {
148 char *end;
149 opts->depth = strtol(value, &end, 0);
150 if (*end)
151 die("transport: invalid depth option '%s'", value);
152 }
c29727d5 153 return 0;
9b288516
DB
154 }
155 return 1;
156}
157
f3ee9ca5 158static int connect_setup(struct transport *transport, int for_push)
ba227857
DB
159{
160 struct git_transport_data *data = transport->data;
f3ee9ca5 161 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
61b075bd
IL
162
163 if (data->conn)
164 return 0;
165
c915f11e
EW
166 switch (transport->family) {
167 case TRANSPORT_FAMILY_ALL: break;
168 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
169 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
170 }
171
5a277f3f
JH
172 data->conn = git_connect(data->fd, transport->url,
173 for_push ? data->options.receivepack :
174 data->options.uploadpack,
f3ee9ca5 175 flags);
61b075bd 176
ba227857
DB
177 return 0;
178}
179
64fcef2d 180static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
c29727d5
DB
181{
182 struct git_transport_data *data = transport->data;
183 struct ref *refs;
c29727d5 184
f3ee9ca5 185 connect_setup(transport, for_push);
85edf4f5 186 get_remote_heads(data->fd[0], NULL, 0, &refs,
beea4152
NTND
187 for_push ? REF_NORMAL : 0,
188 &data->extra_have,
4820a33b 189 &data->shallow);
61b075bd 190 data->got_remote_heads = 1;
c29727d5
DB
191
192 return refs;
193}
194
1788c39c 195static int fetch_refs_via_pack(struct transport *transport,
37148311 196 int nr_heads, struct ref **to_fetch)
c29727d5
DB
197{
198 struct git_transport_data *data = transport->data;
626df76e 199 struct ref *refs;
c29727d5
DB
200 char *dest = xstrdup(transport->url);
201 struct fetch_pack_args args;
00183cbb 202 struct ref *refs_tmp = NULL;
c29727d5 203
fa740529 204 memset(&args, 0, sizeof(args));
aa5af974
IL
205 args.uploadpack = data->options.uploadpack;
206 args.keep_pack = data->options.keep;
fa740529 207 args.lock_pack = 1;
aa5af974
IL
208 args.use_thin_pack = data->options.thin;
209 args.include_tag = data->options.followtags;
bd2c86ef 210 args.verbose = (transport->verbose > 1);
fe8aa148 211 args.quiet = (transport->verbose < 0);
d01b3c02 212 args.no_progress = !transport->progress;
aa5af974 213 args.depth = data->options.depth;
c6807a40
NTND
214 args.check_self_contained_and_connected =
215 data->options.check_self_contained_and_connected;
beea4152 216 args.cloning = transport->cloning;
48d25cae 217 args.update_shallow = data->options.update_shallow;
c29727d5 218
61b075bd 219 if (!data->got_remote_heads) {
f3ee9ca5 220 connect_setup(transport, 0);
b06dcd7d 221 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
4820a33b 222 NULL, &data->shallow);
61b075bd 223 data->got_remote_heads = 1;
ba227857
DB
224 }
225
00183cbb
DB
226 refs = fetch_pack(&args, data->fd, data->conn,
227 refs_tmp ? refs_tmp : transport->remote_refs,
beea4152 228 dest, to_fetch, nr_heads, &data->shallow,
f2db854d 229 &transport->pack_lockfile);
ba227857
DB
230 close(data->fd[0]);
231 close(data->fd[1]);
626df76e
JK
232 if (finish_connect(data->conn)) {
233 free_refs(refs);
ba227857 234 refs = NULL;
626df76e 235 }
ba227857 236 data->conn = NULL;
61b075bd 237 data->got_remote_heads = 0;
c6807a40
NTND
238 data->options.self_contained_and_connected =
239 args.self_contained_and_connected;
c29727d5 240
00183cbb 241 free_refs(refs_tmp);
626df76e 242 free_refs(refs);
c29727d5 243 free(dest);
c6bc4005 244 return (refs ? 0 : -1);
c29727d5
DB
245}
246
481c7a6d
JK
247static int push_had_errors(struct ref *ref)
248{
249 for (; ref; ref = ref->next) {
250 switch (ref->status) {
251 case REF_STATUS_NONE:
252 case REF_STATUS_UPTODATE:
253 case REF_STATUS_OK:
254 break;
255 default:
256 return 1;
257 }
258 }
259 return 0;
260}
261
f1863d0d 262int transport_refs_pushed(struct ref *ref)
64fcef2d
DB
263{
264 for (; ref; ref = ref->next) {
265 switch(ref->status) {
266 case REF_STATUS_NONE:
267 case REF_STATUS_UPTODATE:
268 break;
269 default:
270 return 1;
271 }
272 }
273 return 0;
274}
275
f1863d0d 276void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
64fcef2d
DB
277{
278 struct refspec rs;
279
280 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
281 return;
282
283 rs.src = ref->name;
284 rs.dst = NULL;
285
286 if (!remote_find_tracking(remote, &rs)) {
287 if (verbose)
288 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
289 if (ref->deletion) {
290 delete_ref(rs.dst, NULL, 0);
291 } else
292 update_ref("update by push", rs.dst,
f4e54d02 293 ref->new_oid.hash, NULL, 0, 0);
64fcef2d
DB
294 free(rs.dst);
295 }
296}
297
1965ff74 298static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
64fcef2d 299{
1965ff74
LA
300 if (porcelain) {
301 if (from)
302 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
303 else
304 fprintf(stdout, "%c\t:%s\t", flag, to->name);
305 if (msg)
306 fprintf(stdout, "%s (%s)\n", summary, msg);
307 else
308 fprintf(stdout, "%s\n", summary);
309 } else {
f1863d0d 310 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
1965ff74
LA
311 if (from)
312 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
313 else
314 fputs(prettify_refname(to->name), stderr);
315 if (msg) {
316 fputs(" (", stderr);
317 fputs(msg, stderr);
318 fputc(')', stderr);
319 }
320 fputc('\n', stderr);
64fcef2d 321 }
64fcef2d
DB
322}
323
1965ff74 324static void print_ok_ref_status(struct ref *ref, int porcelain)
64fcef2d
DB
325{
326 if (ref->deletion)
1965ff74 327 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
f4e54d02 328 else if (is_null_oid(&ref->old_oid))
64fcef2d 329 print_ref_status('*',
59556548 330 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
1965ff74
LA
331 "[new branch]"),
332 ref, ref->peer_ref, NULL, porcelain);
64fcef2d 333 else {
bd22d4ff 334 struct strbuf quickref = STRBUF_INIT;
64fcef2d
DB
335 char type;
336 const char *msg;
337
1eb47f16
RS
338 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
339 DEFAULT_ABBREV);
5ece083f 340 if (ref->forced_update) {
bd22d4ff 341 strbuf_addstr(&quickref, "...");
64fcef2d
DB
342 type = '+';
343 msg = "forced update";
344 } else {
bd22d4ff 345 strbuf_addstr(&quickref, "..");
64fcef2d
DB
346 type = ' ';
347 msg = NULL;
348 }
1eb47f16
RS
349 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
350 DEFAULT_ABBREV);
64fcef2d 351
bd22d4ff
JK
352 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
353 strbuf_release(&quickref);
64fcef2d
DB
354 }
355}
356
1965ff74 357static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
64fcef2d 358{
882d49ca
JK
359 if (!count) {
360 char *url = transport_anonymize_url(dest);
361 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
362 free(url);
363 }
64fcef2d
DB
364
365 switch(ref->status) {
366 case REF_STATUS_NONE:
1965ff74 367 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
64fcef2d
DB
368 break;
369 case REF_STATUS_REJECT_NODELETE:
370 print_ref_status('!', "[rejected]", ref, NULL,
1965ff74 371 "remote does not support deleting refs", porcelain);
64fcef2d
DB
372 break;
373 case REF_STATUS_UPTODATE:
374 print_ref_status('=', "[up to date]", ref,
1965ff74 375 ref->peer_ref, NULL, porcelain);
64fcef2d
DB
376 break;
377 case REF_STATUS_REJECT_NONFASTFORWARD:
378 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
a75d7b54 379 "non-fast-forward", porcelain);
64fcef2d 380 break;
dbfeddb1
CR
381 case REF_STATUS_REJECT_ALREADY_EXISTS:
382 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
383 "already exists", porcelain);
384 break;
75e5c0dc
JH
385 case REF_STATUS_REJECT_FETCH_FIRST:
386 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
387 "fetch first", porcelain);
388 break;
389 case REF_STATUS_REJECT_NEEDS_FORCE:
390 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
391 "needs force", porcelain);
392 break;
631b5ef2
JH
393 case REF_STATUS_REJECT_STALE:
394 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
395 "stale info", porcelain);
396 break;
4820a33b
NTND
397 case REF_STATUS_REJECT_SHALLOW:
398 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
399 "new shallow roots not allowed", porcelain);
400 break;
64fcef2d
DB
401 case REF_STATUS_REMOTE_REJECT:
402 print_ref_status('!', "[remote rejected]", ref,
1965ff74
LA
403 ref->deletion ? NULL : ref->peer_ref,
404 ref->remote_status, porcelain);
64fcef2d
DB
405 break;
406 case REF_STATUS_EXPECTING_REPORT:
407 print_ref_status('!', "[remote failure]", ref,
1965ff74
LA
408 ref->deletion ? NULL : ref->peer_ref,
409 "remote failed to report status", porcelain);
64fcef2d 410 break;
4ff17f10
RS
411 case REF_STATUS_ATOMIC_PUSH_FAILED:
412 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
413 "atomic push failed", porcelain);
414 break;
64fcef2d 415 case REF_STATUS_OK:
1965ff74 416 print_ok_ref_status(ref, porcelain);
64fcef2d
DB
417 break;
418 }
419
420 return 1;
421}
422
f1863d0d 423void transport_print_push_status(const char *dest, struct ref *refs,
10643d4e 424 int verbose, int porcelain, unsigned int *reject_reasons)
64fcef2d
DB
425{
426 struct ref *ref;
427 int n = 0;
f25950f3
CT
428 unsigned char head_sha1[20];
429 char *head;
430
7695d118 431 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
64fcef2d
DB
432
433 if (verbose) {
434 for (ref = refs; ref; ref = ref->next)
435 if (ref->status == REF_STATUS_UPTODATE)
1965ff74 436 n += print_one_push_status(ref, dest, n, porcelain);
64fcef2d
DB
437 }
438
439 for (ref = refs; ref; ref = ref->next)
440 if (ref->status == REF_STATUS_OK)
1965ff74 441 n += print_one_push_status(ref, dest, n, porcelain);
64fcef2d 442
10643d4e 443 *reject_reasons = 0;
64fcef2d
DB
444 for (ref = refs; ref; ref = ref->next) {
445 if (ref->status != REF_STATUS_NONE &&
446 ref->status != REF_STATUS_UPTODATE &&
447 ref->status != REF_STATUS_OK)
1965ff74 448 n += print_one_push_status(ref, dest, n, porcelain);
10643d4e 449 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
1d2c14df 450 if (head != NULL && !strcmp(head, ref->name))
10643d4e 451 *reject_reasons |= REJECT_NON_FF_HEAD;
f25950f3 452 else
10643d4e 453 *reject_reasons |= REJECT_NON_FF_OTHER;
dbfeddb1
CR
454 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
455 *reject_reasons |= REJECT_ALREADY_EXISTS;
75e5c0dc
JH
456 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
457 *reject_reasons |= REJECT_FETCH_FIRST;
458 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
459 *reject_reasons |= REJECT_NEEDS_FORCE;
f25950f3 460 }
64fcef2d 461 }
dc76c7f7 462 free(head);
64fcef2d
DB
463}
464
f1863d0d 465void transport_verify_remote_names(int nr_heads, const char **heads)
64fcef2d
DB
466{
467 int i;
468
469 for (i = 0; i < nr_heads; i++) {
470 const char *local = heads[i];
471 const char *remote = strrchr(heads[i], ':');
472
473 if (*local == '+')
474 local++;
475
476 /* A matching refspec is okay. */
477 if (remote == local && remote[1] == '\0')
478 continue;
479
480 remote = remote ? (remote + 1) : local;
8d9c5010
MH
481 if (check_refname_format(remote,
482 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
483 die("remote part of refspec is not a valid name in %s",
484 heads[i]);
64fcef2d
DB
485 }
486}
487
488static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
f3fa1838 489{
9b288516 490 struct git_transport_data *data = transport->data;
40cb4fab 491 struct send_pack_args args;
64fcef2d
DB
492 int ret;
493
61b075bd 494 if (!data->got_remote_heads) {
64fcef2d 495 struct ref *tmp_refs;
f3ee9ca5 496 connect_setup(transport, 1);
64fcef2d 497
b016918b
NTND
498 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
499 NULL, &data->shallow);
61b075bd 500 data->got_remote_heads = 1;
64fcef2d 501 }
9b288516 502
de1a2fdd 503 memset(&args, 0, sizeof(args));
94c89ba6 504 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
40cb4fab 505 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
aa5af974 506 args.use_thin_pack = data->options.thin;
8afd8dc0
TRC
507 args.verbose = (transport->verbose > 0);
508 args.quiet = (transport->verbose < 0);
d7c411b7 509 args.progress = transport->progress;
40cb4fab 510 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
77555854 511 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
d0e8e09c 512 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
9be89160 513 args.url = transport->url;
40cb4fab 514
30261094
DB
515 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
516 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
517 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
518 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
519 else
520 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
521
64fcef2d
DB
522 ret = send_pack(&args, data->fd, data->conn, remote_refs,
523 &data->extra_have);
524
525 close(data->fd[1]);
526 close(data->fd[0]);
527 ret |= finish_connect(data->conn);
528 data->conn = NULL;
61b075bd 529 data->got_remote_heads = 0;
64fcef2d
DB
530
531 return ret;
9b288516
DB
532}
533
b236752a
IL
534static int connect_git(struct transport *transport, const char *name,
535 const char *executable, int fd[2])
536{
537 struct git_transport_data *data = transport->data;
538 data->conn = git_connect(data->fd, transport->url,
539 executable, 0);
540 fd[0] = data->fd[0];
541 fd[1] = data->fd[1];
542 return 0;
543}
544
f4e95765
SP
545static int disconnect_git(struct transport *transport)
546{
ba227857
DB
547 struct git_transport_data *data = transport->data;
548 if (data->conn) {
61b075bd
IL
549 if (data->got_remote_heads)
550 packet_flush(data->fd[1]);
ba227857
DB
551 close(data->fd[0]);
552 close(data->fd[1]);
553 finish_connect(data->conn);
554 }
555
556 free(data);
f4e95765
SP
557 return 0;
558}
559
61b075bd
IL
560void transport_take_over(struct transport *transport,
561 struct child_process *child)
562{
563 struct git_transport_data *data;
564
565 if (!transport->smart_options)
566 die("Bug detected: Taking over transport requires non-NULL "
567 "smart_options field.");
568
569 data = xcalloc(1, sizeof(*data));
570 data->options = *transport->smart_options;
571 data->conn = child;
572 data->fd[0] = data->conn->out;
573 data->fd[1] = data->conn->in;
574 data->got_remote_heads = 0;
575 transport->data = data;
576
577 transport->set_option = NULL;
578 transport->get_refs_list = get_refs_via_connect;
579 transport->fetch = fetch_refs_via_pack;
580 transport->push = NULL;
581 transport->push_refs = git_transport_push;
582 transport->disconnect = disconnect_git;
583 transport->smart_options = &(data->options);
b26ed430
JH
584
585 transport->cannot_reuse = 1;
61b075bd
IL
586}
587
9b288516
DB
588static int is_file(const char *url)
589{
590 struct stat buf;
591 if (stat(url, &buf))
592 return 0;
593 return S_ISREG(buf.st_mode);
594}
595
25d5cc48
IL
596static int external_specification_len(const char *url)
597{
598 return strchr(url, ':') - url;
599}
600
5088d3b3 601static const struct string_list *protocol_whitelist(void)
a5adaced 602{
5088d3b3
JK
603 static int enabled = -1;
604 static struct string_list allowed = STRING_LIST_INIT_DUP;
a5adaced 605
5088d3b3
JK
606 if (enabled < 0) {
607 const char *v = getenv("GIT_ALLOW_PROTOCOL");
608 if (v) {
609 string_list_split(&allowed, v, ':', -1);
610 string_list_sort(&allowed);
611 enabled = 1;
612 } else {
613 enabled = 0;
614 }
615 }
616
617 return enabled ? &allowed : NULL;
618}
a5adaced 619
f1762d77
BW
620enum protocol_allow_config {
621 PROTOCOL_ALLOW_NEVER = 0,
622 PROTOCOL_ALLOW_USER_ONLY,
623 PROTOCOL_ALLOW_ALWAYS
624};
625
626static enum protocol_allow_config parse_protocol_config(const char *key,
627 const char *value)
628{
629 if (!strcasecmp(value, "always"))
630 return PROTOCOL_ALLOW_ALWAYS;
631 else if (!strcasecmp(value, "never"))
632 return PROTOCOL_ALLOW_NEVER;
633 else if (!strcasecmp(value, "user"))
634 return PROTOCOL_ALLOW_USER_ONLY;
635
636 die("unknown value for config '%s': %s", key, value);
637}
638
639static enum protocol_allow_config get_protocol_config(const char *type)
640{
641 char *key = xstrfmt("protocol.%s.allow", type);
642 char *value;
643
644 /* first check the per-protocol config */
645 if (!git_config_get_string(key, &value)) {
646 enum protocol_allow_config ret =
647 parse_protocol_config(key, value);
648 free(key);
649 free(value);
650 return ret;
651 }
652 free(key);
653
654 /* if defined, fallback to user-defined default for unknown protocols */
655 if (!git_config_get_string("protocol.allow", &value)) {
656 enum protocol_allow_config ret =
657 parse_protocol_config("protocol.allow", value);
658 free(value);
659 return ret;
660 }
661
662 /* fallback to built-in defaults */
663 /* known safe */
664 if (!strcmp(type, "http") ||
665 !strcmp(type, "https") ||
666 !strcmp(type, "git") ||
667 !strcmp(type, "ssh") ||
668 !strcmp(type, "file"))
669 return PROTOCOL_ALLOW_ALWAYS;
670
671 /* known scary; err on the side of caution */
672 if (!strcmp(type, "ext"))
673 return PROTOCOL_ALLOW_NEVER;
674
675 /* unknown; by default let them be used only directly by the user */
676 return PROTOCOL_ALLOW_USER_ONLY;
677}
678
5088d3b3
JK
679int is_transport_allowed(const char *type)
680{
f1762d77
BW
681 const struct string_list *whitelist = protocol_whitelist();
682 if (whitelist)
683 return string_list_has_string(whitelist, type);
684
685 switch (get_protocol_config(type)) {
686 case PROTOCOL_ALLOW_ALWAYS:
687 return 1;
688 case PROTOCOL_ALLOW_NEVER:
689 return 0;
690 case PROTOCOL_ALLOW_USER_ONLY:
691 return git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
692 }
693
694 die("BUG: invalid protocol_allow_config type");
5088d3b3
JK
695}
696
697void transport_check_allowed(const char *type)
698{
699 if (!is_transport_allowed(type))
a5adaced 700 die("transport '%s' not allowed", type);
5088d3b3
JK
701}
702
e5f4e214 703struct transport *transport_get(struct remote *remote, const char *url)
9b288516 704{
4da50460 705 const char *helper;
8eb554ae
SP
706 struct transport *ret = xcalloc(1, sizeof(*ret));
707
d01b3c02
TRC
708 ret->progress = isatty(2);
709
c1d45cf7
DB
710 if (!remote)
711 die("No remote provided to transport_get()");
712
b0d66e15 713 ret->got_remote_refs = 0;
8eb554ae 714 ret->remote = remote;
4da50460 715 helper = remote->foreign_vcs;
fb0cc87e 716
cb21d8f0 717 if (!url && remote->url)
fb0cc87e 718 url = remote->url[0];
8eb554ae 719 ret->url = url;
8eb554ae 720
87422439
JS
721 /* maybe it is a foreign URL? */
722 if (url) {
723 const char *p = url;
724
638794cd 725 while (is_urlschemechar(p == url, *p))
87422439 726 p++;
59556548 727 if (starts_with(p, "::"))
4da50460 728 helper = xstrndup(url, p - url);
87422439
JS
729 }
730
4da50460
IL
731 if (helper) {
732 transport_helper_init(ret, helper);
59556548 733 } else if (starts_with(url, "rsync:")) {
0d0bac67 734 die("git-over-rsync is no longer supported");
c59ab2e5 735 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
c7a8a162 736 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
a5adaced 737 transport_check_allowed("file");
c7a8a162 738 ret->data = data;
824d5776
SP
739 ret->get_refs_list = get_refs_from_bundle;
740 ret->fetch = fetch_refs_from_bundle;
741 ret->disconnect = close_bundle;
aa5af974 742 ret->smart_options = NULL;
25d5cc48 743 } else if (!is_url(url)
59556548
CC
744 || starts_with(url, "file://")
745 || starts_with(url, "git://")
746 || starts_with(url, "ssh://")
07c7782c
CMN
747 || starts_with(url, "git+ssh://") /* deprecated - do not use */
748 || starts_with(url, "ssh+git://") /* deprecated - do not use */
749 ) {
a5adaced
JK
750 /*
751 * These are builtin smart transports; "allowed" transports
752 * will be checked individually in git_connect.
753 */
9b288516 754 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 755 ret->data = data;
aa5af974 756 ret->set_option = NULL;
824d5776
SP
757 ret->get_refs_list = get_refs_via_connect;
758 ret->fetch = fetch_refs_via_pack;
64fcef2d 759 ret->push_refs = git_transport_push;
b236752a 760 ret->connect = connect_git;
f4e95765 761 ret->disconnect = disconnect_git;
aa5af974 762 ret->smart_options = &(data->options);
824d5776 763
ba227857 764 data->conn = NULL;
61b075bd 765 data->got_remote_heads = 0;
25d5cc48
IL
766 } else {
767 /* Unknown protocol in URL. Pass to external handler. */
768 int len = external_specification_len(url);
6b33894f 769 char *handler = xmemdupz(url, len);
25d5cc48 770 transport_helper_init(ret, handler);
9b288516 771 }
8eb554ae 772
aa5af974
IL
773 if (ret->smart_options) {
774 ret->smart_options->thin = 1;
775 ret->smart_options->uploadpack = "git-upload-pack";
776 if (remote->uploadpack)
777 ret->smart_options->uploadpack = remote->uploadpack;
778 ret->smart_options->receivepack = "git-receive-pack";
779 if (remote->receivepack)
780 ret->smart_options->receivepack = remote->receivepack;
781 }
782
9b288516
DB
783 return ret;
784}
785
786int transport_set_option(struct transport *transport,
787 const char *name, const char *value)
788{
aa5af974
IL
789 int git_reports = 1, protocol_reports = 1;
790
791 if (transport->smart_options)
792 git_reports = set_git_option(transport->smart_options,
793 name, value);
794
824d5776 795 if (transport->set_option)
aa5af974
IL
796 protocol_reports = transport->set_option(transport, name,
797 value);
798
799 /* If either report is 0, report 0 (success). */
800 if (!git_reports || !protocol_reports)
801 return 0;
802 /* If either reports -1 (invalid value), report -1. */
803 if ((git_reports == -1) || (protocol_reports == -1))
804 return -1;
805 /* Otherwise if both report unknown, report unknown. */
ab865e6e 806 return 1;
9b288516
DB
807}
808
d01b3c02
TRC
809void transport_set_verbosity(struct transport *transport, int verbosity,
810 int force_progress)
bde873c5 811{
bd2c86ef 812 if (verbosity >= 1)
bde873c5
TRC
813 transport->verbose = verbosity <= 3 ? verbosity : 3;
814 if (verbosity < 0)
815 transport->verbose = -1;
d01b3c02
TRC
816
817 /**
818 * Rules used to determine whether to report progress (processing aborts
819 * when a rule is satisfied):
820 *
01fdc21f
CB
821 * . Report progress, if force_progress is 1 (ie. --progress).
822 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
823 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
824 * . Report progress if isatty(2) is 1.
d01b3c02 825 **/
01fdc21f
CB
826 if (force_progress >= 0)
827 transport->progress = !!force_progress;
828 else
829 transport->progress = verbosity >= 0 && isatty(2);
bde873c5
TRC
830}
831
a762e51e
HV
832static void die_with_unpushed_submodules(struct string_list *needs_pushing)
833{
834 int i;
835
836 fprintf(stderr, "The following submodule paths contain changes that can\n"
837 "not be found on any remote:\n");
838 for (i = 0; i < needs_pushing->nr; i++)
839 printf(" %s\n", needs_pushing->items[i].string);
eb21c732
HV
840 fprintf(stderr, "\nPlease try\n\n"
841 " git push --recurse-submodules=on-demand\n\n"
842 "or cd to the path and use\n\n"
843 " git push\n\n"
844 "to push them to a remote.\n\n");
a762e51e
HV
845
846 string_list_clear(needs_pushing, 0);
847
848 die("Aborting.");
849}
850
ec55559f
AS
851static int run_pre_push_hook(struct transport *transport,
852 struct ref *remote_refs)
853{
854 int ret = 0, x;
855 struct ref *r;
d3180279 856 struct child_process proc = CHILD_PROCESS_INIT;
ec55559f
AS
857 struct strbuf buf;
858 const char *argv[4];
859
860 if (!(argv[0] = find_hook("pre-push")))
861 return 0;
862
863 argv[1] = transport->remote->name;
864 argv[2] = transport->url;
865 argv[3] = NULL;
866
ec55559f
AS
867 proc.argv = argv;
868 proc.in = -1;
869
870 if (start_command(&proc)) {
871 finish_command(&proc);
872 return -1;
873 }
874
af65f68c
CB
875 sigchain_push(SIGPIPE, SIG_IGN);
876
ec55559f
AS
877 strbuf_init(&buf, 256);
878
879 for (r = remote_refs; r; r = r->next) {
880 if (!r->peer_ref) continue;
881 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
631b5ef2 882 if (r->status == REF_STATUS_REJECT_STALE) continue;
ec55559f
AS
883 if (r->status == REF_STATUS_UPTODATE) continue;
884
885 strbuf_reset(&buf);
886 strbuf_addf( &buf, "%s %s %s %s\n",
f4e54d02 887 r->peer_ref->name, oid_to_hex(&r->new_oid),
888 r->name, oid_to_hex(&r->old_oid));
ec55559f 889
af65f68c
CB
890 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
891 /* We do not mind if a hook does not read all refs. */
892 if (errno != EPIPE)
893 ret = -1;
ec55559f
AS
894 break;
895 }
896 }
897
898 strbuf_release(&buf);
899
900 x = close(proc.in);
901 if (!ret)
902 ret = x;
903
af65f68c
CB
904 sigchain_pop(SIGPIPE);
905
ec55559f
AS
906 x = finish_command(&proc);
907 if (!ret)
908 ret = x;
909
910 return ret;
911}
912
9b288516 913int transport_push(struct transport *transport,
07436e43 914 int refspec_nr, const char **refspec, int flags,
10643d4e 915 unsigned int *reject_reasons)
9b288516 916{
10643d4e 917 *reject_reasons = 0;
f1863d0d 918 transport_verify_remote_names(refspec_nr, refspec);
64fcef2d 919
61b075bd 920 if (transport->push) {
e9fcd1e2
IL
921 /* Maybe FIXME. But no important transport uses this case. */
922 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
923 die("This transport does not support using --set-upstream");
924
64fcef2d 925 return transport->push(transport, refspec_nr, refspec, flags);
61b075bd 926 } else if (transport->push_refs) {
ba928c13 927 struct ref *remote_refs;
64fcef2d
DB
928 struct ref *local_refs = get_local_heads();
929 int match_flags = MATCH_REFS_NONE;
8afd8dc0
TRC
930 int verbose = (transport->verbose > 0);
931 int quiet = (transport->verbose < 0);
1965ff74 932 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
e9fcd1e2 933 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
77555854 934 int push_ret, ret, err;
64fcef2d 935
ba928c13
JK
936 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
937 return -1;
938
939 remote_refs = transport->get_refs_list(transport, 1);
940
64fcef2d
DB
941 if (flags & TRANSPORT_PUSH_ALL)
942 match_flags |= MATCH_REFS_ALL;
943 if (flags & TRANSPORT_PUSH_MIRROR)
944 match_flags |= MATCH_REFS_MIRROR;
6ddba5e2
FC
945 if (flags & TRANSPORT_PUSH_PRUNE)
946 match_flags |= MATCH_REFS_PRUNE;
c2aba155
JH
947 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
948 match_flags |= MATCH_REFS_FOLLOW_TAGS;
64fcef2d 949
29753cdd
JH
950 if (match_push_refs(local_refs, &remote_refs,
951 refspec_nr, refspec, match_flags)) {
64fcef2d
DB
952 return -1;
953 }
954
91048a95
JH
955 if (transport->smart_options &&
956 transport->smart_options->cas &&
957 !is_empty_cas(transport->smart_options->cas))
958 apply_push_cas(transport->smart_options->cas,
959 transport->remote, remote_refs);
960
20e8b465
TRC
961 set_ref_status_for_push(remote_refs,
962 flags & TRANSPORT_PUSH_MIRROR,
963 flags & TRANSPORT_PUSH_FORCE);
964
ec55559f
AS
965 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
966 if (run_pre_push_hook(transport, remote_refs))
967 return -1;
968
eb21c732 969 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
d2b17b32
FG
970 struct ref *ref = remote_refs;
971 for (; ref; ref = ref->next)
f4e54d02 972 if (!is_null_oid(&ref->new_oid) &&
973 !push_unpushed_submodules(ref->new_oid.hash,
eb21c732
HV
974 transport->remote->name))
975 die ("Failed to push all needed submodules!");
976 }
977
978 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
979 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
d2b17b32 980 struct ref *ref = remote_refs;
f93d7c6f 981 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
a762e51e 982
d2b17b32 983 for (; ref; ref = ref->next)
f4e54d02 984 if (!is_null_oid(&ref->new_oid) &&
985 find_unpushed_submodules(ref->new_oid.hash,
a762e51e
HV
986 transport->remote->name, &needs_pushing))
987 die_with_unpushed_submodules(&needs_pushing);
d2b17b32
FG
988 }
989
77555854 990 push_ret = transport->push_refs(transport, remote_refs, flags);
42328267 991 err = push_had_errors(remote_refs);
77555854 992 ret = push_ret | err;
64fcef2d 993
42328267 994 if (!quiet || err)
f1863d0d 995 transport_print_push_status(transport->url, remote_refs,
6ffd7812 996 verbose | porcelain, porcelain,
10643d4e 997 reject_reasons);
64fcef2d 998
e9fcd1e2
IL
999 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1000 set_upstreams(transport, remote_refs, pretend);
1001
64fcef2d
DB
1002 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1003 struct ref *ref;
1004 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 1005 transport_update_tracking_ref(transport->remote, ref, verbose);
64fcef2d
DB
1006 }
1007
77555854
LA
1008 if (porcelain && !push_ret)
1009 puts("Done");
66bce02e 1010 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
64fcef2d 1011 fprintf(stderr, "Everything up-to-date\n");
77555854 1012
64fcef2d
DB
1013 return ret;
1014 }
1015 return 1;
9b288516
DB
1016}
1017
4577370e 1018const struct ref *transport_get_remote_refs(struct transport *transport)
c29727d5 1019{
b0d66e15 1020 if (!transport->got_remote_refs) {
64fcef2d 1021 transport->remote_refs = transport->get_refs_list(transport, 0);
b0d66e15
TRC
1022 transport->got_remote_refs = 1;
1023 }
61b075bd 1024
c29727d5
DB
1025 return transport->remote_refs;
1026}
1027
37148311 1028int transport_fetch_refs(struct transport *transport, struct ref *refs)
c29727d5 1029{
425b1393 1030 int rc;
86386829 1031 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
37148311
DB
1032 struct ref **heads = NULL;
1033 struct ref *rm;
c29727d5
DB
1034
1035 for (rm = refs; rm; rm = rm->next) {
86386829 1036 nr_refs++;
c29727d5 1037 if (rm->peer_ref &&
f4e54d02 1038 !is_null_oid(&rm->old_oid) &&
1039 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
c29727d5 1040 continue;
7a2bff45 1041 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 1042 heads[nr_heads++] = rm;
c29727d5
DB
1043 }
1044
86386829
NP
1045 if (!nr_heads) {
1046 /*
1047 * When deepening of a shallow repository is requested,
1048 * then local and remote refs are likely to still be equal.
1049 * Just feed them all to the fetch method in that case.
1050 * This condition shouldn't be met in a non-deepening fetch
09b7e220 1051 * (see builtin/fetch.c:quickfetch()).
86386829 1052 */
b32fa95f 1053 ALLOC_ARRAY(heads, nr_refs);
86386829
NP
1054 for (rm = refs; rm; rm = rm->next)
1055 heads[nr_heads++] = rm;
1056 }
1057
824d5776 1058 rc = transport->fetch(transport, nr_heads, heads);
61b075bd 1059
c29727d5 1060 free(heads);
425b1393 1061 return rc;
c29727d5
DB
1062}
1063
1788c39c
SP
1064void transport_unlock_pack(struct transport *transport)
1065{
1066 if (transport->pack_lockfile) {
691f1a28 1067 unlink_or_warn(transport->pack_lockfile);
1788c39c
SP
1068 free(transport->pack_lockfile);
1069 transport->pack_lockfile = NULL;
1070 }
1071}
1072
b236752a
IL
1073int transport_connect(struct transport *transport, const char *name,
1074 const char *exec, int fd[2])
1075{
1076 if (transport->connect)
1077 return transport->connect(transport, name, exec, fd);
1078 else
1079 die("Operation not supported by protocol");
1080}
1081
9b288516
DB
1082int transport_disconnect(struct transport *transport)
1083{
1084 int ret = 0;
824d5776
SP
1085 if (transport->disconnect)
1086 ret = transport->disconnect(transport);
9b288516
DB
1087 free(transport);
1088 return ret;
1089}
47abd85b
AE
1090
1091/*
a7793a74 1092 * Strip username (and password) from a URL and return
47abd85b
AE
1093 * it in a newly allocated string.
1094 */
1095char *transport_anonymize_url(const char *url)
1096{
21f9d0f6 1097 char *scheme_prefix, *anon_part;
47abd85b
AE
1098 size_t anon_len, prefix_len = 0;
1099
1100 anon_part = strchr(url, '@');
c59ab2e5 1101 if (url_is_local_not_ssh(url) || !anon_part)
47abd85b
AE
1102 goto literal_copy;
1103
1104 anon_len = strlen(++anon_part);
1105 scheme_prefix = strstr(url, "://");
1106 if (!scheme_prefix) {
1107 if (!strchr(anon_part, ':'))
1108 /* cannot be "me@there:/path/name" */
1109 goto literal_copy;
1110 } else {
1111 const char *cp;
1112 /* make sure scheme is reasonable */
1113 for (cp = url; cp < scheme_prefix; cp++) {
1114 switch (*cp) {
1115 /* RFC 1738 2.1 */
1116 case '+': case '.': case '-':
1117 break; /* ok */
1118 default:
1119 if (isalnum(*cp))
1120 break;
1121 /* it isn't */
1122 goto literal_copy;
1123 }
1124 }
1125 /* @ past the first slash does not count */
1126 cp = strchr(scheme_prefix + 3, '/');
1127 if (cp && cp < anon_part)
1128 goto literal_copy;
1129 prefix_len = scheme_prefix - url + 3;
1130 }
21f9d0f6
JK
1131 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1132 (int)anon_len, anon_part);
47abd85b
AE
1133literal_copy:
1134 return xstrdup(url);
1135}
36cfda15 1136
114a6a88
JK
1137struct alternate_refs_data {
1138 alternate_ref_fn *fn;
1139 void *data;
1140};
1141
1142static int refs_from_alternate_cb(struct alternate_object_database *e,
1143 void *data)
36cfda15
JH
1144{
1145 char *other;
1146 size_t len;
1147 struct remote *remote;
1148 struct transport *transport;
1149 const struct ref *extra;
114a6a88 1150 struct alternate_refs_data *cb = data;
36cfda15
JH
1151
1152 e->name[-1] = '\0';
91b3c7ce 1153 other = xstrdup(real_path(e->base));
36cfda15
JH
1154 e->name[-1] = '/';
1155 len = strlen(other);
1156
1157 while (other[len-1] == '/')
1158 other[--len] = '\0';
1159 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
def06971 1160 goto out;
36cfda15
JH
1161 /* Is this a git repository with refs? */
1162 memcpy(other + len - 8, "/refs", 6);
1163 if (!is_directory(other))
def06971 1164 goto out;
36cfda15
JH
1165 other[len - 8] = '\0';
1166 remote = remote_get(other);
1167 transport = transport_get(remote, other);
1168 for (extra = transport_get_remote_refs(transport);
1169 extra;
1170 extra = extra->next)
114a6a88 1171 cb->fn(extra, cb->data);
36cfda15 1172 transport_disconnect(transport);
def06971 1173out:
36cfda15
JH
1174 free(other);
1175 return 0;
1176}
114a6a88
JK
1177
1178void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1179{
1180 struct alternate_refs_data cb;
1181 cb.fn = fn;
1182 cb.data = data;
1183 foreach_alt_odb(refs_from_alternate_cb, &cb);
1184}