shallow: add repository argument to is_repository_shallow
[git.git] / shallow.c
blobe903651202b9f6c77646de0a7888661ba74d1816
1 #include "cache.h"
2 #include "repository.h"
3 #include "tempfile.h"
4 #include "lockfile.h"
5 #include "object-store.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "pkt-line.h"
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"
15 #include "revision.h"
16 #include "list-objects.h"
18 static int is_shallow = -1;
19 static struct stat_validity shallow_stat;
20 static char *alternate_shallow_file;
22 void set_alternate_shallow_file_the_repository(const char *path, int override)
24 if (is_shallow != -1)
25 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
26 if (alternate_shallow_file && !override)
27 return;
28 free(alternate_shallow_file);
29 alternate_shallow_file = xstrdup_or_null(path);
32 int register_shallow_the_repository(const struct object_id *oid)
34 struct commit_graft *graft =
35 xmalloc(sizeof(struct commit_graft));
36 struct commit *commit = lookup_commit(oid);
38 oidcpy(&graft->oid, oid);
39 graft->nr_parent = -1;
40 if (commit && commit->object.parsed)
41 commit->parents = NULL;
42 return register_commit_graft(the_repository, graft, 0);
45 int is_repository_shallow_the_repository(void)
47 FILE *fp;
48 char buf[1024];
49 const char *path = alternate_shallow_file;
51 if (is_shallow >= 0)
52 return is_shallow;
54 if (!path)
55 path = git_path_shallow();
57 * fetch-pack sets '--shallow-file ""' as an indicator that no
58 * shallow file should be used. We could just open it and it
59 * will likely fail. But let's do an explicit check instead.
61 if (!*path || (fp = fopen(path, "r")) == NULL) {
62 stat_validity_clear(&shallow_stat);
63 is_shallow = 0;
64 return is_shallow;
66 stat_validity_update(&shallow_stat, fileno(fp));
67 is_shallow = 1;
69 while (fgets(buf, sizeof(buf), fp)) {
70 struct object_id oid;
71 if (get_oid_hex(buf, &oid))
72 die("bad shallow line: %s", buf);
73 register_shallow(the_repository, &oid);
75 fclose(fp);
76 return is_shallow;
79 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
80 int shallow_flag, int not_shallow_flag)
82 int i = 0, cur_depth = 0;
83 struct commit_list *result = NULL;
84 struct object_array stack = OBJECT_ARRAY_INIT;
85 struct commit *commit = NULL;
86 struct commit_graft *graft;
88 while (commit || i < heads->nr || stack.nr) {
89 struct commit_list *p;
90 if (!commit) {
91 if (i < heads->nr) {
92 commit = (struct commit *)
93 deref_tag(heads->objects[i++].item, NULL, 0);
94 if (!commit || commit->object.type != OBJ_COMMIT) {
95 commit = NULL;
96 continue;
98 if (!commit->util)
99 commit->util = xmalloc(sizeof(int));
100 *(int *)commit->util = 0;
101 cur_depth = 0;
102 } else {
103 commit = (struct commit *)
104 object_array_pop(&stack);
105 cur_depth = *(int *)commit->util;
108 parse_commit_or_die(commit);
109 cur_depth++;
110 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
111 (is_repository_shallow(the_repository) && !commit->parents &&
112 (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
113 graft->nr_parent < 0)) {
114 commit_list_insert(commit, &result);
115 commit->object.flags |= shallow_flag;
116 commit = NULL;
117 continue;
119 commit->object.flags |= not_shallow_flag;
120 for (p = commit->parents, commit = NULL; p; p = p->next) {
121 if (!p->item->util) {
122 int *pointer = xmalloc(sizeof(int));
123 p->item->util = pointer;
124 *pointer = cur_depth;
125 } else {
126 int *pointer = p->item->util;
127 if (cur_depth >= *pointer)
128 continue;
129 *pointer = cur_depth;
131 if (p->next)
132 add_object_array(&p->item->object,
133 NULL, &stack);
134 else {
135 commit = p->item;
136 cur_depth = *(int *)commit->util;
141 return result;
144 static void show_commit(struct commit *commit, void *data)
146 commit_list_insert(commit, data);
150 * Given rev-list arguments, run rev-list. All reachable commits
151 * except border ones are marked with not_shallow_flag. Border commits
152 * are marked with shallow_flag. The list of border/shallow commits
153 * are also returned.
155 struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
156 int shallow_flag,
157 int not_shallow_flag)
159 struct commit_list *result = NULL, *p;
160 struct commit_list *not_shallow_list = NULL;
161 struct rev_info revs;
162 int both_flags = shallow_flag | not_shallow_flag;
165 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
166 * set at this point. But better be safe than sorry.
168 clear_object_flags(both_flags);
170 is_repository_shallow(the_repository); /* make sure shallows are read */
172 init_revisions(&revs, NULL);
173 save_commit_buffer = 0;
174 setup_revisions(ac, av, &revs, NULL);
176 if (prepare_revision_walk(&revs))
177 die("revision walk setup failed");
178 traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
180 /* Mark all reachable commits as NOT_SHALLOW */
181 for (p = not_shallow_list; p; p = p->next)
182 p->item->object.flags |= not_shallow_flag;
185 * mark border commits SHALLOW + NOT_SHALLOW.
186 * We cannot clear NOT_SHALLOW right now. Imagine border
187 * commit A is processed first, then commit B, whose parent is
188 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
189 * itself is considered border at step 2, which is incorrect.
191 for (p = not_shallow_list; p; p = p->next) {
192 struct commit *c = p->item;
193 struct commit_list *parent;
195 if (parse_commit(c))
196 die("unable to parse commit %s",
197 oid_to_hex(&c->object.oid));
199 for (parent = c->parents; parent; parent = parent->next)
200 if (!(parent->item->object.flags & not_shallow_flag)) {
201 c->object.flags |= shallow_flag;
202 commit_list_insert(c, &result);
203 break;
206 free_commit_list(not_shallow_list);
209 * Now we can clean up NOT_SHALLOW on border commits. Having
210 * both flags set can confuse the caller.
212 for (p = result; p; p = p->next) {
213 struct object *o = &p->item->object;
214 if ((o->flags & both_flags) == both_flags)
215 o->flags &= ~not_shallow_flag;
217 return result;
220 #define check_shallow_file_for_update(r) check_shallow_file_for_update_##r()
221 static void check_shallow_file_for_update_the_repository(void)
223 if (is_shallow == -1)
224 die("BUG: shallow must be initialized by now");
226 if (!stat_validity_check(&shallow_stat, git_path_shallow()))
227 die("shallow file has changed since we read it");
230 #define SEEN_ONLY 1
231 #define VERBOSE 2
233 struct write_shallow_data {
234 struct strbuf *out;
235 int use_pack_protocol;
236 int count;
237 unsigned flags;
240 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
242 struct write_shallow_data *data = cb_data;
243 const char *hex = oid_to_hex(&graft->oid);
244 if (graft->nr_parent != -1)
245 return 0;
246 if (data->flags & SEEN_ONLY) {
247 struct commit *c = lookup_commit(&graft->oid);
248 if (!c || !(c->object.flags & SEEN)) {
249 if (data->flags & VERBOSE)
250 printf("Removing %s from .git/shallow\n",
251 oid_to_hex(&c->object.oid));
252 return 0;
255 data->count++;
256 if (data->use_pack_protocol)
257 packet_buf_write(data->out, "shallow %s", hex);
258 else {
259 strbuf_addstr(data->out, hex);
260 strbuf_addch(data->out, '\n');
262 return 0;
265 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
266 const struct oid_array *extra,
267 unsigned flags)
269 struct write_shallow_data data;
270 int i;
271 data.out = out;
272 data.use_pack_protocol = use_pack_protocol;
273 data.count = 0;
274 data.flags = flags;
275 for_each_commit_graft(write_one_shallow, &data);
276 if (!extra)
277 return data.count;
278 for (i = 0; i < extra->nr; i++) {
279 strbuf_addstr(out, oid_to_hex(extra->oid + i));
280 strbuf_addch(out, '\n');
281 data.count++;
283 return data.count;
286 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
287 const struct oid_array *extra)
289 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
292 const char *setup_temporary_shallow(const struct oid_array *extra)
294 struct tempfile *temp;
295 struct strbuf sb = STRBUF_INIT;
297 if (write_shallow_commits(&sb, 0, extra)) {
298 temp = xmks_tempfile(git_path("shallow_XXXXXX"));
300 if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
301 close_tempfile_gently(temp) < 0)
302 die_errno("failed to write to %s",
303 get_tempfile_path(temp));
304 strbuf_release(&sb);
305 return get_tempfile_path(temp);
308 * is_repository_shallow() sees empty string as "no shallow
309 * file".
311 return "";
314 void setup_alternate_shallow(struct lock_file *shallow_lock,
315 const char **alternate_shallow_file,
316 const struct oid_array *extra)
318 struct strbuf sb = STRBUF_INIT;
319 int fd;
321 fd = hold_lock_file_for_update(shallow_lock, git_path_shallow(),
322 LOCK_DIE_ON_ERROR);
323 check_shallow_file_for_update(the_repository);
324 if (write_shallow_commits(&sb, 0, extra)) {
325 if (write_in_full(fd, sb.buf, sb.len) < 0)
326 die_errno("failed to write to %s",
327 get_lock_file_path(shallow_lock));
328 *alternate_shallow_file = get_lock_file_path(shallow_lock);
329 } else
331 * is_repository_shallow() sees empty string as "no
332 * shallow file".
334 *alternate_shallow_file = "";
335 strbuf_release(&sb);
338 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
340 int fd = *(int *)cb;
341 if (graft->nr_parent == -1)
342 packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
343 return 0;
346 void advertise_shallow_grafts(int fd)
348 if (!is_repository_shallow(the_repository))
349 return;
350 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
354 * mark_reachable_objects() should have been run prior to this and all
355 * reachable commits marked as "SEEN".
357 void prune_shallow(int show_only)
359 static struct lock_file shallow_lock;
360 struct strbuf sb = STRBUF_INIT;
361 int fd;
363 if (show_only) {
364 write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
365 strbuf_release(&sb);
366 return;
368 fd = hold_lock_file_for_update(&shallow_lock, git_path_shallow(),
369 LOCK_DIE_ON_ERROR);
370 check_shallow_file_for_update(the_repository);
371 if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
372 if (write_in_full(fd, sb.buf, sb.len) < 0)
373 die_errno("failed to write to %s",
374 get_lock_file_path(&shallow_lock));
375 commit_lock_file(&shallow_lock);
376 } else {
377 unlink(git_path_shallow());
378 rollback_lock_file(&shallow_lock);
380 strbuf_release(&sb);
383 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
386 * Step 1, split sender shallow commits into "ours" and "theirs"
387 * Step 2, clean "ours" based on .git/shallow
389 void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
391 int i;
392 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
393 memset(info, 0, sizeof(*info));
394 info->shallow = sa;
395 if (!sa)
396 return;
397 ALLOC_ARRAY(info->ours, sa->nr);
398 ALLOC_ARRAY(info->theirs, sa->nr);
399 for (i = 0; i < sa->nr; i++) {
400 if (has_object_file(sa->oid + i)) {
401 struct commit_graft *graft;
402 graft = lookup_commit_graft(the_repository,
403 &sa->oid[i]);
404 if (graft && graft->nr_parent < 0)
405 continue;
406 info->ours[info->nr_ours++] = i;
407 } else
408 info->theirs[info->nr_theirs++] = i;
412 void clear_shallow_info(struct shallow_info *info)
414 free(info->ours);
415 free(info->theirs);
418 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
420 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
422 struct object_id *oid = info->shallow->oid;
423 int i, dst;
424 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
425 for (i = dst = 0; i < info->nr_theirs; i++) {
426 if (i != dst)
427 info->theirs[dst] = info->theirs[i];
428 if (has_object_file(oid + info->theirs[i]))
429 dst++;
431 info->nr_theirs = dst;
434 define_commit_slab(ref_bitmap, uint32_t *);
436 #define POOL_SIZE (512 * 1024)
438 struct paint_info {
439 struct ref_bitmap ref_bitmap;
440 unsigned nr_bits;
441 char **pools;
442 char *free, *end;
443 unsigned pool_count;
446 static uint32_t *paint_alloc(struct paint_info *info)
448 unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
449 unsigned size = nr * sizeof(uint32_t);
450 void *p;
451 if (!info->pool_count || size > info->end - info->free) {
452 if (size > POOL_SIZE)
453 die("BUG: pool size too small for %d in paint_alloc()",
454 size);
455 info->pool_count++;
456 REALLOC_ARRAY(info->pools, info->pool_count);
457 info->free = xmalloc(POOL_SIZE);
458 info->pools[info->pool_count - 1] = info->free;
459 info->end = info->free + POOL_SIZE;
461 p = info->free;
462 info->free += size;
463 return p;
467 * Given a commit SHA-1, walk down to parents until either SEEN,
468 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
469 * all walked commits.
471 static void paint_down(struct paint_info *info, const struct object_id *oid,
472 unsigned int id)
474 unsigned int i, nr;
475 struct commit_list *head = NULL;
476 int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
477 size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
478 struct commit *c = lookup_commit_reference_gently(oid, 1);
479 uint32_t *tmp; /* to be freed before return */
480 uint32_t *bitmap;
482 if (!c)
483 return;
485 tmp = xmalloc(bitmap_size);
486 bitmap = paint_alloc(info);
487 memset(bitmap, 0, bitmap_size);
488 bitmap[id / 32] |= (1U << (id % 32));
489 commit_list_insert(c, &head);
490 while (head) {
491 struct commit_list *p;
492 struct commit *c = pop_commit(&head);
493 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
495 /* XXX check "UNINTERESTING" from pack bitmaps if available */
496 if (c->object.flags & (SEEN | UNINTERESTING))
497 continue;
498 else
499 c->object.flags |= SEEN;
501 if (*refs == NULL)
502 *refs = bitmap;
503 else {
504 memcpy(tmp, *refs, bitmap_size);
505 for (i = 0; i < bitmap_nr; i++)
506 tmp[i] |= bitmap[i];
507 if (memcmp(tmp, *refs, bitmap_size)) {
508 *refs = paint_alloc(info);
509 memcpy(*refs, tmp, bitmap_size);
513 if (c->object.flags & BOTTOM)
514 continue;
516 if (parse_commit(c))
517 die("unable to parse commit %s",
518 oid_to_hex(&c->object.oid));
520 for (p = c->parents; p; p = p->next) {
521 if (p->item->object.flags & SEEN)
522 continue;
523 commit_list_insert(p->item, &head);
527 nr = get_max_object_index();
528 for (i = 0; i < nr; i++) {
529 struct object *o = get_indexed_object(i);
530 if (o && o->type == OBJ_COMMIT)
531 o->flags &= ~SEEN;
534 free(tmp);
537 static int mark_uninteresting(const char *refname, const struct object_id *oid,
538 int flags, void *cb_data)
540 struct commit *commit = lookup_commit_reference_gently(oid, 1);
541 if (!commit)
542 return 0;
543 commit->object.flags |= UNINTERESTING;
544 mark_parents_uninteresting(commit);
545 return 0;
548 static void post_assign_shallow(struct shallow_info *info,
549 struct ref_bitmap *ref_bitmap,
550 int *ref_status);
552 * Step 6(+7), associate shallow commits with new refs
554 * info->ref must be initialized before calling this function.
556 * If used is not NULL, it's an array of info->shallow->nr
557 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
558 * m-th shallow commit from info->shallow.
560 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
561 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
562 * the ref needs some shallow commits from either info->ours or
563 * info->theirs.
565 void assign_shallow_commits_to_refs(struct shallow_info *info,
566 uint32_t **used, int *ref_status)
568 struct object_id *oid = info->shallow->oid;
569 struct oid_array *ref = info->ref;
570 unsigned int i, nr;
571 int *shallow, nr_shallow = 0;
572 struct paint_info pi;
574 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
575 ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
576 for (i = 0; i < info->nr_ours; i++)
577 shallow[nr_shallow++] = info->ours[i];
578 for (i = 0; i < info->nr_theirs; i++)
579 shallow[nr_shallow++] = info->theirs[i];
582 * Prepare the commit graph to track what refs can reach what
583 * (new) shallow commits.
585 nr = get_max_object_index();
586 for (i = 0; i < nr; i++) {
587 struct object *o = get_indexed_object(i);
588 if (!o || o->type != OBJ_COMMIT)
589 continue;
591 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
594 memset(&pi, 0, sizeof(pi));
595 init_ref_bitmap(&pi.ref_bitmap);
596 pi.nr_bits = ref->nr;
599 * "--not --all" to cut short the traversal if new refs
600 * connect to old refs. If not (e.g. force ref updates) it'll
601 * have to go down to the current shallow commits.
603 head_ref(mark_uninteresting, NULL);
604 for_each_ref(mark_uninteresting, NULL);
606 /* Mark potential bottoms so we won't go out of bound */
607 for (i = 0; i < nr_shallow; i++) {
608 struct commit *c = lookup_commit(&oid[shallow[i]]);
609 c->object.flags |= BOTTOM;
612 for (i = 0; i < ref->nr; i++)
613 paint_down(&pi, ref->oid + i, i);
615 if (used) {
616 int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
617 memset(used, 0, sizeof(*used) * info->shallow->nr);
618 for (i = 0; i < nr_shallow; i++) {
619 const struct commit *c = lookup_commit(&oid[shallow[i]]);
620 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
621 if (*map)
622 used[shallow[i]] = xmemdupz(*map, bitmap_size);
625 * unreachable shallow commits are not removed from
626 * "ours" and "theirs". The user is supposed to run
627 * step 7 on every ref separately and not trust "ours"
628 * and "theirs" any more.
630 } else
631 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
633 clear_ref_bitmap(&pi.ref_bitmap);
634 for (i = 0; i < pi.pool_count; i++)
635 free(pi.pools[i]);
636 free(pi.pools);
637 free(shallow);
640 struct commit_array {
641 struct commit **commits;
642 int nr, alloc;
645 static int add_ref(const char *refname, const struct object_id *oid,
646 int flags, void *cb_data)
648 struct commit_array *ca = cb_data;
649 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
650 ca->commits[ca->nr] = lookup_commit_reference_gently(oid, 1);
651 if (ca->commits[ca->nr])
652 ca->nr++;
653 return 0;
656 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
658 unsigned int i;
659 if (!ref_status)
660 return;
661 for (i = 0; i < nr; i++)
662 if (bitmap[i / 32] & (1U << (i % 32)))
663 ref_status[i]++;
667 * Step 7, reachability test on "ours" at commit level
669 static void post_assign_shallow(struct shallow_info *info,
670 struct ref_bitmap *ref_bitmap,
671 int *ref_status)
673 struct object_id *oid = info->shallow->oid;
674 struct commit *c;
675 uint32_t **bitmap;
676 int dst, i, j;
677 int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
678 struct commit_array ca;
680 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
681 if (ref_status)
682 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
684 /* Remove unreachable shallow commits from "theirs" */
685 for (i = dst = 0; i < info->nr_theirs; i++) {
686 if (i != dst)
687 info->theirs[dst] = info->theirs[i];
688 c = lookup_commit(&oid[info->theirs[i]]);
689 bitmap = ref_bitmap_at(ref_bitmap, c);
690 if (!*bitmap)
691 continue;
692 for (j = 0; j < bitmap_nr; j++)
693 if (bitmap[0][j]) {
694 update_refstatus(ref_status, info->ref->nr, *bitmap);
695 dst++;
696 break;
699 info->nr_theirs = dst;
701 memset(&ca, 0, sizeof(ca));
702 head_ref(add_ref, &ca);
703 for_each_ref(add_ref, &ca);
705 /* Remove unreachable shallow commits from "ours" */
706 for (i = dst = 0; i < info->nr_ours; i++) {
707 if (i != dst)
708 info->ours[dst] = info->ours[i];
709 c = lookup_commit(&oid[info->ours[i]]);
710 bitmap = ref_bitmap_at(ref_bitmap, c);
711 if (!*bitmap)
712 continue;
713 for (j = 0; j < bitmap_nr; j++)
714 if (bitmap[0][j] &&
715 /* Step 7, reachability test at commit level */
716 !in_merge_bases_many(c, ca.nr, ca.commits)) {
717 update_refstatus(ref_status, info->ref->nr, *bitmap);
718 dst++;
719 break;
722 info->nr_ours = dst;
724 free(ca.commits);
727 /* (Delayed) step 7, reachability test at commit level */
728 int delayed_reachability_test(struct shallow_info *si, int c)
730 if (si->need_reachability_test[c]) {
731 struct commit *commit = lookup_commit(&si->shallow->oid[c]);
733 if (!si->commits) {
734 struct commit_array ca;
736 memset(&ca, 0, sizeof(ca));
737 head_ref(add_ref, &ca);
738 for_each_ref(add_ref, &ca);
739 si->commits = ca.commits;
740 si->nr_commits = ca.nr;
743 si->reachable[c] = in_merge_bases_many(commit,
744 si->nr_commits,
745 si->commits);
746 si->need_reachability_test[c] = 0;
748 return si->reachable[c];