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