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