]>
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" |
def88e9a | 13 | |
34263de0 | 14 | static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=nn] <dir>"; |
def88e9a | 15 | |
937a515a JH |
16 | /* bits #0..7 in revision.h, #8..10 in commit.c */ |
17 | #define THEY_HAVE (1u << 11) | |
18 | #define OUR_REF (1u << 12) | |
19 | #define WANTED (1u << 13) | |
20 | #define COMMON_KNOWN (1u << 14) | |
21 | #define REACHABLE (1u << 15) | |
22 | ||
f53514bc JS |
23 | #define SHALLOW (1u << 16) |
24 | #define NOT_SHALLOW (1u << 17) | |
25 | #define CLIENT_SHALLOW (1u << 18) | |
26 | ||
3fbe2d54 | 27 | static unsigned long oldest_have; |
937a515a | 28 | |
96f1e58f | 29 | static int multi_ack, nr_our_refs; |
348e390b SP |
30 | static int use_thin_pack, use_ofs_delta, use_include_tag; |
31 | static int no_progress; | |
b1e9fff7 JH |
32 | static struct object_array have_obj; |
33 | static struct object_array want_obj; | |
96f1e58f | 34 | static unsigned int timeout; |
d47f3db7 JH |
35 | /* 0 for no sideband, |
36 | * otherwise maximum packet size (up to 65520 bytes). | |
37 | */ | |
96f1e58f | 38 | static int use_sideband; |
49aaddd1 | 39 | static int debug_fd; |
960deccb PA |
40 | |
41 | static void reset_timeout(void) | |
42 | { | |
43 | alarm(timeout); | |
44 | } | |
fb9040cc | 45 | |
75bfc6c2 LT |
46 | static int strip(char *line, int len) |
47 | { | |
48 | if (len && line[len-1] == '\n') | |
49 | line[--len] = 0; | |
50 | return len; | |
51 | } | |
52 | ||
583b7ea3 JH |
53 | static ssize_t send_client_data(int fd, const char *data, ssize_t sz) |
54 | { | |
958c24b1 | 55 | if (use_sideband) |
d47f3db7 | 56 | return send_sideband(1, fd, data, sz, use_sideband); |
958c24b1 JH |
57 | if (fd == 3) |
58 | /* emergency quit */ | |
59 | fd = 2; | |
60 | if (fd == 2) { | |
93822c22 | 61 | /* XXX: are we happy to lose stuff here? */ |
958c24b1 JH |
62 | xwrite(fd, data, sz); |
63 | return sz; | |
583b7ea3 | 64 | } |
958c24b1 | 65 | return safe_write(fd, data, sz); |
583b7ea3 JH |
66 | } |
67 | ||
16befb8b | 68 | static FILE *pack_pipe = NULL; |
9b8dc263 JS |
69 | static void show_commit(struct commit *commit) |
70 | { | |
71 | if (commit->object.flags & BOUNDARY) | |
72 | fputc('-', pack_pipe); | |
73 | if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0) | |
74 | die("broken output pipe"); | |
75 | fputc('\n', pack_pipe); | |
76 | fflush(pack_pipe); | |
77 | free(commit->buffer); | |
78 | commit->buffer = NULL; | |
79 | } | |
80 | ||
81 | static void show_object(struct object_array_entry *p) | |
82 | { | |
83 | /* An object with name "foo\n0000000..." can be used to | |
84 | * confuse downstream git-pack-objects very badly. | |
85 | */ | |
86 | const char *ep = strchr(p->name, '\n'); | |
87 | if (ep) { | |
88 | fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(p->item->sha1), | |
89 | (int) (ep - p->name), | |
90 | p->name); | |
91 | } | |
92 | else | |
93 | fprintf(pack_pipe, "%s %s\n", | |
94 | sha1_to_hex(p->item->sha1), p->name); | |
95 | } | |
96 | ||
97 | static void show_edge(struct commit *commit) | |
98 | { | |
99 | fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1)); | |
100 | } | |
101 | ||
21edd3f1 | 102 | static int do_rev_list(int fd, void *create_full_pack) |
80ccaa78 JS |
103 | { |
104 | int i; | |
105 | struct rev_info revs; | |
106 | ||
21edd3f1 | 107 | pack_pipe = fdopen(fd, "w"); |
80ccaa78 JS |
108 | if (create_full_pack) |
109 | use_thin_pack = 0; /* no point doing it */ | |
110 | init_revisions(&revs, NULL); | |
111 | revs.tag_objects = 1; | |
112 | revs.tree_objects = 1; | |
113 | revs.blob_objects = 1; | |
114 | if (use_thin_pack) | |
115 | revs.edge_hint = 1; | |
116 | ||
117 | if (create_full_pack) { | |
118 | const char *args[] = {"rev-list", "--all", NULL}; | |
119 | setup_revisions(2, args, &revs, NULL); | |
120 | } else { | |
121 | for (i = 0; i < want_obj.nr; i++) { | |
122 | struct object *o = want_obj.objects[i].item; | |
123 | /* why??? */ | |
124 | o->flags &= ~UNINTERESTING; | |
125 | add_pending_object(&revs, o, NULL); | |
126 | } | |
127 | for (i = 0; i < have_obj.nr; i++) { | |
128 | struct object *o = have_obj.objects[i].item; | |
129 | o->flags |= UNINTERESTING; | |
130 | add_pending_object(&revs, o, NULL); | |
131 | } | |
132 | setup_revisions(0, NULL, &revs, NULL); | |
133 | } | |
3d51e1b5 MK |
134 | if (prepare_revision_walk(&revs)) |
135 | die("revision walk setup failed"); | |
80ccaa78 JS |
136 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
137 | traverse_commit_list(&revs, show_commit, show_object); | |
618ebe9f JS |
138 | fflush(pack_pipe); |
139 | fclose(pack_pipe); | |
21edd3f1 | 140 | return 0; |
80ccaa78 JS |
141 | } |
142 | ||
fb9040cc LT |
143 | static void create_pack_file(void) |
144 | { | |
21edd3f1 | 145 | struct async rev_list; |
cc41fa8d | 146 | struct child_process pack_objects; |
b1e9fff7 | 147 | int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr); |
363b7817 | 148 | char data[8193], progress[128]; |
583b7ea3 JH |
149 | char abort_msg[] = "aborting due to possible repository " |
150 | "corruption on the remote side."; | |
b1c71b72 | 151 | int buffered = -1; |
4c324c00 | 152 | ssize_t sz; |
cc41fa8d JS |
153 | const char *argv[10]; |
154 | int arg = 0; | |
75bfc6c2 | 155 | |
21edd3f1 JS |
156 | rev_list.proc = do_rev_list; |
157 | /* .data is just a boolean: any non-NULL value will do */ | |
158 | rev_list.data = create_full_pack ? &rev_list : NULL; | |
159 | if (start_async(&rev_list)) | |
7e44c935 | 160 | die("git upload-pack: unable to fork git-rev-list"); |
75bfc6c2 | 161 | |
cc41fa8d JS |
162 | argv[arg++] = "pack-objects"; |
163 | argv[arg++] = "--stdout"; | |
164 | if (!no_progress) | |
165 | argv[arg++] = "--progress"; | |
166 | if (use_ofs_delta) | |
167 | argv[arg++] = "--delta-base-offset"; | |
348e390b SP |
168 | if (use_include_tag) |
169 | argv[arg++] = "--include-tag"; | |
cc41fa8d JS |
170 | argv[arg++] = NULL; |
171 | ||
172 | memset(&pack_objects, 0, sizeof(pack_objects)); | |
21edd3f1 | 173 | pack_objects.in = rev_list.out; /* start_command closes it */ |
cc41fa8d JS |
174 | pack_objects.out = -1; |
175 | pack_objects.err = -1; | |
176 | pack_objects.git_cmd = 1; | |
177 | pack_objects.argv = argv; | |
21edd3f1 | 178 | |
4c324c00 | 179 | if (start_command(&pack_objects)) |
7e44c935 | 180 | die("git upload-pack: unable to fork git-pack-objects"); |
b1c71b72 | 181 | |
cc41fa8d JS |
182 | /* We read from pack_objects.err to capture stderr output for |
183 | * progress bar, and pack_objects.out to capture the pack data. | |
b1c71b72 | 184 | */ |
b1c71b72 JH |
185 | |
186 | while (1) { | |
b1c71b72 | 187 | struct pollfd pfd[2]; |
363b7817 | 188 | int pe, pu, pollsize; |
b1c71b72 | 189 | |
0d516ada ML |
190 | reset_timeout(); |
191 | ||
b1c71b72 | 192 | pollsize = 0; |
363b7817 | 193 | pe = pu = -1; |
b1c71b72 | 194 | |
cc41fa8d JS |
195 | if (0 <= pack_objects.out) { |
196 | pfd[pollsize].fd = pack_objects.out; | |
b1c71b72 JH |
197 | pfd[pollsize].events = POLLIN; |
198 | pu = pollsize; | |
199 | pollsize++; | |
200 | } | |
cc41fa8d JS |
201 | if (0 <= pack_objects.err) { |
202 | pfd[pollsize].fd = pack_objects.err; | |
363b7817 JH |
203 | pfd[pollsize].events = POLLIN; |
204 | pe = pollsize; | |
205 | pollsize++; | |
206 | } | |
b1c71b72 | 207 | |
4c324c00 JS |
208 | if (!pollsize) |
209 | break; | |
210 | ||
211 | if (poll(pfd, pollsize, -1) < 0) { | |
212 | if (errno != EINTR) { | |
213 | error("poll failed, resuming: %s", | |
214 | strerror(errno)); | |
215 | sleep(1); | |
b1c71b72 | 216 | } |
4c324c00 JS |
217 | continue; |
218 | } | |
219 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { | |
220 | /* Data ready; we keep the last byte to ourselves | |
221 | * in case we detect broken rev-list, so that we | |
222 | * can leave the stream corrupted. This is | |
223 | * unfortunate -- unpack-objects would happily | |
224 | * accept a valid packdata with trailing garbage, | |
225 | * so appending garbage after we pass all the | |
226 | * pack data is not good enough to signal | |
227 | * breakage to downstream. | |
228 | */ | |
229 | char *cp = data; | |
230 | ssize_t outsz = 0; | |
231 | if (0 <= buffered) { | |
232 | *cp++ = buffered; | |
233 | outsz++; | |
b1c71b72 | 234 | } |
4c324c00 JS |
235 | sz = xread(pack_objects.out, cp, |
236 | sizeof(data) - outsz); | |
237 | if (0 < sz) | |
238 | ; | |
239 | else if (sz == 0) { | |
240 | close(pack_objects.out); | |
241 | pack_objects.out = -1; | |
363b7817 | 242 | } |
4c324c00 JS |
243 | else |
244 | goto fail; | |
245 | sz += outsz; | |
246 | if (1 < sz) { | |
247 | buffered = data[sz-1] & 0xFF; | |
248 | sz--; | |
b1c71b72 | 249 | } |
4c324c00 JS |
250 | else |
251 | buffered = -1; | |
252 | sz = send_client_data(1, data, sz); | |
253 | if (sz < 0) | |
b1c71b72 | 254 | goto fail; |
4c324c00 JS |
255 | } |
256 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { | |
257 | /* Status ready; we ship that in the side-band | |
258 | * or dump to the standard error. | |
259 | */ | |
260 | sz = xread(pack_objects.err, progress, | |
261 | sizeof(progress)); | |
262 | if (0 < sz) | |
263 | send_client_data(2, progress, sz); | |
264 | else if (sz == 0) { | |
265 | close(pack_objects.err); | |
266 | pack_objects.err = -1; | |
b1c71b72 | 267 | } |
4c324c00 JS |
268 | else |
269 | goto fail; | |
b1c71b72 | 270 | } |
4c324c00 | 271 | } |
b1c71b72 | 272 | |
4c324c00 | 273 | if (finish_command(&pack_objects)) { |
7e44c935 | 274 | error("git upload-pack: git-pack-objects died with error."); |
4c324c00 JS |
275 | goto fail; |
276 | } | |
277 | if (finish_async(&rev_list)) | |
278 | goto fail; /* error was already reported */ | |
b1c71b72 | 279 | |
4c324c00 JS |
280 | /* flush the data */ |
281 | if (0 <= buffered) { | |
282 | data[0] = buffered; | |
283 | sz = send_client_data(1, data, 1); | |
284 | if (sz < 0) | |
285 | goto fail; | |
286 | fprintf(stderr, "flushed.\n"); | |
b1c71b72 | 287 | } |
4c324c00 JS |
288 | if (use_sideband) |
289 | packet_flush(1); | |
290 | return; | |
291 | ||
b1c71b72 | 292 | fail: |
583b7ea3 | 293 | send_client_data(3, abort_msg, sizeof(abort_msg)); |
7e44c935 | 294 | die("git upload-pack: %s", abort_msg); |
fb9040cc LT |
295 | } |
296 | ||
def88e9a LT |
297 | static int got_sha1(char *hex, unsigned char *sha1) |
298 | { | |
b1e9fff7 | 299 | struct object *o; |
937a515a | 300 | int we_knew_they_have = 0; |
b1e9fff7 | 301 | |
def88e9a | 302 | if (get_sha1_hex(hex, sha1)) |
7e44c935 | 303 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
fb9040cc | 304 | if (!has_sha1_file(sha1)) |
937a515a | 305 | return -1; |
b1e9fff7 JH |
306 | |
307 | o = lookup_object(sha1); | |
308 | if (!(o && o->parsed)) | |
309 | o = parse_object(sha1); | |
310 | if (!o) | |
311 | die("oops (%s)", sha1_to_hex(sha1)); | |
182a8dab | 312 | if (o->type == OBJ_COMMIT) { |
b1e9fff7 | 313 | struct commit_list *parents; |
937a515a | 314 | struct commit *commit = (struct commit *)o; |
b1e9fff7 | 315 | if (o->flags & THEY_HAVE) |
937a515a JH |
316 | we_knew_they_have = 1; |
317 | else | |
318 | o->flags |= THEY_HAVE; | |
319 | if (!oldest_have || (commit->date < oldest_have)) | |
320 | oldest_have = commit->date; | |
321 | for (parents = commit->parents; | |
b1e9fff7 JH |
322 | parents; |
323 | parents = parents->next) | |
324 | parents->item->object.flags |= THEY_HAVE; | |
fb9040cc | 325 | } |
937a515a JH |
326 | if (!we_knew_they_have) { |
327 | add_object_array(o, NULL, &have_obj); | |
328 | return 1; | |
329 | } | |
330 | return 0; | |
331 | } | |
332 | ||
333 | static int reachable(struct commit *want) | |
334 | { | |
335 | struct commit_list *work = NULL; | |
336 | ||
337 | insert_by_date(want, &work); | |
338 | while (work) { | |
339 | struct commit_list *list = work->next; | |
340 | struct commit *commit = work->item; | |
341 | free(work); | |
342 | work = list; | |
343 | ||
344 | if (commit->object.flags & THEY_HAVE) { | |
345 | want->object.flags |= COMMON_KNOWN; | |
346 | break; | |
347 | } | |
348 | if (!commit->object.parsed) | |
349 | parse_object(commit->object.sha1); | |
350 | if (commit->object.flags & REACHABLE) | |
351 | continue; | |
352 | commit->object.flags |= REACHABLE; | |
353 | if (commit->date < oldest_have) | |
354 | continue; | |
355 | for (list = commit->parents; list; list = list->next) { | |
356 | struct commit *parent = list->item; | |
357 | if (!(parent->object.flags & REACHABLE)) | |
358 | insert_by_date(parent, &work); | |
359 | } | |
360 | } | |
361 | want->object.flags |= REACHABLE; | |
362 | clear_commit_marks(want, REACHABLE); | |
363 | free_commit_list(work); | |
364 | return (want->object.flags & COMMON_KNOWN); | |
365 | } | |
366 | ||
367 | static int ok_to_give_up(void) | |
368 | { | |
369 | int i; | |
370 | ||
371 | if (!have_obj.nr) | |
372 | return 0; | |
373 | ||
374 | for (i = 0; i < want_obj.nr; i++) { | |
375 | struct object *want = want_obj.objects[i].item; | |
376 | ||
377 | if (want->flags & COMMON_KNOWN) | |
378 | continue; | |
379 | want = deref_tag(want, "a want line", 0); | |
380 | if (!want || want->type != OBJ_COMMIT) { | |
381 | /* no way to tell if this is reachable by | |
382 | * looking at the ancestry chain alone, so | |
383 | * leave a note to ourselves not to worry about | |
384 | * this object anymore. | |
385 | */ | |
386 | want_obj.objects[i].item->flags |= COMMON_KNOWN; | |
387 | continue; | |
388 | } | |
389 | if (!reachable((struct commit *)want)) | |
390 | return 0; | |
391 | } | |
fb9040cc | 392 | return 1; |
def88e9a LT |
393 | } |
394 | ||
395 | static int get_common_commits(void) | |
396 | { | |
397 | static char line[1000]; | |
c04c4e57 JH |
398 | unsigned char sha1[20]; |
399 | char hex[41], last_hex[41]; | |
def88e9a | 400 | |
f0243f26 JS |
401 | save_commit_buffer = 0; |
402 | ||
def88e9a | 403 | for(;;) { |
fd13b21f | 404 | int len = packet_read_line(0, line, sizeof(line)); |
960deccb | 405 | reset_timeout(); |
def88e9a LT |
406 | |
407 | if (!len) { | |
b1e9fff7 | 408 | if (have_obj.nr == 0 || multi_ack) |
1bd8c8f0 | 409 | packet_write(1, "NAK\n"); |
def88e9a LT |
410 | continue; |
411 | } | |
fd13b21f | 412 | strip(line, len); |
cc44c765 | 413 | if (!prefixcmp(line, "have ")) { |
937a515a JH |
414 | switch (got_sha1(line+5, sha1)) { |
415 | case -1: /* they have what we do not */ | |
416 | if (multi_ack && ok_to_give_up()) | |
417 | packet_write(1, "ACK %s continue\n", | |
418 | sha1_to_hex(sha1)); | |
419 | break; | |
420 | default: | |
c04c4e57 JH |
421 | memcpy(hex, sha1_to_hex(sha1), 41); |
422 | if (multi_ack) { | |
423 | const char *msg = "ACK %s continue\n"; | |
424 | packet_write(1, msg, hex); | |
425 | memcpy(last_hex, hex, 41); | |
426 | } | |
427 | else if (have_obj.nr == 1) | |
428 | packet_write(1, "ACK %s\n", hex); | |
937a515a | 429 | break; |
af2d3aa4 | 430 | } |
def88e9a LT |
431 | continue; |
432 | } | |
433 | if (!strcmp(line, "done")) { | |
b1e9fff7 | 434 | if (have_obj.nr > 0) { |
1bd8c8f0 | 435 | if (multi_ack) |
c04c4e57 | 436 | packet_write(1, "ACK %s\n", last_hex); |
1bd8c8f0 JS |
437 | return 0; |
438 | } | |
def88e9a LT |
439 | packet_write(1, "NAK\n"); |
440 | return -1; | |
441 | } | |
7e44c935 | 442 | die("git upload-pack: expected SHA1 list, got '%s'", line); |
def88e9a | 443 | } |
def88e9a LT |
444 | } |
445 | ||
b1e9fff7 | 446 | static void receive_needs(void) |
fb9040cc | 447 | { |
ed09aef0 | 448 | struct object_array shallows = {0, 0, NULL}; |
fb9040cc | 449 | static char line[1000]; |
016e6ccb | 450 | int len, depth = 0; |
fb9040cc | 451 | |
49aaddd1 SP |
452 | if (debug_fd) |
453 | write_in_full(debug_fd, "#S\n", 3); | |
fb9040cc | 454 | for (;;) { |
565ebbf7 | 455 | struct object *o; |
6ece0d30 | 456 | unsigned char sha1_buf[20]; |
fb9040cc | 457 | len = packet_read_line(0, line, sizeof(line)); |
960deccb | 458 | reset_timeout(); |
fb9040cc | 459 | if (!len) |
ed09aef0 | 460 | break; |
49aaddd1 SP |
461 | if (debug_fd) |
462 | write_in_full(debug_fd, line, len); | |
e091eb93 | 463 | |
599065a3 | 464 | if (!prefixcmp(line, "shallow ")) { |
ed09aef0 JS |
465 | unsigned char sha1[20]; |
466 | struct object *object; | |
f53514bc | 467 | use_thin_pack = 0; |
ed09aef0 JS |
468 | if (get_sha1(line + 8, sha1)) |
469 | die("invalid shallow line: %s", line); | |
470 | object = parse_object(sha1); | |
471 | if (!object) | |
472 | die("did not find object for %s", line); | |
f53514bc | 473 | object->flags |= CLIENT_SHALLOW; |
ed09aef0 JS |
474 | add_object_array(object, NULL, &shallows); |
475 | continue; | |
476 | } | |
599065a3 | 477 | if (!prefixcmp(line, "deepen ")) { |
016e6ccb | 478 | char *end; |
f53514bc | 479 | use_thin_pack = 0; |
016e6ccb JS |
480 | depth = strtol(line + 7, &end, 0); |
481 | if (end == line + 7 || depth <= 0) | |
482 | die("Invalid deepen: %s", line); | |
483 | continue; | |
484 | } | |
599065a3 | 485 | if (prefixcmp(line, "want ") || |
6ece0d30 | 486 | get_sha1_hex(line+5, sha1_buf)) |
7e44c935 | 487 | die("git upload-pack: protocol error, " |
e091eb93 | 488 | "expected to get sha, not '%s'", line); |
1bd8c8f0 JS |
489 | if (strstr(line+45, "multi_ack")) |
490 | multi_ack = 1; | |
b19696c2 JH |
491 | if (strstr(line+45, "thin-pack")) |
492 | use_thin_pack = 1; | |
e4fe4b8e NP |
493 | if (strstr(line+45, "ofs-delta")) |
494 | use_ofs_delta = 1; | |
d47f3db7 JH |
495 | if (strstr(line+45, "side-band-64k")) |
496 | use_sideband = LARGE_PACKET_MAX; | |
497 | else if (strstr(line+45, "side-band")) | |
498 | use_sideband = DEFAULT_PACKET_MAX; | |
b0e90897 JS |
499 | if (strstr(line+45, "no-progress")) |
500 | no_progress = 1; | |
348e390b SP |
501 | if (strstr(line+45, "include-tag")) |
502 | use_include_tag = 1; | |
565ebbf7 JH |
503 | |
504 | /* We have sent all our refs already, and the other end | |
505 | * should have chosen out of them; otherwise they are | |
506 | * asking for nonsense. | |
507 | * | |
508 | * Hmph. We may later want to allow "want" line that | |
509 | * asks for something like "master~10" (symbolic)... | |
510 | * would it make sense? I don't know. | |
511 | */ | |
512 | o = lookup_object(sha1_buf); | |
513 | if (!o || !(o->flags & OUR_REF)) | |
7e44c935 | 514 | die("git upload-pack: not our ref %s", line+5); |
565ebbf7 JH |
515 | if (!(o->flags & WANTED)) { |
516 | o->flags |= WANTED; | |
b1e9fff7 | 517 | add_object_array(o, NULL, &want_obj); |
565ebbf7 | 518 | } |
fb9040cc | 519 | } |
49aaddd1 SP |
520 | if (debug_fd) |
521 | write_in_full(debug_fd, "#E\n", 3); | |
f53514bc JS |
522 | if (depth == 0 && shallows.nr == 0) |
523 | return; | |
016e6ccb JS |
524 | if (depth > 0) { |
525 | struct commit_list *result, *backup; | |
f53514bc JS |
526 | int i; |
527 | backup = result = get_shallow_commits(&want_obj, depth, | |
528 | SHALLOW, NOT_SHALLOW); | |
016e6ccb | 529 | while (result) { |
f53514bc | 530 | struct object *object = &result->item->object; |
1f2de769 | 531 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { |
f53514bc JS |
532 | packet_write(1, "shallow %s", |
533 | sha1_to_hex(object->sha1)); | |
534 | register_shallow(object->sha1); | |
535 | } | |
016e6ccb JS |
536 | result = result->next; |
537 | } | |
538 | free_commit_list(backup); | |
f53514bc JS |
539 | for (i = 0; i < shallows.nr; i++) { |
540 | struct object *object = shallows.objects[i].item; | |
541 | if (object->flags & NOT_SHALLOW) { | |
542 | struct commit_list *parents; | |
543 | packet_write(1, "unshallow %s", | |
544 | sha1_to_hex(object->sha1)); | |
545 | object->flags &= ~CLIENT_SHALLOW; | |
546 | /* make sure the real parents are parsed */ | |
547 | unregister_shallow(object->sha1); | |
176d45cb | 548 | object->parsed = 0; |
dec38c81 MK |
549 | if (parse_commit((struct commit *)object)) |
550 | die("invalid commit"); | |
f53514bc JS |
551 | parents = ((struct commit *)object)->parents; |
552 | while (parents) { | |
553 | add_object_array(&parents->item->object, | |
554 | NULL, &want_obj); | |
555 | parents = parents->next; | |
556 | } | |
557 | } | |
558 | /* make sure commit traversal conforms to client */ | |
559 | register_shallow(object->sha1); | |
560 | } | |
561 | packet_flush(1); | |
562 | } else | |
563 | if (shallows.nr > 0) { | |
564 | int i; | |
565 | for (i = 0; i < shallows.nr; i++) | |
566 | register_shallow(shallows.objects[i].item->sha1); | |
567 | } | |
568 | free(shallows.objects); | |
fb9040cc LT |
569 | } |
570 | ||
8da19775 | 571 | static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
def88e9a | 572 | { |
ed09aef0 | 573 | static const char *capabilities = "multi_ack thin-pack side-band" |
348e390b SP |
574 | " side-band-64k ofs-delta shallow no-progress" |
575 | " include-tag"; | |
f6b42a81 JH |
576 | struct object *o = parse_object(sha1); |
577 | ||
b5b16990 | 578 | if (!o) |
7e44c935 | 579 | die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); |
b5b16990 | 580 | |
1f5881bb JS |
581 | if (capabilities) |
582 | packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname, | |
583 | 0, capabilities); | |
584 | else | |
585 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname); | |
586 | capabilities = NULL; | |
565ebbf7 JH |
587 | if (!(o->flags & OUR_REF)) { |
588 | o->flags |= OUR_REF; | |
589 | nr_our_refs++; | |
590 | } | |
1974632c | 591 | if (o->type == OBJ_TAG) { |
9534f40b | 592 | o = deref_tag(o, refname, 0); |
affeef12 MK |
593 | if (o) |
594 | packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname); | |
f6b42a81 | 595 | } |
def88e9a LT |
596 | return 0; |
597 | } | |
598 | ||
59076eba | 599 | static void upload_pack(void) |
def88e9a | 600 | { |
960deccb | 601 | reset_timeout(); |
cb5d709f JH |
602 | head_ref(send_ref, NULL); |
603 | for_each_ref(send_ref, NULL); | |
def88e9a | 604 | packet_flush(1); |
b1e9fff7 | 605 | receive_needs(); |
59076eba DR |
606 | if (want_obj.nr) { |
607 | get_common_commits(); | |
608 | create_pack_file(); | |
609 | } | |
def88e9a LT |
610 | } |
611 | ||
612 | int main(int argc, char **argv) | |
613 | { | |
8d630132 | 614 | char *dir; |
960deccb PA |
615 | int i; |
616 | int strict = 0; | |
617 | ||
2fb3f6db SP |
618 | git_extract_argv0_path(argv[0]); |
619 | ||
960deccb PA |
620 | for (i = 1; i < argc; i++) { |
621 | char *arg = argv[i]; | |
622 | ||
623 | if (arg[0] != '-') | |
624 | break; | |
625 | if (!strcmp(arg, "--strict")) { | |
626 | strict = 1; | |
627 | continue; | |
628 | } | |
cc44c765 | 629 | if (!prefixcmp(arg, "--timeout=")) { |
960deccb PA |
630 | timeout = atoi(arg+10); |
631 | continue; | |
632 | } | |
633 | if (!strcmp(arg, "--")) { | |
634 | i++; | |
635 | break; | |
636 | } | |
637 | } | |
a6080a0a | 638 | |
960deccb | 639 | if (i != argc-1) |
def88e9a | 640 | usage(upload_pack_usage); |
04b33055 | 641 | |
e1464ca7 | 642 | setup_path(); |
04b33055 | 643 | |
960deccb | 644 | dir = argv[i]; |
113b9475 | 645 | |
8d630132 | 646 | if (!enter_repo(dir, strict)) |
05ac6b34 | 647 | die("'%s' does not appear to be a git repository", dir); |
a0022eeb JH |
648 | if (is_repository_shallow()) |
649 | die("attempt to fetch/clone from a shallow repository"); | |
49aaddd1 SP |
650 | if (getenv("GIT_DEBUG_SEND_PACK")) |
651 | debug_fd = atoi(getenv("GIT_DEBUG_SEND_PACK")); | |
def88e9a LT |
652 | upload_pack(); |
653 | return 0; | |
654 | } |