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