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