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