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