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