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
*remote_refs
, **remote_tail
;
138 static int receive_status(int in
, struct ref
*refs
)
143 int len
= packet_read_line(in
, line
, sizeof(line
));
144 if (len
< 10 || memcmp(line
, "unpack ", 7))
145 return error("did not receive remote status");
146 if (memcmp(line
, "unpack ok\n", 10)) {
147 char *p
= line
+ strlen(line
) - 1;
150 error("unpack failed: %s", line
+ 7);
157 len
= packet_read_line(in
, line
, sizeof(line
));
161 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
162 fprintf(stderr
, "protocol error: %s\n", line
);
167 line
[strlen(line
)-1] = '\0';
169 msg
= strchr(refname
, ' ');
173 /* first try searching at our hint, falling back to all refs */
175 hint
= find_ref_by_name(hint
, refname
);
177 hint
= find_ref_by_name(refs
, refname
);
179 warning("remote reported status on unknown ref: %s",
183 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
184 warning("remote reported status on unexpected ref: %s",
189 if (line
[0] == 'o' && line
[1] == 'k')
190 hint
->status
= REF_STATUS_OK
;
192 hint
->status
= REF_STATUS_REMOTE_REJECT
;
196 hint
->remote_status
= xstrdup(msg
);
197 /* start our next search from the next ref */
203 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
207 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
213 if (!remote_find_tracking(remote
, &rs
)) {
215 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
217 delete_ref(rs
.dst
, NULL
, 0);
219 update_ref("update by push", rs
.dst
,
220 ref
->new_sha1
, NULL
, 0, 0);
225 static const char *prettify_ref(const struct ref
*ref
)
227 const char *name
= ref
->name
;
229 !prefixcmp(name
, "refs/heads/") ? 11 :
230 !prefixcmp(name
, "refs/tags/") ? 10 :
231 !prefixcmp(name
, "refs/remotes/") ? 13 :
235 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
237 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
239 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
241 fprintf(stderr
, "%s -> %s", prettify_ref(from
), prettify_ref(to
));
243 fputs(prettify_ref(to
), stderr
);
252 static const char *status_abbrev(unsigned char sha1
[20])
254 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
257 static void print_ok_ref_status(struct ref
*ref
)
260 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
261 else if (is_null_sha1(ref
->old_sha1
))
262 print_ref_status('*',
263 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
265 ref
, ref
->peer_ref
, NULL
);
271 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
272 if (ref
->nonfastforward
) {
273 strcat(quickref
, "...");
275 msg
= "forced update";
277 strcat(quickref
, "..");
281 strcat(quickref
, status_abbrev(ref
->new_sha1
));
283 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
287 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
290 fprintf(stderr
, "To %s\n", dest
);
292 switch(ref
->status
) {
293 case REF_STATUS_NONE
:
294 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
296 case REF_STATUS_REJECT_NODELETE
:
297 print_ref_status('!', "[rejected]", ref
, NULL
,
298 "remote does not support deleting refs");
300 case REF_STATUS_UPTODATE
:
301 print_ref_status('=', "[up to date]", ref
,
302 ref
->peer_ref
, NULL
);
304 case REF_STATUS_REJECT_NONFASTFORWARD
:
305 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
308 case REF_STATUS_REMOTE_REJECT
:
309 print_ref_status('!', "[remote rejected]", ref
,
310 ref
->deletion
? NULL
: ref
->peer_ref
,
313 case REF_STATUS_EXPECTING_REPORT
:
314 print_ref_status('!', "[remote failure]", ref
,
315 ref
->deletion
? NULL
: ref
->peer_ref
,
316 "remote failed to report status");
319 print_ok_ref_status(ref
);
326 static void print_push_status(const char *dest
, struct ref
*refs
)
332 for (ref
= refs
; ref
; ref
= ref
->next
)
333 if (ref
->status
== REF_STATUS_UPTODATE
)
334 n
+= print_one_push_status(ref
, dest
, n
);
337 for (ref
= refs
; ref
; ref
= ref
->next
)
338 if (ref
->status
== REF_STATUS_OK
)
339 n
+= print_one_push_status(ref
, dest
, n
);
341 for (ref
= refs
; ref
; ref
= ref
->next
) {
342 if (ref
->status
!= REF_STATUS_NONE
&&
343 ref
->status
!= REF_STATUS_UPTODATE
&&
344 ref
->status
!= REF_STATUS_OK
)
345 n
+= print_one_push_status(ref
, dest
, n
);
349 static int refs_pushed(struct ref
*ref
)
351 for (; ref
; ref
= ref
->next
) {
352 switch(ref
->status
) {
353 case REF_STATUS_NONE
:
354 case REF_STATUS_UPTODATE
:
363 static int do_send_pack(int in
, int out
, struct remote
*remote
, const char *dest
, int nr_refspec
, const char **refspec
)
365 struct ref
*ref
, *local_refs
;
367 int ask_for_status_report
= 0;
368 int allow_deleting_refs
= 0;
369 int expect_status_report
= 0;
370 int flags
= MATCH_REFS_NONE
;
372 struct extra_have_objects extra_have
;
374 memset(&extra_have
, 0, sizeof(extra_have
));
376 flags
|= MATCH_REFS_ALL
;
377 if (args
.send_mirror
)
378 flags
|= MATCH_REFS_MIRROR
;
380 /* No funny business with the matcher */
381 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
,
383 local_refs
= get_local_heads();
385 /* Does the other end support the reporting? */
386 if (server_supports("report-status"))
387 ask_for_status_report
= 1;
388 if (server_supports("delete-refs"))
389 allow_deleting_refs
= 1;
393 remote_tail
= &remote_refs
;
394 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
395 nr_refspec
, refspec
, flags
)) {
401 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
402 "Perhaps you should specify a branch such as 'master'.\n");
408 * Finally, tell the other end!
411 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
414 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
415 else if (!args
.send_mirror
)
418 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
419 if (ref
->deletion
&& !allow_deleting_refs
) {
420 ref
->status
= REF_STATUS_REJECT_NODELETE
;
423 if (!ref
->deletion
&&
424 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
425 ref
->status
= REF_STATUS_UPTODATE
;
429 /* This part determines what can overwrite what.
432 * (0) you can always use --force or +A:B notation to
433 * selectively force individual ref pairs.
435 * (1) if the old thing does not exist, it is OK.
437 * (2) if you do not have the old thing, you are not allowed
438 * to overwrite it; you would not know what you are losing
441 * (3) if both new and old are commit-ish, and new is a
442 * descendant of old, it is OK.
444 * (4) regardless of all of the above, removing :B is
448 ref
->nonfastforward
=
450 !is_null_sha1(ref
->old_sha1
) &&
451 (!has_sha1_file(ref
->old_sha1
)
452 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
454 if (ref
->nonfastforward
&& !ref
->force
&& !args
.force_update
) {
455 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
463 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
464 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
466 if (ask_for_status_report
) {
467 packet_write(out
, "%s %s %s%c%s",
468 old_hex
, new_hex
, ref
->name
, 0,
470 ask_for_status_report
= 0;
471 expect_status_report
= 1;
474 packet_write(out
, "%s %s %s",
475 old_hex
, new_hex
, ref
->name
);
477 ref
->status
= expect_status_report
?
478 REF_STATUS_EXPECTING_REPORT
:
483 if (new_refs
&& !args
.dry_run
) {
484 if (pack_objects(out
, remote_refs
, &extra_have
) < 0)
490 if (expect_status_report
)
491 ret
= receive_status(in
, remote_refs
);
495 print_push_status(dest
, remote_refs
);
497 if (!args
.dry_run
&& remote
) {
498 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
499 update_tracking_ref(remote
, ref
);
502 if (!refs_pushed(remote_refs
))
503 fprintf(stderr
, "Everything up-to-date\n");
506 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
507 switch (ref
->status
) {
508 case REF_STATUS_NONE
:
509 case REF_STATUS_UPTODATE
:
519 static void verify_remote_names(int nr_heads
, const char **heads
)
523 for (i
= 0; i
< nr_heads
; i
++) {
524 const char *local
= heads
[i
];
525 const char *remote
= strrchr(heads
[i
], ':');
530 /* A matching refspec is okay. */
531 if (remote
== local
&& remote
[1] == '\0')
534 remote
= remote
? (remote
+ 1) : local
;
535 switch (check_ref_format(remote
)) {
537 case CHECK_REF_FORMAT_ONELEVEL
:
538 /* ok but a single level -- that is fine for
541 case CHECK_REF_FORMAT_WILDCARD
:
542 /* ok but ends with a pattern-match character */
545 die("remote part of refspec is not a valid name in %s",
550 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
553 const char **heads
= NULL
;
554 const char *remote_name
= NULL
;
555 struct remote
*remote
= NULL
;
556 const char *dest
= NULL
;
559 for (i
= 1; i
< argc
; i
++, argv
++) {
560 const char *arg
= *argv
;
563 if (!prefixcmp(arg
, "--receive-pack=")) {
564 args
.receivepack
= arg
+ 15;
567 if (!prefixcmp(arg
, "--exec=")) {
568 args
.receivepack
= arg
+ 7;
571 if (!prefixcmp(arg
, "--remote=")) {
572 remote_name
= arg
+ 9;
575 if (!strcmp(arg
, "--all")) {
579 if (!strcmp(arg
, "--dry-run")) {
583 if (!strcmp(arg
, "--mirror")) {
584 args
.send_mirror
= 1;
587 if (!strcmp(arg
, "--force")) {
588 args
.force_update
= 1;
591 if (!strcmp(arg
, "--verbose")) {
595 if (!strcmp(arg
, "--thin")) {
596 args
.use_thin_pack
= 1;
599 usage(send_pack_usage
);
605 heads
= (const char **) argv
;
610 usage(send_pack_usage
);
612 * --all and --mirror are incompatible; neither makes sense
615 if ((heads
&& (args
.send_all
|| args
.send_mirror
)) ||
616 (args
.send_all
&& args
.send_mirror
))
617 usage(send_pack_usage
);
620 remote
= remote_get(remote_name
);
621 if (!remote_has_url(remote
, dest
)) {
622 die("Destination %s is not a uri for %s",
627 return send_pack(&args
, dest
, remote
, nr_heads
, heads
);
630 int send_pack(struct send_pack_args
*my_args
,
631 const char *dest
, struct remote
*remote
,
632 int nr_heads
, const char **heads
)
635 struct child_process
*conn
;
637 memcpy(&args
, my_args
, sizeof(args
));
639 verify_remote_names(nr_heads
, heads
);
641 conn
= git_connect(fd
, dest
, args
.receivepack
, args
.verbose
? CONNECT_VERBOSE
: 0);
642 ret
= do_send_pack(fd
[0], fd
[1], remote
, dest
, nr_heads
, heads
);
644 /* do_send_pack always closes fd[1] */
645 ret
|= finish_connect(conn
);