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