blob: 2478e1851a355f48f2af228e46934aa3c8fc1c9d [file] [log] [blame]
Linus Torvalds61221472005-06-29 19:09:05 -07001#include "cache.h"
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -04002#include "commit.h"
Linus Torvalds584c6cc2005-07-08 13:58:40 -07003#include "refs.h"
Linus Torvaldsf3a32142005-06-29 20:50:15 -07004#include "pkt-line.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07005#include "sideband.h"
Shawn O. Pearce38b1c662007-03-12 19:00:29 -04006#include "run-command.h"
Daniel Barkalow6b628162007-05-12 11:45:59 -04007#include "remote.h"
Daniel Barkalow96249c02007-10-29 22:03:39 -04008#include "send-pack.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07009#include "quote.h"
Linus Torvalds61221472005-06-29 19:09:05 -070010
Junio C Hamano2a245012005-07-14 00:10:05 -070011static const char send_pack_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020012"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
Uwe Kleine-König18bd8822007-01-19 13:43:00 +010013" --all and explicit <ref> specification are mutually exclusive.";
Daniel Barkalow96249c02007-10-29 22:03:39 -040014
Brandon Casey6828f722009-03-26 21:37:53 -050015static struct send_pack_args args;
Linus Torvalds61221472005-06-29 19:09:05 -070016
Junio C Hamano02322e12009-01-27 20:21:31 -080017static int feed_object(const unsigned char *sha1, int fd, int negative)
18{
19 char buf[42];
20
21 if (negative && !has_sha1_file(sha1))
22 return 1;
23
24 memcpy(buf + negative, sha1_to_hex(sha1), 40);
25 if (negative)
26 buf[0] = '^';
27 buf[40 + negative] = '\n';
28 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
29}
30
Andy Whitcroftc727fe22006-09-05 22:52:12 +010031/*
Junio C Hamano0ae5f982006-12-31 01:26:53 -080032 * Make a pack stream and spit it out into file descriptor fd
Andy Whitcroftc727fe22006-09-05 22:52:12 +010033 */
Daniel Barkalow64fcef22009-03-08 21:06:07 -040034static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070035{
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040036 /*
37 * The child becomes pack-objects --revs; we feed
38 * the revision parameters to it via its stdin and
39 * let its stdout go back to the other end.
40 */
Daniel Barkalow96249c02007-10-29 22:03:39 -040041 const char *argv[] = {
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040042 "pack-objects",
Nicolas Pitre4f366272009-11-23 12:43:50 -050043 "--all-progress-implied",
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040044 "--revs",
45 "--stdout",
46 NULL,
47 NULL,
Nicolas Pitreb74fce12009-05-01 16:56:47 -040048 NULL,
Jeff King12070322009-08-05 16:22:36 -040049 NULL,
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040050 };
51 struct child_process po;
Junio C Hamano40c155f2008-09-09 01:27:09 -070052 int i;
Linus Torvalds94fdb7a2005-06-30 10:17:39 -070053
Nicolas Pitreb74fce12009-05-01 16:56:47 -040054 i = 4;
Daniel Barkalow64fcef22009-03-08 21:06:07 -040055 if (args->use_thin_pack)
Nicolas Pitreb74fce12009-05-01 16:56:47 -040056 argv[i++] = "--thin";
57 if (args->use_ofs_delta)
58 argv[i++] = "--delta-base-offset";
Jeff King12070322009-08-05 16:22:36 -040059 if (args->quiet)
60 argv[i++] = "-q";
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040061 memset(&po, 0, sizeof(po));
Daniel Barkalow96249c02007-10-29 22:03:39 -040062 po.argv = argv;
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040063 po.in = -1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070064 po.out = args->stateless_rpc ? -1 : fd;
Shawn O. Pearce38b1c662007-03-12 19:00:29 -040065 po.git_cmd = 1;
66 if (start_command(&po))
Thomas Rastd824cbb2009-06-27 17:58:46 +020067 die_errno("git pack-objects failed");
Andy Whitcroftc727fe22006-09-05 22:52:12 +010068
Junio C Hamano0ae5f982006-12-31 01:26:53 -080069 /*
70 * We feed the pack-objects we just spawned with revision
71 * parameters by writing to the pipe.
Andy Whitcroftc727fe22006-09-05 22:52:12 +010072 */
Junio C Hamano02322e12009-01-27 20:21:31 -080073 for (i = 0; i < extra->nr; i++)
74 if (!feed_object(extra->array[i], po.in, 1))
Junio C Hamano40c155f2008-09-09 01:27:09 -070075 break;
Andy Whitcroftc727fe22006-09-05 22:52:12 +010076
Junio C Hamano40c155f2008-09-09 01:27:09 -070077 while (refs) {
Andy Whitcroftc727fe22006-09-05 22:52:12 +010078 if (!is_null_sha1(refs->old_sha1) &&
Junio C Hamano02322e12009-01-27 20:21:31 -080079 !feed_object(refs->old_sha1, po.in, 1))
80 break;
81 if (!is_null_sha1(refs->new_sha1) &&
82 !feed_object(refs->new_sha1, po.in, 0))
83 break;
Andy Whitcroftc727fe22006-09-05 22:52:12 +010084 refs = refs->next;
85 }
Andy Whitcroftc727fe22006-09-05 22:52:12 +010086
Johannes Sixte72ae282008-02-16 18:36:38 +010087 close(po.in);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070088
89 if (args->stateless_rpc) {
90 char *buf = xmalloc(LARGE_PACKET_MAX);
91 while (1) {
92 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
93 if (n <= 0)
94 break;
95 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
96 }
97 free(buf);
98 close(po.out);
99 po.out = -1;
100 }
101
Shawn O. Pearce38b1c662007-03-12 19:00:29 -0400102 if (finish_command(&po))
103 return error("pack-objects died with strange error");
104 return 0;
Linus Torvalds94fdb7a2005-06-30 10:17:39 -0700105}
Linus Torvaldse4b5c7f2005-06-29 22:31:41 -0700106
Jeff Kingca74c452007-11-17 07:56:03 -0500107static int receive_status(int in, struct ref *refs)
108{
109 struct ref *hint;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800110 char line[1000];
111 int ret = 0;
112 int len = packet_read_line(in, line, sizeof(line));
Jeff King2a0fe892007-11-18 02:16:52 -0500113 if (len < 10 || memcmp(line, "unpack ", 7))
114 return error("did not receive remote status");
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800115 if (memcmp(line, "unpack ok\n", 10)) {
Jeff King2a0fe892007-11-18 02:16:52 -0500116 char *p = line + strlen(line) - 1;
117 if (*p == '\n')
118 *p = '\0';
119 error("unpack failed: %s", line + 7);
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800120 ret = -1;
121 }
Jeff Kingca74c452007-11-17 07:56:03 -0500122 hint = NULL;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800123 while (1) {
Jeff King2a0fe892007-11-18 02:16:52 -0500124 char *refname;
125 char *msg;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800126 len = packet_read_line(in, line, sizeof(line));
127 if (!len)
128 break;
129 if (len < 3 ||
Jeff King2a0fe892007-11-18 02:16:52 -0500130 (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800131 fprintf(stderr, "protocol error: %s\n", line);
132 ret = -1;
133 break;
134 }
Jeff King2a0fe892007-11-18 02:16:52 -0500135
136 line[strlen(line)-1] = '\0';
137 refname = line + 3;
138 msg = strchr(refname, ' ');
139 if (msg)
140 *msg++ = '\0';
141
142 /* first try searching at our hint, falling back to all refs */
Jeff Kingca74c452007-11-17 07:56:03 -0500143 if (hint)
Jeff King2a0fe892007-11-18 02:16:52 -0500144 hint = find_ref_by_name(hint, refname);
Jeff Kingca74c452007-11-17 07:56:03 -0500145 if (!hint)
Jeff King2a0fe892007-11-18 02:16:52 -0500146 hint = find_ref_by_name(refs, refname);
147 if (!hint) {
148 warning("remote reported status on unknown ref: %s",
149 refname);
150 continue;
151 }
152 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
153 warning("remote reported status on unexpected ref: %s",
154 refname);
155 continue;
156 }
157
158 if (line[0] == 'o' && line[1] == 'k')
159 hint->status = REF_STATUS_OK;
160 else {
161 hint->status = REF_STATUS_REMOTE_REJECT;
162 ret = -1;
163 }
164 if (msg)
165 hint->remote_status = xstrdup(msg);
166 /* start our next search from the next ref */
167 hint = hint->next;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800168 }
169 return ret;
170}
171
Jeff King334f4832007-10-18 02:19:15 -0400172static void update_tracking_ref(struct remote *remote, struct ref *ref)
173{
174 struct refspec rs;
Jeff King1f0e2a12007-11-17 07:55:15 -0500175
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100176 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
Jeff King1f0e2a12007-11-17 07:55:15 -0500177 return;
Jeff King334f4832007-10-18 02:19:15 -0400178
179 rs.src = ref->name;
180 rs.dst = NULL;
181
Jeff King334f4832007-10-18 02:19:15 -0400182 if (!remote_find_tracking(remote, &rs)) {
Jeff Kingb50fa2b2007-11-05 00:12:18 -0500183 if (args.verbose)
184 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
Jeff King1f0e2a12007-11-17 07:55:15 -0500185 if (ref->deletion) {
Miklos Vajnaeca35a22008-10-26 03:33:56 +0100186 delete_ref(rs.dst, NULL, 0);
Jeff King334f4832007-10-18 02:19:15 -0400187 } else
188 update_ref("update by push", rs.dst,
189 ref->new_sha1, NULL, 0, 0);
190 free(rs.dst);
191 }
192}
193
Jeff Kingf7673492007-11-05 00:11:15 -0500194#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
195
Jeff King8736a842007-11-17 07:54:27 -0500196static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
197{
198 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
199 if (from)
Felipe Contreras4577e482009-05-14 00:22:04 +0300200 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
Jeff King8736a842007-11-17 07:54:27 -0500201 else
Felipe Contreras4577e482009-05-14 00:22:04 +0300202 fputs(prettify_refname(to->name), stderr);
Jeff King8736a842007-11-17 07:54:27 -0500203 if (msg) {
204 fputs(" (", stderr);
205 fputs(msg, stderr);
206 fputc(')', stderr);
207 }
208 fputc('\n', stderr);
209}
210
211static const char *status_abbrev(unsigned char sha1[20])
212{
Junio C Hamano2efb3b02008-03-01 23:43:32 -0800213 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
Jeff King8736a842007-11-17 07:54:27 -0500214}
215
216static void print_ok_ref_status(struct ref *ref)
217{
218 if (ref->deletion)
219 print_ref_status('-', "[deleted]", ref, NULL, NULL);
220 else if (is_null_sha1(ref->old_sha1))
221 print_ref_status('*',
222 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
223 "[new branch]"),
224 ref, ref->peer_ref, NULL);
225 else {
226 char quickref[84];
227 char type;
228 const char *msg;
229
230 strcpy(quickref, status_abbrev(ref->old_sha1));
231 if (ref->nonfastforward) {
232 strcat(quickref, "...");
233 type = '+';
234 msg = "forced update";
235 } else {
236 strcat(quickref, "..");
237 type = ' ';
238 msg = NULL;
239 }
240 strcat(quickref, status_abbrev(ref->new_sha1));
241
242 print_ref_status(type, quickref, ref, ref->peer_ref, msg);
243 }
244}
245
Jeff King07f50712007-11-20 06:18:01 -0500246static int print_one_push_status(struct ref *ref, const char *dest, int count)
247{
248 if (!count)
249 fprintf(stderr, "To %s\n", dest);
250
251 switch(ref->status) {
252 case REF_STATUS_NONE:
253 print_ref_status('X', "[no match]", ref, NULL, NULL);
254 break;
255 case REF_STATUS_REJECT_NODELETE:
256 print_ref_status('!', "[rejected]", ref, NULL,
257 "remote does not support deleting refs");
258 break;
259 case REF_STATUS_UPTODATE:
260 print_ref_status('=', "[up to date]", ref,
261 ref->peer_ref, NULL);
262 break;
263 case REF_STATUS_REJECT_NONFASTFORWARD:
264 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300265 "non-fast-forward");
Jeff King07f50712007-11-20 06:18:01 -0500266 break;
267 case REF_STATUS_REMOTE_REJECT:
268 print_ref_status('!', "[remote rejected]", ref,
269 ref->deletion ? NULL : ref->peer_ref,
270 ref->remote_status);
271 break;
272 case REF_STATUS_EXPECTING_REPORT:
273 print_ref_status('!', "[remote failure]", ref,
274 ref->deletion ? NULL : ref->peer_ref,
275 "remote failed to report status");
276 break;
277 case REF_STATUS_OK:
278 print_ok_ref_status(ref);
279 break;
280 }
281
282 return 1;
283}
284
Jeff King8736a842007-11-17 07:54:27 -0500285static void print_push_status(const char *dest, struct ref *refs)
286{
287 struct ref *ref;
Jeff King07f50712007-11-20 06:18:01 -0500288 int n = 0;
289
290 if (args.verbose) {
291 for (ref = refs; ref; ref = ref->next)
292 if (ref->status == REF_STATUS_UPTODATE)
293 n += print_one_push_status(ref, dest, n);
294 }
295
296 for (ref = refs; ref; ref = ref->next)
297 if (ref->status == REF_STATUS_OK)
298 n += print_one_push_status(ref, dest, n);
Jeff King8736a842007-11-17 07:54:27 -0500299
300 for (ref = refs; ref; ref = ref->next) {
Jeff King07f50712007-11-20 06:18:01 -0500301 if (ref->status != REF_STATUS_NONE &&
302 ref->status != REF_STATUS_UPTODATE &&
303 ref->status != REF_STATUS_OK)
304 n += print_one_push_status(ref, dest, n);
Jeff King8736a842007-11-17 07:54:27 -0500305 }
306}
307
Jeff King73fa0b42007-11-18 03:08:22 -0500308static int refs_pushed(struct ref *ref)
309{
310 for (; ref; ref = ref->next) {
311 switch(ref->status) {
312 case REF_STATUS_NONE:
313 case REF_STATUS_UPTODATE:
314 break;
315 default:
316 return 1;
317 }
318 }
319 return 0;
320}
321
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700322static void print_helper_status(struct ref *ref)
323{
324 struct strbuf buf = STRBUF_INIT;
325
326 for (; ref; ref = ref->next) {
327 const char *msg = NULL;
328 const char *res;
329
330 switch(ref->status) {
331 case REF_STATUS_NONE:
332 res = "error";
333 msg = "no match";
334 break;
335
336 case REF_STATUS_OK:
337 res = "ok";
338 break;
339
340 case REF_STATUS_UPTODATE:
341 res = "ok";
342 msg = "up to date";
343 break;
344
345 case REF_STATUS_REJECT_NONFASTFORWARD:
346 res = "error";
347 msg = "non-fast forward";
348 break;
349
350 case REF_STATUS_REJECT_NODELETE:
351 case REF_STATUS_REMOTE_REJECT:
352 res = "error";
353 break;
354
355 case REF_STATUS_EXPECTING_REPORT:
356 default:
357 continue;
358 }
359
360 strbuf_reset(&buf);
361 strbuf_addf(&buf, "%s %s", res, ref->name);
362 if (ref->remote_status)
363 msg = ref->remote_status;
364 if (msg) {
365 strbuf_addch(&buf, ' ');
366 quote_two_c_style(&buf, "", msg, 0);
367 }
368 strbuf_addch(&buf, '\n');
369
370 safe_write(1, buf.buf, buf.len);
371 }
372 strbuf_release(&buf);
373}
374
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800375static int sideband_demux(int in, int out, void *data)
376{
377 int *fd = data;
378 int ret = recv_sideband("send-pack", fd[0], out);
379 close(out);
380 return ret;
381}
382
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400383int send_pack(struct send_pack_args *args,
384 int fd[], struct child_process *conn,
385 struct ref *remote_refs,
386 struct extra_have_objects *extra_have)
Junio C Hamanof88395a2005-08-03 16:35:29 -0700387{
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400388 int in = fd[0];
389 int out = fd[1];
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700390 struct strbuf req_buf = STRBUF_INIT;
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400391 struct ref *ref;
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700392 int new_refs;
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800393 int allow_deleting_refs = 0;
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800394 int status_report = 0;
395 int use_sideband = 0;
396 unsigned cmds_sent = 0;
Jeff Kingca74c452007-11-17 07:56:03 -0500397 int ret;
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800398 struct async demux;
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700399
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800400 /* Does the other end support the reporting? */
401 if (server_supports("report-status"))
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800402 status_report = 1;
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800403 if (server_supports("delete-refs"))
404 allow_deleting_refs = 1;
Nicolas Pitreb74fce12009-05-01 16:56:47 -0400405 if (server_supports("ofs-delta"))
406 args->use_ofs_delta = 1;
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800407 if (server_supports("side-band-64k"))
408 use_sideband = 1;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800409
Daniel Barkalow4c353e82005-12-04 11:59:37 -0500410 if (!remote_refs) {
Andrew Clausen7e23b062007-10-16 17:16:05 -0400411 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
412 "Perhaps you should specify a branch such as 'master'.\n");
Daniel Barkalow4c353e82005-12-04 11:59:37 -0500413 return 0;
414 }
415
Linus Torvalds584c6cc2005-07-08 13:58:40 -0700416 /*
417 * Finally, tell the other end!
418 */
419 new_refs = 0;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700420 for (ref = remote_refs; ref; ref = ref->next) {
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800421
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100422 if (ref->peer_ref)
423 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400424 else if (!args->send_mirror)
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100425 continue;
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800426
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100427 ref->deletion = is_null_sha1(ref->new_sha1);
Jeff King8736a842007-11-17 07:54:27 -0500428 if (ref->deletion && !allow_deleting_refs) {
429 ref->status = REF_STATUS_REJECT_NODELETE;
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800430 continue;
431 }
Jeff King8736a842007-11-17 07:54:27 -0500432 if (!ref->deletion &&
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100433 !hashcmp(ref->old_sha1, ref->new_sha1)) {
Jeff King8736a842007-11-17 07:54:27 -0500434 ref->status = REF_STATUS_UPTODATE;
Junio C Hamano37fde872005-08-05 00:47:56 -0700435 continue;
436 }
437
438 /* This part determines what can overwrite what.
439 * The rules are:
440 *
Junio C Hamanoff27adf2005-08-24 00:40:14 -0700441 * (0) you can always use --force or +A:B notation to
442 * selectively force individual ref pairs.
Junio C Hamano37fde872005-08-05 00:47:56 -0700443 *
444 * (1) if the old thing does not exist, it is OK.
445 *
446 * (2) if you do not have the old thing, you are not allowed
447 * to overwrite it; you would not know what you are losing
448 * otherwise.
449 *
450 * (3) if both new and old are commit-ish, and new is a
451 * descendant of old, it is OK.
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800452 *
453 * (4) regardless of all of the above, removing :B is
454 * always allowed.
Junio C Hamano37fde872005-08-05 00:47:56 -0700455 */
456
Jeff King8736a842007-11-17 07:54:27 -0500457 ref->nonfastforward =
458 !ref->deletion &&
Junio C Hamano4b4ee902006-12-31 00:59:53 -0800459 !is_null_sha1(ref->old_sha1) &&
Jeff King8736a842007-11-17 07:54:27 -0500460 (!has_sha1_file(ref->old_sha1)
Clemens Buchacher16ed2f42008-11-05 21:55:54 +0100461 || !ref_newer(ref->new_sha1, ref->old_sha1));
Jeff King8736a842007-11-17 07:54:27 -0500462
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400463 if (ref->nonfastforward && !ref->force && !args->force_update) {
Jeff King8736a842007-11-17 07:54:27 -0500464 ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
465 continue;
Junio C Hamanof88395a2005-08-03 16:35:29 -0700466 }
Jeff King8736a842007-11-17 07:54:27 -0500467
Jeff King8736a842007-11-17 07:54:27 -0500468 if (!ref->deletion)
Junio C Hamanod4f694b2006-11-24 00:26:49 -0800469 new_refs++;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800470
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800471 if (args->dry_run) {
472 ref->status = REF_STATUS_OK;
473 } else {
Jeff King8736a842007-11-17 07:54:27 -0500474 char *old_hex = sha1_to_hex(ref->old_sha1);
475 char *new_hex = sha1_to_hex(ref->new_sha1);
476
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800477 if (!cmds_sent && (status_report || use_sideband)) {
478 packet_buf_write(&req_buf, "%s %s %s%c%s%s",
Brian Ewinsa63103a2007-10-11 20:32:26 +0100479 old_hex, new_hex, ref->name, 0,
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800480 status_report ? " report-status" : "",
481 use_sideband ? " side-band-64k" : "");
Brian Ewinsa63103a2007-10-11 20:32:26 +0100482 }
483 else
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700484 packet_buf_write(&req_buf, "%s %s %s",
Brian Ewinsa63103a2007-10-11 20:32:26 +0100485 old_hex, new_hex, ref->name);
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800486 ref->status = status_report ?
487 REF_STATUS_EXPECTING_REPORT :
488 REF_STATUS_OK;
489 cmds_sent++;
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800490 }
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700491 }
Junio C Hamanof88395a2005-08-03 16:35:29 -0700492
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700493 if (args->stateless_rpc) {
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800494 if (!args->dry_run && cmds_sent) {
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700495 packet_buf_flush(&req_buf);
496 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
497 }
498 } else {
499 safe_write(out, req_buf.buf, req_buf.len);
500 packet_flush(out);
501 }
502 strbuf_release(&req_buf);
503
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800504 if (use_sideband && cmds_sent) {
505 memset(&demux, 0, sizeof(demux));
506 demux.proc = sideband_demux;
507 demux.data = fd;
508 demux.out = -1;
509 if (start_async(&demux))
510 die("receive-pack: unable to fork off sideband demultiplexer");
511 in = demux.out;
512 }
513
514 if (new_refs && cmds_sent) {
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400515 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
516 for (ref = remote_refs; ref; ref = ref->next)
517 ref->status = REF_STATUS_NONE;
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800518 if (use_sideband)
519 finish_async(&demux);
Jeff King8736a842007-11-17 07:54:27 -0500520 return -1;
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400521 }
Jeff King8736a842007-11-17 07:54:27 -0500522 }
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800523 if (args->stateless_rpc && cmds_sent)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700524 packet_flush(out);
Junio C Hamanocfee10a2005-12-25 23:18:37 -0800525
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800526 if (status_report && cmds_sent)
Jeff Kingca74c452007-11-17 07:56:03 -0500527 ret = receive_status(in, remote_refs);
Jeff Kingca74c452007-11-17 07:56:03 -0500528 else
529 ret = 0;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700530 if (args->stateless_rpc)
531 packet_flush(out);
Jeff Kingca74c452007-11-17 07:56:03 -0500532
Shawn O. Pearce0c499ea2010-02-05 12:57:39 -0800533 if (use_sideband && cmds_sent) {
534 if (finish_async(&demux)) {
535 error("error in sideband demultiplexer");
536 ret = -1;
537 }
538 close(demux.out);
539 }
540
Jeff Kingca74c452007-11-17 07:56:03 -0500541 if (ret < 0)
542 return ret;
Jeff King8736a842007-11-17 07:54:27 -0500543 for (ref = remote_refs; ref; ref = ref->next) {
544 switch (ref->status) {
545 case REF_STATUS_NONE:
546 case REF_STATUS_UPTODATE:
547 case REF_STATUS_OK:
548 break;
549 default:
550 return -1;
551 }
552 }
553 return 0;
Linus Torvalds61221472005-06-29 19:09:05 -0700554}
555
Daniel Barkalow45773702007-10-29 21:05:40 -0400556static void verify_remote_names(int nr_heads, const char **heads)
Junio C Hamano37adac72006-12-13 10:30:11 -0800557{
558 int i;
559
560 for (i = 0; i < nr_heads; i++) {
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400561 const char *local = heads[i];
Junio C Hamano46220ca2008-03-20 23:34:37 -0700562 const char *remote = strrchr(heads[i], ':');
Junio C Hamano37adac72006-12-13 10:30:11 -0800563
Paolo Bonzinia83619d2008-04-28 11:32:12 -0400564 if (*local == '+')
565 local++;
566
567 /* A matching refspec is okay. */
568 if (remote == local && remote[1] == '\0')
569 continue;
570
571 remote = remote ? (remote + 1) : local;
Junio C Hamano37adac72006-12-13 10:30:11 -0800572 switch (check_ref_format(remote)) {
573 case 0: /* ok */
Junio C Hamano257f3022008-01-02 11:14:40 -0800574 case CHECK_REF_FORMAT_ONELEVEL:
575 /* ok but a single level -- that is fine for
576 * a match pattern.
577 */
578 case CHECK_REF_FORMAT_WILDCARD:
579 /* ok but ends with a pattern-match character */
Junio C Hamano37adac72006-12-13 10:30:11 -0800580 continue;
581 }
582 die("remote part of refspec is not a valid name in %s",
583 heads[i]);
584 }
585}
Junio C Hamanof88395a2005-08-03 16:35:29 -0700586
Daniel Barkalow96249c02007-10-29 22:03:39 -0400587int cmd_send_pack(int argc, const char **argv, const char *prefix)
Linus Torvalds61221472005-06-29 19:09:05 -0700588{
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400589 int i, nr_refspecs = 0;
590 const char **refspecs = NULL;
Daniel Barkalow96249c02007-10-29 22:03:39 -0400591 const char *remote_name = NULL;
Daniel Barkalowb5169682007-05-15 22:50:19 -0400592 struct remote *remote = NULL;
Daniel Barkalow96249c02007-10-29 22:03:39 -0400593 const char *dest = NULL;
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400594 int fd[2];
595 struct child_process *conn;
596 struct extra_have_objects extra_have;
Clemens Buchacher6d2bf962009-05-31 16:26:48 +0200597 struct ref *remote_refs, *local_refs;
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400598 int ret;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700599 int helper_status = 0;
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400600 int send_all = 0;
601 const char *receivepack = "git-receive-pack";
602 int flags;
Junio C Hamano84a9b582006-03-23 23:41:18 -0800603
Linus Torvalds61221472005-06-29 19:09:05 -0700604 argv++;
Linus Torvaldsd0893912005-07-16 13:26:33 -0700605 for (i = 1; i < argc; i++, argv++) {
Daniel Barkalow96249c02007-10-29 22:03:39 -0400606 const char *arg = *argv;
Linus Torvalds61221472005-06-29 19:09:05 -0700607
608 if (*arg == '-') {
Junio C Hamanocc44c762007-02-20 01:53:29 -0800609 if (!prefixcmp(arg, "--receive-pack=")) {
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400610 receivepack = arg + 15;
Uwe Kleine-Königd23842f2007-01-19 13:49:27 +0100611 continue;
612 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800613 if (!prefixcmp(arg, "--exec=")) {
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400614 receivepack = arg + 7;
Linus Torvalds61221472005-06-29 19:09:05 -0700615 continue;
616 }
Daniel Barkalowb5169682007-05-15 22:50:19 -0400617 if (!prefixcmp(arg, "--remote=")) {
618 remote_name = arg + 9;
619 continue;
620 }
Linus Torvaldsd0893912005-07-16 13:26:33 -0700621 if (!strcmp(arg, "--all")) {
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400622 send_all = 1;
Linus Torvaldsd0893912005-07-16 13:26:33 -0700623 continue;
624 }
Brian Ewinsa63103a2007-10-11 20:32:26 +0100625 if (!strcmp(arg, "--dry-run")) {
Daniel Barkalow96249c02007-10-29 22:03:39 -0400626 args.dry_run = 1;
Brian Ewinsa63103a2007-10-11 20:32:26 +0100627 continue;
628 }
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +0000629 if (!strcmp(arg, "--mirror")) {
630 args.send_mirror = 1;
631 continue;
632 }
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400633 if (!strcmp(arg, "--force")) {
Daniel Barkalow96249c02007-10-29 22:03:39 -0400634 args.force_update = 1;
Linus Torvalds2a9c3fe2005-07-19 07:03:47 -0400635 continue;
636 }
Linus Torvalds41f93a22005-12-20 18:13:02 -0800637 if (!strcmp(arg, "--verbose")) {
Daniel Barkalow96249c02007-10-29 22:03:39 -0400638 args.verbose = 1;
Linus Torvalds41f93a22005-12-20 18:13:02 -0800639 continue;
640 }
Junio C Hamano2245be32006-02-19 15:03:49 -0800641 if (!strcmp(arg, "--thin")) {
Daniel Barkalow96249c02007-10-29 22:03:39 -0400642 args.use_thin_pack = 1;
Junio C Hamano2245be32006-02-19 15:03:49 -0800643 continue;
644 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700645 if (!strcmp(arg, "--stateless-rpc")) {
646 args.stateless_rpc = 1;
647 continue;
648 }
649 if (!strcmp(arg, "--helper-status")) {
650 helper_status = 1;
651 continue;
652 }
Linus Torvalds61221472005-06-29 19:09:05 -0700653 usage(send_pack_usage);
654 }
Linus Torvaldsd0893912005-07-16 13:26:33 -0700655 if (!dest) {
656 dest = arg;
657 continue;
658 }
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400659 refspecs = (const char **) argv;
660 nr_refspecs = argc - i;
Linus Torvalds61221472005-06-29 19:09:05 -0700661 break;
662 }
663 if (!dest)
664 usage(send_pack_usage);
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +0000665 /*
666 * --all and --mirror are incompatible; neither makes sense
667 * with any refspecs.
668 */
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400669 if ((refspecs && (send_all || args.send_mirror)) ||
670 (send_all && args.send_mirror))
Junio C Hamano0bc3cdf2005-08-02 12:20:27 -0700671 usage(send_pack_usage);
Junio C Hamano37adac72006-12-13 10:30:11 -0800672
Daniel Barkalowb5169682007-05-15 22:50:19 -0400673 if (remote_name) {
674 remote = remote_get(remote_name);
Shawn O. Pearce28b91f82007-09-19 00:49:27 -0400675 if (!remote_has_url(remote, dest)) {
Daniel Barkalowb5169682007-05-15 22:50:19 -0400676 die("Destination %s is not a uri for %s",
677 dest, remote_name);
678 }
679 }
680
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700681 if (args.stateless_rpc) {
682 conn = NULL;
683 fd[0] = 0;
684 fd[1] = 1;
685 } else {
686 conn = git_connect(fd, dest, receivepack,
687 args.verbose ? CONNECT_VERBOSE : 0);
688 }
Daniel Barkalow96249c02007-10-29 22:03:39 -0400689
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400690 memset(&extra_have, 0, sizeof(extra_have));
Daniel Barkalow96249c02007-10-29 22:03:39 -0400691
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400692 get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
693 &extra_have);
Daniel Barkalow96249c02007-10-29 22:03:39 -0400694
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400695 verify_remote_names(nr_refspecs, refspecs);
Daniel Barkalow96249c02007-10-29 22:03:39 -0400696
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400697 local_refs = get_local_heads();
698
699 flags = MATCH_REFS_NONE;
700
701 if (send_all)
702 flags |= MATCH_REFS_ALL;
703 if (args.send_mirror)
704 flags |= MATCH_REFS_MIRROR;
705
706 /* match them up */
Clemens Buchacher6d2bf962009-05-31 16:26:48 +0200707 if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400708 return -1;
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400709
710 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
711
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700712 if (helper_status)
713 print_helper_status(remote_refs);
714
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400715 close(fd[1]);
Linus Torvalds7f8e9822005-06-29 22:50:48 -0700716 close(fd[0]);
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400717
Johannes Sixt98158e92007-10-19 21:47:53 +0200718 ret |= finish_connect(conn);
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400719
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700720 if (!helper_status)
721 print_push_status(dest, remote_refs);
Daniel Barkalow64fcef22009-03-08 21:06:07 -0400722
723 if (!args.dry_run && remote) {
724 struct ref *ref;
725 for (ref = remote_refs; ref; ref = ref->next)
726 update_tracking_ref(remote, ref);
727 }
728
729 if (!ret && !refs_pushed(remote_refs))
730 fprintf(stderr, "Everything up-to-date\n");
731
732 return ret;
Linus Torvalds61221472005-06-29 19:09:05 -0700733}