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