]>
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" |
77cb17e9 | 6 | #include "exec_cmd.h" |
61221472 | 7 | |
2a245013 | 8 | static const char send_pack_usage[] = |
0bc3cdfc JH |
9 | "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n" |
10 | " --all and explicit <head> specification are mutually exclusive."; | |
61221472 | 11 | static const char *exec = "git-receive-pack"; |
96f1e58f DR |
12 | static int verbose; |
13 | static int send_all; | |
14 | static int force_update; | |
15 | static int use_thin_pack; | |
61221472 | 16 | |
584c6cc9 LT |
17 | static int is_zero_sha1(const unsigned char *sha1) |
18 | { | |
19 | int i; | |
20 | ||
21 | for (i = 0; i < 20; i++) { | |
22 | if (*sha1++) | |
23 | return 0; | |
24 | } | |
25 | return 1; | |
26 | } | |
27 | ||
94fdb7aa LT |
28 | static void exec_pack_objects(void) |
29 | { | |
9201c707 | 30 | static const char *args[] = { |
77cb17e9 | 31 | "pack-objects", |
fa438a2e | 32 | "--all-progress", |
94fdb7aa LT |
33 | "--stdout", |
34 | NULL | |
35 | }; | |
77cb17e9 | 36 | execv_git_cmd(args); |
94fdb7aa LT |
37 | die("git-pack-objects exec failed (%s)", strerror(errno)); |
38 | } | |
39 | ||
40 | static void exec_rev_list(struct ref *refs) | |
41 | { | |
c727fe2a AW |
42 | static const char *args[4]; |
43 | int i = 0; | |
94fdb7aa | 44 | |
77cb17e9 | 45 | args[i++] = "rev-list"; /* 0 */ |
2245be3e JH |
46 | if (use_thin_pack) /* 1 */ |
47 | args[i++] = "--objects-edge"; | |
48 | else | |
49 | args[i++] = "--objects"; | |
797656e5 | 50 | |
c727fe2a | 51 | args[i++] = "--stdin"; |
797656e5 | 52 | |
94fdb7aa | 53 | args[i] = NULL; |
77cb17e9 | 54 | execv_git_cmd(args); |
94fdb7aa LT |
55 | die("git-rev-list exec failed (%s)", strerror(errno)); |
56 | } | |
57 | ||
c727fe2a AW |
58 | /* |
59 | * Run "rev-list --stdin | pack-objects" pipe. | |
60 | */ | |
e40a9e2c | 61 | static void rev_list(struct ref *refs) |
94fdb7aa LT |
62 | { |
63 | int pipe_fd[2]; | |
64 | pid_t pack_objects_pid; | |
65 | ||
66 | if (pipe(pipe_fd) < 0) | |
67 | die("rev-list setup: pipe failed"); | |
68 | pack_objects_pid = fork(); | |
69 | if (!pack_objects_pid) { | |
c727fe2a AW |
70 | /* The child becomes pack-objects; reads from pipe |
71 | * and writes to the original fd | |
72 | */ | |
94fdb7aa | 73 | dup2(pipe_fd[0], 0); |
94fdb7aa LT |
74 | close(pipe_fd[0]); |
75 | close(pipe_fd[1]); | |
94fdb7aa LT |
76 | exec_pack_objects(); |
77 | die("pack-objects setup failed"); | |
78 | } | |
79 | if (pack_objects_pid < 0) | |
80 | die("pack-objects fork failed"); | |
c727fe2a AW |
81 | |
82 | /* We become rev-list --stdin; output goes to pipe. */ | |
94fdb7aa LT |
83 | dup2(pipe_fd[1], 1); |
84 | close(pipe_fd[0]); | |
85 | close(pipe_fd[1]); | |
94fdb7aa LT |
86 | exec_rev_list(refs); |
87 | } | |
88 | ||
c727fe2a AW |
89 | /* |
90 | * Create "rev-list --stdin | pack-objects" pipe and feed | |
91 | * the refs into the pipeline. | |
92 | */ | |
93 | static void rev_list_generate(int fd, struct ref *refs) | |
94 | { | |
95 | int pipe_fd[2]; | |
96 | pid_t rev_list_generate_pid; | |
97 | ||
98 | if (pipe(pipe_fd) < 0) | |
99 | die("rev-list-generate setup: pipe failed"); | |
100 | rev_list_generate_pid = fork(); | |
101 | if (!rev_list_generate_pid) { | |
102 | /* The child becomes the "rev-list | pack-objects" | |
103 | * pipeline. It takes input from us, and its output | |
104 | * goes to fd. | |
105 | */ | |
106 | dup2(pipe_fd[0], 0); | |
107 | dup2(fd, 1); | |
108 | close(pipe_fd[0]); | |
109 | close(pipe_fd[1]); | |
110 | close(fd); | |
e40a9e2c | 111 | rev_list(refs); |
c727fe2a AW |
112 | die("rev-list setup failed"); |
113 | } | |
114 | if (rev_list_generate_pid < 0) | |
115 | die("rev-list-generate fork failed"); | |
116 | ||
117 | /* We feed the rev parameters to them. We do not write into | |
118 | * fd nor read from the pipe. | |
119 | */ | |
120 | close(pipe_fd[0]); | |
121 | close(fd); | |
122 | while (refs) { | |
123 | char buf[42]; | |
124 | ||
125 | if (!is_null_sha1(refs->old_sha1) && | |
126 | has_sha1_file(refs->old_sha1)) { | |
127 | memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40); | |
128 | buf[0] = '^'; | |
129 | buf[41] = '\n'; | |
130 | write(pipe_fd[1], buf, 42); | |
131 | } | |
132 | if (!is_null_sha1(refs->new_sha1)) { | |
133 | memcpy(buf, sha1_to_hex(refs->new_sha1), 40); | |
134 | buf[40] = '\n'; | |
135 | write(pipe_fd[1], buf, 41); | |
136 | } | |
137 | refs = refs->next; | |
138 | } | |
139 | close(pipe_fd[1]); | |
140 | // waitpid(rev_list_generate_pid); | |
141 | exit(0); | |
142 | } | |
143 | ||
144 | /* | |
145 | * Make a pack stream and spit it out into file descriptor fd | |
146 | */ | |
aa145403 | 147 | static void pack_objects(int fd, struct ref *refs) |
94fdb7aa LT |
148 | { |
149 | pid_t rev_list_pid; | |
150 | ||
151 | rev_list_pid = fork(); | |
152 | if (!rev_list_pid) { | |
c727fe2a | 153 | rev_list_generate(fd, refs); |
94fdb7aa LT |
154 | die("rev-list setup failed"); |
155 | } | |
156 | if (rev_list_pid < 0) | |
157 | die("rev-list fork failed"); | |
158 | /* | |
159 | * We don't wait for the rev-list pipeline in the parent: | |
160 | * we end up waiting for the other end instead | |
161 | */ | |
162 | } | |
e4b5c7ff | 163 | |
51b0fca0 JH |
164 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
165 | { | |
166 | while (list) { | |
167 | struct commit_list *temp = list; | |
168 | temp->item->object.flags &= ~mark; | |
169 | list = temp->next; | |
170 | free(temp); | |
171 | } | |
172 | } | |
173 | ||
37fde874 JH |
174 | static int ref_newer(const unsigned char *new_sha1, |
175 | const unsigned char *old_sha1) | |
584c6cc9 | 176 | { |
37fde874 JH |
177 | struct object *o; |
178 | struct commit *old, *new; | |
51b0fca0 JH |
179 | struct commit_list *list, *used; |
180 | int found = 0; | |
2a9c3fe8 | 181 | |
37fde874 JH |
182 | /* Both new and old must be commit-ish and new is descendant of |
183 | * old. Otherwise we require --force. | |
184 | */ | |
9534f40b | 185 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
1974632c | 186 | if (!o || o->type != OBJ_COMMIT) |
584c6cc9 | 187 | return 0; |
37fde874 JH |
188 | old = (struct commit *) o; |
189 | ||
9534f40b | 190 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
1974632c | 191 | if (!o || o->type != OBJ_COMMIT) |
2a9c3fe8 | 192 | return 0; |
37fde874 JH |
193 | new = (struct commit *) o; |
194 | ||
2a9c3fe8 LT |
195 | if (parse_commit(new) < 0) |
196 | return 0; | |
51b0fca0 JH |
197 | |
198 | used = list = NULL; | |
2a9c3fe8 | 199 | commit_list_insert(new, &list); |
bdf25142 LT |
200 | while (list) { |
201 | new = pop_most_recent_commit(&list, 1); | |
51b0fca0 JH |
202 | commit_list_insert(new, &used); |
203 | if (new == old) { | |
204 | found = 1; | |
205 | break; | |
206 | } | |
2a9c3fe8 | 207 | } |
51b0fca0 JH |
208 | unmark_and_free(list, 1); |
209 | unmark_and_free(used, 1); | |
210 | return found; | |
584c6cc9 LT |
211 | } |
212 | ||
f88395ac JH |
213 | static struct ref *local_refs, **local_tail; |
214 | static struct ref *remote_refs, **remote_tail; | |
584c6cc9 | 215 | |
8da19775 | 216 | static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
584c6cc9 LT |
217 | { |
218 | struct ref *ref; | |
f88395ac JH |
219 | int len = strlen(refname) + 1; |
220 | ref = xcalloc(1, sizeof(*ref) + len); | |
e702496e | 221 | hashcpy(ref->new_sha1, sha1); |
584c6cc9 | 222 | memcpy(ref->name, refname, len); |
f88395ac JH |
223 | *local_tail = ref; |
224 | local_tail = &ref->next; | |
584c6cc9 LT |
225 | return 0; |
226 | } | |
227 | ||
f88395ac JH |
228 | static void get_local_heads(void) |
229 | { | |
230 | local_tail = &local_refs; | |
cb5d709f | 231 | for_each_ref(one_local_ref, NULL); |
f88395ac JH |
232 | } |
233 | ||
cfee10a7 JH |
234 | static int receive_status(int in) |
235 | { | |
236 | char line[1000]; | |
237 | int ret = 0; | |
238 | int len = packet_read_line(in, line, sizeof(line)); | |
239 | if (len < 10 || memcmp(line, "unpack ", 7)) { | |
240 | fprintf(stderr, "did not receive status back\n"); | |
241 | return -1; | |
242 | } | |
243 | if (memcmp(line, "unpack ok\n", 10)) { | |
244 | fputs(line, stderr); | |
245 | ret = -1; | |
246 | } | |
247 | while (1) { | |
248 | len = packet_read_line(in, line, sizeof(line)); | |
249 | if (!len) | |
250 | break; | |
251 | if (len < 3 || | |
252 | (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) { | |
253 | fprintf(stderr, "protocol error: %s\n", line); | |
254 | ret = -1; | |
255 | break; | |
256 | } | |
257 | if (!memcmp(line, "ok", 2)) | |
258 | continue; | |
259 | fputs(line, stderr); | |
260 | ret = -1; | |
261 | } | |
262 | return ret; | |
263 | } | |
264 | ||
f88395ac | 265 | static int send_pack(int in, int out, int nr_refspec, char **refspec) |
61221472 | 266 | { |
7f8e9828 | 267 | struct ref *ref; |
584c6cc9 | 268 | int new_refs; |
ed24928e | 269 | int ret = 0; |
cfee10a7 | 270 | int ask_for_status_report = 0; |
d4f694ba | 271 | int allow_deleting_refs = 0; |
cfee10a7 | 272 | int expect_status_report = 0; |
7f8e9828 | 273 | |
f88395ac | 274 | /* No funny business with the matcher */ |
2718ff09 | 275 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); |
f88395ac | 276 | get_local_heads(); |
584c6cc9 | 277 | |
cfee10a7 JH |
278 | /* Does the other end support the reporting? */ |
279 | if (server_supports("report-status")) | |
280 | ask_for_status_report = 1; | |
d4f694ba JH |
281 | if (server_supports("delete-refs")) |
282 | allow_deleting_refs = 1; | |
cfee10a7 | 283 | |
f88395ac JH |
284 | /* match them up */ |
285 | if (!remote_tail) | |
286 | remote_tail = &remote_refs; | |
287 | if (match_refs(local_refs, remote_refs, &remote_tail, | |
288 | nr_refspec, refspec, send_all)) | |
289 | return -1; | |
4c353e89 DB |
290 | |
291 | if (!remote_refs) { | |
292 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); | |
293 | return 0; | |
294 | } | |
295 | ||
584c6cc9 | 296 | /* |
f88395ac | 297 | * Finally, tell the other end! |
584c6cc9 | 298 | */ |
f88395ac JH |
299 | new_refs = 0; |
300 | for (ref = remote_refs; ref; ref = ref->next) { | |
301 | char old_hex[60], *new_hex; | |
d4f694ba JH |
302 | int delete_ref; |
303 | ||
f88395ac | 304 | if (!ref->peer_ref) |
7f8e9828 | 305 | continue; |
d4f694ba JH |
306 | |
307 | delete_ref = is_null_sha1(ref->peer_ref->new_sha1); | |
308 | if (delete_ref && !allow_deleting_refs) { | |
309 | error("remote does not support deleting refs"); | |
310 | ret = -2; | |
311 | continue; | |
312 | } | |
313 | if (!delete_ref && | |
314 | !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { | |
41f93a2c LT |
315 | if (verbose) |
316 | fprintf(stderr, "'%s': up-to-date\n", ref->name); | |
37fde874 JH |
317 | continue; |
318 | } | |
319 | ||
320 | /* This part determines what can overwrite what. | |
321 | * The rules are: | |
322 | * | |
ff27adf3 JH |
323 | * (0) you can always use --force or +A:B notation to |
324 | * selectively force individual ref pairs. | |
37fde874 JH |
325 | * |
326 | * (1) if the old thing does not exist, it is OK. | |
327 | * | |
328 | * (2) if you do not have the old thing, you are not allowed | |
329 | * to overwrite it; you would not know what you are losing | |
330 | * otherwise. | |
331 | * | |
332 | * (3) if both new and old are commit-ish, and new is a | |
333 | * descendant of old, it is OK. | |
d4f694ba JH |
334 | * |
335 | * (4) regardless of all of the above, removing :B is | |
336 | * always allowed. | |
37fde874 JH |
337 | */ |
338 | ||
ff27adf3 | 339 | if (!force_update && |
d4f694ba | 340 | !delete_ref && |
ff27adf3 JH |
341 | !is_zero_sha1(ref->old_sha1) && |
342 | !ref->force) { | |
69310a34 JH |
343 | if (!has_sha1_file(ref->old_sha1) || |
344 | !ref_newer(ref->peer_ref->new_sha1, | |
f88395ac | 345 | ref->old_sha1)) { |
69310a34 JH |
346 | /* We do not have the remote ref, or |
347 | * we know that the remote ref is not | |
348 | * an ancestor of what we are trying to | |
349 | * push. Either way this can be losing | |
350 | * commits at the remote end and likely | |
351 | * we were not up to date to begin with. | |
352 | */ | |
353 | error("remote '%s' is not a strict " | |
354 | "subset of local ref '%s'. " | |
355 | "maybe you are not up-to-date and " | |
356 | "need to pull first?", | |
357 | ref->name, | |
f88395ac | 358 | ref->peer_ref->name); |
ed24928e | 359 | ret = -2; |
f88395ac JH |
360 | continue; |
361 | } | |
e4b5c7ff | 362 | } |
e702496e | 363 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); |
d4f694ba JH |
364 | if (!delete_ref) |
365 | new_refs++; | |
7f8e9828 LT |
366 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); |
367 | new_hex = sha1_to_hex(ref->new_sha1); | |
cfee10a7 JH |
368 | |
369 | if (ask_for_status_report) { | |
370 | packet_write(out, "%s %s %s%c%s", | |
371 | old_hex, new_hex, ref->name, 0, | |
372 | "report-status"); | |
373 | ask_for_status_report = 0; | |
374 | expect_status_report = 1; | |
375 | } | |
376 | else | |
377 | packet_write(out, "%s %s %s", | |
378 | old_hex, new_hex, ref->name); | |
d4f694ba JH |
379 | if (delete_ref) |
380 | fprintf(stderr, "deleting '%s'\n", ref->name); | |
381 | else { | |
382 | fprintf(stderr, "updating '%s'", ref->name); | |
383 | if (strcmp(ref->name, ref->peer_ref->name)) | |
384 | fprintf(stderr, " using '%s'", | |
385 | ref->peer_ref->name); | |
386 | fprintf(stderr, "\n from %s\n to %s\n", | |
387 | old_hex, new_hex); | |
388 | } | |
61221472 | 389 | } |
f88395ac | 390 | |
f3a3214e | 391 | packet_flush(out); |
584c6cc9 | 392 | if (new_refs) |
f88395ac | 393 | pack_objects(out, remote_refs); |
61221472 | 394 | close(out); |
cfee10a7 JH |
395 | |
396 | if (expect_status_report) { | |
397 | if (receive_status(in)) | |
398 | ret = -4; | |
399 | } | |
400 | ||
401 | if (!new_refs && ret == 0) | |
402 | fprintf(stderr, "Everything up-to-date\n"); | |
ed24928e | 403 | return ret; |
61221472 LT |
404 | } |
405 | ||
37adac76 JH |
406 | static void verify_remote_names(int nr_heads, char **heads) |
407 | { | |
408 | int i; | |
409 | ||
410 | for (i = 0; i < nr_heads; i++) { | |
411 | const char *remote = strchr(heads[i], ':'); | |
412 | ||
413 | remote = remote ? (remote + 1) : heads[i]; | |
414 | switch (check_ref_format(remote)) { | |
415 | case 0: /* ok */ | |
416 | case -2: /* ok but a single level -- that is fine for | |
417 | * a match pattern. | |
418 | */ | |
419 | continue; | |
420 | } | |
421 | die("remote part of refspec is not a valid name in %s", | |
422 | heads[i]); | |
423 | } | |
424 | } | |
f88395ac | 425 | |
61221472 LT |
426 | int main(int argc, char **argv) |
427 | { | |
428 | int i, nr_heads = 0; | |
429 | char *dest = NULL; | |
430 | char **heads = NULL; | |
7f8e9828 LT |
431 | int fd[2], ret; |
432 | pid_t pid; | |
61221472 | 433 | |
5a327713 | 434 | setup_git_directory(); |
84a9b58c JH |
435 | git_config(git_default_config); |
436 | ||
61221472 | 437 | argv++; |
d089391c LT |
438 | for (i = 1; i < argc; i++, argv++) { |
439 | char *arg = *argv; | |
61221472 LT |
440 | |
441 | if (*arg == '-') { | |
442 | if (!strncmp(arg, "--exec=", 7)) { | |
443 | exec = arg + 7; | |
444 | continue; | |
445 | } | |
d089391c LT |
446 | if (!strcmp(arg, "--all")) { |
447 | send_all = 1; | |
448 | continue; | |
449 | } | |
2a9c3fe8 LT |
450 | if (!strcmp(arg, "--force")) { |
451 | force_update = 1; | |
452 | continue; | |
453 | } | |
41f93a2c LT |
454 | if (!strcmp(arg, "--verbose")) { |
455 | verbose = 1; | |
456 | continue; | |
457 | } | |
2245be3e JH |
458 | if (!strcmp(arg, "--thin")) { |
459 | use_thin_pack = 1; | |
460 | continue; | |
461 | } | |
61221472 LT |
462 | usage(send_pack_usage); |
463 | } | |
d089391c LT |
464 | if (!dest) { |
465 | dest = arg; | |
466 | continue; | |
467 | } | |
61221472 | 468 | heads = argv; |
d089391c | 469 | nr_heads = argc - i; |
61221472 LT |
470 | break; |
471 | } | |
472 | if (!dest) | |
473 | usage(send_pack_usage); | |
0bc3cdfc JH |
474 | if (heads && send_all) |
475 | usage(send_pack_usage); | |
37adac76 JH |
476 | verify_remote_names(nr_heads, heads); |
477 | ||
f7192598 | 478 | pid = git_connect(fd, dest, exec); |
7f8e9828 | 479 | if (pid < 0) |
61221472 | 480 | return 1; |
d0efc8a7 | 481 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
7f8e9828 LT |
482 | close(fd[0]); |
483 | close(fd[1]); | |
8a5dbef8 FBH |
484 | ret |= finish_connect(pid); |
485 | return !!ret; | |
61221472 | 486 | } |