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