Coverage Report

Created: 2024-09-08 06:23

/src/git/list-objects.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "tag.h"
5
#include "commit.h"
6
#include "gettext.h"
7
#include "hex.h"
8
#include "tree.h"
9
#include "blob.h"
10
#include "diff.h"
11
#include "tree-walk.h"
12
#include "revision.h"
13
#include "list-objects.h"
14
#include "list-objects-filter.h"
15
#include "list-objects-filter-options.h"
16
#include "packfile.h"
17
#include "object-store-ll.h"
18
#include "trace.h"
19
#include "environment.h"
20
21
struct traversal_context {
22
  struct rev_info *revs;
23
  show_object_fn show_object;
24
  show_commit_fn show_commit;
25
  void *show_data;
26
  struct filter *filter;
27
  int depth;
28
};
29
30
static void show_commit(struct traversal_context *ctx,
31
      struct commit *commit)
32
0
{
33
0
  if (!ctx->show_commit)
34
0
    return;
35
0
  ctx->show_commit(commit, ctx->show_data);
36
0
}
37
38
static void show_object(struct traversal_context *ctx,
39
      struct object *object,
40
      const char *name)
41
0
{
42
0
  if (!ctx->show_object)
43
0
    return;
44
0
  if (ctx->revs->unpacked && has_object_pack(&object->oid))
45
0
    return;
46
47
0
  ctx->show_object(object, name, ctx->show_data);
48
0
}
49
50
static void process_blob(struct traversal_context *ctx,
51
       struct blob *blob,
52
       struct strbuf *path,
53
       const char *name)
54
0
{
55
0
  struct object *obj = &blob->object;
56
0
  size_t pathlen;
57
0
  enum list_objects_filter_result r;
58
59
0
  if (!ctx->revs->blob_objects)
60
0
    return;
61
0
  if (!obj)
62
0
    die("bad blob object");
63
0
  if (obj->flags & (UNINTERESTING | SEEN))
64
0
    return;
65
66
  /*
67
   * Pre-filter known-missing objects when explicitly requested.
68
   * Otherwise, a missing object error message may be reported
69
   * later (depending on other filtering criteria).
70
   *
71
   * Note that this "--exclude-promisor-objects" pre-filtering
72
   * may cause the actual filter to report an incomplete list
73
   * of missing objects.
74
   */
75
0
  if (ctx->revs->exclude_promisor_objects &&
76
0
      !repo_has_object_file(the_repository, &obj->oid) &&
77
0
      is_promisor_object(&obj->oid))
78
0
    return;
79
80
0
  pathlen = path->len;
81
0
  strbuf_addstr(path, name);
82
0
  r = list_objects_filter__filter_object(ctx->revs->repo,
83
0
                 LOFS_BLOB, obj,
84
0
                 path->buf, &path->buf[pathlen],
85
0
                 ctx->filter);
86
0
  if (r & LOFR_MARK_SEEN)
87
0
    obj->flags |= SEEN;
88
0
  if (r & LOFR_DO_SHOW)
89
0
    show_object(ctx, obj, path->buf);
90
0
  strbuf_setlen(path, pathlen);
91
0
}
92
93
static void process_tree(struct traversal_context *ctx,
94
       struct tree *tree,
95
       struct strbuf *base,
96
       const char *name);
97
98
static void process_tree_contents(struct traversal_context *ctx,
99
          struct tree *tree,
100
          struct strbuf *base)
