Coverage Report

Created: 2024-09-16 06:10

/src/git/shallow.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "hex.h"
5
#include "repository.h"
6
#include "tempfile.h"
7
#include "lockfile.h"
8
#include "object-store-ll.h"
9
#include "commit.h"
10
#include "tag.h"
11
#include "pkt-line.h"
12
#include "refs.h"
13
#include "oid-array.h"
14
#include "path.h"
15
#include "diff.h"
16
#include "revision.h"
17
#include "commit-slab.h"
18
#include "list-objects.h"
19
#include "commit-reach.h"
20
#include "shallow.h"
21
#include "statinfo.h"
22
#include "trace.h"
23
24
void set_alternate_shallow_file(struct repository *r, const char *path, int override)
25
0
{
26
0
  if (r->parsed_objects->is_shallow != -1)
27
0
    BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
28
0
  if (r->parsed_objects->alternate_shallow_file && !override)
29
0
    return;
30
0
  free(r->parsed_objects->alternate_shallow_file);
31
0
  r->parsed_objects->alternate_shallow_file = xstrdup_or_null(path);
32
0
}
33
34
int register_shallow(struct repository *r, const struct object_id *oid)
35
0
{
36
0
  struct commit_graft *graft =
37
0
    xmalloc(sizeof(struct commit_graft));
38
0
  struct commit *commit = lookup_commit(r, oid);
39
40
0
  oidcpy(&graft->oid, oid);
41
0
  graft->nr_parent = -1;
42
0
  if (commit && commit->object.parsed) {
43
0
    free_commit_list(commit->parents);
44
0
    commit->parents = NULL;
45
0
  }
46
0
  return register_commit_graft(r, graft, 0);
47
0
}
48
49
int unregister_shallow(const struct object_id *oid)
50
0
{
51
0
  int pos = commit_graft_pos(the_repository, oid);
52
0
  if (pos < 0)
53
0
    return -1;
54
0
  if (pos + 1 < the_repository->parsed_objects->grafts_nr) {
55
0
    free(the_repository->parsed_objects->grafts[pos]);
56
0
    MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
57
0
         the_repository->parsed_objects->grafts + pos + 1,
58
0
         the_repository->parsed_objects->grafts_nr - pos - 1);
59
0
  }
60
0
  the_repository->parsed_objects->grafts_nr--;
61
0
  return 0;
62
0
}
63
64
int is_repository_shallow(struct repository *r)
65
0
{
66
0
  FILE *fp;
67
0
  char buf[1024];
68
0
  const char *path = r->parsed_objects->alternate_shallow_file;
69
70
0
  if (r->parsed_objects->is_shallow >= 0)
71
0
    return r->parsed_objects->is_shallow;
72
73
0
  if (!path)
74
0
    path = git_path_shallow(r);
75
  /*
76
   * fetch-pack sets '--shallow-file ""' as an indicator that no
77
   * shallow file should be used. We could just open it and it
78
   * will likely fail. But let's do an explicit check instead.
79
   */
80
0
  if (!*path || (fp = fopen(path, "r")) == NULL) {
81
0
    stat_validity_clear(r->parsed_objects->shallow_stat);
82
0
    r->parsed_objects->is_shallow = 0;
83
0
    return r->parsed_objects->is_shallow;
84
0
  }
85
0
  stat_validity_update(r->parsed_objects->shallow_stat, fileno(fp));
86
0
  r->parsed_objects->is_shallow = 1;
87
88
0
  while (fgets(buf, sizeof(buf), fp)) {
89
0
    struct object_id oid;
90
0
    if (get_oid_hex(buf, &oid))
91
0
      die("bad shallow line: %s", buf);
92
0
    register_shallow(r, &oid);
93
0
  }
94
0
  fclose(fp);
95
0
  return r->parsed_objects->is_shallow;
96
0
}
97
98
static void reset_repository_shallow(struct repository *r)
99
0
{
100
0
  r->parsed_objects->is_shallow = -1;
101
0
  stat_validity_clear(r->parsed_objects->shallow_stat);
102
0
  parsed_object_pool_reset_commit_grafts(r->parsed_objects);
103
0
}
104
105
int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
106
0
{
107
0
  int res = commit_lock_file(&lk->lock);
108
0
  reset_repository_shallow(r);
109
110
  /*
111
   * Update in-memory data structures with the new shallow information,
112
   * including unparsing all commits that now have grafts.
113
   */
114
0
  is_repository_shallow(r);
115
116
0
  return res;
117
0
}
118
119
void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
120
0
{
121
0
  rollback_lock_file(&lk->lock);
122
0
  reset_repository_shallow(r);
123
0
}
124
125
/*
126
 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
127
 * supports a "valid" flag.
128
 */
