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