]>
Commit | Line | Data |
---|---|---|
def88e9a LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | #include "pkt-line.h" | |
958c24b1 | 4 | #include "sideband.h" |
f6b42a81 JH |
5 | #include "tag.h" |
6 | #include "object.h" | |
f0243f26 | 7 | #include "commit.h" |
77cb17e9 | 8 | #include "exec_cmd.h" |
9b8dc263 JS |
9 | #include "diff.h" |
10 | #include "revision.h" | |
11 | #include "list-objects.h" | |
cc41fa8d | 12 | #include "run-command.h" |
051e4005 | 13 | #include "sigchain.h" |
ff5effdf | 14 | #include "version.h" |
daebaa78 | 15 | #include "string-list.h" |
def88e9a | 16 | |
62b4698e | 17 | static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>"; |
def88e9a | 18 | |
937a515a JH |
19 | /* bits #0..7 in revision.h, #8..10 in commit.c */ |
20 | #define THEY_HAVE (1u << 11) | |
21 | #define OUR_REF (1u << 12) | |
22 | #define WANTED (1u << 13) | |
23 | #define COMMON_KNOWN (1u << 14) | |
24 | #define REACHABLE (1u << 15) | |
25 | ||
f53514bc JS |
26 | #define SHALLOW (1u << 16) |
27 | #define NOT_SHALLOW (1u << 17) | |
28 | #define CLIENT_SHALLOW (1u << 18) | |
390eb36b | 29 | #define HIDDEN_REF (1u << 19) |
f53514bc | 30 | |
3fbe2d54 | 31 | static unsigned long oldest_have; |
937a515a | 32 | |
3f1da57f | 33 | static int multi_ack; |
4e10cf9a | 34 | static int no_done; |
348e390b | 35 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
9462e3f5 | 36 | static int no_progress, daemon_mode; |
390eb36b | 37 | static int allow_tip_sha1_in_want; |
f0cea83f | 38 | static int shallow_nr; |
b1e9fff7 JH |
39 | static struct object_array have_obj; |
40 | static struct object_array want_obj; | |
6523078b | 41 | static struct object_array extra_edge_obj; |
96f1e58f | 42 | static unsigned int timeout; |
115dedd7 | 43 | static int keepalive = 5; |
d47f3db7 JH |
44 | /* 0 for no sideband, |
45 | * otherwise maximum packet size (up to 65520 bytes). | |
46 | */ | |
96f1e58f | 47 | static int use_sideband; |
42526b47 SP |
48 | static int advertise_refs; |
49 | static int stateless_rpc; | |
960deccb PA |
50 | |
51 | static void reset_timeout(void) | |
52 | { | |
53 | alarm(timeout); | |
54 | } | |
fb9040cc | 55 | |
583b7ea3 JH |
56 | static ssize_t send_client_data(int fd, const char *data, ssize_t sz) |
57 | { | |
958c24b1 | 58 | if (use_sideband) |
d47f3db7 | 59 | return send_sideband(1, fd, data, sz, use_sideband); |
958c24b1 JH |
60 | if (fd == 3) |
61 | /* emergency quit */ | |
62 | fd = 2; | |
63 | if (fd == 2) { | |
93822c22 | 64 | /* XXX: are we happy to lose stuff here? */ |
958c24b1 JH |
65 | xwrite(fd, data, sz); |
66 | return sz; | |
583b7ea3 | 67 | } |
cdf4fb8e JK |
68 | write_or_die(fd, data, sz); |
69 | return sz; | |
583b7ea3 JH |
70 | } |
71 | ||
fb9040cc LT |
72 | static void create_pack_file(void) |
73 | { | |
cc41fa8d | 74 | struct child_process pack_objects; |
363b7817 | 75 | char data[8193], progress[128]; |
583b7ea3 JH |
76 | char abort_msg[] = "aborting due to possible repository " |
77 | "corruption on the remote side."; | |
b1c71b72 | 78 | int buffered = -1; |
1456b043 | 79 | ssize_t sz; |
cdab4858 NTND |
80 | const char *argv[12]; |
81 | int i, arg = 0; | |
82 | FILE *pipe_fd; | |
83 | char *shallow_file = NULL; | |
75bfc6c2 | 84 | |
cdab4858 NTND |
85 | if (shallow_nr) { |
86 | shallow_file = setup_temporary_shallow(); | |
87 | argv[arg++] = "--shallow-file"; | |
88 | argv[arg++] = shallow_file; | |
f0cea83f | 89 | } |
cdab4858 NTND |
90 | argv[arg++] = "pack-objects"; |
91 | argv[arg++] = "--revs"; | |
92 | if (use_thin_pack) | |
93 | argv[arg++] = "--thin"; | |
75bfc6c2 | 94 | |
cc41fa8d JS |
95 | argv[arg++] = "--stdout"; |
96 | if (!no_progress) | |
97 | argv[arg++] = "--progress"; | |
98 | if (use_ofs_delta) | |
99 | argv[arg++] = "--delta-base-offset"; | |
348e390b SP |
100 | if (use_include_tag) |
101 | argv[arg++] = "--include-tag"; | |
cc41fa8d JS |
102 | argv[arg++] = NULL; |
103 | ||
104 | memset(&pack_objects, 0, sizeof(pack_objects)); | |
b9612197 | 105 | pack_objects.in = -1; |
cc41fa8d JS |
106 | pack_objects.out = -1; |
107 | pack_objects.err = -1; | |
108 | pack_objects.git_cmd = 1; | |
109 | pack_objects.argv = argv; | |
21edd3f1 | 110 | |
4c324c00 | 111 | if (start_command(&pack_objects)) |
7e44c935 | 112 | die("git upload-pack: unable to fork git-pack-objects"); |
b1c71b72 | 113 | |
cdab4858 | 114 | pipe_fd = xfdopen(pack_objects.in, "w"); |
f0cea83f | 115 | |
cdab4858 NTND |
116 | for (i = 0; i < want_obj.nr; i++) |
117 | fprintf(pipe_fd, "%s\n", | |
118 | sha1_to_hex(want_obj.objects[i].item->sha1)); | |
119 | fprintf(pipe_fd, "--not\n"); | |
120 | for (i = 0; i < have_obj.nr; i++) | |
121 | fprintf(pipe_fd, "%s\n", | |
122 | sha1_to_hex(have_obj.objects[i].item->sha1)); | |
123 | for (i = 0; i < extra_edge_obj.nr; i++) | |
124 | fprintf(pipe_fd, "%s\n", | |
125 | sha1_to_hex(extra_edge_obj.objects[i].item->sha1)); | |
126 | fprintf(pipe_fd, "\n"); | |
127 | fflush(pipe_fd); | |
128 | fclose(pipe_fd); | |
f0cea83f | 129 | |
cc41fa8d JS |
130 | /* We read from pack_objects.err to capture stderr output for |
131 | * progress bar, and pack_objects.out to capture the pack data. | |
b1c71b72 | 132 | */ |
b1c71b72 JH |
133 | |
134 | while (1) { | |
b1c71b72 | 135 | struct pollfd pfd[2]; |
363b7817 | 136 | int pe, pu, pollsize; |
05e95155 | 137 | int ret; |
b1c71b72 | 138 | |
0d516ada ML |
139 | reset_timeout(); |
140 | ||
b1c71b72 | 141 | pollsize = 0; |
363b7817 | 142 | pe = pu = -1; |
b1c71b72 | 143 | |
cc41fa8d JS |
144 | if (0 <= pack_objects.out) { |
145 | pfd[pollsize].fd = pack_objects.out; | |
b1c71b72 JH |
146 | pfd[pollsize].events = POLLIN; |
147 | pu = pollsize; | |
148 | pollsize++; | |
149 | } | |
cc41fa8d JS |
150 | if (0 <= pack_objects.err) { |
151 | pfd[pollsize].fd = pack_objects.err; | |
363b7817 JH |
152 | pfd[pollsize].events = POLLIN; |
153 | pe = pollsize; | |
154 | pollsize++; | |
155 | } | |
b1c71b72 | 156 | |
4c324c00 JS |
157 | if (!pollsize) |
158 | break; | |
159 | ||
05e95155 JK |
160 | ret = poll(pfd, pollsize, 1000 * keepalive); |
161 | if (ret < 0) { | |
4c324c00 JS |
162 | if (errno != EINTR) { |
163 | error("poll failed, resuming: %s", | |
164 | strerror(errno)); | |
165 | sleep(1); | |
b1c71b72 | 166 | } |
4c324c00 JS |
167 | continue; |
168 | } | |
6b59f51b NP |
169 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
170 | /* Status ready; we ship that in the side-band | |
171 | * or dump to the standard error. | |
172 | */ | |
173 | sz = xread(pack_objects.err, progress, | |
174 | sizeof(progress)); | |
175 | if (0 < sz) | |
176 | send_client_data(2, progress, sz); | |
177 | else if (sz == 0) { | |
178 | close(pack_objects.err); | |
179 | pack_objects.err = -1; | |
180 | } | |
181 | else | |
182 | goto fail; | |
183 | /* give priority to status messages */ | |
184 | continue; | |
185 | } | |
4c324c00 JS |
186 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
187 | /* Data ready; we keep the last byte to ourselves | |
188 | * in case we detect broken rev-list, so that we | |
189 | * can leave the stream corrupted. This is | |
190 | * unfortunate -- unpack-objects would happily | |
191 | * accept a valid packdata with trailing garbage, | |
192 | * so appending garbage after we pass all the | |
193 | * pack data is not good enough to signal | |
194 | * breakage to downstream. | |
195 | */ | |
196 | char *cp = data; | |
197 | ssize_t outsz = 0; | |
198 | if (0 <= buffered) { | |
199 | *cp++ = buffered; | |
200 | outsz++; | |
b1c71b72 | 201 | } |
4c324c00 JS |
202 | sz = xread(pack_objects.out, cp, |
203 | sizeof(data) - outsz); | |
204 | if (0 < sz) | |
1456b043 | 205 | ; |
4c324c00 JS |
206 | else if (sz == 0) { |
207 | close(pack_objects.out); | |
208 | pack_objects.out = -1; | |
363b7817 | 209 | } |
4c324c00 JS |
210 | else |
211 | goto fail; | |
212 | sz += outsz; | |
213 | if (1 < sz) { | |
214 | buffered = data[sz-1] & 0xFF; | |
215 | sz--; | |
b1c71b72 | 216 | } |
4c324c00 JS |
217 | else |
218 | buffered = -1; | |
219 | sz = send_client_data(1, data, sz); | |
220 | if (sz < 0) | |
b1c71b72 | 221 | goto fail; |
4c324c00 | 222 | } |
05e95155 JK |
223 | |
224 | /* | |
225 | * We hit the keepalive timeout without saying anything; send | |
226 | * an empty message on the data sideband just to let the other | |
227 | * side know we're still working on it, but don't have any data | |
228 | * yet. | |
229 | * | |
230 | * If we don't have a sideband channel, there's no room in the | |
231 | * protocol to say anything, so those clients are just out of | |
232 | * luck. | |
233 | */ | |
234 | if (!ret && use_sideband) { | |
235 | static const char buf[] = "0005\1"; | |
236 | write_or_die(1, buf, 5); | |
237 | } | |
4c324c00 | 238 | } |
b1c71b72 | 239 | |
4c324c00 | 240 | if (finish_command(&pack_objects)) { |
7e44c935 | 241 | error("git upload-pack: git-pack-objects died with error."); |
4c324c00 JS |
242 | goto fail; |
243 | } | |
cdab4858 NTND |
244 | if (shallow_file) { |
245 | if (*shallow_file) | |
246 | unlink(shallow_file); | |
247 | free(shallow_file); | |
248 | } | |
b1c71b72 | 249 | |
4c324c00 JS |
250 | /* flush the data */ |
251 | if (0 <= buffered) { | |
252 | data[0] = buffered; | |
253 | sz = send_client_data(1, data, 1); | |
254 | if (sz < 0) | |
255 | goto fail; | |
256 | fprintf(stderr, "flushed.\n"); | |
b1c71b72 | 257 | } |
4c324c00 JS |
258 | if (use_sideband) |
259 | packet_flush(1); | |
260 | return; | |
261 | ||
b1c71b72 | 262 | fail: |
583b7ea3 | 263 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
7e44c935 | 264 | die("git upload-pack: %s", abort_msg); |
fb9040cc LT |
265 | } |
266 | ||
def88e9a LT |
267 | static int got_sha1(char *hex, unsigned char *sha1) |
268 | { | |
b1e9fff7 | 269 | struct object *o; |
937a515a | 270 | int we_knew_they_have = 0; |
b1e9fff7 | 271 | |
def88e9a | 272 | if (get_sha1_hex(hex, sha1)) |
7e44c935 | 273 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
fb9040cc | 274 | if (!has_sha1_file(sha1)) |
937a515a | 275 | return -1; |
b1e9fff7 | 276 | |
a6eec126 | 277 | o = parse_object(sha1); |
b1e9fff7 JH |
278 | if (!o) |
279 | die("oops (%s)", sha1_to_hex(sha1)); | |
182a8dab | 280 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 | 281 | struct commit_list *parents; |
937a515a | 282 | struct commit *commit = (struct commit *)o; |
b1e9fff7 | 283 | if (o->flags & THEY_HAVE) |
937a515a JH |
284 | we_knew_they_have = 1; |
285 | else | |
286 | o->flags |= THEY_HAVE; | |
287 | if (!oldest_have || (commit->date < oldest_have)) | |
288 | oldest_have = commit->date; | |
289 | for (parents = commit->parents; | |
b1e9fff7 JH |
290 | parents; |
291 | parents = parents->next) | |
292 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 293 | } |
937a515a JH |
294 | if (!we_knew_they_have) { |
295 | add_object_array(o, NULL, &have_obj); | |
296 | return 1; | |
297 | } | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static int reachable(struct commit *want) | |
302 | { | |
303 | struct commit_list *work = NULL; | |
304 | ||
47e44ed1 | 305 | commit_list_insert_by_date(want, &work); |
937a515a JH |
306 | while (work) { |
307 | struct commit_list *list = work->next; | |
308 | struct commit *commit = work->item; | |
309 | free(work); | |
310 | work = list; | |
311 | ||
312 | if (commit->object.flags & THEY_HAVE) { | |
313 | want->object.flags |= COMMON_KNOWN; | |
314 | break; | |
315 | } | |
316 | if (!commit->object.parsed) | |
317 | parse_object(commit->object.sha1); | |
318 | if (commit->object.flags & REACHABLE) | |
319 | continue; | |
320 | commit->object.flags |= REACHABLE; | |
321 | if (commit->date < oldest_have) | |
322 | continue; | |
323 | for (list = commit->parents; list; list = list->next) { | |
324 | struct commit *parent = list->item; | |
325 | if (!(parent->object.flags & REACHABLE)) | |
47e44ed1 | 326 | commit_list_insert_by_date(parent, &work); |
937a515a JH |
327 | } |
328 | } | |
329 | want->object.flags |= REACHABLE; | |
330 | clear_commit_marks(want, REACHABLE); | |
331 | free_commit_list(work); | |
332 | return (want->object.flags & COMMON_KNOWN); | |
333 | } | |
334 | ||
335 | static int ok_to_give_up(void) | |
336 | { | |
337 | int i; | |
338 | ||
339 | if (!have_obj.nr) | |
340 | return 0; | |
341 | ||
342 | for (i = 0; i < want_obj.nr; i++) { | |
343 | struct object *want = want_obj.objects[i].item; | |
344 | ||
345 | if (want->flags & COMMON_KNOWN) | |
346 | continue; | |
347 | want = deref_tag(want, "a want line", 0); | |
348 | if (!want || want->type != OBJ_COMMIT) { | |
349 | /* no way to tell if this is reachable by | |
350 | * looking at the ancestry chain alone, so | |
351 | * leave a note to ourselves not to worry about | |
352 | * this object anymore. | |
353 | */ | |
354 | want_obj.objects[i].item->flags |= COMMON_KNOWN; | |
355 | continue; | |
356 | } | |
357 | if (!reachable((struct commit *)want)) | |
358 | return 0; | |
359 | } | |
fb9040cc | 360 | return 1; |
def88e9a LT |
361 | } |
362 | ||
363 | static int get_common_commits(void) | |
364 | { | |
c04c4e57 | 365 | unsigned char sha1[20]; |
78affc49 | 366 | char last_hex[41]; |
49bee717 SP |
367 | int got_common = 0; |
368 | int got_other = 0; | |
4e10cf9a | 369 | int sent_ready = 0; |
def88e9a | 370 | |
f0243f26 JS |
371 | save_commit_buffer = 0; |
372 | ||
eeefa7c9 | 373 | for (;;) { |
74543a04 | 374 | char *line = packet_read_line(0, NULL); |
960deccb | 375 | reset_timeout(); |
def88e9a | 376 | |
74543a04 | 377 | if (!line) { |
49bee717 | 378 | if (multi_ack == 2 && got_common |
4e10cf9a JH |
379 | && !got_other && ok_to_give_up()) { |
380 | sent_ready = 1; | |
49bee717 | 381 | packet_write(1, "ACK %s ready\n", last_hex); |
4e10cf9a | 382 | } |
b1e9fff7 | 383 | if (have_obj.nr == 0 || multi_ack) |
1bd8c8f0 | 384 | packet_write(1, "NAK\n"); |
4e10cf9a JH |
385 | |
386 | if (no_done && sent_ready) { | |
387 | packet_write(1, "ACK %s\n", last_hex); | |
388 | return 0; | |
389 | } | |
42526b47 SP |
390 | if (stateless_rpc) |
391 | exit(0); | |
49bee717 SP |
392 | got_common = 0; |
393 | got_other = 0; | |
def88e9a LT |
394 | continue; |
395 | } | |
cc44c765 | 396 | if (!prefixcmp(line, "have ")) { |
937a515a JH |
397 | switch (got_sha1(line+5, sha1)) { |
398 | case -1: /* they have what we do not */ | |
49bee717 | 399 | got_other = 1; |
78affc49 SP |
400 | if (multi_ack && ok_to_give_up()) { |
401 | const char *hex = sha1_to_hex(sha1); | |
4e10cf9a JH |
402 | if (multi_ack == 2) { |
403 | sent_ready = 1; | |
78affc49 | 404 | packet_write(1, "ACK %s ready\n", hex); |
4e10cf9a | 405 | } else |
78affc49 SP |
406 | packet_write(1, "ACK %s continue\n", hex); |
407 | } | |
937a515a JH |
408 | break; |
409 | default: | |
49bee717 | 410 | got_common = 1; |
78affc49 SP |
411 | memcpy(last_hex, sha1_to_hex(sha1), 41); |
412 | if (multi_ack == 2) | |
413 | packet_write(1, "ACK %s common\n", last_hex); | |
414 | else if (multi_ack) | |
415 | packet_write(1, "ACK %s continue\n", last_hex); | |
c04c4e57 | 416 | else if (have_obj.nr == 1) |
78affc49 | 417 | packet_write(1, "ACK %s\n", last_hex); |
937a515a | 418 | break; |
af2d3aa4 | 419 | } |
def88e9a LT |
420 | continue; |
421 | } | |
422 | if (!strcmp(line, "done")) { | |
b1e9fff7 | 423 | if (have_obj.nr > 0) { |
1bd8c8f0 | 424 | if (multi_ack) |
c04c4e57 | 425 | packet_write(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
426 | return 0; |
427 | } | |
def88e9a LT |
428 | packet_write(1, "NAK\n"); |
429 | return -1; | |
430 | } | |
7e44c935 | 431 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
def88e9a | 432 | } |
def88e9a LT |
433 | } |
434 | ||
390eb36b JH |
435 | static int is_our_ref(struct object *o) |
436 | { | |
437 | return o->flags & | |
438 | ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF); | |
439 | } | |
440 | ||
051e4005 JH |
441 | static void check_non_tip(void) |
442 | { | |
443 | static const char *argv[] = { | |
444 | "rev-list", "--stdin", NULL, | |
445 | }; | |
446 | static struct child_process cmd; | |
447 | struct object *o; | |
448 | char namebuf[42]; /* ^ + SHA-1 + LF */ | |
449 | int i; | |
450 | ||
451 | /* In the normal in-process case non-tip request can never happen */ | |
452 | if (!stateless_rpc) | |
453 | goto error; | |
454 | ||
455 | cmd.argv = argv; | |
456 | cmd.git_cmd = 1; | |
457 | cmd.no_stderr = 1; | |
458 | cmd.in = -1; | |
459 | cmd.out = -1; | |
460 | ||
461 | if (start_command(&cmd)) | |
462 | goto error; | |
463 | ||
464 | /* | |
465 | * If rev-list --stdin encounters an unknown commit, it | |
466 | * terminates, which will cause SIGPIPE in the write loop | |
467 | * below. | |
468 | */ | |
469 | sigchain_push(SIGPIPE, SIG_IGN); | |
470 | ||
471 | namebuf[0] = '^'; | |
472 | namebuf[41] = '\n'; | |
473 | for (i = get_max_object_index(); 0 < i; ) { | |
474 | o = get_indexed_object(--i); | |
2a745324 BH |
475 | if (!o) |
476 | continue; | |
390eb36b | 477 | if (!is_our_ref(o)) |
051e4005 JH |
478 | continue; |
479 | memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40); | |
480 | if (write_in_full(cmd.in, namebuf, 42) < 0) | |
481 | goto error; | |
482 | } | |
483 | namebuf[40] = '\n'; | |
484 | for (i = 0; i < want_obj.nr; i++) { | |
485 | o = want_obj.objects[i].item; | |
390eb36b | 486 | if (is_our_ref(o)) |
051e4005 JH |
487 | continue; |
488 | memcpy(namebuf, sha1_to_hex(o->sha1), 40); | |
489 | if (write_in_full(cmd.in, namebuf, 41) < 0) | |
490 | goto error; | |
491 | } | |
492 | close(cmd.in); | |
493 | ||
494 | sigchain_pop(SIGPIPE); | |
495 | ||
496 | /* | |
497 | * The commits out of the rev-list are not ancestors of | |
498 | * our ref. | |
499 | */ | |
500 | i = read_in_full(cmd.out, namebuf, 1); | |
501 | if (i) | |
502 | goto error; | |
503 | close(cmd.out); | |
504 | ||
505 | /* | |
506 | * rev-list may have died by encountering a bad commit | |
507 | * in the history, in which case we do want to bail out | |
508 | * even when it showed no commit. | |
509 | */ | |
510 | if (finish_command(&cmd)) | |
511 | goto error; | |
512 | ||
513 | /* All the non-tip ones are ancestors of what we advertised */ | |
514 | return; | |
515 | ||
516 | error: | |
517 | /* Pick one of them (we know there at least is one) */ | |
518 | for (i = 0; i < want_obj.nr; i++) { | |
519 | o = want_obj.objects[i].item; | |
390eb36b | 520 | if (!is_our_ref(o)) |
051e4005 JH |
521 | die("git upload-pack: not our ref %s", |
522 | sha1_to_hex(o->sha1)); | |
523 | } | |
524 | } | |
525 | ||
b1e9fff7 | 526 | static void receive_needs(void) |
fb9040cc | 527 | { |
3cd47459 | 528 | struct object_array shallows = OBJECT_ARRAY_INIT; |
74543a04 | 529 | int depth = 0; |
051e4005 | 530 | int has_non_tip = 0; |
fb9040cc | 531 | |
f0cea83f | 532 | shallow_nr = 0; |
fb9040cc | 533 | for (;;) { |
565ebbf7 | 534 | struct object *o; |
f47182c8 | 535 | const char *features; |
6ece0d30 | 536 | unsigned char sha1_buf[20]; |
74543a04 | 537 | char *line = packet_read_line(0, NULL); |
960deccb | 538 | reset_timeout(); |
74543a04 | 539 | if (!line) |
ed09aef0 | 540 | break; |
e091eb93 | 541 | |
599065a3 | 542 | if (!prefixcmp(line, "shallow ")) { |
ed09aef0 JS |
543 | unsigned char sha1[20]; |
544 | struct object *object; | |
b7b02170 | 545 | if (get_sha1_hex(line + 8, sha1)) |
ed09aef0 JS |
546 | die("invalid shallow line: %s", line); |
547 | object = parse_object(sha1); | |
548 | if (!object) | |
af04fa2a | 549 | continue; |
6293ded3 NTND |
550 | if (object->type != OBJ_COMMIT) |
551 | die("invalid shallow object %s", sha1_to_hex(sha1)); | |
e58e57e4 JK |
552 | if (!(object->flags & CLIENT_SHALLOW)) { |
553 | object->flags |= CLIENT_SHALLOW; | |
554 | add_object_array(object, NULL, &shallows); | |
555 | } | |
ed09aef0 JS |
556 | continue; |
557 | } | |
599065a3 | 558 | if (!prefixcmp(line, "deepen ")) { |
016e6ccb JS |
559 | char *end; |
560 | depth = strtol(line + 7, &end, 0); | |
561 | if (end == line + 7 || depth <= 0) | |
562 | die("Invalid deepen: %s", line); | |
563 | continue; | |
564 | } | |
599065a3 | 565 | if (prefixcmp(line, "want ") || |
6ece0d30 | 566 | get_sha1_hex(line+5, sha1_buf)) |
7e44c935 | 567 | die("git upload-pack: protocol error, " |
e091eb93 | 568 | "expected to get sha, not '%s'", line); |
f47182c8 JH |
569 | |
570 | features = line + 45; | |
571 | ||
572 | if (parse_feature_request(features, "multi_ack_detailed")) | |
78affc49 | 573 | multi_ack = 2; |
f47182c8 | 574 | else if (parse_feature_request(features, "multi_ack")) |
1bd8c8f0 | 575 | multi_ack = 1; |
f47182c8 | 576 | if (parse_feature_request(features, "no-done")) |
4e10cf9a | 577 | no_done = 1; |
f47182c8 | 578 | if (parse_feature_request(features, "thin-pack")) |
b19696c2 | 579 | use_thin_pack = 1; |
f47182c8 | 580 | if (parse_feature_request(features, "ofs-delta")) |
e4fe4b8e | 581 | use_ofs_delta = 1; |
f47182c8 | 582 | if (parse_feature_request(features, "side-band-64k")) |
d47f3db7 | 583 | use_sideband = LARGE_PACKET_MAX; |
f47182c8 | 584 | else if (parse_feature_request(features, "side-band")) |
d47f3db7 | 585 | use_sideband = DEFAULT_PACKET_MAX; |
f47182c8 | 586 | if (parse_feature_request(features, "no-progress")) |
b0e90897 | 587 | no_progress = 1; |
f47182c8 | 588 | if (parse_feature_request(features, "include-tag")) |
348e390b | 589 | use_include_tag = 1; |
565ebbf7 | 590 | |
f59de5d1 | 591 | o = parse_object(sha1_buf); |
051e4005 | 592 | if (!o) |
9f9aa761 EN |
593 | die("git upload-pack: not our ref %s", |
594 | sha1_to_hex(sha1_buf)); | |
565ebbf7 JH |
595 | if (!(o->flags & WANTED)) { |
596 | o->flags |= WANTED; | |
390eb36b | 597 | if (!is_our_ref(o)) |
051e4005 | 598 | has_non_tip = 1; |
b1e9fff7 | 599 | add_object_array(o, NULL, &want_obj); |
565ebbf7 | 600 | } |
fb9040cc | 601 | } |
9462e3f5 | 602 | |
051e4005 JH |
603 | /* |
604 | * We have sent all our refs already, and the other end | |
605 | * should have chosen out of them. When we are operating | |
606 | * in the stateless RPC mode, however, their choice may | |
607 | * have been based on the set of older refs advertised | |
608 | * by another process that handled the initial request. | |
609 | */ | |
610 | if (has_non_tip) | |
611 | check_non_tip(); | |
612 | ||
9462e3f5 JS |
613 | if (!use_sideband && daemon_mode) |
614 | no_progress = 1; | |
615 | ||
f53514bc JS |
616 | if (depth == 0 && shallows.nr == 0) |
617 | return; | |
016e6ccb | 618 | if (depth > 0) { |
4dcb167f | 619 | struct commit_list *result = NULL, *backup = NULL; |
f53514bc | 620 | int i; |
4dcb167f NTND |
621 | if (depth == INFINITE_DEPTH) |
622 | for (i = 0; i < shallows.nr; i++) { | |
623 | struct object *object = shallows.objects[i].item; | |
624 | object->flags |= NOT_SHALLOW; | |
625 | } | |
626 | else | |
627 | backup = result = | |
628 | get_shallow_commits(&want_obj, depth, | |
629 | SHALLOW, NOT_SHALLOW); | |
016e6ccb | 630 | while (result) { |
f53514bc | 631 | struct object *object = &result->item->object; |
1f2de769 | 632 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { |
f53514bc JS |
633 | packet_write(1, "shallow %s", |
634 | sha1_to_hex(object->sha1)); | |
635 | register_shallow(object->sha1); | |
f0cea83f | 636 | shallow_nr++; |
f53514bc | 637 | } |
016e6ccb JS |
638 | result = result->next; |
639 | } | |
640 | free_commit_list(backup); | |
f53514bc JS |
641 | for (i = 0; i < shallows.nr; i++) { |
642 | struct object *object = shallows.objects[i].item; | |
643 | if (object->flags & NOT_SHALLOW) { | |
644 | struct commit_list *parents; | |
645 | packet_write(1, "unshallow %s", | |
646 | sha1_to_hex(object->sha1)); | |
647 | object->flags &= ~CLIENT_SHALLOW; | |
648 | /* make sure the real parents are parsed */ | |
649 | unregister_shallow(object->sha1); | |
176d45cb | 650 | object->parsed = 0; |
dec38c81 MK |
651 | if (parse_commit((struct commit *)object)) |
652 | die("invalid commit"); | |
f53514bc JS |
653 | parents = ((struct commit *)object)->parents; |
654 | while (parents) { | |
655 | add_object_array(&parents->item->object, | |
656 | NULL, &want_obj); | |
657 | parents = parents->next; | |
658 | } | |
6523078b | 659 | add_object_array(object, NULL, &extra_edge_obj); |
f53514bc JS |
660 | } |
661 | /* make sure commit traversal conforms to client */ | |
662 | register_shallow(object->sha1); | |
663 | } | |
664 | packet_flush(1); | |
665 | } else | |
666 | if (shallows.nr > 0) { | |
667 | int i; | |
668 | for (i = 0; i < shallows.nr; i++) | |
669 | register_shallow(shallows.objects[i].item->sha1); | |
670 | } | |
f0cea83f NE |
671 | |
672 | shallow_nr += shallows.nr; | |
f53514bc | 673 | free(shallows.objects); |
fb9040cc LT |
674 | } |
675 | ||
daebaa78 | 676 | /* return non-zero if the ref is hidden, otherwise 0 */ |
cbbe50db JH |
677 | static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
678 | { | |
679 | struct object *o = lookup_unknown_object(sha1); | |
daebaa78 | 680 | |
390eb36b JH |
681 | if (ref_is_hidden(refname)) { |
682 | o->flags |= HIDDEN_REF; | |
daebaa78 | 683 | return 1; |
390eb36b | 684 | } |
cbbe50db JH |
685 | if (!o) |
686 | die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); | |
3f1da57f | 687 | o->flags |= OUR_REF; |
cbbe50db JH |
688 | return 0; |
689 | } | |
690 | ||
8da19775 | 691 | static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
def88e9a | 692 | { |
ed09aef0 | 693 | static const char *capabilities = "multi_ack thin-pack side-band" |
348e390b | 694 | " side-band-64k ofs-delta shallow no-progress" |
78affc49 | 695 | " include-tag multi_ack_detailed"; |
6b01ecfe | 696 | const char *refname_nons = strip_namespace(refname); |
435c8332 | 697 | unsigned char peeled[20]; |
b5b16990 | 698 | |
daebaa78 JH |
699 | if (mark_our_ref(refname, sha1, flag, cb_data)) |
700 | return 0; | |
cbbe50db | 701 | |
1f5881bb | 702 | if (capabilities) |
390eb36b | 703 | packet_write(1, "%s %s%c%s%s%s agent=%s\n", |
ff5effdf | 704 | sha1_to_hex(sha1), refname_nons, |
cf2ad8e6 | 705 | 0, capabilities, |
390eb36b | 706 | allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "", |
ff5effdf JK |
707 | stateless_rpc ? " no-done" : "", |
708 | git_user_agent_sanitized()); | |
1f5881bb | 709 | else |
6b01ecfe | 710 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons); |
1f5881bb | 711 | capabilities = NULL; |
435c8332 JK |
712 | if (!peel_ref(refname, peeled)) |
713 | packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons); | |
def88e9a LT |
714 | return 0; |
715 | } | |
716 | ||
59076eba | 717 | static void upload_pack(void) |
def88e9a | 718 | { |
42526b47 SP |
719 | if (advertise_refs || !stateless_rpc) { |
720 | reset_timeout(); | |
6b01ecfe JT |
721 | head_ref_namespaced(send_ref, NULL); |
722 | for_each_namespaced_ref(send_ref, NULL); | |
42526b47 SP |
723 | packet_flush(1); |
724 | } else { | |
6b01ecfe JT |
725 | head_ref_namespaced(mark_our_ref, NULL); |
726 | for_each_namespaced_ref(mark_our_ref, NULL); | |
42526b47 SP |
727 | } |
728 | if (advertise_refs) | |
729 | return; | |
730 | ||
b1e9fff7 | 731 | receive_needs(); |
59076eba DR |
732 | if (want_obj.nr) { |
733 | get_common_commits(); | |
734 | create_pack_file(); | |
735 | } | |
def88e9a LT |
736 | } |
737 | ||
daebaa78 JH |
738 | static int upload_pack_config(const char *var, const char *value, void *unused) |
739 | { | |
390eb36b JH |
740 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) |
741 | allow_tip_sha1_in_want = git_config_bool(var, value); | |
05e95155 JK |
742 | else if (!strcmp("uploadpack.keepalive", var)) { |
743 | keepalive = git_config_int(var, value); | |
744 | if (!keepalive) | |
745 | keepalive = -1; | |
746 | } | |
daebaa78 JH |
747 | return parse_hide_refs_config(var, value, "uploadpack"); |
748 | } | |
749 | ||
def88e9a LT |
750 | int main(int argc, char **argv) |
751 | { | |
8d630132 | 752 | char *dir; |
960deccb PA |
753 | int i; |
754 | int strict = 0; | |
755 | ||
5e9637c6 ÆAB |
756 | git_setup_gettext(); |
757 | ||
bbc30f99 | 758 | packet_trace_identity("upload-pack"); |
2fb3f6db | 759 | git_extract_argv0_path(argv[0]); |
dae556bd | 760 | read_replace_refs = 0; |
2fb3f6db | 761 | |
960deccb PA |
762 | for (i = 1; i < argc; i++) { |
763 | char *arg = argv[i]; | |
764 | ||
765 | if (arg[0] != '-') | |
766 | break; | |
42526b47 SP |
767 | if (!strcmp(arg, "--advertise-refs")) { |
768 | advertise_refs = 1; | |
769 | continue; | |
770 | } | |
771 | if (!strcmp(arg, "--stateless-rpc")) { | |
772 | stateless_rpc = 1; | |
773 | continue; | |
774 | } | |
960deccb PA |
775 | if (!strcmp(arg, "--strict")) { |
776 | strict = 1; | |
777 | continue; | |
778 | } | |
cc44c765 | 779 | if (!prefixcmp(arg, "--timeout=")) { |
960deccb | 780 | timeout = atoi(arg+10); |
9462e3f5 | 781 | daemon_mode = 1; |
960deccb PA |
782 | continue; |
783 | } | |
784 | if (!strcmp(arg, "--")) { | |
785 | i++; | |
786 | break; | |
787 | } | |
788 | } | |
a6080a0a | 789 | |
960deccb | 790 | if (i != argc-1) |
def88e9a | 791 | usage(upload_pack_usage); |
04b33055 | 792 | |
e1464ca7 | 793 | setup_path(); |
04b33055 | 794 | |
960deccb | 795 | dir = argv[i]; |
113b9475 | 796 | |
8d630132 | 797 | if (!enter_repo(dir, strict)) |
05ac6b34 | 798 | die("'%s' does not appear to be a git repository", dir); |
a0022eeb JH |
799 | if (is_repository_shallow()) |
800 | die("attempt to fetch/clone from a shallow repository"); | |
daebaa78 | 801 | git_config(upload_pack_config, NULL); |
def88e9a LT |
802 | upload_pack(); |
803 | return 0; | |
804 | } |