]>
Commit | Line | Data |
---|---|---|
def88e9a | 1 | #include "cache.h" |
fb9040cc | 2 | #include "refs.h" |
def88e9a | 3 | #include "pkt-line.h" |
49bb805e JH |
4 | #include "commit.h" |
5 | #include "tag.h" | |
da093d37 | 6 | #include "exec_cmd.h" |
9e10fd1a | 7 | #include "pack.h" |
da093d37 | 8 | #include "sideband.h" |
2d4177c0 | 9 | #include "fetch-pack.h" |
477822c3 | 10 | #include "run-command.h" |
def88e9a | 11 | |
e28714c5 JH |
12 | static int transfer_unpack_limit = -1; |
13 | static int fetch_unpack_limit = -1; | |
af7cf268 | 14 | static int unpack_limit = 100; |
09149c78 DB |
15 | static struct fetch_pack_args args = { |
16 | /* .uploadpack = */ "git-upload-pack", | |
17 | }; | |
fa740529 | 18 | |
33b83034 | 19 | static const char fetch_pack_usage[] = |
83a5ad61 | 20 | "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]"; |
def88e9a | 21 | |
0a8944dd | 22 | #define COMPLETE (1U << 0) |
23d61f83 JS |
23 | #define COMMON (1U << 1) |
24 | #define COMMON_REF (1U << 2) | |
25 | #define SEEN (1U << 3) | |
26 | #define POPPED (1U << 4) | |
27 | ||
f061e5fd JH |
28 | /* |
29 | * After sending this many "have"s if we do not get any new ACK , we | |
30 | * give up traversing our history. | |
31 | */ | |
32 | #define MAX_IN_VAIN 256 | |
33 | ||
96f1e58f | 34 | static struct commit_list *rev_list; |
3891f390 | 35 | static int non_common_revs, multi_ack, use_sideband; |
23d61f83 JS |
36 | |
37 | static void rev_list_push(struct commit *commit, int mark) | |
38 | { | |
39 | if (!(commit->object.flags & mark)) { | |
40 | commit->object.flags |= mark; | |
41 | ||
42 | if (!(commit->object.parsed)) | |
43 | parse_commit(commit); | |
44 | ||
45 | insert_by_date(commit, &rev_list); | |
46 | ||
47 | if (!(commit->object.flags & COMMON)) | |
48 | non_common_revs++; | |
49 | } | |
50 | } | |
51 | ||
8da19775 | 52 | static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
23d61f83 | 53 | { |
9534f40b | 54 | struct object *o = deref_tag(parse_object(sha1), path, 0); |
23d61f83 | 55 | |
1974632c | 56 | if (o && o->type == OBJ_COMMIT) |
23d61f83 JS |
57 | rev_list_push((struct commit *)o, SEEN); |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
62 | /* | |
63 | This function marks a rev and its ancestors as common. | |
64 | In some cases, it is desirable to mark only the ancestors (for example | |
65 | when only the server does not yet know that they are common). | |
66 | */ | |
67 | ||
68 | static void mark_common(struct commit *commit, | |
69 | int ancestors_only, int dont_parse) | |
70 | { | |
71 | if (commit != NULL && !(commit->object.flags & COMMON)) { | |
72 | struct object *o = (struct object *)commit; | |
73 | ||
74 | if (!ancestors_only) | |
75 | o->flags |= COMMON; | |
76 | ||
77 | if (!(o->flags & SEEN)) | |
78 | rev_list_push(commit, SEEN); | |
79 | else { | |
80 | struct commit_list *parents; | |
81 | ||
82 | if (!ancestors_only && !(o->flags & POPPED)) | |
83 | non_common_revs--; | |
84 | if (!o->parsed && !dont_parse) | |
85 | parse_commit(commit); | |
86 | ||
87 | for (parents = commit->parents; | |
88 | parents; | |
89 | parents = parents->next) | |
90 | mark_common(parents->item, 0, dont_parse); | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | /* | |
96 | Get the next rev to send, ignoring the common. | |
97 | */ | |
98 | ||
962554c6 | 99 | static const unsigned char* get_rev(void) |
23d61f83 JS |
100 | { |
101 | struct commit *commit = NULL; | |
102 | ||
103 | while (commit == NULL) { | |
104 | unsigned int mark; | |
105 | struct commit_list* parents; | |
106 | ||
107 | if (rev_list == NULL || non_common_revs == 0) | |
108 | return NULL; | |
109 | ||
110 | commit = rev_list->item; | |
111 | if (!(commit->object.parsed)) | |
112 | parse_commit(commit); | |
113 | commit->object.flags |= POPPED; | |
114 | if (!(commit->object.flags & COMMON)) | |
115 | non_common_revs--; | |
a6080a0a | 116 | |
23d61f83 JS |
117 | parents = commit->parents; |
118 | ||
119 | if (commit->object.flags & COMMON) { | |
120 | /* do not send "have", and ignore ancestors */ | |
121 | commit = NULL; | |
122 | mark = COMMON | SEEN; | |
123 | } else if (commit->object.flags & COMMON_REF) | |
124 | /* send "have", and ignore ancestors */ | |
125 | mark = COMMON | SEEN; | |
126 | else | |
127 | /* send "have", also for its ancestors */ | |
128 | mark = SEEN; | |
129 | ||
130 | while (parents) { | |
131 | if (!(parents->item->object.flags & SEEN)) | |
132 | rev_list_push(parents->item, mark); | |
133 | if (mark & COMMON) | |
134 | mark_common(parents->item, 1, 0); | |
135 | parents = parents->next; | |
136 | } | |
137 | ||
138 | rev_list = rev_list->next; | |
139 | } | |
140 | ||
141 | return commit->object.sha1; | |
142 | } | |
0a8944dd | 143 | |
33b83034 JH |
144 | static int find_common(int fd[2], unsigned char *result_sha1, |
145 | struct ref *refs) | |
def88e9a | 146 | { |
2759cbc7 | 147 | int fetching; |
23d61f83 JS |
148 | int count = 0, flushes = 0, retval; |
149 | const unsigned char *sha1; | |
f061e5fd JH |
150 | unsigned in_vain = 0; |
151 | int got_continue = 0; | |
23d61f83 | 152 | |
cb5d709f | 153 | for_each_ref(rev_list_insert_ref, NULL); |
def88e9a | 154 | |
2759cbc7 LT |
155 | fetching = 0; |
156 | for ( ; refs ; refs = refs->next) { | |
33b83034 | 157 | unsigned char *remote = refs->old_sha1; |
4dab94d5 | 158 | struct object *o; |
2759cbc7 | 159 | |
0a8944dd | 160 | /* |
4dab94d5 JH |
161 | * If that object is complete (i.e. it is an ancestor of a |
162 | * local ref), we tell them we have it but do not have to | |
163 | * tell them about its ancestors, which they already know | |
164 | * about. | |
f1f0a2be JH |
165 | * |
166 | * We use lookup_object here because we are only | |
167 | * interested in the case we *know* the object is | |
168 | * reachable and we have already scanned it. | |
4dab94d5 | 169 | */ |
f1f0a2be | 170 | if (((o = lookup_object(remote)) != NULL) && |
1baaae5e | 171 | (o->flags & COMPLETE)) { |
2759cbc7 | 172 | continue; |
0a8944dd | 173 | } |
23d61f83 | 174 | |
583b7ea3 | 175 | if (!fetching) |
b0e90897 | 176 | packet_write(fd[1], "want %s%s%s%s%s%s%s\n", |
583b7ea3 JH |
177 | sha1_to_hex(remote), |
178 | (multi_ack ? " multi_ack" : ""), | |
d47f3db7 JH |
179 | (use_sideband == 2 ? " side-band-64k" : ""), |
180 | (use_sideband == 1 ? " side-band" : ""), | |
3891f390 | 181 | (args.use_thin_pack ? " thin-pack" : ""), |
fa740529 | 182 | (args.no_progress ? " no-progress" : ""), |
e4fe4b8e | 183 | " ofs-delta"); |
583b7ea3 JH |
184 | else |
185 | packet_write(fd[1], "want %s\n", sha1_to_hex(remote)); | |
2759cbc7 | 186 | fetching++; |
33b83034 | 187 | } |
ed09aef0 JS |
188 | if (is_repository_shallow()) |
189 | write_shallow_commits(fd[1], 1); | |
fa740529 SP |
190 | if (args.depth > 0) |
191 | packet_write(fd[1], "deepen %d", args.depth); | |
fb9040cc | 192 | packet_flush(fd[1]); |
2759cbc7 LT |
193 | if (!fetching) |
194 | return 1; | |
0a8944dd | 195 | |
fa740529 | 196 | if (args.depth > 0) { |
016e6ccb JS |
197 | char line[1024]; |
198 | unsigned char sha1[20]; | |
199 | int len; | |
200 | ||
201 | while ((len = packet_read_line(fd[0], line, sizeof(line)))) { | |
599065a3 | 202 | if (!prefixcmp(line, "shallow ")) { |
016e6ccb JS |
203 | if (get_sha1_hex(line + 8, sha1)) |
204 | die("invalid shallow line: %s", line); | |
016e6ccb | 205 | register_shallow(sha1); |
cf01bd52 JH |
206 | continue; |
207 | } | |
599065a3 | 208 | if (!prefixcmp(line, "unshallow ")) { |
f53514bc JS |
209 | if (get_sha1_hex(line + 10, sha1)) |
210 | die("invalid unshallow line: %s", line); | |
211 | if (!lookup_object(sha1)) | |
212 | die("object not found: %s", line); | |
213 | /* make sure that it is parsed as shallow */ | |
214 | parse_object(sha1); | |
215 | if (unregister_shallow(sha1)) | |
216 | die("no shallow found: %s", line); | |
cf01bd52 JH |
217 | continue; |
218 | } | |
219 | die("expected shallow/unshallow, got %s", line); | |
016e6ccb JS |
220 | } |
221 | } | |
222 | ||
23d61f83 | 223 | flushes = 0; |
75bfc6c2 | 224 | retval = -1; |
23d61f83 | 225 | while ((sha1 = get_rev())) { |
def88e9a | 226 | packet_write(fd[1], "have %s\n", sha1_to_hex(sha1)); |
fa740529 | 227 | if (args.verbose) |
33b83034 | 228 | fprintf(stderr, "have %s\n", sha1_to_hex(sha1)); |
f061e5fd | 229 | in_vain++; |
def88e9a | 230 | if (!(31 & ++count)) { |
c4c86f07 JS |
231 | int ack; |
232 | ||
def88e9a LT |
233 | packet_flush(fd[1]); |
234 | flushes++; | |
235 | ||
236 | /* | |
237 | * We keep one window "ahead" of the other side, and | |
238 | * will wait for an ACK only on the next one | |
239 | */ | |
240 | if (count == 32) | |
241 | continue; | |
c4c86f07 JS |
242 | |
243 | do { | |
244 | ack = get_ack(fd[0], result_sha1); | |
fa740529 | 245 | if (args.verbose && ack) |
c4c86f07 JS |
246 | fprintf(stderr, "got ack %d %s\n", ack, |
247 | sha1_to_hex(result_sha1)); | |
248 | if (ack == 1) { | |
249 | flushes = 0; | |
250 | multi_ack = 0; | |
251 | retval = 0; | |
252 | goto done; | |
253 | } else if (ack == 2) { | |
254 | struct commit *commit = | |
255 | lookup_commit(result_sha1); | |
256 | mark_common(commit, 0, 1); | |
257 | retval = 0; | |
f061e5fd JH |
258 | in_vain = 0; |
259 | got_continue = 1; | |
c4c86f07 JS |
260 | } |
261 | } while (ack); | |
def88e9a | 262 | flushes--; |
f061e5fd | 263 | if (got_continue && MAX_IN_VAIN < in_vain) { |
fa740529 | 264 | if (args.verbose) |
f061e5fd JH |
265 | fprintf(stderr, "giving up\n"); |
266 | break; /* give up */ | |
267 | } | |
def88e9a LT |
268 | } |
269 | } | |
c4c86f07 | 270 | done: |
75bfc6c2 | 271 | packet_write(fd[1], "done\n"); |
fa740529 | 272 | if (args.verbose) |
33b83034 | 273 | fprintf(stderr, "done\n"); |
c4c86f07 JS |
274 | if (retval != 0) { |
275 | multi_ack = 0; | |
23d61f83 | 276 | flushes++; |
c4c86f07 JS |
277 | } |
278 | while (flushes || multi_ack) { | |
279 | int ack = get_ack(fd[0], result_sha1); | |
280 | if (ack) { | |
fa740529 | 281 | if (args.verbose) |
c4c86f07 JS |
282 | fprintf(stderr, "got ack (%d) %s\n", ack, |
283 | sha1_to_hex(result_sha1)); | |
284 | if (ack == 1) | |
285 | return 0; | |
286 | multi_ack = 1; | |
287 | continue; | |
33b83034 | 288 | } |
c4c86f07 | 289 | flushes--; |
def88e9a | 290 | } |
75bfc6c2 | 291 | return retval; |
def88e9a LT |
292 | } |
293 | ||
96f1e58f | 294 | static struct commit_list *complete; |
49bb805e | 295 | |
8da19775 | 296 | static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
49bb805e JH |
297 | { |
298 | struct object *o = parse_object(sha1); | |
299 | ||
1974632c | 300 | while (o && o->type == OBJ_TAG) { |
f1f0a2be JH |
301 | struct tag *t = (struct tag *) o; |
302 | if (!t->tagged) | |
303 | break; /* broken repository */ | |
49bb805e | 304 | o->flags |= COMPLETE; |
f1f0a2be | 305 | o = parse_object(t->tagged->sha1); |
49bb805e | 306 | } |
1974632c | 307 | if (o && o->type == OBJ_COMMIT) { |
49bb805e JH |
308 | struct commit *commit = (struct commit *)o; |
309 | commit->object.flags |= COMPLETE; | |
310 | insert_by_date(commit, &complete); | |
311 | } | |
312 | return 0; | |
313 | } | |
314 | ||
315 | static void mark_recent_complete_commits(unsigned long cutoff) | |
316 | { | |
317 | while (complete && cutoff <= complete->item->date) { | |
fa740529 | 318 | if (args.verbose) |
49bb805e JH |
319 | fprintf(stderr, "Marking %s as complete\n", |
320 | sha1_to_hex(complete->item->object.sha1)); | |
321 | pop_most_recent_commit(&complete, COMPLETE); | |
322 | } | |
323 | } | |
324 | ||
1baaae5e JS |
325 | static void filter_refs(struct ref **refs, int nr_match, char **match) |
326 | { | |
9546010b JH |
327 | struct ref **return_refs; |
328 | struct ref *newlist = NULL; | |
329 | struct ref **newtail = &newlist; | |
330 | struct ref *ref, *next; | |
331 | struct ref *fastarray[32]; | |
332 | ||
fa740529 | 333 | if (nr_match && !args.fetch_all) { |
9546010b JH |
334 | if (ARRAY_SIZE(fastarray) < nr_match) |
335 | return_refs = xcalloc(nr_match, sizeof(struct ref *)); | |
336 | else { | |
337 | return_refs = fastarray; | |
338 | memset(return_refs, 0, sizeof(struct ref *) * nr_match); | |
339 | } | |
340 | } | |
341 | else | |
342 | return_refs = NULL; | |
343 | ||
344 | for (ref = *refs; ref; ref = next) { | |
345 | next = ref->next; | |
346 | if (!memcmp(ref->name, "refs/", 5) && | |
347 | check_ref_format(ref->name + 5)) | |
348 | ; /* trash */ | |
fa740529 SP |
349 | else if (args.fetch_all && |
350 | (!args.depth || prefixcmp(ref->name, "refs/tags/") )) { | |
9546010b JH |
351 | *newtail = ref; |
352 | ref->next = NULL; | |
353 | newtail = &ref->next; | |
354 | continue; | |
355 | } | |
356 | else { | |
357 | int order = path_match(ref->name, nr_match, match); | |
358 | if (order) { | |
359 | return_refs[order-1] = ref; | |
360 | continue; /* we will link it later */ | |
361 | } | |
362 | } | |
363 | free(ref); | |
364 | } | |
365 | ||
fa740529 | 366 | if (!args.fetch_all) { |
9546010b JH |
367 | int i; |
368 | for (i = 0; i < nr_match; i++) { | |
369 | ref = return_refs[i]; | |
370 | if (ref) { | |
371 | *newtail = ref; | |
372 | ref->next = NULL; | |
373 | newtail = &ref->next; | |
374 | } | |
375 | } | |
376 | if (return_refs != fastarray) | |
377 | free(return_refs); | |
1baaae5e | 378 | } |
9546010b | 379 | *refs = newlist; |
1baaae5e JS |
380 | } |
381 | ||
382 | static int everything_local(struct ref **refs, int nr_match, char **match) | |
2759cbc7 | 383 | { |
49bb805e | 384 | struct ref *ref; |
2759cbc7 | 385 | int retval; |
49bb805e JH |
386 | unsigned long cutoff = 0; |
387 | ||
388 | track_object_refs = 0; | |
389 | save_commit_buffer = 0; | |
390 | ||
1baaae5e | 391 | for (ref = *refs; ref; ref = ref->next) { |
49bb805e JH |
392 | struct object *o; |
393 | ||
394 | o = parse_object(ref->old_sha1); | |
395 | if (!o) | |
396 | continue; | |
397 | ||
398 | /* We already have it -- which may mean that we were | |
399 | * in sync with the other side at some time after | |
400 | * that (it is OK if we guess wrong here). | |
401 | */ | |
1974632c | 402 | if (o->type == OBJ_COMMIT) { |
49bb805e JH |
403 | struct commit *commit = (struct commit *)o; |
404 | if (!cutoff || cutoff < commit->date) | |
405 | cutoff = commit->date; | |
406 | } | |
407 | } | |
408 | ||
fa740529 | 409 | if (!args.depth) { |
f53514bc JS |
410 | for_each_ref(mark_complete, NULL); |
411 | if (cutoff) | |
412 | mark_recent_complete_commits(cutoff); | |
413 | } | |
2759cbc7 | 414 | |
1baaae5e JS |
415 | /* |
416 | * Mark all complete remote refs as common refs. | |
417 | * Don't mark them common yet; the server has to be told so first. | |
418 | */ | |
419 | for (ref = *refs; ref; ref = ref->next) { | |
9534f40b JH |
420 | struct object *o = deref_tag(lookup_object(ref->old_sha1), |
421 | NULL, 0); | |
1baaae5e | 422 | |
1974632c | 423 | if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE)) |
1baaae5e JS |
424 | continue; |
425 | ||
426 | if (!(o->flags & SEEN)) { | |
427 | rev_list_push((struct commit *)o, COMMON_REF | SEEN); | |
428 | ||
429 | mark_common((struct commit *)o, 1, 1); | |
430 | } | |
431 | } | |
432 | ||
433 | filter_refs(refs, nr_match, match); | |
434 | ||
435 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { | |
436 | const unsigned char *remote = ref->old_sha1; | |
2759cbc7 | 437 | unsigned char local[20]; |
49bb805e | 438 | struct object *o; |
2759cbc7 | 439 | |
1baaae5e | 440 | o = lookup_object(remote); |
49bb805e | 441 | if (!o || !(o->flags & COMPLETE)) { |
2759cbc7 | 442 | retval = 0; |
fa740529 | 443 | if (!args.verbose) |
2759cbc7 LT |
444 | continue; |
445 | fprintf(stderr, | |
446 | "want %s (%s)\n", sha1_to_hex(remote), | |
1baaae5e | 447 | ref->name); |
2759cbc7 LT |
448 | continue; |
449 | } | |
450 | ||
e702496e | 451 | hashcpy(ref->new_sha1, local); |
fa740529 | 452 | if (!args.verbose) |
2759cbc7 LT |
453 | continue; |
454 | fprintf(stderr, | |
455 | "already have %s (%s)\n", sha1_to_hex(remote), | |
1baaae5e | 456 | ref->name); |
2759cbc7 LT |
457 | } |
458 | return retval; | |
459 | } | |
460 | ||
088fab5f | 461 | static int sideband_demux(int fd, void *data) |
da093d37 | 462 | { |
088fab5f | 463 | int *xd = data; |
da093d37 | 464 | |
088fab5f JS |
465 | return recv_sideband("fetch-pack", xd[0], fd, 2); |
466 | } | |
467 | ||
1788c39c | 468 | static int get_pack(int xd[2], char **pack_lockfile) |
da093d37 | 469 | { |
088fab5f | 470 | struct async demux; |
9e10fd1a JH |
471 | const char *argv[20]; |
472 | char keep_arg[256]; | |
473 | char hdr_arg[256]; | |
474 | const char **av; | |
fa740529 | 475 | int do_keep = args.keep_pack; |
477822c3 | 476 | struct child_process cmd; |
da093d37 | 477 | |
1f759eee JS |
478 | memset(&demux, 0, sizeof(demux)); |
479 | if (use_sideband) { | |
480 | /* xd[] is talking with upload-pack; subprocess reads from | |
481 | * xd[0], spits out band#2 to stderr, and feeds us band#1 | |
482 | * through demux->out. | |
483 | */ | |
484 | demux.proc = sideband_demux; | |
485 | demux.data = xd; | |
486 | if (start_async(&demux)) | |
487 | die("fetch-pack: unable to fork off sideband" | |
488 | " demultiplexer"); | |
489 | } | |
490 | else | |
491 | demux.out = xd[0]; | |
9e10fd1a | 492 | |
477822c3 JS |
493 | memset(&cmd, 0, sizeof(cmd)); |
494 | cmd.argv = argv; | |
9e10fd1a JH |
495 | av = argv; |
496 | *hdr_arg = 0; | |
fa740529 | 497 | if (!args.keep_pack && unpack_limit) { |
9e10fd1a JH |
498 | struct pack_header header; |
499 | ||
1f759eee | 500 | if (read_pack_header(demux.out, &header)) |
9e10fd1a JH |
501 | die("protocol error: bad pack header"); |
502 | snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u", | |
503 | ntohl(header.hdr_version), ntohl(header.hdr_entries)); | |
af7cf268 | 504 | if (ntohl(header.hdr_entries) < unpack_limit) |
9e10fd1a JH |
505 | do_keep = 0; |
506 | else | |
507 | do_keep = 1; | |
508 | } | |
509 | ||
510 | if (do_keep) { | |
477822c3 JS |
511 | if (pack_lockfile) |
512 | cmd.out = -1; | |
9e10fd1a JH |
513 | *av++ = "index-pack"; |
514 | *av++ = "--stdin"; | |
fa740529 | 515 | if (!args.quiet && !args.no_progress) |
9e10fd1a | 516 | *av++ = "-v"; |
fa740529 | 517 | if (args.use_thin_pack) |
9e10fd1a | 518 | *av++ = "--fix-thin"; |
fa740529 | 519 | if (args.lock_pack || unpack_limit) { |
9e10fd1a JH |
520 | int s = sprintf(keep_arg, |
521 | "--keep=fetch-pack %d on ", getpid()); | |
522 | if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) | |
523 | strcpy(keep_arg + s, "localhost"); | |
524 | *av++ = keep_arg; | |
525 | } | |
526 | } | |
527 | else { | |
528 | *av++ = "unpack-objects"; | |
fa740529 | 529 | if (args.quiet) |
9e10fd1a JH |
530 | *av++ = "-q"; |
531 | } | |
532 | if (*hdr_arg) | |
533 | *av++ = hdr_arg; | |
534 | *av++ = NULL; | |
535 | ||
1f759eee | 536 | cmd.in = demux.out; |
477822c3 JS |
537 | cmd.git_cmd = 1; |
538 | if (start_command(&cmd)) | |
da093d37 | 539 | die("fetch-pack: unable to fork off %s", argv[0]); |
477822c3 JS |
540 | if (do_keep && pack_lockfile) |
541 | *pack_lockfile = index_pack_lockfile(cmd.out); | |
542 | ||
543 | if (finish_command(&cmd)) | |
544 | die("%s failed", argv[0]); | |
088fab5f JS |
545 | if (use_sideband && finish_async(&demux)) |
546 | die("error in sideband demultiplexer"); | |
477822c3 | 547 | return 0; |
da093d37 NP |
548 | } |
549 | ||
1788c39c SP |
550 | static struct ref *do_fetch_pack(int fd[2], |
551 | int nr_match, | |
552 | char **match, | |
553 | char **pack_lockfile) | |
def88e9a | 554 | { |
d1c133f5 LT |
555 | struct ref *ref; |
556 | unsigned char sha1[20]; | |
def88e9a | 557 | |
1baaae5e | 558 | get_remote_heads(fd[0], &ref, 0, NULL, 0); |
ed09aef0 JS |
559 | if (is_repository_shallow() && !server_supports("shallow")) |
560 | die("Server does not support shallow clients"); | |
c4c86f07 | 561 | if (server_supports("multi_ack")) { |
fa740529 | 562 | if (args.verbose) |
c4c86f07 JS |
563 | fprintf(stderr, "Server supports multi_ack\n"); |
564 | multi_ack = 1; | |
565 | } | |
d47f3db7 | 566 | if (server_supports("side-band-64k")) { |
fa740529 | 567 | if (args.verbose) |
d47f3db7 JH |
568 | fprintf(stderr, "Server supports side-band-64k\n"); |
569 | use_sideband = 2; | |
570 | } | |
571 | else if (server_supports("side-band")) { | |
fa740529 | 572 | if (args.verbose) |
583b7ea3 JH |
573 | fprintf(stderr, "Server supports side-band\n"); |
574 | use_sideband = 1; | |
575 | } | |
d1c133f5 LT |
576 | if (!ref) { |
577 | packet_flush(fd[1]); | |
578 | die("no matching remote head"); | |
579 | } | |
1baaae5e | 580 | if (everything_local(&ref, nr_match, match)) { |
2759cbc7 LT |
581 | packet_flush(fd[1]); |
582 | goto all_done; | |
583 | } | |
33b83034 | 584 | if (find_common(fd, sha1, ref) < 0) |
fa740529 | 585 | if (!args.keep_pack) |
dfeff66e JH |
586 | /* When cloning, it is not unusual to have |
587 | * no common commit. | |
588 | */ | |
589 | fprintf(stderr, "warning: no common commits\n"); | |
ad897215 | 590 | |
1788c39c | 591 | if (get_pack(fd, pack_lockfile)) |
ad897215 JH |
592 | die("git-fetch-pack: fetch failed."); |
593 | ||
594 | all_done: | |
2d4177c0 | 595 | return ref; |
def88e9a LT |
596 | } |
597 | ||
310b86d4 JH |
598 | static int remove_duplicates(int nr_heads, char **heads) |
599 | { | |
600 | int src, dst; | |
601 | ||
602 | for (src = dst = 0; src < nr_heads; src++) { | |
603 | /* If heads[src] is different from any of | |
604 | * heads[0..dst], push it in. | |
605 | */ | |
606 | int i; | |
607 | for (i = 0; i < dst; i++) { | |
608 | if (!strcmp(heads[i], heads[src])) | |
609 | break; | |
610 | } | |
611 | if (i < dst) | |
612 | continue; | |
613 | if (src != dst) | |
614 | heads[dst] = heads[src]; | |
615 | dst++; | |
616 | } | |
310b86d4 JH |
617 | return dst; |
618 | } | |
619 | ||
af7cf268 JH |
620 | static int fetch_pack_config(const char *var, const char *value) |
621 | { | |
622 | if (strcmp(var, "fetch.unpacklimit") == 0) { | |
e28714c5 JH |
623 | fetch_unpack_limit = git_config_int(var, value); |
624 | return 0; | |
625 | } | |
626 | ||
627 | if (strcmp(var, "transfer.unpacklimit") == 0) { | |
628 | transfer_unpack_limit = git_config_int(var, value); | |
af7cf268 JH |
629 | return 0; |
630 | } | |
631 | ||
632 | return git_default_config(var, value); | |
633 | } | |
634 | ||
54b9e022 JH |
635 | static struct lock_file lock; |
636 | ||
50ab5fd3 | 637 | static void fetch_pack_setup(void) |
def88e9a | 638 | { |
50ab5fd3 SP |
639 | static int did_setup; |
640 | if (did_setup) | |
641 | return; | |
af7cf268 | 642 | git_config(fetch_pack_config); |
e28714c5 JH |
643 | if (0 <= transfer_unpack_limit) |
644 | unpack_limit = transfer_unpack_limit; | |
645 | else if (0 <= fetch_unpack_limit) | |
646 | unpack_limit = fetch_unpack_limit; | |
50ab5fd3 SP |
647 | did_setup = 1; |
648 | } | |
649 | ||
650 | int cmd_fetch_pack(int argc, const char **argv, const char *prefix) | |
651 | { | |
652 | int i, ret, nr_heads; | |
653 | struct ref *ref; | |
654 | char *dest = NULL, **heads; | |
e28714c5 | 655 | |
def88e9a LT |
656 | nr_heads = 0; |
657 | heads = NULL; | |
658 | for (i = 1; i < argc; i++) { | |
2d4177c0 | 659 | const char *arg = argv[i]; |
def88e9a LT |
660 | |
661 | if (*arg == '-') { | |
599065a3 | 662 | if (!prefixcmp(arg, "--upload-pack=")) { |
fa740529 | 663 | args.uploadpack = arg + 14; |
27dca07f UKK |
664 | continue; |
665 | } | |
599065a3 | 666 | if (!prefixcmp(arg, "--exec=")) { |
fa740529 | 667 | args.uploadpack = arg + 7; |
8b3d9dc0 JH |
668 | continue; |
669 | } | |
2247efb4 | 670 | if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { |
fa740529 | 671 | args.quiet = 1; |
33b83034 JH |
672 | continue; |
673 | } | |
2247efb4 | 674 | if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { |
fa740529 SP |
675 | args.lock_pack = args.keep_pack; |
676 | args.keep_pack = 1; | |
9e10fd1a JH |
677 | continue; |
678 | } | |
b19696c2 | 679 | if (!strcmp("--thin", arg)) { |
fa740529 | 680 | args.use_thin_pack = 1; |
b19696c2 JH |
681 | continue; |
682 | } | |
dfeff66e | 683 | if (!strcmp("--all", arg)) { |
fa740529 | 684 | args.fetch_all = 1; |
dfeff66e JH |
685 | continue; |
686 | } | |
33b83034 | 687 | if (!strcmp("-v", arg)) { |
fa740529 | 688 | args.verbose = 1; |
33b83034 JH |
689 | continue; |
690 | } | |
599065a3 | 691 | if (!prefixcmp(arg, "--depth=")) { |
fa740529 | 692 | args.depth = strtol(arg + 8, NULL, 0); |
016e6ccb JS |
693 | continue; |
694 | } | |
83a5ad61 | 695 | if (!strcmp("--no-progress", arg)) { |
fa740529 | 696 | args.no_progress = 1; |
83a5ad61 JS |
697 | continue; |
698 | } | |
def88e9a LT |
699 | usage(fetch_pack_usage); |
700 | } | |
2d4177c0 DB |
701 | dest = (char *)arg; |
702 | heads = (char **)(argv + i + 1); | |
def88e9a LT |
703 | nr_heads = argc - i - 1; |
704 | break; | |
705 | } | |
706 | if (!dest) | |
707 | usage(fetch_pack_usage); | |
2d4177c0 | 708 | |
fa740529 | 709 | ref = fetch_pack(&args, dest, nr_heads, heads, NULL); |
2d4177c0 DB |
710 | ret = !ref; |
711 | ||
712 | while (ref) { | |
713 | printf("%s %s\n", | |
714 | sha1_to_hex(ref->old_sha1), ref->name); | |
715 | ref = ref->next; | |
716 | } | |
717 | ||
718 | return ret; | |
719 | } | |
720 | ||
fa740529 SP |
721 | struct ref *fetch_pack(struct fetch_pack_args *my_args, |
722 | const char *dest, | |
1788c39c SP |
723 | int nr_heads, |
724 | char **heads, | |
725 | char **pack_lockfile) | |
2d4177c0 DB |
726 | { |
727 | int i, ret; | |
728 | int fd[2]; | |
98158e9c | 729 | struct child_process *conn; |
2d4177c0 DB |
730 | struct ref *ref; |
731 | struct stat st; | |
732 | ||
50ab5fd3 | 733 | fetch_pack_setup(); |
fa740529 SP |
734 | memcpy(&args, my_args, sizeof(args)); |
735 | if (args.depth > 0) { | |
2d4177c0 DB |
736 | if (stat(git_path("shallow"), &st)) |
737 | st.st_mtime = 0; | |
738 | } | |
739 | ||
4340a813 | 740 | conn = git_connect(fd, (char *)dest, args.uploadpack, |
fa740529 | 741 | args.verbose ? CONNECT_VERBOSE : 0); |
310b86d4 JH |
742 | if (heads && nr_heads) |
743 | nr_heads = remove_duplicates(nr_heads, heads); | |
1788c39c | 744 | ref = do_fetch_pack(fd, nr_heads, heads, pack_lockfile); |
def88e9a LT |
745 | close(fd[0]); |
746 | close(fd[1]); | |
98158e9c | 747 | ret = finish_connect(conn); |
9e5d2b40 JH |
748 | |
749 | if (!ret && nr_heads) { | |
750 | /* If the heads to pull were given, we should have | |
751 | * consumed all of them by matching the remote. | |
752 | * Otherwise, 'git-fetch remote no-such-ref' would | |
753 | * silently succeed without issuing an error. | |
754 | */ | |
755 | for (i = 0; i < nr_heads; i++) | |
756 | if (heads[i] && heads[i][0]) { | |
757 | error("no such remote ref %s", heads[i]); | |
758 | ret = 1; | |
759 | } | |
760 | } | |
761 | ||
fa740529 | 762 | if (!ret && args.depth > 0) { |
016e6ccb JS |
763 | struct cache_time mtime; |
764 | char *shallow = git_path("shallow"); | |
765 | int fd; | |
766 | ||
767 | mtime.sec = st.st_mtime; | |
768 | #ifdef USE_NSEC | |
769 | mtime.usec = st.st_mtim.usec; | |
770 | #endif | |
771 | if (stat(shallow, &st)) { | |
772 | if (mtime.sec) | |
773 | die("shallow file was removed during fetch"); | |
774 | } else if (st.st_mtime != mtime.sec | |
775 | #ifdef USE_NSEC | |
776 | || st.st_mtim.usec != mtime.usec | |
777 | #endif | |
778 | ) | |
779 | die("shallow file was changed during fetch"); | |
780 | ||
781 | fd = hold_lock_file_for_update(&lock, shallow, 1); | |
782 | if (!write_shallow_commits(fd, 0)) { | |
d6491e3a | 783 | unlink(shallow); |
016e6ccb JS |
784 | rollback_lock_file(&lock); |
785 | } else { | |
016e6ccb JS |
786 | commit_lock_file(&lock); |
787 | } | |
788 | } | |
789 | ||
2d4177c0 DB |
790 | if (ret) |
791 | ref = NULL; | |
792 | ||
793 | return ref; | |
def88e9a | 794 | } |