101
0
{
102
0
  struct tree_desc desc;
103
0
  struct name_entry entry;
104
0
  enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
105
0
    all_entries_interesting : entry_not_interesting;
106
107
0
  init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
108
109
0
  while (tree_entry(&desc, &entry)) {
110
0
    if (match != all_entries_interesting) {
111
0
      match = tree_entry_interesting(ctx->revs->repo->index,
112
0
                   &entry, base,
113
0
                   &ctx->revs->diffopt.pathspec);
114
0
      if (match == all_entries_not_interesting)
115
0
        break;
116
0
      if (match == entry_not_interesting)
117
0
        continue;
118
0
    }
119
120
0
    if (S_ISDIR(entry.mode)) {
121
0
      struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
122
0
      if (!t) {
123
0
        die(_("entry '%s' in tree %s has tree mode, "
124
0
              "but is not a tree"),
125
0
            entry.path, oid_to_hex(&tree->object.oid));
126
0
      }
127
0
      t->object.flags |= NOT_USER_GIVEN;
128
0
      ctx->depth++;
129
0
      process_tree(ctx, t, base, entry.path);
130
0
      ctx->depth--;
131
0
    }
132
0
    else if (S_ISGITLINK(entry.mode))
133
0
      ; /* ignore gitlink */
134
0
    else {
135
0
      struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
136
0
      if (!b) {
137
0
        die(_("entry '%s' in tree %s has blob mode, "
138
0
              "but is not a blob"),
139
0
            entry.path, oid_to_hex(&tree->object.oid));
140
0
      }
141
0
      b->object.flags |= NOT_USER_GIVEN;
142
0
      process_blob(ctx, b, base, entry.path);
143
0
    }
144
0
  }
145
0
}
146
147
static void process_tree(struct traversal_context *ctx,
148
       struct tree *tree,
149
       struct strbuf *base,
150
       const char *name)
151
0
{
152
0
  struct object *obj = &tree->object;
153
0
  struct rev_info *revs = ctx->revs;
154
0
  int baselen = base->len;
155
0
  enum list_objects_filter_result r;
156
0
  int failed_parse;
157
158
0
  if (!revs->tree_objects)
159
0
    return;
160
0
  if (!obj)
161
0
    die("bad tree object");
162
0
  if (obj->flags & (UNINTERESTING | SEEN))
163
0
    return;
164
0
  if (revs->include_check_obj &&
165
0
      !revs->include_check_obj(&tree->object, revs->include_check_data))
166
0
    return;
167
168
0
  if (ctx->depth > max_allowed_tree_depth)
169
0
    die("exceeded maximum allowed tree depth");
170
171
0
  failed_parse = parse_tree_gently(tree, 1);
172
0
  if (failed_parse) {
173
0
    if (revs->ignore_missing_links)
174
0
      return;
175
176
    /*
177
     * Pre-filter known-missing tree objects when explicitly
178
     * requested.  This may cause the actual filter to report
179
     * an incomplete list of missing objects.
180
     */
181
0
    if (revs->exclude_promisor_objects &&
182
0
        is_promisor_object(&obj->oid))
183
0
      return;
184
185
0
    if (!revs->do_not_die_on_missing_objects)
186
0
      die("bad tree object %s", oid_to_hex(&obj->oid));
187
0
  }
188
189
0
  strbuf_addstr(base, name);
190
0
  r = list_objects_filter__filter_object(ctx->revs->repo,
191
0
                 LOFS_BEGIN_TREE, obj,
192
0
                 base->buf, &base->buf[baselen],
193
0
                 ctx->filter);
194
0
  if (r & LOFR_MARK_SEEN)
195
0
    obj->flags |= SEEN;
196
0
  if (r & LOFR_DO_SHOW)
197
0
    show_object(ctx, obj, base->buf);
198
0
  if (base->len)
199
0
    strbuf_addch(base, '/');
200
201
0
  if (r & LOFR_SKIP_TREE)
202
0
    trace_printf("Skipping contents of tree %s...\n", base->buf);
203
0
  else if (!failed_parse)
204
0
    process_tree_contents(ctx, tree, base);
205
206
0
  r = list_objects_filter__filter_object(ctx->revs->repo,
207
0
                 LOFS_END_TREE, obj,
208
0
                 base->buf, &base->buf[baselen],
209
0
                 ctx->filter);
210
0
  if (r & LOFR_MARK_SEEN)
211
0
    obj->flags |= SEEN;
212
0
  if (r & LOFR_DO_SHOW)
213
0
    show_object(ctx, obj, base->buf);
214
215
0
  strbuf_setlen(base, baselen);
216
0
  free_tree_buffer(tree);
217
0
}
218
219
static void process_tag(struct traversal_context *ctx,
220
      struct tag *tag,
221
      const char *name)
222
0
{
223
0
  enum list_objects_filter_result r;
224
225
0
  r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
226
0
                 &tag->object, NULL, NULL,
227
0
                 ctx->filter);
228
0
  if (r & LOFR_MARK_SEEN)