129
define_commit_slab(commit_depth, int *);
130
static void free_depth_in_slab(int **ptr)
131
0
{
132
0
  FREE_AND_NULL(*ptr);
133
0
}
134
struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
135
    int shallow_flag, int not_shallow_flag)
136
0
{
137
0
  int i = 0, cur_depth = 0;
138
0
  struct commit_list *result = NULL;
139
0
  struct object_array stack = OBJECT_ARRAY_INIT;
140
0
  struct commit *commit = NULL;
141
0
  struct commit_graft *graft;
142
0
  struct commit_depth depths;
143
144
0
  init_commit_depth(&depths);
145
0
  while (commit || i < heads->nr || stack.nr) {
146
0
    struct commit_list *p;
147
0
    if (!commit) {
148
0
      if (i < heads->nr) {
149
0
        int **depth_slot;
150
0
        commit = (struct commit *)
151
0
          deref_tag(the_repository,
152
0
              heads->objects[i++].item,
153
0
              NULL, 0);
154
0
        if (!commit || commit->object.type != OBJ_COMMIT) {
155
0
          commit = NULL;
156
0
          continue;
157
0
        }
158
0
        depth_slot = commit_depth_at(&depths, commit);
159
0
        if (!*depth_slot)
160
0
          *depth_slot = xmalloc(sizeof(int));
161
0
        **depth_slot = 0;
162
0
        cur_depth = 0;
163
0
      } else {
164
0
        commit = (struct commit *)
165
0
          object_array_pop(&stack);
166
0
        cur_depth = **commit_depth_at(&depths, commit);
167
0
      }
168
0
    }
169
0
    parse_commit_or_die(commit);
170
0
    cur_depth++;
171
0
    if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
172
0
        (is_repository_shallow(the_repository) && !commit->parents &&
173
0
         (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
174
0
         graft->nr_parent < 0)) {
175
0
      commit_list_insert(commit, &result);
176
0
      commit->object.flags |= shallow_flag;
177
0
      commit = NULL;
178
0
      continue;
179
0
    }
180
0
    commit->object.flags |= not_shallow_flag;
181
0
    for (p = commit->parents, commit = NULL; p; p = p->next) {
182
0
      int **depth_slot = commit_depth_at(&depths, p->item);
183
0
      if (!*depth_slot) {
184
0
        *depth_slot = xmalloc(sizeof(int));
185
0
        **depth_slot = cur_depth;
186
0
      } else {
187
0
        if (cur_depth >= **depth_slot)
188
0
          continue;
189
0
        **depth_slot = cur_depth;
190
0
      }
191
0
      if (p->next)
192
0
        add_object_array(&p->item->object,
193
0
            NULL, &stack);
194
0
      else {
195
0
        commit = p->item;
196
0
        cur_depth = **commit_depth_at(&depths, commit);
197
0
      }
198
0
    }
199
0
  }
200
0
  deep_clear_commit_depth(&depths, free_depth_in_slab);
201
202
0
  return result;
203
0
}
204
205
static void show_commit(struct commit *commit, void *data)
206
0
{
207
0
  commit_list_insert(commit, data);
208
0
}
209
210
/*
211
 * Given rev-list arguments, run rev-list. All reachable commits
212
 * except border ones are marked with not_shallow_flag. Border commits
213
 * are marked with shallow_flag. The list of border/shallow commits
214
 * are also returned.
215
 */
216
struct commit_list *get_shallow_commits_by_rev_list(int ac, const char **av,
217
                int shallow_flag,
218
                int not_shallow_flag)
