2 #include "repository.h"
5 #include "object-store.h"
11 #include "sha1-array.h"
14 #include "commit-slab.h"
16 #include "list-objects.h"
17 #include "commit-slab.h"
18 #include "repository.h"
19 #include "commit-reach.h"
21 void set_alternate_shallow_file(struct repository *r, const char *path, int override)
23 if (r->parsed_objects->is_shallow != -1)
24 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
25 if (r->parsed_objects->alternate_shallow_file && !override)
27 free(r->parsed_objects->alternate_shallow_file);
28 r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
31 int register_shallow(struct repository *r, const struct object_id *oid)
33 struct commit_graft *graft =
34 xmalloc(sizeof(struct commit_graft));
35 struct commit *commit = lookup_commit(the_repository, oid);
37 oidcpy(&graft->oid, oid);
38 graft->nr_parent = -1;
39 if (commit && commit->object.parsed)
40 commit->parents = NULL;
41 return register_commit_graft(r, graft, 0);
44 int is_repository_shallow(struct repository *r)
48 const char *path = r->parsed_objects->alternate_shallow_file;
50 if (r->parsed_objects->is_shallow >= 0)
51 return r->parsed_objects->is_shallow;
54 path = git_path_shallow(r);
56 * fetch-pack sets '--shallow-file ""' as an indicator that no
57 * shallow file should be used. We could just open it and it
58 * will likely fail. But let's do an explicit check instead.
60 if (!*path || (fp = fopen(path, "r")) == NULL) {
61 stat_validity_clear(r->parsed_objects->shallow_stat);
62 r->parsed_objects->is_shallow = 0;
63 return r->parsed_objects->is_shallow;
65 stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
66 r->parsed_objects->is_shallow = 1;
68 while (fgets(buf, sizeof(buf), fp)) {
70 if (get_oid_hex(buf, &oid))
71 die("bad shallow line: %s", buf);
72 register_shallow(r, &oid);
75 return r->parsed_objects->is_shallow;
79 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
80 * supports a "valid" flag.
82 define_commit_slab(commit_depth, int *);
83 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
84 int shallow_flag, int not_shallow_flag)
86 int i = 0, cur_depth = 0;
87 struct commit_list *result = NULL;
88 struct object_array stack = OBJECT_ARRAY_INIT;
89 struct commit *commit = NULL;
90 struct commit_graft *graft;
91 struct commit_depth depths;
93 init_commit_depth(&depths);
94 while (commit || i < heads->nr || stack.nr) {
95 struct commit_list *p;
99 commit = (struct commit *)
100 deref_tag(the_repository,
101 heads->objects[i++].item,
103 if (!commit || commit->object.type != OBJ_COMMIT) {
107 depth_slot = commit_depth_at(&depths, commit);
109 *depth_slot = xmalloc(sizeof(int));
113 commit = (struct commit *)
114 object_array_pop(&stack);
115 cur_depth = **commit_depth_at(&depths, commit);
118 parse_commit_or_die(commit);
120 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
121 (is_repository_shallow(the_repository) && !commit->parents &&
122 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
123 graft->nr_parent < 0)) {
124 commit_list_insert(commit, &result);
125 commit->object.flags |= shallow_flag;
129 commit->object.flags |= not_shallow_flag;
130 for (p = commit->parents, commit = NULL; p; p = p->next) {
131 int **depth_slot = commit_depth_at(&depths, p->item);
133 *depth_slot = xmalloc(sizeof(int));
134 **depth_slot = cur_depth;
136 if (cur_depth >= **depth_slot)
138 **depth_slot = cur_depth;
141 add_object_array(&p->item->object,
145 cur_depth = **commit_depth_at(&depths, commit);
149 for (i = 0; i < depths.slab_count; i++) {
152 for (j = 0; j < depths.slab_size; j++)
153 free(depths.slab[i][j]);
155 clear_commit_depth(&depths);
160 static void show_commit(struct commit *commit, void *data)
162 commit_list_insert(commit, data);
166 * Given rev-list arguments, run rev-list. All reachable commits
167 * except border ones are marked with not_shallow_flag. Border commits
168 * are marked with shallow_flag. The list of border/shallow commits
171 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
173 int not_shallow_flag)
175 struct commit_list *result = NULL, *p;
176 struct commit_list *not_shallow_list = NULL;
177 struct rev_info revs;
178 int both_flags = shallow_flag | not_shallow_flag;
181 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
182 * set at this point. But better be safe than sorry.
184 clear_object_flags(both_flags);
186 is_repository_shallow(the_repository); /* make sure shallows are read */
188 repo_init_revisions(the_repository, &revs, NULL);
189 save_commit_buffer = 0;
190 setup_revisions(ac, av, &revs, NULL);
192 if (prepare_revision_walk(&revs))
193 die("revision walk setup failed");
194 traverse_commit_list(&revs, show_commit, NULL, ¬_shallow_list);
196 if (!not_shallow_list)
197 die("no commits selected for shallow requests");
199 /* Mark all reachable commits as NOT_SHALLOW */
200 for (p = not_shallow_list; p; p = p->next)
201 p->item->object.flags |= not_shallow_flag;
204 * mark border commits SHALLOW + NOT_SHALLOW.
205 * We cannot clear NOT_SHALLOW right now. Imagine border
206 * commit A is processed first, then commit B, whose parent is
207 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
208 * itself is considered border at step 2, which is incorrect.
210 for (p = not_shallow_list; p; p = p->next) {
211 struct commit *c = p->item;
212 struct commit_list *parent;
215 die("unable to parse commit %s",
216 oid_to_hex(&c->object.oid));
218 for (parent = c->parents; parent; parent = parent->next)
219 if (!(parent->item->object.flags & not_shallow_flag)) {
220 c->object.flags |= shallow_flag;
221 commit_list_insert(c, &result);
225 free_commit_list(not_shallow_list);
228 * Now we can clean up NOT_SHALLOW on border commits. Having
229 * both flags set can confuse the caller.
231 for (p = result; p; p = p->next) {
232 struct object *o = &p->item->object;
233 if ((o->flags & both_flags) == both_flags)
234 o->flags &= ~not_shallow_flag;
239 static void check_shallow_file_for_update(struct repository *r)
241 if (r->parsed_objects->is_shallow == -1)
242 BUG("shallow must be initialized by now");
244 if (!stat_validity_check(r->parsed_objects->shallow_stat, git_path_shallow(the_repository)))
245 die("shallow file has changed since we read it");
252 struct write_shallow_data {
254 int use_pack_protocol;
259 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
261 struct write_shallow_data *data = cb_data;
262 const char *hex = oid_to_hex(&graft->oid);
263 if (graft->nr_parent != -1)
265 if (data->flags & QUICK) {
266 if (!has_object_file(&graft->oid))
268 } else if (data->flags & SEEN_ONLY) {
269 struct commit *c = lookup_commit(the_repository, &graft->oid);
270 if (!c || !(c->object.flags & SEEN)) {
271 if (data->flags & VERBOSE)
272 printf("Removing %s from .git/shallow\n",
273 oid_to_hex(&c->object.oid));
278 if (data->use_pack_protocol)
279 packet_buf_write(data->out, "shallow %s", hex);
281 strbuf_addstr(data->out, hex);
282 strbuf_addch(data->out, '\n');
287 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
288 const struct oid_array *extra,
291 struct write_shallow_data data;
294 data.use_pack_protocol = use_pack_protocol;
297 for_each_commit_graft(write_one_shallow, &data);
300 for (i = 0; i < extra->nr; i++) {
301 strbuf_addstr(out, oid_to_hex(extra->oid + i));
302 strbuf_addch(out, '\n');
308 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
309 const struct oid_array *extra)
311 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
314 const char *setup_temporary_shallow(const struct oid_array *extra)
316 struct tempfile *temp;
317 struct strbuf sb = STRBUF_INIT;
319 if (write_shallow_commits(&sb, 0, extra)) {
320 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
322 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
323 close_tempfile_gently(temp) < 0)
324 die_errno("failed to write to %s",
325 get_tempfile_path(temp));
327 return get_tempfile_path(temp);
330 * is_repository_shallow() sees empty string as "no shallow
336 void setup_alternate_shallow(struct lock_file *shallow_lock,
337 const char **alternate_shallow_file,
338 const struct oid_array *extra)
340 struct strbuf sb = STRBUF_INIT;
343 fd = hold_lock_file_for_update(shallow_lock,
344 git_path_shallow(the_repository),
346 check_shallow_file_for_update(the_repository);
347 if (write_shallow_commits(&sb, 0, extra)) {
348 if (write_in_full(fd, sb.buf, sb.len) < 0)
349 die_errno("failed to write to %s",
350 get_lock_file_path(shallow_lock));
351 *alternate_shallow_file = get_lock_file_path(shallow_lock);
354 * is_repository_shallow() sees empty string as "no
357 *alternate_shallow_file = "";
361 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
364 if (graft->nr_parent == -1)
365 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
369 void advertise_shallow_grafts(int fd)
371 if (!is_repository_shallow(the_repository))
373 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
377 * mark_reachable_objects() should have been run prior to this and all
378 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
379 * in which case lines are excised from the shallow file if they refer to
380 * commits that do not exist (any longer).
382 void prune_shallow(unsigned options)
384 struct lock_file shallow_lock = LOCK_INIT;
385 struct strbuf sb = STRBUF_INIT;
386 unsigned flags = SEEN_ONLY;
389 if (options & PRUNE_QUICK)
392 if (options & PRUNE_SHOW_ONLY) {
394 write_shallow_commits_1(&sb, 0, NULL, flags);
398 fd = hold_lock_file_for_update(&shallow_lock,
399 git_path_shallow(the_repository),
401 check_shallow_file_for_update(the_repository);
402 if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
403 if (write_in_full(fd, sb.buf, sb.len) < 0)
404 die_errno("failed to write to %s",
405 get_lock_file_path(&shallow_lock));
406 commit_lock_file(&shallow_lock);
408 unlink(git_path_shallow(the_repository));
409 rollback_lock_file(&shallow_lock);
414 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
417 * Step 1, split sender shallow commits into "ours" and "theirs"
418 * Step 2, clean "ours" based on .git/shallow
420 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
423 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
424 memset(info, 0, sizeof(*info));
428 ALLOC_ARRAY(info->ours, sa->nr);
429 ALLOC_ARRAY(info->theirs, sa->nr);
430 for (i = 0; i < sa->nr; i++) {
431 if (has_object_file(sa->oid + i)) {
432 struct commit_graft *graft;
433 graft = lookup_commit_graft(the_repository,
435 if (graft && graft->nr_parent < 0)
437 info->ours[info->nr_ours++] = i;
439 info->theirs[info->nr_theirs++] = i;
443 void clear_shallow_info(struct shallow_info *info)
449 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
451 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
453 struct object_id *oid = info->shallow->oid;
455 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
456 for (i = dst = 0; i < info->nr_theirs; i++) {
458 info->theirs[dst] = info->theirs[i];
459 if (has_object_file(oid + info->theirs[i]))
462 info->nr_theirs = dst;
465 define_commit_slab(ref_bitmap, uint32_t *);
467 #define POOL_SIZE (512 * 1024)
470 struct ref_bitmap ref_bitmap;
477 static uint32_t *paint_alloc(struct paint_info *info)
479 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
480 unsigned size = nr * sizeof(uint32_t);
482 if (!info->pool_count || size > info->end - info->free) {
483 if (size > POOL_SIZE)
484 BUG("pool size too small for %d in paint_alloc()",
487 REALLOC_ARRAY(info->pools, info->pool_count);
488 info->free = xmalloc(POOL_SIZE);
489 info->pools[info->pool_count - 1] = info->free;
490 info->end = info->free + POOL_SIZE;
498 * Given a commit SHA-1, walk down to parents until either SEEN,
499 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
500 * all walked commits.
502 static void paint_down(struct paint_info *info, const struct object_id *oid,
506 struct commit_list *head = NULL;
507 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
508 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
509 struct commit *c = lookup_commit_reference_gently(the_repository, oid,
511 uint32_t *tmp; /* to be freed before return */
517 tmp = xmalloc(bitmap_size);
518 bitmap = paint_alloc(info);
519 memset(bitmap, 0, bitmap_size);
520 bitmap[id / 32] |= (1U << (id % 32));
521 commit_list_insert(c, &head);
523 struct commit_list *p;
524 struct commit *c = pop_commit(&head);
525 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
527 /* XXX check "UNINTERESTING" from pack bitmaps if available */
528 if (c->object.flags & (SEEN | UNINTERESTING))
531 c->object.flags |= SEEN;
536 memcpy(tmp, *refs, bitmap_size);
537 for (i = 0; i < bitmap_nr; i++)
539 if (memcmp(tmp, *refs, bitmap_size)) {
540 *refs = paint_alloc(info);
541 memcpy(*refs, tmp, bitmap_size);
545 if (c->object.flags & BOTTOM)
549 die("unable to parse commit %s",
550 oid_to_hex(&c->object.oid));
552 for (p = c->parents; p; p = p->next) {
553 if (p->item->object.flags & SEEN)
555 commit_list_insert(p->item, &head);
559 nr = get_max_object_index();
560 for (i = 0; i < nr; i++) {
561 struct object *o = get_indexed_object(i);
562 if (o && o->type == OBJ_COMMIT)
569 static int mark_uninteresting(const char *refname, const struct object_id *oid,
570 int flags, void *cb_data)
572 struct commit *commit = lookup_commit_reference_gently(the_repository,
576 commit->object.flags |= UNINTERESTING;
577 mark_parents_uninteresting(commit);
581 static void post_assign_shallow(struct shallow_info *info,
582 struct ref_bitmap *ref_bitmap,
585 * Step 6(+7), associate shallow commits with new refs
587 * info->ref must be initialized before calling this function.
589 * If used is not NULL, it's an array of info->shallow->nr
590 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
591 * m-th shallow commit from info->shallow.
593 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
594 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
595 * the ref needs some shallow commits from either info->ours or
598 void assign_shallow_commits_to_refs(struct shallow_info *info,
599 uint32_t **used, int *ref_status)
601 struct object_id *oid = info->shallow->oid;
602 struct oid_array *ref = info->ref;
604 int *shallow, nr_shallow = 0;
605 struct paint_info pi;
607 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
608 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
609 for (i = 0; i < info->nr_ours; i++)
610 shallow[nr_shallow++] = info->ours[i];
611 for (i = 0; i < info->nr_theirs; i++)
612 shallow[nr_shallow++] = info->theirs[i];
615 * Prepare the commit graph to track what refs can reach what
616 * (new) shallow commits.
618 nr = get_max_object_index();
619 for (i = 0; i < nr; i++) {
620 struct object *o = get_indexed_object(i);
621 if (!o || o->type != OBJ_COMMIT)
624 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
627 memset(&pi, 0, sizeof(pi));
628 init_ref_bitmap(&pi.ref_bitmap);
629 pi.nr_bits = ref->nr;
632 * "--not --all" to cut short the traversal if new refs
633 * connect to old refs. If not (e.g. force ref updates) it'll
634 * have to go down to the current shallow commits.
636 head_ref(mark_uninteresting, NULL);
637 for_each_ref(mark_uninteresting, NULL);
639 /* Mark potential bottoms so we won't go out of bound */
640 for (i = 0; i < nr_shallow; i++) {
641 struct commit *c = lookup_commit(the_repository,
643 c->object.flags |= BOTTOM;
646 for (i = 0; i < ref->nr; i++)
647 paint_down(&pi, ref->oid + i, i);
650 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
651 memset(used, 0, sizeof(*used) * info->shallow->nr);
652 for (i = 0; i < nr_shallow; i++) {
653 const struct commit *c = lookup_commit(the_repository,
655 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
657 used[shallow[i]] = xmemdupz(*map, bitmap_size);
660 * unreachable shallow commits are not removed from
661 * "ours" and "theirs". The user is supposed to run
662 * step 7 on every ref separately and not trust "ours"
663 * and "theirs" any more.
666 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
668 clear_ref_bitmap(&pi.ref_bitmap);
669 for (i = 0; i < pi.pool_count; i++)
675 struct commit_array {
676 struct commit **commits;
680 static int add_ref(const char *refname, const struct object_id *oid,
681 int flags, void *cb_data)
683 struct commit_array *ca = cb_data;
684 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
685 ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
687 if (ca->commits[ca->nr])
692 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
697 for (i = 0; i < nr; i++)
698 if (bitmap[i / 32] & (1U << (i % 32)))
703 * Step 7, reachability test on "ours" at commit level
705 static void post_assign_shallow(struct shallow_info *info,
706 struct ref_bitmap *ref_bitmap,
709 struct object_id *oid = info->shallow->oid;
713 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
714 struct commit_array ca;
716 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
718 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
720 /* Remove unreachable shallow commits from "theirs" */
721 for (i = dst = 0; i < info->nr_theirs; i++) {
723 info->theirs[dst] = info->theirs[i];
724 c = lookup_commit(the_repository, &oid[info->theirs[i]]);
725 bitmap = ref_bitmap_at(ref_bitmap, c);
728 for (j = 0; j < bitmap_nr; j++)
730 update_refstatus(ref_status, info->ref->nr, *bitmap);
735 info->nr_theirs = dst;
737 memset(&ca, 0, sizeof(ca));
738 head_ref(add_ref, &ca);
739 for_each_ref(add_ref, &ca);
741 /* Remove unreachable shallow commits from "ours" */
742 for (i = dst = 0; i < info->nr_ours; i++) {
744 info->ours[dst] = info->ours[i];
745 c = lookup_commit(the_repository, &oid[info->ours[i]]);
746 bitmap = ref_bitmap_at(ref_bitmap, c);
749 for (j = 0; j < bitmap_nr; j++)
751 /* Step 7, reachability test at commit level */
752 !in_merge_bases_many(c, ca.nr, ca.commits)) {
753 update_refstatus(ref_status, info->ref->nr, *bitmap);
763 /* (Delayed) step 7, reachability test at commit level */
764 int delayed_reachability_test(struct shallow_info *si, int c)
766 if (si->need_reachability_test[c]) {
767 struct commit *commit = lookup_commit(the_repository,
768 &si->shallow->oid[c]);
771 struct commit_array ca;
773 memset(&ca, 0, sizeof(ca));
774 head_ref(add_ref, &ca);
775 for_each_ref(add_ref, &ca);
776 si->commits = ca.commits;
777 si->nr_commits = ca.nr;
780 si->reachable[c] = in_merge_bases_many(commit,
783 si->need_reachability_test[c] = 0;
785 return si->reachable[c];