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 int feed_object(const unsigned char *sha1
, int fd
, int negative
)
22 if (negative
&& !has_sha1_file(sha1
))
25 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
28 buf
[40 + negative
] = '\n';
29 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
33 * Make a pack stream and spit it out into file descriptor fd
35 static int pack_objects(int fd
, struct ref
*refs
, struct extra_have_objects
*extra
)
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.
42 const char *argv
[] = {
50 struct child_process po
;
53 if (args
.use_thin_pack
)
55 memset(&po
, 0, sizeof(po
));
60 if (start_command(&po
))
61 die("git pack-objects failed (%s)", strerror(errno
));
64 * We feed the pack-objects we just spawned with revision
65 * parameters by writing to the pipe.
67 for (i
= 0; i
< extra
->nr
; i
++)
68 if (!feed_object(extra
->array
[i
], po
.in
, 1))
72 if (!is_null_sha1(refs
->old_sha1
) &&
73 !feed_object(refs
->old_sha1
, po
.in
, 1))
75 if (!is_null_sha1(refs
->new_sha1
) &&
76 !feed_object(refs
->new_sha1
, po
.in
, 0))
82 if (finish_command(&po
))
83 return error("pack-objects died with strange error");
87 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
90 struct commit_list
*temp
= list
;
91 temp
->item
->object
.flags
&= ~mark
;
97 static int ref_newer(const unsigned char *new_sha1
,
98 const unsigned char *old_sha1
)
101 struct commit
*old
, *new;
102 struct commit_list
*list
, *used
;
105 /* Both new and old must be commit-ish and new is descendant of
106 * old. Otherwise we require --force.
108 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
109 if (!o
|| o
->type
!= OBJ_COMMIT
)
111 old
= (struct commit
*) o
;
113 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
114 if (!o
|| o
->type
!= OBJ_COMMIT
)
116 new = (struct commit
*) o
;
118 if (parse_commit(new) < 0)
122 commit_list_insert(new, &list
);
124 new = pop_most_recent_commit(&list
, 1);
125 commit_list_insert(new, &used
);
131 unmark_and_free(list
, 1);
132 unmark_and_free(used
, 1);
136 static struct ref
*local_refs
, **local_tail
;
137 static struct ref
*remote_refs
, **remote_tail
;
139 static int one_local_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
144 /* we already know it starts with refs/ to get here */
145 if (check_ref_format(refname
+ 5))
148 len
= strlen(refname
) + 1;
149 ref
= xcalloc(1, sizeof(*ref
) + len
);
150 hashcpy(ref
->new_sha1
, sha1
);
151 memcpy(ref
->name
, refname
, len
);
153 local_tail
= &ref
->next
;
157 static void get_local_heads(void)
159 local_tail
= &local_refs
;
160 for_each_ref(one_local_ref
, NULL
);
163 static int receive_status(int in
, struct ref
*refs
)
168 int len
= packet_read_line(in
, line
, sizeof(line
));
169 if (len
< 10 || memcmp(line
, "unpack ", 7))
170 return error("did not receive remote status");
171 if (memcmp(line
, "unpack ok\n", 10)) {
172 char *p
= line
+ strlen(line
) - 1;
175 error("unpack failed: %s", line
+ 7);
182 len
= packet_read_line(in
, line
, sizeof(line
));
186 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
187 fprintf(stderr
, "protocol error: %s\n", line
);
192 line
[strlen(line
)-1] = '\0';
194 msg
= strchr(refname
, ' ');
198 /* first try searching at our hint, falling back to all refs */
200 hint
= find_ref_by_name(hint
, refname
);
202 hint
= find_ref_by_name(refs
, refname
);
204 warning("remote reported status on unknown ref: %s",
208 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
209 warning("remote reported status on unexpected ref: %s",
214 if (line
[0] == 'o' && line
[1] == 'k')
215 hint
->status
= REF_STATUS_OK
;
217 hint
->status
= REF_STATUS_REMOTE_REJECT
;
221 hint
->remote_status
= xstrdup(msg
);
222 /* start our next search from the next ref */
228 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
232 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
238 if (!remote_find_tracking(remote
, &rs
)) {
240 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
242 delete_ref(rs
.dst
, NULL
, 0);
244 update_ref("update by push", rs
.dst
,
245 ref
->new_sha1
, NULL
, 0, 0);
250 static const char *prettify_ref(const struct ref
*ref
)
252 const char *name
= ref
->name
;
254 !prefixcmp(name
, "refs/heads/") ? 11 :
255 !prefixcmp(name
, "refs/tags/") ? 10 :
256 !prefixcmp(name
, "refs/remotes/") ? 13 :
260 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
262 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
264 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
266 fprintf(stderr
, "%s -> %s", prettify_ref(from
), prettify_ref(to
));
268 fputs(prettify_ref(to
), stderr
);
277 static const char *status_abbrev(unsigned char sha1
[20])
279 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
282 static void print_ok_ref_status(struct ref
*ref
)
285 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
286 else if (is_null_sha1(ref
->old_sha1
))
287 print_ref_status('*',
288 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
290 ref
, ref
->peer_ref
, NULL
);
296 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
297 if (ref
->nonfastforward
) {
298 strcat(quickref
, "...");
300 msg
= "forced update";
302 strcat(quickref
, "..");
306 strcat(quickref
, status_abbrev(ref
->new_sha1
));
308 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
312 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
315 fprintf(stderr
, "To %s\n", dest
);
317 switch(ref
->status
) {
318 case REF_STATUS_NONE
:
319 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
321 case REF_STATUS_REJECT_NODELETE
:
322 print_ref_status('!', "[rejected]", ref
, NULL
,
323 "remote does not support deleting refs");
325 case REF_STATUS_UPTODATE
:
326 print_ref_status('=', "[up to date]", ref
,
327 ref
->peer_ref
, NULL
);
329 case REF_STATUS_REJECT_NONFASTFORWARD
:
330 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
333 case REF_STATUS_REMOTE_REJECT
:
334 print_ref_status('!', "[remote rejected]", ref
,
335 ref
->deletion
? NULL
: ref
->peer_ref
,
338 case REF_STATUS_EXPECTING_REPORT
:
339 print_ref_status('!', "[remote failure]", ref
,
340 ref
->deletion
? NULL
: ref
->peer_ref
,
341 "remote failed to report status");
344 print_ok_ref_status(ref
);
351 static void print_push_status(const char *dest
, struct ref
*refs
)
357 for (ref
= refs
; ref
; ref
= ref
->next
)
358 if (ref
->status
== REF_STATUS_UPTODATE
)
359 n
+= print_one_push_status(ref
, dest
, n
);
362 for (ref
= refs
; ref
; ref
= ref
->next
)
363 if (ref
->status
== REF_STATUS_OK
)
364 n
+= print_one_push_status(ref
, dest
, n
);
366 for (ref
= refs
; ref
; ref
= ref
->next
) {
367 if (ref
->status
!= REF_STATUS_NONE
&&
368 ref
->status
!= REF_STATUS_UPTODATE
&&
369 ref
->status
!= REF_STATUS_OK
)
370 n
+= print_one_push_status(ref
, dest
, n
);
374 static int refs_pushed(struct ref
*ref
)
376 for (; ref
; ref
= ref
->next
) {
377 switch(ref
->status
) {
378 case REF_STATUS_NONE
:
379 case REF_STATUS_UPTODATE
:
388 static int do_send_pack(int in
, int out
, struct remote
*remote
, const char *dest
, int nr_refspec
, const char **refspec
)
392 int ask_for_status_report
= 0;
393 int allow_deleting_refs
= 0;
394 int expect_status_report
= 0;
395 int flags
= MATCH_REFS_NONE
;
397 struct extra_have_objects extra_have
;
399 memset(&extra_have
, 0, sizeof(extra_have
));
401 flags
|= MATCH_REFS_ALL
;
402 if (args
.send_mirror
)
403 flags
|= MATCH_REFS_MIRROR
;
405 /* No funny business with the matcher */
406 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
,
410 /* Does the other end support the reporting? */
411 if (server_supports("report-status"))
412 ask_for_status_report
= 1;
413 if (server_supports("delete-refs"))
414 allow_deleting_refs
= 1;
418 remote_tail
= &remote_refs
;
419 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
420 nr_refspec
, refspec
, flags
)) {
426 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
427 "Perhaps you should specify a branch such as 'master'.\n");
433 * Finally, tell the other end!
436 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
439 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
440 else if (!args
.send_mirror
)
443 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
444 if (ref
->deletion
&& !allow_deleting_refs
) {
445 ref
->status
= REF_STATUS_REJECT_NODELETE
;
448 if (!ref
->deletion
&&
449 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
450 ref
->status
= REF_STATUS_UPTODATE
;
454 /* This part determines what can overwrite what.
457 * (0) you can always use --force or +A:B notation to
458 * selectively force individual ref pairs.
460 * (1) if the old thing does not exist, it is OK.
462 * (2) if you do not have the old thing, you are not allowed
463 * to overwrite it; you would not know what you are losing
466 * (3) if both new and old are commit-ish, and new is a
467 * descendant of old, it is OK.
469 * (4) regardless of all of the above, removing :B is
473 ref
->nonfastforward
=
475 !is_null_sha1(ref
->old_sha1
) &&
476 (!has_sha1_file(ref
->old_sha1
)
477 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
479 if (ref
->nonfastforward
&& !ref
->force
&& !args
.force_update
) {
480 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
488 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
489 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
491 if (ask_for_status_report
) {
492 packet_write(out
, "%s %s %s%c%s",
493 old_hex
, new_hex
, ref
->name
, 0,
495 ask_for_status_report
= 0;
496 expect_status_report
= 1;
499 packet_write(out
, "%s %s %s",
500 old_hex
, new_hex
, ref
->name
);
502 ref
->status
= expect_status_report
?
503 REF_STATUS_EXPECTING_REPORT
:
508 if (new_refs
&& !args
.dry_run
) {
509 if (pack_objects(out
, remote_refs
, &extra_have
) < 0)
515 if (expect_status_report
)
516 ret
= receive_status(in
, remote_refs
);
520 print_push_status(dest
, remote_refs
);
522 if (!args
.dry_run
&& remote
) {
523 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
524 update_tracking_ref(remote
, ref
);
527 if (!refs_pushed(remote_refs
))
528 fprintf(stderr
, "Everything up-to-date\n");
531 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
532 switch (ref
->status
) {
533 case REF_STATUS_NONE
:
534 case REF_STATUS_UPTODATE
:
544 static void verify_remote_names(int nr_heads
, const char **heads
)
548 for (i
= 0; i
< nr_heads
; i
++) {
549 const char *local
= heads
[i
];
550 const char *remote
= strrchr(heads
[i
], ':');
555 /* A matching refspec is okay. */
556 if (remote
== local
&& remote
[1] == '\0')
559 remote
= remote
? (remote
+ 1) : local
;
560 switch (check_ref_format(remote
)) {
562 case CHECK_REF_FORMAT_ONELEVEL
:
563 /* ok but a single level -- that is fine for
566 case CHECK_REF_FORMAT_WILDCARD
:
567 /* ok but ends with a pattern-match character */
570 die("remote part of refspec is not a valid name in %s",
575 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
578 const char **heads
= NULL
;
579 const char *remote_name
= NULL
;
580 struct remote
*remote
= NULL
;
581 const char *dest
= NULL
;
584 for (i
= 1; i
< argc
; i
++, argv
++) {
585 const char *arg
= *argv
;
588 if (!prefixcmp(arg
, "--receive-pack=")) {
589 args
.receivepack
= arg
+ 15;
592 if (!prefixcmp(arg
, "--exec=")) {
593 args
.receivepack
= arg
+ 7;
596 if (!prefixcmp(arg
, "--remote=")) {
597 remote_name
= arg
+ 9;
600 if (!strcmp(arg
, "--all")) {
604 if (!strcmp(arg
, "--dry-run")) {
608 if (!strcmp(arg
, "--mirror")) {
609 args
.send_mirror
= 1;
612 if (!strcmp(arg
, "--force")) {
613 args
.force_update
= 1;
616 if (!strcmp(arg
, "--verbose")) {
620 if (!strcmp(arg
, "--thin")) {
621 args
.use_thin_pack
= 1;
624 usage(send_pack_usage
);
630 heads
= (const char **) argv
;
635 usage(send_pack_usage
);
637 * --all and --mirror are incompatible; neither makes sense
640 if ((heads
&& (args
.send_all
|| args
.send_mirror
)) ||
641 (args
.send_all
&& args
.send_mirror
))
642 usage(send_pack_usage
);
645 remote
= remote_get(remote_name
);
646 if (!remote_has_url(remote
, dest
)) {
647 die("Destination %s is not a uri for %s",
652 return send_pack(&args
, dest
, remote
, nr_heads
, heads
);
655 int send_pack(struct send_pack_args
*my_args
,
656 const char *dest
, struct remote
*remote
,
657 int nr_heads
, const char **heads
)
660 struct child_process
*conn
;
662 memcpy(&args
, my_args
, sizeof(args
));
664 verify_remote_names(nr_heads
, heads
);
666 conn
= git_connect(fd
, dest
, args
.receivepack
, args
.verbose
? CONNECT_VERBOSE
: 0);
667 ret
= do_send_pack(fd
[0], fd
[1], remote
, dest
, nr_heads
, heads
);
669 /* do_send_pack always closes fd[1] */
670 ret
|= finish_connect(conn
);