219
0
{
220
0
  struct commit_list *result = NULL, *p;
221
0
  struct commit_list *not_shallow_list = NULL;
222
0
  struct rev_info revs;
223
0
  int both_flags = shallow_flag | not_shallow_flag;
224
225
  /*
226
   * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
227
   * set at this point. But better be safe than sorry.
228
   */
229
0
  clear_object_flags(both_flags);
230
231
0
  is_repository_shallow(the_repository); /* make sure shallows are read */
232
233
0
  repo_init_revisions(the_repository, &revs, NULL);
234
0
  save_commit_buffer = 0;
235
0
  setup_revisions(ac, av, &revs, NULL);
236
237
0
  if (prepare_revision_walk(&revs))
238
0
    die("revision walk setup failed");
239
0
  traverse_commit_list(&revs, show_commit, NULL, &not_shallow_list);
240
241
0
  if (!not_shallow_list)
242
0
    die("no commits selected for shallow requests");
243
244
  /* Mark all reachable commits as NOT_SHALLOW */
245
0
  for (p = not_shallow_list; p; p = p->next)
246
0
    p->item->object.flags |= not_shallow_flag;
247
248
  /*
249
   * mark border commits SHALLOW + NOT_SHALLOW.
250
   * We cannot clear NOT_SHALLOW right now. Imagine border
251
   * commit A is processed first, then commit B, whose parent is
252
   * A, later. If NOT_SHALLOW on A is cleared at step 1, B
253
   * itself is considered border at step 2, which is incorrect.
254
   */
255
0
  for (p = not_shallow_list; p; p = p->next) {
256
0
    struct commit *c = p->item;
257
0
    struct commit_list *parent;
258
259
0
    if (repo_parse_commit(the_repository, c))
260
0
      die("unable to parse commit %s",
261
0
          oid_to_hex(&c->object.oid));
262
263
0
    for (parent = c->parents; parent; parent = parent->next)
264
0
      if (!(parent->item->object.flags & not_shallow_flag)) {
265
0
        c->object.flags |= shallow_flag;
266
0
        commit_list_insert(c, &result);
267
0
        break;
268
0
      }
269
0
  }
270
0
  free_commit_list(not_shallow_list);
271
272
  /*
273
   * Now we can clean up NOT_SHALLOW on border commits. Having
274
   * both flags set can confuse the caller.
275
   */
276
0
  for (p = result; p; p = p->next) {
277
0
    struct object *o = &p->item->object;
278
0
    if ((o->flags & both_flags) == both_flags)
279
0
      o->flags &= ~not_shallow_flag;
280
0
  }
281
0
  release_revisions(&revs);
282
0
  return result;
283
0
}
284
285
static void check_shallow_file_for_update(struct repository *r)
286
0
{
287
0
  if (r->parsed_objects->is_shallow == -1)
288
0
    BUG("shallow must be initialized by now");
289
290
0
  if (!stat_validity_check(r->parsed_objects->shallow_stat,
291
0
         git_path_shallow(r)))
292
0
    die("shallow file has changed since we read it");
293
0
}
294
295
0
#define SEEN_ONLY 1
296
0
#define VERBOSE   2
297
0
#define QUICK 4
298
299
struct write_shallow_data {
300
  struct strbuf *out;
301
  int use_pack_protocol;
302
  int count;
303
  unsigned flags;
304
};
305
306
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
307
0
{
308
0
  struct write_shallow_data *data = cb_data;
309
0
  const char *hex = oid_to_hex(&graft->oid);
310
0
  if (graft->nr_parent != -1)
311
0
    return 0;
312
0
  if (data->flags & QUICK) {
313
0
    if (!repo_has_object_file(the_repository, &graft->oid))
314
0
      return 0;
315
0
  } else if (data->flags & SEEN_ONLY) {
316
0
    struct commit *c = lookup_commit(the_repository, &graft->oid);
317
0
    if (!c || !(c->object.flags & SEEN)) {
318
0
      if (data->flags & VERBOSE)
319
0
        printf("Removing %s from .git/shallow\n",
320
0
               oid_to_hex(&c->object.oid));
321
0
      return 0;
322
0
    }
323
0
  }
324
0
  data->count++;
325
0
  if (data->use_pack_protocol)
326
0
    packet_buf_write(data->out, "shallow %s", hex);
327
0
  else {
328
0
    strbuf_addstr(data->out, hex);
329
0
    strbuf_addch(data->out, '\n');
330
0
  }
331
0
  return 0;
332
0
}
333
334
static int write_shallow_commits_1(struct strbuf *out, int use_pack_protocol,
335
           const struct oid_array *extra,
336
           unsigned flags)
