9 #include "sha1-array.h"
12 #include "commit-slab.h"
14 static int is_shallow = -1;
15 static struct stat_validity shallow_stat;
16 static char *alternate_shallow_file;
18 void set_alternate_shallow_file(const char *path, int override)
21 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
22 if (alternate_shallow_file && !override)
24 free(alternate_shallow_file);
25 alternate_shallow_file = xstrdup_or_null(path);
28 int register_shallow(const unsigned char *sha1)
30 struct commit_graft *graft =
31 xmalloc(sizeof(struct commit_graft));
32 struct commit *commit = lookup_commit(sha1);
34 hashcpy(graft->oid.hash, sha1);
35 graft->nr_parent = -1;
36 if (commit && commit->object.parsed)
37 commit->parents = NULL;
38 return register_commit_graft(graft, 0);
41 int is_repository_shallow(void)
45 const char *path = alternate_shallow_file;
51 path = git_path_shallow();
53 * fetch-pack sets '--shallow-file ""' as an indicator that no
54 * shallow file should be used. We could just open it and it
55 * will likely fail. But let's do an explicit check instead.
57 if (!*path || (fp = fopen(path, "r")) == NULL) {
58 stat_validity_clear(&shallow_stat);
62 stat_validity_update(&shallow_stat, fileno(fp));
65 while (fgets(buf, sizeof(buf), fp)) {
66 unsigned char sha1[20];
67 if (get_sha1_hex(buf, sha1))
68 die("bad shallow line: %s", buf);
69 register_shallow(sha1);
75 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
76 int shallow_flag, int not_shallow_flag)
78 int i = 0, cur_depth = 0;
79 struct commit_list *result = NULL;
80 struct object_array stack = OBJECT_ARRAY_INIT;
81 struct commit *commit = NULL;
82 struct commit_graft *graft;
84 while (commit || i < heads->nr || stack.nr) {
85 struct commit_list *p;
88 commit = (struct commit *)
89 deref_tag(heads->objects[i++].item, NULL, 0);
90 if (!commit || commit->object.type != OBJ_COMMIT) {
95 commit->util = xmalloc(sizeof(int));
96 *(int *)commit->util = 0;
99 commit = (struct commit *)
100 stack.objects[--stack.nr].item;
101 cur_depth = *(int *)commit->util;
104 parse_commit_or_die(commit);
106 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
107 (is_repository_shallow() && !commit->parents &&
108 (graft = lookup_commit_graft(commit->object.oid.hash)) != NULL &&
109 graft->nr_parent < 0)) {
110 commit_list_insert(commit, &result);
111 commit->object.flags |= shallow_flag;
115 commit->object.flags |= not_shallow_flag;
116 for (p = commit->parents, commit = NULL; p; p = p->next) {
117 if (!p->item->util) {
118 int *pointer = xmalloc(sizeof(int));
119 p->item->util = pointer;
120 *pointer = cur_depth;
122 int *pointer = p->item->util;
123 if (cur_depth >= *pointer)
125 *pointer = cur_depth;
128 add_object_array(&p->item->object,
132 cur_depth = *(int *)commit->util;
140 static void check_shallow_file_for_update(void)
142 if (is_shallow == -1)
143 die("BUG: shallow must be initialized by now");
145 if (!stat_validity_check(&shallow_stat, git_path_shallow()))
146 die("shallow file has changed since we read it");
152 struct write_shallow_data {
154 int use_pack_protocol;
159 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
161 struct write_shallow_data *data = cb_data;
162 const char *hex = oid_to_hex(&graft->oid);
163 if (graft->nr_parent != -1)
165 if (data->flags & SEEN_ONLY) {
166 struct commit *c = lookup_commit(graft->oid.hash);
167 if (!c || !(c->object.flags & SEEN)) {
168 if (data->flags & VERBOSE)
169 printf("Removing %s from .git/shallow\n",
170 oid_to_hex(&c->object.oid));
175 if (data->use_pack_protocol)
176 packet_buf_write(data->out, "shallow %s", hex);
178 strbuf_addstr(data->out, hex);
179 strbuf_addch(data->out, '\n');
184 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
185 const struct sha1_array *extra,
188 struct write_shallow_data data;
191 data.use_pack_protocol = use_pack_protocol;
194 for_each_commit_graft(write_one_shallow, &data);
197 for (i = 0; i < extra->nr; i++) {
198 strbuf_addstr(out, sha1_to_hex(extra->sha1[i]));
199 strbuf_addch(out, '\n');
205 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
206 const struct sha1_array *extra)
208 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
211 static struct tempfile temporary_shallow;
213 const char *setup_temporary_shallow(const struct sha1_array *extra)
215 struct strbuf sb = STRBUF_INIT;
218 if (write_shallow_commits(&sb, 0, extra)) {
219 fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX"));
221 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
222 die_errno("failed to write to %s",
223 get_tempfile_path(&temporary_shallow));
224 close_tempfile(&temporary_shallow);
226 return get_tempfile_path(&temporary_shallow);
229 * is_repository_shallow() sees empty string as "no shallow
232 return get_tempfile_path(&temporary_shallow);
235 void setup_alternate_shallow(struct lock_file *shallow_lock,
236 const char **alternate_shallow_file,
237 const struct sha1_array *extra)
239 struct strbuf sb = STRBUF_INIT;
242 fd = hold_lock_file_for_update(shallow_lock, git_path_shallow(),
244 check_shallow_file_for_update();
245 if (write_shallow_commits(&sb, 0, extra)) {
246 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
247 die_errno("failed to write to %s",
248 get_lock_file_path(shallow_lock));
249 *alternate_shallow_file = get_lock_file_path(shallow_lock);
252 * is_repository_shallow() sees empty string as "no
255 *alternate_shallow_file = "";
259 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
262 if (graft->nr_parent == -1)
263 packet_write(fd, "shallow %s\n", oid_to_hex(&graft->oid));
267 void advertise_shallow_grafts(int fd)
269 if (!is_repository_shallow())
271 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
275 * mark_reachable_objects() should have been run prior to this and all
276 * reachable commits marked as "SEEN".
278 void prune_shallow(int show_only)
280 static struct lock_file shallow_lock;
281 struct strbuf sb = STRBUF_INIT;
285 write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
289 fd = hold_lock_file_for_update(&shallow_lock, git_path_shallow(),
291 check_shallow_file_for_update();
292 if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
293 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
294 die_errno("failed to write to %s",
295 get_lock_file_path(&shallow_lock));
296 commit_lock_file(&shallow_lock);
298 unlink(git_path_shallow());
299 rollback_lock_file(&shallow_lock);
304 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
307 * Step 1, split sender shallow commits into "ours" and "theirs"
308 * Step 2, clean "ours" based on .git/shallow
310 void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
313 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
314 memset(info, 0, sizeof(*info));
318 ALLOC_ARRAY(info->ours, sa->nr);
319 ALLOC_ARRAY(info->theirs, sa->nr);
320 for (i = 0; i < sa->nr; i++) {
321 if (has_sha1_file(sa->sha1[i])) {
322 struct commit_graft *graft;
323 graft = lookup_commit_graft(sa->sha1[i]);
324 if (graft && graft->nr_parent < 0)
326 info->ours[info->nr_ours++] = i;
328 info->theirs[info->nr_theirs++] = i;
332 void clear_shallow_info(struct shallow_info *info)
338 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
340 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
342 unsigned char (*sha1)[20] = info->shallow->sha1;
344 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
345 for (i = dst = 0; i < info->nr_theirs; i++) {
347 info->theirs[dst] = info->theirs[i];
348 if (has_sha1_file(sha1[info->theirs[i]]))
351 info->nr_theirs = dst;
354 define_commit_slab(ref_bitmap, uint32_t *);
356 #define POOL_SIZE (512 * 1024)
359 struct ref_bitmap ref_bitmap;
366 static uint32_t *paint_alloc(struct paint_info *info)
368 unsigned nr = (info->nr_bits + 31) / 32;
369 unsigned size = nr * sizeof(uint32_t);
371 if (!info->pool_count || size > info->end - info->free) {
372 if (size > POOL_SIZE)
373 die("BUG: pool size too small for %d in paint_alloc()",
376 REALLOC_ARRAY(info->pools, info->pool_count);
377 info->free = xmalloc(POOL_SIZE);
378 info->pools[info->pool_count - 1] = info->free;
379 info->end = info->free + POOL_SIZE;
387 * Given a commit SHA-1, walk down to parents until either SEEN,
388 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
389 * all walked commits.
391 static void paint_down(struct paint_info *info, const unsigned char *sha1,
395 struct commit_list *head = NULL;
396 int bitmap_nr = (info->nr_bits + 31) / 32;
397 size_t bitmap_size = st_mult(bitmap_nr, sizeof(uint32_t));
398 uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */
399 uint32_t *bitmap = paint_alloc(info);
400 struct commit *c = lookup_commit_reference_gently(sha1, 1);
403 memset(bitmap, 0, bitmap_size);
404 bitmap[id / 32] |= (1 << (id % 32));
405 commit_list_insert(c, &head);
407 struct commit_list *p;
408 struct commit *c = pop_commit(&head);
409 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
411 /* XXX check "UNINTERESTING" from pack bitmaps if available */
412 if (c->object.flags & (SEEN | UNINTERESTING))
415 c->object.flags |= SEEN;
420 memcpy(tmp, *refs, bitmap_size);
421 for (i = 0; i < bitmap_nr; i++)
423 if (memcmp(tmp, *refs, bitmap_size)) {
424 *refs = paint_alloc(info);
425 memcpy(*refs, tmp, bitmap_size);
429 if (c->object.flags & BOTTOM)
433 die("unable to parse commit %s",
434 oid_to_hex(&c->object.oid));
436 for (p = c->parents; p; p = p->next) {
437 uint32_t **p_refs = ref_bitmap_at(&info->ref_bitmap,
439 if (p->item->object.flags & SEEN)
441 if (*p_refs == NULL || *p_refs == *refs)
443 commit_list_insert(p->item, &head);
447 nr = get_max_object_index();
448 for (i = 0; i < nr; i++) {
449 struct object *o = get_indexed_object(i);
450 if (o && o->type == OBJ_COMMIT)
457 static int mark_uninteresting(const char *refname, const struct object_id *oid,
458 int flags, void *cb_data)
460 struct commit *commit = lookup_commit_reference_gently(oid->hash, 1);
463 commit->object.flags |= UNINTERESTING;
464 mark_parents_uninteresting(commit);
468 static void post_assign_shallow(struct shallow_info *info,
469 struct ref_bitmap *ref_bitmap,
472 * Step 6(+7), associate shallow commits with new refs
474 * info->ref must be initialized before calling this function.
476 * If used is not NULL, it's an array of info->shallow->nr
477 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
478 * m-th shallow commit from info->shallow.
480 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
481 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
482 * the ref needs some shallow commits from either info->ours or
485 void assign_shallow_commits_to_refs(struct shallow_info *info,
486 uint32_t **used, int *ref_status)
488 unsigned char (*sha1)[20] = info->shallow->sha1;
489 struct sha1_array *ref = info->ref;
491 int *shallow, nr_shallow = 0;
492 struct paint_info pi;
494 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
495 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
496 for (i = 0; i < info->nr_ours; i++)
497 shallow[nr_shallow++] = info->ours[i];
498 for (i = 0; i < info->nr_theirs; i++)
499 shallow[nr_shallow++] = info->theirs[i];
502 * Prepare the commit graph to track what refs can reach what
503 * (new) shallow commits.
505 nr = get_max_object_index();
506 for (i = 0; i < nr; i++) {
507 struct object *o = get_indexed_object(i);
508 if (!o || o->type != OBJ_COMMIT)
511 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
514 memset(&pi, 0, sizeof(pi));
515 init_ref_bitmap(&pi.ref_bitmap);
516 pi.nr_bits = ref->nr;
519 * "--not --all" to cut short the traversal if new refs
520 * connect to old refs. If not (e.g. force ref updates) it'll
521 * have to go down to the current shallow commits.
523 head_ref(mark_uninteresting, NULL);
524 for_each_ref(mark_uninteresting, NULL);
526 /* Mark potential bottoms so we won't go out of bound */
527 for (i = 0; i < nr_shallow; i++) {
528 struct commit *c = lookup_commit(sha1[shallow[i]]);
529 c->object.flags |= BOTTOM;
532 for (i = 0; i < ref->nr; i++)
533 paint_down(&pi, ref->sha1[i], i);
536 int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
537 memset(used, 0, sizeof(*used) * info->shallow->nr);
538 for (i = 0; i < nr_shallow; i++) {
539 const struct commit *c = lookup_commit(sha1[shallow[i]]);
540 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
542 used[shallow[i]] = xmemdupz(*map, bitmap_size);
545 * unreachable shallow commits are not removed from
546 * "ours" and "theirs". The user is supposed to run
547 * step 7 on every ref separately and not trust "ours"
548 * and "theirs" any more.
551 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
553 clear_ref_bitmap(&pi.ref_bitmap);
554 for (i = 0; i < pi.pool_count; i++)
560 struct commit_array {
561 struct commit **commits;
565 static int add_ref(const char *refname, const struct object_id *oid,
566 int flags, void *cb_data)
568 struct commit_array *ca = cb_data;
569 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
570 ca->commits[ca->nr] = lookup_commit_reference_gently(oid->hash, 1);
571 if (ca->commits[ca->nr])
576 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
581 for (i = 0; i < nr; i++)
582 if (bitmap[i / 32] & (1 << (i % 32)))
587 * Step 7, reachability test on "ours" at commit level
589 static void post_assign_shallow(struct shallow_info *info,
590 struct ref_bitmap *ref_bitmap,
593 unsigned char (*sha1)[20] = info->shallow->sha1;
597 int bitmap_nr = (info->ref->nr + 31) / 32;
598 struct commit_array ca;
600 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
602 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
604 /* Remove unreachable shallow commits from "theirs" */
605 for (i = dst = 0; i < info->nr_theirs; i++) {
607 info->theirs[dst] = info->theirs[i];
608 c = lookup_commit(sha1[info->theirs[i]]);
609 bitmap = ref_bitmap_at(ref_bitmap, c);
612 for (j = 0; j < bitmap_nr; j++)
614 update_refstatus(ref_status, info->ref->nr, *bitmap);
619 info->nr_theirs = dst;
621 memset(&ca, 0, sizeof(ca));
622 head_ref(add_ref, &ca);
623 for_each_ref(add_ref, &ca);
625 /* Remove unreachable shallow commits from "ours" */
626 for (i = dst = 0; i < info->nr_ours; i++) {
628 info->ours[dst] = info->ours[i];
629 c = lookup_commit(sha1[info->ours[i]]);
630 bitmap = ref_bitmap_at(ref_bitmap, c);
633 for (j = 0; j < bitmap_nr; j++)
635 /* Step 7, reachability test at commit level */
636 !in_merge_bases_many(c, ca.nr, ca.commits)) {
637 update_refstatus(ref_status, info->ref->nr, *bitmap);
647 /* (Delayed) step 7, reachability test at commit level */
648 int delayed_reachability_test(struct shallow_info *si, int c)
650 if (si->need_reachability_test[c]) {
651 struct commit *commit = lookup_commit(si->shallow->sha1[c]);
654 struct commit_array ca;
656 memset(&ca, 0, sizeof(ca));
657 head_ref(add_ref, &ca);
658 for_each_ref(add_ref, &ca);
659 si->commits = ca.commits;
660 si->nr_commits = ca.nr;
663 si->reachable[c] = in_merge_bases_many(commit,
666 si->need_reachability_test[c] = 0;
668 return si->reachable[c];