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