337
0
{
338
0
  struct write_shallow_data data;
339
0
  int i;
340
0
  data.out = out;
341
0
  data.use_pack_protocol = use_pack_protocol;
342
0
  data.count = 0;
343
0
  data.flags = flags;
344
0
  for_each_commit_graft(write_one_shallow, &data);
345
0
  if (!extra)
346
0
    return data.count;
347
0
  for (i = 0; i < extra->nr; i++) {
348
0
    strbuf_addstr(out, oid_to_hex(extra->oid + i));
349
0
    strbuf_addch(out, '\n');
350
0
    data.count++;
351
0
  }
352
0
  return data.count;
353
0
}
354
355
int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
356
        const struct oid_array *extra)
357
0
{
358
0
  return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
359
0
}
360
361
const char *setup_temporary_shallow(const struct oid_array *extra)
362
0
{
363
0
  struct tempfile *temp;
364
0
  struct strbuf sb = STRBUF_INIT;
365
366
0
  if (write_shallow_commits(&sb, 0, extra)) {
367
0
    temp = xmks_tempfile(git_path("shallow_XXXXXX"));
368
369
0
    if (write_in_full(temp->fd, sb.buf, sb.len) < 0 ||
370
0
        close_tempfile_gently(temp) < 0)
371
0
      die_errno("failed to write to %s",
372
0
          get_tempfile_path(temp));
373
0
    strbuf_release(&sb);
374
0
    return get_tempfile_path(temp);
375
0
  }
376
  /*
377
   * is_repository_shallow() sees empty string as "no shallow
378
   * file".
379
   */
380
0
  return "";
381
0
}
382
383
void setup_alternate_shallow(struct shallow_lock *shallow_lock,
384
           const char **alternate_shallow_file,
385
           const struct oid_array *extra)
386
0
{
387
0
  struct strbuf sb = STRBUF_INIT;
388
0
  int fd;
389
390
0
  fd = hold_lock_file_for_update(&shallow_lock->lock,
391
0
               git_path_shallow(the_repository),
392
0
               LOCK_DIE_ON_ERROR);
393
0
  check_shallow_file_for_update(the_repository);
394
0
  if (write_shallow_commits(&sb, 0, extra)) {
395
0
    if (write_in_full(fd, sb.buf, sb.len) < 0)
396
0
      die_errno("failed to write to %s",
397
0
          get_lock_file_path(&shallow_lock->lock));
398
0
    *alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
399
0
  } else
400
    /*
401
     * is_repository_shallow() sees empty string as "no
402
     * shallow file".
403
     */
404
0
    *alternate_shallow_file = "";
405
0
  strbuf_release(&sb);
406
0
}
407
408
static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb)
409
0
{
410
0
  int fd = *(int *)cb;
411
0
  if (graft->nr_parent == -1)
412
0
    packet_write_fmt(fd, "shallow %s\n", oid_to_hex(&graft->oid));
413
0
  return 0;
414
0
}
415
416
void advertise_shallow_grafts(int fd)
417
0
{
418
0
  if (!is_repository_shallow(the_repository))
419
0
    return;
420
0
  for_each_commit_graft(advertise_shallow_grafts_cb, &fd);
421
0
}
422
423
/*
424
 * mark_reachable_objects() should have been run prior to this and all
425
 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
426
 * in which case lines are excised from the shallow file if they refer to
427
 * commits that do not exist (any longer).
428
 */
429
void prune_shallow(unsigned options)
430
0
{
431
0
  struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
432
0
  struct strbuf sb = STRBUF_INIT;
433
0
  unsigned flags = SEEN_ONLY;
434
0
  int fd;
435
436
0
  if (options & PRUNE_QUICK)
437
0
    flags |= QUICK;
438
439
0
  if (options & PRUNE_SHOW_ONLY) {
440
0
    flags |= VERBOSE;
441
0
    write_shallow_commits_1(&sb, 0, NULL, flags);
442
0
    strbuf_release(&sb);
443
0
    return;
444
0
  }
445
0
  fd = hold_lock_file_for_update(&shallow_lock.lock,
446
0
               git_path_shallow(the_repository),
447
0
               LOCK_DIE_ON_ERROR);
448
0
  check_shallow_file_for_update(the_repository);
449
0
  if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
450
0
    if (write_in_full(fd, sb.buf, sb.len) < 0)
451
0
      die_errno("failed to write to %s",
452
0
          get_lock_file_path(&shallow_lock.lock));
453
0
    commit_shallow_file(the_repository, &shallow_lock);
454
0
  } else {
455
0
    unlink(git_path_shallow(the_repository));
456
0
    rollback_shallow_file(the_repository, &shallow_lock);
457
0
  }
