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
= {
14 /* .receivepack = */ "git-receive-pack",
17 static int feed_object(const unsigned char *sha1
, int fd
, int negative
)
21 if (negative
&& !has_sha1_file(sha1
))
24 memcpy(buf
+ negative
, sha1_to_hex(sha1
), 40);
27 buf
[40 + negative
] = '\n';
28 return write_or_whine(fd
, buf
, 41 + negative
, "send-pack: send refs");
32 * Make a pack stream and spit it out into file descriptor fd
34 static int pack_objects(int fd
, struct ref
*refs
, struct extra_have_objects
*extra
)
37 * The child becomes pack-objects --revs; we feed
38 * the revision parameters to it via its stdin and
39 * let its stdout go back to the other end.
41 const char *argv
[] = {
49 struct child_process po
;
52 if (args
.use_thin_pack
)
54 memset(&po
, 0, sizeof(po
));
59 if (start_command(&po
))
60 die("git pack-objects failed (%s)", strerror(errno
));
63 * We feed the pack-objects we just spawned with revision
64 * parameters by writing to the pipe.
66 for (i
= 0; i
< extra
->nr
; i
++)
67 if (!feed_object(extra
->array
[i
], po
.in
, 1))
71 if (!is_null_sha1(refs
->old_sha1
) &&
72 !feed_object(refs
->old_sha1
, po
.in
, 1))
74 if (!is_null_sha1(refs
->new_sha1
) &&
75 !feed_object(refs
->new_sha1
, po
.in
, 0))
81 if (finish_command(&po
))
82 return error("pack-objects died with strange error");
86 static struct ref
*remote_refs
, **remote_tail
;
88 static int receive_status(int in
, struct ref
*refs
)
93 int len
= packet_read_line(in
, line
, sizeof(line
));
94 if (len
< 10 || memcmp(line
, "unpack ", 7))
95 return error("did not receive remote status");
96 if (memcmp(line
, "unpack ok\n", 10)) {
97 char *p
= line
+ strlen(line
) - 1;
100 error("unpack failed: %s", line
+ 7);
107 len
= packet_read_line(in
, line
, sizeof(line
));
111 (memcmp(line
, "ok ", 3) && memcmp(line
, "ng ", 3))) {
112 fprintf(stderr
, "protocol error: %s\n", line
);
117 line
[strlen(line
)-1] = '\0';
119 msg
= strchr(refname
, ' ');
123 /* first try searching at our hint, falling back to all refs */
125 hint
= find_ref_by_name(hint
, refname
);
127 hint
= find_ref_by_name(refs
, refname
);
129 warning("remote reported status on unknown ref: %s",
133 if (hint
->status
!= REF_STATUS_EXPECTING_REPORT
) {
134 warning("remote reported status on unexpected ref: %s",
139 if (line
[0] == 'o' && line
[1] == 'k')
140 hint
->status
= REF_STATUS_OK
;
142 hint
->status
= REF_STATUS_REMOTE_REJECT
;
146 hint
->remote_status
= xstrdup(msg
);
147 /* start our next search from the next ref */
153 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
)
157 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
163 if (!remote_find_tracking(remote
, &rs
)) {
165 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
167 delete_ref(rs
.dst
, NULL
, 0);
169 update_ref("update by push", rs
.dst
,
170 ref
->new_sha1
, NULL
, 0, 0);
175 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
177 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
179 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
181 fprintf(stderr
, "%s -> %s", prettify_ref(from
), prettify_ref(to
));
183 fputs(prettify_ref(to
), stderr
);
192 static const char *status_abbrev(unsigned char sha1
[20])
194 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
197 static void print_ok_ref_status(struct ref
*ref
)
200 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
201 else if (is_null_sha1(ref
->old_sha1
))
202 print_ref_status('*',
203 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
205 ref
, ref
->peer_ref
, NULL
);
211 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
212 if (ref
->nonfastforward
) {
213 strcat(quickref
, "...");
215 msg
= "forced update";
217 strcat(quickref
, "..");
221 strcat(quickref
, status_abbrev(ref
->new_sha1
));
223 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
227 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
230 fprintf(stderr
, "To %s\n", dest
);
232 switch(ref
->status
) {
233 case REF_STATUS_NONE
:
234 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
236 case REF_STATUS_REJECT_NODELETE
:
237 print_ref_status('!', "[rejected]", ref
, NULL
,
238 "remote does not support deleting refs");
240 case REF_STATUS_UPTODATE
:
241 print_ref_status('=', "[up to date]", ref
,
242 ref
->peer_ref
, NULL
);
244 case REF_STATUS_REJECT_NONFASTFORWARD
:
245 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
248 case REF_STATUS_REMOTE_REJECT
:
249 print_ref_status('!', "[remote rejected]", ref
,
250 ref
->deletion
? NULL
: ref
->peer_ref
,
253 case REF_STATUS_EXPECTING_REPORT
:
254 print_ref_status('!', "[remote failure]", ref
,
255 ref
->deletion
? NULL
: ref
->peer_ref
,
256 "remote failed to report status");
259 print_ok_ref_status(ref
);
266 static void print_push_status(const char *dest
, struct ref
*refs
)
272 for (ref
= refs
; ref
; ref
= ref
->next
)
273 if (ref
->status
== REF_STATUS_UPTODATE
)
274 n
+= print_one_push_status(ref
, dest
, n
);
277 for (ref
= refs
; ref
; ref
= ref
->next
)
278 if (ref
->status
== REF_STATUS_OK
)
279 n
+= print_one_push_status(ref
, dest
, n
);
281 for (ref
= refs
; ref
; ref
= ref
->next
) {
282 if (ref
->status
!= REF_STATUS_NONE
&&
283 ref
->status
!= REF_STATUS_UPTODATE
&&
284 ref
->status
!= REF_STATUS_OK
)
285 n
+= print_one_push_status(ref
, dest
, n
);
289 static int refs_pushed(struct ref
*ref
)
291 for (; ref
; ref
= ref
->next
) {
292 switch(ref
->status
) {
293 case REF_STATUS_NONE
:
294 case REF_STATUS_UPTODATE
:
303 static int do_send_pack(int in
, int out
, struct remote
*remote
, const char *dest
, int nr_refspec
, const char **refspec
)
305 struct ref
*ref
, *local_refs
;
307 int ask_for_status_report
= 0;
308 int allow_deleting_refs
= 0;
309 int expect_status_report
= 0;
310 int flags
= MATCH_REFS_NONE
;
312 struct extra_have_objects extra_have
;
314 memset(&extra_have
, 0, sizeof(extra_have
));
316 flags
|= MATCH_REFS_ALL
;
317 if (args
.send_mirror
)
318 flags
|= MATCH_REFS_MIRROR
;
320 /* No funny business with the matcher */
321 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, REF_NORMAL
,
323 local_refs
= get_local_heads();
325 /* Does the other end support the reporting? */
326 if (server_supports("report-status"))
327 ask_for_status_report
= 1;
328 if (server_supports("delete-refs"))
329 allow_deleting_refs
= 1;
333 remote_tail
= &remote_refs
;
334 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
335 nr_refspec
, refspec
, flags
)) {
341 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
342 "Perhaps you should specify a branch such as 'master'.\n");
348 * Finally, tell the other end!
351 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
354 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
355 else if (!args
.send_mirror
)
358 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
359 if (ref
->deletion
&& !allow_deleting_refs
) {
360 ref
->status
= REF_STATUS_REJECT_NODELETE
;
363 if (!ref
->deletion
&&
364 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
365 ref
->status
= REF_STATUS_UPTODATE
;
369 /* This part determines what can overwrite what.
372 * (0) you can always use --force or +A:B notation to
373 * selectively force individual ref pairs.
375 * (1) if the old thing does not exist, it is OK.
377 * (2) if you do not have the old thing, you are not allowed
378 * to overwrite it; you would not know what you are losing
381 * (3) if both new and old are commit-ish, and new is a
382 * descendant of old, it is OK.
384 * (4) regardless of all of the above, removing :B is
388 ref
->nonfastforward
=
390 !is_null_sha1(ref
->old_sha1
) &&
391 (!has_sha1_file(ref
->old_sha1
)
392 || !ref_newer(ref
->new_sha1
, ref
->old_sha1
));
394 if (ref
->nonfastforward
&& !ref
->force
&& !args
.force_update
) {
395 ref
->status
= REF_STATUS_REJECT_NONFASTFORWARD
;
403 char *old_hex
= sha1_to_hex(ref
->old_sha1
);
404 char *new_hex
= sha1_to_hex(ref
->new_sha1
);
406 if (ask_for_status_report
) {
407 packet_write(out
, "%s %s %s%c%s",
408 old_hex
, new_hex
, ref
->name
, 0,
410 ask_for_status_report
= 0;
411 expect_status_report
= 1;
414 packet_write(out
, "%s %s %s",
415 old_hex
, new_hex
, ref
->name
);
417 ref
->status
= expect_status_report
?
418 REF_STATUS_EXPECTING_REPORT
:
423 if (new_refs
&& !args
.dry_run
) {
424 if (pack_objects(out
, remote_refs
, &extra_have
) < 0)
430 if (expect_status_report
)
431 ret
= receive_status(in
, remote_refs
);
435 print_push_status(dest
, remote_refs
);
437 if (!args
.dry_run
&& remote
) {
438 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
439 update_tracking_ref(remote
, ref
);
442 if (!refs_pushed(remote_refs
))
443 fprintf(stderr
, "Everything up-to-date\n");
446 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
447 switch (ref
->status
) {
448 case REF_STATUS_NONE
:
449 case REF_STATUS_UPTODATE
:
459 static void verify_remote_names(int nr_heads
, const char **heads
)
463 for (i
= 0; i
< nr_heads
; i
++) {
464 const char *local
= heads
[i
];
465 const char *remote
= strrchr(heads
[i
], ':');
470 /* A matching refspec is okay. */
471 if (remote
== local
&& remote
[1] == '\0')
474 remote
= remote
? (remote
+ 1) : local
;
475 switch (check_ref_format(remote
)) {
477 case CHECK_REF_FORMAT_ONELEVEL
:
478 /* ok but a single level -- that is fine for
481 case CHECK_REF_FORMAT_WILDCARD
:
482 /* ok but ends with a pattern-match character */
485 die("remote part of refspec is not a valid name in %s",
490 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
493 const char **heads
= NULL
;
494 const char *remote_name
= NULL
;
495 struct remote
*remote
= NULL
;
496 const char *dest
= NULL
;
499 for (i
= 1; i
< argc
; i
++, argv
++) {
500 const char *arg
= *argv
;
503 if (!prefixcmp(arg
, "--receive-pack=")) {
504 args
.receivepack
= arg
+ 15;
507 if (!prefixcmp(arg
, "--exec=")) {
508 args
.receivepack
= arg
+ 7;
511 if (!prefixcmp(arg
, "--remote=")) {
512 remote_name
= arg
+ 9;
515 if (!strcmp(arg
, "--all")) {
519 if (!strcmp(arg
, "--dry-run")) {
523 if (!strcmp(arg
, "--mirror")) {
524 args
.send_mirror
= 1;
527 if (!strcmp(arg
, "--force")) {
528 args
.force_update
= 1;
531 if (!strcmp(arg
, "--verbose")) {
535 if (!strcmp(arg
, "--thin")) {
536 args
.use_thin_pack
= 1;
539 usage(send_pack_usage
);
545 heads
= (const char **) argv
;
550 usage(send_pack_usage
);
552 * --all and --mirror are incompatible; neither makes sense
555 if ((heads
&& (args
.send_all
|| args
.send_mirror
)) ||
556 (args
.send_all
&& args
.send_mirror
))
557 usage(send_pack_usage
);
560 remote
= remote_get(remote_name
);
561 if (!remote_has_url(remote
, dest
)) {
562 die("Destination %s is not a uri for %s",
567 return send_pack(&args
, dest
, remote
, nr_heads
, heads
);
570 int send_pack(struct send_pack_args
*my_args
,
571 const char *dest
, struct remote
*remote
,
572 int nr_heads
, const char **heads
)
575 struct child_process
*conn
;
577 memcpy(&args
, my_args
, sizeof(args
));
579 verify_remote_names(nr_heads
, heads
);
581 conn
= git_connect(fd
, dest
, args
.receivepack
, args
.verbose
? CONNECT_VERBOSE
: 0);
582 ret
= do_send_pack(fd
[0], fd
[1], remote
, dest
, nr_heads
, heads
);
584 /* do_send_pack always closes fd[1] */
585 ret
|= finish_connect(conn
);