Start the 2.46 cycle
[git.git] / builtin / send-pack.c
blob3df9eaad092babdbbfc47f1c5441fc43a4ccd182
1 #include "builtin.h"
2 #include "config.h"
3 #include "hex.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 #include "remote.h"
7 #include "connect.h"
8 #include "send-pack.h"
9 #include "quote.h"
10 #include "transport.h"
11 #include "oid-array.h"
12 #include "gettext.h"
13 #include "protocol.h"
14 #include "parse-options.h"
15 #include "write-or-die.h"
17 static const char * const send_pack_usage[] = {
18 N_("git send-pack [--mirror] [--dry-run] [--force]\n"
19 " [--receive-pack=<git-receive-pack>]\n"
20 " [--verbose] [--thin] [--atomic]\n"
21 " [--[no-]signed | --signed=(true|false|if-asked)]\n"
22 " [<host>:]<directory> (--all | <ref>...)"),
23 NULL,
26 static struct send_pack_args args;
28 static void print_helper_status(struct ref *ref)
30 struct strbuf buf = STRBUF_INIT;
31 struct ref_push_report *report;
33 for (; ref; ref = ref->next) {
34 const char *msg = NULL;
35 const char *res;
36 int count = 0;
38 switch(ref->status) {
39 case REF_STATUS_NONE:
40 res = "error";
41 msg = "no match";
42 break;
44 case REF_STATUS_OK:
45 res = "ok";
46 break;
48 case REF_STATUS_UPTODATE:
49 res = "ok";
50 msg = "up to date";
51 break;
53 case REF_STATUS_REJECT_NONFASTFORWARD:
54 res = "error";
55 msg = "non-fast forward";
56 break;
58 case REF_STATUS_REJECT_FETCH_FIRST:
59 res = "error";
60 msg = "fetch first";
61 break;
63 case REF_STATUS_REJECT_NEEDS_FORCE:
64 res = "error";
65 msg = "needs force";
66 break;
68 case REF_STATUS_REJECT_STALE:
69 res = "error";
70 msg = "stale info";
71 break;
73 case REF_STATUS_REJECT_REMOTE_UPDATED:
74 res = "error";
75 msg = "remote ref updated since checkout";
76 break;
78 case REF_STATUS_REJECT_ALREADY_EXISTS:
79 res = "error";
80 msg = "already exists";
81 break;
83 case REF_STATUS_REJECT_NODELETE:
84 case REF_STATUS_REMOTE_REJECT:
85 res = "error";
86 break;
88 case REF_STATUS_EXPECTING_REPORT:
89 res = "error";
90 msg = "expecting report";
91 break;
93 default:
94 continue;
97 strbuf_reset(&buf);
98 strbuf_addf(&buf, "%s %s", res, ref->name);
99 if (ref->remote_status)
100 msg = ref->remote_status;
101 if (msg) {
102 strbuf_addch(&buf, ' ');
103 quote_two_c_style(&buf, "", msg, 0);
105 strbuf_addch(&buf, '\n');
107 if (ref->status == REF_STATUS_OK) {
108 for (report = ref->report; report; report = report->next) {
109 if (count++ > 0)
110 strbuf_addf(&buf, "ok %s\n", ref->name);
111 if (report->ref_name)
112 strbuf_addf(&buf, "option refname %s\n",
113 report->ref_name);
114 if (report->old_oid)
115 strbuf_addf(&buf, "option old-oid %s\n",
116 oid_to_hex(report->old_oid));
117 if (report->new_oid)
118 strbuf_addf(&buf, "option new-oid %s\n",
119 oid_to_hex(report->new_oid));
120 if (report->forced_update)
121 strbuf_addstr(&buf, "option forced-update\n");
124 write_or_die(1, buf.buf, buf.len);
126 strbuf_release(&buf);
129 static int send_pack_config(const char *k, const char *v,
130 const struct config_context *ctx, void *cb)
132 if (!strcmp(k, "push.gpgsign")) {
133 switch (git_parse_maybe_bool(v)) {
134 case 0:
135 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
136 break;
137 case 1:
138 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
139 break;
140 default:
141 if (!strcasecmp(v, "if-asked"))
142 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
143 else
144 return error(_("invalid value for '%s'"), k);
147 return git_default_config(k, v, ctx, cb);
150 int cmd_send_pack(int argc, const char **argv, const char *prefix)
152 struct refspec rs = REFSPEC_INIT_PUSH;
153 const char *remote_name = NULL;
154 struct remote *remote = NULL;
155 const char *dest = NULL;
156 int fd[2];
157 struct child_process *conn;
158 struct oid_array extra_have = OID_ARRAY_INIT;
159 struct oid_array shallow = OID_ARRAY_INIT;
160 struct ref *remote_refs, *local_refs;
161 int ret;
162 int helper_status = 0;
163 int send_all = 0;
164 int verbose = 0;
165 const char *receivepack = "git-receive-pack";
166 unsigned dry_run = 0;
167 unsigned send_mirror = 0;
168 unsigned force_update = 0;
169 unsigned quiet = 0;
170 int push_cert = 0;
171 struct string_list push_options = STRING_LIST_INIT_NODUP;
172 unsigned use_thin_pack = 0;
173 unsigned atomic = 0;
174 unsigned stateless_rpc = 0;
175 int flags;
176 unsigned int reject_reasons;
177 int progress = -1;
178 int from_stdin = 0;
179 struct push_cas_option cas = {0};
180 int force_if_includes = 0;
181 struct packet_reader reader;
183 struct option options[] = {
184 OPT__VERBOSITY(&verbose),
185 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
186 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
187 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
188 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
189 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
190 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
191 OPT_BOOL('f', "force", &force_update, N_("force updates")),
192 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
193 PARSE_OPT_OPTARG, option_parse_push_signed),
194 OPT_STRING_LIST(0, "push-option", &push_options,
195 N_("server-specific"),
196 N_("option to transmit")),
197 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
198 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
199 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
200 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
201 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
202 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
203 OPT_CALLBACK_F(0, "force-with-lease", &cas, N_("<refname>:<expect>"),
204 N_("require old value of ref to be at this value"),
205 PARSE_OPT_OPTARG, parseopt_push_cas_option),
206 OPT_BOOL(0, TRANS_OPT_FORCE_IF_INCLUDES, &force_if_includes,
207 N_("require remote updates to be integrated locally")),
208 OPT_END()
211 git_config(send_pack_config, NULL);
212 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
213 if (argc > 0) {
214 dest = argv[0];
215 refspec_appendn(&rs, argv + 1, argc - 1);
218 if (!dest)
219 usage_with_options(send_pack_usage, options);
221 args.verbose = verbose;
222 args.dry_run = dry_run;
223 args.send_mirror = send_mirror;
224 args.force_update = force_update;
225 args.quiet = quiet;
226 args.push_cert = push_cert;
227 args.progress = progress;
228 args.use_thin_pack = use_thin_pack;
229 args.atomic = atomic;
230 args.stateless_rpc = stateless_rpc;
231 args.push_options = push_options.nr ? &push_options : NULL;
232 args.url = dest;
234 if (from_stdin) {
235 if (args.stateless_rpc) {
236 const char *buf;
237 while ((buf = packet_read_line(0, NULL)))
238 refspec_append(&rs, buf);
239 } else {
240 struct strbuf line = STRBUF_INIT;
241 while (strbuf_getline(&line, stdin) != EOF)
242 refspec_append(&rs, line.buf);
243 strbuf_release(&line);
248 * --all and --mirror are incompatible; neither makes sense
249 * with any refspecs.
251 if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
252 (send_all && args.send_mirror))
253 usage_with_options(send_pack_usage, options);
255 if (remote_name) {
256 remote = remote_get(remote_name);
257 if (!remote_has_url(remote, dest)) {
258 die("Destination %s is not a uri for %s",
259 dest, remote_name);
263 if (progress == -1)
264 progress = !args.quiet && isatty(2);
265 args.progress = progress;
267 if (args.stateless_rpc) {
268 conn = NULL;
269 fd[0] = 0;
270 fd[1] = 1;
271 } else {
272 conn = git_connect(fd, dest, "git-receive-pack", receivepack,
273 args.verbose ? CONNECT_VERBOSE : 0);
276 packet_reader_init(&reader, fd[0], NULL, 0,
277 PACKET_READ_CHOMP_NEWLINE |
278 PACKET_READ_GENTLE_ON_EOF |
279 PACKET_READ_DIE_ON_ERR_PACKET);
281 switch (discover_version(&reader)) {
282 case protocol_v2:
283 die("support for protocol v2 not implemented yet");
284 break;
285 case protocol_v1:
286 case protocol_v0:
287 get_remote_heads(&reader, &remote_refs, REF_NORMAL,
288 &extra_have, &shallow);
289 break;
290 case protocol_unknown_version:
291 BUG("unknown protocol version");
294 local_refs = get_local_heads();
296 flags = MATCH_REFS_NONE;
298 if (send_all)
299 flags |= MATCH_REFS_ALL;
300 if (args.send_mirror)
301 flags |= MATCH_REFS_MIRROR;
303 /* match them up */
304 if (match_push_refs(local_refs, &remote_refs, &rs, flags))
305 return -1;
307 if (!is_empty_cas(&cas))
308 apply_push_cas(&cas, remote, remote_refs);
310 if (!is_empty_cas(&cas) && force_if_includes)
311 cas.use_force_if_includes = 1;
313 set_ref_status_for_push(remote_refs, args.send_mirror,
314 args.force_update);
316 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
318 if (helper_status)
319 print_helper_status(remote_refs);
321 close(fd[1]);
322 close(fd[0]);
324 ret |= finish_connect(conn);
326 if (!helper_status)
327 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
329 if (!args.dry_run && remote) {
330 struct ref *ref;
331 for (ref = remote_refs; ref; ref = ref->next)
332 transport_update_tracking_ref(remote, ref, args.verbose);
335 if (!ret && !transport_refs_pushed(remote_refs))
336 /* stable plumbing output; do not modify or localize */
337 fprintf(stderr, "Everything up-to-date\n");
339 return ret;