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