Make sha1_array_append take a struct object_id *
[git/git.git] / transport.c
CommitLineData
9b288516
DB
1#include "cache.h"
2#include "transport.h"
3#include "run-command.h"
c29727d5
DB
4#include "pkt-line.h"
5#include "fetch-pack.h"
47a59185
JH
6#include "remote.h"
7#include "connect.h"
40cb4fab 8#include "send-pack.h"
c29727d5 9#include "walker.h"
c7a8a162 10#include "bundle.h"
cd547b48
JS
11#include "dir.h"
12#include "refs.h"
e9fcd1e2 13#include "branch.h"
638794cd 14#include "url.h"
d2b17b32 15#include "submodule.h"
a762e51e 16#include "string-list.h"
13eb4626 17#include "sha1-array.h"
af65f68c 18#include "sigchain.h"
cd547b48 19
e9fcd1e2
IL
20static void set_upstreams(struct transport *transport, struct ref *refs,
21 int pretend)
22{
23 struct ref *ref;
24 for (ref = refs; ref; ref = ref->next) {
25 const char *localname;
26 const char *tmp;
27 const char *remotename;
28 unsigned char sha[20];
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
RS
45 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
46 sha, &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);
f4e54d02 90 hashcpy(ref->old_oid.hash, e->sha1);
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;
13eb4626 119 struct sha1_array extra_have;
beea4152 120 struct sha1_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;
2928325f 474 struct object_id head_oid;
f25950f3 475 char *head;
11fd66de 476 int summary_width = transport_summary_width(refs);
f25950f3 477
2928325f 478 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL);
64fcef2d
DB
479
480 if (verbose) {
481 for (ref = refs; ref; ref = ref->next)
482 if (ref->status == REF_STATUS_UPTODATE)
7101e10c
JH
483 n += print_one_push_status(ref, dest, n,
484 porcelain, summary_width);
64fcef2d
DB
485 }
486
487 for (ref = refs; ref; ref = ref->next)
488 if (ref->status == REF_STATUS_OK)
7101e10c
JH
489 n += print_one_push_status(ref, dest, n,
490 porcelain, summary_width);
64fcef2d 491
10643d4e 492 *reject_reasons = 0;
64fcef2d
DB
493 for (ref = refs; ref; ref = ref->next) {
494 if (ref->status != REF_STATUS_NONE &&
495 ref->status != REF_STATUS_UPTODATE &&
496 ref->status != REF_STATUS_OK)
7101e10c
JH
497 n += print_one_push_status(ref, dest, n,
498 porcelain, summary_width);
10643d4e 499 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
1d2c14df 500 if (head != NULL && !strcmp(head, ref->name))
10643d4e 501 *reject_reasons |= REJECT_NON_FF_HEAD;
f25950f3 502 else
10643d4e 503 *reject_reasons |= REJECT_NON_FF_OTHER;
dbfeddb1
CR
504 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
505 *reject_reasons |= REJECT_ALREADY_EXISTS;
75e5c0dc
JH
506 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
507 *reject_reasons |= REJECT_FETCH_FIRST;
508 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
509 *reject_reasons |= REJECT_NEEDS_FORCE;
f25950f3 510 }
64fcef2d 511 }
dc76c7f7 512 free(head);
64fcef2d
DB
513}
514
f1863d0d 515void transport_verify_remote_names(int nr_heads, const char **heads)
64fcef2d
DB
516{
517 int i;
518
519 for (i = 0; i < nr_heads; i++) {
520 const char *local = heads[i];
521 const char *remote = strrchr(heads[i], ':');
522
523 if (*local == '+')
524 local++;
525
526 /* A matching refspec is okay. */
527 if (remote == local && remote[1] == '\0')
528 continue;
529
530 remote = remote ? (remote + 1) : local;
8d9c5010
MH
531 if (check_refname_format(remote,
532 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
533 die("remote part of refspec is not a valid name in %s",
534 heads[i]);
64fcef2d
DB
535 }
536}
537
538static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
f3fa1838 539{
9b288516 540 struct git_transport_data *data = transport->data;
40cb4fab 541 struct send_pack_args args;
64fcef2d
DB
542 int ret;
543
61b075bd 544 if (!data->got_remote_heads) {
64fcef2d 545 struct ref *tmp_refs;
f3ee9ca5 546 connect_setup(transport, 1);
64fcef2d 547
b016918b
NTND
548 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
549 NULL, &data->shallow);
61b075bd 550 data->got_remote_heads = 1;
64fcef2d 551 }
9b288516 552
de1a2fdd 553 memset(&args, 0, sizeof(args));
94c89ba6 554 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
40cb4fab 555 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
aa5af974 556 args.use_thin_pack = data->options.thin;
8afd8dc0
TRC
557 args.verbose = (transport->verbose > 0);
558 args.quiet = (transport->verbose < 0);
d7c411b7 559 args.progress = transport->progress;
40cb4fab 560 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
77555854 561 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
d0e8e09c 562 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
f6a4e61f 563 args.push_options = transport->push_options;
9be89160 564 args.url = transport->url;
40cb4fab 565
30261094
DB
566 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
567 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
568 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
569 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
570 else
571 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
572
64fcef2d
DB
573 ret = send_pack(&args, data->fd, data->conn, remote_refs,
574 &data->extra_have);
575
576 close(data->fd[1]);
577 close(data->fd[0]);
578 ret |= finish_connect(data->conn);
579 data->conn = NULL;
61b075bd 580 data->got_remote_heads = 0;
64fcef2d
DB
581
582 return ret;
9b288516
DB
583}
584
b236752a
IL
585static int connect_git(struct transport *transport, const char *name,
586 const char *executable, int fd[2])
587{
588 struct git_transport_data *data = transport->data;
589 data->conn = git_connect(data->fd, transport->url,
590 executable, 0);
591 fd[0] = data->fd[0];
592 fd[1] = data->fd[1];
593 return 0;
594}
595
f4e95765
SP
596static int disconnect_git(struct transport *transport)
597{
ba227857
DB
598 struct git_transport_data *data = transport->data;
599 if (data->conn) {
61b075bd
IL
600 if (data->got_remote_heads)
601 packet_flush(data->fd[1]);
ba227857
DB
602 close(data->fd[0]);
603 close(data->fd[1]);
604 finish_connect(data->conn);
605 }
606
607 free(data);
f4e95765
SP
608 return 0;
609}
610
61b075bd
IL
611void transport_take_over(struct transport *transport,
612 struct child_process *child)
613{
614 struct git_transport_data *data;
615
616 if (!transport->smart_options)
ef1177d1 617 die("BUG: taking over transport requires non-NULL "
61b075bd
IL
618 "smart_options field.");
619
620 data = xcalloc(1, sizeof(*data));
621 data->options = *transport->smart_options;
622 data->conn = child;
623 data->fd[0] = data->conn->out;
624 data->fd[1] = data->conn->in;
625 data->got_remote_heads = 0;
626 transport->data = data;
627
628 transport->set_option = NULL;
629 transport->get_refs_list = get_refs_via_connect;
630 transport->fetch = fetch_refs_via_pack;
631 transport->push = NULL;
632 transport->push_refs = git_transport_push;
633 transport->disconnect = disconnect_git;
634 transport->smart_options = &(data->options);
b26ed430
JH
635
636 transport->cannot_reuse = 1;
61b075bd
IL
637}
638
9b288516
DB
639static int is_file(const char *url)
640{
641 struct stat buf;
642 if (stat(url, &buf))
643 return 0;
644 return S_ISREG(buf.st_mode);
645}
646
25d5cc48
IL
647static int external_specification_len(const char *url)
648{
649 return strchr(url, ':') - url;
650}
651
5088d3b3 652static const struct string_list *protocol_whitelist(void)
a5adaced 653{
5088d3b3
JK
654 static int enabled = -1;
655 static struct string_list allowed = STRING_LIST_INIT_DUP;
a5adaced 656
5088d3b3
JK
657 if (enabled < 0) {
658 const char *v = getenv("GIT_ALLOW_PROTOCOL");
659 if (v) {
660 string_list_split(&allowed, v, ':', -1);
661 string_list_sort(&allowed);
662 enabled = 1;
663 } else {
664 enabled = 0;
665 }
666 }
667
668 return enabled ? &allowed : NULL;
669}
a5adaced 670
f1762d77
BW
671enum protocol_allow_config {
672 PROTOCOL_ALLOW_NEVER = 0,
673 PROTOCOL_ALLOW_USER_ONLY,
674 PROTOCOL_ALLOW_ALWAYS
675};
676
677static enum protocol_allow_config parse_protocol_config(const char *key,
678 const char *value)
5088d3b3 679{
f1762d77
BW
680 if (!strcasecmp(value, "always"))
681 return PROTOCOL_ALLOW_ALWAYS;
682 else if (!strcasecmp(value, "never"))
683 return PROTOCOL_ALLOW_NEVER;
684 else if (!strcasecmp(value, "user"))
685 return PROTOCOL_ALLOW_USER_ONLY;
686
687 die("unknown value for config '%s': %s", key, value);
5088d3b3
JK
688}
689
f1762d77 690static enum protocol_allow_config get_protocol_config(const char *type)
5088d3b3 691{
f1762d77
BW
692 char *key = xstrfmt("protocol.%s.allow", type);
693 char *value;
694
695 /* first check the per-protocol config */
696 if (!git_config_get_string(key, &value)) {
697 enum protocol_allow_config ret =
698 parse_protocol_config(key, value);
699 free(key);
700 free(value);
701 return ret;
702 }
703 free(key);
704
705 /* if defined, fallback to user-defined default for unknown protocols */
706 if (!git_config_get_string("protocol.allow", &value)) {
707 enum protocol_allow_config ret =
708 parse_protocol_config("protocol.allow", value);
709 free(value);
710 return ret;
711 }
712
713 /* fallback to built-in defaults */
714 /* known safe */
715 if (!strcmp(type, "http") ||
716 !strcmp(type, "https") ||
717 !strcmp(type, "git") ||
718 !strcmp(type, "ssh") ||
719 !strcmp(type, "file"))
720 return PROTOCOL_ALLOW_ALWAYS;
721
722 /* known scary; err on the side of caution */
723 if (!strcmp(type, "ext"))
724 return PROTOCOL_ALLOW_NEVER;
725
726 /* unknown; by default let them be used only directly by the user */
727 return PROTOCOL_ALLOW_USER_ONLY;
5088d3b3
JK
728}
729
a768a022 730int is_transport_allowed(const char *type, int from_user)
5088d3b3 731{
f1762d77
BW
732 const struct string_list *whitelist = protocol_whitelist();
733 if (whitelist)
734 return string_list_has_string(whitelist, type);
735
736 switch (get_protocol_config(type)) {
737 case PROTOCOL_ALLOW_ALWAYS:
738 return 1;
739 case PROTOCOL_ALLOW_NEVER:
740 return 0;
741 case PROTOCOL_ALLOW_USER_ONLY:
a768a022
BW
742 if (from_user < 0)
743 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
744 return from_user;
f1762d77
BW
745 }
746
747 die("BUG: invalid protocol_allow_config type");
5088d3b3
JK
748}
749
750void transport_check_allowed(const char *type)
751{
a768a022 752 if (!is_transport_allowed(type, -1))
a5adaced 753 die("transport '%s' not allowed", type);
a5adaced
JK
754}
755
e5f4e214 756struct transport *transport_get(struct remote *remote, const char *url)
9b288516 757{
4da50460 758 const char *helper;
8eb554ae
SP
759 struct transport *ret = xcalloc(1, sizeof(*ret));
760
d01b3c02
TRC
761 ret->progress = isatty(2);
762
c1d45cf7
DB
763 if (!remote)
764 die("No remote provided to transport_get()");
765
b0d66e15 766 ret->got_remote_refs = 0;
8eb554ae 767 ret->remote = remote;
4da50460 768 helper = remote->foreign_vcs;
fb0cc87e 769
cb21d8f0 770 if (!url && remote->url)
fb0cc87e 771 url = remote->url[0];
8eb554ae 772 ret->url = url;
8eb554ae 773
87422439
JS
774 /* maybe it is a foreign URL? */
775 if (url) {
776 const char *p = url;
777
638794cd 778 while (is_urlschemechar(p == url, *p))
87422439 779 p++;
59556548 780 if (starts_with(p, "::"))
4da50460 781 helper = xstrndup(url, p - url);
87422439
JS
782 }
783
4da50460
IL
784 if (helper) {
785 transport_helper_init(ret, helper);
59556548 786 } else if (starts_with(url, "rsync:")) {
0d0bac67 787 die("git-over-rsync is no longer supported");
c59ab2e5 788 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
c7a8a162 789 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
a5adaced 790 transport_check_allowed("file");
c7a8a162 791 ret->data = data;
824d5776
SP
792 ret->get_refs_list = get_refs_from_bundle;
793 ret->fetch = fetch_refs_from_bundle;
794 ret->disconnect = close_bundle;
aa5af974 795 ret->smart_options = NULL;
25d5cc48 796 } else if (!is_url(url)
59556548
CC
797 || starts_with(url, "file://")
798 || starts_with(url, "git://")
799 || starts_with(url, "ssh://")
07c7782c
CMN
800 || starts_with(url, "git+ssh://") /* deprecated - do not use */
801 || starts_with(url, "ssh+git://") /* deprecated - do not use */
802 ) {
a5adaced
JK
803 /*
804 * These are builtin smart transports; "allowed" transports
805 * will be checked individually in git_connect.
806 */
9b288516 807 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 808 ret->data = data;
aa5af974 809 ret->set_option = NULL;
824d5776
SP
810 ret->get_refs_list = get_refs_via_connect;
811 ret->fetch = fetch_refs_via_pack;
64fcef2d 812 ret->push_refs = git_transport_push;
b236752a 813 ret->connect = connect_git;
f4e95765 814 ret->disconnect = disconnect_git;
aa5af974 815 ret->smart_options = &(data->options);
824d5776 816
ba227857 817 data->conn = NULL;
61b075bd 818 data->got_remote_heads = 0;
25d5cc48
IL
819 } else {
820 /* Unknown protocol in URL. Pass to external handler. */
821 int len = external_specification_len(url);
6b33894f 822 char *handler = xmemdupz(url, len);
25d5cc48 823 transport_helper_init(ret, handler);
9b288516 824 }
8eb554ae 825
aa5af974
IL
826 if (ret->smart_options) {
827 ret->smart_options->thin = 1;
828 ret->smart_options->uploadpack = "git-upload-pack";
829 if (remote->uploadpack)
830 ret->smart_options->uploadpack = remote->uploadpack;
831 ret->smart_options->receivepack = "git-receive-pack";
832 if (remote->receivepack)
833 ret->smart_options->receivepack = remote->receivepack;
834 }
835
9b288516
DB
836 return ret;
837}
838
839int transport_set_option(struct transport *transport,
840 const char *name, const char *value)
841{
aa5af974
IL
842 int git_reports = 1, protocol_reports = 1;
843
844 if (transport->smart_options)
845 git_reports = set_git_option(transport->smart_options,
846 name, value);
847
824d5776 848 if (transport->set_option)
aa5af974
IL
849 protocol_reports = transport->set_option(transport, name,
850 value);
851
852 /* If either report is 0, report 0 (success). */
853 if (!git_reports || !protocol_reports)
854 return 0;
855 /* If either reports -1 (invalid value), report -1. */
856 if ((git_reports == -1) || (protocol_reports == -1))
857 return -1;
858 /* Otherwise if both report unknown, report unknown. */
ab865e6e 859 return 1;
9b288516
DB
860}
861
d01b3c02
TRC
862void transport_set_verbosity(struct transport *transport, int verbosity,
863 int force_progress)
bde873c5 864{
bd2c86ef 865 if (verbosity >= 1)
bde873c5
TRC
866 transport->verbose = verbosity <= 3 ? verbosity : 3;
867 if (verbosity < 0)
868 transport->verbose = -1;
d01b3c02
TRC
869
870 /**
871 * Rules used to determine whether to report progress (processing aborts
872 * when a rule is satisfied):
873 *
01fdc21f
CB
874 * . Report progress, if force_progress is 1 (ie. --progress).
875 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
876 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
877 * . Report progress if isatty(2) is 1.
d01b3c02 878 **/
01fdc21f
CB
879 if (force_progress >= 0)
880 transport->progress = !!force_progress;
881 else
882 transport->progress = verbosity >= 0 && isatty(2);
bde873c5
TRC
883}
884
a762e51e
HV
885static void die_with_unpushed_submodules(struct string_list *needs_pushing)
886{
887 int i;
888
7ab1d44f
VA
889 fprintf(stderr, _("The following submodule paths contain changes that can\n"
890 "not be found on any remote:\n"));
a762e51e 891 for (i = 0; i < needs_pushing->nr; i++)
5cb5fe4a 892 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
7ab1d44f
VA
893 fprintf(stderr, _("\nPlease try\n\n"
894 " git push --recurse-submodules=on-demand\n\n"
895 "or cd to the path and use\n\n"
896 " git push\n\n"
897 "to push them to a remote.\n\n"));
a762e51e
HV
898
899 string_list_clear(needs_pushing, 0);
900
7ab1d44f 901 die(_("Aborting."));
a762e51e
HV
902}
903
ec55559f
AS
904static int run_pre_push_hook(struct transport *transport,
905 struct ref *remote_refs)
906{
907 int ret = 0, x;
908 struct ref *r;
d3180279 909 struct child_process proc = CHILD_PROCESS_INIT;
ec55559f
AS
910 struct strbuf buf;
911 const char *argv[4];
912
913 if (!(argv[0] = find_hook("pre-push")))
914 return 0;
915
916 argv[1] = transport->remote->name;
917 argv[2] = transport->url;
918 argv[3] = NULL;
919
ec55559f
AS
920 proc.argv = argv;
921 proc.in = -1;
922
923 if (start_command(&proc)) {
924 finish_command(&proc);
925 return -1;
926 }
927
af65f68c
CB
928 sigchain_push(SIGPIPE, SIG_IGN);
929
ec55559f
AS
930 strbuf_init(&buf, 256);
931
932 for (r = remote_refs; r; r = r->next) {
933 if (!r->peer_ref) continue;
934 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
631b5ef2 935 if (r->status == REF_STATUS_REJECT_STALE) continue;
ec55559f
AS
936 if (r->status == REF_STATUS_UPTODATE) continue;
937
938 strbuf_reset(&buf);
939 strbuf_addf( &buf, "%s %s %s %s\n",
f4e54d02 940 r->peer_ref->name, oid_to_hex(&r->new_oid),
941 r->name, oid_to_hex(&r->old_oid));
ec55559f 942
af65f68c
CB
943 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
944 /* We do not mind if a hook does not read all refs. */
945 if (errno != EPIPE)
946 ret = -1;
ec55559f
AS
947 break;
948 }
949 }
950
951 strbuf_release(&buf);
952
953 x = close(proc.in);
954 if (!ret)
955 ret = x;
956
af65f68c
CB
957 sigchain_pop(SIGPIPE);
958
ec55559f
AS
959 x = finish_command(&proc);
960 if (!ret)
961 ret = x;
962
963 return ret;
964}
965
9b288516 966int transport_push(struct transport *transport,
07436e43 967 int refspec_nr, const char **refspec, int flags,
10643d4e 968 unsigned int *reject_reasons)
9b288516 969{
10643d4e 970 *reject_reasons = 0;
f1863d0d 971 transport_verify_remote_names(refspec_nr, refspec);
64fcef2d 972
61b075bd 973 if (transport->push) {
e9fcd1e2
IL
974 /* Maybe FIXME. But no important transport uses this case. */
975 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
976 die("This transport does not support using --set-upstream");
977
64fcef2d 978 return transport->push(transport, refspec_nr, refspec, flags);
61b075bd 979 } else if (transport->push_refs) {
ba928c13 980 struct ref *remote_refs;
64fcef2d
DB
981 struct ref *local_refs = get_local_heads();
982 int match_flags = MATCH_REFS_NONE;
8afd8dc0
TRC
983 int verbose = (transport->verbose > 0);
984 int quiet = (transport->verbose < 0);
1965ff74 985 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
e9fcd1e2 986 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
77555854 987 int push_ret, ret, err;
64fcef2d 988
ba928c13
JK
989 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
990 return -1;
991
992 remote_refs = transport->get_refs_list(transport, 1);
993
64fcef2d
DB
994 if (flags & TRANSPORT_PUSH_ALL)
995 match_flags |= MATCH_REFS_ALL;
996 if (flags & TRANSPORT_PUSH_MIRROR)
997 match_flags |= MATCH_REFS_MIRROR;
6ddba5e2
FC
998 if (flags & TRANSPORT_PUSH_PRUNE)
999 match_flags |= MATCH_REFS_PRUNE;
c2aba155
JH
1000 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1001 match_flags |= MATCH_REFS_FOLLOW_TAGS;
64fcef2d 1002
29753cdd
JH
1003 if (match_push_refs(local_refs, &remote_refs,
1004 refspec_nr, refspec, match_flags)) {
64fcef2d
DB
1005 return -1;
1006 }
1007
91048a95
JH
1008 if (transport->smart_options &&
1009 transport->smart_options->cas &&
1010 !is_empty_cas(transport->smart_options->cas))
1011 apply_push_cas(transport->smart_options->cas,
1012 transport->remote, remote_refs);
1013
20e8b465
TRC
1014 set_ref_status_for_push(remote_refs,
1015 flags & TRANSPORT_PUSH_MIRROR,
1016 flags & TRANSPORT_PUSH_FORCE);
1017
ec55559f
AS
1018 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1019 if (run_pre_push_hook(transport, remote_refs))
1020 return -1;
1021
225e8bf7
BW
1022 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1023 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1024 !is_bare_repository()) {
d2b17b32 1025 struct ref *ref = remote_refs;
9cfa1c26
HV
1026 struct sha1_array commits = SHA1_ARRAY_INIT;
1027
d2b17b32 1028 for (; ref; ref = ref->next)
9cfa1c26 1029 if (!is_null_oid(&ref->new_oid))
98a72ddc 1030 sha1_array_append(&commits,
1031 &ref->new_oid);
9cfa1c26 1032
0301c821
BW
1033 if (!push_unpushed_submodules(&commits,
1034 transport->remote->name,
1035 pretend)) {
9cfa1c26
HV
1036 sha1_array_clear(&commits);
1037 die("Failed to push all needed submodules!");
1038 }
1039 sha1_array_clear(&commits);
eb21c732
HV
1040 }
1041
0301c821 1042 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
225e8bf7
BW
1043 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1044 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
0301c821 1045 !pretend)) && !is_bare_repository()) {
d2b17b32 1046 struct ref *ref = remote_refs;
f93d7c6f 1047 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
9cfa1c26 1048 struct sha1_array commits = SHA1_ARRAY_INIT;
a762e51e 1049
d2b17b32 1050 for (; ref; ref = ref->next)
9cfa1c26 1051 if (!is_null_oid(&ref->new_oid))
98a72ddc 1052 sha1_array_append(&commits,
1053 &ref->new_oid);
9cfa1c26
HV
1054
1055 if (find_unpushed_submodules(&commits, transport->remote->name,
1056 &needs_pushing)) {
1057 sha1_array_clear(&commits);
1058 die_with_unpushed_submodules(&needs_pushing);
1059 }
1060 string_list_clear(&needs_pushing, 0);
1061 sha1_array_clear(&commits);
d2b17b32
FG
1062 }
1063
225e8bf7
BW
1064 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1065 push_ret = transport->push_refs(transport, remote_refs, flags);
1066 else
1067 push_ret = 0;
42328267 1068 err = push_had_errors(remote_refs);
77555854 1069 ret = push_ret | err;
64fcef2d 1070
42328267 1071 if (!quiet || err)
f1863d0d 1072 transport_print_push_status(transport->url, remote_refs,
6ffd7812 1073 verbose | porcelain, porcelain,
10643d4e 1074 reject_reasons);
64fcef2d 1075
e9fcd1e2
IL
1076 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1077 set_upstreams(transport, remote_refs, pretend);
1078
225e8bf7
BW
1079 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1080 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
64fcef2d
DB
1081 struct ref *ref;
1082 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 1083 transport_update_tracking_ref(transport->remote, ref, verbose);
64fcef2d
DB
1084 }
1085
77555854
LA
1086 if (porcelain && !push_ret)
1087 puts("Done");
66bce02e 1088 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
64fcef2d 1089 fprintf(stderr, "Everything up-to-date\n");
77555854 1090
64fcef2d
DB
1091 return ret;
1092 }
1093 return 1;
9b288516
DB
1094}
1095
4577370e 1096const struct ref *transport_get_remote_refs(struct transport *transport)
c29727d5 1097{
b0d66e15 1098 if (!transport->got_remote_refs) {
64fcef2d 1099 transport->remote_refs = transport->get_refs_list(transport, 0);
b0d66e15
TRC
1100 transport->got_remote_refs = 1;
1101 }
61b075bd 1102
c29727d5
DB
1103 return transport->remote_refs;
1104}
1105
37148311 1106int transport_fetch_refs(struct transport *transport, struct ref *refs)
c29727d5 1107{
425b1393 1108 int rc;
86386829 1109 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
37148311
DB
1110 struct ref **heads = NULL;
1111 struct ref *rm;
c29727d5
DB
1112
1113 for (rm = refs; rm; rm = rm->next) {
86386829 1114 nr_refs++;
c29727d5 1115 if (rm->peer_ref &&
f4e54d02 1116 !is_null_oid(&rm->old_oid) &&
1117 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
c29727d5 1118 continue;
7a2bff45 1119 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 1120 heads[nr_heads++] = rm;
c29727d5
DB
1121 }
1122
86386829
NP
1123 if (!nr_heads) {
1124 /*
1125 * When deepening of a shallow repository is requested,
1126 * then local and remote refs are likely to still be equal.
1127 * Just feed them all to the fetch method in that case.
1128 * This condition shouldn't be met in a non-deepening fetch
09b7e220 1129 * (see builtin/fetch.c:quickfetch()).
86386829 1130 */
b32fa95f 1131 ALLOC_ARRAY(heads, nr_refs);
86386829
NP
1132 for (rm = refs; rm; rm = rm->next)
1133 heads[nr_heads++] = rm;
1134 }
1135
824d5776 1136 rc = transport->fetch(transport, nr_heads, heads);
61b075bd 1137
c29727d5 1138 free(heads);
425b1393 1139 return rc;
c29727d5
DB
1140}
1141
1788c39c
SP
1142void transport_unlock_pack(struct transport *transport)
1143{
1144 if (transport->pack_lockfile) {
691f1a28 1145 unlink_or_warn(transport->pack_lockfile);
1788c39c
SP
1146 free(transport->pack_lockfile);
1147 transport->pack_lockfile = NULL;
1148 }
1149}
1150
b236752a
IL
1151int transport_connect(struct transport *transport, const char *name,
1152 const char *exec, int fd[2])
1153{
1154 if (transport->connect)
1155 return transport->connect(transport, name, exec, fd);
1156 else
1157 die("Operation not supported by protocol");
1158}
1159
9b288516
DB
1160int transport_disconnect(struct transport *transport)
1161{
1162 int ret = 0;
824d5776
SP
1163 if (transport->disconnect)
1164 ret = transport->disconnect(transport);
9b288516
DB
1165 free(transport);
1166 return ret;
1167}
47abd85b
AE
1168
1169/*
a7793a74 1170 * Strip username (and password) from a URL and return
47abd85b
AE
1171 * it in a newly allocated string.
1172 */
1173char *transport_anonymize_url(const char *url)
1174{
21f9d0f6 1175 char *scheme_prefix, *anon_part;
47abd85b
AE
1176 size_t anon_len, prefix_len = 0;
1177
1178 anon_part = strchr(url, '@');
c59ab2e5 1179 if (url_is_local_not_ssh(url) || !anon_part)
47abd85b
AE
1180 goto literal_copy;
1181
1182 anon_len = strlen(++anon_part);
1183 scheme_prefix = strstr(url, "://");
1184 if (!scheme_prefix) {
1185 if (!strchr(anon_part, ':'))
1186 /* cannot be "me@there:/path/name" */
1187 goto literal_copy;
1188 } else {
1189 const char *cp;
1190 /* make sure scheme is reasonable */
1191 for (cp = url; cp < scheme_prefix; cp++) {
1192 switch (*cp) {
1193 /* RFC 1738 2.1 */
1194 case '+': case '.': case '-':
1195 break; /* ok */
1196 default:
1197 if (isalnum(*cp))
1198 break;
1199 /* it isn't */
1200 goto literal_copy;
1201 }
1202 }
1203 /* @ past the first slash does not count */
1204 cp = strchr(scheme_prefix + 3, '/');
1205 if (cp && cp < anon_part)
1206 goto literal_copy;
1207 prefix_len = scheme_prefix - url + 3;
1208 }
21f9d0f6
JK
1209 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1210 (int)anon_len, anon_part);
47abd85b
AE
1211literal_copy:
1212 return xstrdup(url);
1213}
36cfda15 1214
a10a1787
JK
1215static void read_alternate_refs(const char *path,
1216 alternate_ref_fn *cb,
1217 void *data)
1218{
1219 struct child_process cmd = CHILD_PROCESS_INIT;
1220 struct strbuf line = STRBUF_INIT;
1221 FILE *fh;
1222
1223 cmd.git_cmd = 1;
1224 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1225 argv_array_push(&cmd.args, "for-each-ref");
1226 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1227 cmd.env = local_repo_env;
1228 cmd.out = -1;
1229
1230 if (start_command(&cmd))
1231 return;
1232
1233 fh = xfdopen(cmd.out, "r");
1234 while (strbuf_getline_lf(&line, fh) != EOF) {
1235 struct object_id oid;
1236
1237 if (get_oid_hex(line.buf, &oid) ||
1238 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1239 warning("invalid line while parsing alternate refs: %s",
1240 line.buf);
1241 break;
1242 }
1243
1244 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1245 }
1246
1247 fclose(fh);
1248 finish_command(&cmd);
1249}
1250
114a6a88
JK
1251struct alternate_refs_data {
1252 alternate_ref_fn *fn;
1253 void *data;
1254};
1255
1256static int refs_from_alternate_cb(struct alternate_object_database *e,
1257 void *data)
36cfda15 1258{
5e8c968c
JK
1259 struct strbuf path = STRBUF_INIT;
1260 size_t base_len;
114a6a88 1261 struct alternate_refs_data *cb = data;
36cfda15 1262
5e8c968c
JK
1263 if (!strbuf_realpath(&path, e->path, 0))
1264 goto out;
1265 if (!strbuf_strip_suffix(&path, "/objects"))
def06971 1266 goto out;
5e8c968c
JK
1267 base_len = path.len;
1268
36cfda15 1269 /* Is this a git repository with refs? */
5e8c968c
JK
1270 strbuf_addstr(&path, "/refs");
1271 if (!is_directory(path.buf))
def06971 1272 goto out;
5e8c968c
JK
1273 strbuf_setlen(&path, base_len);
1274
a10a1787
JK
1275 read_alternate_refs(path.buf, cb->fn, cb->data);
1276
def06971 1277out:
5e8c968c 1278 strbuf_release(&path);
36cfda15
JH
1279 return 0;
1280}
114a6a88
JK
1281
1282void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1283{
1284 struct alternate_refs_data cb;
1285 cb.fn = fn;
1286 cb.data = data;
1287 foreach_alt_odb(refs_from_alternate_cb, &cb);
1288}