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