]>
Commit | Line | Data |
---|---|---|
ed09aef0 | 1 | #include "cache.h" |
6e122b44 | 2 | #include "tempfile.h" |
697cc8ef | 3 | #include "lockfile.h" |
ed09aef0 | 4 | #include "commit.h" |
abef3a16 | 5 | #include "tag.h" |
3125fe52 | 6 | #include "pkt-line.h" |
58babfff NTND |
7 | #include "remote.h" |
8 | #include "refs.h" | |
9 | #include "sha1-array.h" | |
10 | #include "diff.h" | |
11 | #include "revision.h" | |
12 | #include "commit-slab.h" | |
ed09aef0 JS |
13 | |
14 | static int is_shallow = -1; | |
0cc77c38 | 15 | static struct stat_validity shallow_stat; |
6035d6aa NTND |
16 | static char *alternate_shallow_file; |
17 | ||
069c0532 | 18 | void set_alternate_shallow_file(const char *path, int override) |
6035d6aa NTND |
19 | { |
20 | if (is_shallow != -1) | |
21 | die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file"); | |
069c0532 NTND |
22 | if (alternate_shallow_file && !override) |
23 | return; | |
6035d6aa | 24 | free(alternate_shallow_file); |
8c53f071 | 25 | alternate_shallow_file = xstrdup_or_null(path); |
6035d6aa | 26 | } |
ed09aef0 JS |
27 | |
28 | int register_shallow(const unsigned char *sha1) | |
29 | { | |
30 | struct commit_graft *graft = | |
31 | xmalloc(sizeof(struct commit_graft)); | |
32 | struct commit *commit = lookup_commit(sha1); | |
33 | ||
7683e2e6 | 34 | hashcpy(graft->oid.hash, sha1); |
ed09aef0 JS |
35 | graft->nr_parent = -1; |
36 | if (commit && commit->object.parsed) | |
37 | commit->parents = NULL; | |
38 | return register_commit_graft(graft, 0); | |
39 | } | |
40 | ||
f43117a6 | 41 | int is_repository_shallow(void) |
ed09aef0 JS |
42 | { |
43 | FILE *fp; | |
44 | char buf[1024]; | |
6035d6aa | 45 | const char *path = alternate_shallow_file; |
ed09aef0 JS |
46 | |
47 | if (is_shallow >= 0) | |
48 | return is_shallow; | |
49 | ||
6035d6aa | 50 | if (!path) |
f932729c | 51 | path = git_path_shallow(); |
6035d6aa NTND |
52 | /* |
53 | * fetch-pack sets '--shallow-file ""' as an indicator that no | |
54 | * shallow file should be used. We could just open it and it | |
55 | * will likely fail. But let's do an explicit check instead. | |
56 | */ | |
0cc77c38 JK |
57 | if (!*path || (fp = fopen(path, "r")) == NULL) { |
58 | stat_validity_clear(&shallow_stat); | |
ed09aef0 JS |
59 | is_shallow = 0; |
60 | return is_shallow; | |
61 | } | |
0cc77c38 | 62 | stat_validity_update(&shallow_stat, fileno(fp)); |
ed09aef0 JS |
63 | is_shallow = 1; |
64 | ||
65 | while (fgets(buf, sizeof(buf), fp)) { | |
66 | unsigned char sha1[20]; | |
67 | if (get_sha1_hex(buf, sha1)) | |
68 | die("bad shallow line: %s", buf); | |
69 | register_shallow(sha1); | |
70 | } | |
71 | fclose(fp); | |
72 | return is_shallow; | |
73 | } | |
74 | ||
f53514bc JS |
75 | struct commit_list *get_shallow_commits(struct object_array *heads, int depth, |
76 | int shallow_flag, int not_shallow_flag) | |
ed09aef0 JS |
77 | { |
78 | int i = 0, cur_depth = 0; | |
79 | struct commit_list *result = NULL; | |
3cd47459 | 80 | struct object_array stack = OBJECT_ARRAY_INIT; |
ed09aef0 | 81 | struct commit *commit = NULL; |
79d3a236 | 82 | struct commit_graft *graft; |
ed09aef0 JS |
83 | |
84 | while (commit || i < heads->nr || stack.nr) { | |
85 | struct commit_list *p; | |
86 | if (!commit) { | |
87 | if (i < heads->nr) { | |
88 | commit = (struct commit *) | |
abef3a16 | 89 | deref_tag(heads->objects[i++].item, NULL, 0); |
affeef12 | 90 | if (!commit || commit->object.type != OBJ_COMMIT) { |
ed09aef0 JS |
91 | commit = NULL; |
92 | continue; | |
93 | } | |
d64d6c9f AJ |
94 | if (!commit->util) |
95 | commit->util = xmalloc(sizeof(int)); | |
96 | *(int *)commit->util = 0; | |
ed09aef0 JS |
97 | cur_depth = 0; |
98 | } else { | |
99 | commit = (struct commit *) | |
100 | stack.objects[--stack.nr].item; | |
101 | cur_depth = *(int *)commit->util; | |
102 | } | |
103 | } | |
367068e0 | 104 | parse_commit_or_die(commit); |
ed09aef0 | 105 | cur_depth++; |
79d3a236 NTND |
106 | if ((depth != INFINITE_DEPTH && cur_depth >= depth) || |
107 | (is_repository_shallow() && !commit->parents && | |
ed1c9977 | 108 | (graft = lookup_commit_graft(commit->object.oid.hash)) != NULL && |
79d3a236 | 109 | graft->nr_parent < 0)) { |
682c7d2f NTND |
110 | commit_list_insert(commit, &result); |
111 | commit->object.flags |= shallow_flag; | |
112 | commit = NULL; | |
113 | continue; | |
114 | } | |
115 | commit->object.flags |= not_shallow_flag; | |
ed09aef0 JS |
116 | for (p = commit->parents, commit = NULL; p; p = p->next) { |
117 | if (!p->item->util) { | |
118 | int *pointer = xmalloc(sizeof(int)); | |
119 | p->item->util = pointer; | |
120 | *pointer = cur_depth; | |
121 | } else { | |
122 | int *pointer = p->item->util; | |
123 | if (cur_depth >= *pointer) | |
124 | continue; | |
125 | *pointer = cur_depth; | |
126 | } | |
4b796951 MK |
127 | if (p->next) |
128 | add_object_array(&p->item->object, | |
129 | NULL, &stack); | |
130 | else { | |
131 | commit = p->item; | |
132 | cur_depth = *(int *)commit->util; | |
f53514bc | 133 | } |
ed09aef0 JS |
134 | } |
135 | } | |
136 | ||
137 | return result; | |
138 | } | |
6035d6aa | 139 | |
167832c2 | 140 | static void check_shallow_file_for_update(void) |
6035d6aa | 141 | { |
0cc77c38 | 142 | if (is_shallow == -1) |
6035d6aa NTND |
143 | die("BUG: shallow must be initialized by now"); |
144 | ||
f932729c | 145 | if (!stat_validity_check(&shallow_stat, git_path_shallow())) |
0cc77c38 | 146 | die("shallow file has changed since we read it"); |
6035d6aa | 147 | } |
3125fe52 | 148 | |
eab3296c NTND |
149 | #define SEEN_ONLY 1 |
150 | #define VERBOSE 2 | |
151 | ||
3125fe52 NTND |
152 | struct write_shallow_data { |
153 | struct strbuf *out; | |
154 | int use_pack_protocol; | |
155 | int count; | |
eab3296c | 156 | unsigned flags; |
3125fe52 NTND |
157 | }; |
158 | ||
159 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) | |
160 | { | |
161 | struct write_shallow_data *data = cb_data; | |
7683e2e6 | 162 | const char *hex = oid_to_hex(&graft->oid); |
6a3bbb4d NTND |
163 | if (graft->nr_parent != -1) |
164 | return 0; | |
eab3296c | 165 | if (data->flags & SEEN_ONLY) { |
7683e2e6 | 166 | struct commit *c = lookup_commit(graft->oid.hash); |
eab3296c NTND |
167 | if (!c || !(c->object.flags & SEEN)) { |
168 | if (data->flags & VERBOSE) | |
169 | printf("Removing %s from .git/shallow\n", | |
f2fd0760 | 170 | oid_to_hex(&c->object.oid)); |
eab3296c NTND |
171 | return 0; |
172 | } | |
173 | } | |
3125fe52 NTND |
174 | data->count++; |
175 | if (data->use_pack_protocol) | |
176 | packet_buf_write(data->out, "shallow %s", hex); | |
177 | else { | |
178 | strbuf_addstr(data->out, hex); | |
179 | strbuf_addch(data->out, '\n'); | |
180 | } | |
181 | return 0; | |
182 | } | |
183 | ||
eab3296c NTND |
184 | static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol, |
185 | const struct sha1_array *extra, | |
186 | unsigned flags) | |
3125fe52 NTND |
187 | { |
188 | struct write_shallow_data data; | |
1a30f5a2 | 189 | int i; |
3125fe52 NTND |
190 | data.out = out; |
191 | data.use_pack_protocol = use_pack_protocol; | |
192 | data.count = 0; | |
eab3296c | 193 | data.flags = flags; |
3125fe52 | 194 | for_each_commit_graft(write_one_shallow, &data); |
1a30f5a2 NTND |
195 | if (!extra) |
196 | return data.count; | |
197 | for (i = 0; i < extra->nr; i++) { | |
198 | strbuf_addstr(out, sha1_to_hex(extra->sha1[i])); | |
199 | strbuf_addch(out, '\n'); | |
200 | data.count++; | |
201 | } | |
3125fe52 NTND |
202 | return data.count; |
203 | } | |
204 | ||
eab3296c NTND |
205 | int write_shallow_commits(struct strbuf *out, int use_pack_protocol, |
206 | const struct sha1_array *extra) | |
207 | { | |
208 | return write_shallow_commits_1(out, use_pack_protocol, extra, 0); | |
209 | } | |
210 | ||
6e122b44 | 211 | static struct tempfile temporary_shallow; |
0179c945 JK |
212 | |
213 | const char *setup_temporary_shallow(const struct sha1_array *extra) | |
08ea65ad NTND |
214 | { |
215 | struct strbuf sb = STRBUF_INIT; | |
216 | int fd; | |
217 | ||
1a30f5a2 | 218 | if (write_shallow_commits(&sb, 0, extra)) { |
6e122b44 | 219 | fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX")); |
0179c945 | 220 | |
08ea65ad NTND |
221 | if (write_in_full(fd, sb.buf, sb.len) != sb.len) |
222 | die_errno("failed to write to %s", | |
6e122b44 MH |
223 | get_tempfile_path(&temporary_shallow)); |
224 | close_tempfile(&temporary_shallow); | |
08ea65ad | 225 | strbuf_release(&sb); |
6e122b44 | 226 | return get_tempfile_path(&temporary_shallow); |
08ea65ad NTND |
227 | } |
228 | /* | |
229 | * is_repository_shallow() sees empty string as "no shallow | |
230 | * file". | |
231 | */ | |
6e122b44 | 232 | return get_tempfile_path(&temporary_shallow); |
08ea65ad NTND |
233 | } |
234 | ||
3125fe52 | 235 | void setup_alternate_shallow(struct lock_file *shallow_lock, |
1a30f5a2 NTND |
236 | const char **alternate_shallow_file, |
237 | const struct sha1_array *extra) | |
3125fe52 NTND |
238 | { |
239 | struct strbuf sb = STRBUF_INIT; | |
240 | int fd; | |
241 | ||
f932729c | 242 | fd = hold_lock_file_for_update(shallow_lock, git_path_shallow(), |
3125fe52 | 243 | LOCK_DIE_ON_ERROR); |
78396321 | 244 | check_shallow_file_for_update(); |
1a30f5a2 | 245 | if (write_shallow_commits(&sb, 0, extra)) { |
3125fe52 NTND |
246 | if (write_in_full(fd, sb.buf, sb.len) != sb.len) |
247 | die_errno("failed to write to %s", | |
b4fb09e4 MH |
248 | get_lock_file_path(shallow_lock)); |
249 | *alternate_shallow_file = get_lock_file_path(shallow_lock); | |
3125fe52 NTND |
250 | } else |
251 | /* | |
252 | * is_repository_shallow() sees empty string as "no | |
253 | * shallow file". | |
254 | */ | |
255 | *alternate_shallow_file = ""; | |
256 | strbuf_release(&sb); | |
257 | } | |
ad491366 NTND |
258 | |
259 | static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb) | |
260 | { | |
261 | int fd = *(int *)cb; | |
262 | if (graft->nr_parent == -1) | |
7683e2e6 | 263 | packet_write(fd, "shallow %s\n", oid_to_hex(&graft->oid)); |
ad491366 NTND |
264 | return 0; |
265 | } | |
266 | ||
267 | void advertise_shallow_grafts(int fd) | |
268 | { | |
269 | if (!is_repository_shallow()) | |
270 | return; | |
271 | for_each_commit_graft(advertise_shallow_grafts_cb, &fd); | |
272 | } | |
58babfff | 273 | |
eab3296c NTND |
274 | /* |
275 | * mark_reachable_objects() should have been run prior to this and all | |
276 | * reachable commits marked as "SEEN". | |
277 | */ | |
278 | void prune_shallow(int show_only) | |
279 | { | |
280 | static struct lock_file shallow_lock; | |
281 | struct strbuf sb = STRBUF_INIT; | |
282 | int fd; | |
283 | ||
284 | if (show_only) { | |
285 | write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE); | |
286 | strbuf_release(&sb); | |
287 | return; | |
288 | } | |
f932729c | 289 | fd = hold_lock_file_for_update(&shallow_lock, git_path_shallow(), |
eab3296c | 290 | LOCK_DIE_ON_ERROR); |
78396321 | 291 | check_shallow_file_for_update(); |
eab3296c NTND |
292 | if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) { |
293 | if (write_in_full(fd, sb.buf, sb.len) != sb.len) | |
294 | die_errno("failed to write to %s", | |
b4fb09e4 | 295 | get_lock_file_path(&shallow_lock)); |
eab3296c NTND |
296 | commit_lock_file(&shallow_lock); |
297 | } else { | |
f932729c | 298 | unlink(git_path_shallow()); |
eab3296c NTND |
299 | rollback_lock_file(&shallow_lock); |
300 | } | |
301 | strbuf_release(&sb); | |
302 | } | |
303 | ||
6aa30857 | 304 | struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW); |
58babfff NTND |
305 | |
306 | /* | |
307 | * Step 1, split sender shallow commits into "ours" and "theirs" | |
308 | * Step 2, clean "ours" based on .git/shallow | |
309 | */ | |
310 | void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa) | |
311 | { | |
312 | int i; | |
6aa30857 | 313 | trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n"); |
58babfff NTND |
314 | memset(info, 0, sizeof(*info)); |
315 | info->shallow = sa; | |
316 | if (!sa) | |
317 | return; | |
b32fa95f JK |
318 | ALLOC_ARRAY(info->ours, sa->nr); |
319 | ALLOC_ARRAY(info->theirs, sa->nr); | |
58babfff NTND |
320 | for (i = 0; i < sa->nr; i++) { |
321 | if (has_sha1_file(sa->sha1[i])) { | |
322 | struct commit_graft *graft; | |
323 | graft = lookup_commit_graft(sa->sha1[i]); | |
324 | if (graft && graft->nr_parent < 0) | |
325 | continue; | |
326 | info->ours[info->nr_ours++] = i; | |
327 | } else | |
328 | info->theirs[info->nr_theirs++] = i; | |
329 | } | |
330 | } | |
331 | ||
332 | void clear_shallow_info(struct shallow_info *info) | |
333 | { | |
334 | free(info->ours); | |
335 | free(info->theirs); | |
336 | } | |
337 | ||
338 | /* Step 4, remove non-existent ones in "theirs" after getting the pack */ | |
339 | ||
340 | void remove_nonexistent_theirs_shallow(struct shallow_info *info) | |
341 | { | |
342 | unsigned char (*sha1)[20] = info->shallow->sha1; | |
343 | int i, dst; | |
6aa30857 | 344 | trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n"); |
58babfff NTND |
345 | for (i = dst = 0; i < info->nr_theirs; i++) { |
346 | if (i != dst) | |
347 | info->theirs[dst] = info->theirs[i]; | |
348 | if (has_sha1_file(sha1[info->theirs[i]])) | |
349 | dst++; | |
350 | } | |
351 | info->nr_theirs = dst; | |
352 | } | |
353 | ||
8e277383 NTND |
354 | define_commit_slab(ref_bitmap, uint32_t *); |
355 | ||
356 | struct paint_info { | |
357 | struct ref_bitmap ref_bitmap; | |
358 | unsigned nr_bits; | |
359 | char **slab; | |
360 | char *free, *end; | |
361 | unsigned slab_count; | |
362 | }; | |
363 | ||
364 | static uint32_t *paint_alloc(struct paint_info *info) | |
365 | { | |
366 | unsigned nr = (info->nr_bits + 31) / 32; | |
367 | unsigned size = nr * sizeof(uint32_t); | |
368 | void *p; | |
369 | if (!info->slab_count || info->free + size > info->end) { | |
370 | info->slab_count++; | |
2756ca43 | 371 | REALLOC_ARRAY(info->slab, info->slab_count); |
8e277383 NTND |
372 | info->free = xmalloc(COMMIT_SLAB_SIZE); |
373 | info->slab[info->slab_count - 1] = info->free; | |
374 | info->end = info->free + COMMIT_SLAB_SIZE; | |
375 | } | |
376 | p = info->free; | |
377 | info->free += size; | |
378 | return p; | |
379 | } | |
380 | ||
381 | /* | |
382 | * Given a commit SHA-1, walk down to parents until either SEEN, | |
383 | * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for | |
384 | * all walked commits. | |
385 | */ | |
386 | static void paint_down(struct paint_info *info, const unsigned char *sha1, | |
387 | int id) | |
388 | { | |
389 | unsigned int i, nr; | |
390 | struct commit_list *head = NULL; | |
391 | int bitmap_nr = (info->nr_bits + 31) / 32; | |
50492f7b | 392 | size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr); |
8e277383 NTND |
393 | uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */ |
394 | uint32_t *bitmap = paint_alloc(info); | |
395 | struct commit *c = lookup_commit_reference_gently(sha1, 1); | |
396 | if (!c) | |
397 | return; | |
398 | memset(bitmap, 0, bitmap_size); | |
399 | bitmap[id / 32] |= (1 << (id % 32)); | |
400 | commit_list_insert(c, &head); | |
401 | while (head) { | |
402 | struct commit_list *p; | |
e510ab89 | 403 | struct commit *c = pop_commit(&head); |
8e277383 NTND |
404 | uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c); |
405 | ||
8e277383 NTND |
406 | /* XXX check "UNINTERESTING" from pack bitmaps if available */ |
407 | if (c->object.flags & (SEEN | UNINTERESTING)) | |
408 | continue; | |
409 | else | |
410 | c->object.flags |= SEEN; | |
411 | ||
412 | if (*refs == NULL) | |
413 | *refs = bitmap; | |
414 | else { | |
415 | memcpy(tmp, *refs, bitmap_size); | |
416 | for (i = 0; i < bitmap_nr; i++) | |
417 | tmp[i] |= bitmap[i]; | |
418 | if (memcmp(tmp, *refs, bitmap_size)) { | |
419 | *refs = paint_alloc(info); | |
420 | memcpy(*refs, tmp, bitmap_size); | |
421 | } | |
422 | } | |
423 | ||
424 | if (c->object.flags & BOTTOM) | |
425 | continue; | |
426 | ||
427 | if (parse_commit(c)) | |
428 | die("unable to parse commit %s", | |
f2fd0760 | 429 | oid_to_hex(&c->object.oid)); |
8e277383 NTND |
430 | |
431 | for (p = c->parents; p; p = p->next) { | |
432 | uint32_t **p_refs = ref_bitmap_at(&info->ref_bitmap, | |
433 | p->item); | |
434 | if (p->item->object.flags & SEEN) | |
435 | continue; | |
436 | if (*p_refs == NULL || *p_refs == *refs) | |
437 | *p_refs = *refs; | |
438 | commit_list_insert(p->item, &head); | |
439 | } | |
440 | } | |
441 | ||
442 | nr = get_max_object_index(); | |
443 | for (i = 0; i < nr; i++) { | |
444 | struct object *o = get_indexed_object(i); | |
445 | if (o && o->type == OBJ_COMMIT) | |
446 | o->flags &= ~SEEN; | |
447 | } | |
448 | ||
449 | free(tmp); | |
450 | } | |
451 | ||
580b04ef | 452 | static int mark_uninteresting(const char *refname, const struct object_id *oid, |
8e277383 NTND |
453 | int flags, void *cb_data) |
454 | { | |
580b04ef | 455 | struct commit *commit = lookup_commit_reference_gently(oid->hash, 1); |
8e277383 NTND |
456 | if (!commit) |
457 | return 0; | |
458 | commit->object.flags |= UNINTERESTING; | |
459 | mark_parents_uninteresting(commit); | |
460 | return 0; | |
461 | } | |
462 | ||
463 | static void post_assign_shallow(struct shallow_info *info, | |
464 | struct ref_bitmap *ref_bitmap, | |
465 | int *ref_status); | |
466 | /* | |
467 | * Step 6(+7), associate shallow commits with new refs | |
468 | * | |
469 | * info->ref must be initialized before calling this function. | |
470 | * | |
471 | * If used is not NULL, it's an array of info->shallow->nr | |
472 | * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the | |
473 | * m-th shallow commit from info->shallow. | |
474 | * | |
475 | * If used is NULL, "ours" and "theirs" are updated. And if ref_status | |
476 | * is not NULL it's an array of ref->nr ints. ref_status[i] is true if | |
477 | * the ref needs some shallow commits from either info->ours or | |
478 | * info->theirs. | |
479 | */ | |
480 | void assign_shallow_commits_to_refs(struct shallow_info *info, | |
481 | uint32_t **used, int *ref_status) | |
482 | { | |
483 | unsigned char (*sha1)[20] = info->shallow->sha1; | |
484 | struct sha1_array *ref = info->ref; | |
485 | unsigned int i, nr; | |
486 | int *shallow, nr_shallow = 0; | |
487 | struct paint_info pi; | |
488 | ||
6aa30857 | 489 | trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n"); |
b32fa95f | 490 | ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs); |
8e277383 NTND |
491 | for (i = 0; i < info->nr_ours; i++) |
492 | shallow[nr_shallow++] = info->ours[i]; | |
493 | for (i = 0; i < info->nr_theirs; i++) | |
494 | shallow[nr_shallow++] = info->theirs[i]; | |
495 | ||
496 | /* | |
497 | * Prepare the commit graph to track what refs can reach what | |
498 | * (new) shallow commits. | |
499 | */ | |
500 | nr = get_max_object_index(); | |
501 | for (i = 0; i < nr; i++) { | |
502 | struct object *o = get_indexed_object(i); | |
503 | if (!o || o->type != OBJ_COMMIT) | |
504 | continue; | |
505 | ||
506 | o->flags &= ~(UNINTERESTING | BOTTOM | SEEN); | |
507 | } | |
508 | ||
509 | memset(&pi, 0, sizeof(pi)); | |
510 | init_ref_bitmap(&pi.ref_bitmap); | |
511 | pi.nr_bits = ref->nr; | |
512 | ||
513 | /* | |
514 | * "--not --all" to cut short the traversal if new refs | |
515 | * connect to old refs. If not (e.g. force ref updates) it'll | |
516 | * have to go down to the current shallow commits. | |
517 | */ | |
580b04ef MH |
518 | head_ref(mark_uninteresting, NULL); |
519 | for_each_ref(mark_uninteresting, NULL); | |
8e277383 NTND |
520 | |
521 | /* Mark potential bottoms so we won't go out of bound */ | |
522 | for (i = 0; i < nr_shallow; i++) { | |
523 | struct commit *c = lookup_commit(sha1[shallow[i]]); | |
524 | c->object.flags |= BOTTOM; | |
525 | } | |
526 | ||
527 | for (i = 0; i < ref->nr; i++) | |
528 | paint_down(&pi, ref->sha1[i], i); | |
529 | ||
530 | if (used) { | |
531 | int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t); | |
532 | memset(used, 0, sizeof(*used) * info->shallow->nr); | |
533 | for (i = 0; i < nr_shallow; i++) { | |
534 | const struct commit *c = lookup_commit(sha1[shallow[i]]); | |
535 | uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c); | |
536 | if (*map) | |
537 | used[shallow[i]] = xmemdupz(*map, bitmap_size); | |
538 | } | |
539 | /* | |
540 | * unreachable shallow commits are not removed from | |
541 | * "ours" and "theirs". The user is supposed to run | |
542 | * step 7 on every ref separately and not trust "ours" | |
543 | * and "theirs" any more. | |
544 | */ | |
545 | } else | |
546 | post_assign_shallow(info, &pi.ref_bitmap, ref_status); | |
547 | ||
548 | clear_ref_bitmap(&pi.ref_bitmap); | |
549 | for (i = 0; i < pi.slab_count; i++) | |
550 | free(pi.slab[i]); | |
551 | free(pi.slab); | |
552 | free(shallow); | |
553 | } | |
554 | ||
555 | struct commit_array { | |
556 | struct commit **commits; | |
557 | int nr, alloc; | |
558 | }; | |
559 | ||
580b04ef MH |
560 | static int add_ref(const char *refname, const struct object_id *oid, |
561 | int flags, void *cb_data) | |
8e277383 NTND |
562 | { |
563 | struct commit_array *ca = cb_data; | |
564 | ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc); | |
580b04ef | 565 | ca->commits[ca->nr] = lookup_commit_reference_gently(oid->hash, 1); |
8e277383 NTND |
566 | if (ca->commits[ca->nr]) |
567 | ca->nr++; | |
568 | return 0; | |
569 | } | |
570 | ||
571 | static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap) | |
572 | { | |
573 | int i; | |
574 | if (!ref_status) | |
575 | return; | |
576 | for (i = 0; i < nr; i++) | |
577 | if (bitmap[i / 32] & (1 << (i % 32))) | |
578 | ref_status[i]++; | |
579 | } | |
580 | ||
581 | /* | |
582 | * Step 7, reachability test on "ours" at commit level | |
583 | */ | |
584 | static void post_assign_shallow(struct shallow_info *info, | |
585 | struct ref_bitmap *ref_bitmap, | |
586 | int *ref_status) | |
587 | { | |
588 | unsigned char (*sha1)[20] = info->shallow->sha1; | |
589 | struct commit *c; | |
590 | uint32_t **bitmap; | |
591 | int dst, i, j; | |
592 | int bitmap_nr = (info->ref->nr + 31) / 32; | |
593 | struct commit_array ca; | |
594 | ||
6aa30857 | 595 | trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n"); |
8e277383 NTND |
596 | if (ref_status) |
597 | memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr); | |
598 | ||
599 | /* Remove unreachable shallow commits from "theirs" */ | |
600 | for (i = dst = 0; i < info->nr_theirs; i++) { | |
601 | if (i != dst) | |
602 | info->theirs[dst] = info->theirs[i]; | |
603 | c = lookup_commit(sha1[info->theirs[i]]); | |
604 | bitmap = ref_bitmap_at(ref_bitmap, c); | |
605 | if (!*bitmap) | |
606 | continue; | |
607 | for (j = 0; j < bitmap_nr; j++) | |
608 | if (bitmap[0][j]) { | |
609 | update_refstatus(ref_status, info->ref->nr, *bitmap); | |
610 | dst++; | |
611 | break; | |
612 | } | |
613 | } | |
614 | info->nr_theirs = dst; | |
615 | ||
616 | memset(&ca, 0, sizeof(ca)); | |
580b04ef MH |
617 | head_ref(add_ref, &ca); |
618 | for_each_ref(add_ref, &ca); | |
8e277383 NTND |
619 | |
620 | /* Remove unreachable shallow commits from "ours" */ | |
621 | for (i = dst = 0; i < info->nr_ours; i++) { | |
622 | if (i != dst) | |
623 | info->ours[dst] = info->ours[i]; | |
624 | c = lookup_commit(sha1[info->ours[i]]); | |
625 | bitmap = ref_bitmap_at(ref_bitmap, c); | |
626 | if (!*bitmap) | |
627 | continue; | |
628 | for (j = 0; j < bitmap_nr; j++) | |
629 | if (bitmap[0][j] && | |
630 | /* Step 7, reachability test at commit level */ | |
631 | !in_merge_bases_many(c, ca.nr, ca.commits)) { | |
632 | update_refstatus(ref_status, info->ref->nr, *bitmap); | |
633 | dst++; | |
634 | break; | |
635 | } | |
636 | } | |
637 | info->nr_ours = dst; | |
638 | ||
639 | free(ca.commits); | |
640 | } | |
0a1bc12b NTND |
641 | |
642 | /* (Delayed) step 7, reachability test at commit level */ | |
643 | int delayed_reachability_test(struct shallow_info *si, int c) | |
644 | { | |
645 | if (si->need_reachability_test[c]) { | |
646 | struct commit *commit = lookup_commit(si->shallow->sha1[c]); | |
647 | ||
648 | if (!si->commits) { | |
649 | struct commit_array ca; | |
2b2a5be3 | 650 | |
0a1bc12b | 651 | memset(&ca, 0, sizeof(ca)); |
580b04ef MH |
652 | head_ref(add_ref, &ca); |
653 | for_each_ref(add_ref, &ca); | |
0a1bc12b NTND |
654 | si->commits = ca.commits; |
655 | si->nr_commits = ca.nr; | |
656 | } | |
657 | ||
658 | si->reachable[c] = in_merge_bases_many(commit, | |
659 | si->nr_commits, | |
660 | si->commits); | |
661 | si->need_reachability_test[c] = 0; | |
662 | } | |
663 | return si->reachable[c]; | |
664 | } |