6 #include "run-command.h"
10 static const char send_pack_usage
[] =
11 "git-send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
12 " --all and explicit <ref> specification are mutually exclusive.";
14 static struct send_pack_args args
= {
15 /* .receivepack = */ "git-receive-pack",
18 static struct additional_base
{
19 unsigned char tip
[20];
20 unsigned char last
[20];
21 struct commit
*commit
;
23 static int unknown_nr
;
24 static int unknown_alloc
;
27 * Make a pack stream and spit it out into file descriptor fd
29 static int pack_objects(int fd
, struct ref
*refs
)
32 * The child becomes pack-objects --revs; we feed
33 * the revision parameters to it via its stdin and
34 * let its stdout go back to the other end.
36 const char *argv
[] = {
44 struct child_process po
;
46 if (args
.use_thin_pack
)
48 memset(&po
, 0, sizeof(po
));
53 if (start_command(&po
))
54 die("git-pack-objects failed (%s)", strerror(errno
));
57 * We feed the pack-objects we just spawned with revision
58 * parameters by writing to the pipe.
63 if (!is_null_sha1(refs
->old_sha1
) &&
64 has_sha1_file(refs
->old_sha1
)) {
65 memcpy(buf
+ 1, sha1_to_hex(refs
->old_sha1
), 40);
68 if (!write_or_whine(po
.in
, buf
, 42,
69 "send-pack: send refs"))
72 if (!is_null_sha1(refs
->new_sha1
)) {
73 memcpy(buf
, sha1_to_hex(refs
->new_sha1
), 40);
75 if (!write_or_whine(po
.in
, buf
, 41,
76 "send-pack: send refs"))
86 for (i
= 0; i
< unknown_nr
; i
++) {
87 struct commit
*c
= unknown
[i
].commit
;
90 memcpy(buf
+ 1, sha1_to_hex(c
->object
.sha1
), 40);
93 if (!write_or_whine(po
.in
, buf
, 42,
94 "send-pack: send additional base"))
100 if (finish_command(&po
))
101 return error("pack-objects died with strange error");
105 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
108 struct commit_list
*temp
= list
;
109 temp
->item
->object
.flags
&= ~mark
;
115 static int ref_newer(const unsigned char *new_sha1
,
116 const unsigned char *old_sha1
)
119 struct commit
*old
, *new;
120 struct commit_list
*list
, *used
;
123 /* Both new and old must be commit-ish and new is descendant of
124 * old. Otherwise we require --force.
126 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
127 if (!o
|| o
->type
!= OBJ_COMMIT
)
129 old
= (struct commit
*) o
;
131 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
132 if (!o
|| o
->type
!= OBJ_COMMIT
)
134 new = (struct commit
*) o
;
136 if (parse_commit(new) < 0)
140 commit_list_insert(new, &list
);
142 new = pop_most_recent_commit(&list
, 1);
143 commit_list_insert(new, &used
);
149 unmark_and_free(list
, 1);
150 unmark_and_free(used
, 1);
154 static struct ref
*local_refs
, **local_tail
;
155 static struct ref
*remote_refs
, **remote_tail
;
157 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
160 int len
= strlen(refname
) + 1;
161 ref
= xcalloc(1, sizeof(*ref
) + len
);
162 hashcpy(ref
->new_sha1
, sha1
);
163 memcpy(ref
->name
, refname
, len
);
165 local_tail
= &ref
->next
;
169 static void get_local_heads(void)
171 local_tail
= &local_refs
;
172 for_each_ref(one_local_ref
, NULL
);
175 static int receive_status(int in
, struct ref
*refs
)
180 int len
= packet_read_line(in
, line
, sizeof(line
));
181 if (len
< 10 || memcmp(line
, "unpack ", 7))
182 return error("did not receive remote status");
183 if (memcmp(line
, "unpack ok\n", 10)) {
184 char *p
= line
+ strlen(line
) - 1;
187 error("unpack failed: %s", line
+ 7);
194 len
= packet_read_line(in
, line
, sizeof(line
));
198 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
199 fprintf(stderr
, "protocol error: %s\n", line
);
204 line
[strlen(line
)-1] = '\0';
206 msg
= strchr(refname
, ' ');
210 /* first try searching at our hint, falling back to all refs */
212 hint
= find_ref_by_name(hint
, refname
);
214 hint
= find_ref_by_name(refs
, refname
);
216 warning("remote reported status on unknown ref: %s",
220 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
221 warning("remote reported status on unexpected ref: %s",
226 if (line
[0] == 'o' && line
[1] == 'k')
227 hint
->status
= REF_STATUS_OK
;
229 hint
->status
= REF_STATUS_REMOTE_REJECT
;
233 hint
->remote_status
= xstrdup(msg
);
234 /* start our next search from the next ref */
240 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
244 if (ref
->status
!= REF_STATUS_OK
)
250 if (!remote_find_tracking(remote
, &rs
)) {
252 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
254 if (delete_ref(rs
.dst
, NULL
))
255 error("Failed to delete");
257 update_ref("update by push", rs
.dst
,
258 ref
->new_sha1
, NULL
, 0, 0);
263 static const char *prettify_ref(const struct ref
*ref
)
265 const char *name
= ref
->name
;
267 !prefixcmp(name
, "refs/heads/") ? 11 :
268 !prefixcmp(name
, "refs/tags/") ? 10 :
269 !prefixcmp(name
, "refs/remotes/") ? 13 :
273 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
275 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
277 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
279 fprintf(stderr
, "%s -> %s", prettify_ref(from
), prettify_ref(to
));
281 fputs(prettify_ref(to
), stderr
);
290 static const char *status_abbrev(unsigned char sha1
[20])
292 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
295 static void print_ok_ref_status(struct ref
*ref
)
298 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
299 else if (is_null_sha1(ref
->old_sha1
))
300 print_ref_status('*',
301 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
303 ref
, ref
->peer_ref
, NULL
);
309 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
310 if (ref
->nonfastforward
) {
311 strcat(quickref
, "...");
313 msg
= "forced update";
315 strcat(quickref
, "..");
319 strcat(quickref
, status_abbrev(ref
->new_sha1
));
321 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
325 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
328 fprintf(stderr
, "To %s\n", dest
);
330 switch(ref
->status
) {
331 case REF_STATUS_NONE
:
332 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
334 case REF_STATUS_REJECT_NODELETE
:
335 print_ref_status('!', "[rejected]", ref
, NULL
,
336 "remote does not support deleting refs");
338 case REF_STATUS_UPTODATE
:
339 print_ref_status('=', "[up to date]", ref
,
340 ref
->peer_ref
, NULL
);
342 case REF_STATUS_REJECT_NONFASTFORWARD
:
343 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
346 case REF_STATUS_REMOTE_REJECT
:
347 print_ref_status('!', "[remote rejected]", ref
,
348 ref
->deletion
? NULL
: ref
->peer_ref
,
351 case REF_STATUS_EXPECTING_REPORT
:
352 print_ref_status('!', "[remote failure]", ref
,
353 ref
->deletion
? NULL
: ref
->peer_ref
,
354 "remote failed to report status");
357 print_ok_ref_status(ref
);
364 static void print_push_status(const char *dest
, struct ref
*refs
)
370 for (ref
= refs
; ref
; ref
= ref
->next
)
371 if (ref
->status
== REF_STATUS_UPTODATE
)
372 n
+= print_one_push_status(ref
, dest
, n
);
375 for (ref
= refs
; ref
; ref
= ref
->next
)
376 if (ref
->status
== REF_STATUS_OK
)
377 n
+= print_one_push_status(ref
, dest
, n
);
379 for (ref
= refs
; ref
; ref
= ref
->next
) {
380 if (ref
->status
!= REF_STATUS_NONE
&&
381 ref
->status
!= REF_STATUS_UPTODATE
&&
382 ref
->status
!= REF_STATUS_OK
)
383 n
+= print_one_push_status(ref
, dest
, n
);
387 static int refs_pushed(struct ref
*ref
)
389 for (; ref
; ref
= ref
->next
) {
390 switch(ref
->status
) {
391 case REF_STATUS_NONE
:
392 case REF_STATUS_UPTODATE
:
401 static void ask_for_more(int in
, int out
, struct ref
*remote_refs
)
406 for (refs
= remote_refs
; refs
; refs
= refs
->next
) {
407 struct commit
*commit
;
409 if (is_null_sha1(refs
->old_sha1
))
411 commit
= lookup_commit_reference_gently(refs
->old_sha1
, 1);
413 ALLOC_GROW(unknown
, unknown_nr
+1, unknown_alloc
);
414 hashcpy(unknown
[unknown_nr
].last
, refs
->old_sha1
);
415 unknown
[unknown_nr
].commit
= NULL
;
424 for (i
= 0; i
< 5; i
++) {
426 for (j
= 0; j
< unknown_nr
; j
++) {
427 if (unknown
[j
].commit
)
429 hashcpy(unknown
[j
].tip
, unknown
[j
].last
);
430 packet_write(out
, "tellme-more %s\n",
431 sha1_to_hex(unknown
[j
].tip
));
440 unsigned char tip
[20];
441 unsigned char last
[20];
443 struct commit
*commit
;
445 len
= packet_read_line(in
, line
, sizeof(line
));
449 if (line
[len
-1] == '\n')
453 get_sha1_hex(line
, last
) ||
454 get_sha1_hex(line
+ 41, tip
))
455 die("protocol error: got '%s'", line
);
456 for (j
= 0; j
< unknown_nr
; j
++) {
457 if (unknown
[j
].commit
||
458 hashcmp(unknown
[j
].tip
, tip
))
460 hashcpy(unknown
[j
].last
, last
);
461 commit
= lookup_commit_reference_gently(last
, 1);
464 unknown
[j
].commit
= commit
;
468 /* they do not have any more to tell us */
474 static int do_send_pack(int in
, int out
, struct remote
*remote
, const char *dest
, int nr_refspec
, const char **refspec
)
478 int ask_for_status_report
= 0;
479 int allow_deleting_refs
= 0;
480 int expect_status_report
= 0;
481 int can_ask_for_more
= 0;
482 int flags
= MATCH_REFS_NONE
;
486 flags
|= MATCH_REFS_ALL
;
487 if (args
.send_mirror
)
488 flags
|= MATCH_REFS_MIRROR
;
490 /* No funny business with the matcher */
491 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
);
494 /* Does the other end support the reporting? */
495 if (server_supports("report-status"))
496 ask_for_status_report
= 1;
497 if (server_supports("delete-refs"))
498 allow_deleting_refs
= 1;
499 if (server_supports("tellme-more"))
500 can_ask_for_more
= 1;
504 remote_tail
= &remote_refs
;
505 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
506 nr_refspec
, refspec
, flags
)) {
512 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
513 "Perhaps you should specify a branch such as 'master'.\n");
519 * Finally, tell the other end!
522 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
523 const unsigned char *new_sha1
;
525 if (!ref
->peer_ref
) {
526 if (!args
.send_mirror
)
528 new_sha1
= null_sha1
;
531 new_sha1
= ref
->peer_ref
->new_sha1
;
534 ref
->deletion
= is_null_sha1(new_sha1
);
535 if (ref
->deletion
&& !allow_deleting_refs
) {
536 ref
->status
= REF_STATUS_REJECT_NODELETE
;
539 if (!ref
->deletion
&&
540 !hashcmp(ref
->old_sha1
, new_sha1
)) {
541 ref
->status
= REF_STATUS_UPTODATE
;
545 /* This part determines what can overwrite what.
548 * (0) you can always use --force or +A:B notation to
549 * selectively force individual ref pairs.
551 * (1) if the old thing does not exist, it is OK.
553 * (2) if you do not have the old thing, you are not allowed
554 * to overwrite it; you would not know what you are losing
557 * (3) if both new and old are commit-ish, and new is a
558 * descendant of old, it is OK.
560 * (4) regardless of all of the above, removing :B is
564 ref
->nonfastforward
=
566 !is_null_sha1(ref
->old_sha1
) &&
567 (!has_sha1_file(ref
->old_sha1
)
568 || !ref_newer(new_sha1
, ref
->old_sha1
));
570 if (ref
->nonfastforward
&& !ref
->force
&& !args
.force_update
) {
571 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
575 hashcpy(ref
->new_sha1
, new_sha1
);
580 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
581 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
583 if (ask_for_status_report
) {
584 packet_write(out
, "%s %s %s%c%s",
585 old_hex
, new_hex
, ref
->name
, 0,
587 ask_for_status_report
= 0;
588 expect_status_report
= 1;
591 packet_write(out
, "%s %s %s",
592 old_hex
, new_hex
, ref
->name
);
594 ref
->status
= expect_status_report
?
595 REF_STATUS_EXPECTING_REPORT
:
599 if (can_ask_for_more
&& new_refs
)
600 ask_for_more(in
, out
, remote_refs
);
604 if (new_refs
&& !args
.dry_run
) {
605 if (pack_objects(out
, remote_refs
) < 0)
611 if (expect_status_report
)
612 ret
= receive_status(in
, remote_refs
);
616 print_push_status(dest
, remote_refs
);
618 if (!args
.dry_run
&& remote
) {
619 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
620 update_tracking_ref(remote
, ref
);
623 if (!refs_pushed(remote_refs
))
624 fprintf(stderr
, "Everything up-to-date\n");
627 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
628 switch (ref
->status
) {
629 case REF_STATUS_NONE
:
630 case REF_STATUS_UPTODATE
:
640 static void verify_remote_names(int nr_heads
, const char **heads
)
644 for (i
= 0; i
< nr_heads
; i
++) {
645 const char *remote
= strrchr(heads
[i
], ':');
647 remote
= remote
? (remote
+ 1) : heads
[i
];
648 switch (check_ref_format(remote
)) {
650 case CHECK_REF_FORMAT_ONELEVEL
:
651 /* ok but a single level -- that is fine for
654 case CHECK_REF_FORMAT_WILDCARD
:
655 /* ok but ends with a pattern-match character */
658 die("remote part of refspec is not a valid name in %s",
663 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
666 const char **heads
= NULL
;
667 const char *remote_name
= NULL
;
668 struct remote
*remote
= NULL
;
669 const char *dest
= NULL
;
672 for (i
= 1; i
< argc
; i
++, argv
++) {
673 const char *arg
= *argv
;
676 if (!prefixcmp(arg
, "--receive-pack=")) {
677 args
.receivepack
= arg
+ 15;
680 if (!prefixcmp(arg
, "--exec=")) {
681 args
.receivepack
= arg
+ 7;
684 if (!prefixcmp(arg
, "--remote=")) {
685 remote_name
= arg
+ 9;
688 if (!strcmp(arg
, "--all")) {
692 if (!strcmp(arg
, "--dry-run")) {
696 if (!strcmp(arg
, "--mirror")) {
697 args
.send_mirror
= 1;
700 if (!strcmp(arg
, "--force")) {
701 args
.force_update
= 1;
704 if (!strcmp(arg
, "--verbose")) {
708 if (!strcmp(arg
, "--thin")) {
709 args
.use_thin_pack
= 1;
712 usage(send_pack_usage
);
718 heads
= (const char **) argv
;
723 usage(send_pack_usage
);
725 * --all and --mirror are incompatible; neither makes sense
728 if ((heads
&& (args
.send_all
|| args
.send_mirror
)) ||
729 (args
.send_all
&& args
.send_mirror
))
730 usage(send_pack_usage
);
733 remote
= remote_get(remote_name
);
734 if (!remote_has_url(remote
, dest
)) {
735 die("Destination %s is not a uri for %s",
740 return send_pack(&args
, dest
, remote
, nr_heads
, heads
);
743 int send_pack(struct send_pack_args
*my_args
,
744 const char *dest
, struct remote
*remote
,
745 int nr_heads
, const char **heads
)
748 struct child_process
*conn
;
750 memcpy(&args
, my_args
, sizeof(args
));
752 verify_remote_names(nr_heads
, heads
);
754 conn
= git_connect(fd
, dest
, args
.receivepack
, args
.verbose
? CONNECT_VERBOSE
: 0);
755 ret
= do_send_pack(fd
[0], fd
[1], remote
, dest
, nr_heads
, heads
);
757 /* do_send_pack always closes fd[1] */
758 ret
|= finish_connect(conn
);