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