]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-send-pack.c
GIT 1.6.3
[thirdparty/git.git] / builtin-send-pack.c
CommitLineData
61221472 1#include "cache.h"
2a9c3fe8 2#include "commit.h"
584c6cc9 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
38b1c662 5#include "run-command.h"
6b62816c 6#include "remote.h"
96249c04 7#include "send-pack.h"
61221472 8
2a245013 9static const char send_pack_usage[] =
1b1dd23f 10"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
18bd8821 11" --all and explicit <ref> specification are mutually exclusive.";
96249c04 12
6828f72f 13static struct send_pack_args args;
61221472 14
02322e16
JH
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
c727fe2a 29/*
0ae5f98c 30 * Make a pack stream and spit it out into file descriptor fd
c727fe2a 31 */
64fcef2d 32static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
c727fe2a 33{
38b1c662
SP
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 */
96249c04 39 const char *argv[] = {
38b1c662
SP
40 "pack-objects",
41 "--all-progress",
42 "--revs",
43 "--stdout",
44 NULL,
45 NULL,
46 };
47 struct child_process po;
40c155ff 48 int i;
c727fe2a 49
64fcef2d 50 if (args->use_thin_pack)
96249c04 51 argv[4] = "--thin";
38b1c662 52 memset(&po, 0, sizeof(po));
96249c04 53 po.argv = argv;
38b1c662
SP
54 po.in = -1;
55 po.out = fd;
56 po.git_cmd = 1;
57 if (start_command(&po))
f18d244a 58 die("git pack-objects failed (%s)", strerror(errno));
c727fe2a 59
0ae5f98c
JH
60 /*
61 * We feed the pack-objects we just spawned with revision
62 * parameters by writing to the pipe.
c727fe2a 63 */
02322e16
JH
64 for (i = 0; i < extra->nr; i++)
65 if (!feed_object(extra->array[i], po.in, 1))
40c155ff 66 break;
c727fe2a 67
40c155ff 68 while (refs) {
c727fe2a 69 if (!is_null_sha1(refs->old_sha1) &&
02322e16
JH
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;
c727fe2a
AW
75 refs = refs->next;
76 }
c727fe2a 77
e72ae288 78 close(po.in);
38b1c662
SP
79 if (finish_command(&po))
80 return error("pack-objects died with strange error");
81 return 0;
94fdb7aa 82}
e4b5c7ff 83
ca74c458
JK
84static int receive_status(int in, struct ref *refs)
85{
86 struct ref *hint;
cfee10a7
JH
87 char line[1000];
88 int ret = 0;
89 int len = packet_read_line(in, line, sizeof(line));
2a0fe89a
JK
90 if (len < 10 || memcmp(line, "unpack ", 7))
91 return error("did not receive remote status");
cfee10a7 92 if (memcmp(line, "unpack ok\n", 10)) {
2a0fe89a
JK
93 char *p = line + strlen(line) - 1;
94 if (*p == '\n')
95 *p = '\0';
96 error("unpack failed: %s", line + 7);
cfee10a7
JH
97 ret = -1;
98 }
ca74c458 99 hint = NULL;
cfee10a7 100 while (1) {
2a0fe89a
JK
101 char *refname;
102 char *msg;
cfee10a7
JH
103 len = packet_read_line(in, line, sizeof(line));
104 if (!len)
105 break;
106 if (len < 3 ||
2a0fe89a 107 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
cfee10a7
JH
108 fprintf(stderr, "protocol error: %s\n", line);
109 ret = -1;
110 break;
111 }
2a0fe89a
JK
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 */
ca74c458 120 if (hint)
2a0fe89a 121 hint = find_ref_by_name(hint, refname);
ca74c458 122 if (!hint)
2a0fe89a
JK
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;
cfee10a7
JH
145 }
146 return ret;
147}
148
334f4831
JK
149static void update_tracking_ref(struct remote *remote, struct ref *ref)
150{
151 struct refspec rs;
334f4831 152
16ed2f48 153 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
334f4831
JK
154 return;
155
1f0e2a1a
JK
156 rs.src = ref->name;
157 rs.dst = NULL;
334f4831
JK
158
159 if (!remote_find_tracking(remote, &rs)) {
b50fa2bd
JK
160 if (args.verbose)
161 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
1f0e2a1a 162 if (ref->deletion) {
eca35a25 163 delete_ref(rs.dst, NULL, 0);
334f4831
JK
164 } else
165 update_ref("update by push", rs.dst,
166 ref->new_sha1, NULL, 0, 0);
167 free(rs.dst);
168 }
169}
170
f7673490
JK
171#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
172
8736a848
JK
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{
2efb3b06 190 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
8736a848
JK
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
07f50715
JK
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
8736a848
JK
262static void print_push_status(const char *dest, struct ref *refs)
263{
264 struct ref *ref;
07f50715 265 int n = 0;
8736a848 266
07f50715
JK
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 }
8736a848 272
07f50715
JK
273 for (ref = refs; ref; ref = ref->next)
274 if (ref->status == REF_STATUS_OK)
275 n += print_one_push_status(ref, dest, n);
8736a848 276
07f50715
JK
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);
8736a848
JK
282 }
283}
284
73fa0b44
JK
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
64fcef2d
DB
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)
61221472 303{
64fcef2d
DB
304 int in = fd[0];
305 int out = fd[1];
306 struct ref *ref;
584c6cc9 307 int new_refs;
cfee10a7 308 int ask_for_status_report = 0;
d4f694ba 309 int allow_deleting_refs = 0;
cfee10a7 310 int expect_status_report = 0;
ca74c458 311 int ret;
584c6cc9 312
cfee10a7
JH
313 /* Does the other end support the reporting? */
314 if (server_supports("report-status"))
315 ask_for_status_report = 1;
d4f694ba
JH
316 if (server_supports("delete-refs"))
317 allow_deleting_refs = 1;
cfee10a7 318
4c353e89 319 if (!remote_refs) {
7e23b06d
AC
320 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
321 "Perhaps you should specify a branch such as 'master'.\n");
4c353e89
DB
322 return 0;
323 }
324
584c6cc9 325 /*
f88395ac 326 * Finally, tell the other end!
584c6cc9 327 */
f88395ac
JH
328 new_refs = 0;
329 for (ref = remote_refs; ref; ref = ref->next) {
d4f694ba 330
16ed2f48
CB
331 if (ref->peer_ref)
332 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
64fcef2d 333 else if (!args->send_mirror)
16ed2f48 334 continue;
28b9d6e5 335
16ed2f48 336 ref->deletion = is_null_sha1(ref->new_sha1);
8736a848
JK
337 if (ref->deletion && !allow_deleting_refs) {
338 ref->status = REF_STATUS_REJECT_NODELETE;
d4f694ba
JH
339 continue;
340 }
8736a848 341 if (!ref->deletion &&
16ed2f48 342 !hashcmp(ref->old_sha1, ref->new_sha1)) {
8736a848 343 ref->status = REF_STATUS_UPTODATE;
37fde874
JH
344 continue;
345 }
346
347 /* This part determines what can overwrite what.
348 * The rules are:
349 *
ff27adf3
JH
350 * (0) you can always use --force or +A:B notation to
351 * selectively force individual ref pairs.
37fde874
JH
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.
d4f694ba
JH
361 *
362 * (4) regardless of all of the above, removing :B is
363 * always allowed.
37fde874
JH
364 */
365
8736a848
JK
366 ref->nonfastforward =
367 !ref->deletion &&
4b4ee90e 368 !is_null_sha1(ref->old_sha1) &&
8736a848 369 (!has_sha1_file(ref->old_sha1)
16ed2f48 370 || !ref_newer(ref->new_sha1, ref->old_sha1));
8736a848 371
64fcef2d 372 if (ref->nonfastforward && !ref->force && !args->force_update) {
8736a848
JK
373 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
374 continue;
e4b5c7ff 375 }
8736a848 376
8736a848 377 if (!ref->deletion)
d4f694ba 378 new_refs++;
cfee10a7 379
64fcef2d 380 if (!args->dry_run) {
8736a848
JK
381 char *old_hex = sha1_to_hex(ref->old_sha1);
382 char *new_hex = sha1_to_hex(ref->new_sha1);
383
a63103ae
BE
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);
cfee10a7 394 }
2a0fe89a
JK
395 ref->status = expect_status_report ?
396 REF_STATUS_EXPECTING_REPORT :
397 REF_STATUS_OK;
61221472 398 }
f88395ac 399
f3a3214e 400 packet_flush(out);
64fcef2d
DB
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;
8736a848 405 return -1;
64fcef2d 406 }
8736a848 407 }
cfee10a7 408
2a0fe89a 409 if (expect_status_report)
ca74c458 410 ret = receive_status(in, remote_refs);
ca74c458
JK
411 else
412 ret = 0;
413
ca74c458
JK
414 if (ret < 0)
415 return ret;
8736a848
JK
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;
61221472
LT
427}
428
4577370e 429static void verify_remote_names(int nr_heads, const char **heads)
37adac76
JH
430{
431 int i;
432
433 for (i = 0; i < nr_heads; i++) {
a83619d6 434 const char *local = heads[i];
46220ca1 435 const char *remote = strrchr(heads[i], ':');
37adac76 436
a83619d6
PB
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;
37adac76
JH
445 switch (check_ref_format(remote)) {
446 case 0: /* ok */
257f3020
JH
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 */
37adac76
JH
453 continue;
454 }
455 die("remote part of refspec is not a valid name in %s",
456 heads[i]);
457 }
458}
f88395ac 459
96249c04 460int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 461{
64fcef2d
DB
462 int i, nr_refspecs = 0;
463 const char **refspecs = NULL;
96249c04 464 const char *remote_name = NULL;
b516968f 465 struct remote *remote = NULL;
96249c04 466 const char *dest = NULL;
64fcef2d
DB
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;
84a9b58c 475
61221472 476 argv++;
d089391c 477 for (i = 1; i < argc; i++, argv++) {
96249c04 478 const char *arg = *argv;
61221472
LT
479
480 if (*arg == '-') {
cc44c765 481 if (!prefixcmp(arg, "--receive-pack=")) {
64fcef2d 482 receivepack = arg + 15;
d23842fd
UKK
483 continue;
484 }
cc44c765 485 if (!prefixcmp(arg, "--exec=")) {
64fcef2d 486 receivepack = arg + 7;
61221472
LT
487 continue;
488 }
b516968f
DB
489 if (!prefixcmp(arg, "--remote=")) {
490 remote_name = arg + 9;
491 continue;
492 }
d089391c 493 if (!strcmp(arg, "--all")) {
64fcef2d 494 send_all = 1;
d089391c
LT
495 continue;
496 }
a63103ae 497 if (!strcmp(arg, "--dry-run")) {
96249c04 498 args.dry_run = 1;
a63103ae
BE
499 continue;
500 }
28b9d6e5
AW
501 if (!strcmp(arg, "--mirror")) {
502 args.send_mirror = 1;
503 continue;
504 }
2a9c3fe8 505 if (!strcmp(arg, "--force")) {
96249c04 506 args.force_update = 1;
2a9c3fe8
LT
507 continue;
508 }
41f93a2c 509 if (!strcmp(arg, "--verbose")) {
96249c04 510 args.verbose = 1;
41f93a2c
LT
511 continue;
512 }
2245be3e 513 if (!strcmp(arg, "--thin")) {
96249c04 514 args.use_thin_pack = 1;
2245be3e
JH
515 continue;
516 }
61221472
LT
517 usage(send_pack_usage);
518 }
d089391c
LT
519 if (!dest) {
520 dest = arg;
521 continue;
522 }
64fcef2d
DB
523 refspecs = (const char **) argv;
524 nr_refspecs = argc - i;
61221472
LT
525 break;
526 }
527 if (!dest)
528 usage(send_pack_usage);
28b9d6e5
AW
529 /*
530 * --all and --mirror are incompatible; neither makes sense
531 * with any refspecs.
532 */
64fcef2d
DB
533 if ((refspecs && (send_all || args.send_mirror)) ||
534 (send_all && args.send_mirror))
0bc3cdfc 535 usage(send_pack_usage);
37adac76 536
b516968f
DB
537 if (remote_name) {
538 remote = remote_get(remote_name);
28b91f8a 539 if (!remote_has_url(remote, dest)) {
b516968f
DB
540 die("Destination %s is not a uri for %s",
541 dest, remote_name);
542 }
543 }
544
64fcef2d 545 conn = git_connect(fd, dest, receivepack, args.verbose ? CONNECT_VERBOSE : 0);
96249c04 546
64fcef2d
DB
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();
96249c04 555
64fcef2d
DB
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 }
96249c04 571
64fcef2d 572 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 573
64fcef2d 574 close(fd[1]);
7f8e9828 575 close(fd[0]);
64fcef2d 576
98158e9c 577 ret |= finish_connect(conn);
64fcef2d
DB
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;
61221472 591}