]>
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" |
61221472 | 6 | |
2a245013 | 7 | static const char send_pack_usage[] = |
0bc3cdfc JH |
8 | "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n" |
9 | " --all and explicit <head> specification are mutually exclusive."; | |
61221472 | 10 | static const char *exec = "git-receive-pack"; |
41f93a2c | 11 | static int verbose = 0; |
d089391c | 12 | static int send_all = 0; |
2a9c3fe8 | 13 | static int force_update = 0; |
61221472 | 14 | |
584c6cc9 LT |
15 | static int is_zero_sha1(const unsigned char *sha1) |
16 | { | |
17 | int i; | |
18 | ||
19 | for (i = 0; i < 20; i++) { | |
20 | if (*sha1++) | |
21 | return 0; | |
22 | } | |
23 | return 1; | |
24 | } | |
25 | ||
94fdb7aa LT |
26 | static void exec_pack_objects(void) |
27 | { | |
28 | static char *args[] = { | |
29 | "git-pack-objects", | |
30 | "--stdout", | |
31 | NULL | |
32 | }; | |
33 | execvp("git-pack-objects", args); | |
34 | die("git-pack-objects exec failed (%s)", strerror(errno)); | |
35 | } | |
36 | ||
37 | static void exec_rev_list(struct ref *refs) | |
38 | { | |
39 | static char *args[1000]; | |
40 | int i = 0; | |
41 | ||
42 | args[i++] = "git-rev-list"; /* 0 */ | |
43 | args[i++] = "--objects"; /* 1 */ | |
44 | while (refs) { | |
45 | char *buf = malloc(100); | |
46 | if (i > 900) | |
47 | die("git-rev-list environment overflow"); | |
40b64d47 JH |
48 | if (!is_zero_sha1(refs->old_sha1) && |
49 | has_sha1_file(refs->old_sha1)) { | |
584c6cc9 LT |
50 | args[i++] = buf; |
51 | snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1)); | |
52 | buf += 50; | |
53 | } | |
54 | if (!is_zero_sha1(refs->new_sha1)) { | |
55 | args[i++] = buf; | |
56 | snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1)); | |
57 | } | |
94fdb7aa LT |
58 | refs = refs->next; |
59 | } | |
60 | args[i] = NULL; | |
61 | execvp("git-rev-list", args); | |
62 | die("git-rev-list exec failed (%s)", strerror(errno)); | |
63 | } | |
64 | ||
65 | static void rev_list(int fd, struct ref *refs) | |
66 | { | |
67 | int pipe_fd[2]; | |
68 | pid_t pack_objects_pid; | |
69 | ||
70 | if (pipe(pipe_fd) < 0) | |
71 | die("rev-list setup: pipe failed"); | |
72 | pack_objects_pid = fork(); | |
73 | if (!pack_objects_pid) { | |
74 | dup2(pipe_fd[0], 0); | |
75 | dup2(fd, 1); | |
76 | close(pipe_fd[0]); | |
77 | close(pipe_fd[1]); | |
78 | close(fd); | |
79 | exec_pack_objects(); | |
80 | die("pack-objects setup failed"); | |
81 | } | |
82 | if (pack_objects_pid < 0) | |
83 | die("pack-objects fork failed"); | |
84 | dup2(pipe_fd[1], 1); | |
85 | close(pipe_fd[0]); | |
86 | close(pipe_fd[1]); | |
87 | close(fd); | |
88 | exec_rev_list(refs); | |
89 | } | |
90 | ||
91 | static int pack_objects(int fd, struct ref *refs) | |
92 | { | |
93 | pid_t rev_list_pid; | |
94 | ||
95 | rev_list_pid = fork(); | |
96 | if (!rev_list_pid) { | |
97 | rev_list(fd, refs); | |
98 | die("rev-list setup failed"); | |
99 | } | |
100 | if (rev_list_pid < 0) | |
101 | die("rev-list fork failed"); | |
102 | /* | |
103 | * We don't wait for the rev-list pipeline in the parent: | |
104 | * we end up waiting for the other end instead | |
105 | */ | |
7ec4e608 | 106 | return 0; |
94fdb7aa | 107 | } |
e4b5c7ff | 108 | |
51b0fca0 JH |
109 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
110 | { | |
111 | while (list) { | |
112 | struct commit_list *temp = list; | |
113 | temp->item->object.flags &= ~mark; | |
114 | list = temp->next; | |
115 | free(temp); | |
116 | } | |
117 | } | |
118 | ||
37fde874 JH |
119 | static int ref_newer(const unsigned char *new_sha1, |
120 | const unsigned char *old_sha1) | |
584c6cc9 | 121 | { |
37fde874 JH |
122 | struct object *o; |
123 | struct commit *old, *new; | |
51b0fca0 JH |
124 | struct commit_list *list, *used; |
125 | int found = 0; | |
2a9c3fe8 | 126 | |
37fde874 JH |
127 | /* Both new and old must be commit-ish and new is descendant of |
128 | * old. Otherwise we require --force. | |
129 | */ | |
9534f40b | 130 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
37fde874 | 131 | if (!o || o->type != commit_type) |
584c6cc9 | 132 | return 0; |
37fde874 JH |
133 | old = (struct commit *) o; |
134 | ||
9534f40b | 135 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
37fde874 | 136 | if (!o || o->type != commit_type) |
2a9c3fe8 | 137 | return 0; |
37fde874 JH |
138 | new = (struct commit *) o; |
139 | ||
2a9c3fe8 LT |
140 | if (parse_commit(new) < 0) |
141 | return 0; | |
51b0fca0 JH |
142 | |
143 | used = list = NULL; | |
2a9c3fe8 | 144 | commit_list_insert(new, &list); |
bdf25142 LT |
145 | while (list) { |
146 | new = pop_most_recent_commit(&list, 1); | |
51b0fca0 JH |
147 | commit_list_insert(new, &used); |
148 | if (new == old) { | |
149 | found = 1; | |
150 | break; | |
151 | } | |
2a9c3fe8 | 152 | } |
51b0fca0 JH |
153 | unmark_and_free(list, 1); |
154 | unmark_and_free(used, 1); | |
155 | return found; | |
584c6cc9 LT |
156 | } |
157 | ||
f88395ac JH |
158 | static struct ref *local_refs, **local_tail; |
159 | static struct ref *remote_refs, **remote_tail; | |
584c6cc9 | 160 | |
f88395ac | 161 | static int one_local_ref(const char *refname, const unsigned char *sha1) |
584c6cc9 LT |
162 | { |
163 | struct ref *ref; | |
f88395ac JH |
164 | int len = strlen(refname) + 1; |
165 | ref = xcalloc(1, sizeof(*ref) + len); | |
584c6cc9 LT |
166 | memcpy(ref->new_sha1, sha1, 20); |
167 | memcpy(ref->name, refname, len); | |
f88395ac JH |
168 | *local_tail = ref; |
169 | local_tail = &ref->next; | |
584c6cc9 LT |
170 | return 0; |
171 | } | |
172 | ||
f88395ac JH |
173 | static void get_local_heads(void) |
174 | { | |
175 | local_tail = &local_refs; | |
176 | for_each_ref(one_local_ref); | |
177 | } | |
178 | ||
cfee10a7 JH |
179 | static int receive_status(int in) |
180 | { | |
181 | char line[1000]; | |
182 | int ret = 0; | |
183 | int len = packet_read_line(in, line, sizeof(line)); | |
184 | if (len < 10 || memcmp(line, "unpack ", 7)) { | |
185 | fprintf(stderr, "did not receive status back\n"); | |
186 | return -1; | |
187 | } | |
188 | if (memcmp(line, "unpack ok\n", 10)) { | |
189 | fputs(line, stderr); | |
190 | ret = -1; | |
191 | } | |
192 | while (1) { | |
193 | len = packet_read_line(in, line, sizeof(line)); | |
194 | if (!len) | |
195 | break; | |
196 | if (len < 3 || | |
197 | (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) { | |
198 | fprintf(stderr, "protocol error: %s\n", line); | |
199 | ret = -1; | |
200 | break; | |
201 | } | |
202 | if (!memcmp(line, "ok", 2)) | |
203 | continue; | |
204 | fputs(line, stderr); | |
205 | ret = -1; | |
206 | } | |
207 | return ret; | |
208 | } | |
209 | ||
f88395ac | 210 | static int send_pack(int in, int out, int nr_refspec, char **refspec) |
61221472 | 211 | { |
7f8e9828 | 212 | struct ref *ref; |
584c6cc9 | 213 | int new_refs; |
ed24928e | 214 | int ret = 0; |
cfee10a7 JH |
215 | int ask_for_status_report = 0; |
216 | int expect_status_report = 0; | |
7f8e9828 | 217 | |
f88395ac | 218 | /* No funny business with the matcher */ |
1a7141ff | 219 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1); |
f88395ac | 220 | get_local_heads(); |
584c6cc9 | 221 | |
cfee10a7 JH |
222 | /* Does the other end support the reporting? */ |
223 | if (server_supports("report-status")) | |
224 | ask_for_status_report = 1; | |
225 | ||
f88395ac JH |
226 | /* match them up */ |
227 | if (!remote_tail) | |
228 | remote_tail = &remote_refs; | |
229 | if (match_refs(local_refs, remote_refs, &remote_tail, | |
230 | nr_refspec, refspec, send_all)) | |
231 | return -1; | |
4c353e89 DB |
232 | |
233 | if (!remote_refs) { | |
234 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); | |
235 | return 0; | |
236 | } | |
237 | ||
584c6cc9 | 238 | /* |
f88395ac | 239 | * Finally, tell the other end! |
584c6cc9 | 240 | */ |
f88395ac JH |
241 | new_refs = 0; |
242 | for (ref = remote_refs; ref; ref = ref->next) { | |
243 | char old_hex[60], *new_hex; | |
244 | if (!ref->peer_ref) | |
7f8e9828 | 245 | continue; |
37fde874 | 246 | if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) { |
41f93a2c LT |
247 | if (verbose) |
248 | fprintf(stderr, "'%s': up-to-date\n", ref->name); | |
37fde874 JH |
249 | continue; |
250 | } | |
251 | ||
252 | /* This part determines what can overwrite what. | |
253 | * The rules are: | |
254 | * | |
ff27adf3 JH |
255 | * (0) you can always use --force or +A:B notation to |
256 | * selectively force individual ref pairs. | |
37fde874 JH |
257 | * |
258 | * (1) if the old thing does not exist, it is OK. | |
259 | * | |
260 | * (2) if you do not have the old thing, you are not allowed | |
261 | * to overwrite it; you would not know what you are losing | |
262 | * otherwise. | |
263 | * | |
264 | * (3) if both new and old are commit-ish, and new is a | |
265 | * descendant of old, it is OK. | |
266 | */ | |
267 | ||
ff27adf3 JH |
268 | if (!force_update && |
269 | !is_zero_sha1(ref->old_sha1) && | |
270 | !ref->force) { | |
69310a34 JH |
271 | if (!has_sha1_file(ref->old_sha1) || |
272 | !ref_newer(ref->peer_ref->new_sha1, | |
f88395ac | 273 | ref->old_sha1)) { |
69310a34 JH |
274 | /* We do not have the remote ref, or |
275 | * we know that the remote ref is not | |
276 | * an ancestor of what we are trying to | |
277 | * push. Either way this can be losing | |
278 | * commits at the remote end and likely | |
279 | * we were not up to date to begin with. | |
280 | */ | |
281 | error("remote '%s' is not a strict " | |
282 | "subset of local ref '%s'. " | |
283 | "maybe you are not up-to-date and " | |
284 | "need to pull first?", | |
285 | ref->name, | |
f88395ac | 286 | ref->peer_ref->name); |
ed24928e | 287 | ret = -2; |
f88395ac JH |
288 | continue; |
289 | } | |
e4b5c7ff | 290 | } |
f88395ac JH |
291 | memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20); |
292 | if (is_zero_sha1(ref->new_sha1)) { | |
293 | error("cannot happen anymore"); | |
ed24928e | 294 | ret = -3; |
584c6cc9 LT |
295 | continue; |
296 | } | |
584c6cc9 | 297 | new_refs++; |
7f8e9828 LT |
298 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); |
299 | new_hex = sha1_to_hex(ref->new_sha1); | |
cfee10a7 JH |
300 | |
301 | if (ask_for_status_report) { | |
302 | packet_write(out, "%s %s %s%c%s", | |
303 | old_hex, new_hex, ref->name, 0, | |
304 | "report-status"); | |
305 | ask_for_status_report = 0; | |
306 | expect_status_report = 1; | |
307 | } | |
308 | else | |
309 | packet_write(out, "%s %s %s", | |
310 | old_hex, new_hex, ref->name); | |
f88395ac JH |
311 | fprintf(stderr, "updating '%s'", ref->name); |
312 | if (strcmp(ref->name, ref->peer_ref->name)) | |
313 | fprintf(stderr, " using '%s'", ref->peer_ref->name); | |
314 | fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex); | |
61221472 | 315 | } |
f88395ac | 316 | |
f3a3214e | 317 | packet_flush(out); |
584c6cc9 | 318 | if (new_refs) |
f88395ac | 319 | pack_objects(out, remote_refs); |
61221472 | 320 | close(out); |
cfee10a7 JH |
321 | |
322 | if (expect_status_report) { | |
323 | if (receive_status(in)) | |
324 | ret = -4; | |
325 | } | |
326 | ||
327 | if (!new_refs && ret == 0) | |
328 | fprintf(stderr, "Everything up-to-date\n"); | |
ed24928e | 329 | return ret; |
61221472 LT |
330 | } |
331 | ||
f88395ac | 332 | |
61221472 LT |
333 | int main(int argc, char **argv) |
334 | { | |
335 | int i, nr_heads = 0; | |
336 | char *dest = NULL; | |
337 | char **heads = NULL; | |
7f8e9828 LT |
338 | int fd[2], ret; |
339 | pid_t pid; | |
61221472 | 340 | |
5a327713 | 341 | setup_git_directory(); |
61221472 | 342 | argv++; |
d089391c LT |
343 | for (i = 1; i < argc; i++, argv++) { |
344 | char *arg = *argv; | |
61221472 LT |
345 | |
346 | if (*arg == '-') { | |
347 | if (!strncmp(arg, "--exec=", 7)) { | |
348 | exec = arg + 7; | |
349 | continue; | |
350 | } | |
d089391c LT |
351 | if (!strcmp(arg, "--all")) { |
352 | send_all = 1; | |
353 | continue; | |
354 | } | |
2a9c3fe8 LT |
355 | if (!strcmp(arg, "--force")) { |
356 | force_update = 1; | |
357 | continue; | |
358 | } | |
41f93a2c LT |
359 | if (!strcmp(arg, "--verbose")) { |
360 | verbose = 1; | |
361 | continue; | |
362 | } | |
61221472 LT |
363 | usage(send_pack_usage); |
364 | } | |
d089391c LT |
365 | if (!dest) { |
366 | dest = arg; | |
367 | continue; | |
368 | } | |
61221472 | 369 | heads = argv; |
d089391c | 370 | nr_heads = argc - i; |
61221472 LT |
371 | break; |
372 | } | |
373 | if (!dest) | |
374 | usage(send_pack_usage); | |
0bc3cdfc JH |
375 | if (heads && send_all) |
376 | usage(send_pack_usage); | |
f7192598 | 377 | pid = git_connect(fd, dest, exec); |
7f8e9828 | 378 | if (pid < 0) |
61221472 | 379 | return 1; |
d0efc8a7 | 380 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
7f8e9828 LT |
381 | close(fd[0]); |
382 | close(fd[1]); | |
f7192598 | 383 | finish_connect(pid); |
7f8e9828 | 384 | return ret; |
61221472 | 385 | } |