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