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