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.

3975 lines
108KB

  1. #include "cache.h"
  2. #include "object-store.h"
  3. #include "tag.h"
  4. #include "blob.h"
  5. #include "tree.h"
  6. #include "commit.h"
  7. #include "diff.h"
  8. #include "refs.h"
  9. #include "revision.h"
  10. #include "repository.h"
  11. #include "graph.h"
  12. #include "grep.h"
  13. #include "reflog-walk.h"
  14. #include "patch-ids.h"
  15. #include "decorate.h"
  16. #include "log-tree.h"
  17. #include "string-list.h"
  18. #include "line-log.h"
  19. #include "mailmap.h"
  20. #include "commit-slab.h"
  21. #include "dir.h"
  22. #include "cache-tree.h"
  23. #include "bisect.h"
  24. #include "packfile.h"
  25. #include "worktree.h"
  26. #include "argv-array.h"
  27. #include "commit-reach.h"
  28. #include "commit-graph.h"
  29. #include "prio-queue.h"
  30. #include "hashmap.h"
  31. #include "utf8.h"
  32. volatile show_early_output_fn_t show_early_output;
  33. static const char *term_bad;
  34. static const char *term_good;
  35. implement_shared_commit_slab(revision_sources, char *);
  36. void show_object_with_name(FILE *out, struct object *obj, const char *name)
  37. {
  38. const char *p;
  39. fprintf(out, "%s ", oid_to_hex(&obj->oid));
  40. for (p = name; *p && *p != '\n'; p++)
  41. fputc(*p, out);
  42. fputc('\n', out);
  43. }
  44. static void mark_blob_uninteresting(struct blob *blob)
  45. {
  46. if (!blob)
  47. return;
  48. if (blob->object.flags & UNINTERESTING)
  49. return;
  50. blob->object.flags |= UNINTERESTING;
  51. }
  52. static void mark_tree_contents_uninteresting(struct repository *r,
  53. struct tree *tree)
  54. {
  55. struct tree_desc desc;
  56. struct name_entry entry;
  57. if (parse_tree_gently(tree, 1) < 0)
  58. return;
  59. init_tree_desc(&desc, tree->buffer, tree->size);
  60. while (tree_entry(&desc, &entry)) {
  61. switch (object_type(entry.mode)) {
  62. case OBJ_TREE:
  63. mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
  64. break;
  65. case OBJ_BLOB:
  66. mark_blob_uninteresting(lookup_blob(r, &entry.oid));
  67. break;
  68. default:
  69. /* Subproject commit - not in this repository */
  70. break;
  71. }
  72. }
  73. /*
  74. * We don't care about the tree any more
  75. * after it has been marked uninteresting.
  76. */
  77. free_tree_buffer(tree);
  78. }
  79. void mark_tree_uninteresting(struct repository *r, struct tree *tree)
  80. {
  81. struct object *obj;
  82. if (!tree)
  83. return;
  84. obj = &tree->object;
  85. if (obj->flags & UNINTERESTING)
  86. return;
  87. obj->flags |= UNINTERESTING;
  88. mark_tree_contents_uninteresting(r, tree);
  89. }
  90. struct path_and_oids_entry {
  91. struct hashmap_entry ent;
  92. char *path;
  93. struct oidset trees;
  94. };
  95. static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
  96. const struct hashmap_entry *eptr,
  97. const struct hashmap_entry *entry_or_key,
  98. const void *keydata)
  99. {
  100. const struct path_and_oids_entry *e1, *e2;
  101. e1 = container_of(eptr, const struct path_and_oids_entry, ent);
  102. e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
  103. return strcmp(e1->path, e2->path);
  104. }
  105. static void paths_and_oids_init(struct hashmap *map)
  106. {
  107. hashmap_init(map, path_and_oids_cmp, NULL, 0);
  108. }
  109. static void paths_and_oids_clear(struct hashmap *map)
  110. {
  111. struct hashmap_iter iter;
  112. struct path_and_oids_entry *entry;
  113. hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
  114. oidset_clear(&entry->trees);
  115. free(entry->path);
  116. }
  117. hashmap_free_entries(map, struct path_and_oids_entry, ent);
  118. }
  119. static void paths_and_oids_insert(struct hashmap *map,
  120. const char *path,
  121. const struct object_id *oid)
  122. {
  123. int hash = strhash(path);
  124. struct path_and_oids_entry key;
  125. struct path_and_oids_entry *entry;
  126. hashmap_entry_init(&key.ent, hash);
  127. /* use a shallow copy for the lookup */
  128. key.path = (char *)path;
  129. oidset_init(&key.trees, 0);
  130. entry = hashmap_get_entry(map, &key, ent, NULL);
  131. if (!entry) {
  132. entry = xcalloc(1, sizeof(struct path_and_oids_entry));
  133. hashmap_entry_init(&entry->ent, hash);
  134. entry->path = xstrdup(key.path);
  135. oidset_init(&entry->trees, 16);
  136. hashmap_put(map, &entry->ent);
  137. }
  138. oidset_insert(&entry->trees, oid);
  139. }
  140. static void add_children_by_path(struct repository *r,
  141. struct tree *tree,
  142. struct hashmap *map)
  143. {
  144. struct tree_desc desc;
  145. struct name_entry entry;
  146. if (!tree)
  147. return;
  148. if (parse_tree_gently(tree, 1) < 0)
  149. return;
  150. init_tree_desc(&desc, tree->buffer, tree->size);
  151. while (tree_entry(&desc, &entry)) {
  152. switch (object_type(entry.mode)) {
  153. case OBJ_TREE:
  154. paths_and_oids_insert(map, entry.path, &entry.oid);
  155. if (tree->object.flags & UNINTERESTING) {
  156. struct tree *child = lookup_tree(r, &entry.oid);
  157. if (child)
  158. child->object.flags |= UNINTERESTING;
  159. }
  160. break;
  161. case OBJ_BLOB:
  162. if (tree->object.flags & UNINTERESTING) {
  163. struct blob *child = lookup_blob(r, &entry.oid);
  164. if (child)
  165. child->object.flags |= UNINTERESTING;
  166. }
  167. break;
  168. default:
  169. /* Subproject commit - not in this repository */
  170. break;
  171. }
  172. }
  173. free_tree_buffer(tree);
  174. }
  175. void mark_trees_uninteresting_sparse(struct repository *r,
  176. struct oidset *trees)
  177. {
  178. unsigned has_interesting = 0, has_uninteresting = 0;
  179. struct hashmap map;
  180. struct hashmap_iter map_iter;
  181. struct path_and_oids_entry *entry;
  182. struct object_id *oid;
  183. struct oidset_iter iter;
  184. oidset_iter_init(trees, &iter);
  185. while ((!has_interesting || !has_uninteresting) &&
  186. (oid = oidset_iter_next(&iter))) {
  187. struct tree *tree = lookup_tree(r, oid);
  188. if (!tree)
  189. continue;
  190. if (tree->object.flags & UNINTERESTING)
  191. has_uninteresting = 1;
  192. else
  193. has_interesting = 1;
  194. }
  195. /* Do not walk unless we have both types of trees. */
  196. if (!has_uninteresting || !has_interesting)
  197. return;
  198. paths_and_oids_init(&map);
  199. oidset_iter_init(trees, &iter);
  200. while ((oid = oidset_iter_next(&iter))) {
  201. struct tree *tree = lookup_tree(r, oid);
  202. add_children_by_path(r, tree, &map);
  203. }
  204. hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
  205. mark_trees_uninteresting_sparse(r, &entry->trees);
  206. paths_and_oids_clear(&map);
  207. }
  208. struct commit_stack {
  209. struct commit **items;
  210. size_t nr, alloc;
  211. };
  212. #define COMMIT_STACK_INIT { NULL, 0, 0 }
  213. static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
  214. {
  215. ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
  216. stack->items[stack->nr++] = commit;
  217. }
  218. static struct commit *commit_stack_pop(struct commit_stack *stack)
  219. {
  220. return stack->nr ? stack->items[--stack->nr] : NULL;
  221. }
  222. static void commit_stack_clear(struct commit_stack *stack)
  223. {
  224. FREE_AND_NULL(stack->items);
  225. stack->nr = stack->alloc = 0;
  226. }
  227. static void mark_one_parent_uninteresting(struct commit *commit,
  228. struct commit_stack *pending)
  229. {
  230. struct commit_list *l;
  231. if (commit->object.flags & UNINTERESTING)
  232. return;
  233. commit->object.flags |= UNINTERESTING;
  234. /*
  235. * Normally we haven't parsed the parent
  236. * yet, so we won't have a parent of a parent
  237. * here. However, it may turn out that we've
  238. * reached this commit some other way (where it
  239. * wasn't uninteresting), in which case we need
  240. * to mark its parents recursively too..
  241. */
  242. for (l = commit->parents; l; l = l->next)
  243. commit_stack_push(pending, l->item);
  244. }
  245. void mark_parents_uninteresting(struct commit *commit)
  246. {
  247. struct commit_stack pending = COMMIT_STACK_INIT;
  248. struct commit_list *l;
  249. for (l = commit->parents; l; l = l->next)
  250. mark_one_parent_uninteresting(l->item, &pending);
  251. while (pending.nr > 0)
  252. mark_one_parent_uninteresting(commit_stack_pop(&pending),
  253. &pending);
  254. commit_stack_clear(&pending);
  255. }
  256. static void add_pending_object_with_path(struct rev_info *revs,
  257. struct object *obj,
  258. const char *name, unsigned mode,
  259. const char *path)
  260. {
  261. if (!obj)
  262. return;
  263. if (revs->no_walk && (obj->flags & UNINTERESTING))
  264. revs->no_walk = 0;
  265. if (revs->reflog_info && obj->type == OBJ_COMMIT) {
  266. struct strbuf buf = STRBUF_INIT;
  267. int len = interpret_branch_name(name, 0, &buf, 0);
  268. if (0 < len && name[len] && buf.len)
  269. strbuf_addstr(&buf, name + len);
  270. add_reflog_for_walk(revs->reflog_info,
  271. (struct commit *)obj,
  272. buf.buf[0] ? buf.buf: name);
  273. strbuf_release(&buf);
  274. return; /* do not add the commit itself */
  275. }
  276. add_object_array_with_path(obj, name, &revs->pending, mode, path);
  277. }
  278. static void add_pending_object_with_mode(struct rev_info *revs,
  279. struct object *obj,
  280. const char *name, unsigned mode)
  281. {
  282. add_pending_object_with_path(revs, obj, name, mode, NULL);
  283. }
  284. void add_pending_object(struct rev_info *revs,
  285. struct object *obj, const char *name)
  286. {
  287. add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
  288. }
  289. void add_head_to_pending(struct rev_info *revs)
  290. {
  291. struct object_id oid;
  292. struct object *obj;
  293. if (get_oid("HEAD", &oid))
  294. return;
  295. obj = parse_object(revs->repo, &oid);
  296. if (!obj)
  297. return;
  298. add_pending_object(revs, obj, "HEAD");
  299. }
  300. static struct object *get_reference(struct rev_info *revs, const char *name,
  301. const struct object_id *oid,
  302. unsigned int flags)
  303. {
  304. struct object *object;
  305. /*
  306. * If the repository has commit graphs, repo_parse_commit() avoids
  307. * reading the object buffer, so use it whenever possible.
  308. */
  309. if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
  310. struct commit *c = lookup_commit(revs->repo, oid);
  311. if (!repo_parse_commit(revs->repo, c))
  312. object = (struct object *) c;
  313. else
  314. object = NULL;
  315. } else {
  316. object = parse_object(revs->repo, oid);
  317. }
  318. if (!object) {
  319. if (revs->ignore_missing)
  320. return object;
  321. if (revs->exclude_promisor_objects && is_promisor_object(oid))
  322. return NULL;
  323. die("bad object %s", name);
  324. }
  325. object->flags |= flags;
  326. return object;
  327. }
  328. void add_pending_oid(struct rev_info *revs, const char *name,
  329. const struct object_id *oid, unsigned int flags)
  330. {
  331. struct object *object = get_reference(revs, name, oid, flags);
  332. add_pending_object(revs, object, name);
  333. }
  334. static struct commit *handle_commit(struct rev_info *revs,
  335. struct object_array_entry *entry)
  336. {
  337. struct object *object = entry->item;
  338. const char *name = entry->name;
  339. const char *path = entry->path;
  340. unsigned int mode = entry->mode;
  341. unsigned long flags = object->flags;
  342. /*
  343. * Tag object? Look what it points to..
  344. */
  345. while (object->type == OBJ_TAG) {
  346. struct tag *tag = (struct tag *) object;
  347. if (revs->tag_objects && !(flags & UNINTERESTING))
  348. add_pending_object(revs, object, tag->tag);
  349. object = parse_object(revs->repo, get_tagged_oid(tag));
  350. if (!object) {
  351. if (revs->ignore_missing_links || (flags & UNINTERESTING))
  352. return NULL;
  353. if (revs->exclude_promisor_objects &&
  354. is_promisor_object(&tag->tagged->oid))
  355. return NULL;
  356. die("bad object %s", oid_to_hex(&tag->tagged->oid));
  357. }
  358. object->flags |= flags;
  359. /*
  360. * We'll handle the tagged object by looping or dropping
  361. * through to the non-tag handlers below. Do not
  362. * propagate path data from the tag's pending entry.
  363. */
  364. path = NULL;
  365. mode = 0;
  366. }
  367. /*
  368. * Commit object? Just return it, we'll do all the complex
  369. * reachability crud.
  370. */
  371. if (object->type == OBJ_COMMIT) {
  372. struct commit *commit = (struct commit *)object;
  373. if (parse_commit(commit) < 0)
  374. die("unable to parse commit %s", name);
  375. if (flags & UNINTERESTING) {
  376. mark_parents_uninteresting(commit);
  377. if (!revs->topo_order || !generation_numbers_enabled(the_repository))
  378. revs->limited = 1;
  379. }
  380. if (revs->sources) {
  381. char **slot = revision_sources_at(revs->sources, commit);
  382. if (!*slot)
  383. *slot = xstrdup(name);
  384. }
  385. return commit;
  386. }
  387. /*
  388. * Tree object? Either mark it uninteresting, or add it
  389. * to the list of objects to look at later..
  390. */
  391. if (object->type == OBJ_TREE) {
  392. struct tree *tree = (struct tree *)object;
  393. if (!revs->tree_objects)
  394. return NULL;
  395. if (flags & UNINTERESTING) {
  396. mark_tree_contents_uninteresting(revs->repo, tree);
  397. return NULL;
  398. }
  399. add_pending_object_with_path(revs, object, name, mode, path);
  400. return NULL;
  401. }
  402. /*
  403. * Blob object? You know the drill by now..
  404. */
  405. if (object->type == OBJ_BLOB) {
  406. if (!revs->blob_objects)
  407. return NULL;
  408. if (flags & UNINTERESTING)
  409. return NULL;
  410. add_pending_object_with_path(revs, object, name, mode, path);
  411. return NULL;
  412. }
  413. die("%s is unknown object", name);
  414. }
  415. static int everybody_uninteresting(struct commit_list *orig,
  416. struct commit **interesting_cache)
  417. {
  418. struct commit_list *list = orig;
  419. if (*interesting_cache) {
  420. struct commit *commit = *interesting_cache;
  421. if (!(commit->object.flags & UNINTERESTING))
  422. return 0;
  423. }
  424. while (list) {
  425. struct commit *commit = list->item;
  426. list = list->next;
  427. if (commit->object.flags & UNINTERESTING)
  428. continue;
  429. *interesting_cache = commit;
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. /*
  435. * A definition of "relevant" commit that we can use to simplify limited graphs
  436. * by eliminating side branches.
  437. *
  438. * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
  439. * in our list), or that is a specified BOTTOM commit. Then after computing
  440. * a limited list, during processing we can generally ignore boundary merges
  441. * coming from outside the graph, (ie from irrelevant parents), and treat
  442. * those merges as if they were single-parent. TREESAME is defined to consider
  443. * only relevant parents, if any. If we are TREESAME to our on-graph parents,
  444. * we don't care if we were !TREESAME to non-graph parents.
  445. *
  446. * Treating bottom commits as relevant ensures that a limited graph's
  447. * connection to the actual bottom commit is not viewed as a side branch, but
  448. * treated as part of the graph. For example:
  449. *
  450. * ....Z...A---X---o---o---B
  451. * . /
  452. * W---Y
  453. *
  454. * When computing "A..B", the A-X connection is at least as important as
  455. * Y-X, despite A being flagged UNINTERESTING.
  456. *
  457. * And when computing --ancestry-path "A..B", the A-X connection is more
  458. * important than Y-X, despite both A and Y being flagged UNINTERESTING.
  459. */
  460. static inline int relevant_commit(struct commit *commit)
  461. {
  462. return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
  463. }
  464. /*
  465. * Return a single relevant commit from a parent list. If we are a TREESAME
  466. * commit, and this selects one of our parents, then we can safely simplify to
  467. * that parent.
  468. */
  469. static struct commit *one_relevant_parent(const struct rev_info *revs,
  470. struct commit_list *orig)
  471. {
  472. struct commit_list *list = orig;
  473. struct commit *relevant = NULL;
  474. if (!orig)
  475. return NULL;
  476. /*
  477. * For 1-parent commits, or if first-parent-only, then return that
  478. * first parent (even if not "relevant" by the above definition).
  479. * TREESAME will have been set purely on that parent.
  480. */
  481. if (revs->first_parent_only || !orig->next)
  482. return orig->item;
  483. /*
  484. * For multi-parent commits, identify a sole relevant parent, if any.
  485. * If we have only one relevant parent, then TREESAME will be set purely
  486. * with regard to that parent, and we can simplify accordingly.
  487. *
  488. * If we have more than one relevant parent, or no relevant parents
  489. * (and multiple irrelevant ones), then we can't select a parent here
  490. * and return NULL.
  491. */
  492. while (list) {
  493. struct commit *commit = list->item;
  494. list = list->next;
  495. if (relevant_commit(commit)) {
  496. if (relevant)
  497. return NULL;
  498. relevant = commit;
  499. }
  500. }
  501. return relevant;
  502. }
  503. /*
  504. * The goal is to get REV_TREE_NEW as the result only if the
  505. * diff consists of all '+' (and no other changes), REV_TREE_OLD
  506. * if the whole diff is removal of old data, and otherwise
  507. * REV_TREE_DIFFERENT (of course if the trees are the same we
  508. * want REV_TREE_SAME).
  509. *
  510. * The only time we care about the distinction is when
  511. * remove_empty_trees is in effect, in which case we care only about
  512. * whether the whole change is REV_TREE_NEW, or if there's another type
  513. * of change. Which means we can stop the diff early in either of these
  514. * cases:
  515. *
  516. * 1. We're not using remove_empty_trees at all.
  517. *
  518. * 2. We saw anything except REV_TREE_NEW.
  519. */
  520. static int tree_difference = REV_TREE_SAME;
  521. static void file_add_remove(struct diff_options *options,
  522. int addremove, unsigned mode,
  523. const struct object_id *oid,
  524. int oid_valid,
  525. const char *fullpath, unsigned dirty_submodule)
  526. {
  527. int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
  528. struct rev_info *revs = options->change_fn_data;
  529. tree_difference |= diff;
  530. if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
  531. options->flags.has_changes = 1;
  532. }
  533. static void file_change(struct diff_options *options,
  534. unsigned old_mode, unsigned new_mode,
  535. const struct object_id *old_oid,
  536. const struct object_id *new_oid,
  537. int old_oid_valid, int new_oid_valid,
  538. const char *fullpath,
  539. unsigned old_dirty_submodule, unsigned new_dirty_submodule)
  540. {
  541. tree_difference = REV_TREE_DIFFERENT;
  542. options->flags.has_changes = 1;
  543. }
  544. static int rev_compare_tree(struct rev_info *revs,
  545. struct commit *parent, struct commit *commit)
  546. {
  547. struct tree *t1 = get_commit_tree(parent);
  548. struct tree *t2 = get_commit_tree(commit);
  549. if (!t1)
  550. return REV_TREE_NEW;
  551. if (!t2)
  552. return REV_TREE_OLD;
  553. if (revs->simplify_by_decoration) {
  554. /*
  555. * If we are simplifying by decoration, then the commit
  556. * is worth showing if it has a tag pointing at it.
  557. */
  558. if (get_name_decoration(&commit->object))
  559. return REV_TREE_DIFFERENT;
  560. /*
  561. * A commit that is not pointed by a tag is uninteresting
  562. * if we are not limited by path. This means that you will
  563. * see the usual "commits that touch the paths" plus any
  564. * tagged commit by specifying both --simplify-by-decoration
  565. * and pathspec.
  566. */
  567. if (!revs->prune_data.nr)
  568. return REV_TREE_SAME;
  569. }
  570. tree_difference = REV_TREE_SAME;
  571. revs->pruning.flags.has_changes = 0;
  572. if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
  573. &revs->pruning) < 0)
  574. return REV_TREE_DIFFERENT;
  575. return tree_difference;
  576. }
  577. static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
  578. {
  579. int retval;
  580. struct tree *t1 = get_commit_tree(commit);
  581. if (!t1)
  582. return 0;
  583. tree_difference = REV_TREE_SAME;
  584. revs->pruning.flags.has_changes = 0;
  585. retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
  586. return retval >= 0 && (tree_difference == REV_TREE_SAME);
  587. }
  588. struct treesame_state {
  589. unsigned int nparents;
  590. unsigned char treesame[FLEX_ARRAY];
  591. };
  592. static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
  593. {
  594. unsigned n = commit_list_count(commit->parents);
  595. struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
  596. st->nparents = n;
  597. add_decoration(&revs->treesame, &commit->object, st);
  598. return st;
  599. }
  600. /*
  601. * Must be called immediately after removing the nth_parent from a commit's
  602. * parent list, if we are maintaining the per-parent treesame[] decoration.
  603. * This does not recalculate the master TREESAME flag - update_treesame()
  604. * should be called to update it after a sequence of treesame[] modifications
  605. * that may have affected it.
  606. */
  607. static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
  608. {
  609. struct treesame_state *st;
  610. int old_same;
  611. if (!commit->parents) {
  612. /*
  613. * Have just removed the only parent from a non-merge.
  614. * Different handling, as we lack decoration.
  615. */
  616. if (nth_parent != 0)
  617. die("compact_treesame %u", nth_parent);
  618. old_same = !!(commit->object.flags & TREESAME);
  619. if (rev_same_tree_as_empty(revs, commit))
  620. commit->object.flags |= TREESAME;
  621. else
  622. commit->object.flags &= ~TREESAME;
  623. return old_same;
  624. }
  625. st = lookup_decoration(&revs->treesame, &commit->object);
  626. if (!st || nth_parent >= st->nparents)
  627. die("compact_treesame %u", nth_parent);
  628. old_same = st->treesame[nth_parent];
  629. memmove(st->treesame + nth_parent,
  630. st->treesame + nth_parent + 1,
  631. st->nparents - nth_parent - 1);
  632. /*
  633. * If we've just become a non-merge commit, update TREESAME
  634. * immediately, and remove the no-longer-needed decoration.
  635. * If still a merge, defer update until update_treesame().
  636. */
  637. if (--st->nparents == 1) {
  638. if (commit->parents->next)
  639. die("compact_treesame parents mismatch");
  640. if (st->treesame[0] && revs->dense)
  641. commit->object.flags |= TREESAME;
  642. else
  643. commit->object.flags &= ~TREESAME;
  644. free(add_decoration(&revs->treesame, &commit->object, NULL));
  645. }
  646. return old_same;
  647. }
  648. static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
  649. {
  650. if (commit->parents && commit->parents->next) {
  651. unsigned n;
  652. struct treesame_state *st;
  653. struct commit_list *p;
  654. unsigned relevant_parents;
  655. unsigned relevant_change, irrelevant_change;
  656. st = lookup_decoration(&revs->treesame, &commit->object);
  657. if (!st)
  658. die("update_treesame %s", oid_to_hex(&commit->object.oid));
  659. relevant_parents = 0;
  660. relevant_change = irrelevant_change = 0;
  661. for (p = commit->parents, n = 0; p; n++, p = p->next) {
  662. if (relevant_commit(p->item)) {
  663. relevant_change |= !st->treesame[n];
  664. relevant_parents++;
  665. } else
  666. irrelevant_change |= !st->treesame[n];
  667. }
  668. if (relevant_parents ? relevant_change : irrelevant_change)
  669. commit->object.flags &= ~TREESAME;
  670. else
  671. commit->object.flags |= TREESAME;
  672. }
  673. return commit->object.flags & TREESAME;
  674. }
  675. static inline int limiting_can_increase_treesame(const struct rev_info *revs)
  676. {
  677. /*
  678. * TREESAME is irrelevant unless prune && dense;
  679. * if simplify_history is set, we can't have a mixture of TREESAME and
  680. * !TREESAME INTERESTING parents (and we don't have treesame[]
  681. * decoration anyway);
  682. * if first_parent_only is set, then the TREESAME flag is locked
  683. * against the first parent (and again we lack treesame[] decoration).
  684. */
  685. return revs->prune && revs->dense &&
  686. !revs->simplify_history &&
  687. !revs->first_parent_only;
  688. }
  689. static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
  690. {
  691. struct commit_list **pp, *parent;
  692. struct treesame_state *ts = NULL;
  693. int relevant_change = 0, irrelevant_change = 0;
  694. int relevant_parents, nth_parent;
  695. /*
  696. * If we don't do pruning, everything is interesting
  697. */
  698. if (!revs->prune)
  699. return;
  700. if (!get_commit_tree(commit))
  701. return;
  702. if (!commit->parents) {
  703. if (rev_same_tree_as_empty(revs, commit))
  704. commit->object.flags |= TREESAME;
  705. return;
  706. }
  707. /*
  708. * Normal non-merge commit? If we don't want to make the
  709. * history dense, we consider it always to be a change..
  710. */
  711. if (!revs->dense && !commit->parents->next)
  712. return;
  713. for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
  714. (parent = *pp) != NULL;
  715. pp = &parent->next, nth_parent++) {
  716. struct commit *p = parent->item;
  717. if (relevant_commit(p))
  718. relevant_parents++;
  719. if (nth_parent == 1) {
  720. /*
  721. * This our second loop iteration - so we now know
  722. * we're dealing with a merge.
  723. *
  724. * Do not compare with later parents when we care only about
  725. * the first parent chain, in order to avoid derailing the
  726. * traversal to follow a side branch that brought everything
  727. * in the path we are limited to by the pathspec.
  728. */
  729. if (revs->first_parent_only)
  730. break;
  731. /*
  732. * If this will remain a potentially-simplifiable
  733. * merge, remember per-parent treesame if needed.
  734. * Initialise the array with the comparison from our
  735. * first iteration.
  736. */
  737. if (revs->treesame.name &&
  738. !revs->simplify_history &&
  739. !(commit->object.flags & UNINTERESTING)) {
  740. ts = initialise_treesame(revs, commit);
  741. if (!(irrelevant_change || relevant_change))
  742. ts->treesame[0] = 1;
  743. }
  744. }
  745. if (parse_commit(p) < 0)
  746. die("cannot simplify commit %s (because of %s)",
  747. oid_to_hex(&commit->object.oid),
  748. oid_to_hex(&p->object.oid));
  749. switch (rev_compare_tree(revs, p, commit)) {
  750. case REV_TREE_SAME:
  751. if (!revs->simplify_history || !relevant_commit(p)) {
  752. /* Even if a merge with an uninteresting
  753. * side branch brought the entire change
  754. * we are interested in, we do not want
  755. * to lose the other branches of this
  756. * merge, so we just keep going.
  757. */
  758. if (ts)
  759. ts->treesame[nth_parent] = 1;
  760. continue;
  761. }
  762. parent->next = NULL;
  763. commit->parents = parent;
  764. commit->object.flags |= TREESAME;
  765. return;
  766. case REV_TREE_NEW:
  767. if (revs->remove_empty_trees &&
  768. rev_same_tree_as_empty(revs, p)) {
  769. /* We are adding all the specified
  770. * paths from this parent, so the
  771. * history beyond this parent is not
  772. * interesting. Remove its parents
  773. * (they are grandparents for us).
  774. * IOW, we pretend this parent is a
  775. * "root" commit.
  776. */
  777. if (parse_commit(p) < 0)
  778. die("cannot simplify commit %s (invalid %s)",
  779. oid_to_hex(&commit->object.oid),
  780. oid_to_hex(&p->object.oid));
  781. p->parents = NULL;
  782. }
  783. /* fallthrough */
  784. case REV_TREE_OLD:
  785. case REV_TREE_DIFFERENT:
  786. if (relevant_commit(p))
  787. relevant_change = 1;
  788. else
  789. irrelevant_change = 1;
  790. continue;
  791. }
  792. die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
  793. }
  794. /*
  795. * TREESAME is straightforward for single-parent commits. For merge
  796. * commits, it is most useful to define it so that "irrelevant"
  797. * parents cannot make us !TREESAME - if we have any relevant
  798. * parents, then we only consider TREESAMEness with respect to them,
  799. * allowing irrelevant merges from uninteresting branches to be
  800. * simplified away. Only if we have only irrelevant parents do we
  801. * base TREESAME on them. Note that this logic is replicated in
  802. * update_treesame, which should be kept in sync.
  803. */
  804. if (relevant_parents ? !relevant_change : !irrelevant_change)
  805. commit->object.flags |= TREESAME;
  806. }
  807. static int process_parents(struct rev_info *revs, struct commit *commit,
  808. struct commit_list **list, struct prio_queue *queue)
  809. {
  810. struct commit_list *parent = commit->parents;
  811. unsigned left_flag;
  812. if (commit->object.flags & ADDED)
  813. return 0;
  814. commit->object.flags |= ADDED;
  815. if (revs->include_check &&
  816. !revs->include_check(commit, revs->include_check_data))
  817. return 0;
  818. /*
  819. * If the commit is uninteresting, don't try to
  820. * prune parents - we want the maximal uninteresting
  821. * set.
  822. *
  823. * Normally we haven't parsed the parent
  824. * yet, so we won't have a parent of a parent
  825. * here. However, it may turn out that we've
  826. * reached this commit some other way (where it
  827. * wasn't uninteresting), in which case we need
  828. * to mark its parents recursively too..
  829. */
  830. if (commit->object.flags & UNINTERESTING) {
  831. while (parent) {
  832. struct commit *p = parent->item;
  833. parent = parent->next;
  834. if (p)
  835. p->object.flags |= UNINTERESTING;
  836. if (parse_commit_gently(p, 1) < 0)
  837. continue;
  838. if (p->parents)
  839. mark_parents_uninteresting(p);
  840. if (p->object.flags & SEEN)
  841. continue;
  842. p->object.flags |= SEEN;
  843. if (list)
  844. commit_list_insert_by_date(p, list);
  845. if (queue)
  846. prio_queue_put(queue, p);
  847. }
  848. return 0;
  849. }
  850. /*
  851. * Ok, the commit wasn't uninteresting. Try to
  852. * simplify the commit history and find the parent
  853. * that has no differences in the path set if one exists.
  854. */
  855. try_to_simplify_commit(revs, commit);
  856. if (revs->no_walk)
  857. return 0;
  858. left_flag = (commit->object.flags & SYMMETRIC_LEFT);
  859. for (parent = commit->parents; parent; parent = parent->next) {
  860. struct commit *p = parent->item;
  861. int gently = revs->ignore_missing_links ||
  862. revs->exclude_promisor_objects;
  863. if (parse_commit_gently(p, gently) < 0) {
  864. if (revs->exclude_promisor_objects &&
  865. is_promisor_object(&p->object.oid)) {
  866. if (revs->first_parent_only)
  867. break;
  868. continue;
  869. }
  870. return -1;
  871. }
  872. if (revs->sources) {
  873. char **slot = revision_sources_at(revs->sources, p);
  874. if (!*slot)
  875. *slot = *revision_sources_at(revs->sources, commit);
  876. }
  877. p->object.flags |= left_flag;
  878. if (!(p->object.flags & SEEN)) {
  879. p->object.flags |= SEEN;
  880. if (list)
  881. commit_list_insert_by_date(p, list);
  882. if (queue)
  883. prio_queue_put(queue, p);
  884. }
  885. if (revs->first_parent_only)
  886. break;
  887. }
  888. return 0;
  889. }
  890. static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
  891. {
  892. struct commit_list *p;
  893. int left_count = 0, right_count = 0;
  894. int left_first;
  895. struct patch_ids ids;
  896. unsigned cherry_flag;
  897. /* First count the commits on the left and on the right */
  898. for (p = list; p; p = p->next) {
  899. struct commit *commit = p->item;
  900. unsigned flags = commit->object.flags;
  901. if (flags & BOUNDARY)
  902. ;
  903. else if (flags & SYMMETRIC_LEFT)
  904. left_count++;
  905. else
  906. right_count++;
  907. }
  908. if (!left_count || !right_count)
  909. return;
  910. left_first = left_count < right_count;
  911. init_patch_ids(revs->repo, &ids);
  912. ids.diffopts.pathspec = revs->diffopt.pathspec;
  913. /* Compute patch-ids for one side */
  914. for (p = list; p; p = p->next) {
  915. struct commit *commit = p->item;
  916. unsigned flags = commit->object.flags;
  917. if (flags & BOUNDARY)
  918. continue;
  919. /*
  920. * If we have fewer left, left_first is set and we omit
  921. * commits on the right branch in this loop. If we have
  922. * fewer right, we skip the left ones.
  923. */
  924. if (left_first != !!(flags & SYMMETRIC_LEFT))
  925. continue;
  926. add_commit_patch_id(commit, &ids);
  927. }
  928. /* either cherry_mark or cherry_pick are true */
  929. cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
  930. /* Check the other side */
  931. for (p = list; p; p = p->next) {
  932. struct commit *commit = p->item;
  933. struct patch_id *id;
  934. unsigned flags = commit->object.flags;
  935. if (flags & BOUNDARY)
  936. continue;
  937. /*
  938. * If we have fewer left, left_first is set and we omit
  939. * commits on the left branch in this loop.
  940. */
  941. if (left_first == !!(flags & SYMMETRIC_LEFT))
  942. continue;
  943. /*
  944. * Have we seen the same patch id?
  945. */
  946. id = has_commit_patch_id(commit, &ids);
  947. if (!id)
  948. continue;
  949. commit->object.flags |= cherry_flag;
  950. id->commit->object.flags |= cherry_flag;
  951. }
  952. free_patch_ids(&ids);
  953. }
  954. /* How many extra uninteresting commits we want to see.. */
  955. #define SLOP 5
  956. static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
  957. struct commit **interesting_cache)
  958. {
  959. /*
  960. * No source list at all? We're definitely done..
  961. */
  962. if (!src)
  963. return 0;
  964. /*
  965. * Does the destination list contain entries with a date
  966. * before the source list? Definitely _not_ done.
  967. */
  968. if (date <= src->item->date)
  969. return SLOP;
  970. /*
  971. * Does the source list still have interesting commits in
  972. * it? Definitely not done..
  973. */
  974. if (!everybody_uninteresting(src, interesting_cache))
  975. return SLOP;
  976. /* Ok, we're closing in.. */
  977. return slop-1;
  978. }
  979. /*
  980. * "rev-list --ancestry-path A..B" computes commits that are ancestors
  981. * of B but not ancestors of A but further limits the result to those
  982. * that are descendants of A. This takes the list of bottom commits and
  983. * the result of "A..B" without --ancestry-path, and limits the latter
  984. * further to the ones that can reach one of the commits in "bottom".
  985. */
  986. static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
  987. {
  988. struct commit_list *p;
  989. struct commit_list *rlist = NULL;
  990. int made_progress;
  991. /*
  992. * Reverse the list so that it will be likely that we would
  993. * process parents before children.
  994. */
  995. for (p = list; p; p = p->next)
  996. commit_list_insert(p->item, &rlist);
  997. for (p = bottom; p; p = p->next)
  998. p->item->object.flags |= TMP_MARK;
  999. /*
  1000. * Mark the ones that can reach bottom commits in "list",
  1001. * in a bottom-up fashion.
  1002. */
  1003. do {
  1004. made_progress = 0;
  1005. for (p = rlist; p; p = p->next) {
  1006. struct commit *c = p->item;
  1007. struct commit_list *parents;
  1008. if (c->object.flags & (TMP_MARK | UNINTERESTING))
  1009. continue;
  1010. for (parents = c->parents;
  1011. parents;
  1012. parents = parents->next) {
  1013. if (!(parents->item->object.flags & TMP_MARK))
  1014. continue;
  1015. c->object.flags |= TMP_MARK;
  1016. made_progress = 1;
  1017. break;
  1018. }
  1019. }
  1020. } while (made_progress);
  1021. /*
  1022. * NEEDSWORK: decide if we want to remove parents that are
  1023. * not marked with TMP_MARK from commit->parents for commits
  1024. * in the resulting list. We may not want to do that, though.
  1025. */
  1026. /*
  1027. * The ones that are not marked with TMP_MARK are uninteresting
  1028. */
  1029. for (p = list; p; p = p->next) {
  1030. struct commit *c = p->item;
  1031. if (c->object.flags & TMP_MARK)
  1032. continue;
  1033. c->object.flags |= UNINTERESTING;
  1034. }
  1035. /* We are done with the TMP_MARK */
  1036. for (p = list; p; p = p->next)
  1037. p->item->object.flags &= ~TMP_MARK;
  1038. for (p = bottom; p; p = p->next)
  1039. p->item->object.flags &= ~TMP_MARK;
  1040. free_commit_list(rlist);
  1041. }
  1042. /*
  1043. * Before walking the history, keep the set of "negative" refs the
  1044. * caller has asked to exclude.
  1045. *
  1046. * This is used to compute "rev-list --ancestry-path A..B", as we need
  1047. * to filter the result of "A..B" further to the ones that can actually
  1048. * reach A.
  1049. */
  1050. static struct commit_list *collect_bottom_commits(struct commit_list *list)
  1051. {
  1052. struct commit_list *elem, *bottom = NULL;
  1053. for (elem = list; elem; elem = elem->next)
  1054. if (elem->item->object.flags & BOTTOM)
  1055. commit_list_insert(elem->item, &bottom);
  1056. return bottom;
  1057. }
  1058. /* Assumes either left_only or right_only is set */
  1059. static void limit_left_right(struct commit_list *list, struct rev_info *revs)
  1060. {
  1061. struct commit_list *p;
  1062. for (p = list; p; p = p->next) {
  1063. struct commit *commit = p->item;
  1064. if (revs->right_only) {
  1065. if (commit->object.flags & SYMMETRIC_LEFT)
  1066. commit->object.flags |= SHOWN;
  1067. } else /* revs->left_only is set */
  1068. if (!(commit->object.flags & SYMMETRIC_LEFT))
  1069. commit->object.flags |= SHOWN;
  1070. }
  1071. }
  1072. static int limit_list(struct rev_info *revs)
  1073. {
  1074. int slop = SLOP;
  1075. timestamp_t date = TIME_MAX;
  1076. struct commit_list *list = revs->commits;
  1077. struct commit_list *newlist = NULL;
  1078. struct commit_list **p = &newlist;
  1079. struct commit_list *bottom = NULL;
  1080. struct commit *interesting_cache = NULL;
  1081. if (revs->ancestry_path) {
  1082. bottom = collect_bottom_commits(list);
  1083. if (!bottom)
  1084. die("--ancestry-path given but there are no bottom commits");
  1085. }
  1086. while (list) {
  1087. struct commit *commit = pop_commit(&list);
  1088. struct object *obj = &commit->object;
  1089. show_early_output_fn_t show;
  1090. if (commit == interesting_cache)
  1091. interesting_cache = NULL;
  1092. if (revs->max_age != -1 && (commit->date < revs->max_age))
  1093. obj->flags |= UNINTERESTING;
  1094. if (process_parents(revs, commit, &list, NULL) < 0)
  1095. return -1;
  1096. if (obj->flags & UNINTERESTING) {
  1097. mark_parents_uninteresting(commit);
  1098. slop = still_interesting(list, date, slop, &interesting_cache);
  1099. if (slop)
  1100. continue;
  1101. break;
  1102. }
  1103. if (revs->min_age != -1 && (commit->date > revs->min_age))
  1104. continue;
  1105. date = commit->date;
  1106. p = &commit_list_insert(commit, p)->next;
  1107. show = show_early_output;
  1108. if (!show)
  1109. continue;
  1110. show(revs, newlist);
  1111. show_early_output = NULL;
  1112. }
  1113. if (revs->cherry_pick || revs->cherry_mark)
  1114. cherry_pick_list(newlist, revs);
  1115. if (revs->left_only || revs->right_only)
  1116. limit_left_right(newlist, revs);
  1117. if (bottom) {
  1118. limit_to_ancestry(bottom, newlist);
  1119. free_commit_list(bottom);
  1120. }
  1121. /*
  1122. * Check if any commits have become TREESAME by some of their parents
  1123. * becoming UNINTERESTING.
  1124. */
  1125. if (limiting_can_increase_treesame(revs))
  1126. for (list = newlist; list; list = list->next) {
  1127. struct commit *c = list->item;
  1128. if (c->object.flags & (UNINTERESTING | TREESAME))
  1129. continue;
  1130. update_treesame(revs, c);
  1131. }
  1132. revs->commits = newlist;
  1133. return 0;
  1134. }
  1135. /*
  1136. * Add an entry to refs->cmdline with the specified information.
  1137. * *name is copied.
  1138. */
  1139. static void add_rev_cmdline(struct rev_info *revs,
  1140. struct object *item,
  1141. const char *name,
  1142. int whence,
  1143. unsigned flags)
  1144. {
  1145. struct rev_cmdline_info *info = &revs->cmdline;
  1146. unsigned int nr = info->nr;
  1147. ALLOC_GROW(info->rev, nr + 1, info->alloc);
  1148. info->rev[nr].item = item;
  1149. info->rev[nr].name = xstrdup(name);
  1150. info->rev[nr].whence = whence;
  1151. info->rev[nr].flags = flags;
  1152. info->nr++;
  1153. }
  1154. static void add_rev_cmdline_list(struct rev_info *revs,
  1155. struct commit_list *commit_list,
  1156. int whence,
  1157. unsigned flags)
  1158. {
  1159. while (commit_list) {
  1160. struct object *object = &commit_list->item->object;
  1161. add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
  1162. whence, flags);
  1163. commit_list = commit_list->next;
  1164. }
  1165. }
  1166. struct all_refs_cb {
  1167. int all_flags;
  1168. int warned_bad_reflog;
  1169. struct rev_info *all_revs;
  1170. const char *name_for_errormsg;
  1171. struct worktree *wt;
  1172. };
  1173. int ref_excluded(struct string_list *ref_excludes, const char *path)
  1174. {
  1175. struct string_list_item *item;
  1176. if (!ref_excludes)
  1177. return 0;
  1178. for_each_string_list_item(item, ref_excludes) {
  1179. if (!wildmatch(item->string, path, 0))
  1180. return 1;
  1181. }
  1182. return 0;
  1183. }
  1184. static int handle_one_ref(const char *path, const struct object_id *oid,
  1185. int flag, void *cb_data)
  1186. {
  1187. struct all_refs_cb *cb = cb_data;
  1188. struct object *object;
  1189. if (ref_excluded(cb->all_revs->ref_excludes, path))
  1190. return 0;
  1191. object = get_reference(cb->all_revs, path, oid, cb->all_flags);
  1192. add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
  1193. add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
  1194. return 0;
  1195. }
  1196. static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
  1197. unsigned flags)
  1198. {
  1199. cb->all_revs = revs;
  1200. cb->all_flags = flags;
  1201. revs->rev_input_given = 1;
  1202. cb->wt = NULL;
  1203. }
  1204. void clear_ref_exclusion(struct string_list **ref_excludes_p)
  1205. {
  1206. if (*ref_excludes_p) {
  1207. string_list_clear(*ref_excludes_p, 0);
  1208. free(*ref_excludes_p);
  1209. }
  1210. *ref_excludes_p = NULL;
  1211. }
  1212. void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
  1213. {
  1214. if (!*ref_excludes_p) {
  1215. *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
  1216. (*ref_excludes_p)->strdup_strings = 1;
  1217. }
  1218. string_list_append(*ref_excludes_p, exclude);
  1219. }
  1220. static void handle_refs(struct ref_store *refs,
  1221. struct rev_info *revs, unsigned flags,
  1222. int (*for_each)(struct ref_store *, each_ref_fn, void *))
  1223. {
  1224. struct all_refs_cb cb;
  1225. if (!refs) {
  1226. /* this could happen with uninitialized submodules */
  1227. return;
  1228. }
  1229. init_all_refs_cb(&cb, revs, flags);
  1230. for_each(refs, handle_one_ref, &cb);
  1231. }
  1232. static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
  1233. {
  1234. struct all_refs_cb *cb = cb_data;
  1235. if (!is_null_oid(oid)) {
  1236. struct object *o = parse_object(cb->all_revs->repo, oid);
  1237. if (o) {
  1238. o->flags |= cb->all_flags;
  1239. /* ??? CMDLINEFLAGS ??? */
  1240. add_pending_object(cb->all_revs, o, "");
  1241. }
  1242. else if (!cb->warned_bad_reflog) {
  1243. warning("reflog of '%s' references pruned commits",
  1244. cb->name_for_errormsg);
  1245. cb->warned_bad_reflog = 1;
  1246. }
  1247. }
  1248. }
  1249. static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
  1250. const char *email, timestamp_t timestamp, int tz,
  1251. const char *message, void *cb_data)
  1252. {
  1253. handle_one_reflog_commit(ooid, cb_data);
  1254. handle_one_reflog_commit(noid, cb_data);
  1255. return 0;
  1256. }
  1257. static int handle_one_reflog(const char *refname_in_wt,
  1258. const struct object_id *oid,
  1259. int flag, void *cb_data)
  1260. {
  1261. struct all_refs_cb *cb = cb_data;
  1262. struct strbuf refname = STRBUF_INIT;
  1263. cb->warned_bad_reflog = 0;
  1264. strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
  1265. cb->name_for_errormsg = refname.buf;
  1266. refs_for_each_reflog_ent(get_main_ref_store(the_repository),
  1267. refname.buf,
  1268. handle_one_reflog_ent, cb_data);
  1269. strbuf_release(&refname);
  1270. return 0;
  1271. }
  1272. static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
  1273. {
  1274. struct worktree **worktrees, **p;
  1275. worktrees = get_worktrees(0);
  1276. for (p = worktrees; *p; p++) {
  1277. struct worktree *wt = *p;
  1278. if (wt->is_current)
  1279. continue;
  1280. cb->wt = wt;
  1281. refs_for_each_reflog(get_worktree_ref_store(wt),
  1282. handle_one_reflog,
  1283. cb);
  1284. }
  1285. free_worktrees(worktrees);
  1286. }
  1287. void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
  1288. {
  1289. struct all_refs_cb cb;
  1290. cb.all_revs = revs;
  1291. cb.all_flags = flags;
  1292. cb.wt = NULL;
  1293. for_each_reflog(handle_one_reflog, &cb);
  1294. if (!revs->single_worktree)
  1295. add_other_reflogs_to_pending(&cb);
  1296. }
  1297. static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
  1298. struct strbuf *path, unsigned int flags)
  1299. {
  1300. size_t baselen = path->len;
  1301. int i;
  1302. if (it->entry_count >= 0) {
  1303. struct tree *tree = lookup_tree(revs->repo, &it->oid);
  1304. tree->object.flags |= flags;
  1305. add_pending_object_with_path(revs, &tree->object, "",
  1306. 040000, path->buf);
  1307. }
  1308. for (i = 0; i < it->subtree_nr; i++) {
  1309. struct cache_tree_sub *sub = it->down[i];
  1310. strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
  1311. add_cache_tree(sub->cache_tree, revs, path, flags);
  1312. strbuf_setlen(path, baselen);
  1313. }
  1314. }
  1315. static void do_add_index_objects_to_pending(struct rev_info *revs,
  1316. struct index_state *istate,
  1317. unsigned int flags)
  1318. {
  1319. int i;
  1320. for (i = 0; i < istate->cache_nr; i++) {
  1321. struct cache_entry *ce = istate->cache[i];
  1322. struct blob *blob;
  1323. if (S_ISGITLINK(ce->ce_mode))
  1324. continue;
  1325. blob = lookup_blob(revs->repo, &ce->oid);
  1326. if (!blob)
  1327. die("unable to add index blob to traversal");
  1328. blob->object.flags |= flags;
  1329. add_pending_object_with_path(revs, &blob->object, "",
  1330. ce->ce_mode, ce->name);
  1331. }
  1332. if (istate->cache_tree) {
  1333. struct strbuf path = STRBUF_INIT;
  1334. add_cache_tree(istate->cache_tree, revs, &path, flags);
  1335. strbuf_release(&path);
  1336. }
  1337. }
  1338. void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
  1339. {
  1340. struct worktree **worktrees, **p;
  1341. repo_read_index(revs->repo);
  1342. do_add_index_objects_to_pending(revs, revs->repo->index, flags);
  1343. if (revs->single_worktree)
  1344. return;
  1345. worktrees = get_worktrees(0);
  1346. for (p = worktrees; *p; p++) {
  1347. struct worktree *wt = *p;
  1348. struct index_state istate = { NULL };
  1349. if (wt->is_current)
  1350. continue; /* current index already taken care of */
  1351. if (read_index_from(&istate,
  1352. worktree_git_path(wt, "index"),
  1353. get_worktree_git_dir(wt)) > 0)
  1354. do_add_index_objects_to_pending(revs, &istate, flags);
  1355. discard_index(&istate);
  1356. }
  1357. free_worktrees(worktrees);
  1358. }
  1359. struct add_alternate_refs_data {
  1360. struct rev_info *revs;
  1361. unsigned int flags;
  1362. };
  1363. static void add_one_alternate_ref(const struct object_id *oid,
  1364. void *vdata)
  1365. {
  1366. const char *name = ".alternate";
  1367. struct add_alternate_refs_data *data = vdata;
  1368. struct object *obj;
  1369. obj = get_reference(data->revs, name, oid, data->flags);
  1370. add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
  1371. add_pending_object(data->revs, obj, name);
  1372. }
  1373. static void add_alternate_refs_to_pending(struct rev_info *revs,
  1374. unsigned int flags)
  1375. {
  1376. struct add_alternate_refs_data data;
  1377. data.revs = revs;
  1378. data.flags = flags;
  1379. for_each_alternate_ref(add_one_alternate_ref, &data);
  1380. }
  1381. static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
  1382. int exclude_parent)
  1383. {
  1384. struct object_id oid;
  1385. struct object *it;
  1386. struct commit *commit;
  1387. struct commit_list *parents;
  1388. int parent_number;
  1389. const char *arg = arg_;
  1390. if (*arg == '^') {
  1391. flags ^= UNINTERESTING | BOTTOM;
  1392. arg++;
  1393. }
  1394. if (get_oid_committish(arg, &oid))
  1395. return 0;
  1396. while (1) {
  1397. it = get_reference(revs, arg, &oid, 0);
  1398. if (!it && revs->ignore_missing)
  1399. return 0;
  1400. if (it->type != OBJ_TAG)
  1401. break;
  1402. if (!((struct tag*)it)->tagged)
  1403. return 0;
  1404. oidcpy(&oid, &((struct tag*)it)->tagged->oid);
  1405. }
  1406. if (it->type != OBJ_COMMIT)
  1407. return 0;
  1408. commit = (struct commit *)it;
  1409. if (exclude_parent &&
  1410. exclude_parent > commit_list_count(commit->parents))
  1411. return 0;
  1412. for (parents = commit->parents, parent_number = 1;
  1413. parents;
  1414. parents = parents->next, parent_number++) {
  1415. if (exclude_parent && parent_number != exclude_parent)
  1416. continue;
  1417. it = &parents->item->object;
  1418. it->flags |= flags;
  1419. add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
  1420. add_pending_object(revs, it, arg);
  1421. }
  1422. return 1;
  1423. }
  1424. void repo_init_revisions(struct repository *r,
  1425. struct rev_info *revs,
  1426. const char *prefix)
  1427. {
  1428. memset(revs, 0, sizeof(*revs));
  1429. revs->repo = r;
  1430. revs->abbrev = DEFAULT_ABBREV;
  1431. revs->ignore_merges = 1;
  1432. revs->simplify_history = 1;
  1433. revs->pruning.repo = r;
  1434. revs->pruning.flags.recursive = 1;
  1435. revs->pruning.flags.quick = 1;
  1436. revs->pruning.add_remove = file_add_remove;
  1437. revs->pruning.change = file_change;
  1438. revs->pruning.change_fn_data = revs;
  1439. revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
  1440. revs->dense = 1;
  1441. revs->prefix = prefix;
  1442. revs->max_age = -1;
  1443. revs->min_age = -1;
  1444. revs->skip_count = -1;
  1445. revs->max_count = -1;
  1446. revs->max_parents = -1;
  1447. revs->expand_tabs_in_log = -1;
  1448. revs->commit_format = CMIT_FMT_DEFAULT;
  1449. revs->expand_tabs_in_log_default = 8;
  1450. init_grep_defaults(revs->repo);
  1451. grep_init(&revs->grep_filter, revs->repo, prefix);
  1452. revs->grep_filter.status_only = 1;
  1453. repo_diff_setup(revs->repo, &revs->diffopt);
  1454. if (prefix && !revs->diffopt.prefix) {
  1455. revs->diffopt.prefix = prefix;
  1456. revs->diffopt.prefix_length = strlen(prefix);
  1457. }
  1458. revs->notes_opt.use_default_notes = -1;
  1459. }
  1460. static void add_pending_commit_list(struct rev_info *revs,
  1461. struct commit_list *commit_list,
  1462. unsigned int flags)
  1463. {
  1464. while (commit_list) {
  1465. struct object *object = &commit_list->item->object;
  1466. object->flags |= flags;
  1467. add_pending_object(revs, object, oid_to_hex(&object->oid));
  1468. commit_list = commit_list->next;
  1469. }
  1470. }
  1471. static void prepare_show_merge(struct rev_info *revs)
  1472. {
  1473. struct commit_list *bases;
  1474. struct commit *head, *other;
  1475. struct object_id oid;
  1476. const char **prune = NULL;
  1477. int i, prune_num = 1; /* counting terminating NULL */
  1478. struct index_state *istate = revs->repo->index;
  1479. if (get_oid("HEAD", &oid))
  1480. die("--merge without HEAD?");
  1481. head = lookup_commit_or_die(&oid, "HEAD");
  1482. if (get_oid("MERGE_HEAD", &oid))
  1483. die("--merge without MERGE_HEAD?");
  1484. other = lookup_commit_or_die(&oid, "MERGE_HEAD");
  1485. add_pending_object(revs, &head->object, "HEAD");
  1486. add_pending_object(revs, &other->object, "MERGE_HEAD");
  1487. bases = get_merge_bases(head, other);
  1488. add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
  1489. add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
  1490. free_commit_list(bases);
  1491. head->object.flags |= SYMMETRIC_LEFT;
  1492. if (!istate->cache_nr)
  1493. repo_read_index(revs->repo);
  1494. for (i = 0; i < istate->cache_nr; i++) {
  1495. const struct cache_entry *ce = istate->cache[i];
  1496. if (!ce_stage(ce))
  1497. continue;
  1498. if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
  1499. prune_num++;
  1500. REALLOC_ARRAY(prune, prune_num);
  1501. prune[prune_num-2] = ce->name;
  1502. prune[prune_num-1] = NULL;
  1503. }
  1504. while ((i+1 < istate->cache_nr) &&
  1505. ce_same_name(ce, istate->cache[i+1]))
  1506. i++;
  1507. }
  1508. clear_pathspec(&revs->prune_data);
  1509. parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
  1510. PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
  1511. revs->limited = 1;
  1512. }
  1513. static int dotdot_missing(const char *arg, char *dotdot,
  1514. struct rev_info *revs, int symmetric)
  1515. {
  1516. if (revs->ignore_missing)
  1517. return 0;
  1518. /* de-munge so we report the full argument */
  1519. *dotdot = '.';
  1520. die(symmetric
  1521. ? "Invalid symmetric difference expression %s"
  1522. : "Invalid revision range %s", arg);
  1523. }
  1524. static int handle_dotdot_1(const char *arg, char *dotdot,
  1525. struct rev_info *revs, int flags,
  1526. int cant_be_filename,
  1527. struct object_context *a_oc,
  1528. struct object_context *b_oc)
  1529. {
  1530. const char *a_name, *b_name;
  1531. struct object_id a_oid, b_oid;
  1532. struct object *a_obj, *b_obj;
  1533. unsigned int a_flags, b_flags;
  1534. int symmetric = 0;
  1535. unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
  1536. unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
  1537. a_name = arg;
  1538. if (!*a_name)
  1539. a_name = "HEAD";
  1540. b_name = dotdot + 2;
  1541. if (*b_name == '.') {
  1542. symmetric = 1;
  1543. b_name++;
  1544. }
  1545. if (!*b_name)
  1546. b_name = "HEAD";
  1547. if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
  1548. get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
  1549. return -1;
  1550. if (!cant_be_filename) {
  1551. *dotdot = '.';
  1552. verify_non_filename(revs->prefix, arg);
  1553. *dotdot = '\0';
  1554. }
  1555. a_obj = parse_object(revs->repo, &a_oid);
  1556. b_obj = parse_object(revs->repo, &b_oid);
  1557. if (!a_obj || !b_obj)
  1558. return dotdot_missing(arg, dotdot, revs, symmetric);
  1559. if (!symmetric) {
  1560. /* just A..B */
  1561. b_flags = flags;
  1562. a_flags = flags_exclude;
  1563. } else {
  1564. /* A...B -- find merge bases between the two */
  1565. struct commit *a, *b;
  1566. struct commit_list *exclude;
  1567. a = lookup_commit_reference(revs->repo, &a_obj->oid);
  1568. b = lookup_commit_reference(revs->repo, &b_obj->oid);
  1569. if (!a || !b)
  1570. return dotdot_missing(arg, dotdot, revs, symmetric);
  1571. exclude = get_merge_bases(a, b);
  1572. add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
  1573. flags_exclude);
  1574. add_pending_commit_list(revs, exclude, flags_exclude);
  1575. free_commit_list(exclude);
  1576. b_flags = flags;
  1577. a_flags = flags | SYMMETRIC_LEFT;
  1578. }
  1579. a_obj->flags |= a_flags;
  1580. b_obj->flags |= b_flags;
  1581. add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
  1582. add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
  1583. add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
  1584. add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
  1585. return 0;
  1586. }
  1587. static int handle_dotdot(const char *arg,
  1588. struct rev_info *revs, int flags,
  1589. int cant_be_filename)
  1590. {
  1591. struct object_context a_oc, b_oc;
  1592. char *dotdot = strstr(arg, "..");
  1593. int ret;
  1594. if (!dotdot)
  1595. return -1;
  1596. memset(&a_oc, 0, sizeof(a_oc));
  1597. memset(&b_oc, 0, sizeof(b_oc));
  1598. *dotdot = '\0';
  1599. ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
  1600. &a_oc, &b_oc);
  1601. *dotdot = '.';
  1602. free(a_oc.path);
  1603. free(b_oc.path);
  1604. return ret;
  1605. }
  1606. int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
  1607. {
  1608. struct object_context oc;
  1609. char *mark;
  1610. struct object *object;
  1611. struct object_id oid;
  1612. int local_flags;
  1613. const char *arg = arg_;
  1614. int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
  1615. unsigned get_sha1_flags = GET_OID_RECORD_PATH;
  1616. flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
  1617. if (!cant_be_filename && !strcmp(arg, "..")) {
  1618. /*
  1619. * Just ".."? That is not a range but the
  1620. * pathspec for the parent directory.
  1621. */
  1622. return -1;
  1623. }
  1624. if (!handle_dotdot(arg, revs, flags, revarg_opt))
  1625. return 0;
  1626. mark = strstr(arg, "^@");
  1627. if (mark && !mark[2]) {
  1628. *mark = 0;
  1629. if (add_parents_only(revs, arg, flags, 0))
  1630. return 0;
  1631. *mark = '^';
  1632. }
  1633. mark = strstr(arg, "^!");
  1634. if (mark && !mark[2]) {
  1635. *mark = 0;
  1636. if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
  1637. *mark = '^';
  1638. }
  1639. mark = strstr(arg, "^-");
  1640. if (mark) {
  1641. int exclude_parent = 1;
  1642. if (mark[2]) {
  1643. char *end;
  1644. exclude_parent = strtoul(mark + 2, &end, 10);
  1645. if (*end != '\0' || !exclude_parent)
  1646. return -1;
  1647. }
  1648. *mark = 0;
  1649. if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
  1650. *mark = '^';
  1651. }
  1652. local_flags = 0;
  1653. if (*arg == '^') {
  1654. local_flags = UNINTERESTING | BOTTOM;
  1655. arg++;
  1656. }
  1657. if (revarg_opt & REVARG_COMMITTISH)
  1658. get_sha1_flags |= GET_OID_COMMITTISH;
  1659. if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
  1660. return revs->ignore_missing ? 0 : -1;
  1661. if (!cant_be_filename)
  1662. verify_non_filename(revs->prefix, arg);
  1663. object = get_reference(revs, arg, &oid, flags ^ local_flags);
  1664. if (!object)
  1665. return revs->ignore_missing ? 0 : -1;
  1666. add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
  1667. add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
  1668. free(oc.path);
  1669. return 0;
  1670. }
  1671. static void read_pathspec_from_stdin(struct strbuf *sb,
  1672. struct argv_array *prune)
  1673. {
  1674. while (strbuf_getline(sb, stdin) != EOF)
  1675. argv_array_push(prune, sb->buf);
  1676. }
  1677. static void read_revisions_from_stdin(struct rev_info *revs,
  1678. struct argv_array *prune)
  1679. {
  1680. struct strbuf sb;
  1681. int seen_dashdash = 0;
  1682. int save_warning;
  1683. save_warning = warn_on_object_refname_ambiguity;
  1684. warn_on_object_refname_ambiguity = 0;
  1685. strbuf_init(&sb, 1000);
  1686. while (strbuf_getline(&sb, stdin) != EOF) {
  1687. int len = sb.len;
  1688. if (!len)
  1689. break;
  1690. if (sb.buf[0] == '-') {
  1691. if (len == 2 && sb.buf[1] == '-') {
  1692. seen_dashdash = 1;
  1693. break;
  1694. }
  1695. die("options not supported in --stdin mode");
  1696. }
  1697. if (handle_revision_arg(sb.buf, revs, 0,
  1698. REVARG_CANNOT_BE_FILENAME))
  1699. die("bad revision '%s'", sb.buf);
  1700. }
  1701. if (seen_dashdash)
  1702. read_pathspec_from_stdin(&sb, prune);
  1703. strbuf_release(&sb);
  1704. warn_on_object_refname_ambiguity = save_warning;
  1705. }
  1706. static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
  1707. {
  1708. append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
  1709. }
  1710. static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
  1711. {
  1712. append_header_grep_pattern(&revs->grep_filter, field, pattern);
  1713. }
  1714. static void add_message_grep(struct rev_info *revs, const char *pattern)
  1715. {
  1716. add_grep(revs, pattern, GREP_PATTERN_BODY);
  1717. }
  1718. static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
  1719. int *unkc, const char **unkv,
  1720. const struct setup_revision_opt* opt)
  1721. {
  1722. const char *arg = argv[0];
  1723. const char *optarg;
  1724. int argcount;
  1725. const unsigned hexsz = the_hash_algo->hexsz;
  1726. /* pseudo revision arguments */
  1727. if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
  1728. !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
  1729. !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
  1730. !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
  1731. !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
  1732. !strcmp(arg, "--indexed-objects") ||
  1733. !strcmp(arg, "--alternate-refs") ||
  1734. starts_with(arg, "--exclude=") ||
  1735. starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
  1736. starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
  1737. {
  1738. unkv[(*unkc)++] = arg;
  1739. return 1;
  1740. }
  1741. if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
  1742. revs->max_count = atoi(optarg);
  1743. revs->no_walk = 0;
  1744. return argcount;
  1745. } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
  1746. revs->skip_count = atoi(optarg);
  1747. return argcount;
  1748. } else if ((*arg == '-') && isdigit(arg[1])) {
  1749. /* accept -<digit>, like traditional "head" */
  1750. if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
  1751. revs->max_count < 0)
  1752. die("'%s': not a non-negative integer", arg + 1);
  1753. revs->no_walk = 0;
  1754. } else if (!strcmp(arg, "-n")) {
  1755. if (argc <= 1)
  1756. return error("-n requires an argument");
  1757. revs->max_count = atoi(argv[1]);
  1758. revs->no_walk = 0;
  1759. return 2;
  1760. } else if (skip_prefix(arg, "-n", &optarg)) {
  1761. revs->max_count = atoi(optarg);
  1762. revs->no_walk = 0;
  1763. } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
  1764. revs->max_age = atoi(optarg);
  1765. return argcount;
  1766. } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
  1767. revs->max_age = approxidate(optarg);
  1768. return argcount;
  1769. } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
  1770. revs->max_age = approxidate(optarg);
  1771. return argcount;
  1772. } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
  1773. revs->min_age = atoi(optarg);
  1774. return argcount;
  1775. } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
  1776. revs->min_age = approxidate(optarg);
  1777. return argcount;
  1778. } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
  1779. revs->min_age = approxidate(optarg);
  1780. return argcount;
  1781. } else if (!strcmp(arg, "--first-parent")) {
  1782. revs->first_parent_only = 1;
  1783. } else if (!strcmp(arg, "--ancestry-path")) {
  1784. revs->ancestry_path = 1;
  1785. revs->simplify_history = 0;
  1786. revs->limited = 1;
  1787. } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
  1788. init_reflog_walk(&revs->reflog_info);
  1789. } else if (!strcmp(arg, "--default")) {
  1790. if (argc <= 1)
  1791. return error("bad --default argument");
  1792. revs->def = argv[1];
  1793. return 2;
  1794. } else if (!strcmp(arg, "--merge")) {
  1795. revs->show_merge = 1;
  1796. } else if (!strcmp(arg, "--topo-order")) {
  1797. revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
  1798. revs->topo_order = 1;
  1799. } else if (!strcmp(arg, "--simplify-merges")) {
  1800. revs->simplify_merges = 1;
  1801. revs->topo_order = 1;
  1802. revs->rewrite_parents = 1;
  1803. revs->simplify_history = 0;
  1804. revs->limited = 1;
  1805. } else if (!strcmp(arg, "--simplify-by-decoration")) {
  1806. revs->simplify_merges = 1;
  1807. revs->topo_order = 1;
  1808. revs->rewrite_parents = 1;
  1809. revs->simplify_history = 0;
  1810. revs->simplify_by_decoration = 1;
  1811. revs->limited = 1;
  1812. revs->prune = 1;
  1813. } else if (!strcmp(arg, "--date-order")) {
  1814. revs->sort_order = REV_SORT_BY_COMMIT_DATE;
  1815. revs->topo_order = 1;
  1816. } else if (!strcmp(arg, "--author-date-order")) {
  1817. revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
  1818. revs->topo_order = 1;
  1819. } else if (!strcmp(arg, "--early-output")) {
  1820. revs->early_output = 100;
  1821. revs->topo_order = 1;
  1822. } else if (skip_prefix(arg, "--early-output=", &optarg)) {
  1823. if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
  1824. die("'%s': not a non-negative integer", optarg);
  1825. revs->topo_order = 1;
  1826. } else if (!strcmp(arg, "--parents")) {
  1827. revs->rewrite_parents = 1;
  1828. revs->print_parents = 1;
  1829. } else if (!strcmp(arg, "--dense")) {
  1830. revs->dense = 1;
  1831. } else if (!strcmp(arg, "--sparse")) {
  1832. revs->dense = 0;
  1833. } else if (!strcmp(arg, "--in-commit-order")) {
  1834. revs->tree_blobs_in_commit_order = 1;
  1835. } else if (!strcmp(arg, "--remove-empty")) {
  1836. revs->remove_empty_trees = 1;
  1837. } else if (!strcmp(arg, "--merges")) {
  1838. revs->min_parents = 2;
  1839. } else if (!strcmp(arg, "--no-merges")) {
  1840. revs->max_parents = 1;
  1841. } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
  1842. revs->min_parents = atoi(optarg);
  1843. } else if (!strcmp(arg, "--no-min-parents")) {
  1844. revs->min_parents = 0;
  1845. } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
  1846. revs->max_parents = atoi(optarg);
  1847. } else if (!strcmp(arg, "--no-max-parents")) {
  1848. revs->max_parents = -1;
  1849. } else if (!strcmp(arg, "--boundary")) {
  1850. revs->boundary = 1;
  1851. } else if (!strcmp(arg, "--left-right")) {
  1852. revs->left_right = 1;
  1853. } else if (!strcmp(arg, "--left-only")) {
  1854. if (revs->right_only)
  1855. die("--left-only is incompatible with --right-only"
  1856. " or --cherry");
  1857. revs->left_only = 1;
  1858. } else if (!strcmp(arg, "--right-only")) {
  1859. if (revs->left_only)
  1860. die("--right-only is incompatible with --left-only");
  1861. revs->right_only = 1;
  1862. } else if (!strcmp(arg, "--cherry")) {
  1863. if (revs->left_only)
  1864. die("--cherry is incompatible with --left-only");
  1865. revs->cherry_mark = 1;
  1866. revs->right_only = 1;
  1867. revs->max_parents = 1;
  1868. revs->limited = 1;
  1869. } else if (!strcmp(arg, "--count")) {
  1870. revs->count = 1;
  1871. } else if (!strcmp(arg, "--cherry-mark")) {
  1872. if (revs->cherry_pick)
  1873. die("--cherry-mark is incompatible with --cherry-pick");
  1874. revs->cherry_mark = 1;
  1875. revs->limited = 1; /* needs limit_list() */
  1876. } else if (!strcmp(arg, "--cherry-pick")) {
  1877. if (revs->cherry_mark)
  1878. die("--cherry-pick is incompatible with --cherry-mark");
  1879. revs->cherry_pick = 1;
  1880. revs->limited = 1;
  1881. } else if (!strcmp(arg, "--objects")) {
  1882. revs->tag_objects = 1;
  1883. revs->tree_objects = 1;
  1884. revs->blob_objects = 1;
  1885. } else if (!strcmp(arg, "--objects-edge")) {
  1886. revs->tag_objects = 1;
  1887. revs->tree_objects = 1;
  1888. revs->blob_objects = 1;
  1889. revs->edge_hint = 1;
  1890. } else if (!strcmp(arg, "--objects-edge-aggressive")) {
  1891. revs->tag_objects = 1;
  1892. revs->tree_objects = 1;
  1893. revs->blob_objects = 1;
  1894. revs->edge_hint = 1;
  1895. revs->edge_hint_aggressive = 1;
  1896. } else if (!strcmp(arg, "--verify-objects")) {
  1897. revs->tag_objects = 1;
  1898. revs->tree_objects = 1;
  1899. revs->blob_objects = 1;
  1900. revs->verify_objects = 1;
  1901. } else if (!strcmp(arg, "--unpacked")) {
  1902. revs->unpacked = 1;
  1903. } else if (starts_with(arg, "--unpacked=")) {
  1904. die("--unpacked=<packfile> no longer supported.");
  1905. } else if (!strcmp(arg, "-r")) {
  1906. revs->diff = 1;
  1907. revs->diffopt.flags.recursive = 1;
  1908. } else if (!strcmp(arg, "-t")) {
  1909. revs->diff = 1;
  1910. revs->diffopt.flags.recursive = 1;
  1911. revs->diffopt.flags.tree_in_recursive = 1;
  1912. } else if (!strcmp(arg, "-m")) {
  1913. revs->ignore_merges = 0;
  1914. } else if (!strcmp(arg, "-c")) {
  1915. revs->diff = 1;
  1916. revs->dense_combined_merges = 0;
  1917. revs->combine_merges = 1;
  1918. } else if (!strcmp(arg, "--combined-all-paths")) {
  1919. revs->diff = 1;
  1920. revs->combined_all_paths = 1;
  1921. } else if (!strcmp(arg, "--cc")) {
  1922. revs->diff = 1;
  1923. revs->dense_combined_merges = 1;
  1924. revs->combine_merges = 1;
  1925. } else if (!strcmp(arg, "-v")) {
  1926. revs->verbose_header = 1;
  1927. } else if (!strcmp(arg, "--pretty")) {
  1928. revs->verbose_header = 1;
  1929. revs->pretty_given = 1;
  1930. get_commit_format(NULL, revs);
  1931. } else if (skip_prefix(arg, "--pretty=", &optarg) ||
  1932. skip_prefix(arg, "--format=", &optarg)) {
  1933. /*
  1934. * Detached form ("--pretty X" as opposed to "--pretty=X")
  1935. * not allowed, since the argument is optional.
  1936. */
  1937. revs->verbose_header = 1;
  1938. revs->pretty_given = 1;
  1939. get_commit_format(optarg, revs);
  1940. } else if (!strcmp(arg, "--expand-tabs")) {
  1941. revs->expand_tabs_in_log = 8;
  1942. } else if (!strcmp(arg, "--no-expand-tabs")) {
  1943. revs->expand_tabs_in_log = 0;
  1944. } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
  1945. int val;
  1946. if (strtol_i(arg, 10, &val) < 0 || val < 0)
  1947. die("'%s': not a non-negative integer", arg);
  1948. revs->expand_tabs_in_log = val;
  1949. } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
  1950. revs->show_notes = 1;
  1951. revs->show_notes_given = 1;
  1952. revs->notes_opt.use_default_notes = 1;
  1953. } else if (!strcmp(arg, "--show-signature")) {
  1954. revs->show_signature = 1;
  1955. } else if (!strcmp(arg, "--no-show-signature")) {
  1956. revs->show_signature = 0;
  1957. } else if (!strcmp(arg, "--show-linear-break")) {
  1958. revs->break_bar = " ..........";
  1959. revs->track_linear = 1;
  1960. revs->track_first_time = 1;
  1961. } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
  1962. revs->break_bar = xstrdup(optarg);
  1963. revs->track_linear = 1;
  1964. revs->track_first_time = 1;
  1965. } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
  1966. skip_prefix(arg, "--notes=", &optarg)) {
  1967. struct strbuf buf = STRBUF_INIT;
  1968. revs->show_notes = 1;
  1969. revs->show_notes_given = 1;
  1970. if (starts_with(arg, "--show-notes=") &&
  1971. revs->notes_opt.use_default_notes < 0)
  1972. revs->notes_opt.use_default_notes = 1;
  1973. strbuf_addstr(&buf, optarg);
  1974. expand_notes_ref(&buf);
  1975. string_list_append(&revs->notes_opt.extra_notes_refs,
  1976. strbuf_detach(&buf, NULL));
  1977. } else if (!strcmp(arg, "--no-notes")) {
  1978. revs->show_notes = 0;
  1979. revs->show_notes_given = 1;
  1980. revs->notes_opt.use_default_notes = -1;
  1981. /* we have been strdup'ing ourselves, so trick
  1982. * string_list into free()ing strings */
  1983. revs->notes_opt.extra_notes_refs.strdup_strings = 1;
  1984. string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
  1985. revs->notes_opt.extra_notes_refs.strdup_strings = 0;
  1986. } else if (!strcmp(arg, "--standard-notes")) {
  1987. revs->show_notes_given = 1;
  1988. revs->notes_opt.use_default_notes = 1;
  1989. } else if (!strcmp(arg, "--no-standard-notes")) {
  1990. revs->notes_opt.use_default_notes = 0;
  1991. } else if (!strcmp(arg, "--oneline")) {
  1992. revs->verbose_header = 1;
  1993. get_commit_format("oneline", revs);
  1994. revs->pretty_given = 1;
  1995. revs->abbrev_commit = 1;
  1996. } else if (!strcmp(arg, "--graph")) {
  1997. revs->topo_order = 1;
  1998. revs->rewrite_parents = 1;
  1999. revs->graph = graph_init(revs);
  2000. } else if (!strcmp(arg, "--root")) {
  2001. revs->show_root_diff = 1;
  2002. } else if (!strcmp(arg, "--no-commit-id")) {
  2003. revs->no_commit_id = 1;
  2004. } else if (!strcmp(arg, "--always")) {
  2005. revs->always_show_header = 1;
  2006. } else if (!strcmp(arg, "--no-abbrev")) {
  2007. revs->abbrev = 0;
  2008. } else if (!strcmp(arg, "--abbrev")) {
  2009. revs->abbrev = DEFAULT_ABBREV;
  2010. } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
  2011. revs->abbrev = strtoul(optarg, NULL, 10);
  2012. if (revs->abbrev < MINIMUM_ABBREV)
  2013. revs->abbrev = MINIMUM_ABBREV;
  2014. else if (revs->abbrev > hexsz)
  2015. revs->abbrev = hexsz;
  2016. } else if (!strcmp(arg, "--abbrev-commit")) {
  2017. revs->abbrev_commit = 1;
  2018. revs->abbrev_commit_given = 1;
  2019. } else if (!strcmp(arg, "--no-abbrev-commit")) {
  2020. revs->abbrev_commit = 0;
  2021. } else if (!strcmp(arg, "--full-diff")) {
  2022. revs->diff = 1;
  2023. revs->full_diff = 1;
  2024. } else if (!strcmp(arg, "--full-history")) {
  2025. revs->simplify_history = 0;
  2026. } else if (!strcmp(arg, "--relative-date")) {
  2027. revs->date_mode.type = DATE_RELATIVE;
  2028. revs->date_mode_explicit = 1;
  2029. } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
  2030. parse_date_format(optarg, &revs->date_mode);
  2031. revs->date_mode_explicit = 1;
  2032. return argcount;
  2033. } else if (!strcmp(arg, "--log-size")) {
  2034. revs->show_log_size = 1;
  2035. }
  2036. /*
  2037. * Grepping the commit log
  2038. */
  2039. else if ((argcount = parse_long_opt("author", argv, &optarg))) {
  2040. add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
  2041. return argcount;
  2042. } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
  2043. add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
  2044. return argcount;
  2045. } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
  2046. add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
  2047. return argcount;
  2048. } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
  2049. add_message_grep(revs, optarg);
  2050. return argcount;
  2051. } else if (!strcmp(arg, "--grep-debug")) {
  2052. revs->grep_filter.debug = 1;
  2053. } else if (!strcmp(arg, "--basic-regexp")) {
  2054. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
  2055. } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
  2056. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
  2057. } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
  2058. revs->grep_filter.ignore_case = 1;
  2059. revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
  2060. } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
  2061. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
  2062. } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
  2063. revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
  2064. } else if (!strcmp(arg, "--all-match")) {
  2065. revs->grep_filter.all_match = 1;
  2066. } else if (!strcmp(arg, "--invert-grep")) {
  2067. revs->invert_grep = 1;
  2068. } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
  2069. if (strcmp(optarg, "none"))
  2070. git_log_output_encoding = xstrdup(optarg);
  2071. else
  2072. git_log_output_encoding = "";
  2073. return argcount;
  2074. } else if (!strcmp(arg, "--reverse")) {
  2075. revs->reverse ^= 1;
  2076. } else if (!strcmp(arg, "--children")) {
  2077. revs->children.name = "children";
  2078. revs->limited = 1;
  2079. } else if (!strcmp(arg, "--ignore-missing")) {
  2080. revs->ignore_missing = 1;
  2081. } else if (opt && opt->allow_exclude_promisor_objects &&
  2082. !strcmp(arg, "--exclude-promisor-objects")) {
  2083. if (fetch_if_missing)
  2084. BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
  2085. revs->exclude_promisor_objects = 1;
  2086. } else {
  2087. int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
  2088. if (!opts)
  2089. unkv[(*unkc)++] = arg;
  2090. return opts;
  2091. }
  2092. if (revs->graph && revs->track_linear)
  2093. die("--show-linear-break and --graph are incompatible");
  2094. return 1;
  2095. }
  2096. void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
  2097. const struct option *options,
  2098. const char * const usagestr[])
  2099. {
  2100. int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
  2101. &ctx->cpidx, ctx->out, NULL);
  2102. if (n <= 0) {
  2103. error("unknown option `%s'", ctx->argv[0]);
  2104. usage_with_options(usagestr, options);
  2105. }
  2106. ctx->argv += n;
  2107. ctx->argc -= n;
  2108. }
  2109. static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
  2110. void *cb_data, const char *term)
  2111. {
  2112. struct strbuf bisect_refs = STRBUF_INIT;
  2113. int status;
  2114. strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
  2115. status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
  2116. strbuf_release(&bisect_refs);
  2117. return status;
  2118. }
  2119. static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
  2120. {
  2121. return for_each_bisect_ref(refs, fn, cb_data, term_bad);
  2122. }
  2123. static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
  2124. {
  2125. return for_each_bisect_ref(refs, fn, cb_data, term_good);
  2126. }
  2127. static int handle_revision_pseudo_opt(const char *submodule,
  2128. struct rev_info *revs,
  2129. int argc, const char **argv, int *flags)
  2130. {
  2131. const char *arg = argv[0];
  2132. const char *optarg;
  2133. struct ref_store *refs;
  2134. int argcount;
  2135. if (submodule) {
  2136. /*
  2137. * We need some something like get_submodule_worktrees()
  2138. * before we can go through all worktrees of a submodule,
  2139. * .e.g with adding all HEADs from --all, which is not
  2140. * supported right now, so stick to single worktree.
  2141. */
  2142. if (!revs->single_worktree)
  2143. BUG("--single-worktree cannot be used together with submodule");
  2144. refs = get_submodule_ref_store(submodule);
  2145. } else
  2146. refs = get_main_ref_store(revs->repo);
  2147. /*
  2148. * NOTE!
  2149. *
  2150. * Commands like "git shortlog" will not accept the options below
  2151. * unless parse_revision_opt queues them (as opposed to erroring
  2152. * out).
  2153. *
  2154. * When implementing your new pseudo-option, remember to
  2155. * register it in the list at the top of handle_revision_opt.
  2156. */
  2157. if (!strcmp(arg, "--all")) {
  2158. handle_refs(refs, revs, *flags, refs_for_each_ref);
  2159. handle_refs(refs, revs, *flags, refs_head_ref);
  2160. if (!revs->single_worktree) {
  2161. struct all_refs_cb cb;
  2162. init_all_refs_cb(&cb, revs, *flags);
  2163. other_head_refs(handle_one_ref, &cb);
  2164. }
  2165. clear_ref_exclusion(&revs->ref_excludes);
  2166. } else if (!strcmp(arg, "--branches")) {
  2167. handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
  2168. clear_ref_exclusion(&revs->ref_excludes);
  2169. } else if (!strcmp(arg, "--bisect")) {
  2170. read_bisect_terms(&term_bad, &term_good);
  2171. handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
  2172. handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
  2173. for_each_good_bisect_ref);
  2174. revs->bisect = 1;
  2175. } else if (!strcmp(arg, "--tags")) {
  2176. handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
  2177. clear_ref_exclusion(&revs->ref_excludes);
  2178. } else if (!strcmp(arg, "--remotes")) {
  2179. handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
  2180. clear_ref_exclusion(&revs->ref_excludes);
  2181. } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
  2182. struct all_refs_cb cb;
  2183. init_all_refs_cb(&cb, revs, *flags);
  2184. for_each_glob_ref(handle_one_ref, optarg, &cb);
  2185. clear_ref_exclusion(&revs->ref_excludes);
  2186. return argcount;
  2187. } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
  2188. add_ref_exclusion(&revs->ref_excludes, optarg);
  2189. return argcount;
  2190. } else if (skip_prefix(arg, "--branches=", &optarg)) {
  2191. struct all_refs_cb cb;
  2192. init_all_refs_cb(&cb, revs, *flags);
  2193. for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
  2194. clear_ref_exclusion(&revs->ref_excludes);
  2195. } else if (skip_prefix(arg, "--tags=", &optarg)) {
  2196. struct all_refs_cb cb;
  2197. init_all_refs_cb(&cb, revs, *flags);
  2198. for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
  2199. clear_ref_exclusion(&revs->ref_excludes);
  2200. } else if (skip_prefix(arg, "--remotes=", &optarg)) {
  2201. struct all_refs_cb cb;
  2202. init_all_refs_cb(&cb, revs, *flags);
  2203. for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
  2204. clear_ref_exclusion(&revs->ref_excludes);
  2205. } else if (!strcmp(arg, "--reflog")) {
  2206. add_reflogs_to_pending(revs, *flags);
  2207. } else if (!strcmp(arg, "--indexed-objects")) {
  2208. add_index_objects_to_pending(revs, *flags);
  2209. } else if (!strcmp(arg, "--alternate-refs")) {
  2210. add_alternate_refs_to_pending(revs, *flags);
  2211. } else if (!strcmp(arg, "--not")) {
  2212. *flags ^= UNINTERESTING | BOTTOM;
  2213. } else if (!strcmp(arg, "--no-walk")) {
  2214. revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
  2215. } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
  2216. /*
  2217. * Detached form ("--no-walk X" as opposed to "--no-walk=X")
  2218. * not allowed, since the argument is optional.
  2219. */
  2220. if (!strcmp(optarg, "sorted"))
  2221. revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
  2222. else if (!strcmp(optarg, "unsorted"))
  2223. revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
  2224. else
  2225. return error("invalid argument to --no-walk");
  2226. } else if (!strcmp(arg, "--do-walk")) {
  2227. revs->no_walk = 0;
  2228. } else if (!strcmp(arg, "--single-worktree")) {
  2229. revs->single_worktree = 1;
  2230. } else {
  2231. return 0;
  2232. }
  2233. return 1;
  2234. }
  2235. static void NORETURN diagnose_missing_default(const char *def)
  2236. {
  2237. int flags;
  2238. const char *refname;
  2239. refname = resolve_ref_unsafe(def, 0, NULL, &flags);
  2240. if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
  2241. die(_("your current branch appears to be broken"));
  2242. skip_prefix(refname, "refs/heads/", &refname);
  2243. die(_("your current branch '%s' does not have any commits yet"),
  2244. refname);
  2245. }
  2246. /*
  2247. * Parse revision information, filling in the "rev_info" structure,
  2248. * and removing the used arguments from the argument list.
  2249. *
  2250. * Returns the number of arguments left that weren't recognized
  2251. * (which are also moved to the head of the argument list)
  2252. */
  2253. int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
  2254. {
  2255. int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
  2256. struct argv_array prune_data = ARGV_ARRAY_INIT;
  2257. const char *submodule = NULL;
  2258. int seen_end_of_options = 0;
  2259. if (opt)
  2260. submodule = opt->submodule;
  2261. /* First, search for "--" */
  2262. if (opt && opt->assume_dashdash) {
  2263. seen_dashdash = 1;
  2264. } else {
  2265. seen_dashdash = 0;
  2266. for (i = 1; i < argc; i++) {
  2267. const char *arg = argv[i];
  2268. if (strcmp(arg, "--"))
  2269. continue;
  2270. argv[i] = NULL;
  2271. argc = i;
  2272. if (argv[i + 1])
  2273. argv_array_pushv(&prune_data, argv + i + 1);
  2274. seen_dashdash = 1;
  2275. break;
  2276. }
  2277. }
  2278. /* Second, deal with arguments and options */
  2279. flags = 0;
  2280. revarg_opt = opt ? opt->revarg_opt : 0;
  2281. if (seen_dashdash)
  2282. revarg_opt |= REVARG_CANNOT_BE_FILENAME;
  2283. for (left = i = 1; i < argc; i++) {
  2284. const char *arg = argv[i];
  2285. if (!seen_end_of_options && *arg == '-') {
  2286. int opts;
  2287. opts = handle_revision_pseudo_opt(submodule,
  2288. revs, argc - i, argv + i,
  2289. &flags);
  2290. if (opts > 0) {
  2291. i += opts - 1;
  2292. continue;
  2293. }
  2294. if (!strcmp(arg, "--stdin")) {
  2295. if (revs->disable_stdin) {
  2296. argv[left++] = arg;
  2297. continue;
  2298. }
  2299. if (revs->read_from_stdin++)
  2300. die("--stdin given twice?");
  2301. read_revisions_from_stdin(revs, &prune_data);
  2302. continue;
  2303. }
  2304. if (!strcmp(arg, "--end-of-options")) {
  2305. seen_end_of_options = 1;
  2306. continue;
  2307. }
  2308. opts = handle_revision_opt(revs, argc - i, argv + i,
  2309. &left, argv, opt);
  2310. if (opts > 0) {
  2311. i += opts - 1;
  2312. continue;
  2313. }
  2314. if (opts < 0)
  2315. exit(128);
  2316. continue;
  2317. }
  2318. if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
  2319. int j;
  2320. if (seen_dashdash || *arg == '^')
  2321. die("bad revision '%s'", arg);
  2322. /* If we didn't have a "--":
  2323. * (1) all filenames must exist;
  2324. * (2) all rev-args must not be interpretable
  2325. * as a valid filename.
  2326. * but the latter we have checked in the main loop.
  2327. */
  2328. for (j = i; j < argc; j++)
  2329. verify_filename(revs->prefix, argv[j], j == i);
  2330. argv_array_pushv(&prune_data, argv + i);
  2331. break;
  2332. }
  2333. else
  2334. got_rev_arg = 1;
  2335. }
  2336. if (prune_data.argc) {
  2337. /*
  2338. * If we need to introduce the magic "a lone ':' means no
  2339. * pathspec whatsoever", here is the place to do so.
  2340. *
  2341. * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
  2342. * prune_data.nr = 0;
  2343. * prune_data.alloc = 0;
  2344. * free(prune_data.path);
  2345. * prune_data.path = NULL;
  2346. * } else {
  2347. * terminate prune_data.alloc with NULL and
  2348. * call init_pathspec() to set revs->prune_data here.
  2349. * }
  2350. */
  2351. parse_pathspec(&revs->prune_data, 0, 0,
  2352. revs->prefix, prune_data.argv);
  2353. }
  2354. argv_array_clear(&prune_data);
  2355. if (revs->def == NULL)
  2356. revs->def = opt ? opt->def : NULL;
  2357. if (opt && opt->tweak)
  2358. opt->tweak(revs, opt);
  2359. if (revs->show_merge)
  2360. prepare_show_merge(revs);
  2361. if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
  2362. struct object_id oid;
  2363. struct object *object;
  2364. struct object_context oc;
  2365. if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
  2366. diagnose_missing_default(revs->def);
  2367. object = get_reference(revs, revs->def, &oid, 0);
  2368. add_pending_object_with_mode(revs, object, revs->def, oc.mode);
  2369. }
  2370. /* Did the user ask for any diff output? Run the diff! */
  2371. if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
  2372. revs->diff = 1;
  2373. /* Pickaxe, diff-filter and rename following need diffs */
  2374. if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
  2375. revs->diffopt.filter ||
  2376. revs->diffopt.flags.follow_renames)
  2377. revs->diff = 1;
  2378. if (revs->diffopt.objfind)
  2379. revs->simplify_history = 0;
  2380. if (revs->topo_order && !generation_numbers_enabled(the_repository))
  2381. revs->limited = 1;
  2382. if (revs->prune_data.nr) {
  2383. copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
  2384. /* Can't prune commits with rename following: the paths change.. */
  2385. if (!revs->diffopt.flags.follow_renames)
  2386. revs->prune = 1;
  2387. if (!revs->full_diff)
  2388. copy_pathspec(&revs->diffopt.pathspec,
  2389. &revs->prune_data);
  2390. }
  2391. if (revs->combine_merges)
  2392. revs->ignore_merges = 0;
  2393. if (revs->combined_all_paths && !revs->combine_merges)
  2394. die("--combined-all-paths makes no sense without -c or --cc");
  2395. revs->diffopt.abbrev = revs->abbrev;
  2396. if (revs->line_level_traverse) {
  2397. revs->limited = 1;
  2398. revs->topo_order = 1;
  2399. }
  2400. diff_setup_done(&revs->diffopt);
  2401. grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
  2402. &revs->grep_filter);
  2403. if (!is_encoding_utf8(get_log_output_encoding()))
  2404. revs->grep_filter.ignore_locale = 1;
  2405. compile_grep_patterns(&revs->grep_filter);
  2406. if (revs->reverse && revs->reflog_info)
  2407. die("cannot combine --reverse with --walk-reflogs");
  2408. if (revs->reflog_info && revs->limited)
  2409. die("cannot combine --walk-reflogs with history-limiting options");
  2410. if (revs->rewrite_parents && revs->children.name)
  2411. die("cannot combine --parents and --children");
  2412. /*
  2413. * Limitations on the graph functionality
  2414. */
  2415. if (revs->reverse && revs->graph)
  2416. die("cannot combine --reverse with --graph");
  2417. if (revs->reflog_info && revs->graph)
  2418. die("cannot combine --walk-reflogs with --graph");
  2419. if (revs->no_walk && revs->graph)
  2420. die("cannot combine --no-walk with --graph");
  2421. if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
  2422. die("cannot use --grep-reflog without --walk-reflogs");
  2423. if (revs->first_parent_only && revs->bisect)
  2424. die(_("--first-parent is incompatible with --bisect"));
  2425. if (revs->line_level_traverse &&
  2426. (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
  2427. die(_("-L does not yet support diff formats besides -p and -s"));
  2428. if (revs->expand_tabs_in_log < 0)
  2429. revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
  2430. return left;
  2431. }
  2432. static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
  2433. {
  2434. struct commit_list *l = xcalloc(1, sizeof(*l));
  2435. l->item = child;
  2436. l->next = add_decoration(&revs->children, &parent->object, l);
  2437. }
  2438. static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
  2439. {
  2440. struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
  2441. struct commit_list **pp, *p;
  2442. int surviving_parents;
  2443. /* Examine existing parents while marking ones we have seen... */
  2444. pp = &commit->parents;
  2445. surviving_parents = 0;
  2446. while ((p = *pp) != NULL) {
  2447. struct commit *parent = p->item;
  2448. if (parent->object.flags & TMP_MARK) {
  2449. *pp = p->next;
  2450. if (ts)
  2451. compact_treesame(revs, commit, surviving_parents);
  2452. continue;
  2453. }
  2454. parent->object.flags |= TMP_MARK;
  2455. surviving_parents++;
  2456. pp = &p->next;
  2457. }
  2458. /* clear the temporary mark */
  2459. for (p = commit->parents; p; p = p->next) {
  2460. p->item->object.flags &= ~TMP_MARK;
  2461. }
  2462. /* no update_treesame() - removing duplicates can't affect TREESAME */
  2463. return surviving_parents;
  2464. }
  2465. struct merge_simplify_state {
  2466. struct commit *simplified;
  2467. };
  2468. static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
  2469. {
  2470. struct merge_simplify_state *st;
  2471. st = lookup_decoration(&revs->merge_simplification, &commit->object);
  2472. if (!st) {
  2473. st = xcalloc(1, sizeof(*st));
  2474. add_decoration(&revs->merge_simplification, &commit->object, st);
  2475. }
  2476. return st;
  2477. }
  2478. static int mark_redundant_parents(struct commit *commit)
  2479. {
  2480. struct commit_list *h = reduce_heads(commit->parents);
  2481. int i = 0, marked = 0;
  2482. struct commit_list *po, *pn;
  2483. /* Want these for sanity-checking only */
  2484. int orig_cnt = commit_list_count(commit->parents);
  2485. int cnt = commit_list_count(h);
  2486. /*
  2487. * Not ready to remove items yet, just mark them for now, based
  2488. * on the output of reduce_heads(). reduce_heads outputs the reduced
  2489. * set in its original order, so this isn't too hard.
  2490. */
  2491. po = commit->parents;
  2492. pn = h;
  2493. while (po) {
  2494. if (pn && po->item == pn->item) {
  2495. pn = pn->next;
  2496. i++;
  2497. } else {
  2498. po->item->object.flags |= TMP_MARK;
  2499. marked++;
  2500. }
  2501. po=po->next;
  2502. }
  2503. if (i != cnt || cnt+marked != orig_cnt)
  2504. die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
  2505. free_commit_list(h);
  2506. return marked;
  2507. }
  2508. static int mark_treesame_root_parents(struct commit *commit)
  2509. {
  2510. struct commit_list *p;
  2511. int marked = 0;
  2512. for (p = commit->parents; p; p = p->next) {
  2513. struct commit *parent = p->item;
  2514. if (!parent->parents && (parent->object.flags & TREESAME)) {
  2515. parent->object.flags |= TMP_MARK;
  2516. marked++;
  2517. }
  2518. }
  2519. return marked;
  2520. }
  2521. /*
  2522. * Awkward naming - this means one parent we are TREESAME to.
  2523. * cf mark_treesame_root_parents: root parents that are TREESAME (to an
  2524. * empty tree). Better name suggestions?
  2525. */
  2526. static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
  2527. {
  2528. struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
  2529. struct commit *unmarked = NULL, *marked = NULL;
  2530. struct commit_list *p;
  2531. unsigned n;
  2532. for (p = commit->parents, n = 0; p; p = p->next, n++) {
  2533. if (ts->treesame[n]) {
  2534. if (p->item->object.flags & TMP_MARK) {
  2535. if (!marked)
  2536. marked = p->item;
  2537. } else {
  2538. if (!unmarked) {
  2539. unmarked = p->item;
  2540. break;
  2541. }
  2542. }
  2543. }
  2544. }
  2545. /*
  2546. * If we are TREESAME to a marked-for-deletion parent, but not to any
  2547. * unmarked parents, unmark the first TREESAME parent. This is the
  2548. * parent that the default simplify_history==1 scan would have followed,
  2549. * and it doesn't make sense to omit that path when asking for a
  2550. * simplified full history. Retaining it improves the chances of
  2551. * understanding odd missed merges that took an old version of a file.
  2552. *
  2553. * Example:
  2554. *
  2555. * I--------*X A modified the file, but mainline merge X used
  2556. * \ / "-s ours", so took the version from I. X is
  2557. * `-*A--' TREESAME to I and !TREESAME to A.
  2558. *
  2559. * Default log from X would produce "I". Without this check,
  2560. * --full-history --simplify-merges would produce "I-A-X", showing
  2561. * the merge commit X and that it changed A, but not making clear that
  2562. * it had just taken the I version. With this check, the topology above
  2563. * is retained.
  2564. *
  2565. * Note that it is possible that the simplification chooses a different
  2566. * TREESAME parent from the default, in which case this test doesn't
  2567. * activate, and we _do_ drop the default parent. Example:
  2568. *
  2569. * I------X A modified the file, but it was reverted in B,
  2570. * \ / meaning mainline merge X is TREESAME to both
  2571. * *A-*B parents.
  2572. *
  2573. * Default log would produce "I" by following the first parent;
  2574. * --full-history --simplify-merges will produce "I-A-B". But this is a
  2575. * reasonable result - it presents a logical full history leading from
  2576. * I to X, and X is not an important merge.
  2577. */
  2578. if (!unmarked && marked) {
  2579. marked->object.flags &= ~TMP_MARK;
  2580. return 1;
  2581. }
  2582. return 0;
  2583. }
  2584. static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
  2585. {
  2586. struct commit_list **pp, *p;
  2587. int nth_parent, removed = 0;
  2588. pp = &commit->parents;
  2589. nth_parent = 0;
  2590. while ((p = *pp) != NULL) {
  2591. struct commit *parent = p->item;
  2592. if (parent->object.flags & TMP_MARK) {
  2593. parent->object.flags &= ~TMP_MARK;
  2594. *pp = p->next;
  2595. free(p);
  2596. removed++;
  2597. compact_treesame(revs, commit, nth_parent);
  2598. continue;
  2599. }
  2600. pp = &p->next;
  2601. nth_parent++;
  2602. }
  2603. /* Removing parents can only increase TREESAMEness */
  2604. if (removed && !(commit->object.flags & TREESAME))
  2605. update_treesame(revs, commit);
  2606. return nth_parent;
  2607. }
  2608. static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
  2609. {
  2610. struct commit_list *p;
  2611. struct commit *parent;
  2612. struct merge_simplify_state *st, *pst;
  2613. int cnt;
  2614. st = locate_simplify_state(revs, commit);
  2615. /*
  2616. * Have we handled this one?
  2617. */
  2618. if (st->simplified)
  2619. return tail;
  2620. /*
  2621. * An UNINTERESTING commit simplifies to itself, so does a
  2622. * root commit. We do not rewrite parents of such commit
  2623. * anyway.
  2624. */
  2625. if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
  2626. st->simplified = commit;
  2627. return tail;
  2628. }
  2629. /*
  2630. * Do we know what commit all of our parents that matter
  2631. * should be rewritten to? Otherwise we are not ready to
  2632. * rewrite this one yet.
  2633. */
  2634. for (cnt = 0, p = commit->parents; p; p = p->next) {
  2635. pst = locate_simplify_state(revs, p->item);
  2636. if (!pst->simplified) {
  2637. tail = &commit_list_insert(p->item, tail)->next;
  2638. cnt++;
  2639. }
  2640. if (revs->first_parent_only)
  2641. break;
  2642. }
  2643. if (cnt) {
  2644. tail = &commit_list_insert(commit, tail)->next;
  2645. return tail;
  2646. }
  2647. /*
  2648. * Rewrite our list of parents. Note that this cannot
  2649. * affect our TREESAME flags in any way - a commit is
  2650. * always TREESAME to its simplification.
  2651. */
  2652. for (p = commit->parents; p; p = p->next) {
  2653. pst = locate_simplify_state(revs, p->item);
  2654. p->item = pst->simplified;
  2655. if (revs->first_parent_only)
  2656. break;
  2657. }
  2658. if (revs->first_parent_only)
  2659. cnt = 1;
  2660. else
  2661. cnt = remove_duplicate_parents(revs, commit);
  2662. /*
  2663. * It is possible that we are a merge and one side branch
  2664. * does not have any commit that touches the given paths;
  2665. * in such a case, the immediate parent from that branch
  2666. * will be rewritten to be the merge base.
  2667. *
  2668. * o----X X: the commit we are looking at;
  2669. * / / o: a commit that touches the paths;
  2670. * ---o----'
  2671. *
  2672. * Further, a merge of an independent branch that doesn't
  2673. * touch the path will reduce to a treesame root parent:
  2674. *
  2675. * ----o----X X: the commit we are looking at;
  2676. * / o: a commit that touches the paths;
  2677. * r r: a root commit not touching the paths
  2678. *
  2679. * Detect and simplify both cases.
  2680. */
  2681. if (1 < cnt) {
  2682. int marked = mark_redundant_parents(commit);
  2683. marked += mark_treesame_root_parents(commit);
  2684. if (marked)
  2685. marked -= leave_one_treesame_to_parent(revs, commit);
  2686. if (marked)
  2687. cnt = remove_marked_parents(revs, commit);
  2688. }
  2689. /*
  2690. * A commit simplifies to itself if it is a root, if it is
  2691. * UNINTERESTING, if it touches the given paths, or if it is a
  2692. * merge and its parents don't simplify to one relevant commit
  2693. * (the first two cases are already handled at the beginning of
  2694. * this function).
  2695. *
  2696. * Otherwise, it simplifies to what its sole relevant parent
  2697. * simplifies to.
  2698. */
  2699. if (!cnt ||
  2700. (commit->object.flags & UNINTERESTING) ||
  2701. !(commit->object.flags & TREESAME) ||
  2702. (parent = one_relevant_parent(revs, commit->parents)) == NULL)
  2703. st->simplified = commit;
  2704. else {
  2705. pst = locate_simplify_state(revs, parent);
  2706. st->simplified = pst->simplified;
  2707. }
  2708. return tail;
  2709. }
  2710. static void simplify_merges(struct rev_info *revs)
  2711. {
  2712. struct commit_list *list, *next;
  2713. struct commit_list *yet_to_do, **tail;
  2714. struct commit *commit;
  2715. if (!revs->prune)
  2716. return;
  2717. /* feed the list reversed */
  2718. yet_to_do = NULL;
  2719. for (list = revs->commits; list; list = next) {
  2720. commit = list->item;
  2721. next = list->next;
  2722. /*
  2723. * Do not free(list) here yet; the original list
  2724. * is used later in this function.
  2725. */
  2726. commit_list_insert(commit, &yet_to_do);
  2727. }
  2728. while (yet_to_do) {
  2729. list = yet_to_do;
  2730. yet_to_do = NULL;
  2731. tail = &yet_to_do;
  2732. while (list) {
  2733. commit = pop_commit(&list);
  2734. tail = simplify_one(revs, commit, tail);
  2735. }
  2736. }
  2737. /* clean up the result, removing the simplified ones */
  2738. list = revs->commits;
  2739. revs->commits = NULL;
  2740. tail = &revs->commits;
  2741. while (list) {
  2742. struct merge_simplify_state *st;
  2743. commit = pop_commit(&list);
  2744. st = locate_simplify_state(revs, commit);
  2745. if (st->simplified == commit)
  2746. tail = &commit_list_insert(commit, tail)->next;
  2747. }
  2748. }
  2749. static void set_children(struct rev_info *revs)
  2750. {
  2751. struct commit_list *l;
  2752. for (l = revs->commits; l; l = l->next) {
  2753. struct commit *commit = l->item;
  2754. struct commit_list *p;
  2755. for (p = commit->parents; p; p = p->next)
  2756. add_child(revs, p->item, commit);
  2757. }
  2758. }
  2759. void reset_revision_walk(void)
  2760. {
  2761. clear_object_flags(SEEN | ADDED | SHOWN);
  2762. }
  2763. static int mark_uninteresting(const struct object_id *oid,
  2764. struct packed_git *pack,
  2765. uint32_t pos,
  2766. void *cb)
  2767. {
  2768. struct rev_info *revs = cb;
  2769. struct object *o = parse_object(revs->repo, oid);
  2770. o->flags |= UNINTERESTING | SEEN;
  2771. return 0;
  2772. }
  2773. define_commit_slab(indegree_slab, int);
  2774. define_commit_slab(author_date_slab, timestamp_t);
  2775. struct topo_walk_info {
  2776. uint32_t min_generation;
  2777. struct prio_queue explore_queue;
  2778. struct prio_queue indegree_queue;
  2779. struct prio_queue topo_queue;
  2780. struct indegree_slab indegree;
  2781. struct author_date_slab author_date;
  2782. };
  2783. static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
  2784. {
  2785. if (c->object.flags & flag)
  2786. return;
  2787. c->object.flags |= flag;
  2788. prio_queue_put(q, c);
  2789. }
  2790. static void explore_walk_step(struct rev_info *revs)
  2791. {
  2792. struct topo_walk_info *info = revs->topo_walk_info;
  2793. struct commit_list *p;
  2794. struct commit *c = prio_queue_get(&info->explore_queue);
  2795. if (!c)
  2796. return;
  2797. if (parse_commit_gently(c, 1) < 0)
  2798. return;
  2799. if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
  2800. record_author_date(&info->author_date, c);
  2801. if (revs->max_age != -1 && (c->date < revs->max_age))
  2802. c->object.flags |= UNINTERESTING;
  2803. if (process_parents(revs, c, NULL, NULL) < 0)
  2804. return;
  2805. if (c->object.flags & UNINTERESTING)
  2806. mark_parents_uninteresting(c);
  2807. for (p = c->parents; p; p = p->next)
  2808. test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
  2809. }
  2810. static void explore_to_depth(struct rev_info *revs,
  2811. uint32_t gen_cutoff)
  2812. {
  2813. struct topo_walk_info *info = revs->topo_walk_info;
  2814. struct commit *c;
  2815. while ((c = prio_queue_peek(&info->explore_queue)) &&
  2816. c->generation >= gen_cutoff)
  2817. explore_walk_step(revs);
  2818. }
  2819. static void indegree_walk_step(struct rev_info *revs)
  2820. {
  2821. struct commit_list *p;
  2822. struct topo_walk_info *info = revs->topo_walk_info;
  2823. struct commit *c = prio_queue_get(&info->indegree_queue);
  2824. if (!c)
  2825. return;
  2826. if (parse_commit_gently(c, 1) < 0)
  2827. return;
  2828. explore_to_depth(revs, c->generation);
  2829. for (p = c->parents; p; p = p->next) {
  2830. struct commit *parent = p->item;
  2831. int *pi = indegree_slab_at(&info->indegree, parent);
  2832. if (*pi)
  2833. (*pi)++;
  2834. else
  2835. *pi = 2;
  2836. test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
  2837. if (revs->first_parent_only)
  2838. return;
  2839. }
  2840. }
  2841. static void compute_indegrees_to_depth(struct rev_info *revs,
  2842. uint32_t gen_cutoff)
  2843. {
  2844. struct topo_walk_info *info = revs->topo_walk_info;
  2845. struct commit *c;
  2846. while ((c = prio_queue_peek(&info->indegree_queue)) &&
  2847. c->generation >= gen_cutoff)
  2848. indegree_walk_step(revs);
  2849. }
  2850. static void init_topo_walk(struct rev_info *revs)
  2851. {
  2852. struct topo_walk_info *info;
  2853. struct commit_list *list;
  2854. revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
  2855. info = revs->topo_walk_info;
  2856. memset(info, 0, sizeof(struct topo_walk_info));
  2857. init_indegree_slab(&info->indegree);
  2858. memset(&info->explore_queue, 0, sizeof(info->explore_queue));
  2859. memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
  2860. memset(&info->topo_queue, 0, sizeof(info->topo_queue));
  2861. switch (revs->sort_order) {
  2862. default: /* REV_SORT_IN_GRAPH_ORDER */
  2863. info->topo_queue.compare = NULL;
  2864. break;
  2865. case REV_SORT_BY_COMMIT_DATE:
  2866. info->topo_queue.compare = compare_commits_by_commit_date;
  2867. break;
  2868. case REV_SORT_BY_AUTHOR_DATE:
  2869. init_author_date_slab(&info->author_date);
  2870. info->topo_queue.compare = compare_commits_by_author_date;
  2871. info->topo_queue.cb_data = &info->author_date;
  2872. break;
  2873. }
  2874. info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
  2875. info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
  2876. info->min_generation = GENERATION_NUMBER_INFINITY;
  2877. for (list = revs->commits; list; list = list->next) {
  2878. struct commit *c = list->item;
  2879. if (parse_commit_gently(c, 1))
  2880. continue;
  2881. test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
  2882. test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
  2883. if (c->generation < info->min_generation)
  2884. info->min_generation = c->generation;
  2885. *(indegree_slab_at(&info->indegree, c)) = 1;
  2886. if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
  2887. record_author_date(&info->author_date, c);
  2888. }
  2889. compute_indegrees_to_depth(revs, info->min_generation);
  2890. for (list = revs->commits; list; list = list->next) {
  2891. struct commit *c = list->item;
  2892. if (*(indegree_slab_at(&info->indegree, c)) == 1)
  2893. prio_queue_put(&info->topo_queue, c);
  2894. }
  2895. /*
  2896. * This is unfortunate; the initial tips need to be shown
  2897. * in the order given from the revision traversal machinery.
  2898. */
  2899. if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
  2900. prio_queue_reverse(&info->topo_queue);
  2901. }
  2902. static struct commit *next_topo_commit(struct rev_info *revs)
  2903. {
  2904. struct commit *c;
  2905. struct topo_walk_info *info = revs->topo_walk_info;
  2906. /* pop next off of topo_queue */
  2907. c = prio_queue_get(&info->topo_queue);
  2908. if (c)
  2909. *(indegree_slab_at(&info->indegree, c)) = 0;
  2910. return c;
  2911. }
  2912. static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
  2913. {
  2914. struct commit_list *p;
  2915. struct topo_walk_info *info = revs->topo_walk_info;
  2916. if (process_parents(revs, commit, NULL, NULL) < 0) {
  2917. if (!revs->ignore_missing_links)
  2918. die("Failed to traverse parents of commit %s",
  2919. oid_to_hex(&commit->object.oid));
  2920. }
  2921. for (p = commit->parents; p; p = p->next) {
  2922. struct commit *parent = p->item;
  2923. int *pi;
  2924. if (parent->object.flags & UNINTERESTING)
  2925. continue;
  2926. if (parse_commit_gently(parent, 1) < 0)
  2927. continue;
  2928. if (parent->generation < info->min_generation) {
  2929. info->min_generation = parent->generation;
  2930. compute_indegrees_to_depth(revs, info->min_generation);
  2931. }
  2932. pi = indegree_slab_at(&info->indegree, parent);
  2933. (*pi)--;
  2934. if (*pi == 1)
  2935. prio_queue_put(&info->topo_queue, parent);
  2936. if (revs->first_parent_only)
  2937. return;
  2938. }
  2939. }
  2940. int prepare_revision_walk(struct rev_info *revs)
  2941. {
  2942. int i;
  2943. struct object_array old_pending;
  2944. struct commit_list **next = &revs->commits;
  2945. memcpy(&old_pending, &revs->pending, sizeof(old_pending));
  2946. revs->pending.nr = 0;
  2947. revs->pending.alloc = 0;
  2948. revs->pending.objects = NULL;
  2949. for (i = 0; i < old_pending.nr; i++) {
  2950. struct object_array_entry *e = old_pending.objects + i;
  2951. struct commit *commit = handle_commit(revs, e);
  2952. if (commit) {
  2953. if (!(commit->object.flags & SEEN)) {
  2954. commit->object.flags |= SEEN;
  2955. next = commit_list_append(commit, next);
  2956. }
  2957. }
  2958. }
  2959. object_array_clear(&old_pending);
  2960. /* Signal whether we need per-parent treesame decoration */
  2961. if (revs->simplify_merges ||
  2962. (revs->limited && limiting_can_increase_treesame(revs)))
  2963. revs->treesame.name = "treesame";
  2964. if (revs->exclude_promisor_objects) {
  2965. for_each_packed_object(mark_uninteresting, revs,
  2966. FOR_EACH_OBJECT_PROMISOR_ONLY);
  2967. }
  2968. if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
  2969. commit_list_sort_by_date(&revs->commits);
  2970. if (revs->no_walk)
  2971. return 0;
  2972. if (revs->limited) {
  2973. if (limit_list(revs) < 0)
  2974. return -1;
  2975. if (revs->topo_order)
  2976. sort_in_topological_order(&revs->commits, revs->sort_order);
  2977. } else if (revs->topo_order)
  2978. init_topo_walk(revs);
  2979. if (revs->line_level_traverse)
  2980. line_log_filter(revs);
  2981. if (revs->simplify_merges)
  2982. simplify_merges(revs);
  2983. if (revs->children.name)
  2984. set_children(revs);
  2985. return 0;
  2986. }
  2987. static enum rewrite_result rewrite_one_1(struct rev_info *revs,
  2988. struct commit **pp,
  2989. struct prio_queue *queue)
  2990. {
  2991. for (;;) {
  2992. struct commit *p = *pp;
  2993. if (!revs->limited)
  2994. if (process_parents(revs, p, NULL, queue) < 0)
  2995. return rewrite_one_error;
  2996. if (p->object.flags & UNINTERESTING)
  2997. return rewrite_one_ok;
  2998. if (!(p->object.flags & TREESAME))
  2999. return rewrite_one_ok;
  3000. if (!p->parents)
  3001. return rewrite_one_noparents;
  3002. if ((p = one_relevant_parent(revs, p->parents)) == NULL)
  3003. return rewrite_one_ok;
  3004. *pp = p;
  3005. }
  3006. }
  3007. static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
  3008. {
  3009. while (q->nr) {
  3010. struct commit *item = prio_queue_peek(q);
  3011. struct commit_list *p = *list;
  3012. if (p && p->item->date >= item->date)
  3013. list = &p->next;
  3014. else {
  3015. p = commit_list_insert(item, list);
  3016. list = &p->next; /* skip newly added item */
  3017. prio_queue_get(q); /* pop item */
  3018. }
  3019. }
  3020. }
  3021. static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
  3022. {
  3023. struct prio_queue queue = { compare_commits_by_commit_date };
  3024. enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
  3025. merge_queue_into_list(&queue, &revs->commits);
  3026. clear_prio_queue(&queue);
  3027. return ret;
  3028. }
  3029. int rewrite_parents(struct rev_info *revs, struct commit *commit,
  3030. rewrite_parent_fn_t rewrite_parent)
  3031. {
  3032. struct commit_list **pp = &commit->parents;
  3033. while (*pp) {
  3034. struct commit_list *parent = *pp;
  3035. switch (rewrite_parent(revs, &parent->item)) {
  3036. case rewrite_one_ok:
  3037. break;
  3038. case rewrite_one_noparents:
  3039. *pp = parent->next;
  3040. continue;
  3041. case rewrite_one_error:
  3042. return -1;
  3043. }
  3044. pp = &parent->next;
  3045. }
  3046. remove_duplicate_parents(revs, commit);
  3047. return 0;
  3048. }
  3049. static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
  3050. {
  3051. char *person, *endp;
  3052. size_t len, namelen, maillen;
  3053. const char *name;
  3054. const char *mail;
  3055. struct ident_split ident;
  3056. person = strstr(buf->buf, what);
  3057. if (!person)
  3058. return 0;
  3059. person += strlen(what);
  3060. endp = strchr(person, '\n');
  3061. if (!endp)
  3062. return 0;
  3063. len = endp - person;
  3064. if (split_ident_line(&ident, person, len))
  3065. return 0;
  3066. mail = ident.mail_begin;
  3067. maillen = ident.mail_end - ident.mail_begin;
  3068. name = ident.name_begin;
  3069. namelen = ident.name_end - ident.name_begin;
  3070. if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
  3071. struct strbuf namemail = STRBUF_INIT;
  3072. strbuf_addf(&namemail, "%.*s <%.*s>",
  3073. (int)namelen, name, (int)maillen, mail);
  3074. strbuf_splice(buf, ident.name_begin - buf->buf,
  3075. ident.mail_end - ident.name_begin + 1,
  3076. namemail.buf, namemail.len);
  3077. strbuf_release(&namemail);
  3078. return 1;
  3079. }
  3080. return 0;
  3081. }
  3082. static int commit_match(struct commit *commit, struct rev_info *opt)
  3083. {
  3084. int retval;
  3085. const char *encoding;
  3086. const char *message;
  3087. struct strbuf buf = STRBUF_INIT;
  3088. if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
  3089. return 1;
  3090. /* Prepend "fake" headers as needed */
  3091. if (opt->grep_filter.use_reflog_filter) {
  3092. strbuf_addstr(&buf, "reflog ");
  3093. get_reflog_message(&buf, opt->reflog_info);
  3094. strbuf_addch(&buf, '\n');
  3095. }
  3096. /*
  3097. * We grep in the user's output encoding, under the assumption that it
  3098. * is the encoding they are most likely to write their grep pattern
  3099. * for. In addition, it means we will match the "notes" encoding below,
  3100. * so we will not end up with a buffer that has two different encodings
  3101. * in it.
  3102. */
  3103. encoding = get_log_output_encoding();
  3104. message = logmsg_reencode(commit, NULL, encoding);
  3105. /* Copy the commit to temporary if we are using "fake" headers */
  3106. if (buf.len)
  3107. strbuf_addstr(&buf, message);
  3108. if (opt->grep_filter.header_list && opt->mailmap) {
  3109. if (!buf.len)
  3110. strbuf_addstr(&buf, message);
  3111. commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
  3112. commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
  3113. }
  3114. /* Append "fake" message parts as needed */
  3115. if (opt->show_notes) {
  3116. if (!buf.len)
  3117. strbuf_addstr(&buf, message);
  3118. format_display_notes(&commit->object.oid, &buf, encoding, 1);
  3119. }
  3120. /*
  3121. * Find either in the original commit message, or in the temporary.
  3122. * Note that we cast away the constness of "message" here. It is
  3123. * const because it may come from the cached commit buffer. That's OK,
  3124. * because we know that it is modifiable heap memory, and that while
  3125. * grep_buffer may modify it for speed, it will restore any
  3126. * changes before returning.
  3127. */
  3128. if (buf.len)
  3129. retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
  3130. else
  3131. retval = grep_buffer(&opt->grep_filter,
  3132. (char *)message, strlen(message));
  3133. strbuf_release(&buf);
  3134. unuse_commit_buffer(commit, message);
  3135. return opt->invert_grep ? !retval : retval;
  3136. }
  3137. static inline int want_ancestry(const struct rev_info *revs)
  3138. {
  3139. return (revs->rewrite_parents || revs->children.name);
  3140. }
  3141. /*
  3142. * Return a timestamp to be used for --since/--until comparisons for this
  3143. * commit, based on the revision options.
  3144. */
  3145. static timestamp_t comparison_date(const struct rev_info *revs,
  3146. struct commit *commit)
  3147. {
  3148. return revs->reflog_info ?
  3149. get_reflog_timestamp(revs->reflog_info) :
  3150. commit->date;
  3151. }
  3152. enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
  3153. {
  3154. if (commit->object.flags & SHOWN)
  3155. return commit_ignore;
  3156. if (revs->unpacked && has_object_pack(&commit->object.oid))
  3157. return commit_ignore;
  3158. if (commit->object.flags & UNINTERESTING)
  3159. return commit_ignore;
  3160. if (revs->min_age != -1 &&
  3161. comparison_date(revs, commit) > revs->min_age)
  3162. return commit_ignore;
  3163. if (revs->min_parents || (revs->max_parents >= 0)) {
  3164. int n = commit_list_count(commit->parents);
  3165. if ((n < revs->min_parents) ||
  3166. ((revs->max_parents >= 0) && (n > revs->max_parents)))
  3167. return commit_ignore;
  3168. }
  3169. if (!commit_match(commit, revs))
  3170. return commit_ignore;
  3171. if (revs->prune && revs->dense) {
  3172. /* Commit without changes? */
  3173. if (commit->object.flags & TREESAME) {
  3174. int n;
  3175. struct commit_list *p;
  3176. /* drop merges unless we want parenthood */
  3177. if (!want_ancestry(revs))
  3178. return commit_ignore;
  3179. /*
  3180. * If we want ancestry, then need to keep any merges
  3181. * between relevant commits to tie together topology.
  3182. * For consistency with TREESAME and simplification
  3183. * use "relevant" here rather than just INTERESTING,
  3184. * to treat bottom commit(s) as part of the topology.
  3185. */
  3186. for (n = 0, p = commit->parents; p; p = p->next)
  3187. if (relevant_commit(p->item))
  3188. if (++n >= 2)
  3189. return commit_show;
  3190. return commit_ignore;
  3191. }
  3192. }
  3193. return commit_show;
  3194. }
  3195. define_commit_slab(saved_parents, struct commit_list *);
  3196. #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
  3197. /*
  3198. * You may only call save_parents() once per commit (this is checked
  3199. * for non-root commits).
  3200. */
  3201. static void save_parents(struct rev_info *revs, struct commit *commit)
  3202. {
  3203. struct commit_list **pp;
  3204. if (!revs->saved_parents_slab) {
  3205. revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
  3206. init_saved_parents(revs->saved_parents_slab);
  3207. }
  3208. pp = saved_parents_at(revs->saved_parents_slab, commit);
  3209. /*
  3210. * When walking with reflogs, we may visit the same commit
  3211. * several times: once for each appearance in the reflog.
  3212. *
  3213. * In this case, save_parents() will be called multiple times.
  3214. * We want to keep only the first set of parents. We need to
  3215. * store a sentinel value for an empty (i.e., NULL) parent
  3216. * list to distinguish it from a not-yet-saved list, however.
  3217. */
  3218. if (*pp)
  3219. return;
  3220. if (commit->parents)
  3221. *pp = copy_commit_list(commit->parents);
  3222. else
  3223. *pp = EMPTY_PARENT_LIST;
  3224. }
  3225. static void free_saved_parents(struct rev_info *revs)
  3226. {
  3227. if (revs->saved_parents_slab)
  3228. clear_saved_parents(revs->saved_parents_slab);
  3229. }
  3230. struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
  3231. {
  3232. struct commit_list *parents;
  3233. if (!revs->saved_parents_slab)
  3234. return commit->parents;
  3235. parents = *saved_parents_at(revs->saved_parents_slab, commit);
  3236. if (parents == EMPTY_PARENT_LIST)
  3237. return NULL;
  3238. return parents;
  3239. }
  3240. enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
  3241. {
  3242. enum commit_action action = get_commit_action(revs, commit);
  3243. if (action == commit_show &&
  3244. revs->prune && revs->dense && want_ancestry(revs)) {
  3245. /*
  3246. * --full-diff on simplified parents is no good: it
  3247. * will show spurious changes from the commits that
  3248. * were elided. So we save the parents on the side
  3249. * when --full-diff is in effect.
  3250. */
  3251. if (revs->full_diff)
  3252. save_parents(revs, commit);
  3253. if (rewrite_parents(revs, commit, rewrite_one) < 0)
  3254. return commit_error;
  3255. }
  3256. return action;
  3257. }
  3258. static void track_linear(struct rev_info *revs, struct commit *commit)
  3259. {
  3260. if (revs->track_first_time) {
  3261. revs->linear = 1;
  3262. revs->track_first_time = 0;
  3263. } else {
  3264. struct commit_list *p;
  3265. for (p = revs->previous_parents; p; p = p->next)
  3266. if (p->item == NULL || /* first commit */
  3267. oideq(&p->item->object.oid, &commit->object.oid))
  3268. break;
  3269. revs->linear = p != NULL;
  3270. }
  3271. if (revs->reverse) {
  3272. if (revs->linear)
  3273. commit->object.flags |= TRACK_LINEAR;
  3274. }
  3275. free_commit_list(revs->previous_parents);
  3276. revs->previous_parents = copy_commit_list(commit->parents);
  3277. }
  3278. static struct commit *get_revision_1(struct rev_info *revs)
  3279. {
  3280. while (1) {
  3281. struct commit *commit;
  3282. if (revs->reflog_info)
  3283. commit = next_reflog_entry(revs->reflog_info);
  3284. else if (revs->topo_walk_info)
  3285. commit = next_topo_commit(revs);
  3286. else
  3287. commit = pop_commit(&revs->commits);
  3288. if (!commit)
  3289. return NULL;
  3290. if (revs->reflog_info)
  3291. commit->object.flags &= ~(ADDED | SEEN | SHOWN);
  3292. /*
  3293. * If we haven't done the list limiting, we need to look at
  3294. * the parents here. We also need to do the date-based limiting
  3295. * that we'd otherwise have done in limit_list().
  3296. */
  3297. if (!revs->limited) {
  3298. if (revs->max_age != -1 &&
  3299. comparison_date(revs, commit) < revs->max_age)
  3300. continue;
  3301. if (revs->reflog_info)
  3302. try_to_simplify_commit(revs, commit);
  3303. else if (revs->topo_walk_info)
  3304. expand_topo_walk(revs, commit);
  3305. else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
  3306. if (!revs->ignore_missing_links)
  3307. die("Failed to traverse parents of commit %s",
  3308. oid_to_hex(&commit->object.oid));
  3309. }
  3310. }
  3311. switch (simplify_commit(revs, commit)) {
  3312. case commit_ignore:
  3313. continue;
  3314. case commit_error:
  3315. die("Failed to simplify parents of commit %s",
  3316. oid_to_hex(&commit->object.oid));
  3317. default:
  3318. if (revs->track_linear)
  3319. track_linear(revs, commit);
  3320. return commit;
  3321. }
  3322. }
  3323. }
  3324. /*
  3325. * Return true for entries that have not yet been shown. (This is an
  3326. * object_array_each_func_t.)
  3327. */
  3328. static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
  3329. {
  3330. return !(entry->item->flags & SHOWN);
  3331. }
  3332. /*
  3333. * If array is on the verge of a realloc, garbage-collect any entries
  3334. * that have already been shown to try to free up some space.
  3335. */
  3336. static void gc_boundary(struct object_array *array)
  3337. {
  3338. if (array->nr == array->alloc)
  3339. object_array_filter(array, entry_unshown, NULL);
  3340. }
  3341. static void create_boundary_commit_list(struct rev_info *revs)
  3342. {
  3343. unsigned i;
  3344. struct commit *c;
  3345. struct object_array *array = &revs->boundary_commits;
  3346. struct object_array_entry *objects = array->objects;
  3347. /*
  3348. * If revs->commits is non-NULL at this point, an error occurred in
  3349. * get_revision_1(). Ignore the error and continue printing the
  3350. * boundary commits anyway. (This is what the code has always
  3351. * done.)
  3352. */
  3353. if (revs->commits) {
  3354. free_commit_list(revs->commits);
  3355. revs->commits = NULL;
  3356. }
  3357. /*
  3358. * Put all of the actual boundary commits from revs->boundary_commits
  3359. * into revs->commits
  3360. */
  3361. for (i = 0; i < array->nr; i++) {
  3362. c = (struct commit *)(objects[i].item);
  3363. if (!c)
  3364. continue;
  3365. if (!(c->object.flags & CHILD_SHOWN))
  3366. continue;
  3367. if (c->object.flags & (SHOWN | BOUNDARY))
  3368. continue;
  3369. c->object.flags |= BOUNDARY;
  3370. commit_list_insert(c, &revs->commits);
  3371. }
  3372. /*
  3373. * If revs->topo_order is set, sort the boundary commits
  3374. * in topological order
  3375. */
  3376. sort_in_topological_order(&revs->commits, revs->sort_order);
  3377. }
  3378. static struct commit *get_revision_internal(struct rev_info *revs)
  3379. {
  3380. struct commit *c = NULL;
  3381. struct commit_list *l;
  3382. if (revs->boundary == 2) {
  3383. /*
  3384. * All of the normal commits have already been returned,
  3385. * and we are now returning boundary commits.
  3386. * create_boundary_commit_list() has populated
  3387. * revs->commits with the remaining commits to return.
  3388. */
  3389. c = pop_commit(&revs->commits);
  3390. if (c)
  3391. c->object.flags |= SHOWN;
  3392. return c;
  3393. }
  3394. /*
  3395. * If our max_count counter has reached zero, then we are done. We
  3396. * don't simply return NULL because we still might need to show
  3397. * boundary commits. But we want to avoid calling get_revision_1, which
  3398. * might do a considerable amount of work finding the next commit only
  3399. * for us to throw it away.
  3400. *
  3401. * If it is non-zero, then either we don't have a max_count at all
  3402. * (-1), or it is still counting, in which case we decrement.
  3403. */
  3404. if (revs->max_count) {
  3405. c = get_revision_1(revs);
  3406. if (c) {
  3407. while (revs->skip_count > 0) {
  3408. revs->skip_count--;
  3409. c = get_revision_1(revs);
  3410. if (!c)
  3411. break;
  3412. }
  3413. }
  3414. if (revs->max_count > 0)
  3415. revs->max_count--;
  3416. }
  3417. if (c)
  3418. c->object.flags |= SHOWN;
  3419. if (!revs->boundary)
  3420. return c;
  3421. if (!c) {
  3422. /*
  3423. * get_revision_1() runs out the commits, and
  3424. * we are done computing the boundaries.
  3425. * switch to boundary commits output mode.
  3426. */
  3427. revs->boundary = 2;
  3428. /*
  3429. * Update revs->commits to contain the list of
  3430. * boundary commits.
  3431. */
  3432. create_boundary_commit_list(revs);
  3433. return get_revision_internal(revs);
  3434. }
  3435. /*
  3436. * boundary commits are the commits that are parents of the
  3437. * ones we got from get_revision_1() but they themselves are
  3438. * not returned from get_revision_1(). Before returning
  3439. * 'c', we need to mark its parents that they could be boundaries.
  3440. */
  3441. for (l = c->parents; l; l = l->next) {
  3442. struct object *p;
  3443. p = &(l->item->object);
  3444. if (p->flags & (CHILD_SHOWN | SHOWN))
  3445. continue;
  3446. p->flags |= CHILD_SHOWN;
  3447. gc_boundary(&revs->boundary_commits);
  3448. add_object_array(p, NULL, &revs->boundary_commits);
  3449. }
  3450. return c;
  3451. }
  3452. struct commit *get_revision(struct rev_info *revs)
  3453. {
  3454. struct commit *c;
  3455. struct commit_list *reversed;
  3456. if (revs->reverse) {
  3457. reversed = NULL;
  3458. while ((c = get_revision_internal(revs)))
  3459. commit_list_insert(c, &reversed);
  3460. revs->commits = reversed;
  3461. revs->reverse = 0;
  3462. revs->reverse_output_stage = 1;
  3463. }
  3464. if (revs->reverse_output_stage) {
  3465. c = pop_commit(&revs->commits);
  3466. if (revs->track_linear)
  3467. revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
  3468. return c;
  3469. }
  3470. c = get_revision_internal(revs);
  3471. if (c && revs->graph)
  3472. graph_update(revs->graph, c);
  3473. if (!c) {
  3474. free_saved_parents(revs);
  3475. if (revs->previous_parents) {
  3476. free_commit_list(revs->previous_parents);
  3477. revs->previous_parents = NULL;
  3478. }
  3479. }
  3480. return c;
  3481. }
  3482. char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
  3483. {
  3484. if (commit->object.flags & BOUNDARY)
  3485. return "-";
  3486. else if (commit->object.flags & UNINTERESTING)
  3487. return "^";
  3488. else if (commit->object.flags & PATCHSAME)
  3489. return "=";
  3490. else if (!revs || revs->left_right) {
  3491. if (commit->object.flags & SYMMETRIC_LEFT)
  3492. return "<";
  3493. else
  3494. return ">";
  3495. } else if (revs->graph)
  3496. return "*";
  3497. else if (revs->cherry_mark)
  3498. return "+";
  3499. return "";
  3500. }
  3501. void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
  3502. {
  3503. char *mark = get_revision_mark(revs, commit);
  3504. if (!strlen(mark))
  3505. return;
  3506. fputs(mark, stdout);
  3507. putchar(' ');
  3508. }