229
0
    tag->object.flags |= SEEN;
230
0
  if (r & LOFR_DO_SHOW)
231
0
    show_object(ctx, &tag->object, name);
232
0
}
233
234
static void mark_edge_parents_uninteresting(struct commit *commit,
235
              struct rev_info *revs,
236
              show_edge_fn show_edge)
237
0
{
238
0
  struct commit_list *parents;
239
240
0
  for (parents = commit->parents; parents; parents = parents->next) {
241
0
    struct commit *parent = parents->item;
242
0
    if (!(parent->object.flags & UNINTERESTING))
243
0
      continue;
244
0
    mark_tree_uninteresting(revs->repo,
245
0
          repo_get_commit_tree(the_repository, parent));
246
0
    if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
247
0
      parent->object.flags |= SHOWN;
248
0
      show_edge(parent);
249
0
    }
250
0
  }
251
0
}
252
253
static void add_edge_parents(struct commit *commit,
254
           struct rev_info *revs,
255
           show_edge_fn show_edge,
256
           struct oidset *set)
257
0
{
258
0
  struct commit_list *parents;
259
260
0
  for (parents = commit->parents; parents; parents = parents->next) {
261
0
    struct commit *parent = parents->item;
262
0
    struct tree *tree = repo_get_commit_tree(the_repository,
263
0
               parent);
264
265
0
    if (!tree)
266
0
      continue;
267
268
0
    oidset_insert(set, &tree->object.oid);
269
270
0
    if (!(parent->object.flags & UNINTERESTING))
271
0
      continue;
272
0
    tree->object.flags |= UNINTERESTING;
273
274
0
    if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
275
0
      parent->object.flags |= SHOWN;
276
0
      show_edge(parent);
277
0
    }
278
0
  }
279
0
}
280
281
void mark_edges_uninteresting(struct rev_info *revs,
282
            show_edge_fn show_edge,
283
            int sparse)
284
0
{
285
0
  struct commit_list *list;
286
0
  int i;
287
288
0
  if (sparse) {
289
0
    struct oidset set;
290
0
    oidset_init(&set, 16);
291
292
0
    for (list = revs->commits; list; list = list->next) {
293
0
      struct commit *commit = list->item;
294
0
      struct tree *tree = repo_get_commit_tree(the_repository,
295
0
                 commit);
296
297
0
      if (commit->object.flags & UNINTERESTING)
298
0
        tree->object.flags |= UNINTERESTING;
299
300
0
      oidset_insert(&set, &tree->object.oid);
301
0
      add_edge_parents(commit, revs, show_edge, &set);
302
0
    }
303
304
0
    mark_trees_uninteresting_sparse(revs->repo, &set);
305
0
    oidset_clear(&set);
306
0
  } else {
307
0
    for (list = revs->commits; list; list = list->next) {
308
0
      struct commit *commit = list->item;
309
0
      if (commit->object.flags & UNINTERESTING) {
310
0
        mark_tree_uninteresting(revs->repo,
311
0
              repo_get_commit_tree(the_repository, commit));
312
0
        if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
313
0
          commit->object.flags |= SHOWN;
314
0
          show_edge(commit);
315
0
        }
316
0
        continue;
317
0
      }
318
0
      mark_edge_parents_uninteresting(commit, revs, show_edge);
319
0
    }
320
0
  }
321
322
0
  if (revs->edge_hint_aggressive) {
323
0
    for (i = 0; i < revs->cmdline.nr; i++) {
324
0
      struct object *obj = revs->cmdline.rev[i].item;
325
0
      struct commit *commit = (struct commit *)obj;
326
0
      if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
327
0
        continue;
328
0
      mark_tree_uninteresting(revs->repo,
329
0
            repo_get_commit_tree(the_repository, commit));
330
0
      if (!(obj->flags & SHOWN)) {
331
0
        obj->flags |= SHOWN;
332
0
        show_edge(commit);
333
0
      }
334
0
    }
335
0
  }
336
0
}
337
338
static void add_pending_tree(struct rev_info *revs, struct tree *tree)
339
0
{
340
0
  add_pending_object(revs, &tree->object, "");
341
0
}
342
343
static void traverse_non_commits(struct traversal_context *ctx,
344
         struct strbuf *base)
