7 #include "run-command.h"
10 #include "send-pack.h"
12 #include "transport.h"
14 #include "oid-array.h"
15 #include "gpg-interface.h"
19 static const char * const send_pack_usage[] = {
20 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
21 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
22 "[<host>:]<directory> [<ref>...]\n"
23 " --all and explicit <ref> specification are mutually exclusive."),
27 static struct send_pack_args args;
29 static void print_helper_status(struct ref *ref)
31 struct strbuf buf = STRBUF_INIT;
32 struct ref_push_report *report;
34 for (; ref; ref = ref->next) {
35 const char *msg = NULL;
49 case REF_STATUS_UPTODATE:
54 case REF_STATUS_REJECT_NONFASTFORWARD:
56 msg = "non-fast forward";
59 case REF_STATUS_REJECT_FETCH_FIRST:
64 case REF_STATUS_REJECT_NEEDS_FORCE:
69 case REF_STATUS_REJECT_STALE:
74 case REF_STATUS_REJECT_ALREADY_EXISTS:
76 msg = "already exists";
79 case REF_STATUS_REJECT_NODELETE:
80 case REF_STATUS_REMOTE_REJECT:
84 case REF_STATUS_EXPECTING_REPORT:
90 strbuf_addf(&buf, "%s %s", res, ref->name);
91 if (ref->remote_status)
92 msg = ref->remote_status;
94 strbuf_addch(&buf, ' ');
95 quote_two_c_style(&buf, "", msg, 0);
97 strbuf_addch(&buf, '\n');
99 if (ref->status == REF_STATUS_OK) {
100 for (report = ref->report; report; report = report->next) {
102 strbuf_addf(&buf, "ok %s\n", ref->name);
103 if (report->ref_name)
104 strbuf_addf(&buf, "option refname %s\n",
107 strbuf_addf(&buf, "option old-oid %s\n",
108 oid_to_hex(report->old_oid));
110 strbuf_addf(&buf, "option new-oid %s\n",
111 oid_to_hex(report->new_oid));
112 if (report->forced_update)
113 strbuf_addstr(&buf, "option forced-update\n");
116 write_or_die(1, buf.buf, buf.len);
118 strbuf_release(&buf);
121 static int send_pack_config(const char *k, const char *v, void *cb)
123 git_gpg_config(k, v, NULL);
125 if (!strcmp(k, "push.gpgsign")) {
127 if (!git_config_get_value("push.gpgsign", &value)) {
128 switch (git_parse_maybe_bool(value)) {
130 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
133 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
136 if (value && !strcasecmp(value, "if-asked"))
137 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
139 return error("Invalid value for '%s'", k);
143 return git_default_config(k, v, cb);
146 int cmd_send_pack(int argc, const char **argv, const char *prefix)
148 struct refspec rs = REFSPEC_INIT_PUSH;
149 const char *remote_name = NULL;
150 struct remote *remote = NULL;
151 const char *dest = NULL;
153 struct child_process *conn;
154 struct oid_array extra_have = OID_ARRAY_INIT;
155 struct oid_array shallow = OID_ARRAY_INIT;
156 struct ref *remote_refs, *local_refs;
158 int helper_status = 0;
161 const char *receivepack = "git-receive-pack";
162 unsigned dry_run = 0;
163 unsigned send_mirror = 0;
164 unsigned force_update = 0;
167 struct string_list push_options = STRING_LIST_INIT_NODUP;
168 unsigned use_thin_pack = 0;
170 unsigned stateless_rpc = 0;
172 unsigned int reject_reasons;
175 struct push_cas_option cas = {0};
176 struct packet_reader reader;
178 struct option options[] = {
179 OPT__VERBOSITY(&verbose),
180 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
181 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
182 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
183 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
184 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
185 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
186 OPT_BOOL('f', "force", &force_update, N_("force updates")),
187 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
188 PARSE_OPT_OPTARG, option_parse_push_signed),
189 OPT_STRING_LIST(0, "push-option", &push_options,
190 N_("server-specific"),
191 N_("option to transmit")),
192 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
193 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
194 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
195 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
196 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
197 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
198 OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
199 N_("require old value of ref to be at this value"),
200 PARSE_OPT_OPTARG, parseopt_push_cas_option),
204 git_config(send_pack_config, NULL);
205 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
208 refspec_appendn(&rs, argv + 1, argc - 1);
212 usage_with_options(send_pack_usage, options);
214 args.verbose = verbose;
215 args.dry_run = dry_run;
216 args.send_mirror = send_mirror;
217 args.force_update = force_update;
219 args.push_cert = push_cert;
220 args.progress = progress;
221 args.use_thin_pack = use_thin_pack;
222 args.atomic = atomic;
223 args.stateless_rpc = stateless_rpc;
224 args.push_options = push_options.nr ? &push_options : NULL;
227 if (args.stateless_rpc) {
229 while ((buf = packet_read_line(0, NULL)))
230 refspec_append(&rs, buf);
232 struct strbuf line = STRBUF_INIT;
233 while (strbuf_getline(&line, stdin) != EOF)
234 refspec_append(&rs, line.buf);
235 strbuf_release(&line);
240 * --all and --mirror are incompatible; neither makes sense
243 if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
244 (send_all && args.send_mirror))
245 usage_with_options(send_pack_usage, options);
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 packet_reader_init(&reader, fd[0], NULL, 0,
269 PACKET_READ_CHOMP_NEWLINE |
270 PACKET_READ_GENTLE_ON_EOF |
271 PACKET_READ_DIE_ON_ERR_PACKET);
273 switch (discover_version(&reader)) {
275 die("support for protocol v2 not implemented yet");
279 get_remote_heads(&reader, &remote_refs, REF_NORMAL,
280 &extra_have, &shallow);
282 case protocol_unknown_version:
283 BUG("unknown protocol version");
286 local_refs = get_local_heads();
288 flags = MATCH_REFS_NONE;
291 flags |= MATCH_REFS_ALL;
292 if (args.send_mirror)
293 flags |= MATCH_REFS_MIRROR;
296 if (match_push_refs(local_refs, &remote_refs, &rs, flags))
299 if (!is_empty_cas(&cas))
300 apply_push_cas(&cas, remote, remote_refs);
302 set_ref_status_for_push(remote_refs, args.send_mirror,
305 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
308 print_helper_status(remote_refs);
313 ret |= finish_connect(conn);
316 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
318 if (!args.dry_run && remote) {
320 for (ref = remote_refs; ref; ref = ref->next)
321 transport_update_tracking_ref(remote, ref, args.verbose);
324 if (!ret && !transport_refs_pushed(remote_refs))
325 fprintf(stderr, "Everything up-to-date\n");