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