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