1#include "builtin.h"
2#include "commit.h"
3#include "refs.h"
4#include "pkt-line.h"
5#include "sideband.h"
6#include "run-command.h"
7#include "remote.h"
8#include "connect.h"
9#include "send-pack.h"
10#include "quote.h"
11#include "transport.h"
12#include "version.h"
13#include "sha1-array.h"
14#include "gpg-interface.h"
15
16static const char send_pack_usage[] =
17"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] [<host>:]<directory> [<ref>...]\n"
18" --all and explicit <ref> specification are mutually exclusive.";
19
20static struct send_pack_args args;
21
22static void print_helper_status(struct ref *ref)
23{
24 struct strbuf buf = STRBUF_INIT;
25
26 for (; ref; ref = ref->next) {
27 const char *msg = NULL;
28 const char *res;
29
30 switch(ref->status) {
31 case REF_STATUS_NONE:
32 res = "error";
33 msg = "no match";
34 break;
35
36 case REF_STATUS_OK:
37 res = "ok";
38 break;
39
40 case REF_STATUS_UPTODATE:
41 res = "ok";
42 msg = "up to date";
43 break;
44
45 case REF_STATUS_REJECT_NONFASTFORWARD:
46 res = "error";
47 msg = "non-fast forward";
48 break;
49
50 case REF_STATUS_REJECT_FETCH_FIRST:
51 res = "error";
52 msg = "fetch first";
53 break;
54
55 case REF_STATUS_REJECT_NEEDS_FORCE:
56 res = "error";
57 msg = "needs force";
58 break;
59
60 case REF_STATUS_REJECT_STALE:
61 res = "error";
62 msg = "stale info";
63 break;
64
65 case REF_STATUS_REJECT_ALREADY_EXISTS:
66 res = "error";
67 msg = "already exists";
68 break;
69
70 case REF_STATUS_REJECT_NODELETE:
71 case REF_STATUS_REMOTE_REJECT:
72 res = "error";
73 break;
74
75 case REF_STATUS_EXPECTING_REPORT:
76 default:
77 continue;
78 }
79
80 strbuf_reset(&buf);
81 strbuf_addf(&buf, "%s %s", res, ref->name);
82 if (ref->remote_status)
83 msg = ref->remote_status;
84 if (msg) {
85 strbuf_addch(&buf, ' ');
86 quote_two_c_style(&buf, "", msg, 0);
87 }
88 strbuf_addch(&buf, '\n');
89
90 write_or_die(1, buf.buf, buf.len);
91 }
92 strbuf_release(&buf);
93}
94
95int cmd_send_pack(int argc, const char **argv, const char *prefix)
96{
97 int i, nr_refspecs = 0;
98 const char **refspecs = NULL;
99 const char *remote_name = NULL;
100 struct remote *remote = NULL;
101 const char *dest = NULL;
102 int fd[2];
103 struct child_process *conn;
104 struct sha1_array extra_have = SHA1_ARRAY_INIT;
105 struct sha1_array shallow = SHA1_ARRAY_INIT;
106 struct ref *remote_refs, *local_refs;
107 int ret;
108 int helper_status = 0;
109 int send_all = 0;
110 const char *receivepack = "git-receive-pack";
111 int flags;
112 unsigned int reject_reasons;
113 int progress = -1;
114 int from_stdin = 0;
115 struct push_cas_option cas = {0};
116
117 git_config(git_gpg_config, NULL);
118
119 argv++;
120 for (i = 1; i < argc; i++, argv++) {
121 const char *arg = *argv;
122
123 if (*arg == '-') {
124 if (starts_with(arg, "--receive-pack=")) {
125 receivepack = arg + 15;
126 continue;
127 }
128 if (starts_with(arg, "--exec=")) {
129 receivepack = arg + 7;
130 continue;
131 }
132 if (starts_with(arg, "--remote=")) {
133 remote_name = arg + 9;
134 continue;
135 }
136 if (!strcmp(arg, "--all")) {
137 send_all = 1;
138 continue;
139 }
140 if (!strcmp(arg, "--dry-run")) {
141 args.dry_run = 1;
142 continue;
143 }
144 if (!strcmp(arg, "--mirror")) {
145 args.send_mirror = 1;
146 continue;
147 }
148 if (!strcmp(arg, "--force")) {
149 args.force_update = 1;
150 continue;
151 }
152 if (!strcmp(arg, "--quiet")) {
153 args.quiet = 1;
154 continue;
155 }
156 if (!strcmp(arg, "--verbose")) {
157 args.verbose = 1;
158 continue;
159 }
160 if (!strcmp(arg, "--signed")) {
161 args.push_cert = 1;
162 continue;
163 }
164 if (!strcmp(arg, "--progress")) {
165 progress = 1;
166 continue;
167 }
168 if (!strcmp(arg, "--no-progress")) {
169 progress = 0;
170 continue;
171 }
172 if (!strcmp(arg, "--thin")) {
173 args.use_thin_pack = 1;
174 continue;
175 }
176 if (!strcmp(arg, "--atomic")) {
177 args.atomic = 1;
178 continue;
179 }
180 if (!strcmp(arg, "--stateless-rpc")) {
181 args.stateless_rpc = 1;
182 continue;
183 }
184 if (!strcmp(arg, "--stdin")) {
185 from_stdin = 1;
186 continue;
187 }
188 if (!strcmp(arg, "--helper-status")) {
189 helper_status = 1;
190 continue;
191 }
192 if (!strcmp(arg, "--" CAS_OPT_NAME)) {
193 if (parse_push_cas_option(&cas, NULL, 0) < 0)
194 exit(1);
195 continue;
196 }
197 if (!strcmp(arg, "--no-" CAS_OPT_NAME)) {
198 if (parse_push_cas_option(&cas, NULL, 1) < 0)
199 exit(1);
200 continue;
201 }
202 if (starts_with(arg, "--" CAS_OPT_NAME "=")) {
203 if (parse_push_cas_option(&cas,
204 strchr(arg, '=') + 1, 0) < 0)
205 exit(1);
206 continue;
207 }
208 usage(send_pack_usage);
209 }
210 if (!dest) {
211 dest = arg;
212 continue;
213 }
214 refspecs = (const char **) argv;
215 nr_refspecs = argc - i;
216 break;
217 }
218 if (!dest)
219 usage(send_pack_usage);
220
221 if (from_stdin) {
222 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
223
224 for (i = 0; i < nr_refspecs; i++)
225 argv_array_push(&all_refspecs, refspecs[i]);
226
227 if (args.stateless_rpc) {
228 const char *buf;
229 while ((buf = packet_read_line(0, NULL)))
230 argv_array_push(&all_refspecs, buf);
231 } else {
232 struct strbuf line = STRBUF_INIT;
233 while (strbuf_getline(&line, stdin, '\n') != EOF)
234 argv_array_push(&all_refspecs, line.buf);
235 strbuf_release(&line);
236 }
237
238 refspecs = all_refspecs.argv;
239 nr_refspecs = all_refspecs.argc;
240 }
241
242 /*
243 * --all and --mirror are incompatible; neither makes sense
244 * with any refspecs.
245 */
246 if ((refspecs && (send_all || args.send_mirror)) ||
247 (send_all && args.send_mirror))
248 usage(send_pack_usage);
249
250 if (remote_name) {
251 remote = remote_get(remote_name);
252 if (!remote_has_url(remote, dest)) {
253 die("Destination %s is not a uri for %s",
254 dest, remote_name);
255 }
256 }
257
258 if (progress == -1)
259 progress = !args.quiet && isatty(2);
260 args.progress = progress;
261
262 if (args.stateless_rpc) {
263 conn = NULL;
264 fd[0] = 0;
265 fd[1] = 1;
266 } else {
267 conn = git_connect(fd, dest, receivepack,
268 args.verbose ? CONNECT_VERBOSE : 0);
269 }
270
271 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
272 &extra_have, &shallow);
273
274 transport_verify_remote_names(nr_refspecs, refspecs);
275
276 local_refs = get_local_heads();
277
278 flags = MATCH_REFS_NONE;
279
280 if (send_all)
281 flags |= MATCH_REFS_ALL;
282 if (args.send_mirror)
283 flags |= MATCH_REFS_MIRROR;
284
285 /* match them up */
286 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
287 return -1;
288
289 if (!is_empty_cas(&cas))
290 apply_push_cas(&cas, remote, remote_refs);
291
292 set_ref_status_for_push(remote_refs, args.send_mirror,
293 args.force_update);
294
295 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
296
297 if (helper_status)
298 print_helper_status(remote_refs);
299
300 close(fd[1]);
301 close(fd[0]);
302
303 ret |= finish_connect(conn);
304
305 if (!helper_status)
306 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
307
308 if (!args.dry_run && remote) {
309 struct ref *ref;
310 for (ref = remote_refs; ref; ref = ref->next)
311 transport_update_tracking_ref(remote, ref, args.verbose);
312 }
313
314 if (!ret && !transport_refs_pushed(remote_refs))
315 fprintf(stderr, "Everything up-to-date\n");
316
317 return ret;
318}