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