345
0
{
346
0
  int i;
347
348
0
  assert(base->len == 0);
349
350
0
  for (i = 0; i < ctx->revs->pending.nr; i++) {
351
0
    struct object_array_entry *pending = ctx->revs->pending.objects + i;
352
0
    struct object *obj = pending->item;
353
0
    const char *name = pending->name;
354
0
    const char *path = pending->path;
355
0
    if (obj->flags & (UNINTERESTING | SEEN))
356
0
      continue;
357
0
    if (obj->type == OBJ_TAG) {
358
0
      process_tag(ctx, (struct tag *)obj, name);
359
0
      continue;
360
0
    }
361
0
    if (!path)
362
0
      path = "";
363
0
    if (obj->type == OBJ_TREE) {
364
0
      ctx->depth = 0;
365
0
      process_tree(ctx, (struct tree *)obj, base, path);
366
0
      continue;
367
0
    }
368
0
    if (obj->type == OBJ_BLOB) {
369
0
      process_blob(ctx, (struct blob *)obj, base, path);
370
0
      continue;
371
0
    }
372
0
    die("unknown pending object %s (%s)",
373
0
        oid_to_hex(&obj->oid), name);
374
0
  }
375
0
  object_array_clear(&ctx->revs->pending);
376
0
}
377
378
static void do_traverse(struct traversal_context *ctx)
379
0
{
380
0
  struct commit *commit;
381
0
  struct strbuf csp; /* callee's scratch pad */
382
0
  strbuf_init(&csp, PATH_MAX);
383
384
0
  while ((commit = get_revision(ctx->revs)) != NULL) {
385
0
    enum list_objects_filter_result r;
386
387
0
    r = list_objects_filter__filter_object(ctx->revs->repo,
388
0
        LOFS_COMMIT, &commit->object,
389
0
        NULL, NULL, ctx->filter);
390
391
    /*
392
     * an uninteresting boundary commit may not have its tree
393
     * parsed yet, but we are not going to show them anyway
394
     */
395
0
    if (!ctx->revs->tree_objects)
396
0
      ; /* do not bother loading tree */
397
0
    else if (ctx->revs->do_not_die_on_missing_objects &&
398
0
       oidset_contains(&ctx->revs->missing_commits, &commit->object.oid))
399
0
      ;
400
0
    else if (repo_get_commit_tree(the_repository, commit)) {
401
0
      struct tree *tree = repo_get_commit_tree(the_repository,
402
0
                 commit);
403
0
      tree->object.flags |= NOT_USER_GIVEN;
404
0
      add_pending_tree(ctx->revs, tree);
405
0
    } else if (commit->object.parsed) {
406
0
      die(_("unable to load root tree for commit %s"),
407
0
            oid_to_hex(&commit->object.oid));
408
0
    }
409
410
0
    if (r & LOFR_MARK_SEEN)
411
0
      commit->object.flags |= SEEN;
412
0
    if (r & LOFR_DO_SHOW)
413
0
      show_commit(ctx, commit);
414
415
0
    if (ctx->revs->tree_blobs_in_commit_order)
416
      /*
417
       * NEEDSWORK: Adding the tree and then flushing it here
418
       * needs a reallocation for each commit. Can we pass the
419
       * tree directory without allocation churn?
420
       */
421
0
      traverse_non_commits(ctx, &csp);
422
0
  }
423
0
  traverse_non_commits(ctx, &csp);
424
0
  strbuf_release(&csp);
425
0
}
426
427
void traverse_commit_list_filtered(
428
  struct rev_info *revs,
429
  show_commit_fn show_commit,
430
  show_object_fn show_object,
431
  void *show_data,
432
  struct oidset *omitted)
433
0
{
434
0
  struct traversal_context ctx = {
435
0
    .revs = revs,
436
0
    .show_object = show_object,
437
0
    .show_commit = show_commit,
438
0
    .show_data = show_data,
439
0
  };
440
441
0
  if (revs->filter.choice)
442
0
    ctx.filter = list_objects_filter__init(omitted, &revs->filter);
443
444
0
  do_traverse(&ctx);
445
446
0
  if (ctx.filter)
447
0
    list_objects_filter__free(ctx.filter);
448
0
}