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