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