git
/
revision.c
4520 строк · 125.0 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "config.h"
5#include "environment.h"
6#include "gettext.h"
7#include "hex.h"
8#include "object-name.h"
9#include "object-file.h"
10#include "object-store-ll.h"
11#include "oidset.h"
12#include "tag.h"
13#include "blob.h"
14#include "tree.h"
15#include "commit.h"
16#include "diff.h"
17#include "diff-merges.h"
18#include "refs.h"
19#include "revision.h"
20#include "repository.h"
21#include "graph.h"
22#include "grep.h"
23#include "reflog-walk.h"
24#include "patch-ids.h"
25#include "decorate.h"
26#include "string-list.h"
27#include "line-log.h"
28#include "mailmap.h"
29#include "commit-slab.h"
30#include "cache-tree.h"
31#include "bisect.h"
32#include "packfile.h"
33#include "worktree.h"
34#include "path.h"
35#include "read-cache.h"
36#include "setup.h"
37#include "sparse-index.h"
38#include "strvec.h"
39#include "trace2.h"
40#include "commit-reach.h"
41#include "commit-graph.h"
42#include "prio-queue.h"
43#include "hashmap.h"
44#include "utf8.h"
45#include "bloom.h"
46#include "json-writer.h"
47#include "list-objects-filter-options.h"
48#include "resolve-undo.h"
49#include "parse-options.h"
50#include "wildmatch.h"
51
52volatile show_early_output_fn_t show_early_output;
53
54static const char *term_bad;
55static const char *term_good;
56
57implement_shared_commit_slab(revision_sources, char *);
58
59static inline int want_ancestry(const struct rev_info *revs);
60
61void show_object_with_name(FILE *out, struct object *obj, const char *name)
62{
63fprintf(out, "%s ", oid_to_hex(&obj->oid));
64for (const char *p = name; *p && *p != '\n'; p++)
65fputc(*p, out);
66fputc('\n', out);
67}
68
69static void mark_blob_uninteresting(struct blob *blob)
70{
71if (!blob)
72return;
73if (blob->object.flags & UNINTERESTING)
74return;
75blob->object.flags |= UNINTERESTING;
76}
77
78static void mark_tree_contents_uninteresting(struct repository *r,
79struct tree *tree)
80{
81struct tree_desc desc;
82struct name_entry entry;
83
84if (parse_tree_gently(tree, 1) < 0)
85return;
86
87init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
88while (tree_entry(&desc, &entry)) {
89switch (object_type(entry.mode)) {
90case OBJ_TREE:
91mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
92break;
93case OBJ_BLOB:
94mark_blob_uninteresting(lookup_blob(r, &entry.oid));
95break;
96default:
97/* Subproject commit - not in this repository */
98break;
99}
100}
101
102/*
103* We don't care about the tree any more
104* after it has been marked uninteresting.
105*/
106free_tree_buffer(tree);
107}
108
109void mark_tree_uninteresting(struct repository *r, struct tree *tree)
110{
111struct object *obj;
112
113if (!tree)
114return;
115
116obj = &tree->object;
117if (obj->flags & UNINTERESTING)
118return;
119obj->flags |= UNINTERESTING;
120mark_tree_contents_uninteresting(r, tree);
121}
122
123struct path_and_oids_entry {
124struct hashmap_entry ent;
125char *path;
126struct oidset trees;
127};
128
129static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
130const struct hashmap_entry *eptr,
131const struct hashmap_entry *entry_or_key,
132const void *keydata UNUSED)
133{
134const struct path_and_oids_entry *e1, *e2;
135
136e1 = container_of(eptr, const struct path_and_oids_entry, ent);
137e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
138
139return strcmp(e1->path, e2->path);
140}
141
142static void paths_and_oids_clear(struct hashmap *map)
143{
144struct hashmap_iter iter;
145struct path_and_oids_entry *entry;
146
147hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
148oidset_clear(&entry->trees);
149free(entry->path);
150}
151
152hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
153}
154
155static void paths_and_oids_insert(struct hashmap *map,
156const char *path,
157const struct object_id *oid)
158{
159int hash = strhash(path);
160struct path_and_oids_entry key;
161struct path_and_oids_entry *entry;
162
163hashmap_entry_init(&key.ent, hash);
164
165/* use a shallow copy for the lookup */
166key.path = (char *)path;
167oidset_init(&key.trees, 0);
168
169entry = hashmap_get_entry(map, &key, ent, NULL);
170if (!entry) {
171CALLOC_ARRAY(entry, 1);
172hashmap_entry_init(&entry->ent, hash);
173entry->path = xstrdup(key.path);
174oidset_init(&entry->trees, 16);
175hashmap_put(map, &entry->ent);
176}
177
178oidset_insert(&entry->trees, oid);
179}
180
181static void add_children_by_path(struct repository *r,
182struct tree *tree,
183struct hashmap *map)
184{
185struct tree_desc desc;
186struct name_entry entry;
187
188if (!tree)
189return;
190
191if (parse_tree_gently(tree, 1) < 0)
192return;
193
194init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
195while (tree_entry(&desc, &entry)) {
196switch (object_type(entry.mode)) {
197case OBJ_TREE:
198paths_and_oids_insert(map, entry.path, &entry.oid);
199
200if (tree->object.flags & UNINTERESTING) {
201struct tree *child = lookup_tree(r, &entry.oid);
202if (child)
203child->object.flags |= UNINTERESTING;
204}
205break;
206case OBJ_BLOB:
207if (tree->object.flags & UNINTERESTING) {
208struct blob *child = lookup_blob(r, &entry.oid);
209if (child)
210child->object.flags |= UNINTERESTING;
211}
212break;
213default:
214/* Subproject commit - not in this repository */
215break;
216}
217}
218
219free_tree_buffer(tree);
220}
221
222void mark_trees_uninteresting_sparse(struct repository *r,
223struct oidset *trees)
224{
225unsigned has_interesting = 0, has_uninteresting = 0;
226struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
227struct hashmap_iter map_iter;
228struct path_and_oids_entry *entry;
229struct object_id *oid;
230struct oidset_iter iter;
231
232oidset_iter_init(trees, &iter);
233while ((!has_interesting || !has_uninteresting) &&
234(oid = oidset_iter_next(&iter))) {
235struct tree *tree = lookup_tree(r, oid);
236
237if (!tree)
238continue;
239
240if (tree->object.flags & UNINTERESTING)
241has_uninteresting = 1;
242else
243has_interesting = 1;
244}
245
246/* Do not walk unless we have both types of trees. */
247if (!has_uninteresting || !has_interesting)
248return;
249
250oidset_iter_init(trees, &iter);
251while ((oid = oidset_iter_next(&iter))) {
252struct tree *tree = lookup_tree(r, oid);
253add_children_by_path(r, tree, &map);
254}
255
256hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
257mark_trees_uninteresting_sparse(r, &entry->trees);
258
259paths_and_oids_clear(&map);
260}
261
262struct commit_stack {
263struct commit **items;
264size_t nr, alloc;
265};
266#define COMMIT_STACK_INIT { 0 }
267
268static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
269{
270ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
271stack->items[stack->nr++] = commit;
272}
273
274static struct commit *commit_stack_pop(struct commit_stack *stack)
275{
276return stack->nr ? stack->items[--stack->nr] : NULL;
277}
278
279static void commit_stack_clear(struct commit_stack *stack)
280{
281FREE_AND_NULL(stack->items);
282stack->nr = stack->alloc = 0;
283}
284
285static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
286struct commit_stack *pending)
287{
288struct commit_list *l;
289
290if (commit->object.flags & UNINTERESTING)
291return;
292commit->object.flags |= UNINTERESTING;
293
294/*
295* Normally we haven't parsed the parent
296* yet, so we won't have a parent of a parent
297* here. However, it may turn out that we've
298* reached this commit some other way (where it
299* wasn't uninteresting), in which case we need
300* to mark its parents recursively too..
301*/
302for (l = commit->parents; l; l = l->next) {
303commit_stack_push(pending, l->item);
304if (revs && revs->exclude_first_parent_only)
305break;
306}
307}
308
309void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
310{
311struct commit_stack pending = COMMIT_STACK_INIT;
312struct commit_list *l;
313
314for (l = commit->parents; l; l = l->next) {
315mark_one_parent_uninteresting(revs, l->item, &pending);
316if (revs && revs->exclude_first_parent_only)
317break;
318}
319
320while (pending.nr > 0)
321mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
322&pending);
323
324commit_stack_clear(&pending);
325}
326
327static void add_pending_object_with_path(struct rev_info *revs,
328struct object *obj,
329const char *name, unsigned mode,
330const char *path)
331{
332struct interpret_branch_name_options options = { 0 };
333if (!obj)
334return;
335if (revs->no_walk && (obj->flags & UNINTERESTING))
336revs->no_walk = 0;
337if (revs->reflog_info && obj->type == OBJ_COMMIT) {
338struct strbuf buf = STRBUF_INIT;
339size_t namelen = strlen(name);
340int len = repo_interpret_branch_name(the_repository, name,
341namelen, &buf, &options);
342
343if (0 < len && len < namelen && buf.len)
344strbuf_addstr(&buf, name + len);
345add_reflog_for_walk(revs->reflog_info,
346(struct commit *)obj,
347buf.buf[0] ? buf.buf: name);
348strbuf_release(&buf);
349return; /* do not add the commit itself */
350}
351add_object_array_with_path(obj, name, &revs->pending, mode, path);
352}
353
354static void add_pending_object_with_mode(struct rev_info *revs,
355struct object *obj,
356const char *name, unsigned mode)
357{
358add_pending_object_with_path(revs, obj, name, mode, NULL);
359}
360
361void add_pending_object(struct rev_info *revs,
362struct object *obj, const char *name)
363{
364add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
365}
366
367void add_head_to_pending(struct rev_info *revs)
368{
369struct object_id oid;
370struct object *obj;
371if (repo_get_oid(the_repository, "HEAD", &oid))
372return;
373obj = parse_object(revs->repo, &oid);
374if (!obj)
375return;
376add_pending_object(revs, obj, "HEAD");
377}
378
379static struct object *get_reference(struct rev_info *revs, const char *name,
380const struct object_id *oid,
381unsigned int flags)
382{
383struct object *object;
384
385object = parse_object_with_flags(revs->repo, oid,
386revs->verify_objects ? 0 :
387PARSE_OBJECT_SKIP_HASH_CHECK |
388PARSE_OBJECT_DISCARD_TREE);
389
390if (!object) {
391if (revs->ignore_missing)
392return NULL;
393if (revs->exclude_promisor_objects && is_promisor_object(oid))
394return NULL;
395if (revs->do_not_die_on_missing_objects) {
396oidset_insert(&revs->missing_commits, oid);
397return NULL;
398}
399die("bad object %s", name);
400}
401object->flags |= flags;
402return object;
403}
404
405void add_pending_oid(struct rev_info *revs, const char *name,
406const struct object_id *oid, unsigned int flags)
407{
408struct object *object = get_reference(revs, name, oid, flags);
409add_pending_object(revs, object, name);
410}
411
412static struct commit *handle_commit(struct rev_info *revs,
413struct object_array_entry *entry)
414{
415struct object *object = entry->item;
416const char *name = entry->name;
417const char *path = entry->path;
418unsigned int mode = entry->mode;
419unsigned long flags = object->flags;
420
421/*
422* Tag object? Look what it points to..
423*/
424while (object->type == OBJ_TAG) {
425struct tag *tag = (struct tag *) object;
426struct object_id *oid;
427if (revs->tag_objects && !(flags & UNINTERESTING))
428add_pending_object(revs, object, tag->tag);
429oid = get_tagged_oid(tag);
430object = parse_object(revs->repo, oid);
431if (!object) {
432if (revs->ignore_missing_links || (flags & UNINTERESTING))
433return NULL;
434if (revs->exclude_promisor_objects &&
435is_promisor_object(&tag->tagged->oid))
436return NULL;
437if (revs->do_not_die_on_missing_objects && oid) {
438oidset_insert(&revs->missing_commits, oid);
439return NULL;
440}
441die("bad object %s", oid_to_hex(&tag->tagged->oid));
442}
443object->flags |= flags;
444/*
445* We'll handle the tagged object by looping or dropping
446* through to the non-tag handlers below. Do not
447* propagate path data from the tag's pending entry.
448*/
449path = NULL;
450mode = 0;
451}
452
453/*
454* Commit object? Just return it, we'll do all the complex
455* reachability crud.
456*/
457if (object->type == OBJ_COMMIT) {
458struct commit *commit = (struct commit *)object;
459
460if (repo_parse_commit(revs->repo, commit) < 0)
461die("unable to parse commit %s", name);
462if (flags & UNINTERESTING) {
463mark_parents_uninteresting(revs, commit);
464
465if (!revs->topo_order || !generation_numbers_enabled(the_repository))
466revs->limited = 1;
467}
468if (revs->sources) {
469char **slot = revision_sources_at(revs->sources, commit);
470
471if (!*slot)
472*slot = xstrdup(name);
473}
474return commit;
475}
476
477/*
478* Tree object? Either mark it uninteresting, or add it
479* to the list of objects to look at later..
480*/
481if (object->type == OBJ_TREE) {
482struct tree *tree = (struct tree *)object;
483if (!revs->tree_objects)
484return NULL;
485if (flags & UNINTERESTING) {
486mark_tree_contents_uninteresting(revs->repo, tree);
487return NULL;
488}
489add_pending_object_with_path(revs, object, name, mode, path);
490return NULL;
491}
492
493/*
494* Blob object? You know the drill by now..
495*/
496if (object->type == OBJ_BLOB) {
497if (!revs->blob_objects)
498return NULL;
499if (flags & UNINTERESTING)
500return NULL;
501add_pending_object_with_path(revs, object, name, mode, path);
502return NULL;
503}
504die("%s is unknown object", name);
505}
506
507static int everybody_uninteresting(struct commit_list *orig,
508struct commit **interesting_cache)
509{
510struct commit_list *list = orig;
511
512if (*interesting_cache) {
513struct commit *commit = *interesting_cache;
514if (!(commit->object.flags & UNINTERESTING))
515return 0;
516}
517
518while (list) {
519struct commit *commit = list->item;
520list = list->next;
521if (commit->object.flags & UNINTERESTING)
522continue;
523
524*interesting_cache = commit;
525return 0;
526}
527return 1;
528}
529
530/*
531* A definition of "relevant" commit that we can use to simplify limited graphs
532* by eliminating side branches.
533*
534* A "relevant" commit is one that is !UNINTERESTING (ie we are including it
535* in our list), or that is a specified BOTTOM commit. Then after computing
536* a limited list, during processing we can generally ignore boundary merges
537* coming from outside the graph, (ie from irrelevant parents), and treat
538* those merges as if they were single-parent. TREESAME is defined to consider
539* only relevant parents, if any. If we are TREESAME to our on-graph parents,
540* we don't care if we were !TREESAME to non-graph parents.
541*
542* Treating bottom commits as relevant ensures that a limited graph's
543* connection to the actual bottom commit is not viewed as a side branch, but
544* treated as part of the graph. For example:
545*
546* ....Z...A---X---o---o---B
547* . /
548* W---Y
549*
550* When computing "A..B", the A-X connection is at least as important as
551* Y-X, despite A being flagged UNINTERESTING.
552*
553* And when computing --ancestry-path "A..B", the A-X connection is more
554* important than Y-X, despite both A and Y being flagged UNINTERESTING.
555*/
556static inline int relevant_commit(struct commit *commit)
557{
558return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
559}
560
561/*
562* Return a single relevant commit from a parent list. If we are a TREESAME
563* commit, and this selects one of our parents, then we can safely simplify to
564* that parent.
565*/
566static struct commit *one_relevant_parent(const struct rev_info *revs,
567struct commit_list *orig)
568{
569struct commit_list *list = orig;
570struct commit *relevant = NULL;
571
572if (!orig)
573return NULL;
574
575/*
576* For 1-parent commits, or if first-parent-only, then return that
577* first parent (even if not "relevant" by the above definition).
578* TREESAME will have been set purely on that parent.
579*/
580if (revs->first_parent_only || !orig->next)
581return orig->item;
582
583/*
584* For multi-parent commits, identify a sole relevant parent, if any.
585* If we have only one relevant parent, then TREESAME will be set purely
586* with regard to that parent, and we can simplify accordingly.
587*
588* If we have more than one relevant parent, or no relevant parents
589* (and multiple irrelevant ones), then we can't select a parent here
590* and return NULL.
591*/
592while (list) {
593struct commit *commit = list->item;
594list = list->next;
595if (relevant_commit(commit)) {
596if (relevant)
597return NULL;
598relevant = commit;
599}
600}
601return relevant;
602}
603
604/*
605* The goal is to get REV_TREE_NEW as the result only if the
606* diff consists of all '+' (and no other changes), REV_TREE_OLD
607* if the whole diff is removal of old data, and otherwise
608* REV_TREE_DIFFERENT (of course if the trees are the same we
609* want REV_TREE_SAME).
610*
611* The only time we care about the distinction is when
612* remove_empty_trees is in effect, in which case we care only about
613* whether the whole change is REV_TREE_NEW, or if there's another type
614* of change. Which means we can stop the diff early in either of these
615* cases:
616*
617* 1. We're not using remove_empty_trees at all.
618*
619* 2. We saw anything except REV_TREE_NEW.
620*/
621#define REV_TREE_SAME 0
622#define REV_TREE_NEW 1 /* Only new files */
623#define REV_TREE_OLD 2 /* Only files removed */
624#define REV_TREE_DIFFERENT 3 /* Mixed changes */
625static int tree_difference = REV_TREE_SAME;
626
627static void file_add_remove(struct diff_options *options,
628int addremove,
629unsigned mode UNUSED,
630const struct object_id *oid UNUSED,
631int oid_valid UNUSED,
632const char *fullpath UNUSED,
633unsigned dirty_submodule UNUSED)
634{
635int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
636struct rev_info *revs = options->change_fn_data;
637
638tree_difference |= diff;
639if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
640options->flags.has_changes = 1;
641}
642
643static void file_change(struct diff_options *options,
644unsigned old_mode UNUSED,
645unsigned new_mode UNUSED,
646const struct object_id *old_oid UNUSED,
647const struct object_id *new_oid UNUSED,
648int old_oid_valid UNUSED,
649int new_oid_valid UNUSED,
650const char *fullpath UNUSED,
651unsigned old_dirty_submodule UNUSED,
652unsigned new_dirty_submodule UNUSED)
653{
654tree_difference = REV_TREE_DIFFERENT;
655options->flags.has_changes = 1;
656}
657
658static int bloom_filter_atexit_registered;
659static unsigned int count_bloom_filter_maybe;
660static unsigned int count_bloom_filter_definitely_not;
661static unsigned int count_bloom_filter_false_positive;
662static unsigned int count_bloom_filter_not_present;
663
664static void trace2_bloom_filter_statistics_atexit(void)
665{
666struct json_writer jw = JSON_WRITER_INIT;
667
668jw_object_begin(&jw, 0);
669jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
670jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
671jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
672jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
673jw_end(&jw);
674
675trace2_data_json("bloom", the_repository, "statistics", &jw);
676
677jw_release(&jw);
678}
679
680static int forbid_bloom_filters(struct pathspec *spec)
681{
682if (spec->has_wildcard)
683return 1;
684if (spec->nr > 1)
685return 1;
686if (spec->magic & ~PATHSPEC_LITERAL)
687return 1;
688if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
689return 1;
690
691return 0;
692}
693
694static void prepare_to_use_bloom_filter(struct rev_info *revs)
695{
696struct pathspec_item *pi;
697char *path_alloc = NULL;
698const char *path, *p;
699size_t len;
700int path_component_nr = 1;
701
702if (!revs->commits)
703return;
704
705if (forbid_bloom_filters(&revs->prune_data))
706return;
707
708repo_parse_commit(revs->repo, revs->commits->item);
709
710revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
711if (!revs->bloom_filter_settings)
712return;
713
714if (!revs->pruning.pathspec.nr)
715return;
716
717pi = &revs->pruning.pathspec.items[0];
718
719/* remove single trailing slash from path, if needed */
720if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
721path_alloc = xmemdupz(pi->match, pi->len - 1);
722path = path_alloc;
723} else
724path = pi->match;
725
726len = strlen(path);
727if (!len) {
728revs->bloom_filter_settings = NULL;
729free(path_alloc);
730return;
731}
732
733p = path;
734while (*p) {
735/*
736* At this point, the path is normalized to use Unix-style
737* path separators. This is required due to how the
738* changed-path Bloom filters store the paths.
739*/
740if (*p == '/')
741path_component_nr++;
742p++;
743}
744
745revs->bloom_keys_nr = path_component_nr;
746ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
747
748fill_bloom_key(path, len, &revs->bloom_keys[0],
749revs->bloom_filter_settings);
750path_component_nr = 1;
751
752p = path + len - 1;
753while (p > path) {
754if (*p == '/')
755fill_bloom_key(path, p - path,
756&revs->bloom_keys[path_component_nr++],
757revs->bloom_filter_settings);
758p--;
759}
760
761if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
762atexit(trace2_bloom_filter_statistics_atexit);
763bloom_filter_atexit_registered = 1;
764}
765
766free(path_alloc);
767}
768
769static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
770struct commit *commit)
771{
772struct bloom_filter *filter;
773int result = 1, j;
774
775if (!revs->repo->objects->commit_graph)
776return -1;
777
778if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
779return -1;
780
781filter = get_bloom_filter(revs->repo, commit);
782
783if (!filter) {
784count_bloom_filter_not_present++;
785return -1;
786}
787
788for (j = 0; result && j < revs->bloom_keys_nr; j++) {
789result = bloom_filter_contains(filter,
790&revs->bloom_keys[j],
791revs->bloom_filter_settings);
792}
793
794if (result)
795count_bloom_filter_maybe++;
796else
797count_bloom_filter_definitely_not++;
798
799return result;
800}
801
802static int rev_compare_tree(struct rev_info *revs,
803struct commit *parent, struct commit *commit, int nth_parent)
804{
805struct tree *t1 = repo_get_commit_tree(the_repository, parent);
806struct tree *t2 = repo_get_commit_tree(the_repository, commit);
807int bloom_ret = 1;
808
809if (!t1)
810return REV_TREE_NEW;
811if (!t2)
812return REV_TREE_OLD;
813
814if (revs->simplify_by_decoration) {
815/*
816* If we are simplifying by decoration, then the commit
817* is worth showing if it has a tag pointing at it.
818*/
819if (get_name_decoration(&commit->object))
820return REV_TREE_DIFFERENT;
821/*
822* A commit that is not pointed by a tag is uninteresting
823* if we are not limited by path. This means that you will
824* see the usual "commits that touch the paths" plus any
825* tagged commit by specifying both --simplify-by-decoration
826* and pathspec.
827*/
828if (!revs->prune_data.nr)
829return REV_TREE_SAME;
830}
831
832if (revs->bloom_keys_nr && !nth_parent) {
833bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
834
835if (bloom_ret == 0)
836return REV_TREE_SAME;
837}
838
839tree_difference = REV_TREE_SAME;
840revs->pruning.flags.has_changes = 0;
841diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
842
843if (!nth_parent)
844if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
845count_bloom_filter_false_positive++;
846
847return tree_difference;
848}
849
850static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit,
851int nth_parent)
852{
853struct tree *t1 = repo_get_commit_tree(the_repository, commit);
854int bloom_ret = -1;
855
856if (!t1)
857return 0;
858
859if (!nth_parent && revs->bloom_keys_nr) {
860bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
861if (!bloom_ret)
862return 1;
863}
864
865tree_difference = REV_TREE_SAME;
866revs->pruning.flags.has_changes = 0;
867diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
868
869if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
870count_bloom_filter_false_positive++;
871
872return tree_difference == REV_TREE_SAME;
873}
874
875struct treesame_state {
876unsigned int nparents;
877unsigned char treesame[FLEX_ARRAY];
878};
879
880static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
881{
882unsigned n = commit_list_count(commit->parents);
883struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
884st->nparents = n;
885add_decoration(&revs->treesame, &commit->object, st);
886return st;
887}
888
889/*
890* Must be called immediately after removing the nth_parent from a commit's
891* parent list, if we are maintaining the per-parent treesame[] decoration.
892* This does not recalculate the master TREESAME flag - update_treesame()
893* should be called to update it after a sequence of treesame[] modifications
894* that may have affected it.
895*/
896static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
897{
898struct treesame_state *st;
899int old_same;
900
901if (!commit->parents) {
902/*
903* Have just removed the only parent from a non-merge.
904* Different handling, as we lack decoration.
905*/
906if (nth_parent != 0)
907die("compact_treesame %u", nth_parent);
908old_same = !!(commit->object.flags & TREESAME);
909if (rev_same_tree_as_empty(revs, commit, nth_parent))
910commit->object.flags |= TREESAME;
911else
912commit->object.flags &= ~TREESAME;
913return old_same;
914}
915
916st = lookup_decoration(&revs->treesame, &commit->object);
917if (!st || nth_parent >= st->nparents)
918die("compact_treesame %u", nth_parent);
919
920old_same = st->treesame[nth_parent];
921memmove(st->treesame + nth_parent,
922st->treesame + nth_parent + 1,
923st->nparents - nth_parent - 1);
924
925/*
926* If we've just become a non-merge commit, update TREESAME
927* immediately, and remove the no-longer-needed decoration.
928* If still a merge, defer update until update_treesame().
929*/
930if (--st->nparents == 1) {
931if (commit->parents->next)
932die("compact_treesame parents mismatch");
933if (st->treesame[0] && revs->dense)
934commit->object.flags |= TREESAME;
935else
936commit->object.flags &= ~TREESAME;
937free(add_decoration(&revs->treesame, &commit->object, NULL));
938}
939
940return old_same;
941}
942
943static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
944{
945if (commit->parents && commit->parents->next) {
946unsigned n;
947struct treesame_state *st;
948struct commit_list *p;
949unsigned relevant_parents;
950unsigned relevant_change, irrelevant_change;
951
952st = lookup_decoration(&revs->treesame, &commit->object);
953if (!st)
954die("update_treesame %s", oid_to_hex(&commit->object.oid));
955relevant_parents = 0;
956relevant_change = irrelevant_change = 0;
957for (p = commit->parents, n = 0; p; n++, p = p->next) {
958if (relevant_commit(p->item)) {
959relevant_change |= !st->treesame[n];
960relevant_parents++;
961} else
962irrelevant_change |= !st->treesame[n];
963}
964if (relevant_parents ? relevant_change : irrelevant_change)
965commit->object.flags &= ~TREESAME;
966else
967commit->object.flags |= TREESAME;
968}
969
970return commit->object.flags & TREESAME;
971}
972
973static inline int limiting_can_increase_treesame(const struct rev_info *revs)
974{
975/*
976* TREESAME is irrelevant unless prune && dense;
977* if simplify_history is set, we can't have a mixture of TREESAME and
978* !TREESAME INTERESTING parents (and we don't have treesame[]
979* decoration anyway);
980* if first_parent_only is set, then the TREESAME flag is locked
981* against the first parent (and again we lack treesame[] decoration).
982*/
983return revs->prune && revs->dense &&
984!revs->simplify_history &&
985!revs->first_parent_only;
986}
987
988static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
989{
990struct commit_list **pp, *parent;
991struct treesame_state *ts = NULL;
992int relevant_change = 0, irrelevant_change = 0;
993int relevant_parents, nth_parent;
994
995/*
996* If we don't do pruning, everything is interesting
997*/
998if (!revs->prune)
999return;
1000
1001if (!repo_get_commit_tree(the_repository, commit))
1002return;
1003
1004if (!commit->parents) {
1005/*
1006* Pretend as if we are comparing ourselves to the
1007* (non-existent) first parent of this commit object. Even
1008* though no such parent exists, its changed-path Bloom filter
1009* (if one exists) is relative to the empty tree, using Bloom
1010* filters is allowed here.
1011*/
1012if (rev_same_tree_as_empty(revs, commit, 0))
1013commit->object.flags |= TREESAME;
1014return;
1015}
1016
1017/*
1018* Normal non-merge commit? If we don't want to make the
1019* history dense, we consider it always to be a change..
1020*/
1021if (!revs->dense && !commit->parents->next)
1022return;
1023
1024for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1025(parent = *pp) != NULL;
1026pp = &parent->next, nth_parent++) {
1027struct commit *p = parent->item;
1028if (relevant_commit(p))
1029relevant_parents++;
1030
1031if (nth_parent == 1) {
1032/*
1033* This our second loop iteration - so we now know
1034* we're dealing with a merge.
1035*
1036* Do not compare with later parents when we care only about
1037* the first parent chain, in order to avoid derailing the
1038* traversal to follow a side branch that brought everything
1039* in the path we are limited to by the pathspec.
1040*/
1041if (revs->first_parent_only)
1042break;
1043/*
1044* If this will remain a potentially-simplifiable
1045* merge, remember per-parent treesame if needed.
1046* Initialise the array with the comparison from our
1047* first iteration.
1048*/
1049if (revs->treesame.name &&
1050!revs->simplify_history &&
1051!(commit->object.flags & UNINTERESTING)) {
1052ts = initialise_treesame(revs, commit);
1053if (!(irrelevant_change || relevant_change))
1054ts->treesame[0] = 1;
1055}
1056}
1057if (repo_parse_commit(revs->repo, p) < 0)
1058die("cannot simplify commit %s (because of %s)",
1059oid_to_hex(&commit->object.oid),
1060oid_to_hex(&p->object.oid));
1061switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1062case REV_TREE_SAME:
1063if (!revs->simplify_history || !relevant_commit(p)) {
1064/* Even if a merge with an uninteresting
1065* side branch brought the entire change
1066* we are interested in, we do not want
1067* to lose the other branches of this
1068* merge, so we just keep going.
1069*/
1070if (ts)
1071ts->treesame[nth_parent] = 1;
1072continue;
1073}
1074parent->next = NULL;
1075commit->parents = parent;
1076
1077/*
1078* A merge commit is a "diversion" if it is not
1079* TREESAME to its first parent but is TREESAME
1080* to a later parent. In the simplified history,
1081* we "divert" the history walk to the later
1082* parent. These commits are shown when "show_pulls"
1083* is enabled, so do not mark the object as
1084* TREESAME here.
1085*/
1086if (!revs->show_pulls || !nth_parent)
1087commit->object.flags |= TREESAME;
1088
1089return;
1090
1091case REV_TREE_NEW:
1092if (revs->remove_empty_trees &&
1093rev_same_tree_as_empty(revs, p, nth_parent)) {
1094/* We are adding all the specified
1095* paths from this parent, so the
1096* history beyond this parent is not
1097* interesting. Remove its parents
1098* (they are grandparents for us).
1099* IOW, we pretend this parent is a
1100* "root" commit.
1101*/
1102if (repo_parse_commit(revs->repo, p) < 0)
1103die("cannot simplify commit %s (invalid %s)",
1104oid_to_hex(&commit->object.oid),
1105oid_to_hex(&p->object.oid));
1106p->parents = NULL;
1107}
1108/* fallthrough */
1109case REV_TREE_OLD:
1110case REV_TREE_DIFFERENT:
1111if (relevant_commit(p))
1112relevant_change = 1;
1113else
1114irrelevant_change = 1;
1115
1116if (!nth_parent)
1117commit->object.flags |= PULL_MERGE;
1118
1119continue;
1120}
1121die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1122}
1123
1124/*
1125* TREESAME is straightforward for single-parent commits. For merge
1126* commits, it is most useful to define it so that "irrelevant"
1127* parents cannot make us !TREESAME - if we have any relevant
1128* parents, then we only consider TREESAMEness with respect to them,
1129* allowing irrelevant merges from uninteresting branches to be
1130* simplified away. Only if we have only irrelevant parents do we
1131* base TREESAME on them. Note that this logic is replicated in
1132* update_treesame, which should be kept in sync.
1133*/
1134if (relevant_parents ? !relevant_change : !irrelevant_change)
1135commit->object.flags |= TREESAME;
1136}
1137
1138static int process_parents(struct rev_info *revs, struct commit *commit,
1139struct commit_list **list, struct prio_queue *queue)
1140{
1141struct commit_list *parent = commit->parents;
1142unsigned pass_flags;
1143
1144if (commit->object.flags & ADDED)
1145return 0;
1146if (revs->do_not_die_on_missing_objects &&
1147oidset_contains(&revs->missing_commits, &commit->object.oid))
1148return 0;
1149commit->object.flags |= ADDED;
1150
1151if (revs->include_check &&
1152!revs->include_check(commit, revs->include_check_data))
1153return 0;
1154
1155/*
1156* If the commit is uninteresting, don't try to
1157* prune parents - we want the maximal uninteresting
1158* set.
1159*
1160* Normally we haven't parsed the parent
1161* yet, so we won't have a parent of a parent
1162* here. However, it may turn out that we've
1163* reached this commit some other way (where it
1164* wasn't uninteresting), in which case we need
1165* to mark its parents recursively too..
1166*/
1167if (commit->object.flags & UNINTERESTING) {
1168while (parent) {
1169struct commit *p = parent->item;
1170parent = parent->next;
1171if (p)
1172p->object.flags |= UNINTERESTING;
1173if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1174continue;
1175if (p->parents)
1176mark_parents_uninteresting(revs, p);
1177if (p->object.flags & SEEN)
1178continue;
1179p->object.flags |= (SEEN | NOT_USER_GIVEN);
1180if (list)
1181commit_list_insert_by_date(p, list);
1182if (queue)
1183prio_queue_put(queue, p);
1184if (revs->exclude_first_parent_only)
1185break;
1186}
1187return 0;
1188}
1189
1190/*
1191* Ok, the commit wasn't uninteresting. Try to
1192* simplify the commit history and find the parent
1193* that has no differences in the path set if one exists.
1194*/
1195try_to_simplify_commit(revs, commit);
1196
1197if (revs->no_walk)
1198return 0;
1199
1200pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1201
1202for (parent = commit->parents; parent; parent = parent->next) {
1203struct commit *p = parent->item;
1204int gently = revs->ignore_missing_links ||
1205revs->exclude_promisor_objects ||
1206revs->do_not_die_on_missing_objects;
1207if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1208if (revs->exclude_promisor_objects &&
1209is_promisor_object(&p->object.oid)) {
1210if (revs->first_parent_only)
1211break;
1212continue;
1213}
1214
1215if (revs->do_not_die_on_missing_objects)
1216oidset_insert(&revs->missing_commits, &p->object.oid);
1217else
1218return -1; /* corrupt repository */
1219}
1220if (revs->sources) {
1221char **slot = revision_sources_at(revs->sources, p);
1222
1223if (!*slot)
1224*slot = *revision_sources_at(revs->sources, commit);
1225}
1226p->object.flags |= pass_flags;
1227if (!(p->object.flags & SEEN)) {
1228p->object.flags |= (SEEN | NOT_USER_GIVEN);
1229if (list)
1230commit_list_insert_by_date(p, list);
1231if (queue)
1232prio_queue_put(queue, p);
1233}
1234if (revs->first_parent_only)
1235break;
1236}
1237return 0;
1238}
1239
1240static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1241{
1242struct commit_list *p;
1243int left_count = 0, right_count = 0;
1244int left_first;
1245struct patch_ids ids;
1246unsigned cherry_flag;
1247
1248/* First count the commits on the left and on the right */
1249for (p = list; p; p = p->next) {
1250struct commit *commit = p->item;
1251unsigned flags = commit->object.flags;
1252if (flags & BOUNDARY)
1253;
1254else if (flags & SYMMETRIC_LEFT)
1255left_count++;
1256else
1257right_count++;
1258}
1259
1260if (!left_count || !right_count)
1261return;
1262
1263left_first = left_count < right_count;
1264init_patch_ids(revs->repo, &ids);
1265ids.diffopts.pathspec = revs->diffopt.pathspec;
1266
1267/* Compute patch-ids for one side */
1268for (p = list; p; p = p->next) {
1269struct commit *commit = p->item;
1270unsigned flags = commit->object.flags;
1271
1272if (flags & BOUNDARY)
1273continue;
1274/*
1275* If we have fewer left, left_first is set and we omit
1276* commits on the right branch in this loop. If we have
1277* fewer right, we skip the left ones.
1278*/
1279if (left_first != !!(flags & SYMMETRIC_LEFT))
1280continue;
1281add_commit_patch_id(commit, &ids);
1282}
1283
1284/* either cherry_mark or cherry_pick are true */
1285cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1286
1287/* Check the other side */
1288for (p = list; p; p = p->next) {
1289struct commit *commit = p->item;
1290struct patch_id *id;
1291unsigned flags = commit->object.flags;
1292
1293if (flags & BOUNDARY)
1294continue;
1295/*
1296* If we have fewer left, left_first is set and we omit
1297* commits on the left branch in this loop.
1298*/
1299if (left_first == !!(flags & SYMMETRIC_LEFT))
1300continue;
1301
1302/*
1303* Have we seen the same patch id?
1304*/
1305id = patch_id_iter_first(commit, &ids);
1306if (!id)
1307continue;
1308
1309commit->object.flags |= cherry_flag;
1310do {
1311id->commit->object.flags |= cherry_flag;
1312} while ((id = patch_id_iter_next(id, &ids)));
1313}
1314
1315free_patch_ids(&ids);
1316}
1317
1318/* How many extra uninteresting commits we want to see.. */
1319#define SLOP 5
1320
1321static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1322struct commit **interesting_cache)
1323{
1324/*
1325* No source list at all? We're definitely done..
1326*/
1327if (!src)
1328return 0;
1329
1330/*
1331* Does the destination list contain entries with a date
1332* before the source list? Definitely _not_ done.
1333*/
1334if (date <= src->item->date)
1335return SLOP;
1336
1337/*
1338* Does the source list still have interesting commits in
1339* it? Definitely not done..
1340*/
1341if (!everybody_uninteresting(src, interesting_cache))
1342return SLOP;
1343
1344/* Ok, we're closing in.. */
1345return slop-1;
1346}
1347
1348/*
1349* "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1350* computes commits that are ancestors of B but not ancestors of A but
1351* further limits the result to those that have any of C in their
1352* ancestry path (i.e. are either ancestors of any of C, descendants
1353* of any of C, or are any of C). If --ancestry-path is specified with
1354* no commit, we use all bottom commits for C.
1355*
1356* Before this function is called, ancestors of C will have already
1357* been marked with ANCESTRY_PATH previously.
1358*
1359* This takes the list of bottom commits and the result of "A..B"
1360* without --ancestry-path, and limits the latter further to the ones
1361* that have any of C in their ancestry path. Since the ancestors of C
1362* have already been marked (a prerequisite of this function), we just
1363* need to mark the descendants, then exclude any commit that does not
1364* have any of these marks.
1365*/
1366static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1367{
1368struct commit_list *p;
1369struct commit_list *rlist = NULL;
1370int made_progress;
1371
1372/*
1373* Reverse the list so that it will be likely that we would
1374* process parents before children.
1375*/
1376for (p = list; p; p = p->next)
1377commit_list_insert(p->item, &rlist);
1378
1379for (p = bottoms; p; p = p->next)
1380p->item->object.flags |= TMP_MARK;
1381
1382/*
1383* Mark the ones that can reach bottom commits in "list",
1384* in a bottom-up fashion.
1385*/
1386do {
1387made_progress = 0;
1388for (p = rlist; p; p = p->next) {
1389struct commit *c = p->item;
1390struct commit_list *parents;
1391if (c->object.flags & (TMP_MARK | UNINTERESTING))
1392continue;
1393for (parents = c->parents;
1394parents;
1395parents = parents->next) {
1396if (!(parents->item->object.flags & TMP_MARK))
1397continue;
1398c->object.flags |= TMP_MARK;
1399made_progress = 1;
1400break;
1401}
1402}
1403} while (made_progress);
1404
1405/*
1406* NEEDSWORK: decide if we want to remove parents that are
1407* not marked with TMP_MARK from commit->parents for commits
1408* in the resulting list. We may not want to do that, though.
1409*/
1410
1411/*
1412* The ones that are not marked with either TMP_MARK or
1413* ANCESTRY_PATH are uninteresting
1414*/
1415for (p = list; p; p = p->next) {
1416struct commit *c = p->item;
1417if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1418continue;
1419c->object.flags |= UNINTERESTING;
1420}
1421
1422/* We are done with TMP_MARK and ANCESTRY_PATH */
1423for (p = list; p; p = p->next)
1424p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1425for (p = bottoms; p; p = p->next)
1426p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1427free_commit_list(rlist);
1428}
1429
1430/*
1431* Before walking the history, add the set of "negative" refs the
1432* caller has asked to exclude to the bottom list.
1433*
1434* This is used to compute "rev-list --ancestry-path A..B", as we need
1435* to filter the result of "A..B" further to the ones that can actually
1436* reach A.
1437*/
1438static void collect_bottom_commits(struct commit_list *list,
1439struct commit_list **bottom)
1440{
1441struct commit_list *elem;
1442for (elem = list; elem; elem = elem->next)
1443if (elem->item->object.flags & BOTTOM)
1444commit_list_insert(elem->item, bottom);
1445}
1446
1447/* Assumes either left_only or right_only is set */
1448static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1449{
1450struct commit_list *p;
1451
1452for (p = list; p; p = p->next) {
1453struct commit *commit = p->item;
1454
1455if (revs->right_only) {
1456if (commit->object.flags & SYMMETRIC_LEFT)
1457commit->object.flags |= SHOWN;
1458} else /* revs->left_only is set */
1459if (!(commit->object.flags & SYMMETRIC_LEFT))
1460commit->object.flags |= SHOWN;
1461}
1462}
1463
1464static int limit_list(struct rev_info *revs)
1465{
1466int slop = SLOP;
1467timestamp_t date = TIME_MAX;
1468struct commit_list *original_list = revs->commits;
1469struct commit_list *newlist = NULL;
1470struct commit_list **p = &newlist;
1471struct commit *interesting_cache = NULL;
1472
1473if (revs->ancestry_path_implicit_bottoms) {
1474collect_bottom_commits(original_list,
1475&revs->ancestry_path_bottoms);
1476if (!revs->ancestry_path_bottoms)
1477die("--ancestry-path given but there are no bottom commits");
1478}
1479
1480while (original_list) {
1481struct commit *commit = pop_commit(&original_list);
1482struct object *obj = &commit->object;
1483show_early_output_fn_t show;
1484
1485if (commit == interesting_cache)
1486interesting_cache = NULL;
1487
1488if (revs->max_age != -1 && (commit->date < revs->max_age))
1489obj->flags |= UNINTERESTING;
1490if (process_parents(revs, commit, &original_list, NULL) < 0)
1491return -1;
1492if (obj->flags & UNINTERESTING) {
1493mark_parents_uninteresting(revs, commit);
1494slop = still_interesting(original_list, date, slop, &interesting_cache);
1495if (slop)
1496continue;
1497break;
1498}
1499if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1500!revs->line_level_traverse)
1501continue;
1502if (revs->max_age_as_filter != -1 &&
1503(commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1504continue;
1505date = commit->date;
1506p = &commit_list_insert(commit, p)->next;
1507
1508show = show_early_output;
1509if (!show)
1510continue;
1511
1512show(revs, newlist);
1513show_early_output = NULL;
1514}
1515if (revs->cherry_pick || revs->cherry_mark)
1516cherry_pick_list(newlist, revs);
1517
1518if (revs->left_only || revs->right_only)
1519limit_left_right(newlist, revs);
1520
1521if (revs->ancestry_path)
1522limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1523
1524/*
1525* Check if any commits have become TREESAME by some of their parents
1526* becoming UNINTERESTING.
1527*/
1528if (limiting_can_increase_treesame(revs)) {
1529struct commit_list *list = NULL;
1530for (list = newlist; list; list = list->next) {
1531struct commit *c = list->item;
1532if (c->object.flags & (UNINTERESTING | TREESAME))
1533continue;
1534update_treesame(revs, c);
1535}
1536}
1537
1538free_commit_list(original_list);
1539revs->commits = newlist;
1540return 0;
1541}
1542
1543/*
1544* Add an entry to refs->cmdline with the specified information.
1545* *name is copied.
1546*/
1547static void add_rev_cmdline(struct rev_info *revs,
1548struct object *item,
1549const char *name,
1550int whence,
1551unsigned flags)
1552{
1553struct rev_cmdline_info *info = &revs->cmdline;
1554unsigned int nr = info->nr;
1555
1556ALLOC_GROW(info->rev, nr + 1, info->alloc);
1557info->rev[nr].item = item;
1558info->rev[nr].name = xstrdup(name);
1559info->rev[nr].whence = whence;
1560info->rev[nr].flags = flags;
1561info->nr++;
1562}
1563
1564static void add_rev_cmdline_list(struct rev_info *revs,
1565struct commit_list *commit_list,
1566int whence,
1567unsigned flags)
1568{
1569while (commit_list) {
1570struct object *object = &commit_list->item->object;
1571add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1572whence, flags);
1573commit_list = commit_list->next;
1574}
1575}
1576
1577int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1578{
1579const char *stripped_path = strip_namespace(path);
1580struct string_list_item *item;
1581
1582for_each_string_list_item(item, &exclusions->excluded_refs) {
1583if (!wildmatch(item->string, path, 0))
1584return 1;
1585}
1586
1587if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1588return 1;
1589
1590return 0;
1591}
1592
1593void init_ref_exclusions(struct ref_exclusions *exclusions)
1594{
1595struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1596memcpy(exclusions, &blank, sizeof(*exclusions));
1597}
1598
1599void clear_ref_exclusions(struct ref_exclusions *exclusions)
1600{
1601string_list_clear(&exclusions->excluded_refs, 0);
1602strvec_clear(&exclusions->hidden_refs);
1603exclusions->hidden_refs_configured = 0;
1604}
1605
1606void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1607{
1608string_list_append(&exclusions->excluded_refs, exclude);
1609}
1610
1611struct exclude_hidden_refs_cb {
1612struct ref_exclusions *exclusions;
1613const char *section;
1614};
1615
1616static int hide_refs_config(const char *var, const char *value,
1617const struct config_context *ctx UNUSED,
1618void *cb_data)
1619{
1620struct exclude_hidden_refs_cb *cb = cb_data;
1621cb->exclusions->hidden_refs_configured = 1;
1622return parse_hide_refs_config(var, value, cb->section,
1623&cb->exclusions->hidden_refs);
1624}
1625
1626void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1627{
1628struct exclude_hidden_refs_cb cb;
1629
1630if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1631strcmp(section, "uploadpack"))
1632die(_("unsupported section for hidden refs: %s"), section);
1633
1634if (exclusions->hidden_refs_configured)
1635die(_("--exclude-hidden= passed more than once"));
1636
1637cb.exclusions = exclusions;
1638cb.section = section;
1639
1640git_config(hide_refs_config, &cb);
1641}
1642
1643struct all_refs_cb {
1644int all_flags;
1645int warned_bad_reflog;
1646struct rev_info *all_revs;
1647const char *name_for_errormsg;
1648struct worktree *wt;
1649};
1650
1651static int handle_one_ref(const char *path, const char *referent UNUSED, const struct object_id *oid,
1652int flag UNUSED,
1653void *cb_data)
1654{
1655struct all_refs_cb *cb = cb_data;
1656struct object *object;
1657
1658if (ref_excluded(&cb->all_revs->ref_excludes, path))
1659return 0;
1660
1661object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1662add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1663add_pending_object(cb->all_revs, object, path);
1664return 0;
1665}
1666
1667static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1668unsigned flags)
1669{
1670cb->all_revs = revs;
1671cb->all_flags = flags;
1672revs->rev_input_given = 1;
1673cb->wt = NULL;
1674}
1675
1676static void handle_refs(struct ref_store *refs,
1677struct rev_info *revs, unsigned flags,
1678int (*for_each)(struct ref_store *, each_ref_fn, void *))
1679{
1680struct all_refs_cb cb;
1681
1682if (!refs) {
1683/* this could happen with uninitialized submodules */
1684return;
1685}
1686
1687init_all_refs_cb(&cb, revs, flags);
1688for_each(refs, handle_one_ref, &cb);
1689}
1690
1691static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1692{
1693struct all_refs_cb *cb = cb_data;
1694if (!is_null_oid(oid)) {
1695struct object *o = parse_object(cb->all_revs->repo, oid);
1696if (o) {
1697o->flags |= cb->all_flags;
1698/* ??? CMDLINEFLAGS ??? */
1699add_pending_object(cb->all_revs, o, "");
1700}
1701else if (!cb->warned_bad_reflog) {
1702warning("reflog of '%s' references pruned commits",
1703cb->name_for_errormsg);
1704cb->warned_bad_reflog = 1;
1705}
1706}
1707}
1708
1709static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1710const char *email UNUSED,
1711timestamp_t timestamp UNUSED,
1712int tz UNUSED,
1713const char *message UNUSED,
1714void *cb_data)
1715{
1716handle_one_reflog_commit(ooid, cb_data);
1717handle_one_reflog_commit(noid, cb_data);
1718return 0;
1719}
1720
1721static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1722{
1723struct all_refs_cb *cb = cb_data;
1724struct strbuf refname = STRBUF_INIT;
1725
1726cb->warned_bad_reflog = 0;
1727strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1728cb->name_for_errormsg = refname.buf;
1729refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1730refname.buf,
1731handle_one_reflog_ent, cb_data);
1732strbuf_release(&refname);
1733return 0;
1734}
1735
1736static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1737{
1738struct worktree **worktrees, **p;
1739
1740worktrees = get_worktrees();
1741for (p = worktrees; *p; p++) {
1742struct worktree *wt = *p;
1743
1744if (wt->is_current)
1745continue;
1746
1747cb->wt = wt;
1748refs_for_each_reflog(get_worktree_ref_store(wt),
1749handle_one_reflog,
1750cb);
1751}
1752free_worktrees(worktrees);
1753}
1754
1755void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1756{
1757struct all_refs_cb cb;
1758
1759cb.all_revs = revs;
1760cb.all_flags = flags;
1761cb.wt = NULL;
1762refs_for_each_reflog(get_main_ref_store(the_repository),
1763handle_one_reflog, &cb);
1764
1765if (!revs->single_worktree)
1766add_other_reflogs_to_pending(&cb);
1767}
1768
1769static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1770struct strbuf *path, unsigned int flags)
1771{
1772size_t baselen = path->len;
1773int i;
1774
1775if (it->entry_count >= 0) {
1776struct tree *tree = lookup_tree(revs->repo, &it->oid);
1777tree->object.flags |= flags;
1778add_pending_object_with_path(revs, &tree->object, "",
1779040000, path->buf);
1780}
1781
1782for (i = 0; i < it->subtree_nr; i++) {
1783struct cache_tree_sub *sub = it->down[i];
1784strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1785add_cache_tree(sub->cache_tree, revs, path, flags);
1786strbuf_setlen(path, baselen);
1787}
1788
1789}
1790
1791static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1792{
1793struct string_list_item *item;
1794struct string_list *resolve_undo = istate->resolve_undo;
1795
1796if (!resolve_undo)
1797return;
1798
1799for_each_string_list_item(item, resolve_undo) {
1800const char *path = item->string;
1801struct resolve_undo_info *ru = item->util;
1802int i;
1803
1804if (!ru)
1805continue;
1806for (i = 0; i < 3; i++) {
1807struct blob *blob;
1808
1809if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1810continue;
1811
1812blob = lookup_blob(revs->repo, &ru->oid[i]);
1813if (!blob) {
1814warning(_("resolve-undo records `%s` which is missing"),
1815oid_to_hex(&ru->oid[i]));
1816continue;
1817}
1818add_pending_object_with_path(revs, &blob->object, "",
1819ru->mode[i], path);
1820}
1821}
1822}
1823
1824static void do_add_index_objects_to_pending(struct rev_info *revs,
1825struct index_state *istate,
1826unsigned int flags)
1827{
1828int i;
1829
1830/* TODO: audit for interaction with sparse-index. */
1831ensure_full_index(istate);
1832for (i = 0; i < istate->cache_nr; i++) {
1833struct cache_entry *ce = istate->cache[i];
1834struct blob *blob;
1835
1836if (S_ISGITLINK(ce->ce_mode))
1837continue;
1838
1839blob = lookup_blob(revs->repo, &ce->oid);
1840if (!blob)
1841die("unable to add index blob to traversal");
1842blob->object.flags |= flags;
1843add_pending_object_with_path(revs, &blob->object, "",
1844ce->ce_mode, ce->name);
1845}
1846
1847if (istate->cache_tree) {
1848struct strbuf path = STRBUF_INIT;
1849add_cache_tree(istate->cache_tree, revs, &path, flags);
1850strbuf_release(&path);
1851}
1852
1853add_resolve_undo_to_pending(istate, revs);
1854}
1855
1856void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1857{
1858struct worktree **worktrees, **p;
1859
1860repo_read_index(revs->repo);
1861do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1862
1863if (revs->single_worktree)
1864return;
1865
1866worktrees = get_worktrees();
1867for (p = worktrees; *p; p++) {
1868struct worktree *wt = *p;
1869struct index_state istate = INDEX_STATE_INIT(revs->repo);
1870
1871if (wt->is_current)
1872continue; /* current index already taken care of */
1873
1874if (read_index_from(&istate,
1875worktree_git_path(the_repository, wt, "index"),
1876get_worktree_git_dir(wt)) > 0)
1877do_add_index_objects_to_pending(revs, &istate, flags);
1878discard_index(&istate);
1879}
1880free_worktrees(worktrees);
1881}
1882
1883struct add_alternate_refs_data {
1884struct rev_info *revs;
1885unsigned int flags;
1886};
1887
1888static void add_one_alternate_ref(const struct object_id *oid,
1889void *vdata)
1890{
1891const char *name = ".alternate";
1892struct add_alternate_refs_data *data = vdata;
1893struct object *obj;
1894
1895obj = get_reference(data->revs, name, oid, data->flags);
1896add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1897add_pending_object(data->revs, obj, name);
1898}
1899
1900static void add_alternate_refs_to_pending(struct rev_info *revs,
1901unsigned int flags)
1902{
1903struct add_alternate_refs_data data;
1904data.revs = revs;
1905data.flags = flags;
1906for_each_alternate_ref(add_one_alternate_ref, &data);
1907}
1908
1909static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1910int exclude_parent)
1911{
1912struct object_id oid;
1913struct object *it;
1914struct commit *commit;
1915struct commit_list *parents;
1916int parent_number;
1917const char *arg = arg_;
1918
1919if (*arg == '^') {
1920flags ^= UNINTERESTING | BOTTOM;
1921arg++;
1922}
1923if (repo_get_oid_committish(the_repository, arg, &oid))
1924return 0;
1925while (1) {
1926it = get_reference(revs, arg, &oid, 0);
1927if (!it && revs->ignore_missing)
1928return 0;
1929if (it->type != OBJ_TAG)
1930break;
1931if (!((struct tag*)it)->tagged)
1932return 0;
1933oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1934}
1935if (it->type != OBJ_COMMIT)
1936return 0;
1937commit = (struct commit *)it;
1938if (exclude_parent &&
1939exclude_parent > commit_list_count(commit->parents))
1940return 0;
1941for (parents = commit->parents, parent_number = 1;
1942parents;
1943parents = parents->next, parent_number++) {
1944if (exclude_parent && parent_number != exclude_parent)
1945continue;
1946
1947it = &parents->item->object;
1948it->flags |= flags;
1949add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1950add_pending_object(revs, it, arg);
1951}
1952return 1;
1953}
1954
1955void repo_init_revisions(struct repository *r,
1956struct rev_info *revs,
1957const char *prefix)
1958{
1959struct rev_info blank = REV_INFO_INIT;
1960memcpy(revs, &blank, sizeof(*revs));
1961
1962revs->repo = r;
1963revs->pruning.repo = r;
1964revs->pruning.add_remove = file_add_remove;
1965revs->pruning.change = file_change;
1966revs->pruning.change_fn_data = revs;
1967revs->prefix = prefix;
1968
1969grep_init(&revs->grep_filter, revs->repo);
1970revs->grep_filter.status_only = 1;
1971
1972repo_diff_setup(revs->repo, &revs->diffopt);
1973if (prefix && !revs->diffopt.prefix) {
1974revs->diffopt.prefix = prefix;
1975revs->diffopt.prefix_length = strlen(prefix);
1976}
1977
1978init_display_notes(&revs->notes_opt);
1979list_objects_filter_init(&revs->filter);
1980init_ref_exclusions(&revs->ref_excludes);
1981oidset_init(&revs->missing_commits, 0);
1982}
1983
1984static void add_pending_commit_list(struct rev_info *revs,
1985struct commit_list *commit_list,
1986unsigned int flags)
1987{
1988while (commit_list) {
1989struct object *object = &commit_list->item->object;
1990object->flags |= flags;
1991add_pending_object(revs, object, oid_to_hex(&object->oid));
1992commit_list = commit_list->next;
1993}
1994}
1995
1996static const char *lookup_other_head(struct object_id *oid)
1997{
1998int i;
1999static const char *const other_head[] = {
2000"MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
2001};
2002
2003for (i = 0; i < ARRAY_SIZE(other_head); i++)
2004if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
2005RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2006oid, NULL)) {
2007if (is_null_oid(oid))
2008die(_("%s exists but is a symbolic ref"), other_head[i]);
2009return other_head[i];
2010}
2011
2012die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
2013}
2014
2015static void prepare_show_merge(struct rev_info *revs)
2016{
2017struct commit_list *bases = NULL;
2018struct commit *head, *other;
2019struct object_id oid;
2020const char *other_name;
2021const char **prune = NULL;
2022int i, prune_num = 1; /* counting terminating NULL */
2023struct index_state *istate = revs->repo->index;
2024
2025if (repo_get_oid(the_repository, "HEAD", &oid))
2026die("--merge without HEAD?");
2027head = lookup_commit_or_die(&oid, "HEAD");
2028other_name = lookup_other_head(&oid);
2029other = lookup_commit_or_die(&oid, other_name);
2030add_pending_object(revs, &head->object, "HEAD");
2031add_pending_object(revs, &other->object, other_name);
2032if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2033exit(128);
2034add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2035add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2036free_commit_list(bases);
2037head->object.flags |= SYMMETRIC_LEFT;
2038
2039if (!istate->cache_nr)
2040repo_read_index(revs->repo);
2041for (i = 0; i < istate->cache_nr; i++) {
2042const struct cache_entry *ce = istate->cache[i];
2043if (!ce_stage(ce))
2044continue;
2045if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2046prune_num++;
2047REALLOC_ARRAY(prune, prune_num);
2048prune[prune_num-2] = ce->name;
2049prune[prune_num-1] = NULL;
2050}
2051while ((i+1 < istate->cache_nr) &&
2052ce_same_name(ce, istate->cache[i+1]))
2053i++;
2054}
2055clear_pathspec(&revs->prune_data);
2056parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2057PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2058revs->limited = 1;
2059}
2060
2061static int dotdot_missing(const char *arg, char *dotdot,
2062struct rev_info *revs, int symmetric)
2063{
2064if (revs->ignore_missing)
2065return 0;
2066/* de-munge so we report the full argument */
2067*dotdot = '.';
2068die(symmetric
2069? "Invalid symmetric difference expression %s"
2070: "Invalid revision range %s", arg);
2071}
2072
2073static int handle_dotdot_1(const char *arg, char *dotdot,
2074struct rev_info *revs, int flags,
2075int cant_be_filename,
2076struct object_context *a_oc,
2077struct object_context *b_oc)
2078{
2079const char *a_name, *b_name;
2080struct object_id a_oid, b_oid;
2081struct object *a_obj, *b_obj;
2082unsigned int a_flags, b_flags;
2083int symmetric = 0;
2084unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2085unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2086
2087a_name = arg;
2088if (!*a_name)
2089a_name = "HEAD";
2090
2091b_name = dotdot + 2;
2092if (*b_name == '.') {
2093symmetric = 1;
2094b_name++;
2095}
2096if (!*b_name)
2097b_name = "HEAD";
2098
2099if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2100get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2101return -1;
2102
2103if (!cant_be_filename) {
2104*dotdot = '.';
2105verify_non_filename(revs->prefix, arg);
2106*dotdot = '\0';
2107}
2108
2109a_obj = parse_object(revs->repo, &a_oid);
2110b_obj = parse_object(revs->repo, &b_oid);
2111if (!a_obj || !b_obj)
2112return dotdot_missing(arg, dotdot, revs, symmetric);
2113
2114if (!symmetric) {
2115/* just A..B */
2116b_flags = flags;
2117a_flags = flags_exclude;
2118} else {
2119/* A...B -- find merge bases between the two */
2120struct commit *a, *b;
2121struct commit_list *exclude = NULL;
2122
2123a = lookup_commit_reference(revs->repo, &a_obj->oid);
2124b = lookup_commit_reference(revs->repo, &b_obj->oid);
2125if (!a || !b)
2126return dotdot_missing(arg, dotdot, revs, symmetric);
2127
2128if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2129free_commit_list(exclude);
2130return -1;
2131}
2132add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2133flags_exclude);
2134add_pending_commit_list(revs, exclude, flags_exclude);
2135free_commit_list(exclude);
2136
2137b_flags = flags;
2138a_flags = flags | SYMMETRIC_LEFT;
2139}
2140
2141a_obj->flags |= a_flags;
2142b_obj->flags |= b_flags;
2143add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2144add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2145add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2146add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2147return 0;
2148}
2149
2150static int handle_dotdot(const char *arg,
2151struct rev_info *revs, int flags,
2152int cant_be_filename)
2153{
2154struct object_context a_oc = {0}, b_oc = {0};
2155char *dotdot = strstr(arg, "..");
2156int ret;
2157
2158if (!dotdot)
2159return -1;
2160
2161*dotdot = '\0';
2162ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2163&a_oc, &b_oc);
2164*dotdot = '.';
2165
2166object_context_release(&a_oc);
2167object_context_release(&b_oc);
2168return ret;
2169}
2170
2171static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2172{
2173struct object_context oc = {0};
2174char *mark;
2175struct object *object;
2176struct object_id oid;
2177int local_flags;
2178const char *arg = arg_;
2179int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2180unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2181int ret;
2182
2183flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2184
2185if (!cant_be_filename && !strcmp(arg, "..")) {
2186/*
2187* Just ".."? That is not a range but the
2188* pathspec for the parent directory.
2189*/
2190ret = -1;
2191goto out;
2192}
2193
2194if (!handle_dotdot(arg, revs, flags, revarg_opt)) {
2195ret = 0;
2196goto out;
2197}
2198
2199mark = strstr(arg, "^@");
2200if (mark && !mark[2]) {
2201*mark = 0;
2202if (add_parents_only(revs, arg, flags, 0)) {
2203ret = 0;
2204goto out;
2205}
2206*mark = '^';
2207}
2208mark = strstr(arg, "^!");
2209if (mark && !mark[2]) {
2210*mark = 0;
2211if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2212*mark = '^';
2213}
2214mark = strstr(arg, "^-");
2215if (mark) {
2216int exclude_parent = 1;
2217
2218if (mark[2]) {
2219if (strtol_i(mark + 2, 10, &exclude_parent) ||
2220exclude_parent < 1) {
2221ret = -1;
2222goto out;
2223}
2224}
2225
2226*mark = 0;
2227if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2228*mark = '^';
2229}
2230
2231local_flags = 0;
2232if (*arg == '^') {
2233local_flags = UNINTERESTING | BOTTOM;
2234arg++;
2235}
2236
2237if (revarg_opt & REVARG_COMMITTISH)
2238get_sha1_flags |= GET_OID_COMMITTISH;
2239
2240/*
2241* Even if revs->do_not_die_on_missing_objects is set, we
2242* should error out if we can't even get an oid, as
2243* `--missing=print` should be able to report missing oids.
2244*/
2245if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) {
2246ret = revs->ignore_missing ? 0 : -1;
2247goto out;
2248}
2249if (!cant_be_filename)
2250verify_non_filename(revs->prefix, arg);
2251object = get_reference(revs, arg, &oid, flags ^ local_flags);
2252if (!object) {
2253ret = (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2254goto out;
2255}
2256add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2257add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2258
2259ret = 0;
2260
2261out:
2262object_context_release(&oc);
2263return ret;
2264}
2265
2266int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2267{
2268int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2269if (!ret)
2270revs->rev_input_given = 1;
2271return ret;
2272}
2273
2274static void read_pathspec_from_stdin(struct strbuf *sb,
2275struct strvec *prune)
2276{
2277while (strbuf_getline(sb, stdin) != EOF)
2278strvec_push(prune, sb->buf);
2279}
2280
2281static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2282{
2283append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2284}
2285
2286static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2287{
2288append_header_grep_pattern(&revs->grep_filter, field, pattern);
2289}
2290
2291static void add_message_grep(struct rev_info *revs, const char *pattern)
2292{
2293add_grep(revs, pattern, GREP_PATTERN_BODY);
2294}
2295
2296static int parse_count(const char *arg)
2297{
2298int count;
2299
2300if (strtol_i(arg, 10, &count) < 0)
2301die("'%s': not an integer", arg);
2302return count;
2303}
2304
2305static timestamp_t parse_age(const char *arg)
2306{
2307timestamp_t num;
2308char *p;
2309
2310errno = 0;
2311num = parse_timestamp(arg, &p, 10);
2312if (errno || *p || p == arg)
2313die("'%s': not a number of seconds since epoch", arg);
2314return num;
2315}
2316
2317static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2318int *unkc, const char **unkv,
2319const struct setup_revision_opt* opt)
2320{
2321const char *arg = argv[0];
2322const char *optarg = NULL;
2323int argcount;
2324const unsigned hexsz = the_hash_algo->hexsz;
2325
2326/* pseudo revision arguments */
2327if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2328!strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2329!strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2330!strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2331!strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2332!strcmp(arg, "--indexed-objects") ||
2333!strcmp(arg, "--alternate-refs") ||
2334starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2335starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2336starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2337{
2338unkv[(*unkc)++] = arg;
2339return 1;
2340}
2341
2342if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2343revs->max_count = parse_count(optarg);
2344revs->no_walk = 0;
2345return argcount;
2346} else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2347revs->skip_count = parse_count(optarg);
2348return argcount;
2349} else if ((*arg == '-') && isdigit(arg[1])) {
2350/* accept -<digit>, like traditional "head" */
2351revs->max_count = parse_count(arg + 1);
2352revs->no_walk = 0;
2353} else if (!strcmp(arg, "-n")) {
2354if (argc <= 1)
2355return error("-n requires an argument");
2356revs->max_count = parse_count(argv[1]);
2357revs->no_walk = 0;
2358return 2;
2359} else if (skip_prefix(arg, "-n", &optarg)) {
2360revs->max_count = parse_count(optarg);
2361revs->no_walk = 0;
2362} else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2363revs->max_age = parse_age(optarg);
2364return argcount;
2365} else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2366revs->max_age = approxidate(optarg);
2367return argcount;
2368} else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2369revs->max_age_as_filter = approxidate(optarg);
2370return argcount;
2371} else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2372revs->max_age = approxidate(optarg);
2373return argcount;
2374} else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2375revs->min_age = parse_age(optarg);
2376return argcount;
2377} else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2378revs->min_age = approxidate(optarg);
2379return argcount;
2380} else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2381revs->min_age = approxidate(optarg);
2382return argcount;
2383} else if (!strcmp(arg, "--first-parent")) {
2384revs->first_parent_only = 1;
2385} else if (!strcmp(arg, "--exclude-first-parent-only")) {
2386revs->exclude_first_parent_only = 1;
2387} else if (!strcmp(arg, "--ancestry-path")) {
2388revs->ancestry_path = 1;
2389revs->simplify_history = 0;
2390revs->limited = 1;
2391revs->ancestry_path_implicit_bottoms = 1;
2392} else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2393struct commit *c;
2394struct object_id oid;
2395const char *msg = _("could not get commit for --ancestry-path argument %s");
2396
2397revs->ancestry_path = 1;
2398revs->simplify_history = 0;
2399revs->limited = 1;
2400
2401if (repo_get_oid_committish(revs->repo, optarg, &oid))
2402return error(msg, optarg);
2403get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2404c = lookup_commit_reference(revs->repo, &oid);
2405if (!c)
2406return error(msg, optarg);
2407commit_list_insert(c, &revs->ancestry_path_bottoms);
2408} else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2409init_reflog_walk(&revs->reflog_info);
2410} else if (!strcmp(arg, "--default")) {
2411if (argc <= 1)
2412return error("bad --default argument");
2413revs->def = argv[1];
2414return 2;
2415} else if (!strcmp(arg, "--merge")) {
2416revs->show_merge = 1;
2417} else if (!strcmp(arg, "--topo-order")) {
2418revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2419revs->topo_order = 1;
2420} else if (!strcmp(arg, "--simplify-merges")) {
2421revs->simplify_merges = 1;
2422revs->topo_order = 1;
2423revs->rewrite_parents = 1;
2424revs->simplify_history = 0;
2425revs->limited = 1;
2426} else if (!strcmp(arg, "--simplify-by-decoration")) {
2427revs->simplify_merges = 1;
2428revs->topo_order = 1;
2429revs->rewrite_parents = 1;
2430revs->simplify_history = 0;
2431revs->simplify_by_decoration = 1;
2432revs->limited = 1;
2433revs->prune = 1;
2434} else if (!strcmp(arg, "--date-order")) {
2435revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2436revs->topo_order = 1;
2437} else if (!strcmp(arg, "--author-date-order")) {
2438revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2439revs->topo_order = 1;
2440} else if (!strcmp(arg, "--early-output")) {
2441revs->early_output = 100;
2442revs->topo_order = 1;
2443} else if (skip_prefix(arg, "--early-output=", &optarg)) {
2444if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2445die("'%s': not a non-negative integer", optarg);
2446revs->topo_order = 1;
2447} else if (!strcmp(arg, "--parents")) {
2448revs->rewrite_parents = 1;
2449revs->print_parents = 1;
2450} else if (!strcmp(arg, "--dense")) {
2451revs->dense = 1;
2452} else if (!strcmp(arg, "--sparse")) {
2453revs->dense = 0;
2454} else if (!strcmp(arg, "--in-commit-order")) {
2455revs->tree_blobs_in_commit_order = 1;
2456} else if (!strcmp(arg, "--remove-empty")) {
2457revs->remove_empty_trees = 1;
2458} else if (!strcmp(arg, "--merges")) {
2459revs->min_parents = 2;
2460} else if (!strcmp(arg, "--no-merges")) {
2461revs->max_parents = 1;
2462} else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2463revs->min_parents = parse_count(optarg);
2464} else if (!strcmp(arg, "--no-min-parents")) {
2465revs->min_parents = 0;
2466} else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2467revs->max_parents = parse_count(optarg);
2468} else if (!strcmp(arg, "--no-max-parents")) {
2469revs->max_parents = -1;
2470} else if (!strcmp(arg, "--boundary")) {
2471revs->boundary = 1;
2472} else if (!strcmp(arg, "--left-right")) {
2473revs->left_right = 1;
2474} else if (!strcmp(arg, "--left-only")) {
2475if (revs->right_only)
2476die(_("options '%s' and '%s' cannot be used together"),
2477"--left-only", "--right-only/--cherry");
2478revs->left_only = 1;
2479} else if (!strcmp(arg, "--right-only")) {
2480if (revs->left_only)
2481die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2482revs->right_only = 1;
2483} else if (!strcmp(arg, "--cherry")) {
2484if (revs->left_only)
2485die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2486revs->cherry_mark = 1;
2487revs->right_only = 1;
2488revs->max_parents = 1;
2489revs->limited = 1;
2490} else if (!strcmp(arg, "--count")) {
2491revs->count = 1;
2492} else if (!strcmp(arg, "--cherry-mark")) {
2493if (revs->cherry_pick)
2494die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2495revs->cherry_mark = 1;
2496revs->limited = 1; /* needs limit_list() */
2497} else if (!strcmp(arg, "--cherry-pick")) {
2498if (revs->cherry_mark)
2499die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2500revs->cherry_pick = 1;
2501revs->limited = 1;
2502} else if (!strcmp(arg, "--objects")) {
2503revs->tag_objects = 1;
2504revs->tree_objects = 1;
2505revs->blob_objects = 1;
2506} else if (!strcmp(arg, "--objects-edge")) {
2507revs->tag_objects = 1;
2508revs->tree_objects = 1;
2509revs->blob_objects = 1;
2510revs->edge_hint = 1;
2511} else if (!strcmp(arg, "--objects-edge-aggressive")) {
2512revs->tag_objects = 1;
2513revs->tree_objects = 1;
2514revs->blob_objects = 1;
2515revs->edge_hint = 1;
2516revs->edge_hint_aggressive = 1;
2517} else if (!strcmp(arg, "--verify-objects")) {
2518revs->tag_objects = 1;
2519revs->tree_objects = 1;
2520revs->blob_objects = 1;
2521revs->verify_objects = 1;
2522disable_commit_graph(revs->repo);
2523} else if (!strcmp(arg, "--unpacked")) {
2524revs->unpacked = 1;
2525} else if (starts_with(arg, "--unpacked=")) {
2526die(_("--unpacked=<packfile> no longer supported"));
2527} else if (!strcmp(arg, "--no-kept-objects")) {
2528revs->no_kept_objects = 1;
2529revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2530revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2531} else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2532revs->no_kept_objects = 1;
2533if (!strcmp(optarg, "in-core"))
2534revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2535if (!strcmp(optarg, "on-disk"))
2536revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2537} else if (!strcmp(arg, "-r")) {
2538revs->diff = 1;
2539revs->diffopt.flags.recursive = 1;
2540} else if (!strcmp(arg, "-t")) {
2541revs->diff = 1;
2542revs->diffopt.flags.recursive = 1;
2543revs->diffopt.flags.tree_in_recursive = 1;
2544} else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2545return argcount;
2546} else if (!strcmp(arg, "-v")) {
2547revs->verbose_header = 1;
2548} else if (!strcmp(arg, "--pretty")) {
2549revs->verbose_header = 1;
2550revs->pretty_given = 1;
2551get_commit_format(NULL, revs);
2552} else if (skip_prefix(arg, "--pretty=", &optarg) ||
2553skip_prefix(arg, "--format=", &optarg)) {
2554/*
2555* Detached form ("--pretty X" as opposed to "--pretty=X")
2556* not allowed, since the argument is optional.
2557*/
2558revs->verbose_header = 1;
2559revs->pretty_given = 1;
2560get_commit_format(optarg, revs);
2561} else if (!strcmp(arg, "--expand-tabs")) {
2562revs->expand_tabs_in_log = 8;
2563} else if (!strcmp(arg, "--no-expand-tabs")) {
2564revs->expand_tabs_in_log = 0;
2565} else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2566int val;
2567if (strtol_i(arg, 10, &val) < 0 || val < 0)
2568die("'%s': not a non-negative integer", arg);
2569revs->expand_tabs_in_log = val;
2570} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2571enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2572revs->show_notes_given = 1;
2573} else if (!strcmp(arg, "--show-signature")) {
2574revs->show_signature = 1;
2575} else if (!strcmp(arg, "--no-show-signature")) {
2576revs->show_signature = 0;
2577} else if (!strcmp(arg, "--show-linear-break")) {
2578revs->break_bar = " ..........";
2579revs->track_linear = 1;
2580revs->track_first_time = 1;
2581} else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2582revs->break_bar = xstrdup(optarg);
2583revs->track_linear = 1;
2584revs->track_first_time = 1;
2585} else if (!strcmp(arg, "--show-notes-by-default")) {
2586revs->show_notes_by_default = 1;
2587} else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2588skip_prefix(arg, "--notes=", &optarg)) {
2589if (starts_with(arg, "--show-notes=") &&
2590revs->notes_opt.use_default_notes < 0)
2591revs->notes_opt.use_default_notes = 1;
2592enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2593revs->show_notes_given = 1;
2594} else if (!strcmp(arg, "--no-notes")) {
2595disable_display_notes(&revs->notes_opt, &revs->show_notes);
2596revs->show_notes_given = 1;
2597} else if (!strcmp(arg, "--standard-notes")) {
2598revs->show_notes_given = 1;
2599revs->notes_opt.use_default_notes = 1;
2600} else if (!strcmp(arg, "--no-standard-notes")) {
2601revs->notes_opt.use_default_notes = 0;
2602} else if (!strcmp(arg, "--oneline")) {
2603revs->verbose_header = 1;
2604get_commit_format("oneline", revs);
2605revs->pretty_given = 1;
2606revs->abbrev_commit = 1;
2607} else if (!strcmp(arg, "--graph")) {
2608graph_clear(revs->graph);
2609revs->graph = graph_init(revs);
2610} else if (!strcmp(arg, "--no-graph")) {
2611graph_clear(revs->graph);
2612revs->graph = NULL;
2613} else if (!strcmp(arg, "--encode-email-headers")) {
2614revs->encode_email_headers = 1;
2615} else if (!strcmp(arg, "--no-encode-email-headers")) {
2616revs->encode_email_headers = 0;
2617} else if (!strcmp(arg, "--root")) {
2618revs->show_root_diff = 1;
2619} else if (!strcmp(arg, "--no-commit-id")) {
2620revs->no_commit_id = 1;
2621} else if (!strcmp(arg, "--always")) {
2622revs->always_show_header = 1;
2623} else if (!strcmp(arg, "--no-abbrev")) {
2624revs->abbrev = 0;
2625} else if (!strcmp(arg, "--abbrev")) {
2626revs->abbrev = DEFAULT_ABBREV;
2627} else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2628revs->abbrev = strtoul(optarg, NULL, 10);
2629if (revs->abbrev < MINIMUM_ABBREV)
2630revs->abbrev = MINIMUM_ABBREV;
2631else if (revs->abbrev > hexsz)
2632revs->abbrev = hexsz;
2633} else if (!strcmp(arg, "--abbrev-commit")) {
2634revs->abbrev_commit = 1;
2635revs->abbrev_commit_given = 1;
2636} else if (!strcmp(arg, "--no-abbrev-commit")) {
2637revs->abbrev_commit = 0;
2638} else if (!strcmp(arg, "--full-diff")) {
2639revs->diff = 1;
2640revs->full_diff = 1;
2641} else if (!strcmp(arg, "--show-pulls")) {
2642revs->show_pulls = 1;
2643} else if (!strcmp(arg, "--full-history")) {
2644revs->simplify_history = 0;
2645} else if (!strcmp(arg, "--relative-date")) {
2646revs->date_mode.type = DATE_RELATIVE;
2647revs->date_mode_explicit = 1;
2648} else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2649parse_date_format(optarg, &revs->date_mode);
2650revs->date_mode_explicit = 1;
2651return argcount;
2652} else if (!strcmp(arg, "--log-size")) {
2653revs->show_log_size = 1;
2654}
2655/*
2656* Grepping the commit log
2657*/
2658else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2659add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2660return argcount;
2661} else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2662add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2663return argcount;
2664} else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2665add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2666return argcount;
2667} else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2668add_message_grep(revs, optarg);
2669return argcount;
2670} else if (!strcmp(arg, "--basic-regexp")) {
2671revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2672} else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2673revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2674} else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2675revs->grep_filter.ignore_case = 1;
2676revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2677} else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2678revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2679} else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2680revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2681} else if (!strcmp(arg, "--all-match")) {
2682revs->grep_filter.all_match = 1;
2683} else if (!strcmp(arg, "--invert-grep")) {
2684revs->grep_filter.no_body_match = 1;
2685} else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2686free(git_log_output_encoding);
2687if (strcmp(optarg, "none"))
2688git_log_output_encoding = xstrdup(optarg);
2689else
2690git_log_output_encoding = xstrdup("");
2691return argcount;
2692} else if (!strcmp(arg, "--reverse")) {
2693revs->reverse ^= 1;
2694} else if (!strcmp(arg, "--children")) {
2695revs->children.name = "children";
2696revs->limited = 1;
2697} else if (!strcmp(arg, "--ignore-missing")) {
2698revs->ignore_missing = 1;
2699} else if (opt && opt->allow_exclude_promisor_objects &&
2700!strcmp(arg, "--exclude-promisor-objects")) {
2701if (fetch_if_missing)
2702BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2703revs->exclude_promisor_objects = 1;
2704} else {
2705int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2706if (!opts)
2707unkv[(*unkc)++] = arg;
2708return opts;
2709}
2710
2711return 1;
2712}
2713
2714void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2715const struct option *options,
2716const char * const usagestr[])
2717{
2718int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2719&ctx->cpidx, ctx->out, NULL);
2720if (n <= 0) {
2721error("unknown option `%s'", ctx->argv[0]);
2722usage_with_options(usagestr, options);
2723}
2724ctx->argv += n;
2725ctx->argc -= n;
2726}
2727
2728void revision_opts_finish(struct rev_info *revs)
2729{
2730if (revs->graph && revs->track_linear)
2731die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2732
2733if (revs->graph) {
2734revs->topo_order = 1;
2735revs->rewrite_parents = 1;
2736}
2737}
2738
2739static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2740void *cb_data, const char *term)
2741{
2742struct strbuf bisect_refs = STRBUF_INIT;
2743int status;
2744strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2745status = refs_for_each_fullref_in(refs, bisect_refs.buf, NULL, fn, cb_data);
2746strbuf_release(&bisect_refs);
2747return status;
2748}
2749
2750static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2751{
2752return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2753}
2754
2755static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2756{
2757return for_each_bisect_ref(refs, fn, cb_data, term_good);
2758}
2759
2760static int handle_revision_pseudo_opt(struct rev_info *revs,
2761const char **argv, int *flags)
2762{
2763const char *arg = argv[0];
2764const char *optarg;
2765struct ref_store *refs;
2766int argcount;
2767
2768if (revs->repo != the_repository) {
2769/*
2770* We need some something like get_submodule_worktrees()
2771* before we can go through all worktrees of a submodule,
2772* .e.g with adding all HEADs from --all, which is not
2773* supported right now, so stick to single worktree.
2774*/
2775if (!revs->single_worktree)
2776BUG("--single-worktree cannot be used together with submodule");
2777}
2778refs = get_main_ref_store(revs->repo);
2779
2780/*
2781* NOTE!
2782*
2783* Commands like "git shortlog" will not accept the options below
2784* unless parse_revision_opt queues them (as opposed to erroring
2785* out).
2786*
2787* When implementing your new pseudo-option, remember to
2788* register it in the list at the top of handle_revision_opt.
2789*/
2790if (!strcmp(arg, "--all")) {
2791handle_refs(refs, revs, *flags, refs_for_each_ref);
2792handle_refs(refs, revs, *flags, refs_head_ref);
2793if (!revs->single_worktree) {
2794struct all_refs_cb cb;
2795
2796init_all_refs_cb(&cb, revs, *flags);
2797other_head_refs(handle_one_ref, &cb);
2798}
2799clear_ref_exclusions(&revs->ref_excludes);
2800} else if (!strcmp(arg, "--branches")) {
2801if (revs->ref_excludes.hidden_refs_configured)
2802return error(_("options '%s' and '%s' cannot be used together"),
2803"--exclude-hidden", "--branches");
2804handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2805clear_ref_exclusions(&revs->ref_excludes);
2806} else if (!strcmp(arg, "--bisect")) {
2807read_bisect_terms(&term_bad, &term_good);
2808handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2809handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2810for_each_good_bisect_ref);
2811revs->bisect = 1;
2812} else if (!strcmp(arg, "--tags")) {
2813if (revs->ref_excludes.hidden_refs_configured)
2814return error(_("options '%s' and '%s' cannot be used together"),
2815"--exclude-hidden", "--tags");
2816handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2817clear_ref_exclusions(&revs->ref_excludes);
2818} else if (!strcmp(arg, "--remotes")) {
2819if (revs->ref_excludes.hidden_refs_configured)
2820return error(_("options '%s' and '%s' cannot be used together"),
2821"--exclude-hidden", "--remotes");
2822handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2823clear_ref_exclusions(&revs->ref_excludes);
2824} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2825struct all_refs_cb cb;
2826init_all_refs_cb(&cb, revs, *flags);
2827refs_for_each_glob_ref(get_main_ref_store(the_repository),
2828handle_one_ref, optarg, &cb);
2829clear_ref_exclusions(&revs->ref_excludes);
2830return argcount;
2831} else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2832add_ref_exclusion(&revs->ref_excludes, optarg);
2833return argcount;
2834} else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2835exclude_hidden_refs(&revs->ref_excludes, optarg);
2836return argcount;
2837} else if (skip_prefix(arg, "--branches=", &optarg)) {
2838struct all_refs_cb cb;
2839if (revs->ref_excludes.hidden_refs_configured)
2840return error(_("options '%s' and '%s' cannot be used together"),
2841"--exclude-hidden", "--branches");
2842init_all_refs_cb(&cb, revs, *flags);
2843refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2844handle_one_ref, optarg,
2845"refs/heads/", &cb);
2846clear_ref_exclusions(&revs->ref_excludes);
2847} else if (skip_prefix(arg, "--tags=", &optarg)) {
2848struct all_refs_cb cb;
2849if (revs->ref_excludes.hidden_refs_configured)
2850return error(_("options '%s' and '%s' cannot be used together"),
2851"--exclude-hidden", "--tags");
2852init_all_refs_cb(&cb, revs, *flags);
2853refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2854handle_one_ref, optarg,
2855"refs/tags/", &cb);
2856clear_ref_exclusions(&revs->ref_excludes);
2857} else if (skip_prefix(arg, "--remotes=", &optarg)) {
2858struct all_refs_cb cb;
2859if (revs->ref_excludes.hidden_refs_configured)
2860return error(_("options '%s' and '%s' cannot be used together"),
2861"--exclude-hidden", "--remotes");
2862init_all_refs_cb(&cb, revs, *flags);
2863refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
2864handle_one_ref, optarg,
2865"refs/remotes/", &cb);
2866clear_ref_exclusions(&revs->ref_excludes);
2867} else if (!strcmp(arg, "--reflog")) {
2868add_reflogs_to_pending(revs, *flags);
2869} else if (!strcmp(arg, "--indexed-objects")) {
2870add_index_objects_to_pending(revs, *flags);
2871} else if (!strcmp(arg, "--alternate-refs")) {
2872add_alternate_refs_to_pending(revs, *flags);
2873} else if (!strcmp(arg, "--not")) {
2874*flags ^= UNINTERESTING | BOTTOM;
2875} else if (!strcmp(arg, "--no-walk")) {
2876revs->no_walk = 1;
2877} else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2878/*
2879* Detached form ("--no-walk X" as opposed to "--no-walk=X")
2880* not allowed, since the argument is optional.
2881*/
2882revs->no_walk = 1;
2883if (!strcmp(optarg, "sorted"))
2884revs->unsorted_input = 0;
2885else if (!strcmp(optarg, "unsorted"))
2886revs->unsorted_input = 1;
2887else
2888return error("invalid argument to --no-walk");
2889} else if (!strcmp(arg, "--do-walk")) {
2890revs->no_walk = 0;
2891} else if (!strcmp(arg, "--single-worktree")) {
2892revs->single_worktree = 1;
2893} else if (skip_prefix(arg, ("--filter="), &arg)) {
2894parse_list_objects_filter(&revs->filter, arg);
2895} else if (!strcmp(arg, ("--no-filter"))) {
2896list_objects_filter_set_no_filter(&revs->filter);
2897} else {
2898return 0;
2899}
2900
2901return 1;
2902}
2903
2904static void read_revisions_from_stdin(struct rev_info *revs,
2905struct strvec *prune)
2906{
2907struct strbuf sb;
2908int seen_dashdash = 0;
2909int seen_end_of_options = 0;
2910int save_warning;
2911int flags = 0;
2912
2913save_warning = warn_on_object_refname_ambiguity;
2914warn_on_object_refname_ambiguity = 0;
2915
2916strbuf_init(&sb, 1000);
2917while (strbuf_getline(&sb, stdin) != EOF) {
2918if (!sb.len)
2919break;
2920
2921if (!strcmp(sb.buf, "--")) {
2922seen_dashdash = 1;
2923break;
2924}
2925
2926if (!seen_end_of_options && sb.buf[0] == '-') {
2927const char *argv[] = { sb.buf, NULL };
2928
2929if (!strcmp(sb.buf, "--end-of-options")) {
2930seen_end_of_options = 1;
2931continue;
2932}
2933
2934if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2935continue;
2936
2937die(_("invalid option '%s' in --stdin mode"), sb.buf);
2938}
2939
2940if (handle_revision_arg(sb.buf, revs, flags,
2941REVARG_CANNOT_BE_FILENAME))
2942die("bad revision '%s'", sb.buf);
2943}
2944if (seen_dashdash)
2945read_pathspec_from_stdin(&sb, prune);
2946
2947strbuf_release(&sb);
2948warn_on_object_refname_ambiguity = save_warning;
2949}
2950
2951static void NORETURN diagnose_missing_default(const char *def)
2952{
2953int flags;
2954const char *refname;
2955
2956refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2957def, 0, NULL, &flags);
2958if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2959die(_("your current branch appears to be broken"));
2960
2961skip_prefix(refname, "refs/heads/", &refname);
2962die(_("your current branch '%s' does not have any commits yet"),
2963refname);
2964}
2965
2966/*
2967* Parse revision information, filling in the "rev_info" structure,
2968* and removing the used arguments from the argument list.
2969*
2970* Returns the number of arguments left that weren't recognized
2971* (which are also moved to the head of the argument list)
2972*/
2973int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2974{
2975int i, flags, left, seen_dashdash, revarg_opt;
2976struct strvec prune_data = STRVEC_INIT;
2977int seen_end_of_options = 0;
2978
2979/* First, search for "--" */
2980if (opt && opt->assume_dashdash) {
2981seen_dashdash = 1;
2982} else {
2983seen_dashdash = 0;
2984for (i = 1; i < argc; i++) {
2985const char *arg = argv[i];
2986if (strcmp(arg, "--"))
2987continue;
2988if (opt && opt->free_removed_argv_elements)
2989free((char *)argv[i]);
2990argv[i] = NULL;
2991argc = i;
2992if (argv[i + 1])
2993strvec_pushv(&prune_data, argv + i + 1);
2994seen_dashdash = 1;
2995break;
2996}
2997}
2998
2999/* Second, deal with arguments and options */
3000flags = 0;
3001revarg_opt = opt ? opt->revarg_opt : 0;
3002if (seen_dashdash)
3003revarg_opt |= REVARG_CANNOT_BE_FILENAME;
3004for (left = i = 1; i < argc; i++) {
3005const char *arg = argv[i];
3006if (!seen_end_of_options && *arg == '-') {
3007int opts;
3008
3009opts = handle_revision_pseudo_opt(
3010revs, argv + i,
3011&flags);
3012if (opts > 0) {
3013i += opts - 1;
3014continue;
3015}
3016
3017if (!strcmp(arg, "--stdin")) {
3018if (revs->disable_stdin) {
3019argv[left++] = arg;
3020continue;
3021}
3022if (revs->read_from_stdin++)
3023die("--stdin given twice?");
3024read_revisions_from_stdin(revs, &prune_data);
3025continue;
3026}
3027
3028if (!strcmp(arg, "--end-of-options")) {
3029seen_end_of_options = 1;
3030continue;
3031}
3032
3033opts = handle_revision_opt(revs, argc - i, argv + i,
3034&left, argv, opt);
3035if (opts > 0) {
3036i += opts - 1;
3037continue;
3038}
3039if (opts < 0)
3040exit(128);
3041continue;
3042}
3043
3044
3045if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
3046int j;
3047if (seen_dashdash || *arg == '^')
3048die("bad revision '%s'", arg);
3049
3050/* If we didn't have a "--":
3051* (1) all filenames must exist;
3052* (2) all rev-args must not be interpretable
3053* as a valid filename.
3054* but the latter we have checked in the main loop.
3055*/
3056for (j = i; j < argc; j++)
3057verify_filename(revs->prefix, argv[j], j == i);
3058
3059strvec_pushv(&prune_data, argv + i);
3060break;
3061}
3062}
3063revision_opts_finish(revs);
3064
3065if (prune_data.nr) {
3066/*
3067* If we need to introduce the magic "a lone ':' means no
3068* pathspec whatsoever", here is the place to do so.
3069*
3070* if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3071* prune_data.nr = 0;
3072* prune_data.alloc = 0;
3073* free(prune_data.path);
3074* prune_data.path = NULL;
3075* } else {
3076* terminate prune_data.alloc with NULL and
3077* call init_pathspec() to set revs->prune_data here.
3078* }
3079*/
3080parse_pathspec(&revs->prune_data, 0, 0,
3081revs->prefix, prune_data.v);
3082}
3083strvec_clear(&prune_data);
3084
3085if (!revs->def)
3086revs->def = opt ? opt->def : NULL;
3087if (opt && opt->tweak)
3088opt->tweak(revs);
3089if (revs->show_merge)
3090prepare_show_merge(revs);
3091if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3092struct object_id oid;
3093struct object *object;
3094struct object_context oc;
3095if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3096diagnose_missing_default(revs->def);
3097object = get_reference(revs, revs->def, &oid, 0);
3098add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3099object_context_release(&oc);
3100}
3101
3102/* Did the user ask for any diff output? Run the diff! */
3103if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3104revs->diff = 1;
3105
3106/* Pickaxe, diff-filter and rename following need diffs */
3107if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3108revs->diffopt.filter ||
3109revs->diffopt.flags.follow_renames)
3110revs->diff = 1;
3111
3112if (revs->diffopt.objfind)
3113revs->simplify_history = 0;
3114
3115if (revs->line_level_traverse) {
3116if (want_ancestry(revs))
3117revs->limited = 1;
3118revs->topo_order = 1;
3119}
3120
3121if (revs->topo_order && !generation_numbers_enabled(the_repository))
3122revs->limited = 1;
3123
3124if (revs->prune_data.nr) {
3125copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3126/* Can't prune commits with rename following: the paths change.. */
3127if (!revs->diffopt.flags.follow_renames)
3128revs->prune = 1;
3129if (!revs->full_diff)
3130copy_pathspec(&revs->diffopt.pathspec,
3131&revs->prune_data);
3132}
3133
3134diff_merges_setup_revs(revs);
3135
3136revs->diffopt.abbrev = revs->abbrev;
3137
3138diff_setup_done(&revs->diffopt);
3139
3140if (!is_encoding_utf8(get_log_output_encoding()))
3141revs->grep_filter.ignore_locale = 1;
3142compile_grep_patterns(&revs->grep_filter);
3143
3144if (revs->reflog_info && revs->limited)
3145die("cannot combine --walk-reflogs with history-limiting options");
3146if (revs->rewrite_parents && revs->children.name)
3147die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3148if (revs->filter.choice && !revs->blob_objects)
3149die(_("object filtering requires --objects"));
3150
3151/*
3152* Limitations on the graph functionality
3153*/
3154die_for_incompatible_opt3(!!revs->graph, "--graph",
3155!!revs->reverse, "--reverse",
3156!!revs->reflog_info, "--walk-reflogs");
3157
3158if (revs->no_walk && revs->graph)
3159die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3160if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3161die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3162
3163if (revs->line_level_traverse &&
3164(revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3165die(_("-L does not yet support diff formats besides -p and -s"));
3166
3167if (revs->expand_tabs_in_log < 0)
3168revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3169
3170if (!revs->show_notes_given && revs->show_notes_by_default) {
3171enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3172revs->show_notes_given = 1;
3173}
3174
3175return left;
3176}
3177
3178static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3179{
3180unsigned int i;
3181
3182for (i = 0; i < cmdline->nr; i++)
3183free((char *)cmdline->rev[i].name);
3184free(cmdline->rev);
3185}
3186
3187static void release_revisions_mailmap(struct string_list *mailmap)
3188{
3189if (!mailmap)
3190return;
3191clear_mailmap(mailmap);
3192free(mailmap);
3193}
3194
3195static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3196
3197static void free_void_commit_list(void *list)
3198{
3199free_commit_list(list);
3200}
3201
3202void release_revisions(struct rev_info *revs)
3203{
3204free_commit_list(revs->commits);
3205free_commit_list(revs->ancestry_path_bottoms);
3206release_display_notes(&revs->notes_opt);
3207object_array_clear(&revs->pending);
3208object_array_clear(&revs->boundary_commits);
3209release_revisions_cmdline(&revs->cmdline);
3210list_objects_filter_release(&revs->filter);
3211clear_pathspec(&revs->prune_data);
3212date_mode_release(&revs->date_mode);
3213release_revisions_mailmap(revs->mailmap);
3214free_grep_patterns(&revs->grep_filter);
3215graph_clear(revs->graph);
3216diff_free(&revs->diffopt);
3217diff_free(&revs->pruning);
3218reflog_walk_info_release(revs->reflog_info);
3219release_revisions_topo_walk_info(revs->topo_walk_info);
3220clear_decoration(&revs->children, free_void_commit_list);
3221clear_decoration(&revs->merge_simplification, free);
3222clear_decoration(&revs->treesame, free);
3223line_log_free(revs);
3224oidset_clear(&revs->missing_commits);
3225}
3226
3227static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3228{
3229struct commit_list *l = xcalloc(1, sizeof(*l));
3230
3231l->item = child;
3232l->next = add_decoration(&revs->children, &parent->object, l);
3233}
3234
3235static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3236{
3237struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3238struct commit_list **pp, *p;
3239int surviving_parents;
3240
3241/* Examine existing parents while marking ones we have seen... */
3242pp = &commit->parents;
3243surviving_parents = 0;
3244while ((p = *pp) != NULL) {
3245struct commit *parent = p->item;
3246if (parent->object.flags & TMP_MARK) {
3247*pp = p->next;
3248if (ts)
3249compact_treesame(revs, commit, surviving_parents);
3250continue;
3251}
3252parent->object.flags |= TMP_MARK;
3253surviving_parents++;
3254pp = &p->next;
3255}
3256/* clear the temporary mark */
3257for (p = commit->parents; p; p = p->next) {
3258p->item->object.flags &= ~TMP_MARK;
3259}
3260/* no update_treesame() - removing duplicates can't affect TREESAME */
3261return surviving_parents;
3262}
3263
3264struct merge_simplify_state {
3265struct commit *simplified;
3266};
3267
3268static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3269{
3270struct merge_simplify_state *st;
3271
3272st = lookup_decoration(&revs->merge_simplification, &commit->object);
3273if (!st) {
3274CALLOC_ARRAY(st, 1);
3275add_decoration(&revs->merge_simplification, &commit->object, st);
3276}
3277return st;
3278}
3279
3280static int mark_redundant_parents(struct commit *commit)
3281{
3282struct commit_list *h = reduce_heads(commit->parents);
3283int i = 0, marked = 0;
3284struct commit_list *po, *pn;
3285
3286/* Want these for sanity-checking only */
3287int orig_cnt = commit_list_count(commit->parents);
3288int cnt = commit_list_count(h);
3289
3290/*
3291* Not ready to remove items yet, just mark them for now, based
3292* on the output of reduce_heads(). reduce_heads outputs the reduced
3293* set in its original order, so this isn't too hard.
3294*/
3295po = commit->parents;
3296pn = h;
3297while (po) {
3298if (pn && po->item == pn->item) {
3299pn = pn->next;
3300i++;
3301} else {
3302po->item->object.flags |= TMP_MARK;
3303marked++;
3304}
3305po=po->next;
3306}
3307
3308if (i != cnt || cnt+marked != orig_cnt)
3309die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3310
3311free_commit_list(h);
3312
3313return marked;
3314}
3315
3316static int mark_treesame_root_parents(struct commit *commit)
3317{
3318struct commit_list *p;
3319int marked = 0;
3320
3321for (p = commit->parents; p; p = p->next) {
3322struct commit *parent = p->item;
3323if (!parent->parents && (parent->object.flags & TREESAME)) {
3324parent->object.flags |= TMP_MARK;
3325marked++;
3326}
3327}
3328
3329return marked;
3330}
3331
3332/*
3333* Awkward naming - this means one parent we are TREESAME to.
3334* cf mark_treesame_root_parents: root parents that are TREESAME (to an
3335* empty tree). Better name suggestions?
3336*/
3337static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3338{
3339struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3340struct commit *unmarked = NULL, *marked = NULL;
3341struct commit_list *p;
3342unsigned n;
3343
3344for (p = commit->parents, n = 0; p; p = p->next, n++) {
3345if (ts->treesame[n]) {
3346if (p->item->object.flags & TMP_MARK) {
3347if (!marked)
3348marked = p->item;
3349} else {
3350if (!unmarked) {
3351unmarked = p->item;
3352break;
3353}
3354}
3355}
3356}
3357
3358/*
3359* If we are TREESAME to a marked-for-deletion parent, but not to any
3360* unmarked parents, unmark the first TREESAME parent. This is the
3361* parent that the default simplify_history==1 scan would have followed,
3362* and it doesn't make sense to omit that path when asking for a
3363* simplified full history. Retaining it improves the chances of
3364* understanding odd missed merges that took an old version of a file.
3365*
3366* Example:
3367*
3368* I--------*X A modified the file, but mainline merge X used
3369* \ / "-s ours", so took the version from I. X is
3370* `-*A--' TREESAME to I and !TREESAME to A.
3371*
3372* Default log from X would produce "I". Without this check,
3373* --full-history --simplify-merges would produce "I-A-X", showing
3374* the merge commit X and that it changed A, but not making clear that
3375* it had just taken the I version. With this check, the topology above
3376* is retained.
3377*
3378* Note that it is possible that the simplification chooses a different
3379* TREESAME parent from the default, in which case this test doesn't
3380* activate, and we _do_ drop the default parent. Example:
3381*
3382* I------X A modified the file, but it was reverted in B,
3383* \ / meaning mainline merge X is TREESAME to both
3384* *A-*B parents.
3385*
3386* Default log would produce "I" by following the first parent;
3387* --full-history --simplify-merges will produce "I-A-B". But this is a
3388* reasonable result - it presents a logical full history leading from
3389* I to X, and X is not an important merge.
3390*/
3391if (!unmarked && marked) {
3392marked->object.flags &= ~TMP_MARK;
3393return 1;
3394}
3395
3396return 0;
3397}
3398
3399static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3400{
3401struct commit_list **pp, *p;
3402int nth_parent, removed = 0;
3403
3404pp = &commit->parents;
3405nth_parent = 0;
3406while ((p = *pp) != NULL) {
3407struct commit *parent = p->item;
3408if (parent->object.flags & TMP_MARK) {
3409parent->object.flags &= ~TMP_MARK;
3410*pp = p->next;
3411free(p);
3412removed++;
3413compact_treesame(revs, commit, nth_parent);
3414continue;
3415}
3416pp = &p->next;
3417nth_parent++;
3418}
3419
3420/* Removing parents can only increase TREESAMEness */
3421if (removed && !(commit->object.flags & TREESAME))
3422update_treesame(revs, commit);
3423
3424return nth_parent;
3425}
3426
3427static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3428{
3429struct commit_list *p;
3430struct commit *parent;
3431struct merge_simplify_state *st, *pst;
3432int cnt;
3433
3434st = locate_simplify_state(revs, commit);
3435
3436/*
3437* Have we handled this one?
3438*/
3439if (st->simplified)
3440return tail;
3441
3442/*
3443* An UNINTERESTING commit simplifies to itself, so does a
3444* root commit. We do not rewrite parents of such commit
3445* anyway.
3446*/
3447if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3448st->simplified = commit;
3449return tail;
3450}
3451
3452/*
3453* Do we know what commit all of our parents that matter
3454* should be rewritten to? Otherwise we are not ready to
3455* rewrite this one yet.
3456*/
3457for (cnt = 0, p = commit->parents; p; p = p->next) {
3458pst = locate_simplify_state(revs, p->item);
3459if (!pst->simplified) {
3460tail = &commit_list_insert(p->item, tail)->next;
3461cnt++;
3462}
3463if (revs->first_parent_only)
3464break;
3465}
3466if (cnt) {
3467tail = &commit_list_insert(commit, tail)->next;
3468return tail;
3469}
3470
3471/*
3472* Rewrite our list of parents. Note that this cannot
3473* affect our TREESAME flags in any way - a commit is
3474* always TREESAME to its simplification.
3475*/
3476for (p = commit->parents; p; p = p->next) {
3477pst = locate_simplify_state(revs, p->item);
3478p->item = pst->simplified;
3479if (revs->first_parent_only)
3480break;
3481}
3482
3483if (revs->first_parent_only)
3484cnt = 1;
3485else
3486cnt = remove_duplicate_parents(revs, commit);
3487
3488/*
3489* It is possible that we are a merge and one side branch
3490* does not have any commit that touches the given paths;
3491* in such a case, the immediate parent from that branch
3492* will be rewritten to be the merge base.
3493*
3494* o----X X: the commit we are looking at;
3495* / / o: a commit that touches the paths;
3496* ---o----'
3497*
3498* Further, a merge of an independent branch that doesn't
3499* touch the path will reduce to a treesame root parent:
3500*
3501* ----o----X X: the commit we are looking at;
3502* / o: a commit that touches the paths;
3503* r r: a root commit not touching the paths
3504*
3505* Detect and simplify both cases.
3506*/
3507if (1 < cnt) {
3508int marked = mark_redundant_parents(commit);
3509marked += mark_treesame_root_parents(commit);
3510if (marked)
3511marked -= leave_one_treesame_to_parent(revs, commit);
3512if (marked)
3513cnt = remove_marked_parents(revs, commit);
3514}
3515
3516/*
3517* A commit simplifies to itself if it is a root, if it is
3518* UNINTERESTING, if it touches the given paths, or if it is a
3519* merge and its parents don't simplify to one relevant commit
3520* (the first two cases are already handled at the beginning of
3521* this function).
3522*
3523* Otherwise, it simplifies to what its sole relevant parent
3524* simplifies to.
3525*/
3526if (!cnt ||
3527(commit->object.flags & UNINTERESTING) ||
3528!(commit->object.flags & TREESAME) ||
3529(parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3530(revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3531st->simplified = commit;
3532else {
3533pst = locate_simplify_state(revs, parent);
3534st->simplified = pst->simplified;
3535}
3536return tail;
3537}
3538
3539static void simplify_merges(struct rev_info *revs)
3540{
3541struct commit_list *list, *next;
3542struct commit_list *yet_to_do, **tail;
3543struct commit *commit;
3544
3545if (!revs->prune)
3546return;
3547
3548/* feed the list reversed */
3549yet_to_do = NULL;
3550for (list = revs->commits; list; list = next) {
3551commit = list->item;
3552next = list->next;
3553/*
3554* Do not free(list) here yet; the original list
3555* is used later in this function.
3556*/
3557commit_list_insert(commit, &yet_to_do);
3558}
3559while (yet_to_do) {
3560list = yet_to_do;
3561yet_to_do = NULL;
3562tail = &yet_to_do;
3563while (list) {
3564commit = pop_commit(&list);
3565tail = simplify_one(revs, commit, tail);
3566}
3567}
3568
3569/* clean up the result, removing the simplified ones */
3570list = revs->commits;
3571revs->commits = NULL;
3572tail = &revs->commits;
3573while (list) {
3574struct merge_simplify_state *st;
3575
3576commit = pop_commit(&list);
3577st = locate_simplify_state(revs, commit);
3578if (st->simplified == commit)
3579tail = &commit_list_insert(commit, tail)->next;
3580}
3581}
3582
3583static void set_children(struct rev_info *revs)
3584{
3585struct commit_list *l;
3586for (l = revs->commits; l; l = l->next) {
3587struct commit *commit = l->item;
3588struct commit_list *p;
3589
3590for (p = commit->parents; p; p = p->next)
3591add_child(revs, p->item, commit);
3592}
3593}
3594
3595void reset_revision_walk(void)
3596{
3597clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3598}
3599
3600static int mark_uninteresting(const struct object_id *oid,
3601struct packed_git *pack UNUSED,
3602uint32_t pos UNUSED,
3603void *cb)
3604{
3605struct rev_info *revs = cb;
3606struct object *o = lookup_unknown_object(revs->repo, oid);
3607o->flags |= UNINTERESTING | SEEN;
3608return 0;
3609}
3610
3611define_commit_slab(indegree_slab, int);
3612define_commit_slab(author_date_slab, timestamp_t);
3613
3614struct topo_walk_info {
3615timestamp_t min_generation;
3616struct prio_queue explore_queue;
3617struct prio_queue indegree_queue;
3618struct prio_queue topo_queue;
3619struct indegree_slab indegree;
3620struct author_date_slab author_date;
3621};
3622
3623static int topo_walk_atexit_registered;
3624static unsigned int count_explore_walked;
3625static unsigned int count_indegree_walked;
3626static unsigned int count_topo_walked;
3627
3628static void trace2_topo_walk_statistics_atexit(void)
3629{
3630struct json_writer jw = JSON_WRITER_INIT;
3631
3632jw_object_begin(&jw, 0);
3633jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3634jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3635jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3636jw_end(&jw);
3637
3638trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3639
3640jw_release(&jw);
3641}
3642
3643static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3644{
3645if (c->object.flags & flag)
3646return;
3647
3648c->object.flags |= flag;
3649prio_queue_put(q, c);
3650}
3651
3652static void explore_walk_step(struct rev_info *revs)
3653{
3654struct topo_walk_info *info = revs->topo_walk_info;
3655struct commit_list *p;
3656struct commit *c = prio_queue_get(&info->explore_queue);
3657
3658if (!c)
3659return;
3660
3661if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3662return;
3663
3664count_explore_walked++;
3665
3666if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3667record_author_date(&info->author_date, c);
3668
3669if (revs->max_age != -1 && (c->date < revs->max_age))
3670c->object.flags |= UNINTERESTING;
3671
3672if (process_parents(revs, c, NULL, NULL) < 0)
3673return;
3674
3675if (c->object.flags & UNINTERESTING)
3676mark_parents_uninteresting(revs, c);
3677
3678for (p = c->parents; p; p = p->next)
3679test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3680}
3681
3682static void explore_to_depth(struct rev_info *revs,
3683timestamp_t gen_cutoff)
3684{
3685struct topo_walk_info *info = revs->topo_walk_info;
3686struct commit *c;
3687while ((c = prio_queue_peek(&info->explore_queue)) &&
3688commit_graph_generation(c) >= gen_cutoff)
3689explore_walk_step(revs);
3690}
3691
3692static void indegree_walk_step(struct rev_info *revs)
3693{
3694struct commit_list *p;
3695struct topo_walk_info *info = revs->topo_walk_info;
3696struct commit *c = prio_queue_get(&info->indegree_queue);
3697
3698if (!c)
3699return;
3700
3701if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3702return;
3703
3704count_indegree_walked++;
3705
3706explore_to_depth(revs, commit_graph_generation(c));
3707
3708for (p = c->parents; p; p = p->next) {
3709struct commit *parent = p->item;
3710int *pi = indegree_slab_at(&info->indegree, parent);
3711
3712if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3713return;
3714
3715if (*pi)
3716(*pi)++;
3717else
3718*pi = 2;
3719
3720test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3721
3722if (revs->first_parent_only)
3723return;
3724}
3725}
3726
3727static void compute_indegrees_to_depth(struct rev_info *revs,
3728timestamp_t gen_cutoff)
3729{
3730struct topo_walk_info *info = revs->topo_walk_info;
3731struct commit *c;
3732while ((c = prio_queue_peek(&info->indegree_queue)) &&
3733commit_graph_generation(c) >= gen_cutoff)
3734indegree_walk_step(revs);
3735}
3736
3737static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3738{
3739if (!info)
3740return;
3741clear_prio_queue(&info->explore_queue);
3742clear_prio_queue(&info->indegree_queue);
3743clear_prio_queue(&info->topo_queue);
3744clear_indegree_slab(&info->indegree);
3745clear_author_date_slab(&info->author_date);
3746free(info);
3747}
3748
3749static void reset_topo_walk(struct rev_info *revs)
3750{
3751release_revisions_topo_walk_info(revs->topo_walk_info);
3752revs->topo_walk_info = NULL;
3753}
3754
3755static void init_topo_walk(struct rev_info *revs)
3756{
3757struct topo_walk_info *info;
3758struct commit_list *list;
3759if (revs->topo_walk_info)
3760reset_topo_walk(revs);
3761
3762revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3763info = revs->topo_walk_info;
3764memset(info, 0, sizeof(struct topo_walk_info));
3765
3766init_indegree_slab(&info->indegree);
3767memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3768memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3769memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3770
3771switch (revs->sort_order) {
3772default: /* REV_SORT_IN_GRAPH_ORDER */
3773info->topo_queue.compare = NULL;
3774break;
3775case REV_SORT_BY_COMMIT_DATE:
3776info->topo_queue.compare = compare_commits_by_commit_date;
3777break;
3778case REV_SORT_BY_AUTHOR_DATE:
3779init_author_date_slab(&info->author_date);
3780info->topo_queue.compare = compare_commits_by_author_date;
3781info->topo_queue.cb_data = &info->author_date;
3782break;
3783}
3784
3785info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3786info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3787
3788info->min_generation = GENERATION_NUMBER_INFINITY;
3789for (list = revs->commits; list; list = list->next) {
3790struct commit *c = list->item;
3791timestamp_t generation;
3792
3793if (repo_parse_commit_gently(revs->repo, c, 1))
3794continue;
3795
3796test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3797test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3798
3799generation = commit_graph_generation(c);
3800if (generation < info->min_generation)
3801info->min_generation = generation;
3802
3803*(indegree_slab_at(&info->indegree, c)) = 1;
3804
3805if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3806record_author_date(&info->author_date, c);
3807}
3808compute_indegrees_to_depth(revs, info->min_generation);
3809
3810for (list = revs->commits; list; list = list->next) {
3811struct commit *c = list->item;
3812
3813if (*(indegree_slab_at(&info->indegree, c)) == 1)
3814prio_queue_put(&info->topo_queue, c);
3815}
3816
3817/*
3818* This is unfortunate; the initial tips need to be shown
3819* in the order given from the revision traversal machinery.
3820*/
3821if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3822prio_queue_reverse(&info->topo_queue);
3823
3824if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3825atexit(trace2_topo_walk_statistics_atexit);
3826topo_walk_atexit_registered = 1;
3827}
3828}
3829
3830static struct commit *next_topo_commit(struct rev_info *revs)
3831{
3832struct commit *c;
3833struct topo_walk_info *info = revs->topo_walk_info;
3834
3835/* pop next off of topo_queue */
3836c = prio_queue_get(&info->topo_queue);
3837
3838if (c)
3839*(indegree_slab_at(&info->indegree, c)) = 0;
3840
3841return c;
3842}
3843
3844static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3845{
3846struct commit_list *p;
3847struct topo_walk_info *info = revs->topo_walk_info;
3848if (process_parents(revs, commit, NULL, NULL) < 0) {
3849if (!revs->ignore_missing_links)
3850die("Failed to traverse parents of commit %s",
3851oid_to_hex(&commit->object.oid));
3852}
3853
3854count_topo_walked++;
3855
3856for (p = commit->parents; p; p = p->next) {
3857struct commit *parent = p->item;
3858int *pi;
3859timestamp_t generation;
3860
3861if (parent->object.flags & UNINTERESTING)
3862continue;
3863
3864if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3865continue;
3866
3867generation = commit_graph_generation(parent);
3868if (generation < info->min_generation) {
3869info->min_generation = generation;
3870compute_indegrees_to_depth(revs, info->min_generation);
3871}
3872
3873pi = indegree_slab_at(&info->indegree, parent);
3874
3875(*pi)--;
3876if (*pi == 1)
3877prio_queue_put(&info->topo_queue, parent);
3878
3879if (revs->first_parent_only)
3880return;
3881}
3882}
3883
3884int prepare_revision_walk(struct rev_info *revs)
3885{
3886int i;
3887struct object_array old_pending;
3888struct commit_list **next = &revs->commits;
3889
3890memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3891revs->pending.nr = 0;
3892revs->pending.alloc = 0;
3893revs->pending.objects = NULL;
3894for (i = 0; i < old_pending.nr; i++) {
3895struct object_array_entry *e = old_pending.objects + i;
3896struct commit *commit = handle_commit(revs, e);
3897if (commit) {
3898if (!(commit->object.flags & SEEN)) {
3899commit->object.flags |= SEEN;
3900next = commit_list_append(commit, next);
3901}
3902}
3903}
3904object_array_clear(&old_pending);
3905
3906/* Signal whether we need per-parent treesame decoration */
3907if (revs->simplify_merges ||
3908(revs->limited && limiting_can_increase_treesame(revs)))
3909revs->treesame.name = "treesame";
3910
3911if (revs->exclude_promisor_objects) {
3912for_each_packed_object(mark_uninteresting, revs,
3913FOR_EACH_OBJECT_PROMISOR_ONLY);
3914}
3915
3916if (!revs->reflog_info)
3917prepare_to_use_bloom_filter(revs);
3918if (!revs->unsorted_input)
3919commit_list_sort_by_date(&revs->commits);
3920if (revs->no_walk)
3921return 0;
3922if (revs->limited) {
3923if (limit_list(revs) < 0)
3924return -1;
3925if (revs->topo_order)
3926sort_in_topological_order(&revs->commits, revs->sort_order);
3927} else if (revs->topo_order)
3928init_topo_walk(revs);
3929if (revs->line_level_traverse && want_ancestry(revs))
3930/*
3931* At the moment we can only do line-level log with parent
3932* rewriting by performing this expensive pre-filtering step.
3933* If parent rewriting is not requested, then we rather
3934* perform the line-level log filtering during the regular
3935* history traversal.
3936*/
3937line_log_filter(revs);
3938if (revs->simplify_merges)
3939simplify_merges(revs);
3940if (revs->children.name)
3941set_children(revs);
3942
3943return 0;
3944}
3945
3946static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3947struct commit **pp,
3948struct prio_queue *queue)
3949{
3950for (;;) {
3951struct commit *p = *pp;
3952if (!revs->limited)
3953if (process_parents(revs, p, NULL, queue) < 0)
3954return rewrite_one_error;
3955if (p->object.flags & UNINTERESTING)
3956return rewrite_one_ok;
3957if (!(p->object.flags & TREESAME))
3958return rewrite_one_ok;
3959if (!p->parents)
3960return rewrite_one_noparents;
3961if (!(p = one_relevant_parent(revs, p->parents)))
3962return rewrite_one_ok;
3963*pp = p;
3964}
3965}
3966
3967static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3968{
3969while (q->nr) {
3970struct commit *item = prio_queue_peek(q);
3971struct commit_list *p = *list;
3972
3973if (p && p->item->date >= item->date)
3974list = &p->next;
3975else {
3976p = commit_list_insert(item, list);
3977list = &p->next; /* skip newly added item */
3978prio_queue_get(q); /* pop item */
3979}
3980}
3981}
3982
3983static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3984{
3985struct prio_queue queue = { compare_commits_by_commit_date };
3986enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3987merge_queue_into_list(&queue, &revs->commits);
3988clear_prio_queue(&queue);
3989return ret;
3990}
3991
3992int rewrite_parents(struct rev_info *revs, struct commit *commit,
3993rewrite_parent_fn_t rewrite_parent)
3994{
3995struct commit_list **pp = &commit->parents;
3996while (*pp) {
3997struct commit_list *parent = *pp;
3998switch (rewrite_parent(revs, &parent->item)) {
3999case rewrite_one_ok:
4000break;
4001case rewrite_one_noparents:
4002*pp = parent->next;
4003continue;
4004case rewrite_one_error:
4005return -1;
4006}
4007pp = &parent->next;
4008}
4009remove_duplicate_parents(revs, commit);
4010return 0;
4011}
4012
4013static int commit_match(struct commit *commit, struct rev_info *opt)
4014{
4015int retval;
4016const char *encoding;
4017const char *message;
4018struct strbuf buf = STRBUF_INIT;
4019
4020if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
4021return 1;
4022
4023/* Prepend "fake" headers as needed */
4024if (opt->grep_filter.use_reflog_filter) {
4025strbuf_addstr(&buf, "reflog ");
4026get_reflog_message(&buf, opt->reflog_info);
4027strbuf_addch(&buf, '\n');
4028}
4029
4030/*
4031* We grep in the user's output encoding, under the assumption that it
4032* is the encoding they are most likely to write their grep pattern
4033* for. In addition, it means we will match the "notes" encoding below,
4034* so we will not end up with a buffer that has two different encodings
4035* in it.
4036*/
4037encoding = get_log_output_encoding();
4038message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
4039
4040/* Copy the commit to temporary if we are using "fake" headers */
4041if (buf.len)
4042strbuf_addstr(&buf, message);
4043
4044if (opt->grep_filter.header_list && opt->mailmap) {
4045const char *commit_headers[] = { "author ", "committer ", NULL };
4046
4047if (!buf.len)
4048strbuf_addstr(&buf, message);
4049
4050apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4051}
4052
4053/* Append "fake" message parts as needed */
4054if (opt->show_notes) {
4055if (!buf.len)
4056strbuf_addstr(&buf, message);
4057format_display_notes(&commit->object.oid, &buf, encoding, 1);
4058}
4059
4060/*
4061* Find either in the original commit message, or in the temporary.
4062* Note that we cast away the constness of "message" here. It is
4063* const because it may come from the cached commit buffer. That's OK,
4064* because we know that it is modifiable heap memory, and that while
4065* grep_buffer may modify it for speed, it will restore any
4066* changes before returning.
4067*/
4068if (buf.len)
4069retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4070else
4071retval = grep_buffer(&opt->grep_filter,
4072(char *)message, strlen(message));
4073strbuf_release(&buf);
4074repo_unuse_commit_buffer(the_repository, commit, message);
4075return retval;
4076}
4077
4078static inline int want_ancestry(const struct rev_info *revs)
4079{
4080return (revs->rewrite_parents || revs->children.name);
4081}
4082
4083/*
4084* Return a timestamp to be used for --since/--until comparisons for this
4085* commit, based on the revision options.
4086*/
4087static timestamp_t comparison_date(const struct rev_info *revs,
4088struct commit *commit)
4089{
4090return revs->reflog_info ?
4091get_reflog_timestamp(revs->reflog_info) :
4092commit->date;
4093}
4094
4095enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4096{
4097if (commit->object.flags & SHOWN)
4098return commit_ignore;
4099if (revs->unpacked && has_object_pack(&commit->object.oid))
4100return commit_ignore;
4101if (revs->no_kept_objects) {
4102if (has_object_kept_pack(&commit->object.oid,
4103revs->keep_pack_cache_flags))
4104return commit_ignore;
4105}
4106if (commit->object.flags & UNINTERESTING)
4107return commit_ignore;
4108if (revs->line_level_traverse && !want_ancestry(revs)) {
4109/*
4110* In case of line-level log with parent rewriting
4111* prepare_revision_walk() already took care of all line-level
4112* log filtering, and there is nothing left to do here.
4113*
4114* If parent rewriting was not requested, then this is the
4115* place to perform the line-level log filtering. Notably,
4116* this check, though expensive, must come before the other,
4117* cheaper filtering conditions, because the tracked line
4118* ranges must be adjusted even when the commit will end up
4119* being ignored based on other conditions.
4120*/
4121if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4122return commit_ignore;
4123}
4124if (revs->min_age != -1 &&
4125comparison_date(revs, commit) > revs->min_age)
4126return commit_ignore;
4127if (revs->max_age_as_filter != -1 &&
4128comparison_date(revs, commit) < revs->max_age_as_filter)
4129return commit_ignore;
4130if (revs->min_parents || (revs->max_parents >= 0)) {
4131int n = commit_list_count(commit->parents);
4132if ((n < revs->min_parents) ||
4133((revs->max_parents >= 0) && (n > revs->max_parents)))
4134return commit_ignore;
4135}
4136if (!commit_match(commit, revs))
4137return commit_ignore;
4138if (revs->prune && revs->dense) {
4139/* Commit without changes? */
4140if (commit->object.flags & TREESAME) {
4141int n;
4142struct commit_list *p;
4143/* drop merges unless we want parenthood */
4144if (!want_ancestry(revs))
4145return commit_ignore;
4146
4147if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4148return commit_show;
4149
4150/*
4151* If we want ancestry, then need to keep any merges
4152* between relevant commits to tie together topology.
4153* For consistency with TREESAME and simplification
4154* use "relevant" here rather than just INTERESTING,
4155* to treat bottom commit(s) as part of the topology.
4156*/
4157for (n = 0, p = commit->parents; p; p = p->next)
4158if (relevant_commit(p->item))
4159if (++n >= 2)
4160return commit_show;
4161return commit_ignore;
4162}
4163}
4164return commit_show;
4165}
4166
4167define_commit_slab(saved_parents, struct commit_list *);
4168
4169#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4170
4171/*
4172* You may only call save_parents() once per commit (this is checked
4173* for non-root commits).
4174*/
4175static void save_parents(struct rev_info *revs, struct commit *commit)
4176{
4177struct commit_list **pp;
4178
4179if (!revs->saved_parents_slab) {
4180revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4181init_saved_parents(revs->saved_parents_slab);
4182}
4183
4184pp = saved_parents_at(revs->saved_parents_slab, commit);
4185
4186/*
4187* When walking with reflogs, we may visit the same commit
4188* several times: once for each appearance in the reflog.
4189*
4190* In this case, save_parents() will be called multiple times.
4191* We want to keep only the first set of parents. We need to
4192* store a sentinel value for an empty (i.e., NULL) parent
4193* list to distinguish it from a not-yet-saved list, however.
4194*/
4195if (*pp)
4196return;
4197if (commit->parents)
4198*pp = copy_commit_list(commit->parents);
4199else
4200*pp = EMPTY_PARENT_LIST;
4201}
4202
4203static void free_saved_parents(struct rev_info *revs)
4204{
4205if (revs->saved_parents_slab)
4206clear_saved_parents(revs->saved_parents_slab);
4207}
4208
4209struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4210{
4211struct commit_list *parents;
4212
4213if (!revs->saved_parents_slab)
4214return commit->parents;
4215
4216parents = *saved_parents_at(revs->saved_parents_slab, commit);
4217if (parents == EMPTY_PARENT_LIST)
4218return NULL;
4219return parents;
4220}
4221
4222enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4223{
4224enum commit_action action = get_commit_action(revs, commit);
4225
4226if (action == commit_show &&
4227revs->prune && revs->dense && want_ancestry(revs)) {
4228/*
4229* --full-diff on simplified parents is no good: it
4230* will show spurious changes from the commits that
4231* were elided. So we save the parents on the side
4232* when --full-diff is in effect.
4233*/
4234if (revs->full_diff)
4235save_parents(revs, commit);
4236if (rewrite_parents(revs, commit, rewrite_one) < 0)
4237return commit_error;
4238}
4239return action;
4240}
4241
4242static void track_linear(struct rev_info *revs, struct commit *commit)
4243{
4244if (revs->track_first_time) {
4245revs->linear = 1;
4246revs->track_first_time = 0;
4247} else {
4248struct commit_list *p;
4249for (p = revs->previous_parents; p; p = p->next)
4250if (p->item == NULL || /* first commit */
4251oideq(&p->item->object.oid, &commit->object.oid))
4252break;
4253revs->linear = p != NULL;
4254}
4255if (revs->reverse) {
4256if (revs->linear)
4257commit->object.flags |= TRACK_LINEAR;
4258}
4259free_commit_list(revs->previous_parents);
4260revs->previous_parents = copy_commit_list(commit->parents);
4261}
4262
4263static struct commit *get_revision_1(struct rev_info *revs)
4264{
4265while (1) {
4266struct commit *commit;
4267
4268if (revs->reflog_info)
4269commit = next_reflog_entry(revs->reflog_info);
4270else if (revs->topo_walk_info)
4271commit = next_topo_commit(revs);
4272else
4273commit = pop_commit(&revs->commits);
4274
4275if (!commit)
4276return NULL;
4277
4278if (revs->reflog_info)
4279commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4280
4281/*
4282* If we haven't done the list limiting, we need to look at
4283* the parents here. We also need to do the date-based limiting
4284* that we'd otherwise have done in limit_list().
4285*/
4286if (!revs->limited) {
4287if (revs->max_age != -1 &&
4288comparison_date(revs, commit) < revs->max_age)
4289continue;
4290
4291if (revs->reflog_info)
4292try_to_simplify_commit(revs, commit);
4293else if (revs->topo_walk_info)
4294expand_topo_walk(revs, commit);
4295else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4296if (!revs->ignore_missing_links)
4297die("Failed to traverse parents of commit %s",
4298oid_to_hex(&commit->object.oid));
4299}
4300}
4301
4302switch (simplify_commit(revs, commit)) {
4303case commit_ignore:
4304continue;
4305case commit_error:
4306die("Failed to simplify parents of commit %s",
4307oid_to_hex(&commit->object.oid));
4308default:
4309if (revs->track_linear)
4310track_linear(revs, commit);
4311return commit;
4312}
4313}
4314}
4315
4316/*
4317* Return true for entries that have not yet been shown. (This is an
4318* object_array_each_func_t.)
4319*/
4320static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4321{
4322return !(entry->item->flags & SHOWN);
4323}
4324
4325/*
4326* If array is on the verge of a realloc, garbage-collect any entries
4327* that have already been shown to try to free up some space.
4328*/
4329static void gc_boundary(struct object_array *array)
4330{
4331if (array->nr == array->alloc)
4332object_array_filter(array, entry_unshown, NULL);
4333}
4334
4335static void create_boundary_commit_list(struct rev_info *revs)
4336{
4337unsigned i;
4338struct commit *c;
4339struct object_array *array = &revs->boundary_commits;
4340struct object_array_entry *objects = array->objects;
4341
4342/*
4343* If revs->commits is non-NULL at this point, an error occurred in
4344* get_revision_1(). Ignore the error and continue printing the
4345* boundary commits anyway. (This is what the code has always
4346* done.)
4347*/
4348free_commit_list(revs->commits);
4349revs->commits = NULL;
4350
4351/*
4352* Put all of the actual boundary commits from revs->boundary_commits
4353* into revs->commits
4354*/
4355for (i = 0; i < array->nr; i++) {
4356c = (struct commit *)(objects[i].item);
4357if (!c)
4358continue;
4359if (!(c->object.flags & CHILD_SHOWN))
4360continue;
4361if (c->object.flags & (SHOWN | BOUNDARY))
4362continue;
4363c->object.flags |= BOUNDARY;
4364commit_list_insert(c, &revs->commits);
4365}
4366
4367/*
4368* If revs->topo_order is set, sort the boundary commits
4369* in topological order
4370*/
4371sort_in_topological_order(&revs->commits, revs->sort_order);
4372}
4373
4374static struct commit *get_revision_internal(struct rev_info *revs)
4375{
4376struct commit *c = NULL;
4377struct commit_list *l;
4378
4379if (revs->boundary == 2) {
4380/*
4381* All of the normal commits have already been returned,
4382* and we are now returning boundary commits.
4383* create_boundary_commit_list() has populated
4384* revs->commits with the remaining commits to return.
4385*/
4386c = pop_commit(&revs->commits);
4387if (c)
4388c->object.flags |= SHOWN;
4389return c;
4390}
4391
4392/*
4393* If our max_count counter has reached zero, then we are done. We
4394* don't simply return NULL because we still might need to show
4395* boundary commits. But we want to avoid calling get_revision_1, which
4396* might do a considerable amount of work finding the next commit only
4397* for us to throw it away.
4398*
4399* If it is non-zero, then either we don't have a max_count at all
4400* (-1), or it is still counting, in which case we decrement.
4401*/
4402if (revs->max_count) {
4403c = get_revision_1(revs);
4404if (c) {
4405while (revs->skip_count > 0) {
4406revs->skip_count--;
4407c = get_revision_1(revs);
4408if (!c)
4409break;
4410}
4411}
4412
4413if (revs->max_count > 0)
4414revs->max_count--;
4415}
4416
4417if (c)
4418c->object.flags |= SHOWN;
4419
4420if (!revs->boundary)
4421return c;
4422
4423if (!c) {
4424/*
4425* get_revision_1() runs out the commits, and
4426* we are done computing the boundaries.
4427* switch to boundary commits output mode.
4428*/
4429revs->boundary = 2;
4430
4431/*
4432* Update revs->commits to contain the list of
4433* boundary commits.
4434*/
4435create_boundary_commit_list(revs);
4436
4437return get_revision_internal(revs);
4438}
4439
4440/*
4441* boundary commits are the commits that are parents of the
4442* ones we got from get_revision_1() but they themselves are
4443* not returned from get_revision_1(). Before returning
4444* 'c', we need to mark its parents that they could be boundaries.
4445*/
4446
4447for (l = c->parents; l; l = l->next) {
4448struct object *p;
4449p = &(l->item->object);
4450if (p->flags & (CHILD_SHOWN | SHOWN))
4451continue;
4452p->flags |= CHILD_SHOWN;
4453gc_boundary(&revs->boundary_commits);
4454add_object_array(p, NULL, &revs->boundary_commits);
4455}
4456
4457return c;
4458}
4459
4460struct commit *get_revision(struct rev_info *revs)
4461{
4462struct commit *c;
4463struct commit_list *reversed;
4464
4465if (revs->reverse) {
4466reversed = NULL;
4467while ((c = get_revision_internal(revs)))
4468commit_list_insert(c, &reversed);
4469free_commit_list(revs->commits);
4470revs->commits = reversed;
4471revs->reverse = 0;
4472revs->reverse_output_stage = 1;
4473}
4474
4475if (revs->reverse_output_stage) {
4476c = pop_commit(&revs->commits);
4477if (revs->track_linear)
4478revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4479return c;
4480}
4481
4482c = get_revision_internal(revs);
4483if (c && revs->graph)
4484graph_update(revs->graph, c);
4485if (!c) {
4486free_saved_parents(revs);
4487free_commit_list(revs->previous_parents);
4488revs->previous_parents = NULL;
4489}
4490return c;
4491}
4492
4493const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4494{
4495if (commit->object.flags & BOUNDARY)
4496return "-";
4497else if (commit->object.flags & UNINTERESTING)
4498return "^";
4499else if (commit->object.flags & PATCHSAME)
4500return "=";
4501else if (!revs || revs->left_right) {
4502if (commit->object.flags & SYMMETRIC_LEFT)
4503return "<";
4504else
4505return ">";
4506} else if (revs->graph)
4507return "*";
4508else if (revs->cherry_mark)
4509return "+";
4510return "";
4511}
4512
4513void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4514{
4515const char *mark = get_revision_mark(revs, commit);
4516if (!strlen(mark))
4517return;
4518fputs(mark, stdout);
4519putchar(' ');
4520}
4521