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