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