]>
Commit | Line | Data |
---|---|---|
61221472 | 1 | #include "cache.h" |
2a9c3fe8 | 2 | #include "commit.h" |
37fde874 | 3 | #include "tag.h" |
584c6cc9 | 4 | #include "refs.h" |
f3a3214e | 5 | #include "pkt-line.h" |
38b1c662 | 6 | #include "run-command.h" |
6b62816c | 7 | #include "remote.h" |
96249c04 | 8 | #include "send-pack.h" |
61221472 | 9 | |
2a245013 | 10 | static const char send_pack_usage[] = |
1b1dd23f | 11 | "git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n" |
18bd8821 | 12 | " --all and explicit <ref> specification are mutually exclusive."; |
96249c04 DB |
13 | |
14 | static struct send_pack_args args = { | |
15 | /* .receivepack = */ "git-receive-pack", | |
16 | }; | |
61221472 | 17 | |
c727fe2a | 18 | /* |
0ae5f98c | 19 | * Make a pack stream and spit it out into file descriptor fd |
c727fe2a | 20 | */ |
40c155ff | 21 | static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra) |
c727fe2a | 22 | { |
38b1c662 SP |
23 | /* |
24 | * The child becomes pack-objects --revs; we feed | |
25 | * the revision parameters to it via its stdin and | |
26 | * let its stdout go back to the other end. | |
27 | */ | |
96249c04 | 28 | const char *argv[] = { |
38b1c662 SP |
29 | "pack-objects", |
30 | "--all-progress", | |
31 | "--revs", | |
32 | "--stdout", | |
33 | NULL, | |
34 | NULL, | |
35 | }; | |
36 | struct child_process po; | |
40c155ff JH |
37 | int i; |
38 | char buf[42]; | |
c727fe2a | 39 | |
96249c04 DB |
40 | if (args.use_thin_pack) |
41 | argv[4] = "--thin"; | |
38b1c662 | 42 | memset(&po, 0, sizeof(po)); |
96249c04 | 43 | po.argv = argv; |
38b1c662 SP |
44 | po.in = -1; |
45 | po.out = fd; | |
46 | po.git_cmd = 1; | |
47 | if (start_command(&po)) | |
f18d244a | 48 | die("git pack-objects failed (%s)", strerror(errno)); |
c727fe2a | 49 | |
0ae5f98c JH |
50 | /* |
51 | * We feed the pack-objects we just spawned with revision | |
52 | * parameters by writing to the pipe. | |
c727fe2a | 53 | */ |
40c155ff JH |
54 | for (i = 0; i < extra->nr; i++) { |
55 | memcpy(buf + 1, sha1_to_hex(&extra->array[i][0]), 40); | |
56 | buf[0] = '^'; | |
57 | buf[41] = '\n'; | |
58 | if (!write_or_whine(po.in, buf, 42, "send-pack: send refs")) | |
59 | break; | |
60 | } | |
c727fe2a | 61 | |
40c155ff | 62 | while (refs) { |
c727fe2a AW |
63 | if (!is_null_sha1(refs->old_sha1) && |
64 | has_sha1_file(refs->old_sha1)) { | |
65 | memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40); | |
66 | buf[0] = '^'; | |
67 | buf[41] = '\n'; | |
38b1c662 | 68 | if (!write_or_whine(po.in, buf, 42, |
825cee7b AW |
69 | "send-pack: send refs")) |
70 | break; | |
c727fe2a AW |
71 | } |
72 | if (!is_null_sha1(refs->new_sha1)) { | |
73 | memcpy(buf, sha1_to_hex(refs->new_sha1), 40); | |
74 | buf[40] = '\n'; | |
38b1c662 | 75 | if (!write_or_whine(po.in, buf, 41, |
825cee7b AW |
76 | "send-pack: send refs")) |
77 | break; | |
c727fe2a AW |
78 | } |
79 | refs = refs->next; | |
80 | } | |
c727fe2a | 81 | |
e72ae288 | 82 | close(po.in); |
38b1c662 SP |
83 | if (finish_command(&po)) |
84 | return error("pack-objects died with strange error"); | |
85 | return 0; | |
94fdb7aa | 86 | } |
e4b5c7ff | 87 | |
51b0fca0 JH |
88 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
89 | { | |
90 | while (list) { | |
91 | struct commit_list *temp = list; | |
92 | temp->item->object.flags &= ~mark; | |
93 | list = temp->next; | |
94 | free(temp); | |
95 | } | |
96 | } | |
97 | ||
37fde874 JH |
98 | static int ref_newer(const unsigned char *new_sha1, |
99 | const unsigned char *old_sha1) | |
584c6cc9 | 100 | { |
37fde874 JH |
101 | struct object *o; |
102 | struct commit *old, *new; | |
51b0fca0 JH |
103 | struct commit_list *list, *used; |
104 | int found = 0; | |
2a9c3fe8 | 105 | |
37fde874 JH |
106 | /* Both new and old must be commit-ish and new is descendant of |
107 | * old. Otherwise we require --force. | |
108 | */ | |
9534f40b | 109 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
1974632c | 110 | if (!o || o->type != OBJ_COMMIT) |
584c6cc9 | 111 | return 0; |
37fde874 JH |
112 | old = (struct commit *) o; |
113 | ||
9534f40b | 114 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
1974632c | 115 | if (!o || o->type != OBJ_COMMIT) |
2a9c3fe8 | 116 | return 0; |
37fde874 JH |
117 | new = (struct commit *) o; |
118 | ||
2a9c3fe8 LT |
119 | if (parse_commit(new) < 0) |
120 | return 0; | |
51b0fca0 JH |
121 | |
122 | used = list = NULL; | |
2a9c3fe8 | 123 | commit_list_insert(new, &list); |
bdf25142 LT |
124 | while (list) { |
125 | new = pop_most_recent_commit(&list, 1); | |
51b0fca0 JH |
126 | commit_list_insert(new, &used); |
127 | if (new == old) { | |
128 | found = 1; | |
129 | break; | |
130 | } | |
2a9c3fe8 | 131 | } |
51b0fca0 JH |
132 | unmark_and_free(list, 1); |
133 | unmark_and_free(used, 1); | |
134 | return found; | |
584c6cc9 LT |
135 | } |
136 | ||
f88395ac JH |
137 | static struct ref *local_refs, **local_tail; |
138 | static struct ref *remote_refs, **remote_tail; | |
584c6cc9 | 139 | |
8da19775 | 140 | static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
584c6cc9 LT |
141 | { |
142 | struct ref *ref; | |
30affa1e JK |
143 | int len; |
144 | ||
145 | /* we already know it starts with refs/ to get here */ | |
146 | if (check_ref_format(refname + 5)) | |
147 | return 0; | |
148 | ||
149 | len = strlen(refname) + 1; | |
f88395ac | 150 | ref = xcalloc(1, sizeof(*ref) + len); |
e702496e | 151 | hashcpy(ref->new_sha1, sha1); |
584c6cc9 | 152 | memcpy(ref->name, refname, len); |
f88395ac JH |
153 | *local_tail = ref; |
154 | local_tail = &ref->next; | |
584c6cc9 LT |
155 | return 0; |
156 | } | |
157 | ||
f88395ac JH |
158 | static void get_local_heads(void) |
159 | { | |
160 | local_tail = &local_refs; | |
cb5d709f | 161 | for_each_ref(one_local_ref, NULL); |
f88395ac JH |
162 | } |
163 | ||
ca74c458 JK |
164 | static int receive_status(int in, struct ref *refs) |
165 | { | |
166 | struct ref *hint; | |
cfee10a7 JH |
167 | char line[1000]; |
168 | int ret = 0; | |
169 | int len = packet_read_line(in, line, sizeof(line)); | |
2a0fe89a JK |
170 | if (len < 10 || memcmp(line, "unpack ", 7)) |
171 | return error("did not receive remote status"); | |
cfee10a7 | 172 | if (memcmp(line, "unpack ok\n", 10)) { |
2a0fe89a JK |
173 | char *p = line + strlen(line) - 1; |
174 | if (*p == '\n') | |
175 | *p = '\0'; | |
176 | error("unpack failed: %s", line + 7); | |
cfee10a7 JH |
177 | ret = -1; |
178 | } | |
ca74c458 | 179 | hint = NULL; |
cfee10a7 | 180 | while (1) { |
2a0fe89a JK |
181 | char *refname; |
182 | char *msg; | |
cfee10a7 JH |
183 | len = packet_read_line(in, line, sizeof(line)); |
184 | if (!len) | |
185 | break; | |
186 | if (len < 3 || | |
2a0fe89a | 187 | (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) { |
cfee10a7 JH |
188 | fprintf(stderr, "protocol error: %s\n", line); |
189 | ret = -1; | |
190 | break; | |
191 | } | |
2a0fe89a JK |
192 | |
193 | line[strlen(line)-1] = '\0'; | |
194 | refname = line + 3; | |
195 | msg = strchr(refname, ' '); | |
196 | if (msg) | |
197 | *msg++ = '\0'; | |
198 | ||
199 | /* first try searching at our hint, falling back to all refs */ | |
ca74c458 | 200 | if (hint) |
2a0fe89a | 201 | hint = find_ref_by_name(hint, refname); |
ca74c458 | 202 | if (!hint) |
2a0fe89a JK |
203 | hint = find_ref_by_name(refs, refname); |
204 | if (!hint) { | |
205 | warning("remote reported status on unknown ref: %s", | |
206 | refname); | |
207 | continue; | |
208 | } | |
209 | if (hint->status != REF_STATUS_EXPECTING_REPORT) { | |
210 | warning("remote reported status on unexpected ref: %s", | |
211 | refname); | |
212 | continue; | |
213 | } | |
214 | ||
215 | if (line[0] == 'o' && line[1] == 'k') | |
216 | hint->status = REF_STATUS_OK; | |
217 | else { | |
218 | hint->status = REF_STATUS_REMOTE_REJECT; | |
219 | ret = -1; | |
220 | } | |
221 | if (msg) | |
222 | hint->remote_status = xstrdup(msg); | |
223 | /* start our next search from the next ref */ | |
224 | hint = hint->next; | |
cfee10a7 JH |
225 | } |
226 | return ret; | |
227 | } | |
228 | ||
334f4831 JK |
229 | static void update_tracking_ref(struct remote *remote, struct ref *ref) |
230 | { | |
231 | struct refspec rs; | |
334f4831 | 232 | |
16ed2f48 | 233 | if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE) |
334f4831 JK |
234 | return; |
235 | ||
1f0e2a1a JK |
236 | rs.src = ref->name; |
237 | rs.dst = NULL; | |
334f4831 JK |
238 | |
239 | if (!remote_find_tracking(remote, &rs)) { | |
b50fa2bd JK |
240 | if (args.verbose) |
241 | fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); | |
1f0e2a1a | 242 | if (ref->deletion) { |
eca35a25 | 243 | delete_ref(rs.dst, NULL, 0); |
334f4831 JK |
244 | } else |
245 | update_ref("update by push", rs.dst, | |
246 | ref->new_sha1, NULL, 0, 0); | |
247 | free(rs.dst); | |
248 | } | |
249 | } | |
250 | ||
8736a848 | 251 | static const char *prettify_ref(const struct ref *ref) |
f7673490 | 252 | { |
8736a848 | 253 | const char *name = ref->name; |
f7673490 JK |
254 | return name + ( |
255 | !prefixcmp(name, "refs/heads/") ? 11 : | |
256 | !prefixcmp(name, "refs/tags/") ? 10 : | |
257 | !prefixcmp(name, "refs/remotes/") ? 13 : | |
258 | 0); | |
259 | } | |
260 | ||
261 | #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3) | |
262 | ||
8736a848 JK |
263 | static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg) |
264 | { | |
265 | fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary); | |
266 | if (from) | |
267 | fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to)); | |
268 | else | |
269 | fputs(prettify_ref(to), stderr); | |
270 | if (msg) { | |
271 | fputs(" (", stderr); | |
272 | fputs(msg, stderr); | |
273 | fputc(')', stderr); | |
274 | } | |
275 | fputc('\n', stderr); | |
276 | } | |
277 | ||
278 | static const char *status_abbrev(unsigned char sha1[20]) | |
279 | { | |
2efb3b06 | 280 | return find_unique_abbrev(sha1, DEFAULT_ABBREV); |
8736a848 JK |
281 | } |
282 | ||
283 | static void print_ok_ref_status(struct ref *ref) | |
284 | { | |
285 | if (ref->deletion) | |
286 | print_ref_status('-', "[deleted]", ref, NULL, NULL); | |
287 | else if (is_null_sha1(ref->old_sha1)) | |
288 | print_ref_status('*', | |
289 | (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" : | |
290 | "[new branch]"), | |
291 | ref, ref->peer_ref, NULL); | |
292 | else { | |
293 | char quickref[84]; | |
294 | char type; | |
295 | const char *msg; | |
296 | ||
297 | strcpy(quickref, status_abbrev(ref->old_sha1)); | |
298 | if (ref->nonfastforward) { | |
299 | strcat(quickref, "..."); | |
300 | type = '+'; | |
301 | msg = "forced update"; | |
302 | } else { | |
303 | strcat(quickref, ".."); | |
304 | type = ' '; | |
305 | msg = NULL; | |
306 | } | |
307 | strcat(quickref, status_abbrev(ref->new_sha1)); | |
308 | ||
309 | print_ref_status(type, quickref, ref, ref->peer_ref, msg); | |
310 | } | |
311 | } | |
312 | ||
07f50715 JK |
313 | static int print_one_push_status(struct ref *ref, const char *dest, int count) |
314 | { | |
315 | if (!count) | |
316 | fprintf(stderr, "To %s\n", dest); | |
317 | ||
318 | switch(ref->status) { | |
319 | case REF_STATUS_NONE: | |
320 | print_ref_status('X', "[no match]", ref, NULL, NULL); | |
321 | break; | |
322 | case REF_STATUS_REJECT_NODELETE: | |
323 | print_ref_status('!', "[rejected]", ref, NULL, | |
324 | "remote does not support deleting refs"); | |
325 | break; | |
326 | case REF_STATUS_UPTODATE: | |
327 | print_ref_status('=', "[up to date]", ref, | |
328 | ref->peer_ref, NULL); | |
329 | break; | |
330 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
331 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
332 | "non-fast forward"); | |
333 | break; | |
334 | case REF_STATUS_REMOTE_REJECT: | |
335 | print_ref_status('!', "[remote rejected]", ref, | |
336 | ref->deletion ? NULL : ref->peer_ref, | |
337 | ref->remote_status); | |
338 | break; | |
339 | case REF_STATUS_EXPECTING_REPORT: | |
340 | print_ref_status('!', "[remote failure]", ref, | |
341 | ref->deletion ? NULL : ref->peer_ref, | |
342 | "remote failed to report status"); | |
343 | break; | |
344 | case REF_STATUS_OK: | |
345 | print_ok_ref_status(ref); | |
346 | break; | |
347 | } | |
348 | ||
349 | return 1; | |
350 | } | |
351 | ||
8736a848 JK |
352 | static void print_push_status(const char *dest, struct ref *refs) |
353 | { | |
354 | struct ref *ref; | |
07f50715 | 355 | int n = 0; |
8736a848 | 356 | |
07f50715 JK |
357 | if (args.verbose) { |
358 | for (ref = refs; ref; ref = ref->next) | |
359 | if (ref->status == REF_STATUS_UPTODATE) | |
360 | n += print_one_push_status(ref, dest, n); | |
361 | } | |
8736a848 | 362 | |
07f50715 JK |
363 | for (ref = refs; ref; ref = ref->next) |
364 | if (ref->status == REF_STATUS_OK) | |
365 | n += print_one_push_status(ref, dest, n); | |
8736a848 | 366 | |
07f50715 JK |
367 | for (ref = refs; ref; ref = ref->next) { |
368 | if (ref->status != REF_STATUS_NONE && | |
369 | ref->status != REF_STATUS_UPTODATE && | |
370 | ref->status != REF_STATUS_OK) | |
371 | n += print_one_push_status(ref, dest, n); | |
8736a848 JK |
372 | } |
373 | } | |
374 | ||
73fa0b44 JK |
375 | static int refs_pushed(struct ref *ref) |
376 | { | |
377 | for (; ref; ref = ref->next) { | |
378 | switch(ref->status) { | |
379 | case REF_STATUS_NONE: | |
380 | case REF_STATUS_UPTODATE: | |
381 | break; | |
382 | default: | |
383 | return 1; | |
384 | } | |
385 | } | |
386 | return 0; | |
387 | } | |
388 | ||
f7673490 | 389 | static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec) |
61221472 | 390 | { |
7f8e9828 | 391 | struct ref *ref; |
584c6cc9 | 392 | int new_refs; |
cfee10a7 | 393 | int ask_for_status_report = 0; |
d4f694ba | 394 | int allow_deleting_refs = 0; |
cfee10a7 | 395 | int expect_status_report = 0; |
28b9d6e5 | 396 | int flags = MATCH_REFS_NONE; |
ca74c458 | 397 | int ret; |
40c155ff | 398 | struct extra_have_objects extra_have; |
28b9d6e5 | 399 | |
40c155ff | 400 | memset(&extra_have, 0, sizeof(extra_have)); |
28b9d6e5 AW |
401 | if (args.send_all) |
402 | flags |= MATCH_REFS_ALL; | |
403 | if (args.send_mirror) | |
404 | flags |= MATCH_REFS_MIRROR; | |
7f8e9828 | 405 | |
f88395ac | 406 | /* No funny business with the matcher */ |
40c155ff JH |
407 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL, |
408 | &extra_have); | |
f88395ac | 409 | get_local_heads(); |
584c6cc9 | 410 | |
cfee10a7 JH |
411 | /* Does the other end support the reporting? */ |
412 | if (server_supports("report-status")) | |
413 | ask_for_status_report = 1; | |
d4f694ba JH |
414 | if (server_supports("delete-refs")) |
415 | allow_deleting_refs = 1; | |
cfee10a7 | 416 | |
f88395ac JH |
417 | /* match them up */ |
418 | if (!remote_tail) | |
419 | remote_tail = &remote_refs; | |
420 | if (match_refs(local_refs, remote_refs, &remote_tail, | |
c20181e3 JS |
421 | nr_refspec, refspec, flags)) { |
422 | close(out); | |
f88395ac | 423 | return -1; |
c20181e3 | 424 | } |
4c353e89 DB |
425 | |
426 | if (!remote_refs) { | |
7e23b06d AC |
427 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" |
428 | "Perhaps you should specify a branch such as 'master'.\n"); | |
c20181e3 | 429 | close(out); |
4c353e89 DB |
430 | return 0; |
431 | } | |
432 | ||
584c6cc9 | 433 | /* |
f88395ac | 434 | * Finally, tell the other end! |
584c6cc9 | 435 | */ |
f88395ac JH |
436 | new_refs = 0; |
437 | for (ref = remote_refs; ref; ref = ref->next) { | |
d4f694ba | 438 | |
16ed2f48 CB |
439 | if (ref->peer_ref) |
440 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); | |
441 | else if (!args.send_mirror) | |
442 | continue; | |
28b9d6e5 | 443 | |
16ed2f48 | 444 | ref->deletion = is_null_sha1(ref->new_sha1); |
8736a848 JK |
445 | if (ref->deletion && !allow_deleting_refs) { |
446 | ref->status = REF_STATUS_REJECT_NODELETE; | |
d4f694ba JH |
447 | continue; |
448 | } | |
8736a848 | 449 | if (!ref->deletion && |
16ed2f48 | 450 | !hashcmp(ref->old_sha1, ref->new_sha1)) { |
8736a848 | 451 | ref->status = REF_STATUS_UPTODATE; |
37fde874 JH |
452 | continue; |
453 | } | |
454 | ||
455 | /* This part determines what can overwrite what. | |
456 | * The rules are: | |
457 | * | |
ff27adf3 JH |
458 | * (0) you can always use --force or +A:B notation to |
459 | * selectively force individual ref pairs. | |
37fde874 JH |
460 | * |
461 | * (1) if the old thing does not exist, it is OK. | |
462 | * | |
463 | * (2) if you do not have the old thing, you are not allowed | |
464 | * to overwrite it; you would not know what you are losing | |
465 | * otherwise. | |
466 | * | |
467 | * (3) if both new and old are commit-ish, and new is a | |
468 | * descendant of old, it is OK. | |
d4f694ba JH |
469 | * |
470 | * (4) regardless of all of the above, removing :B is | |
471 | * always allowed. | |
37fde874 JH |
472 | */ |
473 | ||
8736a848 JK |
474 | ref->nonfastforward = |
475 | !ref->deletion && | |
4b4ee90e | 476 | !is_null_sha1(ref->old_sha1) && |
8736a848 | 477 | (!has_sha1_file(ref->old_sha1) |
16ed2f48 | 478 | || !ref_newer(ref->new_sha1, ref->old_sha1)); |
8736a848 JK |
479 | |
480 | if (ref->nonfastforward && !ref->force && !args.force_update) { | |
481 | ref->status = REF_STATUS_REJECT_NONFASTFORWARD; | |
482 | continue; | |
e4b5c7ff | 483 | } |
8736a848 | 484 | |
8736a848 | 485 | if (!ref->deletion) |
d4f694ba | 486 | new_refs++; |
cfee10a7 | 487 | |
96249c04 | 488 | if (!args.dry_run) { |
8736a848 JK |
489 | char *old_hex = sha1_to_hex(ref->old_sha1); |
490 | char *new_hex = sha1_to_hex(ref->new_sha1); | |
491 | ||
a63103ae BE |
492 | if (ask_for_status_report) { |
493 | packet_write(out, "%s %s %s%c%s", | |
494 | old_hex, new_hex, ref->name, 0, | |
495 | "report-status"); | |
496 | ask_for_status_report = 0; | |
497 | expect_status_report = 1; | |
498 | } | |
499 | else | |
500 | packet_write(out, "%s %s %s", | |
501 | old_hex, new_hex, ref->name); | |
cfee10a7 | 502 | } |
2a0fe89a JK |
503 | ref->status = expect_status_report ? |
504 | REF_STATUS_EXPECTING_REPORT : | |
505 | REF_STATUS_OK; | |
61221472 | 506 | } |
f88395ac | 507 | |
f3a3214e | 508 | packet_flush(out); |
8736a848 | 509 | if (new_refs && !args.dry_run) { |
40c155ff | 510 | if (pack_objects(out, remote_refs, &extra_have) < 0) |
8736a848 | 511 | return -1; |
8736a848 | 512 | } |
c20181e3 JS |
513 | else |
514 | close(out); | |
cfee10a7 | 515 | |
2a0fe89a | 516 | if (expect_status_report) |
ca74c458 | 517 | ret = receive_status(in, remote_refs); |
ca74c458 JK |
518 | else |
519 | ret = 0; | |
520 | ||
521 | print_push_status(dest, remote_refs); | |
cfee10a7 | 522 | |
8736a848 | 523 | if (!args.dry_run && remote) { |
334f4831 | 524 | for (ref = remote_refs; ref; ref = ref->next) |
1f0e2a1a | 525 | update_tracking_ref(remote, ref); |
334f4831 JK |
526 | } |
527 | ||
73fa0b44 | 528 | if (!refs_pushed(remote_refs)) |
cfee10a7 | 529 | fprintf(stderr, "Everything up-to-date\n"); |
ca74c458 JK |
530 | if (ret < 0) |
531 | return ret; | |
8736a848 JK |
532 | for (ref = remote_refs; ref; ref = ref->next) { |
533 | switch (ref->status) { | |
534 | case REF_STATUS_NONE: | |
535 | case REF_STATUS_UPTODATE: | |
536 | case REF_STATUS_OK: | |
537 | break; | |
538 | default: | |
539 | return -1; | |
540 | } | |
541 | } | |
542 | return 0; | |
61221472 LT |
543 | } |
544 | ||
4577370e | 545 | static void verify_remote_names(int nr_heads, const char **heads) |
37adac76 JH |
546 | { |
547 | int i; | |
548 | ||
549 | for (i = 0; i < nr_heads; i++) { | |
a83619d6 | 550 | const char *local = heads[i]; |
46220ca1 | 551 | const char *remote = strrchr(heads[i], ':'); |
37adac76 | 552 | |
a83619d6 PB |
553 | if (*local == '+') |
554 | local++; | |
555 | ||
556 | /* A matching refspec is okay. */ | |
557 | if (remote == local && remote[1] == '\0') | |
558 | continue; | |
559 | ||
560 | remote = remote ? (remote + 1) : local; | |
37adac76 JH |
561 | switch (check_ref_format(remote)) { |
562 | case 0: /* ok */ | |
257f3020 JH |
563 | case CHECK_REF_FORMAT_ONELEVEL: |
564 | /* ok but a single level -- that is fine for | |
565 | * a match pattern. | |
566 | */ | |
567 | case CHECK_REF_FORMAT_WILDCARD: | |
568 | /* ok but ends with a pattern-match character */ | |
37adac76 JH |
569 | continue; |
570 | } | |
571 | die("remote part of refspec is not a valid name in %s", | |
572 | heads[i]); | |
573 | } | |
574 | } | |
f88395ac | 575 | |
96249c04 | 576 | int cmd_send_pack(int argc, const char **argv, const char *prefix) |
61221472 LT |
577 | { |
578 | int i, nr_heads = 0; | |
4577370e | 579 | const char **heads = NULL; |
96249c04 | 580 | const char *remote_name = NULL; |
b516968f | 581 | struct remote *remote = NULL; |
96249c04 | 582 | const char *dest = NULL; |
84a9b58c | 583 | |
61221472 | 584 | argv++; |
d089391c | 585 | for (i = 1; i < argc; i++, argv++) { |
96249c04 | 586 | const char *arg = *argv; |
61221472 LT |
587 | |
588 | if (*arg == '-') { | |
cc44c765 | 589 | if (!prefixcmp(arg, "--receive-pack=")) { |
96249c04 | 590 | args.receivepack = arg + 15; |
d23842fd UKK |
591 | continue; |
592 | } | |
cc44c765 | 593 | if (!prefixcmp(arg, "--exec=")) { |
96249c04 | 594 | args.receivepack = arg + 7; |
61221472 LT |
595 | continue; |
596 | } | |
b516968f DB |
597 | if (!prefixcmp(arg, "--remote=")) { |
598 | remote_name = arg + 9; | |
599 | continue; | |
600 | } | |
d089391c | 601 | if (!strcmp(arg, "--all")) { |
96249c04 | 602 | args.send_all = 1; |
d089391c LT |
603 | continue; |
604 | } | |
a63103ae | 605 | if (!strcmp(arg, "--dry-run")) { |
96249c04 | 606 | args.dry_run = 1; |
a63103ae BE |
607 | continue; |
608 | } | |
28b9d6e5 AW |
609 | if (!strcmp(arg, "--mirror")) { |
610 | args.send_mirror = 1; | |
611 | continue; | |
612 | } | |
2a9c3fe8 | 613 | if (!strcmp(arg, "--force")) { |
96249c04 | 614 | args.force_update = 1; |
2a9c3fe8 LT |
615 | continue; |
616 | } | |
41f93a2c | 617 | if (!strcmp(arg, "--verbose")) { |
96249c04 | 618 | args.verbose = 1; |
41f93a2c LT |
619 | continue; |
620 | } | |
2245be3e | 621 | if (!strcmp(arg, "--thin")) { |
96249c04 | 622 | args.use_thin_pack = 1; |
2245be3e JH |
623 | continue; |
624 | } | |
61221472 LT |
625 | usage(send_pack_usage); |
626 | } | |
d089391c LT |
627 | if (!dest) { |
628 | dest = arg; | |
629 | continue; | |
630 | } | |
4577370e | 631 | heads = (const char **) argv; |
d089391c | 632 | nr_heads = argc - i; |
61221472 LT |
633 | break; |
634 | } | |
635 | if (!dest) | |
636 | usage(send_pack_usage); | |
28b9d6e5 AW |
637 | /* |
638 | * --all and --mirror are incompatible; neither makes sense | |
639 | * with any refspecs. | |
640 | */ | |
641 | if ((heads && (args.send_all || args.send_mirror)) || | |
642 | (args.send_all && args.send_mirror)) | |
0bc3cdfc | 643 | usage(send_pack_usage); |
37adac76 | 644 | |
b516968f DB |
645 | if (remote_name) { |
646 | remote = remote_get(remote_name); | |
28b91f8a | 647 | if (!remote_has_url(remote, dest)) { |
b516968f DB |
648 | die("Destination %s is not a uri for %s", |
649 | dest, remote_name); | |
650 | } | |
651 | } | |
652 | ||
96249c04 DB |
653 | return send_pack(&args, dest, remote, nr_heads, heads); |
654 | } | |
655 | ||
656 | int send_pack(struct send_pack_args *my_args, | |
657 | const char *dest, struct remote *remote, | |
658 | int nr_heads, const char **heads) | |
659 | { | |
660 | int fd[2], ret; | |
661 | struct child_process *conn; | |
662 | ||
663 | memcpy(&args, my_args, sizeof(args)); | |
664 | ||
665 | verify_remote_names(nr_heads, heads); | |
666 | ||
667 | conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0); | |
f7673490 | 668 | ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads); |
7f8e9828 | 669 | close(fd[0]); |
c20181e3 | 670 | /* do_send_pack always closes fd[1] */ |
98158e9c | 671 | ret |= finish_connect(conn); |
8a5dbef8 | 672 | return !!ret; |
61221472 | 673 | } |