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