Coverage Report

Created: 2025-12-31 07:01

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