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