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