6 #include "run-command.h"
11 #include "transport.h"
13 #include "sha1-array.h"
14 #include "gpg-interface.h"
17 static const char * const send_pack_usage[] = {
18 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
19 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
20 "[<host>:]<directory> [<ref>...]\n"
21 " --all and explicit <ref> specification are mutually exclusive."),
25 static struct send_pack_args args;
27 static void print_helper_status(struct ref *ref)
29 struct strbuf buf = STRBUF_INIT;
31 for (; ref; ref = ref->next) {
32 const char *msg = NULL;
45 case REF_STATUS_UPTODATE:
50 case REF_STATUS_REJECT_NONFASTFORWARD:
52 msg = "non-fast forward";
55 case REF_STATUS_REJECT_FETCH_FIRST:
60 case REF_STATUS_REJECT_NEEDS_FORCE:
65 case REF_STATUS_REJECT_STALE:
70 case REF_STATUS_REJECT_ALREADY_EXISTS:
72 msg = "already exists";
75 case REF_STATUS_REJECT_NODELETE:
76 case REF_STATUS_REMOTE_REJECT:
80 case REF_STATUS_EXPECTING_REPORT:
86 strbuf_addf(&buf, "%s %s", res, ref->name);
87 if (ref->remote_status)
88 msg = ref->remote_status;
90 strbuf_addch(&buf, ' ');
91 quote_two_c_style(&buf, "", msg, 0);
93 strbuf_addch(&buf, '\n');
95 write_or_die(1, buf.buf, buf.len);
100 static int send_pack_config(const char *k, const char *v, void *cb)
102 git_gpg_config(k, v, NULL);
104 if (!strcmp(k, "push.gpgsign")) {
106 if (!git_config_get_value("push.gpgsign", &value)) {
107 switch (git_config_maybe_bool("push.gpgsign", value)) {
109 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
112 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
115 if (value && !strcasecmp(value, "if-asked"))
116 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
118 return error("Invalid value for '%s'", k);
125 int cmd_send_pack(int argc, const char **argv, const char *prefix)
127 int i, nr_refspecs = 0;
128 const char **refspecs = NULL;
129 const char *remote_name = NULL;
130 struct remote *remote = NULL;
131 const char *dest = NULL;
133 struct child_process *conn;
134 struct sha1_array extra_have = SHA1_ARRAY_INIT;
135 struct sha1_array shallow = SHA1_ARRAY_INIT;
136 struct ref *remote_refs, *local_refs;
138 int helper_status = 0;
141 const char *receivepack = "git-receive-pack";
142 unsigned dry_run = 0;
143 unsigned send_mirror = 0;
144 unsigned force_update = 0;
147 unsigned use_thin_pack = 0;
149 unsigned stateless_rpc = 0;
151 unsigned int reject_reasons;
154 struct push_cas_option cas = {0};
156 struct option options[] = {
157 OPT__VERBOSITY(&verbose),
158 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
159 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
160 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
161 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
162 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
163 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
164 OPT_BOOL('f', "force", &force_update, N_("force updates")),
166 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
167 PARSE_OPT_OPTARG, option_parse_push_signed },
168 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
169 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
170 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
171 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
172 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
173 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
175 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
176 N_("require old value of ref to be at this value"),
177 PARSE_OPT_OPTARG, parseopt_push_cas_option },
181 git_config(send_pack_config, NULL);
182 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
185 refspecs = (const char **)(argv + 1);
186 nr_refspecs = argc - 1;
190 usage_with_options(send_pack_usage, options);
192 args.verbose = verbose;
193 args.dry_run = dry_run;
194 args.send_mirror = send_mirror;
195 args.force_update = force_update;
197 args.push_cert = push_cert;
198 args.progress = progress;
199 args.use_thin_pack = use_thin_pack;
200 args.atomic = atomic;
201 args.stateless_rpc = stateless_rpc;
204 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
206 for (i = 0; i < nr_refspecs; i++)
207 argv_array_push(&all_refspecs, refspecs[i]);
209 if (args.stateless_rpc) {
211 while ((buf = packet_read_line(0, NULL)))
212 argv_array_push(&all_refspecs, buf);
214 struct strbuf line = STRBUF_INIT;
215 while (strbuf_getline(&line, stdin) != EOF)
216 argv_array_push(&all_refspecs, line.buf);
217 strbuf_release(&line);
220 refspecs = all_refspecs.argv;
221 nr_refspecs = all_refspecs.argc;
225 * --all and --mirror are incompatible; neither makes sense
228 if ((refspecs && (send_all || args.send_mirror)) ||
229 (send_all && args.send_mirror))
230 usage_with_options(send_pack_usage, options);
233 remote = remote_get(remote_name);
234 if (!remote_has_url(remote, dest)) {
235 die("Destination %s is not a uri for %s",
241 progress = !args.quiet && isatty(2);
242 args.progress = progress;
244 if (args.stateless_rpc) {
249 conn = git_connect(fd, dest, receivepack,
250 args.verbose ? CONNECT_VERBOSE : 0);
253 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
254 &extra_have, &shallow);
256 transport_verify_remote_names(nr_refspecs, refspecs);
258 local_refs = get_local_heads();
260 flags = MATCH_REFS_NONE;
263 flags |= MATCH_REFS_ALL;
264 if (args.send_mirror)
265 flags |= MATCH_REFS_MIRROR;
268 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
271 if (!is_empty_cas(&cas))
272 apply_push_cas(&cas, remote, remote_refs);
274 set_ref_status_for_push(remote_refs, args.send_mirror,
277 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
280 print_helper_status(remote_refs);
285 ret |= finish_connect(conn);
288 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
290 if (!args.dry_run && remote) {
292 for (ref = remote_refs; ref; ref = ref->next)
293 transport_update_tracking_ref(remote, ref, args.verbose);
296 if (!ret && !transport_refs_pushed(remote_refs))
297 fprintf(stderr, "Everything up-to-date\n");