Merge branch 'da/mergetool-temporary-directory'
[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         static int installed_handler;
231         struct strbuf sb = STRBUF_INIT;
232         int fd;
233
234         if (temporary_shallow.len)
235                 die("BUG: attempt to create two temporary shallow files");
236
237         if (write_shallow_commits(&sb, 0, extra)) {
238                 strbuf_addstr(&temporary_shallow, git_path("shallow_XXXXXX"));
239                 fd = xmkstemp(temporary_shallow.buf);
240
241                 if (!installed_handler) {
242                         atexit(remove_temporary_shallow);
243                         sigchain_push_common(remove_temporary_shallow_on_signal);
244                 }
245
246                 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
247                         die_errno("failed to write to %s",
248                                   temporary_shallow.buf);
249                 close(fd);
250                 strbuf_release(&sb);
251                 return temporary_shallow.buf;
252         }
253         /*
254          * is_repository_shallow() sees empty string as "no shallow
255          * file".
256          */
257         return temporary_shallow.buf;
258 }
259
260 void setup_alternate_shallow(struct lock_file *shallow_lock,
261                              const char **alternate_shallow_file,
262                              const struct sha1_array *extra)
263 {
264         struct strbuf sb = STRBUF_INIT;
265         int fd;
266
267         fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"),
268                                        LOCK_DIE_ON_ERROR);
269         check_shallow_file_for_update();
270         if (write_shallow_commits(&sb, 0, extra)) {
271                 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
272                         die_errno("failed to write to %s",
273                                   shallow_lock->filename.buf);
274                 *alternate_shallow_file = shallow_lock->filename.buf;
275         } else
276                 /*
277                  * is_repository_shallow() sees empty string as "no
278                  * shallow file".
279                  */
280                 *alternate_shallow_file = "";
281         strbuf_release(&sb);
282 }
283
284 static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
285 {
286         int fd = *(int *)cb;
287         if (graft->nr_parent == -1)
288                 packet_write(fd, "shallow %s\n", sha1_to_hex(graft->sha1));
289         return 0;
290 }
291
292 void advertise_shallow_grafts(int fd)
293 {
294         if (!is_repository_shallow())
295                 return;
296         for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
297 }
298
299 /*
300  * mark_reachable_objects() should have been run prior to this and all
301  * reachable commits marked as "SEEN".
302  */
303 void prune_shallow(int show_only)
304 {
305         static struct lock_file shallow_lock;
306         struct strbuf sb = STRBUF_INIT;
307         int fd;
308
309         if (show_only) {
310                 write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY | VERBOSE);
311                 strbuf_release(&sb);
312                 return;
313         }
314         fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
315                                        LOCK_DIE_ON_ERROR);
316         check_shallow_file_for_update();
317         if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
318                 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
319                         die_errno("failed to write to %s",
320                                   shallow_lock.filename.buf);
321                 commit_lock_file(&shallow_lock);
322         } else {
323                 unlink(git_path("shallow"));
324                 rollback_lock_file(&shallow_lock);
325         }
326         strbuf_release(&sb);
327 }
328
329 struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
330
331 /*
332  * Step 1, split sender shallow commits into "ours" and "theirs"
333  * Step 2, clean "ours" based on .git/shallow
334  */
335 void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa)
336 {
337         int i;
338         trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
339         memset(info, 0, sizeof(*info));
340         info->shallow = sa;
341         if (!sa)
342                 return;
343         info->ours = xmalloc(sizeof(*info->ours) * sa->nr);
344         info->theirs = xmalloc(sizeof(*info->theirs) * sa->nr);
345         for (i = 0; i < sa->nr; i++) {
346                 if (has_sha1_file(sa->sha1[i])) {
347                         struct commit_graft *graft;
348                         graft = lookup_commit_graft(sa->sha1[i]);
349                         if (graft && graft->nr_parent < 0)
350                                 continue;
351                         info->ours[info->nr_ours++] = i;
352                 } else
353                         info->theirs[info->nr_theirs++] = i;
354         }
355 }
356
357 void clear_shallow_info(struct shallow_info *info)
358 {
359         free(info->ours);
360         free(info->theirs);
361 }
362
363 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
364
365 void remove_nonexistent_theirs_shallow(struct shallow_info *info)
366 {
367         unsigned char (*sha1)[20] = info->shallow->sha1;
368         int i, dst;
369         trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
370         for (i = dst = 0; i < info->nr_theirs; i++) {
371                 if (i != dst)
372                         info->theirs[dst] = info->theirs[i];
373                 if (has_sha1_file(sha1[info->theirs[i]]))
374                         dst++;
375         }
376         info->nr_theirs = dst;
377 }
378
379 define_commit_slab(ref_bitmap, uint32_t *);
380
381 struct paint_info {
382         struct ref_bitmap ref_bitmap;
383         unsigned nr_bits;
384         char **slab;
385         char *free, *end;
386         unsigned slab_count;
387 };
388
389 static uint32_t *paint_alloc(struct paint_info *info)
390 {
391         unsigned nr = (info->nr_bits + 31) / 32;
392         unsigned size = nr * sizeof(uint32_t);
393         void *p;
394         if (!info->slab_count || info->free + size > info->end) {
395                 info->slab_count++;
396                 REALLOC_ARRAY(info->slab, info->slab_count);
397                 info->free = xmalloc(COMMIT_SLAB_SIZE);
398                 info->slab[info->slab_count - 1] = info->free;
399                 info->end = info->free + COMMIT_SLAB_SIZE;
400         }
401         p = info->free;
402         info->free += size;
403         return p;
404 }
405
406 /*
407  * Given a commit SHA-1, walk down to parents until either SEEN,
408  * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
409  * all walked commits.
410  */
411 static void paint_down(struct paint_info *info, const unsigned char *sha1,
412                        int id)
413 {
414         unsigned int i, nr;
415         struct commit_list *head = NULL;
416         int bitmap_nr = (info->nr_bits + 31) / 32;
417         int bitmap_size = bitmap_nr * sizeof(uint32_t);
418         uint32_t *tmp = xmalloc(bitmap_size); /* to be freed before return */
419         uint32_t *bitmap = paint_alloc(info);
420         struct commit *c = lookup_commit_reference_gently(sha1, 1);
421         if (!c)
422                 return;
423         memset(bitmap, 0, bitmap_size);
424         bitmap[id / 32] |= (1 << (id % 32));
425         commit_list_insert(c, &head);
426         while (head) {
427                 struct commit_list *p;
428                 struct commit *c = head->item;
429                 uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
430
431                 p = head;
432                 head = head->next;
433                 free(p);
434
435                 /* XXX check "UNINTERESTING" from pack bitmaps if available */
436                 if (c->object.flags & (SEEN | UNINTERESTING))
437                         continue;
438                 else
439                         c->object.flags |= SEEN;
440
441                 if (*refs == NULL)
442                         *refs = bitmap;
443                 else {
444                         memcpy(tmp, *refs, bitmap_size);
445                         for (i = 0; i < bitmap_nr; i++)
446                                 tmp[i] |= bitmap[i];
447                         if (memcmp(tmp, *refs, bitmap_size)) {
448                                 *refs = paint_alloc(info);
449                                 memcpy(*refs, tmp, bitmap_size);
450                         }
451                 }
452
453                 if (c->object.flags & BOTTOM)
454                         continue;
455
456                 if (parse_commit(c))
457                         die("unable to parse commit %s",
458                             sha1_to_hex(c->object.sha1));
459
460                 for (p = c->parents; p; p = p->next) {
461                         uint32_t **p_refs = ref_bitmap_at(&info->ref_bitmap,
462                                                           p->item);
463                         if (p->item->object.flags & SEEN)
464                                 continue;
465                         if (*p_refs == NULL || *p_refs == *refs)
466                                 *p_refs = *refs;
467                         commit_list_insert(p->item, &head);
468                 }
469         }
470
471         nr = get_max_object_index();
472         for (i = 0; i < nr; i++) {
473                 struct object *o = get_indexed_object(i);
474                 if (o && o->type == OBJ_COMMIT)
475                         o->flags &= ~SEEN;
476         }
477
478         free(tmp);
479 }
480
481 static int mark_uninteresting(const char *refname,
482                               const unsigned char *sha1,
483                               int flags, void *cb_data)
484 {
485         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
486         if (!commit)
487                 return 0;
488         commit->object.flags |= UNINTERESTING;
489         mark_parents_uninteresting(commit);
490         return 0;
491 }
492
493 static void post_assign_shallow(struct shallow_info *info,
494                                 struct ref_bitmap *ref_bitmap,
495                                 int *ref_status);
496 /*
497  * Step 6(+7), associate shallow commits with new refs
498  *
499  * info->ref must be initialized before calling this function.
500  *
501  * If used is not NULL, it's an array of info->shallow->nr
502  * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
503  * m-th shallow commit from info->shallow.
504  *
505  * If used is NULL, "ours" and "theirs" are updated. And if ref_status
506  * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
507  * the ref needs some shallow commits from either info->ours or
508  * info->theirs.
509  */
510 void assign_shallow_commits_to_refs(struct shallow_info *info,
511                                     uint32_t **used, int *ref_status)
512 {
513         unsigned char (*sha1)[20] = info->shallow->sha1;
514         struct sha1_array *ref = info->ref;
515         unsigned int i, nr;
516         int *shallow, nr_shallow = 0;
517         struct paint_info pi;
518
519         trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
520         shallow = xmalloc(sizeof(*shallow) * (info->nr_ours + info->nr_theirs));
521         for (i = 0; i < info->nr_ours; i++)
522                 shallow[nr_shallow++] = info->ours[i];
523         for (i = 0; i < info->nr_theirs; i++)
524                 shallow[nr_shallow++] = info->theirs[i];
525
526         /*
527          * Prepare the commit graph to track what refs can reach what
528          * (new) shallow commits.
529          */
530         nr = get_max_object_index();
531         for (i = 0; i < nr; i++) {
532                 struct object *o = get_indexed_object(i);
533                 if (!o || o->type != OBJ_COMMIT)
534                         continue;
535
536                 o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
537         }
538
539         memset(&pi, 0, sizeof(pi));
540         init_ref_bitmap(&pi.ref_bitmap);
541         pi.nr_bits = ref->nr;
542
543         /*
544          * "--not --all" to cut short the traversal if new refs
545          * connect to old refs. If not (e.g. force ref updates) it'll
546          * have to go down to the current shallow commits.
547          */
548         head_ref(mark_uninteresting, NULL);
549         for_each_ref(mark_uninteresting, NULL);
550
551         /* Mark potential bottoms so we won't go out of bound */
552         for (i = 0; i < nr_shallow; i++) {
553                 struct commit *c = lookup_commit(sha1[shallow[i]]);
554                 c->object.flags |= BOTTOM;
555         }
556
557         for (i = 0; i < ref->nr; i++)
558                 paint_down(&pi, ref->sha1[i], i);
559
560         if (used) {
561                 int bitmap_size = ((pi.nr_bits + 31) / 32) * sizeof(uint32_t);
562                 memset(used, 0, sizeof(*used) * info->shallow->nr);
563                 for (i = 0; i < nr_shallow; i++) {
564                         const struct commit *c = lookup_commit(sha1[shallow[i]]);
565                         uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
566                         if (*map)
567                                 used[shallow[i]] = xmemdupz(*map, bitmap_size);
568                 }
569                 /*
570                  * unreachable shallow commits are not removed from
571                  * "ours" and "theirs". The user is supposed to run
572                  * step 7 on every ref separately and not trust "ours"
573                  * and "theirs" any more.
574                  */
575         } else
576                 post_assign_shallow(info, &pi.ref_bitmap, ref_status);
577
578         clear_ref_bitmap(&pi.ref_bitmap);
579         for (i = 0; i < pi.slab_count; i++)
580                 free(pi.slab[i]);
581         free(pi.slab);
582         free(shallow);
583 }
584
585 struct commit_array {
586         struct commit **commits;
587         int nr, alloc;
588 };
589
590 static int add_ref(const char *refname,
591                    const unsigned char *sha1, int flags, void *cb_data)
592 {
593         struct commit_array *ca = cb_data;
594         ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
595         ca->commits[ca->nr] = lookup_commit_reference_gently(sha1, 1);
596         if (ca->commits[ca->nr])
597                 ca->nr++;
598         return 0;
599 }
600
601 static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
602 {
603         int i;
604         if (!ref_status)
605                 return;
606         for (i = 0; i < nr; i++)
607                 if (bitmap[i / 32] & (1 << (i % 32)))
608                         ref_status[i]++;
609 }
610
611 /*
612  * Step 7, reachability test on "ours" at commit level
613  */
614 static void post_assign_shallow(struct shallow_info *info,
615                                 struct ref_bitmap *ref_bitmap,
616                                 int *ref_status)
617 {
618         unsigned char (*sha1)[20] = info->shallow->sha1;
619         struct commit *c;
620         uint32_t **bitmap;
621         int dst, i, j;
622         int bitmap_nr = (info->ref->nr + 31) / 32;
623         struct commit_array ca;
624
625         trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
626         if (ref_status)
627                 memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
628
629         /* Remove unreachable shallow commits from "theirs" */
630         for (i = dst = 0; i < info->nr_theirs; i++) {
631                 if (i != dst)
632                         info->theirs[dst] = info->theirs[i];
633                 c = lookup_commit(sha1[info->theirs[i]]);
634                 bitmap = ref_bitmap_at(ref_bitmap, c);
635                 if (!*bitmap)
636                         continue;
637                 for (j = 0; j < bitmap_nr; j++)
638                         if (bitmap[0][j]) {
639                                 update_refstatus(ref_status, info->ref->nr, *bitmap);
640                                 dst++;
641                                 break;
642                         }
643         }
644         info->nr_theirs = dst;
645
646         memset(&ca, 0, sizeof(ca));
647         head_ref(add_ref, &ca);
648         for_each_ref(add_ref, &ca);
649
650         /* Remove unreachable shallow commits from "ours" */
651         for (i = dst = 0; i < info->nr_ours; i++) {
652                 if (i != dst)
653                         info->ours[dst] = info->ours[i];
654                 c = lookup_commit(sha1[info->ours[i]]);
655                 bitmap = ref_bitmap_at(ref_bitmap, c);
656                 if (!*bitmap)
657                         continue;
658                 for (j = 0; j < bitmap_nr; j++)
659                         if (bitmap[0][j] &&
660                             /* Step 7, reachability test at commit level */
661                             !in_merge_bases_many(c, ca.nr, ca.commits)) {
662                                 update_refstatus(ref_status, info->ref->nr, *bitmap);
663                                 dst++;
664                                 break;
665                         }
666         }
667         info->nr_ours = dst;
668
669         free(ca.commits);
670 }
671
672 /* (Delayed) step 7, reachability test at commit level */
673 int delayed_reachability_test(struct shallow_info *si, int c)
674 {
675         if (si->need_reachability_test[c]) {
676                 struct commit *commit = lookup_commit(si->shallow->sha1[c]);
677
678                 if (!si->commits) {
679                         struct commit_array ca;
680                         memset(&ca, 0, sizeof(ca));
681                         head_ref(add_ref, &ca);
682                         for_each_ref(add_ref, &ca);
683                         si->commits = ca.commits;
684                         si->nr_commits = ca.nr;
685                 }
686
687                 si->reachable[c] = in_merge_bases_many(commit,
688                                                        si->nr_commits,
689                                                        si->commits);
690                 si->need_reachability_test[c] = 0;
691         }
692         return si->reachable[c];
693 }