458
0
  strbuf_release(&sb);
459
0
}
460
461
struct trace_key trace_shallow = TRACE_KEY_INIT(SHALLOW);
462
463
/*
464
 * Step 1, split sender shallow commits into "ours" and "theirs"
465
 * Step 2, clean "ours" based on .git/shallow
466
 */
467
void prepare_shallow_info(struct shallow_info *info, struct oid_array *sa)
468
0
{
469
0
  int i;
470
0
  trace_printf_key(&trace_shallow, "shallow: prepare_shallow_info\n");
471
0
  memset(info, 0, sizeof(*info));
472
0
  info->shallow = sa;
473
0
  if (!sa)
474
0
    return;
475
0
  ALLOC_ARRAY(info->ours, sa->nr);
476
0
  ALLOC_ARRAY(info->theirs, sa->nr);
477
0
  for (i = 0; i < sa->nr; i++) {
478
0
    if (repo_has_object_file(the_repository, sa->oid + i)) {
479
0
      struct commit_graft *graft;
480
0
      graft = lookup_commit_graft(the_repository,
481
0
                &sa->oid[i]);
482
0
      if (graft && graft->nr_parent < 0)
483
0
        continue;
484
0
      info->ours[info->nr_ours++] = i;
485
0
    } else
486
0
      info->theirs[info->nr_theirs++] = i;
487
0
  }
488
0
}
489
490
void clear_shallow_info(struct shallow_info *info)
491
0
{
492
0
  if (info->used_shallow) {
493
0
    for (size_t i = 0; i < info->shallow->nr; i++)
494
0
      free(info->used_shallow[i]);
495
0
    free(info->used_shallow);
496
0
  }
497
498
0
  free(info->need_reachability_test);
499
0
  free(info->reachable);
500
0
  free(info->shallow_ref);
501
0
  free(info->ours);
502
0
  free(info->theirs);
503
0
}
504
505
/* Step 4, remove non-existent ones in "theirs" after getting the pack */
506
507
void remove_nonexistent_theirs_shallow(struct shallow_info *info)
508
0
{
509
0
  struct object_id *oid = info->shallow->oid;
510
0
  int i, dst;
511
0
  trace_printf_key(&trace_shallow, "shallow: remove_nonexistent_theirs_shallow\n");
512
0
  for (i = dst = 0; i < info->nr_theirs; i++) {
513
0
    if (i != dst)
514
0
      info->theirs[dst] = info->theirs[i];
515
0
    if (repo_has_object_file(the_repository, oid + info->theirs[i]))
516
0
      dst++;
517
0
  }
518
0
  info->nr_theirs = dst;
519
0
}
520
521
define_commit_slab(ref_bitmap, uint32_t *);
522
523
0
#define POOL_SIZE (512 * 1024)
524
525
struct paint_info {
526
  struct ref_bitmap ref_bitmap;
527
  unsigned nr_bits;
528
  char **pools;
529
  char *free, *end;
530
  unsigned pool_count;
531
};
532
533
static uint32_t *paint_alloc(struct paint_info *info)
534
0
{
535
0
  unsigned nr = DIV_ROUND_UP(info->nr_bits, 32);
536
0
  unsigned size = nr * sizeof(uint32_t);
537
0
  void *p;
538
0
  if (!info->pool_count || size > info->end - info->free) {
539
0
    if (size > POOL_SIZE)
540
0
      BUG("pool size too small for %d in paint_alloc()",
541
0
          size);
542
0
    info->pool_count++;
543
0
    REALLOC_ARRAY(info->pools, info->pool_count);
544
0
    info->free = xmalloc(POOL_SIZE);
545
0
    info->pools[info->pool_count - 1] = info->free;
546
0
    info->end = info->free + POOL_SIZE;
547
0
  }
548
0
  p = info->free;
549
0
  info->free += size;
550
0
  return p;
551
0
}
552
553
/*
554
 * Given a commit SHA-1, walk down to parents until either SEEN,
555
 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
556
 * all walked commits.
557
 */
