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