Merge branch 'dl/doc-stash-remove-mention-of-reflog'
[git.git] / builtin / send-pack.c
blob2b9610f12176a7d23df3dfea9765e595f0405c41
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"
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,
27 static struct send_pack_args args;
29 static void print_helper_status(struct ref *ref)
31 struct strbuf buf = STRBUF_INIT;
33 for (; ref; ref = ref->next) {
34 const char *msg = NULL;
35 const char *res;
37 switch(ref->status) {
38 case REF_STATUS_NONE:
39 res = "error";
40 msg = "no match";
41 break;
43 case REF_STATUS_OK:
44 res = "ok";
45 break;
47 case REF_STATUS_UPTODATE:
48 res = "ok";
49 msg = "up to date";
50 break;
52 case REF_STATUS_REJECT_NONFASTFORWARD:
53 res = "error";
54 msg = "non-fast forward";
55 break;
57 case REF_STATUS_REJECT_FETCH_FIRST:
58 res = "error";
59 msg = "fetch first";
60 break;
62 case REF_STATUS_REJECT_NEEDS_FORCE:
63 res = "error";
64 msg = "needs force";
65 break;
67 case REF_STATUS_REJECT_STALE:
68 res = "error";
69 msg = "stale info";
70 break;
72 case REF_STATUS_REJECT_ALREADY_EXISTS:
73 res = "error";
74 msg = "already exists";
75 break;
77 case REF_STATUS_REJECT_NODELETE:
78 case REF_STATUS_REMOTE_REJECT:
79 res = "error";
80 break;
82 case REF_STATUS_EXPECTING_REPORT:
83 default:
84 continue;
87 strbuf_reset(&buf);
88 strbuf_addf(&buf, "%s %s", res, ref->name);
89 if (ref->remote_status)
90 msg = ref->remote_status;
91 if (msg) {
92 strbuf_addch(&buf, ' ');
93 quote_two_c_style(&buf, "", msg, 0);
95 strbuf_addch(&buf, '\n');
97 write_or_die(1, buf.buf, buf.len);
99 strbuf_release(&buf);
102 static int send_pack_config(const char *k, const char *v, void *cb)
104 git_gpg_config(k, v, NULL);
106 if (!strcmp(k, "push.gpgsign")) {
107 const char *value;
108 if (!git_config_get_value("push.gpgsign", &value)) {
109 switch (git_parse_maybe_bool(value)) {
110 case 0:
111 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
112 break;
113 case 1:
114 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
115 break;
116 default:
117 if (value && !strcasecmp(value, "if-asked"))
118 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
119 else
120 return error("Invalid value for '%s'", k);
124 return git_default_config(k, v, cb);
127 int cmd_send_pack(int argc, const char **argv, const char *prefix)
129 struct refspec rs = REFSPEC_INIT_PUSH;
130 const char *remote_name = NULL;
131 struct remote *remote = NULL;
132 const char *dest = NULL;
133 int fd[2];
134 struct child_process *conn;
135 struct oid_array extra_have = OID_ARRAY_INIT;
136 struct oid_array shallow = OID_ARRAY_INIT;
137 struct ref *remote_refs, *local_refs;
138 int ret;
139 int helper_status = 0;
140 int send_all = 0;
141 int verbose = 0;
142 const char *receivepack = "git-receive-pack";
143 unsigned dry_run = 0;
144 unsigned send_mirror = 0;
145 unsigned force_update = 0;
146 unsigned quiet = 0;
147 int push_cert = 0;
148 struct string_list push_options = STRING_LIST_INIT_NODUP;
149 unsigned use_thin_pack = 0;
150 unsigned atomic = 0;
151 unsigned stateless_rpc = 0;
152 int flags;
153 unsigned int reject_reasons;
154 int progress = -1;
155 int from_stdin = 0;
156 struct push_cas_option cas = {0};
157 struct packet_reader reader;
159 struct option options[] = {
160 OPT__VERBOSITY(&verbose),
161 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
162 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
163 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
164 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
165 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
166 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
167 OPT_BOOL('f', "force", &force_update, N_("force updates")),
168 OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
169 PARSE_OPT_OPTARG, option_parse_push_signed),
170 OPT_STRING_LIST(0, "push-option", &push_options,
171 N_("server-specific"),
172 N_("option to transmit")),
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 OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
180 N_("require old value of ref to be at this value"),
181 PARSE_OPT_OPTARG, parseopt_push_cas_option),
182 OPT_END()
185 git_config(send_pack_config, NULL);
186 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
187 if (argc > 0) {
188 dest = argv[0];
189 refspec_appendn(&rs, argv + 1, argc - 1);
192 if (!dest)
193 usage_with_options(send_pack_usage, options);
195 args.verbose = verbose;
196 args.dry_run = dry_run;
197 args.send_mirror = send_mirror;
198 args.force_update = force_update;
199 args.quiet = quiet;
200 args.push_cert = push_cert;
201 args.progress = progress;
202 args.use_thin_pack = use_thin_pack;
203 args.atomic = atomic;
204 args.stateless_rpc = stateless_rpc;
205 args.push_options = push_options.nr ? &push_options : NULL;
207 if (from_stdin) {
208 if (args.stateless_rpc) {
209 const char *buf;
210 while ((buf = packet_read_line(0, NULL)))
211 refspec_append(&rs, buf);
212 } else {
213 struct strbuf line = STRBUF_INIT;
214 while (strbuf_getline(&line, stdin) != EOF)
215 refspec_append(&rs, line.buf);
216 strbuf_release(&line);
221 * --all and --mirror are incompatible; neither makes sense
222 * with any refspecs.
224 if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
225 (send_all && args.send_mirror))
226 usage_with_options(send_pack_usage, options);
228 if (remote_name) {
229 remote = remote_get(remote_name);
230 if (!remote_has_url(remote, dest)) {
231 die("Destination %s is not a uri for %s",
232 dest, remote_name);
236 if (progress == -1)
237 progress = !args.quiet && isatty(2);
238 args.progress = progress;
240 if (args.stateless_rpc) {
241 conn = NULL;
242 fd[0] = 0;
243 fd[1] = 1;
244 } else {
245 conn = git_connect(fd, dest, receivepack,
246 args.verbose ? CONNECT_VERBOSE : 0);
249 packet_reader_init(&reader, fd[0], NULL, 0,
250 PACKET_READ_CHOMP_NEWLINE |
251 PACKET_READ_GENTLE_ON_EOF |
252 PACKET_READ_DIE_ON_ERR_PACKET);
254 switch (discover_version(&reader)) {
255 case protocol_v2:
256 die("support for protocol v2 not implemented yet");
257 break;
258 case protocol_v1:
259 case protocol_v0:
260 get_remote_heads(&reader, &remote_refs, REF_NORMAL,
261 &extra_have, &shallow);
262 break;
263 case protocol_unknown_version:
264 BUG("unknown protocol version");
267 local_refs = get_local_heads();
269 flags = MATCH_REFS_NONE;
271 if (send_all)
272 flags |= MATCH_REFS_ALL;
273 if (args.send_mirror)
274 flags |= MATCH_REFS_MIRROR;
276 /* match them up */
277 if (match_push_refs(local_refs, &remote_refs, &rs, flags))
278 return -1;
280 if (!is_empty_cas(&cas))
281 apply_push_cas(&cas, remote, remote_refs);
283 set_ref_status_for_push(remote_refs, args.send_mirror,
284 args.force_update);
286 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
288 if (helper_status)
289 print_helper_status(remote_refs);
291 close(fd[1]);
292 close(fd[0]);
294 ret |= finish_connect(conn);
296 if (!helper_status)
297 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
299 if (!args.dry_run && remote) {
300 struct ref *ref;
301 for (ref = remote_refs; ref; ref = ref->next)
302 transport_update_tracking_ref(remote, ref, args.verbose);
305 if (!ret && !transport_refs_pushed(remote_refs))
306 fprintf(stderr, "Everything up-to-date\n");
308 return ret;