558
static void paint_down(struct paint_info *info, const struct object_id *oid,
559
           unsigned int id)
560
0
{
561
0
  unsigned int i, nr;
562
0
  struct commit_list *head = NULL;
563
0
  int bitmap_nr = DIV_ROUND_UP(info->nr_bits, 32);
564
0
  size_t bitmap_size = st_mult(sizeof(uint32_t), bitmap_nr);
565
0
  struct commit *c = lookup_commit_reference_gently(the_repository, oid,
566
0
                1);
567
0
  uint32_t *tmp; /* to be freed before return */
568
0
  uint32_t *bitmap;
569
570
0
  if (!c)
571
0
    return;
572
573
0
  tmp = xmalloc(bitmap_size);
574
0
  bitmap = paint_alloc(info);
575
0
  memset(bitmap, 0, bitmap_size);
576
0
  bitmap[id / 32] |= (1U << (id % 32));
577
0
  commit_list_insert(c, &head);
578
0
  while (head) {
579
0
    struct commit_list *p;
580
0
    struct commit *c = pop_commit(&head);
581
0
    uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
582
583
    /* XXX check "UNINTERESTING" from pack bitmaps if available */
584
0
    if (c->object.flags & (SEEN | UNINTERESTING))
585
0
      continue;
586
0
    else
587
0
      c->object.flags |= SEEN;
588
589
0
    if (!*refs)
590
0
      *refs = bitmap;
591
0
    else {
592
0
      memcpy(tmp, *refs, bitmap_size);
593
0
      for (i = 0; i < bitmap_nr; i++)
594
0
        tmp[i] |= bitmap[i];
595
0
      if (memcmp(tmp, *refs, bitmap_size)) {
596
0
        *refs = paint_alloc(info);
597
0
        memcpy(*refs, tmp, bitmap_size);
598
0
      }
599
0
    }
600
601
0
    if (c->object.flags & BOTTOM)
602
0
      continue;
603
604
0
    if (repo_parse_commit(the_repository, c))
605
0
      die("unable to parse commit %s",
606
0
          oid_to_hex(&c->object.oid));
607
608
0
    for (p = c->parents; p; p = p->next) {
609
0
      if (p->item->object.flags & SEEN)
610
0
        continue;
611
0
      commit_list_insert(p->item, &head);
612
0
    }
613
0
  }
614
615
0
  nr = get_max_object_index();
616
0
  for (i = 0; i < nr; i++) {
617
0
    struct object *o = get_indexed_object(i);
618
0
    if (o && o->type == OBJ_COMMIT)
619
0
      o->flags &= ~SEEN;
620
0
  }
621
622
0
  free(tmp);
623
0
}
624
625
static int mark_uninteresting(const char *refname UNUSED,
626
            const char *referent UNUSED,
627
            const struct object_id *oid,
628
            int flags UNUSED,
629
            void *cb_data UNUSED)
630
0
{
631
0
  struct commit *commit = lookup_commit_reference_gently(the_repository,
632
0
                     oid, 1);
633
0
  if (!commit)
634
0
    return 0;
635
0
  commit->object.flags |= UNINTERESTING;
636
0
  mark_parents_uninteresting(NULL, commit);
637
0
  return 0;
638
0
}
639
640
static void post_assign_shallow(struct shallow_info *info,
641
        struct ref_bitmap *ref_bitmap,
642
        int *ref_status);
643
/*
644
 * Step 6(+7), associate shallow commits with new refs
645
 *
646
 * info->ref must be initialized before calling this function.
647
 *
648
 * If used is not NULL, it's an array of info->shallow->nr
649
 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
650
 * m-th shallow commit from info->shallow.
651
 *
652
 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
653
 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
654
 * the ref needs some shallow commits from either info->ours or
655
 * info->theirs.
656
 */
657
void assign_shallow_commits_to_refs(struct shallow_info *info,
658
            uint32_t **used, int *ref_status)
