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