]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/send-pack.c
Merge branch 'pb/ref-filter-with-crlf'
[thirdparty/git.git] / builtin / send-pack.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "sideband.h"
7 #include "run-command.h"
8 #include "remote.h"
9 #include "connect.h"
10 #include "send-pack.h"
11 #include "quote.h"
12 #include "transport.h"
13 #include "version.h"
14 #include "oid-array.h"
15 #include "gpg-interface.h"
16 #include "gettext.h"
17 #include "protocol.h"
18
19 static const char * const send_pack_usage[] = {
20 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
21 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
22 "[<host>:]<directory> [<ref>...]\n"
23 " --all and explicit <ref> specification are mutually exclusive."),
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 default:
91 continue;
92 }
93
94 strbuf_reset(&buf);
95 strbuf_addf(&buf, "%s %s", res, ref->name);
96 if (ref->remote_status)
97 msg = ref->remote_status;
98 if (msg) {
99 strbuf_addch(&buf, ' ');
100 quote_two_c_style(&buf, "", msg, 0);
101 }
102 strbuf_addch(&buf, '\n');
103
104 if (ref->status == REF_STATUS_OK) {
105 for (report = ref->report; report; report = report->next) {
106 if (count++ > 0)
107 strbuf_addf(&buf, "ok %s\n", ref->name);
108 if (report->ref_name)
109 strbuf_addf(&buf, "option refname %s\n",
110 report->ref_name);
111 if (report->old_oid)
112 strbuf_addf(&buf, "option old-oid %s\n",
113 oid_to_hex(report->old_oid));
114 if (report->new_oid)
115 strbuf_addf(&buf, "option new-oid %s\n",
116 oid_to_hex(report->new_oid));
117 if (report->forced_update)
118 strbuf_addstr(&buf, "option forced-update\n");
119 }
120 }
121 write_or_die(1, buf.buf, buf.len);
122 }
123 strbuf_release(&buf);
124 }
125
126 static int send_pack_config(const char *k, const char *v, void *cb)
127 {
128 git_gpg_config(k, v, NULL);
129
130 if (!strcmp(k, "push.gpgsign")) {
131 const char *value;
132 if (!git_config_get_value("push.gpgsign", &value)) {
133 switch (git_parse_maybe_bool(value)) {
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 (value && !strcasecmp(value, "if-asked"))
142 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
143 else
144 return error("Invalid value for '%s'", k);
145 }
146 }
147 }
148 return git_default_config(k, v, cb);
149 }
150
151 int cmd_send_pack(int argc, const char **argv, const char *prefix)
152 {
153 struct refspec rs = REFSPEC_INIT_PUSH;
154 const char *remote_name = NULL;
155 struct remote *remote = NULL;
156 const char *dest = NULL;
157 int fd[2];
158 struct child_process *conn;
159 struct oid_array extra_have = OID_ARRAY_INIT;
160 struct oid_array shallow = OID_ARRAY_INIT;
161 struct ref *remote_refs, *local_refs;
162 int ret;
163 int helper_status = 0;
164 int send_all = 0;
165 int verbose = 0;
166 const char *receivepack = "git-receive-pack";
167 unsigned dry_run = 0;
168 unsigned send_mirror = 0;
169 unsigned force_update = 0;
170 unsigned quiet = 0;
171 int push_cert = 0;
172 struct string_list push_options = STRING_LIST_INIT_NODUP;
173 unsigned use_thin_pack = 0;
174 unsigned atomic = 0;
175 unsigned stateless_rpc = 0;
176 int flags;
177 unsigned int reject_reasons;
178 int progress = -1;
179 int from_stdin = 0;
180 struct push_cas_option cas = {0};
181 int force_if_includes = 0;
182 struct packet_reader reader;
183
184 struct option options[] = {
185 OPT__VERBOSITY(&verbose),
186 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
187 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
188 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
189 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
190 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
191 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
192 OPT_BOOL('f', "force", &force_update, N_("force updates")),
193 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
194 PARSE_OPT_OPTARG, option_parse_push_signed),
195 OPT_STRING_LIST(0, "push-option", &push_options,
196 N_("server-specific"),
197 N_("option to transmit")),
198 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
199 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
200 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
201 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
202 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
203 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
204 OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
205 N_("require old value of ref to be at this value"),
206 PARSE_OPT_OPTARG, parseopt_push_cas_option),
207 OPT_BOOL(0, TRANS_OPT_FORCE_IF_INCLUDES, &force_if_includes,
208 N_("require remote updates to be integrated locally")),
209 OPT_END()
210 };
211
212 git_config(send_pack_config, NULL);
213 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
214 if (argc > 0) {
215 dest = argv[0];
216 refspec_appendn(&rs, argv + 1, argc - 1);
217 }
218
219 if (!dest)
220 usage_with_options(send_pack_usage, options);
221
222 args.verbose = verbose;
223 args.dry_run = dry_run;
224 args.send_mirror = send_mirror;
225 args.force_update = force_update;
226 args.quiet = quiet;
227 args.push_cert = push_cert;
228 args.progress = progress;
229 args.use_thin_pack = use_thin_pack;
230 args.atomic = atomic;
231 args.stateless_rpc = stateless_rpc;
232 args.push_options = push_options.nr ? &push_options : NULL;
233
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);
244 }
245 }
246
247 /*
248 * --all and --mirror are incompatible; neither makes sense
249 * with any refspecs.
250 */
251 if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
252 (send_all && args.send_mirror))
253 usage_with_options(send_pack_usage, options);
254
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);
260 }
261 }
262
263 if (progress == -1)
264 progress = !args.quiet && isatty(2);
265 args.progress = progress;
266
267 if (args.stateless_rpc) {
268 conn = NULL;
269 fd[0] = 0;
270 fd[1] = 1;
271 } else {
272 conn = git_connect(fd, dest, receivepack,
273 args.verbose ? CONNECT_VERBOSE : 0);
274 }
275
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);
280
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");
292 }
293
294 local_refs = get_local_heads();
295
296 flags = MATCH_REFS_NONE;
297
298 if (send_all)
299 flags |= MATCH_REFS_ALL;
300 if (args.send_mirror)
301 flags |= MATCH_REFS_MIRROR;
302
303 /* match them up */
304 if (match_push_refs(local_refs, &remote_refs, &rs, flags))
305 return -1;
306
307 if (!is_empty_cas(&cas))
308 apply_push_cas(&cas, remote, remote_refs);
309
310 if (!is_empty_cas(&cas) && force_if_includes)
311 cas.use_force_if_includes = 1;
312
313 set_ref_status_for_push(remote_refs, args.send_mirror,
314 args.force_update);
315
316 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
317
318 if (helper_status)
319 print_helper_status(remote_refs);
320
321 close(fd[1]);
322 close(fd[0]);
323
324 ret |= finish_connect(conn);
325
326 if (!helper_status)
327 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
328
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);
333 }
334
335 if (!ret && !transport_refs_pushed(remote_refs))
336 fprintf(stderr, "Everything up-to-date\n");
337
338 return ret;
339 }