Merge branch 'ak/restore-both-incompatible-with-conflicts'
[git/debian.git] / builtin / send-pack.c
blobfb5b2bad2cc8e51e1ee83629a0cead31b1d4714e
1 #include "builtin.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "hex.h"
5 #include "refs.h"
6 #include "pkt-line.h"
7 #include "sideband.h"
8 #include "run-command.h"
9 #include "remote.h"
10 #include "connect.h"
11 #include "send-pack.h"
12 #include "quote.h"
13 #include "transport.h"
14 #include "version.h"
15 #include "oid-array.h"
16 #include "gpg-interface.h"
17 #include "gettext.h"
18 #include "protocol.h"
20 static const char * const send_pack_usage[] = {
21 N_("git send-pack [--mirror] [--dry-run] [--force]\n"
22 " [--receive-pack=<git-receive-pack>]\n"
23 " [--verbose] [--thin] [--atomic]\n"
24 " [--[no-]signed | --signed=(true|false|if-asked)]\n"
25 " [<host>:]<directory> (--all | <ref>...)"),
26 NULL,
29 static struct send_pack_args args;
31 static void print_helper_status(struct ref *ref)
33 struct strbuf buf = STRBUF_INIT;
34 struct ref_push_report *report;
36 for (; ref; ref = ref->next) {
37 const char *msg = NULL;
38 const char *res;
39 int count = 0;
41 switch(ref->status) {
42 case REF_STATUS_NONE:
43 res = "error";
44 msg = "no match";
45 break;
47 case REF_STATUS_OK:
48 res = "ok";
49 break;
51 case REF_STATUS_UPTODATE:
52 res = "ok";
53 msg = "up to date";
54 break;
56 case REF_STATUS_REJECT_NONFASTFORWARD:
57 res = "error";
58 msg = "non-fast forward";
59 break;
61 case REF_STATUS_REJECT_FETCH_FIRST:
62 res = "error";
63 msg = "fetch first";
64 break;
66 case REF_STATUS_REJECT_NEEDS_FORCE:
67 res = "error";
68 msg = "needs force";
69 break;
71 case REF_STATUS_REJECT_STALE:
72 res = "error";
73 msg = "stale info";
74 break;
76 case REF_STATUS_REJECT_REMOTE_UPDATED:
77 res = "error";
78 msg = "remote ref updated since checkout";
79 break;
81 case REF_STATUS_REJECT_ALREADY_EXISTS:
82 res = "error";
83 msg = "already exists";
84 break;
86 case REF_STATUS_REJECT_NODELETE:
87 case REF_STATUS_REMOTE_REJECT:
88 res = "error";
89 break;
91 case REF_STATUS_EXPECTING_REPORT:
92 res = "error";
93 msg = "expecting report";
94 break;
96 default:
97 continue;
100 strbuf_reset(&buf);
101 strbuf_addf(&buf, "%s %s", res, ref->name);
102 if (ref->remote_status)
103 msg = ref->remote_status;
104 if (msg) {
105 strbuf_addch(&buf, ' ');
106 quote_two_c_style(&buf, "", msg, 0);
108 strbuf_addch(&buf, '\n');
110 if (ref->status == REF_STATUS_OK) {
111 for (report = ref->report; report; report = report->next) {
112 if (count++ > 0)
113 strbuf_addf(&buf, "ok %s\n", ref->name);
114 if (report->ref_name)
115 strbuf_addf(&buf, "option refname %s\n",
116 report->ref_name);
117 if (report->old_oid)
118 strbuf_addf(&buf, "option old-oid %s\n",
119 oid_to_hex(report->old_oid));
120 if (report->new_oid)
121 strbuf_addf(&buf, "option new-oid %s\n",
122 oid_to_hex(report->new_oid));
123 if (report->forced_update)
124 strbuf_addstr(&buf, "option forced-update\n");
127 write_or_die(1, buf.buf, buf.len);
129 strbuf_release(&buf);
132 static int send_pack_config(const char *k, const char *v, void *cb)
134 if (!strcmp(k, "push.gpgsign")) {
135 const char *value;
136 if (!git_config_get_value("push.gpgsign", &value)) {
137 switch (git_parse_maybe_bool(value)) {
138 case 0:
139 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
140 break;
141 case 1:
142 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
143 break;
144 default:
145 if (value && !strcasecmp(value, "if-asked"))
146 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
147 else
148 return error(_("invalid value for '%s'"), k);
152 return git_default_config(k, v, cb);
155 int cmd_send_pack(int argc, const char **argv, const char *prefix)
157 struct refspec rs = REFSPEC_INIT_PUSH;
158 const char *remote_name = NULL;
159 struct remote *remote = NULL;
160 const char *dest = NULL;
161 int fd[2];
162 struct child_process *conn;
163 struct oid_array extra_have = OID_ARRAY_INIT;
164 struct oid_array shallow = OID_ARRAY_INIT;
165 struct ref *remote_refs, *local_refs;
166 int ret;
167 int helper_status = 0;
168 int send_all = 0;
169 int verbose = 0;
170 const char *receivepack = "git-receive-pack";
171 unsigned dry_run = 0;
172 unsigned send_mirror = 0;
173 unsigned force_update = 0;
174 unsigned quiet = 0;
175 int push_cert = 0;
176 struct string_list push_options = STRING_LIST_INIT_NODUP;
177 unsigned use_thin_pack = 0;
178 unsigned atomic = 0;
179 unsigned stateless_rpc = 0;
180 int flags;
181 unsigned int reject_reasons;
182 int progress = -1;
183 int from_stdin = 0;
184 struct push_cas_option cas = {0};
185 int force_if_includes = 0;
186 struct packet_reader reader;
188 struct option options[] = {
189 OPT__VERBOSITY(&verbose),
190 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
191 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
192 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
193 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
194 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
195 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
196 OPT_BOOL('f', "force", &force_update, N_("force updates")),
197 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
198 PARSE_OPT_OPTARG, option_parse_push_signed),
199 OPT_STRING_LIST(0, "push-option", &push_options,
200 N_("server-specific"),
201 N_("option to transmit")),
202 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
203 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
204 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
205 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
206 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
207 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
208 OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
209 N_("require old value of ref to be at this value"),
210 PARSE_OPT_OPTARG, parseopt_push_cas_option),
211 OPT_BOOL(0, TRANS_OPT_FORCE_IF_INCLUDES, &force_if_includes,
212 N_("require remote updates to be integrated locally")),
213 OPT_END()
216 git_config(send_pack_config, NULL);
217 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
218 if (argc > 0) {
219 dest = argv[0];
220 refspec_appendn(&rs, argv + 1, argc - 1);
223 if (!dest)
224 usage_with_options(send_pack_usage, options);
226 args.verbose = verbose;
227 args.dry_run = dry_run;
228 args.send_mirror = send_mirror;
229 args.force_update = force_update;
230 args.quiet = quiet;
231 args.push_cert = push_cert;
232 args.progress = progress;
233 args.use_thin_pack = use_thin_pack;
234 args.atomic = atomic;
235 args.stateless_rpc = stateless_rpc;
236 args.push_options = push_options.nr ? &push_options : NULL;
237 args.url = dest;
239 if (from_stdin) {
240 if (args.stateless_rpc) {
241 const char *buf;
242 while ((buf = packet_read_line(0, NULL)))
243 refspec_append(&rs, buf);
244 } else {
245 struct strbuf line = STRBUF_INIT;
246 while (strbuf_getline(&line, stdin) != EOF)
247 refspec_append(&rs, line.buf);
248 strbuf_release(&line);
253 * --all and --mirror are incompatible; neither makes sense
254 * with any refspecs.
256 if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
257 (send_all && args.send_mirror))
258 usage_with_options(send_pack_usage, options);
260 if (remote_name) {
261 remote = remote_get(remote_name);
262 if (!remote_has_url(remote, dest)) {
263 die("Destination %s is not a uri for %s",
264 dest, remote_name);
268 if (progress == -1)
269 progress = !args.quiet && isatty(2);
270 args.progress = progress;
272 if (args.stateless_rpc) {
273 conn = NULL;
274 fd[0] = 0;
275 fd[1] = 1;
276 } else {
277 conn = git_connect(fd, dest, receivepack,
278 args.verbose ? CONNECT_VERBOSE : 0);
281 packet_reader_init(&reader, fd[0], NULL, 0,
282 PACKET_READ_CHOMP_NEWLINE |
283 PACKET_READ_GENTLE_ON_EOF |
284 PACKET_READ_DIE_ON_ERR_PACKET);
286 switch (discover_version(&reader)) {
287 case protocol_v2:
288 die("support for protocol v2 not implemented yet");
289 break;
290 case protocol_v1:
291 case protocol_v0:
292 get_remote_heads(&reader, &remote_refs, REF_NORMAL,
293 &extra_have, &shallow);
294 break;
295 case protocol_unknown_version:
296 BUG("unknown protocol version");
299 local_refs = get_local_heads();
301 flags = MATCH_REFS_NONE;
303 if (send_all)
304 flags |= MATCH_REFS_ALL;
305 if (args.send_mirror)
306 flags |= MATCH_REFS_MIRROR;
308 /* match them up */
309 if (match_push_refs(local_refs, &remote_refs, &rs, flags))
310 return -1;
312 if (!is_empty_cas(&cas))
313 apply_push_cas(&cas, remote, remote_refs);
315 if (!is_empty_cas(&cas) && force_if_includes)
316 cas.use_force_if_includes = 1;
318 set_ref_status_for_push(remote_refs, args.send_mirror,
319 args.force_update);
321 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
323 if (helper_status)
324 print_helper_status(remote_refs);
326 close(fd[1]);
327 close(fd[0]);
329 ret |= finish_connect(conn);
331 if (!helper_status)
332 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
334 if (!args.dry_run && remote) {
335 struct ref *ref;
336 for (ref = remote_refs; ref; ref = ref->next)
337 transport_update_tracking_ref(remote, ref, args.verbose);
340 if (!ret && !transport_refs_pushed(remote_refs))
341 fprintf(stderr, "Everything up-to-date\n");
343 return ret;