5 #include "run-command.h"
9 static const char send_pack_usage
[] =
10 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
11 " --all and explicit <ref> specification are mutually exclusive.";
13 static struct send_pack_args args
;
15 static int feed_object(const unsigned char *sha1
, int fd
, int negative
)
19 if (negative
&& !has_sha1_file(sha1
))
22 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
25 buf
[40 + negative
] = '\n';
26 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
30 * Make a pack stream and spit it out into file descriptor fd
32 static int pack_objects(int fd
, struct ref
*refs
, struct extra_have_objects
*extra
, struct send_pack_args
*args
)
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.
39 const char *argv
[] = {
47 struct child_process po
;
50 if (args
->use_thin_pack
)
52 memset(&po
, 0, sizeof(po
));
57 if (start_command(&po
))
58 die("git pack-objects failed (%s)", strerror(errno
));
61 * We feed the pack-objects we just spawned with revision
62 * parameters by writing to the pipe.
64 for (i
= 0; i
< extra
->nr
; i
++)
65 if (!feed_object(extra
->array
[i
], po
.in
, 1))
69 if (!is_null_sha1(refs
->old_sha1
) &&
70 !feed_object(refs
->old_sha1
, po
.in
, 1))
72 if (!is_null_sha1(refs
->new_sha1
) &&
73 !feed_object(refs
->new_sha1
, po
.in
, 0))
79 if (finish_command(&po
))
80 return error("pack-objects died with strange error");
84 static int receive_status(int in
, struct ref
*refs
)
89 int len
= packet_read_line(in
, line
, sizeof(line
));
90 if (len
< 10 || memcmp(line
, "unpack ", 7))
91 return error("did not receive remote status");
92 if (memcmp(line
, "unpack ok\n", 10)) {
93 char *p
= line
+ strlen(line
) - 1;
96 error("unpack failed: %s", line
+ 7);
103 len
= packet_read_line(in
, line
, sizeof(line
));
107 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
108 fprintf(stderr
, "protocol error: %s\n", line
);
113 line
[strlen(line
)-1] = '\0';
115 msg
= strchr(refname
, ' ');
119 /* first try searching at our hint, falling back to all refs */
121 hint
= find_ref_by_name(hint
, refname
);
123 hint
= find_ref_by_name(refs
, refname
);
125 warning("remote reported status on unknown ref: %s",
129 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
130 warning("remote reported status on unexpected ref: %s",
135 if (line
[0] == 'o' && line
[1] == 'k')
136 hint
->status
= REF_STATUS_OK
;
138 hint
->status
= REF_STATUS_REMOTE_REJECT
;
142 hint
->remote_status
= xstrdup(msg
);
143 /* start our next search from the next ref */
149 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
153 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
159 if (!remote_find_tracking(remote
, &rs
)) {
161 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
163 delete_ref(rs
.dst
, NULL
, 0);
165 update_ref("update by push", rs
.dst
,
166 ref
->new_sha1
, NULL
, 0, 0);
171 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
173 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
175 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
177 fprintf(stderr
, "%s -> %s", prettify_ref(from
), prettify_ref(to
));
179 fputs(prettify_ref(to
), stderr
);
188 static const char *status_abbrev(unsigned char sha1
[20])
190 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
193 static void print_ok_ref_status(struct ref
*ref
)
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]" :
201 ref
, ref
->peer_ref
, NULL
);
207 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
208 if (ref
->nonfastforward
) {
209 strcat(quickref
, "...");
211 msg
= "forced update";
213 strcat(quickref
, "..");
217 strcat(quickref
, status_abbrev(ref
->new_sha1
));
219 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
223 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
226 fprintf(stderr
, "To %s\n", dest
);
228 switch(ref
->status
) {
229 case REF_STATUS_NONE
:
230 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
232 case REF_STATUS_REJECT_NODELETE
:
233 print_ref_status('!', "[rejected]", ref
, NULL
,
234 "remote does not support deleting refs");
236 case REF_STATUS_UPTODATE
:
237 print_ref_status('=', "[up to date]", ref
,
238 ref
->peer_ref
, NULL
);
240 case REF_STATUS_REJECT_NONFASTFORWARD
:
241 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
244 case REF_STATUS_REMOTE_REJECT
:
245 print_ref_status('!', "[remote rejected]", ref
,
246 ref
->deletion
? NULL
: ref
->peer_ref
,
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");
255 print_ok_ref_status(ref
);
262 static void print_push_status(const char *dest
, struct ref
*refs
)
268 for (ref
= refs
; ref
; ref
= ref
->next
)
269 if (ref
->status
== REF_STATUS_UPTODATE
)
270 n
+= print_one_push_status(ref
, dest
, n
);
273 for (ref
= refs
; ref
; ref
= ref
->next
)
274 if (ref
->status
== REF_STATUS_OK
)
275 n
+= print_one_push_status(ref
, dest
, n
);
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
);
285 static int refs_pushed(struct ref
*ref
)
287 for (; ref
; ref
= ref
->next
) {
288 switch(ref
->status
) {
289 case REF_STATUS_NONE
:
290 case REF_STATUS_UPTODATE
:
299 int 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
)
308 int ask_for_status_report
= 0;
309 int allow_deleting_refs
= 0;
310 int expect_status_report
= 0;
313 /* Does the other end support the reporting? */
314 if (server_supports("report-status"))
315 ask_for_status_report
= 1;
316 if (server_supports("delete-refs"))
317 allow_deleting_refs
= 1;
320 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
321 "Perhaps you should specify a branch such as 'master'.\n");
326 * Finally, tell the other end!
329 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
332 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
333 else if (!args
->send_mirror
)
336 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
337 if (ref
->deletion
&& !allow_deleting_refs
) {
338 ref
->status
= REF_STATUS_REJECT_NODELETE
;
341 if (!ref
->deletion
&&
342 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
343 ref
->status
= REF_STATUS_UPTODATE
;
347 /* This part determines what can overwrite what.
350 * (0) you can always use --force or +A:B notation to
351 * selectively force individual ref pairs.
353 * (1) if the old thing does not exist, it is OK.
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
359 * (3) if both new and old are commit-ish, and new is a
360 * descendant of old, it is OK.
362 * (4) regardless of all of the above, removing :B is
366 ref
->nonfastforward
=
368 !is_null_sha1(ref
->old_sha1
) &&
369 (!has_sha1_file(ref
->old_sha1
)
370 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
372 if (ref
->nonfastforward
&& !ref
->force
&& !args
->force_update
) {
373 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
380 if (!args
->dry_run
) {
381 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
382 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
384 if (ask_for_status_report
) {
385 packet_write(out
, "%s %s %s%c%s",
386 old_hex
, new_hex
, ref
->name
, 0,
388 ask_for_status_report
= 0;
389 expect_status_report
= 1;
392 packet_write(out
, "%s %s %s",
393 old_hex
, new_hex
, ref
->name
);
395 ref
->status
= expect_status_report
?
396 REF_STATUS_EXPECTING_REPORT
:
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
;
409 if (expect_status_report
)
410 ret
= receive_status(in
, remote_refs
);
416 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
417 switch (ref
->status
) {
418 case REF_STATUS_NONE
:
419 case REF_STATUS_UPTODATE
:
429 static void verify_remote_names(int nr_heads
, const char **heads
)
433 for (i
= 0; i
< nr_heads
; i
++) {
434 const char *local
= heads
[i
];
435 const char *remote
= strrchr(heads
[i
], ':');
440 /* A matching refspec is okay. */
441 if (remote
== local
&& remote
[1] == '\0')
444 remote
= remote
? (remote
+ 1) : local
;
445 switch (check_ref_format(remote
)) {
447 case CHECK_REF_FORMAT_ONELEVEL
:
448 /* ok but a single level -- that is fine for
451 case CHECK_REF_FORMAT_WILDCARD
:
452 /* ok but ends with a pattern-match character */
455 die("remote part of refspec is not a valid name in %s",
460 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
462 int i
, nr_refspecs
= 0;
463 const char **refspecs
= NULL
;
464 const char *remote_name
= NULL
;
465 struct remote
*remote
= NULL
;
466 const char *dest
= NULL
;
468 struct child_process
*conn
;
469 struct extra_have_objects extra_have
;
470 struct ref
*remote_refs
, **remote_tail
, *local_refs
;
473 const char *receivepack
= "git-receive-pack";
477 for (i
= 1; i
< argc
; i
++, argv
++) {
478 const char *arg
= *argv
;
481 if (!prefixcmp(arg
, "--receive-pack=")) {
482 receivepack
= arg
+ 15;
485 if (!prefixcmp(arg
, "--exec=")) {
486 receivepack
= arg
+ 7;
489 if (!prefixcmp(arg
, "--remote=")) {
490 remote_name
= arg
+ 9;
493 if (!strcmp(arg
, "--all")) {
497 if (!strcmp(arg
, "--dry-run")) {
501 if (!strcmp(arg
, "--mirror")) {
502 args
.send_mirror
= 1;
505 if (!strcmp(arg
, "--force")) {
506 args
.force_update
= 1;
509 if (!strcmp(arg
, "--verbose")) {
513 if (!strcmp(arg
, "--thin")) {
514 args
.use_thin_pack
= 1;
517 usage(send_pack_usage
);
523 refspecs
= (const char **) argv
;
524 nr_refspecs
= argc
- i
;
528 usage(send_pack_usage
);
530 * --all and --mirror are incompatible; neither makes sense
533 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
534 (send_all
&& args
.send_mirror
))
535 usage(send_pack_usage
);
538 remote
= remote_get(remote_name
);
539 if (!remote_has_url(remote
, dest
)) {
540 die("Destination %s is not a uri for %s",
545 conn
= git_connect(fd
, dest
, receivepack
, args
.verbose
? CONNECT_VERBOSE
: 0);
547 memset(&extra_have
, 0, sizeof(extra_have
));
549 get_remote_heads(fd
[0], &remote_refs
, 0, NULL
, REF_NORMAL
,
552 verify_remote_names(nr_refspecs
, refspecs
);
554 local_refs
= get_local_heads();
556 flags
= MATCH_REFS_NONE
;
559 flags
|= MATCH_REFS_ALL
;
560 if (args
.send_mirror
)
561 flags
|= MATCH_REFS_MIRROR
;
564 remote_tail
= &remote_refs
;
566 remote_tail
= &((*remote_tail
)->next
);
567 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
568 nr_refspecs
, refspecs
, flags
)) {
572 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
577 ret
|= finish_connect(conn
);
579 print_push_status(dest
, remote_refs
);
581 if (!args
.dry_run
&& remote
) {
583 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
584 update_tracking_ref(remote
, ref
);
587 if (!ret
&& !refs_pushed(remote_refs
))
588 fprintf(stderr
, "Everything up-to-date\n");