]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/send-pack.c
strmap: enable allocations to come from a mem_pool
[thirdparty/git.git] / builtin / send-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
b2141fc1 2#include "config.h"
2a9c3fe8 3#include "commit.h"
584c6cc9 4#include "refs.h"
f3a3214e 5#include "pkt-line.h"
de1a2fdd 6#include "sideband.h"
38b1c662 7#include "run-command.h"
6b62816c 8#include "remote.h"
47a59185 9#include "connect.h"
96249c04 10#include "send-pack.h"
de1a2fdd 11#include "quote.h"
f1863d0d 12#include "transport.h"
ff5effdf 13#include "version.h"
fe299ec5 14#include "oid-array.h"
d830d395 15#include "gpg-interface.h"
068c77a5 16#include "gettext.h"
ad6ac124 17#include "protocol.h"
61221472 18
068c77a5
DB
19static 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};
96249c04 26
6828f72f 27static struct send_pack_args args;
61221472 28
de1a2fdd
SP
29static void print_helper_status(struct ref *ref)
30{
31 struct strbuf buf = STRBUF_INIT;
63518a57 32 struct ref_push_report *report;
de1a2fdd
SP
33
34 for (; ref; ref = ref->next) {
35 const char *msg = NULL;
36 const char *res;
63518a57 37 int count = 0;
de1a2fdd
SP
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";
dbfeddb1
CR
57 break;
58
75e5c0dc
JH
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
631b5ef2
JH
69 case REF_STATUS_REJECT_STALE:
70 res = "error";
71 msg = "stale info";
72 break;
73
dbfeddb1
CR
74 case REF_STATUS_REJECT_ALREADY_EXISTS:
75 res = "error";
76 msg = "already exists";
de1a2fdd
SP
77 break;
78
79 case REF_STATUS_REJECT_NODELETE:
80 case REF_STATUS_REMOTE_REJECT:
81 res = "error";
82 break;
83
84 case REF_STATUS_EXPECTING_REPORT:
85 default:
86 continue;
87 }
88
89 strbuf_reset(&buf);
90 strbuf_addf(&buf, "%s %s", res, ref->name);
91 if (ref->remote_status)
92 msg = ref->remote_status;
93 if (msg) {
94 strbuf_addch(&buf, ' ');
95 quote_two_c_style(&buf, "", msg, 0);
96 }
97 strbuf_addch(&buf, '\n');
98
63518a57
JX
99 if (ref->status == REF_STATUS_OK) {
100 for (report = ref->report; report; report = report->next) {
101 if (count++ > 0)
102 strbuf_addf(&buf, "ok %s\n", ref->name);
103 if (report->ref_name)
104 strbuf_addf(&buf, "option refname %s\n",
105 report->ref_name);
106 if (report->old_oid)
107 strbuf_addf(&buf, "option old-oid %s\n",
108 oid_to_hex(report->old_oid));
109 if (report->new_oid)
110 strbuf_addf(&buf, "option new-oid %s\n",
111 oid_to_hex(report->new_oid));
112 if (report->forced_update)
113 strbuf_addstr(&buf, "option forced-update\n");
114 }
115 }
cdf4fb8e 116 write_or_die(1, buf.buf, buf.len);
de1a2fdd
SP
117 }
118 strbuf_release(&buf);
119}
120
68c757f2
DB
121static int send_pack_config(const char *k, const char *v, void *cb)
122{
123 git_gpg_config(k, v, NULL);
124
125 if (!strcmp(k, "push.gpgsign")) {
126 const char *value;
127 if (!git_config_get_value("push.gpgsign", &value)) {
89576613 128 switch (git_parse_maybe_bool(value)) {
68c757f2
DB
129 case 0:
130 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
131 break;
132 case 1:
133 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
134 break;
135 default:
136 if (value && !strcasecmp(value, "if-asked"))
137 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
138 else
139 return error("Invalid value for '%s'", k);
140 }
141 }
142 }
d067d988 143 return git_default_config(k, v, cb);
68c757f2
DB
144}
145
96249c04 146int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 147{
168dba68 148 struct refspec rs = REFSPEC_INIT_PUSH;
96249c04 149 const char *remote_name = NULL;
b516968f 150 struct remote *remote = NULL;
96249c04 151 const char *dest = NULL;
64fcef2d
DB
152 int fd[2];
153 struct child_process *conn;
910650d2 154 struct oid_array extra_have = OID_ARRAY_INIT;
155 struct oid_array shallow = OID_ARRAY_INIT;
6d2bf96e 156 struct ref *remote_refs, *local_refs;
64fcef2d 157 int ret;
de1a2fdd 158 int helper_status = 0;
64fcef2d 159 int send_all = 0;
068c77a5 160 int verbose = 0;
64fcef2d 161 const char *receivepack = "git-receive-pack";
068c77a5
DB
162 unsigned dry_run = 0;
163 unsigned send_mirror = 0;
164 unsigned force_update = 0;
165 unsigned quiet = 0;
30261094 166 int push_cert = 0;
511155db 167 struct string_list push_options = STRING_LIST_INIT_NODUP;
068c77a5
DB
168 unsigned use_thin_pack = 0;
169 unsigned atomic = 0;
170 unsigned stateless_rpc = 0;
64fcef2d 171 int flags;
10643d4e 172 unsigned int reject_reasons;
391b1f20 173 int progress = -1;
26be19ba 174 int from_stdin = 0;
28f5d176 175 struct push_cas_option cas = {0};
ad6ac124 176 struct packet_reader reader;
84a9b58c 177
068c77a5
DB
178 struct option options[] = {
179 OPT__VERBOSITY(&verbose),
180 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
181 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
182 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
183 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
184 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
185 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
186 OPT_BOOL('f', "force", &force_update, N_("force updates")),
203c8533
DL
187 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
188 PARSE_OPT_OPTARG, option_parse_push_signed),
511155db
BW
189 OPT_STRING_LIST(0, "push-option", &push_options,
190 N_("server-specific"),
191 N_("option to transmit")),
068c77a5
DB
192 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
193 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
194 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
195 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
196 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
197 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
203c8533 198 OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
068c77a5 199 N_("require old value of ref to be at this value"),
203c8533 200 PARSE_OPT_OPTARG, parseopt_push_cas_option),
068c77a5
DB
201 OPT_END()
202 };
61221472 203
68c757f2 204 git_config(send_pack_config, NULL);
068c77a5
DB
205 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
206 if (argc > 0) {
207 dest = argv[0];
168dba68 208 refspec_appendn(&rs, argv + 1, argc - 1);
61221472 209 }
068c77a5 210
61221472 211 if (!dest)
068c77a5
DB
212 usage_with_options(send_pack_usage, options);
213
214 args.verbose = verbose;
215 args.dry_run = dry_run;
216 args.send_mirror = send_mirror;
217 args.force_update = force_update;
218 args.quiet = quiet;
219 args.push_cert = push_cert;
220 args.progress = progress;
221 args.use_thin_pack = use_thin_pack;
222 args.atomic = atomic;
223 args.stateless_rpc = stateless_rpc;
511155db 224 args.push_options = push_options.nr ? &push_options : NULL;
26be19ba
JK
225
226 if (from_stdin) {
26be19ba
JK
227 if (args.stateless_rpc) {
228 const char *buf;
229 while ((buf = packet_read_line(0, NULL)))
168dba68 230 refspec_append(&rs, buf);
26be19ba
JK
231 } else {
232 struct strbuf line = STRBUF_INIT;
933bea92 233 while (strbuf_getline(&line, stdin) != EOF)
168dba68 234 refspec_append(&rs, line.buf);
26be19ba
JK
235 strbuf_release(&line);
236 }
26be19ba
JK
237 }
238
28b9d6e5
AW
239 /*
240 * --all and --mirror are incompatible; neither makes sense
241 * with any refspecs.
242 */
168dba68 243 if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
64fcef2d 244 (send_all && args.send_mirror))
068c77a5 245 usage_with_options(send_pack_usage, options);
37adac76 246
b516968f
DB
247 if (remote_name) {
248 remote = remote_get(remote_name);
28b91f8a 249 if (!remote_has_url(remote, dest)) {
b516968f
DB
250 die("Destination %s is not a uri for %s",
251 dest, remote_name);
252 }
253 }
254
391b1f20
JK
255 if (progress == -1)
256 progress = !args.quiet && isatty(2);
257 args.progress = progress;
8d32e60d 258
de1a2fdd
SP
259 if (args.stateless_rpc) {
260 conn = NULL;
261 fd[0] = 0;
262 fd[1] = 1;
263 } else {
5a277f3f 264 conn = git_connect(fd, dest, receivepack,
de1a2fdd
SP
265 args.verbose ? CONNECT_VERBOSE : 0);
266 }
96249c04 267
ad6ac124
BW
268 packet_reader_init(&reader, fd[0], NULL, 0,
269 PACKET_READ_CHOMP_NEWLINE |
2d103c31
MS
270 PACKET_READ_GENTLE_ON_EOF |
271 PACKET_READ_DIE_ON_ERR_PACKET);
ad6ac124
BW
272
273 switch (discover_version(&reader)) {
8f6982b4
BW
274 case protocol_v2:
275 die("support for protocol v2 not implemented yet");
276 break;
ad6ac124
BW
277 case protocol_v1:
278 case protocol_v0:
279 get_remote_heads(&reader, &remote_refs, REF_NORMAL,
280 &extra_have, &shallow);
281 break;
282 case protocol_unknown_version:
283 BUG("unknown protocol version");
284 }
64fcef2d 285
64fcef2d 286 local_refs = get_local_heads();
96249c04 287
64fcef2d
DB
288 flags = MATCH_REFS_NONE;
289
290 if (send_all)
291 flags |= MATCH_REFS_ALL;
292 if (args.send_mirror)
293 flags |= MATCH_REFS_MIRROR;
294
295 /* match them up */
5c7ec846 296 if (match_push_refs(local_refs, &remote_refs, &rs, flags))
64fcef2d 297 return -1;
96249c04 298
91048a95
JH
299 if (!is_empty_cas(&cas))
300 apply_push_cas(&cas, remote, remote_refs);
301
20e8b465
TRC
302 set_ref_status_for_push(remote_refs, args.send_mirror,
303 args.force_update);
304
64fcef2d 305 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 306
de1a2fdd
SP
307 if (helper_status)
308 print_helper_status(remote_refs);
309
64fcef2d 310 close(fd[1]);
7f8e9828 311 close(fd[0]);
64fcef2d 312
98158e9c 313 ret |= finish_connect(conn);
64fcef2d 314
de1a2fdd 315 if (!helper_status)
10643d4e 316 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
64fcef2d
DB
317
318 if (!args.dry_run && remote) {
319 struct ref *ref;
320 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 321 transport_update_tracking_ref(remote, ref, args.verbose);
64fcef2d
DB
322 }
323
f1863d0d 324 if (!ret && !transport_refs_pushed(remote_refs))
64fcef2d
DB
325 fprintf(stderr, "Everything up-to-date\n");
326
327 return ret;
61221472 328}