THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
11KB

  1. #include "cache.h"
  2. #include "tag.h"
  3. #include "commit.h"
  4. #include "tree.h"
  5. #include "blob.h"
  6. #include "diff.h"
  7. #include "tree-walk.h"
  8. #include "revision.h"
  9. #include "list-objects.h"
  10. #include "list-objects-filter.h"
  11. #include "list-objects-filter-options.h"
  12. #include "packfile.h"
  13. #include "object-store.h"
  14. #include "trace.h"
  15. struct traversal_context {
  16. struct rev_info *revs;
  17. show_object_fn show_object;
  18. show_commit_fn show_commit;
  19. void *show_data;
  20. struct filter *filter;
  21. };
  22. static void process_blob(struct traversal_context *ctx,
  23. struct blob *blob,
  24. struct strbuf *path,
  25. const char *name)
  26. {
  27. struct object *obj = &blob->object;
  28. size_t pathlen;
  29. enum list_objects_filter_result r;
  30. if (!ctx->revs->blob_objects)
  31. return;
  32. if (!obj)
  33. die("bad blob object");
  34. if (obj->flags & (UNINTERESTING | SEEN))
  35. return;
  36. /*
  37. * Pre-filter known-missing objects when explicitly requested.
  38. * Otherwise, a missing object error message may be reported
  39. * later (depending on other filtering criteria).
  40. *
  41. * Note that this "--exclude-promisor-objects" pre-filtering
  42. * may cause the actual filter to report an incomplete list
  43. * of missing objects.
  44. */
  45. if (ctx->revs->exclude_promisor_objects &&
  46. !has_object_file(&obj->oid) &&
  47. is_promisor_object(&obj->oid))
  48. return;
  49. pathlen = path->len;
  50. strbuf_addstr(path, name);
  51. r = list_objects_filter__filter_object(ctx->revs->repo,
  52. LOFS_BLOB, obj,
  53. path->buf, &path->buf[pathlen],
  54. ctx->filter);
  55. if (r & LOFR_MARK_SEEN)
  56. obj->flags |= SEEN;
  57. if (r & LOFR_DO_SHOW)
  58. ctx->show_object(obj, path->buf, ctx->show_data);
  59. strbuf_setlen(path, pathlen);
  60. }
  61. /*
  62. * Processing a gitlink entry currently does nothing, since
  63. * we do not recurse into the subproject.
  64. *
  65. * We *could* eventually add a flag that actually does that,
  66. * which would involve:
  67. * - is the subproject actually checked out?
  68. * - if so, see if the subproject has already been added
  69. * to the alternates list, and add it if not.
  70. * - process the commit (or tag) the gitlink points to
  71. * recursively.
  72. *
  73. * However, it's unclear whether there is really ever any
  74. * reason to see superprojects and subprojects as such a
  75. * "unified" object pool (potentially resulting in a totally
  76. * humongous pack - avoiding which was the whole point of
  77. * having gitlinks in the first place!).
  78. *
  79. * So for now, there is just a note that we *could* follow
  80. * the link, and how to do it. Whether it necessarily makes
  81. * any sense what-so-ever to ever do that is another issue.
  82. */
  83. static void process_gitlink(struct traversal_context *ctx,
  84. const unsigned char *sha1,
  85. struct strbuf *path,
  86. const char *name)
  87. {
  88. /* Nothing to do */
  89. }
  90. static void process_tree(struct traversal_context *ctx,
  91. struct tree *tree,
  92. struct strbuf *base,
  93. const char *name);
  94. static void process_tree_contents(struct traversal_context *ctx,
  95. struct tree *tree,
  96. struct strbuf *base)
  97. {
  98. struct tree_desc desc;
  99. struct name_entry entry;
  100. enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
  101. all_entries_interesting : entry_not_interesting;
  102. init_tree_desc(&desc, tree->buffer, tree->size);
  103. while (tree_entry(&desc, &entry)) {
  104. if (match != all_entries_interesting) {
  105. match = tree_entry_interesting(ctx->revs->repo->index,
  106. &entry, base, 0,
  107. &ctx->revs->diffopt.pathspec);
  108. if (match == all_entries_not_interesting)
  109. break;
  110. if (match == entry_not_interesting)
  111. continue;
  112. }
  113. if (S_ISDIR(entry.mode)) {
  114. struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
  115. if (!t) {
  116. die(_("entry '%s' in tree %s has tree mode, "
  117. "but is not a tree"),
  118. entry.path, oid_to_hex(&tree->object.oid));
  119. }
  120. t->object.flags |= NOT_USER_GIVEN;
  121. process_tree(ctx, t, base, entry.path);
  122. }
  123. else if (S_ISGITLINK(entry.mode))
  124. process_gitlink(ctx, entry.oid.hash,
  125. base, entry.path);
  126. else {
  127. struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
  128. if (!b) {
  129. die(_("entry '%s' in tree %s has blob mode, "
  130. "but is not a blob"),
  131. entry.path, oid_to_hex(&tree->object.oid));
  132. }
  133. b->object.flags |= NOT_USER_GIVEN;
  134. process_blob(ctx, b, base, entry.path);
  135. }
  136. }
  137. }
  138. static void process_tree(struct traversal_context *ctx,
  139. struct tree *tree,
  140. struct strbuf *base,
  141. const char *name)
  142. {
  143. struct object *obj = &tree->object;
  144. struct rev_info *revs = ctx->revs;
  145. int baselen = base->len;
  146. enum list_objects_filter_result r;
  147. int failed_parse;
  148. if (!revs->tree_objects)
  149. return;
  150. if (!obj)
  151. die("bad tree object");
  152. if (obj->flags & (UNINTERESTING | SEEN))
  153. return;
  154. failed_parse = parse_tree_gently(tree, 1);
  155. if (failed_parse) {
  156. if (revs->ignore_missing_links)
  157. return;
  158. /*
  159. * Pre-filter known-missing tree objects when explicitly
  160. * requested. This may cause the actual filter to report
  161. * an incomplete list of missing objects.
  162. */
  163. if (revs->exclude_promisor_objects &&
  164. is_promisor_object(&obj->oid))
  165. return;
  166. if (!revs->do_not_die_on_missing_tree)
  167. die("bad tree object %s", oid_to_hex(&obj->oid));
  168. }
  169. strbuf_addstr(base, name);
  170. r = list_objects_filter__filter_object(ctx->revs->repo,
  171. LOFS_BEGIN_TREE, obj,
  172. base->buf, &base->buf[baselen],
  173. ctx->filter);
  174. if (r & LOFR_MARK_SEEN)
  175. obj->flags |= SEEN;
  176. if (r & LOFR_DO_SHOW)
  177. ctx->show_object(obj, base->buf, ctx->show_data);
  178. if (base->len)
  179. strbuf_addch(base, '/');
  180. if (r & LOFR_SKIP_TREE)
  181. trace_printf("Skipping contents of tree %s...\n", base->buf);
  182. else if (!failed_parse)
  183. process_tree_contents(ctx, tree, base);
  184. r = list_objects_filter__filter_object(ctx->revs->repo,
  185. LOFS_END_TREE, obj,
  186. base->buf, &base->buf[baselen],
  187. ctx->filter);
  188. if (r & LOFR_MARK_SEEN)
  189. obj->flags |= SEEN;
  190. if (r & LOFR_DO_SHOW)
  191. ctx->show_object(obj, base->buf, ctx->show_data);
  192. strbuf_setlen(base, baselen);
  193. free_tree_buffer(tree);
  194. }
  195. static void mark_edge_parents_uninteresting(struct commit *commit,
  196. struct rev_info *revs,
  197. show_edge_fn show_edge)
  198. {
  199. struct commit_list *parents;
  200. for (parents = commit->parents; parents; parents = parents->next) {
  201. struct commit *parent = parents->item;
  202. if (!(parent->object.flags & UNINTERESTING))
  203. continue;
  204. mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
  205. if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
  206. parent->object.flags |= SHOWN;
  207. show_edge(parent);
  208. }
  209. }
  210. }
  211. static void add_edge_parents(struct commit *commit,
  212. struct rev_info *revs,
  213. show_edge_fn show_edge,
  214. struct oidset *set)
  215. {
  216. struct commit_list *parents;
  217. for (parents = commit->parents; parents; parents = parents->next) {
  218. struct commit *parent = parents->item;
  219. struct tree *tree = get_commit_tree(parent);
  220. if (!tree)
  221. continue;
  222. oidset_insert(set, &tree->object.oid);
  223. if (!(parent->object.flags & UNINTERESTING))
  224. continue;
  225. tree->object.flags |= UNINTERESTING;
  226. if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
  227. parent->object.flags |= SHOWN;
  228. show_edge(parent);
  229. }
  230. }
  231. }
  232. void mark_edges_uninteresting(struct rev_info *revs,
  233. show_edge_fn show_edge,
  234. int sparse)
  235. {
  236. struct commit_list *list;
  237. int i;
  238. if (sparse) {
  239. struct oidset set;
  240. oidset_init(&set, 16);
  241. for (list = revs->commits; list; list = list->next) {
  242. struct commit *commit = list->item;
  243. struct tree *tree = get_commit_tree(commit);
  244. if (commit->object.flags & UNINTERESTING)
  245. tree->object.flags |= UNINTERESTING;
  246. oidset_insert(&set, &tree->object.oid);
  247. add_edge_parents(commit, revs, show_edge, &set);
  248. }
  249. mark_trees_uninteresting_sparse(revs->repo, &set);
  250. oidset_clear(&set);
  251. } else {
  252. for (list = revs->commits; list; list = list->next) {
  253. struct commit *commit = list->item;
  254. if (commit->object.flags & UNINTERESTING) {
  255. mark_tree_uninteresting(revs->repo,
  256. get_commit_tree(commit));
  257. if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
  258. commit->object.flags |= SHOWN;
  259. show_edge(commit);
  260. }
  261. continue;
  262. }
  263. mark_edge_parents_uninteresting(commit, revs, show_edge);
  264. }
  265. }
  266. if (revs->edge_hint_aggressive) {
  267. for (i = 0; i < revs->cmdline.nr; i++) {
  268. struct object *obj = revs->cmdline.rev[i].item;
  269. struct commit *commit = (struct commit *)obj;
  270. if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
  271. continue;
  272. mark_tree_uninteresting(revs->repo,
  273. get_commit_tree(commit));
  274. if (!(obj->flags & SHOWN)) {
  275. obj->flags |= SHOWN;
  276. show_edge(commit);
  277. }
  278. }
  279. }
  280. }
  281. static void add_pending_tree(struct rev_info *revs, struct tree *tree)
  282. {
  283. add_pending_object(revs, &tree->object, "");
  284. }
  285. static void traverse_trees_and_blobs(struct traversal_context *ctx,
  286. struct strbuf *base)
  287. {
  288. int i;
  289. assert(base->len == 0);
  290. for (i = 0; i < ctx->revs->pending.nr; i++) {
  291. struct object_array_entry *pending = ctx->revs->pending.objects + i;
  292. struct object *obj = pending->item;
  293. const char *name = pending->name;
  294. const char *path = pending->path;
  295. if (obj->flags & (UNINTERESTING | SEEN))
  296. continue;
  297. if (obj->type == OBJ_TAG) {
  298. obj->flags |= SEEN;
  299. ctx->show_object(obj, name, ctx->show_data);
  300. continue;
  301. }
  302. if (!path)
  303. path = "";
  304. if (obj->type == OBJ_TREE) {
  305. process_tree(ctx, (struct tree *)obj, base, path);
  306. continue;
  307. }
  308. if (obj->type == OBJ_BLOB) {
  309. process_blob(ctx, (struct blob *)obj, base, path);
  310. continue;
  311. }
  312. die("unknown pending object %s (%s)",
  313. oid_to_hex(&obj->oid), name);
  314. }
  315. object_array_clear(&ctx->revs->pending);
  316. }
  317. static void do_traverse(struct traversal_context *ctx)
  318. {
  319. struct commit *commit;
  320. struct strbuf csp; /* callee's scratch pad */
  321. strbuf_init(&csp, PATH_MAX);
  322. while ((commit = get_revision(ctx->revs)) != NULL) {
  323. /*
  324. * an uninteresting boundary commit may not have its tree
  325. * parsed yet, but we are not going to show them anyway
  326. */
  327. if (!ctx->revs->tree_objects)
  328. ; /* do not bother loading tree */
  329. else if (get_commit_tree(commit)) {
  330. struct tree *tree = get_commit_tree(commit);
  331. tree->object.flags |= NOT_USER_GIVEN;
  332. add_pending_tree(ctx->revs, tree);
  333. } else if (commit->object.parsed) {
  334. die(_("unable to load root tree for commit %s"),
  335. oid_to_hex(&commit->object.oid));
  336. }
  337. ctx->show_commit(commit, ctx->show_data);
  338. if (ctx->revs->tree_blobs_in_commit_order)
  339. /*
  340. * NEEDSWORK: Adding the tree and then flushing it here
  341. * needs a reallocation for each commit. Can we pass the
  342. * tree directory without allocation churn?
  343. */
  344. traverse_trees_and_blobs(ctx, &csp);
  345. }
  346. traverse_trees_and_blobs(ctx, &csp);
  347. strbuf_release(&csp);
  348. }
  349. void traverse_commit_list(struct rev_info *revs,
  350. show_commit_fn show_commit,
  351. show_object_fn show_object,
  352. void *show_data)
  353. {
  354. struct traversal_context ctx;
  355. ctx.revs = revs;
  356. ctx.show_commit = show_commit;
  357. ctx.show_object = show_object;
  358. ctx.show_data = show_data;
  359. ctx.filter = NULL;
  360. do_traverse(&ctx);
  361. }
  362. void traverse_commit_list_filtered(
  363. struct list_objects_filter_options *filter_options,
  364. struct rev_info *revs,
  365. show_commit_fn show_commit,
  366. show_object_fn show_object,
  367. void *show_data,
  368. struct oidset *omitted)
  369. {
  370. struct traversal_context ctx;
  371. ctx.revs = revs;
  372. ctx.show_object = show_object;
  373. ctx.show_commit = show_commit;
  374. ctx.show_data = show_data;
  375. ctx.filter = list_objects_filter__init(omitted, filter_options);
  376. do_traverse(&ctx);
  377. list_objects_filter__free(ctx.filter);
  378. }