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