shallow: remove unused code
[alt-git.git] / shallow.c
blobe483780d4916a817e5256ec325e6cbcf31eb9328
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "pkt-line.h"
5 #include "remote.h"
6 #include "refs.h"
7 #include "sha1-array.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "commit-slab.h"
12 static int is_shallow = -1;
13 static struct stat shallow_stat;
14 static char *alternate_shallow_file;
16 void set_alternate_shallow_file(const char *path, int override)
18 if (is_shallow != -1)
19 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
20 if (alternate_shallow_file && !override)
21 return;
22 free(alternate_shallow_file);
23 alternate_shallow_file = path ? xstrdup(path) : NULL;
26 int register_shallow(const unsigned char *sha1)
28 struct commit_graft *graft =
29 xmalloc(sizeof(struct commit_graft));
30 struct commit *commit = lookup_commit(sha1);
32 hashcpy(graft->sha1, sha1);
33 graft->nr_parent = -1;
34 if (commit && commit->object.parsed)
35 commit->parents = NULL;
36 return register_commit_graft(graft, 0);
39 int is_repository_shallow(void)
41 FILE *fp;
42 char buf[1024];
43 const char *path = alternate_shallow_file;
45 if (is_shallow >= 0)
46 return is_shallow;
48 if (!path)
49 path = git_path("shallow");
51 * fetch-pack sets '--shallow-file ""' as an indicator that no
52 * shallow file should be used. We could just open it and it
53 * will likely fail. But let's do an explicit check instead.
55 if (!*path ||
56 stat(path, &shallow_stat) ||
57 (fp = fopen(path, "r")) == NULL) {
58 is_shallow = 0;
59 return is_shallow;
61 is_shallow = 1;
63 while (fgets(buf, sizeof(buf), fp)) {
64 unsigned char sha1[20];
65 if (get_sha1_hex(buf, sha1))
66 die("bad shallow line: %s", buf);
67 register_shallow(sha1);
69 fclose(fp);
70 return is_shallow;
73 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
74 int shallow_flag, int not_shallow_flag)
76 int i = 0, cur_depth = 0;
77 struct commit_list *result = NULL;
78 struct object_array stack = OBJECT_ARRAY_INIT;
79 struct commit *commit = NULL;
80 struct commit_graft *graft;
82 while (commit || i < heads->nr || stack.nr) {
83 struct commit_list *p;
84 if (!commit) {
85 if (i < heads->nr) {
86 commit = (struct commit *)
87 deref_tag(heads->objects[i++].item, NULL, 0);
88 if (!commit || commit->object.type != OBJ_COMMIT) {
89 commit = NULL;
90 continue;
92 if (!commit->util)
93 commit->util = xmalloc(sizeof(int));
94 *(int *)commit->util = 0;
95 cur_depth = 0;
96 } else {
97 commit = (struct commit *)
98 stack.objects[--stack.nr].item;
99 cur_depth = *(int *)commit->util;
102 if (parse_commit(commit))
103 die("invalid commit");
104 cur_depth++;
105 if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
106 (is_repository_shallow() && !commit->parents &&
107 (graft = lookup_commit_graft(commit->object.sha1)) != NULL &&
108 graft->nr_parent < 0)) {
109 commit_list_insert(commit, &result);
110 commit->object.flags |= shallow_flag;
111 commit = NULL;
112 continue;
114 commit->object.flags |= not_shallow_flag;
115 for (p = commit->parents, commit = NULL; p; p = p->next) {
116 if (!p->item->util) {
117 int *pointer = xmalloc(sizeof(int));
118 p->item->util = pointer;
119 *pointer = cur_depth;
120 } else {
121 int *pointer = p->item->util;
122 if (cur_depth >= *pointer)
123 continue;
124 *pointer = cur_depth;
126 if (p->next)
127 add_object_array(&p->item->object,
128 NULL, &stack);
129 else {
130 commit = p->item;
131 cur_depth = *(int *)commit->util;
136 return result;
139 void check_shallow_file_for_update(void)
141 struct stat st;
143 if (!is_shallow)
144 return;
145 else if (is_shallow == -1)
146 die("BUG: shallow must be initialized by now");
148 if (stat(git_path("shallow"), &st))
149 die("shallow file was removed during fetch");
150 else if (st.st_mtime != shallow_stat.st_mtime
151 #ifdef USE_NSEC
152 || ST_MTIME_NSEC(st) != ST_MTIME_NSEC(shallow_stat)
153 #endif
155 die("shallow file was changed during fetch");
158 #define SEEN_ONLY 1
159 #define VERBOSE 2
161 struct write_shallow_data {
162 struct strbuf *out;
163 int use_pack_protocol;
164 int count;
165 unsigned flags;
168 static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
170 struct write_shallow_data *data = cb_data;
171 const char *hex = sha1_to_hex(graft->sha1);
172 if (graft->nr_parent != -1)
173 return 0;
174 if (data->flags & SEEN_ONLY) {
175 struct commit *c = lookup_commit(graft->sha1);
176 if (!c || !(c->object.flags & SEEN)) {
177 if (data->flags & VERBOSE)
178 printf("Removing %s from .git/shallow\n",
179 sha1_to_hex(c->object.sha1));
180 return 0;
183 data->count++;
184 if (data->use_pack_protocol)
185 packet_buf_write(data->out, "shallow %s", hex);
186 else {
187 strbuf_addstr(data->out, hex);
188 strbuf_addch(data->out, '\n');
190 return 0;
193 static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
194 const struct sha1_array *extra,
195 unsigned flags)
197 struct write_shallow_data data;
198 int i;
199 data.out = out;
200 data.use_pack_protocol = use_pack_protocol;
201 data.count = 0;
202 data.flags = flags;
203 for_each_commit_graft(write_one_shallow, &data);
204 if (!extra)
205 return data.count;
206 for (i = 0; i < extra->nr; i++) {
207 strbuf_addstr(out, sha1_to_hex(extra->sha1[i]));
208 strbuf_addch(out, '\n');
209 data.count++;
211 return data.count;
214 int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
215 const struct sha1_array *extra)
217 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
220 char *setup_temporary_shallow(const struct sha1_array *extra)
222 struct strbuf sb = STRBUF_INIT;
223 int fd;
225 if (write_shallow_commits(&sb, 0, extra)) {
226 struct strbuf path = STRBUF_INIT;
227 strbuf_addstr(&path, git_path("shallow_XXXXXX"));
228 fd = xmkstemp(path.buf);
229 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
230 die_errno("failed to write to %s",
231 path.buf);
232 close(fd);
233 strbuf_release(&sb);
234 return strbuf_detach(&path, NULL);
237 * is_repository_shallow() sees empty string as "no shallow
238 * file".
240 return xstrdup("");
243 void setup_alternate_shallow(struct lock_file *shallow_lock,
244 const char **alternate_shallow_file,
245 const struct sha1_array *extra)
247 struct strbuf sb = STRBUF_INIT;
248 int fd;
250 check_shallow_file_for_update();
251 fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"),
252 LOCK_DIE_ON_ERROR);
253 if (write_shallow_commits(&sb, 0, extra)) {
254 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
255 die_errno("failed to write to %s",
256 shallow_lock->filename);
257 *alternate_shallow_file = shallow_lock->filename;
258 } else
260 * is_repository_shallow() sees empty string as "no
261 * shallow file".
263 *alternate_shallow_file = "";
264 strbuf_release(&sb);
267 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
269 int fd = *(int *)cb;
270 if (graft->nr_parent == -1)
271 packet_write(fd, "shallow %s\n", sha1_to_hex(graft->sha1));
272 return 0;
275 void advertise_shallow_grafts(int fd)
277 if (!is_repository_shallow())
278 return;
279 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
283 * mark_reachable_objects() should have been run prior to this and all
284 * reachable commits marked as "SEEN".
286 void prune_shallow(int show_only)
288 static struct lock_file shallow_lock;
289 struct strbuf sb = STRBUF_INIT;
290 int fd;
292 if (show_only) {
293 write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
294 strbuf_release(&sb);
295 return;
297 check_shallow_file_for_update();
298 fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
299 LOCK_DIE_ON_ERROR);
300 if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
301 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
302 die_errno("failed to write to %s",
303 shallow_lock.filename);
304 commit_lock_file(&shallow_lock);
305 } else {
306 unlink(git_path("shallow"));
307 rollback_lock_file(&shallow_lock);
309 strbuf_release(&sb);
312 #define TRACE_KEY "GIT_TRACE_SHALLOW"
315 * Step 1, split sender shallow commits into "ours" and "theirs"
316 * Step 2, clean "ours" based on .git/shallow
318 void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
320 int i;
321 trace_printf_key(TRACE_KEY, "shallow: prepare_shallow_info\n");
322 memset(info, 0, sizeof(*info));
323 info->shallow = sa;
324 if (!sa)
325 return;
326 info->ours = xmalloc(sizeof(*info->ours) * sa->nr);
327 info->theirs = xmalloc(sizeof(*info->theirs) * sa->nr);
328 for (i = 0; i < sa->nr; i++) {
329 if (has_sha1_file(sa->sha1[i])) {
330 struct commit_graft *graft;
331 graft = lookup_commit_graft(sa->sha1[i]);
332 if (graft && graft->nr_parent < 0)
333 continue;
334 info->ours[info->nr_ours++] = i;
335 } else
336 info->theirs[info->nr_theirs++] = i;
340 void clear_shallow_info(struct shallow_info *info)
342 free(info->ours);
343 free(info->theirs);
346 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
348 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
350 unsigned char (*sha1)[20] = info->shallow->sha1;
351 int i, dst;
352 trace_printf_key(TRACE_KEY, "shallow: remove_nonexistent_theirs_shallow\n");
353 for (i = dst = 0; i < info->nr_theirs; i++) {
354 if (i != dst)
355 info->theirs[dst] = info->theirs[i];
356 if (has_sha1_file(sha1[info->theirs[i]]))
357 dst++;
359 info->nr_theirs = dst;
362 define_commit_slab(ref_bitmap, uint32_t *);
364 struct paint_info {
365 struct ref_bitmap ref_bitmap;
366 unsigned nr_bits;
367 char **slab;
368 char *free, *end;
369 unsigned slab_count;
372 static uint32_t *paint_alloc(struct paint_info *info)
374 unsigned nr = (info->nr_bits + 31) / 32;
375 unsigned size = nr * sizeof(uint32_t);
376 void *p;
377 if (!info->slab_count || info->free + size > info->end) {
378 info->slab_count++;
379 info->slab = xrealloc(info->slab,
380 info->slab_count * sizeof(*info->slab));
381 info->free = xmalloc(COMMIT_SLAB_SIZE);
382 info->slab[info->slab_count - 1] = info->free;
383 info->end = info->free + COMMIT_SLAB_SIZE;
385 p = info->free;
386 info->free += size;
387 return p;
391 * Given a commit SHA-1, walk down to parents until either SEEN,
392 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
393 * all walked commits.
395 static void paint_down(struct paint_info *info, const unsigned char *sha1,
396 int id)
398 unsigned int i, nr;
399 struct commit_list *head = NULL;
400 int bitmap_nr = (info->nr_bits + 31) / 32;
401 int bitmap_size = bitmap_nr * sizeof(uint32_t);
402 uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */
403 uint32_t *bitmap = paint_alloc(info);
404 struct commit *c = lookup_commit_reference_gently(sha1, 1);
405 if (!c)
406 return;
407 memset(bitmap, 0, bitmap_size);
408 bitmap[id / 32] |= (1 << (id % 32));
409 commit_list_insert(c, &head);
410 while (head) {
411 struct commit_list *p;
412 struct commit *c = head->item;
413 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
415 p = head;
416 head = head->next;
417 free(p);
419 /* XXX check "UNINTERESTING" from pack bitmaps if available */
420 if (c->object.flags & (SEEN | UNINTERESTING))
421 continue;
422 else
423 c->object.flags |= SEEN;
425 if (*refs == NULL)
426 *refs = bitmap;
427 else {
428 memcpy(tmp, *refs, bitmap_size);
429 for (i = 0; i < bitmap_nr; i++)
430 tmp[i] |= bitmap[i];
431 if (memcmp(tmp, *refs, bitmap_size)) {
432 *refs = paint_alloc(info);
433 memcpy(*refs, tmp, bitmap_size);
437 if (c->object.flags & BOTTOM)
438 continue;
440 if (parse_commit(c))
441 die("unable to parse commit %s",
442 sha1_to_hex(c->object.sha1));
444 for (p = c->parents; p; p = p->next) {
445 uint32_t **p_refs = ref_bitmap_at(&info->ref_bitmap,
446 p->item);
447 if (p->item->object.flags & SEEN)
448 continue;
449 if (*p_refs == NULL || *p_refs == *refs)
450 *p_refs = *refs;
451 commit_list_insert(p->item, &head);
455 nr = get_max_object_index();
456 for (i = 0; i < nr; i++) {
457 struct object *o = get_indexed_object(i);
458 if (o && o->type == OBJ_COMMIT)
459 o->flags &= ~SEEN;
462 free(tmp);
465 static int mark_uninteresting(const char *refname,
466 const unsigned char *sha1,
467 int flags, void *cb_data)
469 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
470 if (!commit)
471 return 0;
472 commit->object.flags |= UNINTERESTING;
473 mark_parents_uninteresting(commit);
474 return 0;
477 static void post_assign_shallow(struct shallow_info *info,
478 struct ref_bitmap *ref_bitmap,
479 int *ref_status);
481 * Step 6(+7), associate shallow commits with new refs
483 * info->ref must be initialized before calling this function.
485 * If used is not NULL, it's an array of info->shallow->nr
486 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
487 * m-th shallow commit from info->shallow.
489 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
490 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
491 * the ref needs some shallow commits from either info->ours or
492 * info->theirs.
494 void assign_shallow_commits_to_refs(struct shallow_info *info,
495 uint32_t **used, int *ref_status)
497 unsigned char (*sha1)[20] = info->shallow->sha1;
498 struct sha1_array *ref = info->ref;
499 unsigned int i, nr;
500 int *shallow, nr_shallow = 0;
501 struct paint_info pi;
503 trace_printf_key(TRACE_KEY, "shallow: assign_shallow_commits_to_refs\n");
504 shallow = xmalloc(sizeof(*shallow) * (info->nr_ours + info->nr_theirs));
505 for (i = 0; i < info->nr_ours; i++)
506 shallow[nr_shallow++] = info->ours[i];
507 for (i = 0; i < info->nr_theirs; i++)
508 shallow[nr_shallow++] = info->theirs[i];
511 * Prepare the commit graph to track what refs can reach what
512 * (new) shallow commits.
514 nr = get_max_object_index();
515 for (i = 0; i < nr; i++) {
516 struct object *o = get_indexed_object(i);
517 if (!o || o->type != OBJ_COMMIT)
518 continue;
520 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
523 memset(&pi, 0, sizeof(pi));
524 init_ref_bitmap(&pi.ref_bitmap);
525 pi.nr_bits = ref->nr;
528 * "--not --all" to cut short the traversal if new refs
529 * connect to old refs. If not (e.g. force ref updates) it'll
530 * have to go down to the current shallow commits.
532 head_ref(mark_uninteresting, NULL);
533 for_each_ref(mark_uninteresting, NULL);
535 /* Mark potential bottoms so we won't go out of bound */
536 for (i = 0; i < nr_shallow; i++) {
537 struct commit *c = lookup_commit(sha1[shallow[i]]);
538 c->object.flags |= BOTTOM;
541 for (i = 0; i < ref->nr; i++)
542 paint_down(&pi, ref->sha1[i], i);
544 if (used) {
545 int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
546 memset(used, 0, sizeof(*used) * info->shallow->nr);
547 for (i = 0; i < nr_shallow; i++) {
548 const struct commit *c = lookup_commit(sha1[shallow[i]]);
549 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
550 if (*map)
551 used[shallow[i]] = xmemdupz(*map, bitmap_size);
554 * unreachable shallow commits are not removed from
555 * "ours" and "theirs". The user is supposed to run
556 * step 7 on every ref separately and not trust "ours"
557 * and "theirs" any more.
559 } else
560 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
562 clear_ref_bitmap(&pi.ref_bitmap);
563 for (i = 0; i < pi.slab_count; i++)
564 free(pi.slab[i]);
565 free(pi.slab);
566 free(shallow);
569 struct commit_array {
570 struct commit **commits;
571 int nr, alloc;
574 static int add_ref(const char *refname,
575 const unsigned char *sha1, int flags, void *cb_data)
577 struct commit_array *ca = cb_data;
578 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
579 ca->commits[ca->nr] = lookup_commit_reference_gently(sha1, 1);
580 if (ca->commits[ca->nr])
581 ca->nr++;
582 return 0;
585 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
587 int i;
588 if (!ref_status)
589 return;
590 for (i = 0; i < nr; i++)
591 if (bitmap[i / 32] & (1 << (i % 32)))
592 ref_status[i]++;
596 * Step 7, reachability test on "ours" at commit level
598 static void post_assign_shallow(struct shallow_info *info,
599 struct ref_bitmap *ref_bitmap,
600 int *ref_status)
602 unsigned char (*sha1)[20] = info->shallow->sha1;
603 struct commit *c;
604 uint32_t **bitmap;
605 int dst, i, j;
606 int bitmap_nr = (info->ref->nr + 31) / 32;
607 struct commit_array ca;
609 trace_printf_key(TRACE_KEY, "shallow: post_assign_shallow\n");
610 if (ref_status)
611 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
613 /* Remove unreachable shallow commits from "theirs" */
614 for (i = dst = 0; i < info->nr_theirs; i++) {
615 if (i != dst)
616 info->theirs[dst] = info->theirs[i];
617 c = lookup_commit(sha1[info->theirs[i]]);
618 bitmap = ref_bitmap_at(ref_bitmap, c);
619 if (!*bitmap)
620 continue;
621 for (j = 0; j < bitmap_nr; j++)
622 if (bitmap[0][j]) {
623 update_refstatus(ref_status, info->ref->nr, *bitmap);
624 dst++;
625 break;
628 info->nr_theirs = dst;
630 memset(&ca, 0, sizeof(ca));
631 head_ref(add_ref, &ca);
632 for_each_ref(add_ref, &ca);
634 /* Remove unreachable shallow commits from "ours" */
635 for (i = dst = 0; i < info->nr_ours; i++) {
636 if (i != dst)
637 info->ours[dst] = info->ours[i];
638 c = lookup_commit(sha1[info->ours[i]]);
639 bitmap = ref_bitmap_at(ref_bitmap, c);
640 if (!*bitmap)
641 continue;
642 for (j = 0; j < bitmap_nr; j++)
643 if (bitmap[0][j] &&
644 /* Step 7, reachability test at commit level */
645 !in_merge_bases_many(c, ca.nr, ca.commits)) {
646 update_refstatus(ref_status, info->ref->nr, *bitmap);
647 dst++;
648 break;
651 info->nr_ours = dst;
653 free(ca.commits);
656 /* (Delayed) step 7, reachability test at commit level */
657 int delayed_reachability_test(struct shallow_info *si, int c)
659 if (si->need_reachability_test[c]) {
660 struct commit *commit = lookup_commit(si->shallow->sha1[c]);
662 if (!si->commits) {
663 struct commit_array ca;
664 memset(&ca, 0, sizeof(ca));
665 head_ref(add_ref, &ca);
666 for_each_ref(add_ref, &ca);
667 si->commits = ca.commits;
668 si->nr_commits = ca.nr;
671 si->reachable[c] = in_merge_bases_many(commit,
672 si->nr_commits,
673 si->commits);
674 si->need_reachability_test[c] = 0;
676 return si->reachable[c];