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