add 'git credential' plumbing command
[git/git.git] / builtin / send-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
2a9c3fe8 2#include "commit.h"
584c6cc9 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
de1a2fdd 5#include "sideband.h"
38b1c662 6#include "run-command.h"
6b62816c 7#include "remote.h"
96249c04 8#include "send-pack.h"
de1a2fdd 9#include "quote.h"
f1863d0d 10#include "transport.h"
61221472 11
2a245013 12static const char send_pack_usage[] =
1b1dd23f 13"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
18bd8821 14" --all and explicit <ref> specification are mutually exclusive.";
96249c04 15
6828f72f 16static struct send_pack_args args;
61221472 17
02322e16
JH
18static int feed_object(const unsigned char *sha1, int fd, int negative)
19{
20 char buf[42];
21
22 if (negative && !has_sha1_file(sha1))
23 return 1;
24
25 memcpy(buf + negative, sha1_to_hex(sha1), 40);
26 if (negative)
27 buf[0] = '^';
28 buf[40 + negative] = '\n';
29 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
30}
31
c727fe2a 32/*
0ae5f98c 33 * Make a pack stream and spit it out into file descriptor fd
c727fe2a 34 */
64fcef2d 35static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
c727fe2a 36{
38b1c662
SP
37 /*
38 * The child becomes pack-objects --revs; we feed
39 * the revision parameters to it via its stdin and
40 * let its stdout go back to the other end.
41 */
96249c04 42 const char *argv[] = {
38b1c662 43 "pack-objects",
4f366275 44 "--all-progress-implied",
38b1c662
SP
45 "--revs",
46 "--stdout",
47 NULL,
48 NULL,
b74fce16 49 NULL,
12070329 50 NULL,
d7c411b7 51 NULL,
38b1c662
SP
52 };
53 struct child_process po;
40c155ff 54 int i;
c727fe2a 55
b74fce16 56 i = 4;
64fcef2d 57 if (args->use_thin_pack)
b74fce16
NP
58 argv[i++] = "--thin";
59 if (args->use_ofs_delta)
60 argv[i++] = "--delta-base-offset";
01fdc21f 61 if (args->quiet || !args->progress)
12070329 62 argv[i++] = "-q";
d7c411b7
JK
63 if (args->progress)
64 argv[i++] = "--progress";
38b1c662 65 memset(&po, 0, sizeof(po));
96249c04 66 po.argv = argv;
38b1c662 67 po.in = -1;
de1a2fdd 68 po.out = args->stateless_rpc ? -1 : fd;
38b1c662
SP
69 po.git_cmd = 1;
70 if (start_command(&po))
d824cbba 71 die_errno("git pack-objects failed");
c727fe2a 72
0ae5f98c
JH
73 /*
74 * We feed the pack-objects we just spawned with revision
75 * parameters by writing to the pipe.
c727fe2a 76 */
02322e16
JH
77 for (i = 0; i < extra->nr; i++)
78 if (!feed_object(extra->array[i], po.in, 1))
40c155ff 79 break;
c727fe2a 80
40c155ff 81 while (refs) {
c727fe2a 82 if (!is_null_sha1(refs->old_sha1) &&
02322e16
JH
83 !feed_object(refs->old_sha1, po.in, 1))
84 break;
85 if (!is_null_sha1(refs->new_sha1) &&
86 !feed_object(refs->new_sha1, po.in, 0))
87 break;
c727fe2a
AW
88 refs = refs->next;
89 }
c727fe2a 90
e72ae288 91 close(po.in);
de1a2fdd
SP
92
93 if (args->stateless_rpc) {
94 char *buf = xmalloc(LARGE_PACKET_MAX);
95 while (1) {
96 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
97 if (n <= 0)
98 break;
99 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
100 }
101 free(buf);
102 close(po.out);
103 po.out = -1;
104 }
105
38b1c662 106 if (finish_command(&po))
64f003ab 107 return -1;
38b1c662 108 return 0;
94fdb7aa 109}
e4b5c7ff 110
ca74c458
JK
111static int receive_status(int in, struct ref *refs)
112{
113 struct ref *hint;
cfee10a7
JH
114 char line[1000];
115 int ret = 0;
116 int len = packet_read_line(in, line, sizeof(line));
2a0fe89a
JK
117 if (len < 10 || memcmp(line, "unpack ", 7))
118 return error("did not receive remote status");
cfee10a7 119 if (memcmp(line, "unpack ok\n", 10)) {
2a0fe89a
JK
120 char *p = line + strlen(line) - 1;
121 if (*p == '\n')
122 *p = '\0';
123 error("unpack failed: %s", line + 7);
cfee10a7
JH
124 ret = -1;
125 }
ca74c458 126 hint = NULL;
cfee10a7 127 while (1) {
2a0fe89a
JK
128 char *refname;
129 char *msg;
cfee10a7
JH
130 len = packet_read_line(in, line, sizeof(line));
131 if (!len)
132 break;
133 if (len < 3 ||
2a0fe89a 134 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
cfee10a7
JH
135 fprintf(stderr, "protocol error: %s\n", line);
136 ret = -1;
137 break;
138 }
2a0fe89a
JK
139
140 line[strlen(line)-1] = '\0';
141 refname = line + 3;
142 msg = strchr(refname, ' ');
143 if (msg)
144 *msg++ = '\0';
145
146 /* first try searching at our hint, falling back to all refs */
ca74c458 147 if (hint)
2a0fe89a 148 hint = find_ref_by_name(hint, refname);
ca74c458 149 if (!hint)
2a0fe89a
JK
150 hint = find_ref_by_name(refs, refname);
151 if (!hint) {
152 warning("remote reported status on unknown ref: %s",
153 refname);
154 continue;
155 }
156 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
157 warning("remote reported status on unexpected ref: %s",
158 refname);
159 continue;
160 }
161
162 if (line[0] == 'o' && line[1] == 'k')
163 hint->status = REF_STATUS_OK;
164 else {
165 hint->status = REF_STATUS_REMOTE_REJECT;
166 ret = -1;
167 }
168 if (msg)
169 hint->remote_status = xstrdup(msg);
170 /* start our next search from the next ref */
171 hint = hint->next;
cfee10a7
JH
172 }
173 return ret;
174}
175
de1a2fdd
SP
176static void print_helper_status(struct ref *ref)
177{
178 struct strbuf buf = STRBUF_INIT;
179
180 for (; ref; ref = ref->next) {
181 const char *msg = NULL;
182 const char *res;
183
184 switch(ref->status) {
185 case REF_STATUS_NONE:
186 res = "error";
187 msg = "no match";
188 break;
189
190 case REF_STATUS_OK:
191 res = "ok";
192 break;
193
194 case REF_STATUS_UPTODATE:
195 res = "ok";
196 msg = "up to date";
197 break;
198
199 case REF_STATUS_REJECT_NONFASTFORWARD:
200 res = "error";
201 msg = "non-fast forward";
202 break;
203
204 case REF_STATUS_REJECT_NODELETE:
205 case REF_STATUS_REMOTE_REJECT:
206 res = "error";
207 break;
208
209 case REF_STATUS_EXPECTING_REPORT:
210 default:
211 continue;
212 }
213
214 strbuf_reset(&buf);
215 strbuf_addf(&buf, "%s %s", res, ref->name);
216 if (ref->remote_status)
217 msg = ref->remote_status;
218 if (msg) {
219 strbuf_addch(&buf, ' ');
220 quote_two_c_style(&buf, "", msg, 0);
221 }
222 strbuf_addch(&buf, '\n');
223
224 safe_write(1, buf.buf, buf.len);
225 }
226 strbuf_release(&buf);
227}
228
0c499ea6
SP
229static int sideband_demux(int in, int out, void *data)
230{
61432146 231 int *fd = data, ret;
73776dc1 232#ifdef NO_PTHREADS
09c9957c
JS
233 close(fd[1]);
234#endif
61432146 235 ret = recv_sideband("send-pack", fd[0], out);
0c499ea6
SP
236 close(out);
237 return ret;
238}
239
64fcef2d
DB
240int send_pack(struct send_pack_args *args,
241 int fd[], struct child_process *conn,
242 struct ref *remote_refs,
243 struct extra_have_objects *extra_have)
61221472 244{
64fcef2d
DB
245 int in = fd[0];
246 int out = fd[1];
de1a2fdd 247 struct strbuf req_buf = STRBUF_INIT;
64fcef2d 248 struct ref *ref;
584c6cc9 249 int new_refs;
d4f694ba 250 int allow_deleting_refs = 0;
0c499ea6
SP
251 int status_report = 0;
252 int use_sideband = 0;
01fdc21f 253 int quiet_supported = 0;
0c499ea6 254 unsigned cmds_sent = 0;
ca74c458 255 int ret;
0c499ea6 256 struct async demux;
584c6cc9 257
cfee10a7
JH
258 /* Does the other end support the reporting? */
259 if (server_supports("report-status"))
0c499ea6 260 status_report = 1;
d4f694ba
JH
261 if (server_supports("delete-refs"))
262 allow_deleting_refs = 1;
b74fce16
NP
263 if (server_supports("ofs-delta"))
264 args->use_ofs_delta = 1;
0c499ea6
SP
265 if (server_supports("side-band-64k"))
266 use_sideband = 1;
01fdc21f
CB
267 if (server_supports("quiet"))
268 quiet_supported = 1;
cfee10a7 269
4c353e89 270 if (!remote_refs) {
7e23b06d
AC
271 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
272 "Perhaps you should specify a branch such as 'master'.\n");
4c353e89
DB
273 return 0;
274 }
275
584c6cc9 276 /*
f88395ac 277 * Finally, tell the other end!
584c6cc9 278 */
f88395ac
JH
279 new_refs = 0;
280 for (ref = remote_refs; ref; ref = ref->next) {
20e8b465 281 if (!ref->peer_ref && !args->send_mirror)
16ed2f48 282 continue;
28b9d6e5 283
20e8b465
TRC
284 /* Check for statuses set by set_ref_status_for_push() */
285 switch (ref->status) {
286 case REF_STATUS_REJECT_NONFASTFORWARD:
287 case REF_STATUS_UPTODATE:
37fde874 288 continue;
20e8b465
TRC
289 default:
290 ; /* do nothing */
37fde874
JH
291 }
292
20e8b465
TRC
293 if (ref->deletion && !allow_deleting_refs) {
294 ref->status = REF_STATUS_REJECT_NODELETE;
8736a848 295 continue;
e4b5c7ff 296 }
8736a848 297
8736a848 298 if (!ref->deletion)
d4f694ba 299 new_refs++;
cfee10a7 300
0c499ea6
SP
301 if (args->dry_run) {
302 ref->status = REF_STATUS_OK;
303 } else {
8736a848
JK
304 char *old_hex = sha1_to_hex(ref->old_sha1);
305 char *new_hex = sha1_to_hex(ref->new_sha1);
01fdc21f 306 int quiet = quiet_supported && (args->quiet || !args->progress);
8736a848 307
c207e34f
CB
308 if (!cmds_sent && (status_report || use_sideband || args->quiet)) {
309 packet_buf_write(&req_buf, "%s %s %s%c%s%s%s",
01fdc21f
CB
310 old_hex, new_hex, ref->name, 0,
311 status_report ? " report-status" : "",
312 use_sideband ? " side-band-64k" : "",
313 quiet ? " quiet" : "");
a63103ae
BE
314 }
315 else
de1a2fdd 316 packet_buf_write(&req_buf, "%s %s %s",
01fdc21f 317 old_hex, new_hex, ref->name);
0c499ea6
SP
318 ref->status = status_report ?
319 REF_STATUS_EXPECTING_REPORT :
320 REF_STATUS_OK;
321 cmds_sent++;
cfee10a7 322 }
61221472 323 }
f88395ac 324
de1a2fdd 325 if (args->stateless_rpc) {
0c499ea6 326 if (!args->dry_run && cmds_sent) {
de1a2fdd
SP
327 packet_buf_flush(&req_buf);
328 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
329 }
330 } else {
331 safe_write(out, req_buf.buf, req_buf.len);
332 packet_flush(out);
333 }
334 strbuf_release(&req_buf);
335
0c499ea6
SP
336 if (use_sideband && cmds_sent) {
337 memset(&demux, 0, sizeof(demux));
338 demux.proc = sideband_demux;
339 demux.data = fd;
340 demux.out = -1;
341 if (start_async(&demux))
e6bcd979 342 die("send-pack: unable to fork off sideband demultiplexer");
0c499ea6
SP
343 in = demux.out;
344 }
345
346 if (new_refs && cmds_sent) {
64fcef2d
DB
347 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
348 for (ref = remote_refs; ref; ref = ref->next)
349 ref->status = REF_STATUS_NONE;
e07fd15b
JK
350 if (args->stateless_rpc)
351 close(out);
a1a3fd1f
JK
352 if (git_connection_is_socket(conn))
353 shutdown(fd[0], SHUT_WR);
0c499ea6
SP
354 if (use_sideband)
355 finish_async(&demux);
8736a848 356 return -1;
64fcef2d 357 }
8736a848 358 }
0c499ea6 359 if (args->stateless_rpc && cmds_sent)
de1a2fdd 360 packet_flush(out);
cfee10a7 361
0c499ea6 362 if (status_report && cmds_sent)
ca74c458 363 ret = receive_status(in, remote_refs);
ca74c458
JK
364 else
365 ret = 0;
de1a2fdd
SP
366 if (args->stateless_rpc)
367 packet_flush(out);
ca74c458 368
0c499ea6
SP
369 if (use_sideband && cmds_sent) {
370 if (finish_async(&demux)) {
371 error("error in sideband demultiplexer");
372 ret = -1;
373 }
374 close(demux.out);
375 }
376
ca74c458
JK
377 if (ret < 0)
378 return ret;
77555854
LA
379
380 if (args->porcelain)
381 return 0;
382
8736a848
JK
383 for (ref = remote_refs; ref; ref = ref->next) {
384 switch (ref->status) {
385 case REF_STATUS_NONE:
386 case REF_STATUS_UPTODATE:
387 case REF_STATUS_OK:
388 break;
389 default:
390 return -1;
391 }
392 }
393 return 0;
61221472
LT
394}
395
96249c04 396int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 397{
64fcef2d
DB
398 int i, nr_refspecs = 0;
399 const char **refspecs = NULL;
96249c04 400 const char *remote_name = NULL;
b516968f 401 struct remote *remote = NULL;
96249c04 402 const char *dest = NULL;
64fcef2d
DB
403 int fd[2];
404 struct child_process *conn;
405 struct extra_have_objects extra_have;
6d2bf96e 406 struct ref *remote_refs, *local_refs;
64fcef2d 407 int ret;
de1a2fdd 408 int helper_status = 0;
64fcef2d
DB
409 int send_all = 0;
410 const char *receivepack = "git-receive-pack";
411 int flags;
f1863d0d 412 int nonfastforward = 0;
391b1f20 413 int progress = -1;
84a9b58c 414
61221472 415 argv++;
d089391c 416 for (i = 1; i < argc; i++, argv++) {
96249c04 417 const char *arg = *argv;
61221472
LT
418
419 if (*arg == '-') {
cc44c765 420 if (!prefixcmp(arg, "--receive-pack=")) {
64fcef2d 421 receivepack = arg + 15;
d23842fd
UKK
422 continue;
423 }
cc44c765 424 if (!prefixcmp(arg, "--exec=")) {
64fcef2d 425 receivepack = arg + 7;
61221472
LT
426 continue;
427 }
b516968f
DB
428 if (!prefixcmp(arg, "--remote=")) {
429 remote_name = arg + 9;
430 continue;
431 }
d089391c 432 if (!strcmp(arg, "--all")) {
64fcef2d 433 send_all = 1;
d089391c
LT
434 continue;
435 }
a63103ae 436 if (!strcmp(arg, "--dry-run")) {
96249c04 437 args.dry_run = 1;
a63103ae
BE
438 continue;
439 }
28b9d6e5
AW
440 if (!strcmp(arg, "--mirror")) {
441 args.send_mirror = 1;
442 continue;
443 }
2a9c3fe8 444 if (!strcmp(arg, "--force")) {
96249c04 445 args.force_update = 1;
2a9c3fe8
LT
446 continue;
447 }
c207e34f
CB
448 if (!strcmp(arg, "--quiet")) {
449 args.quiet = 1;
450 continue;
451 }
41f93a2c 452 if (!strcmp(arg, "--verbose")) {
96249c04 453 args.verbose = 1;
41f93a2c
LT
454 continue;
455 }
391b1f20
JK
456 if (!strcmp(arg, "--progress")) {
457 progress = 1;
458 continue;
459 }
460 if (!strcmp(arg, "--no-progress")) {
461 progress = 0;
462 continue;
463 }
2245be3e 464 if (!strcmp(arg, "--thin")) {
96249c04 465 args.use_thin_pack = 1;
2245be3e
JH
466 continue;
467 }
de1a2fdd
SP
468 if (!strcmp(arg, "--stateless-rpc")) {
469 args.stateless_rpc = 1;
470 continue;
471 }
472 if (!strcmp(arg, "--helper-status")) {
473 helper_status = 1;
474 continue;
475 }
61221472
LT
476 usage(send_pack_usage);
477 }
d089391c
LT
478 if (!dest) {
479 dest = arg;
480 continue;
481 }
64fcef2d
DB
482 refspecs = (const char **) argv;
483 nr_refspecs = argc - i;
61221472
LT
484 break;
485 }
486 if (!dest)
487 usage(send_pack_usage);
28b9d6e5
AW
488 /*
489 * --all and --mirror are incompatible; neither makes sense
490 * with any refspecs.
491 */
64fcef2d
DB
492 if ((refspecs && (send_all || args.send_mirror)) ||
493 (send_all && args.send_mirror))
0bc3cdfc 494 usage(send_pack_usage);
37adac76 495
b516968f
DB
496 if (remote_name) {
497 remote = remote_get(remote_name);
28b91f8a 498 if (!remote_has_url(remote, dest)) {
b516968f
DB
499 die("Destination %s is not a uri for %s",
500 dest, remote_name);
501 }
502 }
503
391b1f20
JK
504 if (progress == -1)
505 progress = !args.quiet && isatty(2);
506 args.progress = progress;
8d32e60d 507
de1a2fdd
SP
508 if (args.stateless_rpc) {
509 conn = NULL;
510 fd[0] = 0;
511 fd[1] = 1;
512 } else {
5a277f3f 513 conn = git_connect(fd, dest, receivepack,
de1a2fdd
SP
514 args.verbose ? CONNECT_VERBOSE : 0);
515 }
96249c04 516
64fcef2d
DB
517 memset(&extra_have, 0, sizeof(extra_have));
518
afe7c5ff 519 get_remote_heads(fd[0], &remote_refs, REF_NORMAL, &extra_have);
64fcef2d 520
f1863d0d 521 transport_verify_remote_names(nr_refspecs, refspecs);
64fcef2d
DB
522
523 local_refs = get_local_heads();
96249c04 524
64fcef2d
DB
525 flags = MATCH_REFS_NONE;
526
527 if (send_all)
528 flags |= MATCH_REFS_ALL;
529 if (args.send_mirror)
530 flags |= MATCH_REFS_MIRROR;
531
532 /* match them up */
29753cdd 533 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
64fcef2d 534 return -1;
96249c04 535
20e8b465
TRC
536 set_ref_status_for_push(remote_refs, args.send_mirror,
537 args.force_update);
538
64fcef2d 539 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 540
de1a2fdd
SP
541 if (helper_status)
542 print_helper_status(remote_refs);
543
64fcef2d 544 close(fd[1]);
7f8e9828 545 close(fd[0]);
64fcef2d 546
98158e9c 547 ret |= finish_connect(conn);
64fcef2d 548
de1a2fdd 549 if (!helper_status)
f1863d0d 550 transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
64fcef2d
DB
551
552 if (!args.dry_run && remote) {
553 struct ref *ref;
554 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 555 transport_update_tracking_ref(remote, ref, args.verbose);
64fcef2d
DB
556 }
557
f1863d0d 558 if (!ret && !transport_refs_pushed(remote_refs))
64fcef2d
DB
559 fprintf(stderr, "Everything up-to-date\n");
560
561 return ret;
61221472 562}