]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/send-pack.c
Git 2.14.4
[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"
13eb4626 14#include "sha1-array.h"
d830d395 15#include "gpg-interface.h"
068c77a5 16#include "gettext.h"
61221472 17
068c77a5
DB
18static const char * const send_pack_usage[] = {
19 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
20 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
21 "[<host>:]<directory> [<ref>...]\n"
22 " --all and explicit <ref> specification are mutually exclusive."),
23 NULL,
24};
96249c04 25
6828f72f 26static struct send_pack_args args;
61221472 27
de1a2fdd
SP
28static void print_helper_status(struct ref *ref)
29{
30 struct strbuf buf = STRBUF_INIT;
31
32 for (; ref; ref = ref->next) {
33 const char *msg = NULL;
34 const char *res;
35
36 switch(ref->status) {
37 case REF_STATUS_NONE:
38 res = "error";
39 msg = "no match";
40 break;
41
42 case REF_STATUS_OK:
43 res = "ok";
44 break;
45
46 case REF_STATUS_UPTODATE:
47 res = "ok";
48 msg = "up to date";
49 break;
50
51 case REF_STATUS_REJECT_NONFASTFORWARD:
52 res = "error";
53 msg = "non-fast forward";
dbfeddb1
CR
54 break;
55
75e5c0dc
JH
56 case REF_STATUS_REJECT_FETCH_FIRST:
57 res = "error";
58 msg = "fetch first";
59 break;
60
61 case REF_STATUS_REJECT_NEEDS_FORCE:
62 res = "error";
63 msg = "needs force";
64 break;
65
631b5ef2
JH
66 case REF_STATUS_REJECT_STALE:
67 res = "error";
68 msg = "stale info";
69 break;
70
dbfeddb1
CR
71 case REF_STATUS_REJECT_ALREADY_EXISTS:
72 res = "error";
73 msg = "already exists";
de1a2fdd
SP
74 break;
75
76 case REF_STATUS_REJECT_NODELETE:
77 case REF_STATUS_REMOTE_REJECT:
78 res = "error";
79 break;
80
81 case REF_STATUS_EXPECTING_REPORT:
82 default:
83 continue;
84 }
85
86 strbuf_reset(&buf);
87 strbuf_addf(&buf, "%s %s", res, ref->name);
88 if (ref->remote_status)
89 msg = ref->remote_status;
90 if (msg) {
91 strbuf_addch(&buf, ' ');
92 quote_two_c_style(&buf, "", msg, 0);
93 }
94 strbuf_addch(&buf, '\n');
95
cdf4fb8e 96 write_or_die(1, buf.buf, buf.len);
de1a2fdd
SP
97 }
98 strbuf_release(&buf);
99}
100
68c757f2
DB
101static int send_pack_config(const char *k, const char *v, void *cb)
102{
103 git_gpg_config(k, v, NULL);
104
105 if (!strcmp(k, "push.gpgsign")) {
106 const char *value;
107 if (!git_config_get_value("push.gpgsign", &value)) {
108 switch (git_config_maybe_bool("push.gpgsign", value)) {
109 case 0:
110 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
111 break;
112 case 1:
113 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
114 break;
115 default:
116 if (value && !strcasecmp(value, "if-asked"))
117 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
118 else
119 return error("Invalid value for '%s'", k);
120 }
121 }
122 }
123 return 0;
124}
125
96249c04 126int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 127{
64fcef2d
DB
128 int i, nr_refspecs = 0;
129 const char **refspecs = NULL;
96249c04 130 const char *remote_name = NULL;
b516968f 131 struct remote *remote = NULL;
96249c04 132 const char *dest = NULL;
64fcef2d
DB
133 int fd[2];
134 struct child_process *conn;
910650d2 135 struct oid_array extra_have = OID_ARRAY_INIT;
136 struct oid_array shallow = OID_ARRAY_INIT;
6d2bf96e 137 struct ref *remote_refs, *local_refs;
64fcef2d 138 int ret;
de1a2fdd 139 int helper_status = 0;
64fcef2d 140 int send_all = 0;
068c77a5 141 int verbose = 0;
64fcef2d 142 const char *receivepack = "git-receive-pack";
068c77a5
DB
143 unsigned dry_run = 0;
144 unsigned send_mirror = 0;
145 unsigned force_update = 0;
146 unsigned quiet = 0;
30261094 147 int push_cert = 0;
511155db 148 struct string_list push_options = STRING_LIST_INIT_NODUP;
068c77a5
DB
149 unsigned use_thin_pack = 0;
150 unsigned atomic = 0;
151 unsigned stateless_rpc = 0;
64fcef2d 152 int flags;
10643d4e 153 unsigned int reject_reasons;
391b1f20 154 int progress = -1;
26be19ba 155 int from_stdin = 0;
28f5d176 156 struct push_cas_option cas = {0};
84a9b58c 157
068c77a5
DB
158 struct option options[] = {
159 OPT__VERBOSITY(&verbose),
160 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
161 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
162 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
163 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
164 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
165 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
166 OPT_BOOL('f', "force", &force_update, N_("force updates")),
30261094
DB
167 { OPTION_CALLBACK,
168 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
169 PARSE_OPT_OPTARG, option_parse_push_signed },
511155db
BW
170 OPT_STRING_LIST(0, "push-option", &push_options,
171 N_("server-specific"),
172 N_("option to transmit")),
068c77a5
DB
173 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
174 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
175 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
176 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
177 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
178 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
179 { OPTION_CALLBACK,
180 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
181 N_("require old value of ref to be at this value"),
182 PARSE_OPT_OPTARG, parseopt_push_cas_option },
183 OPT_END()
184 };
61221472 185
68c757f2 186 git_config(send_pack_config, NULL);
068c77a5
DB
187 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
188 if (argc > 0) {
189 dest = argv[0];
190 refspecs = (const char **)(argv + 1);
191 nr_refspecs = argc - 1;
61221472 192 }
068c77a5 193
61221472 194 if (!dest)
068c77a5
DB
195 usage_with_options(send_pack_usage, options);
196
197 args.verbose = verbose;
198 args.dry_run = dry_run;
199 args.send_mirror = send_mirror;
200 args.force_update = force_update;
201 args.quiet = quiet;
202 args.push_cert = push_cert;
203 args.progress = progress;
204 args.use_thin_pack = use_thin_pack;
205 args.atomic = atomic;
206 args.stateless_rpc = stateless_rpc;
511155db 207 args.push_options = push_options.nr ? &push_options : NULL;
26be19ba
JK
208
209 if (from_stdin) {
210 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
211
212 for (i = 0; i < nr_refspecs; i++)
213 argv_array_push(&all_refspecs, refspecs[i]);
214
215 if (args.stateless_rpc) {
216 const char *buf;
217 while ((buf = packet_read_line(0, NULL)))
218 argv_array_push(&all_refspecs, buf);
219 } else {
220 struct strbuf line = STRBUF_INIT;
933bea92 221 while (strbuf_getline(&line, stdin) != EOF)
26be19ba
JK
222 argv_array_push(&all_refspecs, line.buf);
223 strbuf_release(&line);
224 }
225
226 refspecs = all_refspecs.argv;
227 nr_refspecs = all_refspecs.argc;
228 }
229
28b9d6e5
AW
230 /*
231 * --all and --mirror are incompatible; neither makes sense
232 * with any refspecs.
233 */
c6777563 234 if ((nr_refspecs > 0 && (send_all || args.send_mirror)) ||
64fcef2d 235 (send_all && args.send_mirror))
068c77a5 236 usage_with_options(send_pack_usage, options);
37adac76 237
b516968f
DB
238 if (remote_name) {
239 remote = remote_get(remote_name);
28b91f8a 240 if (!remote_has_url(remote, dest)) {
b516968f
DB
241 die("Destination %s is not a uri for %s",
242 dest, remote_name);
243 }
244 }
245
391b1f20
JK
246 if (progress == -1)
247 progress = !args.quiet && isatty(2);
248 args.progress = progress;
8d32e60d 249
de1a2fdd
SP
250 if (args.stateless_rpc) {
251 conn = NULL;
252 fd[0] = 0;
253 fd[1] = 1;
254 } else {
5a277f3f 255 conn = git_connect(fd, dest, receivepack,
de1a2fdd
SP
256 args.verbose ? CONNECT_VERBOSE : 0);
257 }
96249c04 258
b016918b
NTND
259 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
260 &extra_have, &shallow);
64fcef2d 261
f1863d0d 262 transport_verify_remote_names(nr_refspecs, refspecs);
64fcef2d
DB
263
264 local_refs = get_local_heads();
96249c04 265
64fcef2d
DB
266 flags = MATCH_REFS_NONE;
267
268 if (send_all)
269 flags |= MATCH_REFS_ALL;
270 if (args.send_mirror)
271 flags |= MATCH_REFS_MIRROR;
272
273 /* match them up */
29753cdd 274 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
64fcef2d 275 return -1;
96249c04 276
91048a95
JH
277 if (!is_empty_cas(&cas))
278 apply_push_cas(&cas, remote, remote_refs);
279
20e8b465
TRC
280 set_ref_status_for_push(remote_refs, args.send_mirror,
281 args.force_update);
282
64fcef2d 283 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 284
de1a2fdd
SP
285 if (helper_status)
286 print_helper_status(remote_refs);
287
64fcef2d 288 close(fd[1]);
7f8e9828 289 close(fd[0]);
64fcef2d 290
98158e9c 291 ret |= finish_connect(conn);
64fcef2d 292
de1a2fdd 293 if (!helper_status)
10643d4e 294 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
64fcef2d
DB
295
296 if (!args.dry_run && remote) {
297 struct ref *ref;
298 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 299 transport_update_tracking_ref(remote, ref, args.verbose);
64fcef2d
DB
300 }
301
f1863d0d 302 if (!ret && !transport_refs_pushed(remote_refs))
64fcef2d
DB
303 fprintf(stderr, "Everything up-to-date\n");
304
305 return ret;
61221472 306}