8 #include "run-command.h"
11 #include "send-pack.h"
13 #include "transport.h"
15 #include "oid-array.h"
16 #include "gpg-interface.h"
19 #include "parse-options.h"
20 #include "write-or-die.h"
22 static const char * const send_pack_usage
[] = {
23 N_("git send-pack [--mirror] [--dry-run] [--force]\n"
24 " [--receive-pack=<git-receive-pack>]\n"
25 " [--verbose] [--thin] [--atomic]\n"
26 " [--[no-]signed | --signed=(true|false|if-asked)]\n"
27 " [<host>:]<directory> (--all | <ref>...)"),
31 static struct send_pack_args args
;
33 static void print_helper_status(struct ref
*ref
)
35 struct strbuf buf
= STRBUF_INIT
;
36 struct ref_push_report
*report
;
38 for (; ref
; ref
= ref
->next
) {
39 const char *msg
= NULL
;
53 case REF_STATUS_UPTODATE
:
58 case REF_STATUS_REJECT_NONFASTFORWARD
:
60 msg
= "non-fast forward";
63 case REF_STATUS_REJECT_FETCH_FIRST
:
68 case REF_STATUS_REJECT_NEEDS_FORCE
:
73 case REF_STATUS_REJECT_STALE
:
78 case REF_STATUS_REJECT_REMOTE_UPDATED
:
80 msg
= "remote ref updated since checkout";
83 case REF_STATUS_REJECT_ALREADY_EXISTS
:
85 msg
= "already exists";
88 case REF_STATUS_REJECT_NODELETE
:
89 case REF_STATUS_REMOTE_REJECT
:
93 case REF_STATUS_EXPECTING_REPORT
:
95 msg
= "expecting report";
103 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
104 if (ref
->remote_status
)
105 msg
= ref
->remote_status
;
107 strbuf_addch(&buf
, ' ');
108 quote_two_c_style(&buf
, "", msg
, 0);
110 strbuf_addch(&buf
, '\n');
112 if (ref
->status
== REF_STATUS_OK
) {
113 for (report
= ref
->report
; report
; report
= report
->next
) {
115 strbuf_addf(&buf
, "ok %s\n", ref
->name
);
116 if (report
->ref_name
)
117 strbuf_addf(&buf
, "option refname %s\n",
120 strbuf_addf(&buf
, "option old-oid %s\n",
121 oid_to_hex(report
->old_oid
));
123 strbuf_addf(&buf
, "option new-oid %s\n",
124 oid_to_hex(report
->new_oid
));
125 if (report
->forced_update
)
126 strbuf_addstr(&buf
, "option forced-update\n");
129 write_or_die(1, buf
.buf
, buf
.len
);
131 strbuf_release(&buf
);
134 static int send_pack_config(const char *k
, const char *v
, void *cb
)
136 if (!strcmp(k
, "push.gpgsign")) {
138 if (!git_config_get_value("push.gpgsign", &value
)) {
139 switch (git_parse_maybe_bool(value
)) {
141 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
144 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
147 if (value
&& !strcasecmp(value
, "if-asked"))
148 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
150 return error(_("invalid value for '%s'"), k
);
154 return git_default_config(k
, v
, cb
);
157 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
159 struct refspec rs
= REFSPEC_INIT_PUSH
;
160 const char *remote_name
= NULL
;
161 struct remote
*remote
= NULL
;
162 const char *dest
= NULL
;
164 struct child_process
*conn
;
165 struct oid_array extra_have
= OID_ARRAY_INIT
;
166 struct oid_array shallow
= OID_ARRAY_INIT
;
167 struct ref
*remote_refs
, *local_refs
;
169 int helper_status
= 0;
172 const char *receivepack
= "git-receive-pack";
173 unsigned dry_run
= 0;
174 unsigned send_mirror
= 0;
175 unsigned force_update
= 0;
178 struct string_list push_options
= STRING_LIST_INIT_NODUP
;
179 unsigned use_thin_pack
= 0;
181 unsigned stateless_rpc
= 0;
183 unsigned int reject_reasons
;
186 struct push_cas_option cas
= {0};
187 int force_if_includes
= 0;
188 struct packet_reader reader
;
190 struct option options
[] = {
191 OPT__VERBOSITY(&verbose
),
192 OPT_STRING(0, "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
193 OPT_STRING(0, "exec", &receivepack
, "receive-pack", N_("receive pack program")),
194 OPT_STRING(0, "remote", &remote_name
, "remote", N_("remote name")),
195 OPT_BOOL(0, "all", &send_all
, N_("push all refs")),
196 OPT_BOOL('n' , "dry-run", &dry_run
, N_("dry run")),
197 OPT_BOOL(0, "mirror", &send_mirror
, N_("mirror all refs")),
198 OPT_BOOL('f', "force", &force_update
, N_("force updates")),
199 OPT_CALLBACK_F(0, "signed", &push_cert
, "(yes|no|if-asked)", N_("GPG sign the push"),
200 PARSE_OPT_OPTARG
, option_parse_push_signed
),
201 OPT_STRING_LIST(0, "push-option", &push_options
,
202 N_("server-specific"),
203 N_("option to transmit")),
204 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
205 OPT_BOOL(0, "thin", &use_thin_pack
, N_("use thin pack")),
206 OPT_BOOL(0, "atomic", &atomic
, N_("request atomic transaction on remote side")),
207 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
, N_("use stateless RPC protocol")),
208 OPT_BOOL(0, "stdin", &from_stdin
, N_("read refs from stdin")),
209 OPT_BOOL(0, "helper-status", &helper_status
, N_("print status from remote helper")),
210 OPT_CALLBACK_F(0, CAS_OPT_NAME
, &cas
, N_("<refname>:<expect>"),
211 N_("require old value of ref to be at this value"),
212 PARSE_OPT_OPTARG
, parseopt_push_cas_option
),
213 OPT_BOOL(0, TRANS_OPT_FORCE_IF_INCLUDES
, &force_if_includes
,
214 N_("require remote updates to be integrated locally")),
218 git_config(send_pack_config
, NULL
);
219 argc
= parse_options(argc
, argv
, prefix
, options
, send_pack_usage
, 0);
222 refspec_appendn(&rs
, argv
+ 1, argc
- 1);
226 usage_with_options(send_pack_usage
, options
);
228 args
.verbose
= verbose
;
229 args
.dry_run
= dry_run
;
230 args
.send_mirror
= send_mirror
;
231 args
.force_update
= force_update
;
233 args
.push_cert
= push_cert
;
234 args
.progress
= progress
;
235 args
.use_thin_pack
= use_thin_pack
;
236 args
.atomic
= atomic
;
237 args
.stateless_rpc
= stateless_rpc
;
238 args
.push_options
= push_options
.nr
? &push_options
: NULL
;
242 if (args
.stateless_rpc
) {
244 while ((buf
= packet_read_line(0, NULL
)))
245 refspec_append(&rs
, buf
);
247 struct strbuf line
= STRBUF_INIT
;
248 while (strbuf_getline(&line
, stdin
) != EOF
)
249 refspec_append(&rs
, line
.buf
);
250 strbuf_release(&line
);
255 * --all and --mirror are incompatible; neither makes sense
258 if ((rs
.nr
> 0 && (send_all
|| args
.send_mirror
)) ||
259 (send_all
&& args
.send_mirror
))
260 usage_with_options(send_pack_usage
, options
);
263 remote
= remote_get(remote_name
);
264 if (!remote_has_url(remote
, dest
)) {
265 die("Destination %s is not a uri for %s",
271 progress
= !args
.quiet
&& isatty(2);
272 args
.progress
= progress
;
274 if (args
.stateless_rpc
) {
279 conn
= git_connect(fd
, dest
, "git-receive-pack", receivepack
,
280 args
.verbose
? CONNECT_VERBOSE
: 0);
283 packet_reader_init(&reader
, fd
[0], NULL
, 0,
284 PACKET_READ_CHOMP_NEWLINE
|
285 PACKET_READ_GENTLE_ON_EOF
|
286 PACKET_READ_DIE_ON_ERR_PACKET
);
288 switch (discover_version(&reader
)) {
290 die("support for protocol v2 not implemented yet");
294 get_remote_heads(&reader
, &remote_refs
, REF_NORMAL
,
295 &extra_have
, &shallow
);
297 case protocol_unknown_version
:
298 BUG("unknown protocol version");
301 local_refs
= get_local_heads();
303 flags
= MATCH_REFS_NONE
;
306 flags
|= MATCH_REFS_ALL
;
307 if (args
.send_mirror
)
308 flags
|= MATCH_REFS_MIRROR
;
311 if (match_push_refs(local_refs
, &remote_refs
, &rs
, flags
))
314 if (!is_empty_cas(&cas
))
315 apply_push_cas(&cas
, remote
, remote_refs
);
317 if (!is_empty_cas(&cas
) && force_if_includes
)
318 cas
.use_force_if_includes
= 1;
320 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
323 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
326 print_helper_status(remote_refs
);
331 ret
|= finish_connect(conn
);
334 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &reject_reasons
);
336 if (!args
.dry_run
&& remote
) {
338 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
339 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
342 if (!ret
&& !transport_refs_pushed(remote_refs
))
343 fprintf(stderr
, "Everything up-to-date\n");