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