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] [<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 struct push_cas_option cas = {0};
114
115 argv++;
116 for (i = 1; i < argc; i++, argv++) {
117 const char *arg = *argv;
118
119 if (*arg == '-') {
120 if (starts_with(arg, "--receive-pack=")) {
121 receivepack = arg + 15;
122 continue;
123 }
124 if (starts_with(arg, "--exec=")) {
125 receivepack = arg + 7;
126 continue;
127 }
128 if (starts_with(arg, "--remote=")) {
129 remote_name = arg + 9;
130 continue;
131 }
132 if (!strcmp(arg, "--all")) {
133 send_all = 1;
134 continue;
135 }
136 if (!strcmp(arg, "--dry-run")) {
137 args.dry_run = 1;
138 continue;
139 }
140 if (!strcmp(arg, "--mirror")) {
141 args.send_mirror = 1;
142 continue;
143 }
144 if (!strcmp(arg, "--force")) {
145 args.force_update = 1;
146 continue;
147 }
148 if (!strcmp(arg, "--quiet")) {
149 args.quiet = 1;
150 continue;
151 }
152 if (!strcmp(arg, "--verbose")) {
153 args.verbose = 1;
154 continue;
155 }
156 if (!strcmp(arg, "--signed")) {
157 args.push_cert = 1;
158 continue;
159 }
160 if (!strcmp(arg, "--progress")) {
161 progress = 1;
162 continue;
163 }
164 if (!strcmp(arg, "--no-progress")) {
165 progress = 0;
166 continue;
167 }
168 if (!strcmp(arg, "--thin")) {
169 args.use_thin_pack = 1;
170 continue;
171 }
172 if (!strcmp(arg, "--stateless-rpc")) {
173 args.stateless_rpc = 1;
174 continue;
175 }
176 if (!strcmp(arg, "--helper-status")) {
177 helper_status = 1;
178 continue;
179 }
180 if (!strcmp(arg, "--" CAS_OPT_NAME)) {
181 if (parse_push_cas_option(&cas, NULL, 0) < 0)
182 exit(1);
183 continue;
184 }
185 if (!strcmp(arg, "--no-" CAS_OPT_NAME)) {
186 if (parse_push_cas_option(&cas, NULL, 1) < 0)
187 exit(1);
188 continue;
189 }
190 if (starts_with(arg, "--" CAS_OPT_NAME "=")) {
191 if (parse_push_cas_option(&cas,
192 strchr(arg, '=') + 1, 0) < 0)
193 exit(1);
194 continue;
195 }
196 usage(send_pack_usage);
197 }
198 if (!dest) {
199 dest = arg;
200 continue;
201 }
202 refspecs = (const char **) argv;
203 nr_refspecs = argc - i;
204 break;
205 }
206 if (!dest)
207 usage(send_pack_usage);
208 /*
209 * --all and --mirror are incompatible; neither makes sense
210 * with any refspecs.
211 */
212 if ((refspecs && (send_all || args.send_mirror)) ||
213 (send_all && args.send_mirror))
214 usage(send_pack_usage);
215
216 if (remote_name) {
217 remote = remote_get(remote_name);
218 if (!remote_has_url(remote, dest)) {
219 die("Destination %s is not a uri for %s",
220 dest, remote_name);
221 }
222 }
223
224 if (progress == -1)
225 progress = !args.quiet && isatty(2);
226 args.progress = progress;
227
228 if (args.stateless_rpc) {
229 conn = NULL;
230 fd[0] = 0;
231 fd[1] = 1;
232 } else {
233 conn = git_connect(fd, dest, receivepack,
234 args.verbose ? CONNECT_VERBOSE : 0);
235 }
236
237 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
238 &extra_have, &shallow);
239
240 transport_verify_remote_names(nr_refspecs, refspecs);
241
242 local_refs = get_local_heads();
243
244 flags = MATCH_REFS_NONE;
245
246 if (send_all)
247 flags |= MATCH_REFS_ALL;
248 if (args.send_mirror)
249 flags |= MATCH_REFS_MIRROR;
250
251 /* match them up */
252 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
253 return -1;
254
255 if (!is_empty_cas(&cas))
256 apply_push_cas(&cas, remote, remote_refs);
257
258 set_ref_status_for_push(remote_refs, args.send_mirror,
259 args.force_update);
260
261 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
262
263 if (helper_status)
264 print_helper_status(remote_refs);
265
266 close(fd[1]);
267 close(fd[0]);
268
269 ret |= finish_connect(conn);
270
271 if (!helper_status)
272 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
273
274 if (!args.dry_run && remote) {
275 struct ref *ref;
276 for (ref = remote_refs; ref; ref = ref->next)
277 transport_update_tracking_ref(remote, ref, args.verbose);
278 }
279
280 if (!ret && !transport_refs_pushed(remote_refs))
281 fprintf(stderr, "Everything up-to-date\n");
282
283 return ret;
284}