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"
11#include "sigchain.h"
12
13static int is_shallow = -1;
14static struct stat_validity shallow_stat;
15static char *alternate_shallow_file;
16
17void set_alternate_shallow_file(const char *path, int override)
18{
19 if (is_shallow != -1)
20 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
21 if (alternate_shallow_file && !override)
22 return;
23 free(alternate_shallow_file);
24 alternate_shallow_file = path ? xstrdup(path) : NULL;
25}
26
27int register_shallow(const unsigned char *sha1)
28{
29 struct commit_graft *graft =
30 xmalloc(sizeof(struct commit_graft));
31 struct commit *commit = lookup_commit(sha1);
32
33 hashcpy(graft->sha1, sha1);
34 graft->nr_parent = -1;
35 if (commit && commit->object.parsed)
36 commit->parents = NULL;
37 return register_commit_graft(graft, 0);
38}
39
40int is_repository_shallow(void)
41{
42 FILE *fp;
43 char buf[1024];
44 const char *path = alternate_shallow_file;
45
46 if (is_shallow >= 0)
47 return is_shallow;
48
49 if (!path)
50 path = git_path("shallow");
51 /*
52 * fetch-pack sets '--shallow-file ""' as an indicator that no
53 * shallow file should be used. We could just open it and it
54 * will likely fail. But let's do an explicit check instead.
55 */
56 if (!*path || (fp = fopen(path, "r")) == NULL) {
57 stat_validity_clear(&shallow_stat);
58 is_shallow = 0;
59 return is_shallow;
60 }
61 stat_validity_update(&shallow_stat, fileno(fp));
62 is_shallow = 1;
63
64 while (fgets(buf, sizeof(buf), fp)) {
65 unsigned char sha1[20];
66 if (get_sha1_hex(buf, sha1))
67 die("bad shallow line: %s", buf);
68 register_shallow(sha1);
69 }
70 fclose(fp);
71 return is_shallow;
72}
73
74struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
75 int shallow_flag, int not_shallow_flag)
76{
77 int i = 0, cur_depth = 0;
78 struct commit_list *result = NULL;
79 struct object_array stack = OBJECT_ARRAY_INIT;
80 struct commit *commit = NULL;
81 struct commit_graft *graft;
82
83 while (commit || i < heads->nr || stack.nr) {
84 struct commit_list *p;
85 if (!commit) {
86 if (i < heads->nr) {
87 commit = (struct commit *)
88 deref_tag(heads->objects[i++].item, NULL, 0);
89 if (!commit || commit->object.type != OBJ_COMMIT) {
90 commit = NULL;
91 continue;
92 }
93 if (!commit->util)
94 commit->util = xmalloc(sizeof(int));
95 *(int *)commit->util = 0;
96 cur_depth = 0;
97 } else {
98 commit = (struct commit *)
99 stack.objects[--stack.nr].item;
100 cur_depth = *(int *)commit->util;
101 }
102 }
103 parse_commit_or_die(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;
113 }
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;
125 }
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;
132 }
133 }
134 }
135
136 return result;
137}
138
139void check_shallow_file_for_update(void)
140{
141 if (is_shallow == -1)
142 die("BUG: shallow must be initialized by now");
143
144 if (!stat_validity_check(&shallow_stat, git_path("shallow")))
145 die("shallow file has changed since we read it");
146}
147
148#define SEEN_ONLY 1
149#define VERBOSE 2
150
151struct write_shallow_data {
152 struct strbuf *out;
153 int use_pack_protocol;
154 int count;
155 unsigned flags;
156};
157
158static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
159{
160 struct write_shallow_data *data = cb_data;
161 const char *hex = sha1_to_hex(graft->sha1);
162 if (graft->nr_parent != -1)
163 return 0;
164 if (data->flags & SEEN_ONLY) {
165 struct commit *c = lookup_commit(graft->sha1);
166 if (!c || !(c->object.flags & SEEN)) {
167 if (data->flags & VERBOSE)
168 printf("Removing %s from .git/shallow\n",
169 sha1_to_hex(c->object.sha1));
170 return 0;
171 }
172 }
173 data->count++;
174 if (data->use_pack_protocol)
175 packet_buf_write(data->out, "shallow %s", hex);
176 else {
177 strbuf_addstr(data->out, hex);
178 strbuf_addch(data->out, '\n');
179 }
180 return 0;
181}
182
183static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
184 const struct sha1_array *extra,
185 unsigned flags)
186{
187 struct write_shallow_data data;
188 int i;
189 data.out = out;
190 data.use_pack_protocol = use_pack_protocol;
191 data.count = 0;
192 data.flags = flags;
193 for_each_commit_graft(write_one_shallow, &data);
194 if (!extra)
195 return data.count;
196 for (i = 0; i < extra->nr; i++) {
197 strbuf_addstr(out, sha1_to_hex(extra->sha1[i]));
198 strbuf_addch(out, '\n');
199 data.count++;
200 }
201 return data.count;
202}
203
204int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
205 const struct sha1_array *extra)
206{
207 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
208}
209
210static struct strbuf temporary_shallow = STRBUF_INIT;
211
212static void remove_temporary_shallow(void)
213{
214 if (temporary_shallow.len) {
215 unlink_or_warn(temporary_shallow.buf);
216 strbuf_reset(&temporary_shallow);
217 }
218}
219
220static void remove_temporary_shallow_on_signal(int signo)
221{
222 remove_temporary_shallow();
223 sigchain_pop(signo);
224 raise(signo);
225}
226
227const char *setup_temporary_shallow(const struct sha1_array *extra)
228{
229 struct strbuf sb = STRBUF_INIT;
230 int fd;
231
232 if (temporary_shallow.len)
233 die("BUG: attempt to create two temporary shallow files");
234
235 if (write_shallow_commits(&sb, 0, extra)) {
236 strbuf_addstr(&temporary_shallow, git_path("shallow_XXXXXX"));
237 fd = xmkstemp(temporary_shallow.buf);
238
239 atexit(remove_temporary_shallow);
240 sigchain_push_common(remove_temporary_shallow_on_signal);
241
242 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
243 die_errno("failed to write to %s",
244 temporary_shallow.buf);
245 close(fd);
246 strbuf_release(&sb);
247 return temporary_shallow.buf;
248 }
249 /*
250 * is_repository_shallow() sees empty string as "no shallow
251 * file".
252 */
253 return temporary_shallow.buf;
254}
255
256void setup_alternate_shallow(struct lock_file *shallow_lock,
257 const char **alternate_shallow_file,
258 const struct sha1_array *extra)
259{
260 struct strbuf sb = STRBUF_INIT;
261 int fd;
262
263 fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"),
264 LOCK_DIE_ON_ERROR);
265 check_shallow_file_for_update();
266 if (write_shallow_commits(&sb, 0, extra)) {
267 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
268 die_errno("failed to write to %s",
269 shallow_lock->filename);
270 *alternate_shallow_file = shallow_lock->filename;
271 } else
272 /*
273 * is_repository_shallow() sees empty string as "no
274 * shallow file".
275 */
276 *alternate_shallow_file = "";
277 strbuf_release(&sb);
278}
279
280static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
281{
282 int fd = *(int *)cb;
283 if (graft->nr_parent == -1)
284 packet_write(fd, "shallow %s\n", sha1_to_hex(graft->sha1));
285 return 0;
286}
287
288void advertise_shallow_grafts(int fd)
289{
290 if (!is_repository_shallow())
291 return;
292 for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
293}
294
295/*
296 * mark_reachable_objects() should have been run prior to this and all
297 * reachable commits marked as "SEEN".
298 */
299void prune_shallow(int show_only)
300{
301 static struct lock_file shallow_lock;
302 struct strbuf sb = STRBUF_INIT;
303 int fd;
304
305 if (show_only) {
306 write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
307 strbuf_release(&sb);
308 return;
309 }
310 fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
311 LOCK_DIE_ON_ERROR);
312 check_shallow_file_for_update();
313 if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
314 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
315 die_errno("failed to write to %s",
316 shallow_lock.filename);
317 commit_lock_file(&shallow_lock);
318 } else {
319 unlink(git_path("shallow"));
320 rollback_lock_file(&shallow_lock);
321 }
322 strbuf_release(&sb);
323}
324
325struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
326
327/*
328 * Step 1, split sender shallow commits into "ours" and "theirs"
329 * Step 2, clean "ours" based on .git/shallow
330 */
331void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
332{
333 int i;
334 trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
335 memset(info, 0, sizeof(*info));
336 info->shallow = sa;
337 if (!sa)
338 return;
339 info->ours = xmalloc(sizeof(*info->ours) * sa->nr);
340 info->theirs = xmalloc(sizeof(*info->theirs) * sa->nr);
341 for (i = 0; i < sa->nr; i++) {
342 if (has_sha1_file(sa->sha1[i])) {
343 struct commit_graft *graft;
344 graft = lookup_commit_graft(sa->sha1[i]);
345 if (graft && graft->nr_parent < 0)
346 continue;
347 info->ours[info->nr_ours++] = i;
348 } else
349 info->theirs[info->nr_theirs++] = i;
350 }
351}
352
353void clear_shallow_info(struct shallow_info *info)
354{
355 free(info->ours);
356 free(info->theirs);
357}
358
359/* Step 4, remove non-existent ones in "theirs" after getting the pack */
360
361void remove_nonexistent_theirs_shallow(struct shallow_info *info)
362{
363 unsigned char (*sha1)[20] = info->shallow->sha1;
364 int i, dst;
365 trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
366 for (i = dst = 0; i < info->nr_theirs; i++) {
367 if (i != dst)
368 info->theirs[dst] = info->theirs[i];
369 if (has_sha1_file(sha1[info->theirs[i]]))
370 dst++;
371 }
372 info->nr_theirs = dst;
373}
374
375define_commit_slab(ref_bitmap, uint32_t *);
376
377struct paint_info {
378 struct ref_bitmap ref_bitmap;
379 unsigned nr_bits;
380 char **slab;
381 char *free, *end;
382 unsigned slab_count;
383};
384
385static uint32_t *paint_alloc(struct paint_info *info)
386{
387 unsigned nr = (info->nr_bits + 31) / 32;
388 unsigned size = nr * sizeof(uint32_t);
389 void *p;
390 if (!info->slab_count || info->free + size > info->end) {
391 info->slab_count++;
392 REALLOC_ARRAY(info->slab, info->slab_count);
393 info->free = xmalloc(COMMIT_SLAB_SIZE);
394 info->slab[info->slab_count - 1] = info->free;
395 info->end = info->free + COMMIT_SLAB_SIZE;
396 }
397 p = info->free;
398 info->free += size;
399 return p;
400}
401
402/*
403 * Given a commit SHA-1, walk down to parents until either SEEN,
404 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
405 * all walked commits.
406 */
407static void paint_down(struct paint_info *info, const unsigned char *sha1,
408 int id)
409{
410 unsigned int i, nr;
411 struct commit_list *head = NULL;
412 int bitmap_nr = (info->nr_bits + 31) / 32;
413 int bitmap_size = bitmap_nr * sizeof(uint32_t);
414 uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */
415 uint32_t *bitmap = paint_alloc(info);
416 struct commit *c = lookup_commit_reference_gently(sha1, 1);
417 if (!c)
418 return;
419 memset(bitmap, 0, bitmap_size);
420 bitmap[id / 32] |= (1 << (id % 32));
421 commit_list_insert(c, &head);
422 while (head) {
423 struct commit_list *p;
424 struct commit *c = head->item;
425 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
426
427 p = head;
428 head = head->next;
429 free(p);
430
431 /* XXX check "UNINTERESTING" from pack bitmaps if available */
432 if (c->object.flags & (SEEN | UNINTERESTING))
433 continue;
434 else
435 c->object.flags |= SEEN;
436
437 if (*refs == NULL)
438 *refs = bitmap;
439 else {
440 memcpy(tmp, *refs, bitmap_size);
441 for (i = 0; i < bitmap_nr; i++)
442 tmp[i] |= bitmap[i];
443 if (memcmp(tmp, *refs, bitmap_size)) {
444 *refs = paint_alloc(info);
445 memcpy(*refs, tmp, bitmap_size);
446 }
447 }
448
449 if (c->object.flags & BOTTOM)
450 continue;
451
452 if (parse_commit(c))
453 die("unable to parse commit %s",
454 sha1_to_hex(c->object.sha1));
455
456 for (p = c->parents; p; p = p->next) {
457 uint32_t **p_refs = ref_bitmap_at(&info->ref_bitmap,
458 p->item);
459 if (p->item->object.flags & SEEN)
460 continue;
461 if (*p_refs == NULL || *p_refs == *refs)
462 *p_refs = *refs;
463 commit_list_insert(p->item, &head);
464 }
465 }
466
467 nr = get_max_object_index();
468 for (i = 0; i < nr; i++) {
469 struct object *o = get_indexed_object(i);
470 if (o && o->type == OBJ_COMMIT)
471 o->flags &= ~SEEN;
472 }
473
474 free(tmp);
475}
476
477static int mark_uninteresting(const char *refname,
478 const unsigned char *sha1,
479 int flags, void *cb_data)
480{
481 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
482 if (!commit)
483 return 0;
484 commit->object.flags |= UNINTERESTING;
485 mark_parents_uninteresting(commit);
486 return 0;
487}
488
489static void post_assign_shallow(struct shallow_info *info,
490 struct ref_bitmap *ref_bitmap,
491 int *ref_status);
492/*
493 * Step 6(+7), associate shallow commits with new refs
494 *
495 * info->ref must be initialized before calling this function.
496 *
497 * If used is not NULL, it's an array of info->shallow->nr
498 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
499 * m-th shallow commit from info->shallow.
500 *
501 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
502 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
503 * the ref needs some shallow commits from either info->ours or
504 * info->theirs.
505 */
506void assign_shallow_commits_to_refs(struct shallow_info *info,
507 uint32_t **used, int *ref_status)
508{
509 unsigned char (*sha1)[20] = info->shallow->sha1;
510 struct sha1_array *ref = info->ref;
511 unsigned int i, nr;
512 int *shallow, nr_shallow = 0;
513 struct paint_info pi;
514
515 trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
516 shallow = xmalloc(sizeof(*shallow) * (info->nr_ours + info->nr_theirs));
517 for (i = 0; i < info->nr_ours; i++)
518 shallow[nr_shallow++] = info->ours[i];
519 for (i = 0; i < info->nr_theirs; i++)
520 shallow[nr_shallow++] = info->theirs[i];
521
522 /*
523 * Prepare the commit graph to track what refs can reach what
524 * (new) shallow commits.
525 */
526 nr = get_max_object_index();
527 for (i = 0; i < nr; i++) {
528 struct object *o = get_indexed_object(i);
529 if (!o || o->type != OBJ_COMMIT)
530 continue;
531
532 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
533 }
534
535 memset(&pi, 0, sizeof(pi));
536 init_ref_bitmap(&pi.ref_bitmap);
537 pi.nr_bits = ref->nr;
538
539 /*
540 * "--not --all" to cut short the traversal if new refs
541 * connect to old refs. If not (e.g. force ref updates) it'll
542 * have to go down to the current shallow commits.
543 */
544 head_ref(mark_uninteresting, NULL);
545 for_each_ref(mark_uninteresting, NULL);
546
547 /* Mark potential bottoms so we won't go out of bound */
548 for (i = 0; i < nr_shallow; i++) {
549 struct commit *c = lookup_commit(sha1[shallow[i]]);
550 c->object.flags |= BOTTOM;
551 }
552
553 for (i = 0; i < ref->nr; i++)
554 paint_down(&pi, ref->sha1[i], i);
555
556 if (used) {
557 int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
558 memset(used, 0, sizeof(*used) * info->shallow->nr);
559 for (i = 0; i < nr_shallow; i++) {
560 const struct commit *c = lookup_commit(sha1[shallow[i]]);
561 uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
562 if (*map)
563 used[shallow[i]] = xmemdupz(*map, bitmap_size);
564 }
565 /*
566 * unreachable shallow commits are not removed from
567 * "ours" and "theirs". The user is supposed to run
568 * step 7 on every ref separately and not trust "ours"
569 * and "theirs" any more.
570 */
571 } else
572 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
573
574 clear_ref_bitmap(&pi.ref_bitmap);
575 for (i = 0; i < pi.slab_count; i++)
576 free(pi.slab[i]);
577 free(pi.slab);
578 free(shallow);
579}
580
581struct commit_array {
582 struct commit **commits;
583 int nr, alloc;
584};
585
586static int add_ref(const char *refname,
587 const unsigned char *sha1, int flags, void *cb_data)
588{
589 struct commit_array *ca = cb_data;
590 ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
591 ca->commits[ca->nr] = lookup_commit_reference_gently(sha1, 1);
592 if (ca->commits[ca->nr])
593 ca->nr++;
594 return 0;
595}
596
597static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
598{
599 int i;
600 if (!ref_status)
601 return;
602 for (i = 0; i < nr; i++)
603 if (bitmap[i / 32] & (1 << (i % 32)))
604 ref_status[i]++;
605}
606
607/*
608 * Step 7, reachability test on "ours" at commit level
609 */
610static void post_assign_shallow(struct shallow_info *info,
611 struct ref_bitmap *ref_bitmap,
612 int *ref_status)
613{
614 unsigned char (*sha1)[20] = info->shallow->sha1;
615 struct commit *c;
616 uint32_t **bitmap;
617 int dst, i, j;
618 int bitmap_nr = (info->ref->nr + 31) / 32;
619 struct commit_array ca;
620
621 trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
622 if (ref_status)
623 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
624
625 /* Remove unreachable shallow commits from "theirs" */
626 for (i = dst = 0; i < info->nr_theirs; i++) {
627 if (i != dst)
628 info->theirs[dst] = info->theirs[i];
629 c = lookup_commit(sha1[info->theirs[i]]);
630 bitmap = ref_bitmap_at(ref_bitmap, c);
631 if (!*bitmap)
632 continue;
633 for (j = 0; j < bitmap_nr; j++)
634 if (bitmap[0][j]) {
635 update_refstatus(ref_status, info->ref->nr, *bitmap);
636 dst++;
637 break;
638 }
639 }
640 info->nr_theirs = dst;
641
642 memset(&ca, 0, sizeof(ca));
643 head_ref(add_ref, &ca);
644 for_each_ref(add_ref, &ca);
645
646 /* Remove unreachable shallow commits from "ours" */
647 for (i = dst = 0; i < info->nr_ours; i++) {
648 if (i != dst)
649 info->ours[dst] = info->ours[i];
650 c = lookup_commit(sha1[info->ours[i]]);
651 bitmap = ref_bitmap_at(ref_bitmap, c);
652 if (!*bitmap)
653 continue;
654 for (j = 0; j < bitmap_nr; j++)
655 if (bitmap[0][j] &&
656 /* Step 7, reachability test at commit level */
657 !in_merge_bases_many(c, ca.nr, ca.commits)) {
658 update_refstatus(ref_status, info->ref->nr, *bitmap);
659 dst++;
660 break;
661 }
662 }
663 info->nr_ours = dst;
664
665 free(ca.commits);
666}
667
668/* (Delayed) step 7, reachability test at commit level */
669int delayed_reachability_test(struct shallow_info *si, int c)
670{
671 if (si->need_reachability_test[c]) {
672 struct commit *commit = lookup_commit(si->shallow->sha1[c]);
673
674 if (!si->commits) {
675 struct commit_array ca;
676 memset(&ca, 0, sizeof(ca));
677 head_ref(add_ref, &ca);
678 for_each_ref(add_ref, &ca);
679 si->commits = ca.commits;
680 si->nr_commits = ca.nr;
681 }
682
683 si->reachable[c] = in_merge_bases_many(commit,
684 si->nr_commits,
685 si->commits);
686 si->need_reachability_test[c] = 0;
687 }
688 return si->reachable[c];
689}