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