6 #include "run-command.h"
11 #include "transport.h"
13 #include "sha1-array.h"
15 static const char send_pack_usage
[] =
16 "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] [<host>:]<directory> [<ref>...]\n"
17 " --all and explicit <ref> specification are mutually exclusive.";
19 static struct send_pack_args args
;
21 static void print_helper_status(struct ref
*ref
)
23 struct strbuf buf
= STRBUF_INIT
;
25 for (; ref
; ref
= ref
->next
) {
26 const char *msg
= NULL
;
39 case REF_STATUS_UPTODATE
:
44 case REF_STATUS_REJECT_NONFASTFORWARD
:
46 msg
= "non-fast forward";
49 case REF_STATUS_REJECT_FETCH_FIRST
:
54 case REF_STATUS_REJECT_NEEDS_FORCE
:
59 case REF_STATUS_REJECT_STALE
:
64 case REF_STATUS_REJECT_ALREADY_EXISTS
:
66 msg
= "already exists";
69 case REF_STATUS_REJECT_NODELETE
:
70 case REF_STATUS_REMOTE_REJECT
:
74 case REF_STATUS_EXPECTING_REPORT
:
80 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
81 if (ref
->remote_status
)
82 msg
= ref
->remote_status
;
84 strbuf_addch(&buf
, ' ');
85 quote_two_c_style(&buf
, "", msg
, 0);
87 strbuf_addch(&buf
, '\n');
89 write_or_die(1, buf
.buf
, buf
.len
);
94 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
96 int i
, nr_refspecs
= 0;
97 const char **refspecs
= NULL
;
98 const char *remote_name
= NULL
;
99 struct remote
*remote
= NULL
;
100 const char *dest
= NULL
;
102 struct child_process
*conn
;
103 struct sha1_array extra_have
= SHA1_ARRAY_INIT
;
104 struct sha1_array shallow
= SHA1_ARRAY_INIT
;
105 struct ref
*remote_refs
, *local_refs
;
107 int helper_status
= 0;
109 const char *receivepack
= "git-receive-pack";
111 unsigned int reject_reasons
;
114 struct push_cas_option cas
= {0};
117 for (i
= 1; i
< argc
; i
++, argv
++) {
118 const char *arg
= *argv
;
121 if (starts_with(arg
, "--receive-pack=")) {
122 receivepack
= arg
+ 15;
125 if (starts_with(arg
, "--exec=")) {
126 receivepack
= arg
+ 7;
129 if (starts_with(arg
, "--remote=")) {
130 remote_name
= arg
+ 9;
133 if (!strcmp(arg
, "--all")) {
137 if (!strcmp(arg
, "--dry-run")) {
141 if (!strcmp(arg
, "--mirror")) {
142 args
.send_mirror
= 1;
145 if (!strcmp(arg
, "--force")) {
146 args
.force_update
= 1;
149 if (!strcmp(arg
, "--quiet")) {
153 if (!strcmp(arg
, "--verbose")) {
157 if (!strcmp(arg
, "--signed")) {
161 if (!strcmp(arg
, "--progress")) {
165 if (!strcmp(arg
, "--no-progress")) {
169 if (!strcmp(arg
, "--thin")) {
170 args
.use_thin_pack
= 1;
173 if (!strcmp(arg
, "--atomic")) {
177 if (!strcmp(arg
, "--stateless-rpc")) {
178 args
.stateless_rpc
= 1;
181 if (!strcmp(arg
, "--stdin")) {
185 if (!strcmp(arg
, "--helper-status")) {
189 if (!strcmp(arg
, "--" CAS_OPT_NAME
)) {
190 if (parse_push_cas_option(&cas
, NULL
, 0) < 0)
194 if (!strcmp(arg
, "--no-" CAS_OPT_NAME
)) {
195 if (parse_push_cas_option(&cas
, NULL
, 1) < 0)
199 if (starts_with(arg
, "--" CAS_OPT_NAME
"=")) {
200 if (parse_push_cas_option(&cas
,
201 strchr(arg
, '=') + 1, 0) < 0)
205 usage(send_pack_usage
);
211 refspecs
= (const char **) argv
;
212 nr_refspecs
= argc
- i
;
216 usage(send_pack_usage
);
219 struct argv_array all_refspecs
= ARGV_ARRAY_INIT
;
221 for (i
= 0; i
< nr_refspecs
; i
++)
222 argv_array_push(&all_refspecs
, refspecs
[i
]);
224 if (args
.stateless_rpc
) {
226 while ((buf
= packet_read_line(0, NULL
)))
227 argv_array_push(&all_refspecs
, buf
);
229 struct strbuf line
= STRBUF_INIT
;
230 while (strbuf_getline(&line
, stdin
, '\n') != EOF
)
231 argv_array_push(&all_refspecs
, line
.buf
);
232 strbuf_release(&line
);
235 refspecs
= all_refspecs
.argv
;
236 nr_refspecs
= all_refspecs
.argc
;
240 * --all and --mirror are incompatible; neither makes sense
243 if ((refspecs
&& (send_all
|| args
.send_mirror
)) ||
244 (send_all
&& args
.send_mirror
))
245 usage(send_pack_usage
);
248 remote
= remote_get(remote_name
);
249 if (!remote_has_url(remote
, dest
)) {
250 die("Destination %s is not a uri for %s",
256 progress
= !args
.quiet
&& isatty(2);
257 args
.progress
= progress
;
259 if (args
.stateless_rpc
) {
264 conn
= git_connect(fd
, dest
, receivepack
,
265 args
.verbose
? CONNECT_VERBOSE
: 0);
268 get_remote_heads(fd
[0], NULL
, 0, &remote_refs
, REF_NORMAL
,
269 &extra_have
, &shallow
);
271 transport_verify_remote_names(nr_refspecs
, refspecs
);
273 local_refs
= get_local_heads();
275 flags
= MATCH_REFS_NONE
;
278 flags
|= MATCH_REFS_ALL
;
279 if (args
.send_mirror
)
280 flags
|= MATCH_REFS_MIRROR
;
283 if (match_push_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
286 if (!is_empty_cas(&cas
))
287 apply_push_cas(&cas
, remote
, remote_refs
);
289 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
292 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
295 print_helper_status(remote_refs
);
300 ret
|= finish_connect(conn
);
303 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &reject_reasons
);
305 if (!args
.dry_run
&& remote
) {
307 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
308 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
311 if (!ret
&& !transport_refs_pushed(remote_refs
))
312 fprintf(stderr
, "Everything up-to-date\n");