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