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