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
[] = {
49 struct child_process po
;
53 if (args
->use_thin_pack
)
55 if (args
->use_ofs_delta
)
56 argv
[i
++] = "--delta-base-offset";
59 memset(&po
, 0, sizeof(po
));
64 if (start_command(&po
))
65 die_errno("git pack-objects failed");
68 * We feed the pack-objects we just spawned with revision
69 * parameters by writing to the pipe.
71 for (i
= 0; i
< extra
->nr
; i
++)
72 if (!feed_object(extra
->array
[i
], po
.in
, 1))
76 if (!is_null_sha1(refs
->old_sha1
) &&
77 !feed_object(refs
->old_sha1
, po
.in
, 1))
79 if (!is_null_sha1(refs
->new_sha1
) &&
80 !feed_object(refs
->new_sha1
, po
.in
, 0))
86 if (finish_command(&po
))
87 return error("pack-objects died with strange error");
91 static int receive_status(int in
, struct ref
*refs
)
96 int len
= packet_read_line(in
, line
, sizeof(line
));
97 if (len
< 10 || memcmp(line
, "unpack ", 7))
98 return error("did not receive remote status");
99 if (memcmp(line
, "unpack ok\n", 10)) {
100 char *p
= line
+ strlen(line
) - 1;
103 error("unpack failed: %s", line
+ 7);
110 len
= packet_read_line(in
, line
, sizeof(line
));
114 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
115 fprintf(stderr
, "protocol error: %s\n", line
);
120 line
[strlen(line
)-1] = '\0';
122 msg
= strchr(refname
, ' ');
126 /* first try searching at our hint, falling back to all refs */
128 hint
= find_ref_by_name(hint
, refname
);
130 hint
= find_ref_by_name(refs
, refname
);
132 warning("remote reported status on unknown ref: %s",
136 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
137 warning("remote reported status on unexpected ref: %s",
142 if (line
[0] == 'o' && line
[1] == 'k')
143 hint
->status
= REF_STATUS_OK
;
145 hint
->status
= REF_STATUS_REMOTE_REJECT
;
149 hint
->remote_status
= xstrdup(msg
);
150 /* start our next search from the next ref */
156 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
160 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
166 if (!remote_find_tracking(remote
, &rs
)) {
168 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
170 delete_ref(rs
.dst
, NULL
, 0);
172 update_ref("update by push", rs
.dst
,
173 ref
->new_sha1
, NULL
, 0, 0);
178 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
180 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
182 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
184 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
186 fputs(prettify_refname(to
->name
), stderr
);
195 static const char *status_abbrev(unsigned char sha1
[20])
197 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
200 static void print_ok_ref_status(struct ref
*ref
)
203 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
204 else if (is_null_sha1(ref
->old_sha1
))
205 print_ref_status('*',
206 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
208 ref
, ref
->peer_ref
, NULL
);
214 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
215 if (ref
->nonfastforward
) {
216 strcat(quickref
, "...");
218 msg
= "forced update";
220 strcat(quickref
, "..");
224 strcat(quickref
, status_abbrev(ref
->new_sha1
));
226 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
230 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
233 fprintf(stderr
, "To %s\n", dest
);
235 switch(ref
->status
) {
236 case REF_STATUS_NONE
:
237 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
239 case REF_STATUS_REJECT_NODELETE
:
240 print_ref_status('!', "[rejected]", ref
, NULL
,
241 "remote does not support deleting refs");
243 case REF_STATUS_UPTODATE
:
244 print_ref_status('=', "[up to date]", ref
,
245 ref
->peer_ref
, NULL
);
247 case REF_STATUS_REJECT_NONFASTFORWARD
:
248 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
251 case REF_STATUS_REMOTE_REJECT
:
252 print_ref_status('!', "[remote rejected]", ref
,
253 ref
->deletion
? NULL
: ref
->peer_ref
,
256 case REF_STATUS_EXPECTING_REPORT
:
257 print_ref_status('!', "[remote failure]", ref
,
258 ref
->deletion
? NULL
: ref
->peer_ref
,
259 "remote failed to report status");
262 print_ok_ref_status(ref
);
269 static void print_push_status(const char *dest
, struct ref
*refs
)
275 for (ref
= refs
; ref
; ref
= ref
->next
)
276 if (ref
->status
== REF_STATUS_UPTODATE
)
277 n
+= print_one_push_status(ref
, dest
, n
);
280 for (ref
= refs
; ref
; ref
= ref
->next
)
281 if (ref
->status
== REF_STATUS_OK
)
282 n
+= print_one_push_status(ref
, dest
, n
);
284 for (ref
= refs
; ref
; ref
= ref
->next
) {
285 if (ref
->status
!= REF_STATUS_NONE
&&
286 ref
->status
!= REF_STATUS_UPTODATE
&&
287 ref
->status
!= REF_STATUS_OK
)
288 n
+= print_one_push_status(ref
, dest
, n
);
292 static int refs_pushed(struct ref
*ref
)
294 for (; ref
; ref
= ref
->next
) {
295 switch(ref
->status
) {
296 case REF_STATUS_NONE
:
297 case REF_STATUS_UPTODATE
:
306 int send_pack(struct send_pack_args
*args
,
307 int fd
[], struct child_process
*conn
,
308 struct ref
*remote_refs
,
309 struct extra_have_objects
*extra_have
)
315 int ask_for_status_report
= 0;
316 int allow_deleting_refs
= 0;
317 int expect_status_report
= 0;
320 /* Does the other end support the reporting? */
321 if (server_supports("report-status"))
322 ask_for_status_report
= 1;
323 if (server_supports("delete-refs"))
324 allow_deleting_refs
= 1;
325 if (server_supports("ofs-delta"))
326 args
->use_ofs_delta
= 1;
329 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
330 "Perhaps you should specify a branch such as 'master'.\n");
335 * Finally, tell the other end!
338 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
341 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
342 else if (!args
->send_mirror
)
345 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
346 if (ref
->deletion
&& !allow_deleting_refs
) {
347 ref
->status
= REF_STATUS_REJECT_NODELETE
;
350 if (!ref
->deletion
&&
351 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
352 ref
->status
= REF_STATUS_UPTODATE
;
356 /* This part determines what can overwrite what.
359 * (0) you can always use --force or +A:B notation to
360 * selectively force individual ref pairs.
362 * (1) if the old thing does not exist, it is OK.
364 * (2) if you do not have the old thing, you are not allowed
365 * to overwrite it; you would not know what you are losing
368 * (3) if both new and old are commit-ish, and new is a
369 * descendant of old, it is OK.
371 * (4) regardless of all of the above, removing :B is
375 ref
->nonfastforward
=
377 !is_null_sha1(ref
->old_sha1
) &&
378 (!has_sha1_file(ref
->old_sha1
)
379 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
381 if (ref
->nonfastforward
&& !ref
->force
&& !args
->force_update
) {
382 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
389 if (!args
->dry_run
) {
390 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
391 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
393 if (ask_for_status_report
) {
394 packet_write(out
, "%s %s %s%c%s",
395 old_hex
, new_hex
, ref
->name
, 0,
397 ask_for_status_report
= 0;
398 expect_status_report
= 1;
401 packet_write(out
, "%s %s %s",
402 old_hex
, new_hex
, ref
->name
);
404 ref
->status
= expect_status_report
?
405 REF_STATUS_EXPECTING_REPORT
:
410 if (new_refs
&& !args
->dry_run
) {
411 if (pack_objects(out
, remote_refs
, extra_have
, args
) < 0) {
412 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
413 ref
->status
= REF_STATUS_NONE
;
418 if (expect_status_report
)
419 ret
= receive_status(in
, remote_refs
);
425 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
426 switch (ref
->status
) {
427 case REF_STATUS_NONE
:
428 case REF_STATUS_UPTODATE
:
438 static void verify_remote_names(int nr_heads
, const char **heads
)
442 for (i
= 0; i
< nr_heads
; i
++) {
443 const char *local
= heads
[i
];
444 const char *remote
= strrchr(heads
[i
], ':');
449 /* A matching refspec is okay. */
450 if (remote
== local
&& remote
[1] == '\0')
453 remote
= remote
? (remote
+ 1) : local
;
454 switch (check_ref_format(remote
)) {
456 case CHECK_REF_FORMAT_ONELEVEL
:
457 /* ok but a single level -- that is fine for
460 case CHECK_REF_FORMAT_WILDCARD
:
461 /* ok but ends with a pattern-match character */
464 die("remote part of refspec is not a valid name in %s",
469 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
471 int i
, nr_refspecs
= 0;
472 const char **refspecs
= NULL
;
473 const char *remote_name
= NULL
;
474 struct remote
*remote
= NULL
;
475 const char *dest
= NULL
;
477 struct child_process
*conn
;
478 struct extra_have_objects extra_have
;
479 struct ref
*remote_refs
, *local_refs
;
482 const char *receivepack
= "git-receive-pack";
486 for (i
= 1; i
< argc
; i
++, argv
++) {
487 const char *arg
= *argv
;
490 if (!prefixcmp(arg
, "--receive-pack=")) {
491 receivepack
= arg
+ 15;
494 if (!prefixcmp(arg
, "--exec=")) {
495 receivepack
= arg
+ 7;
498 if (!prefixcmp(arg
, "--remote=")) {
499 remote_name
= arg
+ 9;
502 if (!strcmp(arg
, "--all")) {
506 if (!strcmp(arg
, "--dry-run")) {
510 if (!strcmp(arg
, "--mirror")) {
511 args
.send_mirror
= 1;
514 if (!strcmp(arg
, "--force")) {
515 args
.force_update
= 1;
518 if (!strcmp(arg
, "--verbose")) {
522 if (!strcmp(arg
, "--thin")) {
523 args
.use_thin_pack
= 1;
526 usage(send_pack_usage
);
532 refspecs
= (const char **) argv
;
533 nr_refspecs
= argc
- i
;
537 usage(send_pack_usage
);
539 * --all and --mirror are incompatible; neither makes sense
542 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
543 (send_all
&& args
.send_mirror
))
544 usage(send_pack_usage
);
547 remote
= remote_get(remote_name
);
548 if (!remote_has_url(remote
, dest
)) {
549 die("Destination %s is not a uri for %s",
554 conn
= git_connect(fd
, dest
, receivepack
, args
.verbose
? CONNECT_VERBOSE
: 0);
556 memset(&extra_have
, 0, sizeof(extra_have
));
558 get_remote_heads(fd
[0], &remote_refs
, 0, NULL
, REF_NORMAL
,
561 verify_remote_names(nr_refspecs
, refspecs
);
563 local_refs
= get_local_heads();
565 flags
= MATCH_REFS_NONE
;
568 flags
|= MATCH_REFS_ALL
;
569 if (args
.send_mirror
)
570 flags
|= MATCH_REFS_MIRROR
;
573 if (match_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
576 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
581 ret
|= finish_connect(conn
);
583 print_push_status(dest
, remote_refs
);
585 if (!args
.dry_run
&& remote
) {
587 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
588 update_tracking_ref(remote
, ref
);
591 if (!ret
&& !refs_pushed(remote_refs
))
592 fprintf(stderr
, "Everything up-to-date\n");