5
#include "run-command.h"
14
#include "parse-options.h"
15
#include "write-or-die.h"
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>...)"),
26
static struct send_pack_args args;
28
static void print_helper_status(struct ref *ref)
30
struct strbuf buf = STRBUF_INIT;
31
struct ref_push_report *report;
33
for (; ref; ref = ref->next) {
34
const char *msg = NULL;
48
case REF_STATUS_UPTODATE:
53
case REF_STATUS_REJECT_NONFASTFORWARD:
55
msg = "non-fast forward";
58
case REF_STATUS_REJECT_FETCH_FIRST:
63
case REF_STATUS_REJECT_NEEDS_FORCE:
68
case REF_STATUS_REJECT_STALE:
73
case REF_STATUS_REJECT_REMOTE_UPDATED:
75
msg = "remote ref updated since checkout";
78
case REF_STATUS_REJECT_ALREADY_EXISTS:
80
msg = "already exists";
83
case REF_STATUS_REJECT_NODELETE:
84
case REF_STATUS_REMOTE_REJECT:
88
case REF_STATUS_EXPECTING_REPORT:
90
msg = "expecting report";
98
strbuf_addf(&buf, "%s %s", res, ref->name);
99
if (ref->remote_status)
100
msg = ref->remote_status;
102
strbuf_addch(&buf, ' ');
103
quote_two_c_style(&buf, "", msg, 0);
105
strbuf_addch(&buf, '\n');
107
if (ref->status == REF_STATUS_OK) {
108
for (report = ref->report; report; report = report->next) {
110
strbuf_addf(&buf, "ok %s\n", ref->name);
111
if (report->ref_name)
112
strbuf_addf(&buf, "option refname %s\n",
115
strbuf_addf(&buf, "option old-oid %s\n",
116
oid_to_hex(report->old_oid));
118
strbuf_addf(&buf, "option new-oid %s\n",
119
oid_to_hex(report->new_oid));
120
if (report->forced_update)
121
strbuf_addstr(&buf, "option forced-update\n");
124
write_or_die(1, buf.buf, buf.len);
126
strbuf_release(&buf);
129
static int send_pack_config(const char *k, const char *v,
130
const struct config_context *ctx, void *cb)
132
if (!strcmp(k, "push.gpgsign")) {
133
switch (git_parse_maybe_bool(v)) {
135
args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
138
args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
141
if (!strcasecmp(v, "if-asked"))
142
args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
144
return error(_("invalid value for '%s'"), k);
147
return git_default_config(k, v, ctx, cb);
150
int cmd_send_pack(int argc, const char **argv, const char *prefix)
152
struct refspec rs = REFSPEC_INIT_PUSH;
153
const char *remote_name = NULL;
154
struct remote *remote = NULL;
155
const char *dest = NULL;
157
struct child_process *conn;
158
struct oid_array extra_have = OID_ARRAY_INIT;
159
struct oid_array shallow = OID_ARRAY_INIT;
160
struct ref *remote_refs, *local_refs;
162
int helper_status = 0;
165
const char *receivepack = "git-receive-pack";
166
unsigned dry_run = 0;
167
unsigned send_mirror = 0;
168
unsigned force_update = 0;
171
struct string_list push_options = STRING_LIST_INIT_NODUP;
172
unsigned use_thin_pack = 0;
174
unsigned stateless_rpc = 0;
176
unsigned int reject_reasons;
179
struct push_cas_option cas = {0};
180
int force_if_includes = 0;
181
struct packet_reader reader;
183
struct option options[] = {
184
OPT__VERBOSITY(&verbose),
185
OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
186
OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
187
OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
188
OPT_BOOL(0, "all", &send_all, N_("push all refs")),
189
OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
190
OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
191
OPT_BOOL('f', "force", &force_update, N_("force updates")),
192
OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
193
PARSE_OPT_OPTARG, option_parse_push_signed),
194
OPT_STRING_LIST(0, "push-option", &push_options,
195
N_("server-specific"),
196
N_("option to transmit")),
197
OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
198
OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
199
OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
200
OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
201
OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
202
OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
203
OPT_CALLBACK_F(0, "force-with-lease", &cas, N_("<refname>:<expect>"),
204
N_("require old value of ref to be at this value"),
205
PARSE_OPT_OPTARG, parseopt_push_cas_option),
206
OPT_BOOL(0, TRANS_OPT_FORCE_IF_INCLUDES, &force_if_includes,
207
N_("require remote updates to be integrated locally")),
211
git_config(send_pack_config, NULL);
212
argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
215
refspec_appendn(&rs, argv + 1, argc - 1);
219
usage_with_options(send_pack_usage, options);
221
args.verbose = verbose;
222
args.dry_run = dry_run;
223
args.send_mirror = send_mirror;
224
args.force_update = force_update;
226
args.push_cert = push_cert;
227
args.progress = progress;
228
args.use_thin_pack = use_thin_pack;
229
args.atomic = atomic;
230
args.stateless_rpc = stateless_rpc;
231
args.push_options = push_options.nr ? &push_options : NULL;
235
if (args.stateless_rpc) {
237
while ((buf = packet_read_line(0, NULL)))
238
refspec_append(&rs, buf);
240
struct strbuf line = STRBUF_INIT;
241
while (strbuf_getline(&line, stdin) != EOF)
242
refspec_append(&rs, line.buf);
243
strbuf_release(&line);
248
* --all and --mirror are incompatible; neither makes sense
251
if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
252
(send_all && args.send_mirror))
253
usage_with_options(send_pack_usage, options);
256
remote = remote_get(remote_name);
257
if (!remote_has_url(remote, dest)) {
258
die("Destination %s is not a uri for %s",
264
progress = !args.quiet && isatty(2);
265
args.progress = progress;
267
if (args.stateless_rpc) {
272
conn = git_connect(fd, dest, "git-receive-pack", receivepack,
273
args.verbose ? CONNECT_VERBOSE : 0);
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);
281
switch (discover_version(&reader)) {
283
die("support for protocol v2 not implemented yet");
287
get_remote_heads(&reader, &remote_refs, REF_NORMAL,
288
&extra_have, &shallow);
290
case protocol_unknown_version:
291
BUG("unknown protocol version");
294
local_refs = get_local_heads();
296
flags = MATCH_REFS_NONE;
299
flags |= MATCH_REFS_ALL;
300
if (args.send_mirror)
301
flags |= MATCH_REFS_MIRROR;
304
if (match_push_refs(local_refs, &remote_refs, &rs, flags))
307
if (!is_empty_cas(&cas))
308
apply_push_cas(&cas, remote, remote_refs);
310
if (!is_empty_cas(&cas) && force_if_includes)
311
cas.use_force_if_includes = 1;
313
set_ref_status_for_push(remote_refs, args.send_mirror,
316
ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
319
print_helper_status(remote_refs);
324
ret |= finish_connect(conn);
327
transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
329
if (!args.dry_run && remote) {
331
for (ref = remote_refs; ref; ref = ref->next)
332
transport_update_tracking_ref(remote, ref, args.verbose);
335
if (!ret && !transport_refs_pushed(remote_refs))
336
/* stable plumbing output; do not modify or localize */
337
fprintf(stderr, "Everything up-to-date\n");
339
free_refs(remote_refs);
340
free_refs(local_refs);