659
0
{
660
0
  struct object_id *oid = info->shallow->oid;
661
0
  struct oid_array *ref = info->ref;
662
0
  unsigned int i, nr;
663
0
  int *shallow, nr_shallow = 0;
664
0
  struct paint_info pi;
665
666
0
  trace_printf_key(&trace_shallow, "shallow: assign_shallow_commits_to_refs\n");
667
0
  ALLOC_ARRAY(shallow, info->nr_ours + info->nr_theirs);
668
0
  for (i = 0; i < info->nr_ours; i++)
669
0
    shallow[nr_shallow++] = info->ours[i];
670
0
  for (i = 0; i < info->nr_theirs; i++)
671
0
    shallow[nr_shallow++] = info->theirs[i];
672
673
  /*
674
   * Prepare the commit graph to track what refs can reach what
675
   * (new) shallow commits.
676
   */
677
0
  nr = get_max_object_index();
678
0
  for (i = 0; i < nr; i++) {
679
0
    struct object *o = get_indexed_object(i);
680
0
    if (!o || o->type != OBJ_COMMIT)
681
0
      continue;
682
683
0
    o->flags &= ~(UNINTERESTING | BOTTOM | SEEN);
684
0
  }
685
686
0
  memset(&pi, 0, sizeof(pi));
687
0
  init_ref_bitmap(&pi.ref_bitmap);
688
0
  pi.nr_bits = ref->nr;
689
690
  /*
691
   * "--not --all" to cut short the traversal if new refs
692
   * connect to old refs. If not (e.g. force ref updates) it'll
693
   * have to go down to the current shallow commits.
694
   */
695
0
  refs_head_ref(get_main_ref_store(the_repository), mark_uninteresting,
696
0
          NULL);
697
0
  refs_for_each_ref(get_main_ref_store(the_repository),
698
0
        mark_uninteresting, NULL);
699
700
  /* Mark potential bottoms so we won't go out of bound */
701
0
  for (i = 0; i < nr_shallow; i++) {
702
0
    struct commit *c = lookup_commit(the_repository,
703
0
             &oid[shallow[i]]);
704
0
    c->object.flags |= BOTTOM;
705
0
  }
706
707
0
  for (i = 0; i < ref->nr; i++)
708
0
    paint_down(&pi, ref->oid + i, i);
709
710
0
  if (used) {
711
0
    int bitmap_size = DIV_ROUND_UP(pi.nr_bits, 32) * sizeof(uint32_t);
712
0
    memset(used, 0, sizeof(*used) * info->shallow->nr);
713
0
    for (i = 0; i < nr_shallow; i++) {
714
0
      const struct commit *c = lookup_commit(the_repository,
715
0
                     &oid[shallow[i]]);
716
0
      uint32_t **map = ref_bitmap_at(&pi.ref_bitmap, c);
717
0
      if (*map)
718
0
        used[shallow[i]] = xmemdupz(*map, bitmap_size);
719
0
    }
720
    /*
721
     * unreachable shallow commits are not removed from
722
     * "ours" and "theirs". The user is supposed to run
723
     * step 7 on every ref separately and not trust "ours"
724
     * and "theirs" any more.
725
     */
726
0
  } else
727
0
    post_assign_shallow(info, &pi.ref_bitmap, ref_status);
728
729
0
  clear_ref_bitmap(&pi.ref_bitmap);
730
0
  for (i = 0; i < pi.pool_count; i++)
731
0
    free(pi.pools[i]);
732
0
  free(pi.pools);
733
0
  free(shallow);
734
0
}
735
736
struct commit_array {
737
  struct commit **commits;
738
  int nr, alloc;
739
};
740
741
static int add_ref(const char *refname UNUSED,
742
      const char *referent UNUSED,
743
       const struct object_id *oid,
744
       int flags UNUSED,
745
       void *cb_data)
746
0
{
747
0
  struct commit_array *ca = cb_data;
748
0
  ALLOC_GROW(ca->commits, ca->nr + 1, ca->alloc);
749
0
  ca->commits[ca->nr] = lookup_commit_reference_gently(the_repository,
750
0
                   oid, 1);
751
0
  if (ca->commits[ca->nr])
752
0
    ca->nr++;
753
0
  return 0;
754
0
}
755
756
static void update_refstatus(int *ref_status, int nr, uint32_t *bitmap)
757
0
{
758
0
  unsigned int i;
759
0
  if (!ref_status)
760
0
    return;
761
0
  for (i = 0; i < nr; i++)
762
0
    if (bitmap[i / 32] & (1U << (i % 32)))
763
0
      ref_status[i]++;
764
0
}
765
766
/*
767
 * Step 7, reachability test on "ours" at commit level
768
 */
