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