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