769
static void post_assign_shallow(struct shallow_info *info,
770
        struct ref_bitmap *ref_bitmap,
771
        int *ref_status)
772
0
{
773
0
  struct object_id *oid = info->shallow->oid;
774
0
  struct commit *c;
775
0
  uint32_t **bitmap;
776
0
  int dst, i, j;
777
0
  int bitmap_nr = DIV_ROUND_UP(info->ref->nr, 32);
778
0
  struct commit_array ca;
779
780
0
  trace_printf_key(&trace_shallow, "shallow: post_assign_shallow\n");
781
0
  if (ref_status)
782
0
    memset(ref_status, 0, sizeof(*ref_status) * info->ref->nr);
783
784
  /* Remove unreachable shallow commits from "theirs" */
785
0
  for (i = dst = 0; i < info->nr_theirs; i++) {
786
0
    if (i != dst)
787
0
      info->theirs[dst] = info->theirs[i];
788
0
    c = lookup_commit(the_repository, &oid[info->theirs[i]]);
789
0
    bitmap = ref_bitmap_at(ref_bitmap, c);
790
0
    if (!*bitmap)
791
0
      continue;
792
0
    for (j = 0; j < bitmap_nr; j++)
793
0
      if (bitmap[0][j]) {
794
0
        update_refstatus(ref_status, info->ref->nr, *bitmap);
795
0
        dst++;
796
0
        break;
797
0
      }
798
0
  }
799
0
  info->nr_theirs = dst;
800
801
0
  memset(&ca, 0, sizeof(ca));
802
0
  refs_head_ref(get_main_ref_store(the_repository), add_ref, &ca);
803
0
  refs_for_each_ref(get_main_ref_store(the_repository), add_ref, &ca);
804
805
  /* Remove unreachable shallow commits from "ours" */
806
0
  for (i = dst = 0; i < info->nr_ours; i++) {
807
0
    if (i != dst)
808
0
      info->ours[dst] = info->ours[i];
809
0
    c = lookup_commit(the_repository, &oid[info->ours[i]]);
810
0
    bitmap = ref_bitmap_at(ref_bitmap, c);
811
0
    if (!*bitmap)
812
0
      continue;
813
0
    for (j = 0; j < bitmap_nr; j++)
814
0
      if (bitmap[0][j]) {
815
        /* Step 7, reachability test at commit level */
816
0
        int ret = repo_in_merge_bases_many(the_repository, c, ca.nr, ca.commits, 1);
817
0
        if (ret < 0)
818
0
          exit(128);
819
0
        if (!ret) {
820
0
          update_refstatus(ref_status, info->ref->nr, *bitmap);
821
0
          dst++;
822
0
          break;
823
0
        }
824
0
      }
825
0
  }
826
0
  info->nr_ours = dst;
827
828
0
  free(ca.commits);
829
0
}
830
831
/* (Delayed) step 7, reachability test at commit level */
832
int delayed_reachability_test(struct shallow_info *si, int c)
833
0
{
834
0
  if (si->need_reachability_test[c]) {
835
0
    struct commit *commit = lookup_commit(the_repository,
836
0
                  &si->shallow->oid[c]);
837
838
0
    if (!si->commits) {
839
0
      struct commit_array ca;
840
841
0
      memset(&ca, 0, sizeof(ca));
842
0
      refs_head_ref(get_main_ref_store(the_repository),
843
0
              add_ref, &ca);
844
0
      refs_for_each_ref(get_main_ref_store(the_repository),
845
0
            add_ref, &ca);
846
0
      si->commits = ca.commits;
847
0
      si->nr_commits = ca.nr;
848
0
    }
849
850
0
    si->reachable[c] = repo_in_merge_bases_many(the_repository,
851
0
                  commit,
852
0
                  si->nr_commits,
853
0
                  si->commits,
854
0
                  1);
855
0
    if (si->reachable[c] < 0)
856
0
      exit(128);
857
0
    si->need_reachability_test[c] = 0;
858
0
  }
859
0
  return si->reachable[c];
860
0
}