1#include "cache.h"
2#include "commit.h"
3#include "refs.h"
4#include "pkt-line.h"
5#include "run-command.h"
6#include "remote.h"
7#include "send-pack.h"
8
9static const char send_pack_usage[] =
10"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
11" --all and explicit <ref> specification are mutually exclusive.";
12
13static struct send_pack_args args;
14
15static int feed_object(const unsigned char *sha1, int fd, int negative)
16{
17 char buf[42];
18
19 if (negative && !has_sha1_file(sha1))
20 return 1;
21
22 memcpy(buf + negative, sha1_to_hex(sha1), 40);
23 if (negative)
24 buf[0] = '^';
25 buf[40 + negative] = '\n';
26 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
27}
28
29/*
30 * Make a pack stream and spit it out into file descriptor fd
31 */
32static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
33{
34 /*
35 * The child becomes pack-objects --revs; we feed
36 * the revision parameters to it via its stdin and
37 * let its stdout go back to the other end.
38 */
39 const char *argv[] = {
40 "pack-objects",
41 "--all-progress",
42 "--revs",
43 "--stdout",
44 NULL,
45 NULL,
46 };
47 struct child_process po;
48 int i;
49
50 if (args->use_thin_pack)
51 argv[4] = "--thin";
52 memset(&po, 0, sizeof(po));
53 po.argv = argv;
54 po.in = -1;
55 po.out = fd;
56 po.git_cmd = 1;
57 if (start_command(&po))
58 die("git pack-objects failed (%s)", strerror(errno));
59
60 /*
61 * We feed the pack-objects we just spawned with revision
62 * parameters by writing to the pipe.
63 */
64 for (i = 0; i < extra->nr; i++)
65 if (!feed_object(extra->array[i], po.in, 1))
66 break;
67
68 while (refs) {
69 if (!is_null_sha1(refs->old_sha1) &&
70 !feed_object(refs->old_sha1, po.in, 1))
71 break;
72 if (!is_null_sha1(refs->new_sha1) &&
73 !feed_object(refs->new_sha1, po.in, 0))
74 break;
75 refs = refs->next;
76 }
77
78 close(po.in);
79 if (finish_command(&po))
80 return error("pack-objects died with strange error");
81 return 0;
82}
83
84static int receive_status(int in, struct ref *refs)
85{
86 struct ref *hint;
87 char line[1000];
88 int ret = 0;
89 int len = packet_read_line(in, line, sizeof(line));
90 if (len < 10 || memcmp(line, "unpack ", 7))
91 return error("did not receive remote status");
92 if (memcmp(line, "unpack ok\n", 10)) {
93 char *p = line + strlen(line) - 1;
94 if (*p == '\n')
95 *p = '\0';
96 error("unpack failed: %s", line + 7);
97 ret = -1;
98 }
99 hint = NULL;
100 while (1) {
101 char *refname;
102 char *msg;
103 len = packet_read_line(in, line, sizeof(line));
104 if (!len)
105 break;
106 if (len < 3 ||
107 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
108 fprintf(stderr, "protocol error: %s\n", line);
109 ret = -1;
110 break;
111 }
112
113 line[strlen(line)-1] = '\0';
114 refname = line + 3;
115 msg = strchr(refname, ' ');
116 if (msg)
117 *msg++ = '\0';
118
119 /* first try searching at our hint, falling back to all refs */
120 if (hint)
121 hint = find_ref_by_name(hint, refname);
122 if (!hint)
123 hint = find_ref_by_name(refs, refname);
124 if (!hint) {
125 warning("remote reported status on unknown ref: %s",
126 refname);
127 continue;
128 }
129 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
130 warning("remote reported status on unexpected ref: %s",
131 refname);
132 continue;
133 }
134
135 if (line[0] == 'o' && line[1] == 'k')
136 hint->status = REF_STATUS_OK;
137 else {
138 hint->status = REF_STATUS_REMOTE_REJECT;
139 ret = -1;
140 }
141 if (msg)
142 hint->remote_status = xstrdup(msg);
143 /* start our next search from the next ref */
144 hint = hint->next;
145 }
146 return ret;
147}
148
149static void update_tracking_ref(struct remote *remote, struct ref *ref)
150{
151 struct refspec rs;
152
153 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
154 return;
155
156 rs.src = ref->name;
157 rs.dst = NULL;
158
159 if (!remote_find_tracking(remote, &rs)) {
160 if (args.verbose)
161 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
162 if (ref->deletion) {
163 delete_ref(rs.dst, NULL, 0);
164 } else
165 update_ref("update by push", rs.dst,
166 ref->new_sha1, NULL, 0, 0);
167 free(rs.dst);
168 }
169}
170
171#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
172
173static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
174{
175 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
176 if (from)
177 fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to));
178 else
179 fputs(prettify_ref(to), stderr);
180 if (msg) {
181 fputs(" (", stderr);
182 fputs(msg, stderr);
183 fputc(')', stderr);
184 }
185 fputc('\n', stderr);
186}
187
188static const char *status_abbrev(unsigned char sha1[20])
189{
190 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
191}
192
193static void print_ok_ref_status(struct ref *ref)
194{
195 if (ref->deletion)
196 print_ref_status('-', "[deleted]", ref, NULL, NULL);
197 else if (is_null_sha1(ref->old_sha1))
198 print_ref_status('*',
199 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
200 "[new branch]"),
201 ref, ref->peer_ref, NULL);
202 else {
203 char quickref[84];
204 char type;
205 const char *msg;
206
207 strcpy(quickref, status_abbrev(ref->old_sha1));
208 if (ref->nonfastforward) {
209 strcat(quickref, "...");
210 type = '+';
211 msg = "forced update";
212 } else {
213 strcat(quickref, "..");
214 type = ' ';
215 msg = NULL;
216 }
217 strcat(quickref, status_abbrev(ref->new_sha1));
218
219 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
220 }
221}
222
223static int print_one_push_status(struct ref *ref, const char *dest, int count)
224{
225 if (!count)
226 fprintf(stderr, "To %s\n", dest);
227
228 switch(ref->status) {
229 case REF_STATUS_NONE:
230 print_ref_status('X', "[no match]", ref, NULL, NULL);
231 break;
232 case REF_STATUS_REJECT_NODELETE:
233 print_ref_status('!', "[rejected]", ref, NULL,
234 "remote does not support deleting refs");
235 break;
236 case REF_STATUS_UPTODATE:
237 print_ref_status('=', "[up to date]", ref,
238 ref->peer_ref, NULL);
239 break;
240 case REF_STATUS_REJECT_NONFASTFORWARD:
241 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
242 "non-fast forward");
243 break;
244 case REF_STATUS_REMOTE_REJECT:
245 print_ref_status('!', "[remote rejected]", ref,
246 ref->deletion ? NULL : ref->peer_ref,
247 ref->remote_status);
248 break;
249 case REF_STATUS_EXPECTING_REPORT:
250 print_ref_status('!', "[remote failure]", ref,
251 ref->deletion ? NULL : ref->peer_ref,
252 "remote failed to report status");
253 break;
254 case REF_STATUS_OK:
255 print_ok_ref_status(ref);
256 break;
257 }
258
259 return 1;
260}
261
262static void print_push_status(const char *dest, struct ref *refs)
263{
264 struct ref *ref;
265 int n = 0;
266
267 if (args.verbose) {
268 for (ref = refs; ref; ref = ref->next)
269 if (ref->status == REF_STATUS_UPTODATE)
270 n += print_one_push_status(ref, dest, n);
271 }
272
273 for (ref = refs; ref; ref = ref->next)
274 if (ref->status == REF_STATUS_OK)
275 n += print_one_push_status(ref, dest, n);
276
277 for (ref = refs; ref; ref = ref->next) {
278 if (ref->status != REF_STATUS_NONE &&
279 ref->status != REF_STATUS_UPTODATE &&
280 ref->status != REF_STATUS_OK)
281 n += print_one_push_status(ref, dest, n);
282 }
283}
284
285static int refs_pushed(struct ref *ref)
286{
287 for (; ref; ref = ref->next) {
288 switch(ref->status) {
289 case REF_STATUS_NONE:
290 case REF_STATUS_UPTODATE:
291 break;
292 default:
293 return 1;
294 }
295 }
296 return 0;
297}
298
299int send_pack(struct send_pack_args *args,
300 int fd[], struct child_process *conn,
301 struct ref *remote_refs,
302 struct extra_have_objects *extra_have)
303{
304 int in = fd[0];
305 int out = fd[1];
306 struct ref *ref;
307 int new_refs;
308 int ask_for_status_report = 0;
309 int allow_deleting_refs = 0;
310 int expect_status_report = 0;
311 int ret;
312
313 /* Does the other end support the reporting? */
314 if (server_supports("report-status"))
315 ask_for_status_report = 1;
316 if (server_supports("delete-refs"))
317 allow_deleting_refs = 1;
318
319 if (!remote_refs) {
320 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
321 "Perhaps you should specify a branch such as 'master'.\n");
322 return 0;
323 }
324
325 /*
326 * Finally, tell the other end!
327 */
328 new_refs = 0;
329 for (ref = remote_refs; ref; ref = ref->next) {
330
331 if (ref->peer_ref)
332 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
333 else if (!args->send_mirror)
334 continue;
335
336 ref->deletion = is_null_sha1(ref->new_sha1);
337 if (ref->deletion && !allow_deleting_refs) {
338 ref->status = REF_STATUS_REJECT_NODELETE;
339 continue;
340 }
341 if (!ref->deletion &&
342 !hashcmp(ref->old_sha1, ref->new_sha1)) {
343 ref->status = REF_STATUS_UPTODATE;
344 continue;
345 }
346
347 /* This part determines what can overwrite what.
348 * The rules are:
349 *
350 * (0) you can always use --force or +A:B notation to
351 * selectively force individual ref pairs.
352 *
353 * (1) if the old thing does not exist, it is OK.
354 *
355 * (2) if you do not have the old thing, you are not allowed
356 * to overwrite it; you would not know what you are losing
357 * otherwise.
358 *
359 * (3) if both new and old are commit-ish, and new is a
360 * descendant of old, it is OK.
361 *
362 * (4) regardless of all of the above, removing :B is
363 * always allowed.
364 */
365
366 ref->nonfastforward =
367 !ref->deletion &&
368 !is_null_sha1(ref->old_sha1) &&
369 (!has_sha1_file(ref->old_sha1)
370 || !ref_newer(ref->new_sha1, ref->old_sha1));
371
372 if (ref->nonfastforward && !ref->force && !args->force_update) {
373 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
374 continue;
375 }
376
377 if (!ref->deletion)
378 new_refs++;
379
380 if (!args->dry_run) {
381 char *old_hex = sha1_to_hex(ref->old_sha1);
382 char *new_hex = sha1_to_hex(ref->new_sha1);
383
384 if (ask_for_status_report) {
385 packet_write(out, "%s %s %s%c%s",
386 old_hex, new_hex, ref->name, 0,
387 "report-status");
388 ask_for_status_report = 0;
389 expect_status_report = 1;
390 }
391 else
392 packet_write(out, "%s %s %s",
393 old_hex, new_hex, ref->name);
394 }
395 ref->status = expect_status_report ?
396 REF_STATUS_EXPECTING_REPORT :
397 REF_STATUS_OK;
398 }
399
400 packet_flush(out);
401 if (new_refs && !args->dry_run) {
402 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
403 for (ref = remote_refs; ref; ref = ref->next)
404 ref->status = REF_STATUS_NONE;
405 return -1;
406 }
407 }
408
409 if (expect_status_report)
410 ret = receive_status(in, remote_refs);
411 else
412 ret = 0;
413
414 if (ret < 0)
415 return ret;
416 for (ref = remote_refs; ref; ref = ref->next) {
417 switch (ref->status) {
418 case REF_STATUS_NONE:
419 case REF_STATUS_UPTODATE:
420 case REF_STATUS_OK:
421 break;
422 default:
423 return -1;
424 }
425 }
426 return 0;
427}
428
429static void verify_remote_names(int nr_heads, const char **heads)
430{
431 int i;
432
433 for (i = 0; i < nr_heads; i++) {
434 const char *local = heads[i];
435 const char *remote = strrchr(heads[i], ':');
436
437 if (*local == '+')
438 local++;
439
440 /* A matching refspec is okay. */
441 if (remote == local && remote[1] == '\0')
442 continue;
443
444 remote = remote ? (remote + 1) : local;
445 switch (check_ref_format(remote)) {
446 case 0: /* ok */
447 case CHECK_REF_FORMAT_ONELEVEL:
448 /* ok but a single level -- that is fine for
449 * a match pattern.
450 */
451 case CHECK_REF_FORMAT_WILDCARD:
452 /* ok but ends with a pattern-match character */
453 continue;
454 }
455 die("remote part of refspec is not a valid name in %s",
456 heads[i]);
457 }
458}
459
460int cmd_send_pack(int argc, const char **argv, const char *prefix)
461{
462 int i, nr_refspecs = 0;
463 const char **refspecs = NULL;
464 const char *remote_name = NULL;
465 struct remote *remote = NULL;
466 const char *dest = NULL;
467 int fd[2];
468 struct child_process *conn;
469 struct extra_have_objects extra_have;
470 struct ref *remote_refs, **remote_tail, *local_refs;
471 int ret;
472 int send_all = 0;
473 const char *receivepack = "git-receive-pack";
474 int flags;
475
476 argv++;
477 for (i = 1; i < argc; i++, argv++) {
478 const char *arg = *argv;
479
480 if (*arg == '-') {
481 if (!prefixcmp(arg, "--receive-pack=")) {
482 receivepack = arg + 15;
483 continue;
484 }
485 if (!prefixcmp(arg, "--exec=")) {
486 receivepack = arg + 7;
487 continue;
488 }
489 if (!prefixcmp(arg, "--remote=")) {
490 remote_name = arg + 9;
491 continue;
492 }
493 if (!strcmp(arg, "--all")) {
494 send_all = 1;
495 continue;
496 }
497 if (!strcmp(arg, "--dry-run")) {
498 args.dry_run = 1;
499 continue;
500 }
501 if (!strcmp(arg, "--mirror")) {
502 args.send_mirror = 1;
503 continue;
504 }
505 if (!strcmp(arg, "--force")) {
506 args.force_update = 1;
507 continue;
508 }
509 if (!strcmp(arg, "--verbose")) {
510 args.verbose = 1;
511 continue;
512 }
513 if (!strcmp(arg, "--thin")) {
514 args.use_thin_pack = 1;
515 continue;
516 }
517 usage(send_pack_usage);
518 }
519 if (!dest) {
520 dest = arg;
521 continue;
522 }
523 refspecs = (const char **) argv;
524 nr_refspecs = argc - i;
525 break;
526 }
527 if (!dest)
528 usage(send_pack_usage);
529 /*
530 * --all and --mirror are incompatible; neither makes sense
531 * with any refspecs.
532 */
533 if ((refspecs && (send_all || args.send_mirror)) ||
534 (send_all && args.send_mirror))
535 usage(send_pack_usage);
536
537 if (remote_name) {
538 remote = remote_get(remote_name);
539 if (!remote_has_url(remote, dest)) {
540 die("Destination %s is not a uri for %s",
541 dest, remote_name);
542 }
543 }
544
545 conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);
546
547 memset(&extra_have, 0, sizeof(extra_have));
548
549 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
550 &extra_have);
551
552 verify_remote_names(nr_refspecs, refspecs);
553
554 local_refs = get_local_heads();
555
556 flags = MATCH_REFS_NONE;
557
558 if (send_all)
559 flags |= MATCH_REFS_ALL;
560 if (args.send_mirror)
561 flags |= MATCH_REFS_MIRROR;
562
563 /* match them up */
564 remote_tail = &remote_refs;
565 while (*remote_tail)
566 remote_tail = &((*remote_tail)->next);
567 if (match_refs(local_refs, remote_refs, &remote_tail,
568 nr_refspecs, refspecs, flags)) {
569 return -1;
570 }
571
572 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
573
574 close(fd[1]);
575 close(fd[0]);
576
577 ret |= finish_connect(conn);
578
579 print_push_status(dest, remote_refs);
580
581 if (!args.dry_run && remote) {
582 struct ref *ref;
583 for (ref = remote_refs; ref; ref = ref->next)
584 update_tracking_ref(remote, ref);
585 }
586
587 if (!ret && !refs_pushed(remote_refs))
588 fprintf(stderr, "Everything up-to-date\n");
589
590 return ret;
591}