Raw
1 #ifndef REVISION_H
2 #define REVISION_H
3
4 #include "commit.h"
5 #include "grep.h"
6 #include "notes.h"
7 #include "object-name.h"
8 #include "oidset.h"
9 #include "pretty.h"
10 #include "diff.h"
11 #include "commit-slab-decl.h"
12 #include "decorate.h"
13 #include "ident.h"
14 #include "list-objects-filter-options.h"
15 #include "prio-queue.h"
16 #include "strvec.h"
17
18 /**
19 * The revision walking API offers functions to build a list of revisions
20 * and then iterate over that list.
21 *
22 * Calling sequence
23 * ----------------
24 *
25 * The walking API has a given calling sequence: first you need to initialize
26 * a rev_info structure, then add revisions to control what kind of revision
27 * list do you want to get, finally you can iterate over the revision list.
28 *
29 */
30
31 /* Remember to update object flag allocation in object.h */
32 #define SEEN (1u<<0)
33 #define UNINTERESTING (1u<<1)
34 #define TREESAME (1u<<2)
35 #define SHOWN (1u<<3)
36 #define TMP_MARK (1u<<4) /* for isolated cases; clean after use */
37 #define BOUNDARY (1u<<5)
38 #define CHILD_SHOWN (1u<<6)
39 #define ADDED (1u<<7) /* Parents already parsed and added? */
40 #define SYMMETRIC_LEFT (1u<<8)
41 #define PATCHSAME (1u<<9)
42 #define BOTTOM (1u<<10)
43
44 /* WARNING: This is also used as REACHABLE in commit-graph.c. */
45 #define PULL_MERGE (1u<<15)
46
47 #define TOPO_WALK_EXPLORED (1u<<23)
48 #define TOPO_WALK_INDEGREE (1u<<24)
49
50 /*
51 * Indicates object was reached by traversal. i.e. not given by user on
52 * command-line or stdin.
53 */
54 #define NOT_USER_GIVEN (1u<<25)
55 #define TRACK_LINEAR (1u<<26)
56 #define ANCESTRY_PATH (1u<<27)
57 #define CHILD_VISITED (1u<<28)
58 #define ALL_REV_FLAGS (((1u<<11)-1) | NOT_USER_GIVEN | TRACK_LINEAR \
59 | PULL_MERGE | CHILD_VISITED)
60
61 #define DECORATE_SHORT_REFS 1
62 #define DECORATE_FULL_REFS 2
63
64 struct log_info;
65 struct repository;
66 struct rev_info;
67 struct string_list;
68 struct saved_parents;
69 struct follow_pathspec_slab;
70 struct bloom_keyvec;
71 struct bloom_filter;
72 struct bloom_filter_settings;
73 struct option;
74 struct parse_opt_ctx_t;
75 define_shared_commit_slab(revision_sources, char *);
76
77 struct rev_cmdline_info {
78 unsigned int nr;
79 unsigned int alloc;
80 struct rev_cmdline_entry {
81 struct object *item;
82 const char *name;
83 enum {
84 REV_CMD_REF,
85 REV_CMD_PARENTS_ONLY,
86 REV_CMD_LEFT,
87 REV_CMD_RIGHT,
88 REV_CMD_MERGE_BASE,
89 REV_CMD_REV
90 } whence;
91 unsigned flags;
92 } *rev;
93 };
94
95 struct ref_exclusions {
96 /*
97 * Excluded refs is a list of wildmatch patterns. If any of the
98 * patterns match, the reference will be excluded.
99 */
100 struct string_list excluded_refs;
101
102 /*
103 * Hidden refs is a list of patterns that is to be hidden via
104 * `ref_is_hidden()`.
105 */
106 struct strvec hidden_refs;
107
108 /*
109 * Indicates whether hidden refs have been configured. This is to
110 * distinguish between no hidden refs existing and hidden refs not
111 * being parsed.
112 */
113 char hidden_refs_configured;
114 };
115
116 /**
117 * Initialize a `struct ref_exclusions` with a macro.
118 */
119 #define REF_EXCLUSIONS_INIT { \
120 .excluded_refs = STRING_LIST_INIT_DUP, \
121 .hidden_refs = STRVEC_INIT, \
122 }
123
124 struct oidset;
125 struct topo_walk_info;
126
127 struct rev_info {
128 /*
129 * Work queue of commits, stored as either a linked list or a
130 * priority queue, but never both at the same time.
131 * rev_info_commit_list_to_queue() converts list to queue.
132 */
133 struct commit_list *commits;
134 struct prio_queue commit_queue;
135
136 struct object_array pending;
137 struct repository *repo;
138
139 /* Parents of shown commits */
140 struct object_array boundary_commits;
141
142 /* The end-points specified by the end user */
143 struct rev_cmdline_info cmdline;
144
145 /*
146 * Object filter options. No filtering is specified
147 * if and only if filter.choice is zero.
148 */
149 struct list_objects_filter_options filter;
150
151 /* excluding from --branches, --refs, etc. expansion */
152 struct ref_exclusions ref_excludes;
153
154 /* Basic information */
155 const char *prefix;
156 const char *def;
157 char *ps_matched; /* optionally record matches of prune_data */
158 struct pathspec prune_data;
159
160 /*
161 * Whether the arguments parsed by setup_revisions() included any
162 * "input" revisions that might still have yielded an empty pending
163 * list (e.g., patterns like "--all" or "--glob").
164 */
165 int rev_input_given;
166
167 /*
168 * Whether we read from stdin due to the --stdin option.
169 */
170 int read_from_stdin;
171
172 /* topo-sort */
173 enum rev_sort_order sort_order;
174
175 unsigned int ignore_missing:1,
176 ignore_missing_links:1;
177
178 /* Traversal flags */
179 unsigned int dense:1,
180 prune:1,
181 no_walk:1,
182 unsorted_input:1,
183 remove_empty_trees:1,
184 simplify_history:1,
185 show_pulls:1,
186 topo_order:1,
187 simplify_merges:1,
188 simplify_by_decoration:1,
189 single_worktree:1,
190 tag_objects:1,
191 tree_objects:1,
192 blob_objects:1,
193 verify_objects:1,
194 edge_hint:1,
195 edge_hint_aggressive:1,
196 limited:1,
197 unpacked:1,
198 no_kept_objects:1,
199 boundary:2,
200 count:1,
201 left_right:1,
202 left_only:1,
203 right_only:1,
204 maximal_only:1,
205 rewrite_parents:1,
206 print_parents:1,
207 show_decorations:1,
208 reverse:1,
209 reverse_output_stage:1,
210 cherry_pick:1,
211 cherry_mark:1,
212 bisect:1,
213 ancestry_path:1,
214
215 /* True if --ancestry-path was specified without an
216 * argument. The bottom revisions are implicitly
217 * the arguments in this case.
218 */
219 ancestry_path_implicit_bottoms:1,
220
221 first_parent_only:1,
222 exclude_first_parent_only:1,
223 line_level_traverse:1,
224 tree_blobs_in_commit_order:1,
225
226 /*
227 * Blobs are shown without regard for their existence.
228 * But not so for trees/commits: unless exclude_promisor_objects
229 * is set and the tree in question is a promisor object;
230 * OR ignore_missing_links is set, the revision walker
231 * dies with a "bad <type> object HASH" message when
232 * encountering a missing object. For callers that can
233 * handle missing trees/commits and want them to be filterable
234 * and showable, set this to true. The revision walker
235 * will filter and show such a missing object as usual,
236 * but will not attempt to recurse into this tree/commit
237 * object. The revision walker will also set the MISSING
238 * flag for such objects.
239 */
240 do_not_die_on_missing_objects:1,
241
242 /* for internal use only */
243 exclude_promisor_objects:1;
244
245 /* Diff flags */
246 unsigned int diff:1,
247 full_diff:1,
248 show_root_diff:1,
249 match_missing:1,
250 no_commit_id:1,
251 verbose_header:1,
252 always_show_header:1,
253 /* Diff-merge flags */
254 explicit_diff_merges: 1,
255 merges_need_diff: 1,
256 merges_imply_patch:1,
257 separate_merges: 1,
258 combine_merges:1,
259 combined_all_paths:1,
260 dense_combined_merges:1,
261 first_parent_merges:1,
262 remerge_diff:1;
263
264 /* Format info */
265 int show_notes;
266 unsigned int shown_one:1,
267 shown_dashes:1,
268 show_merge:1,
269 show_notes_given:1,
270 show_notes_by_default:1,
271 show_signature:1,
272 pretty_given:1,
273 abbrev_commit:1,
274 abbrev_commit_given:1,
275 zero_commit:1,
276 use_terminator:1,
277 missing_newline:1,
278 date_mode_explicit:1,
279 preserve_subject:1,
280 force_in_body_from:1,
281 encode_email_headers:1,
282 include_header:1;
283 unsigned int disable_stdin:1;
284 /* --show-linear-break */
285 unsigned int track_linear:1,
286 track_first_time:1,
287 linear:1;
288
289 struct date_mode date_mode;
290 int expand_tabs_in_log; /* unset if negative */
291 int expand_tabs_in_log_default;
292
293 unsigned int abbrev;
294 enum cmit_fmt commit_format;
295 struct log_info *loginfo;
296 int nr, total;
297 const char *mime_boundary;
298 const char *patch_suffix;
299 int numbered_files;
300 const char *reroll_count;
301 char *message_id;
302 struct ident_split from_ident;
303 struct string_list *ref_message_ids;
304 int add_signoff;
305 const char *extra_headers;
306 const char *subject_prefix;
307 int patch_name_max;
308 int no_inline;
309 int show_log_size;
310 struct string_list *mailmap;
311
312 /* Filter by commit log message */
313 struct grep_opt grep_filter;
314
315 /* Display history graph */
316 struct git_graph *graph;
317 int graph_max_lanes;
318 unsigned int no_graph_indent:1;
319 unsigned int graph_indent_set:1;
320
321 /* special limits */
322 int skip_count;
323 int max_count;
324 unsigned int max_count_type:1;
325 unsigned int max_count_stage:1;
326 timestamp_t max_age;
327 timestamp_t max_age_as_filter;
328 timestamp_t min_age;
329 int min_parents;
330 int max_parents;
331 int (*include_check)(struct commit *, void *);
332 int (*include_check_obj)(struct object *obj, void *);
333 void *include_check_data;
334
335 /* diff info for patches and for paths limiting */
336 struct diff_options diffopt;
337 struct diff_options pruning;
338
339 struct reflog_walk_info *reflog_info;
340 struct decoration children;
341 struct decoration merge_simplification;
342 struct decoration treesame;
343
344 /* notes-specific options: which refs to show */
345 struct display_notes_opt notes_opt;
346
347 /* interdiff */
348 const struct object_id *idiff_oid1;
349 const struct object_id *idiff_oid2;
350 const char *idiff_title;
351
352 /* range-diff */
353 const char *rdiff1;
354 const char *rdiff2;
355 struct strvec rdiff_log_arg;
356 int creation_factor;
357 const char *rdiff_title;
358
359 /* commit counts */
360 int count_left;
361 int count_right;
362 int count_same;
363
364 /* line level range that we are chasing */
365 struct decoration line_log_data;
366
367 /* copies of the parent lists, for --full-diff display */
368 struct saved_parents *saved_parents_slab;
369
370 /* per-commit pathspec for --follow across merges */
371 struct follow_pathspec_slab *follow_pathspec_slab;
372
373 struct commit_list *previous_parents;
374 struct commit_list *ancestry_path_bottoms;
375 const char *break_bar;
376
377 struct revision_sources *sources;
378
379 struct topo_walk_info *topo_walk_info;
380
381 /* Commit graph bloom filter fields */
382 /* The bloom filter key(s) for the pathspec */
383 struct bloom_keyvec **bloom_keyvecs;
384 int bloom_keyvecs_nr;
385
386 /*
387 * The bloom filter settings used to generate the key.
388 * This is loaded from the commit-graph being used.
389 */
390 struct bloom_filter_settings *bloom_filter_settings;
391
392 /* misc. flags related to '--no-kept-objects' */
393 unsigned keep_pack_cache_flags;
394
395 /* Location where temporary objects for remerge-diff are written. */
396 struct tmp_objdir *remerge_objdir;
397
398 /* Missing commits to be tracked without failing traversal. */
399 struct oidset missing_commits;
400 };
401
402 /**
403 * Initialize the "struct rev_info" structure with a macro.
404 *
405 * This will not fully initialize a "struct rev_info", the
406 * repo_init_revisions() function needs to be called before
407 * setup_revisions() and any revision walking takes place.
408 *
409 * Use REV_INFO_INIT to make the "struct rev_info" safe for passing to
410 * release_revisions() when it's inconvenient (e.g. due to a "goto
411 * cleanup" pattern) to arrange for repo_init_revisions() to be called
412 * before release_revisions() is called.
413 *
414 * Initializing with this REV_INFO_INIT is redundant to invoking
415 * repo_init_revisions(). If repo_init_revisions() is guaranteed to be
416 * called before release_revisions() the "struct rev_info" can be left
417 * uninitialized.
418 */
419 #define REV_INFO_INIT { \
420 .commit_queue = { .compare = compare_commits_by_commit_date }, \
421 .abbrev = DEFAULT_ABBREV, \
422 .simplify_history = 1, \
423 .pruning.flags.recursive = 1, \
424 .pruning.flags.quick = 1, \
425 .sort_order = REV_SORT_IN_GRAPH_ORDER, \
426 .dense = 1, \
427 .max_age = -1, \
428 .max_age_as_filter = -1, \
429 .min_age = -1, \
430 .skip_count = -1, \
431 .max_count = -1, \
432 .max_parents = -1, \
433 .expand_tabs_in_log = -1, \
434 .commit_format = CMIT_FMT_DEFAULT, \
435 .expand_tabs_in_log_default = 8, \
436 .rdiff_log_arg = STRVEC_INIT, \
437 }
438
439 /**
440 * Initialize a rev_info structure with default values. The third parameter may
441 * be NULL or can be prefix path, and then the `.prefix` variable will be set
442 * to it. This is typically the first function you want to call when you want
443 * to deal with a revision list. After calling this function, you are free to
444 * customize options, like set `.ignore_merges` to 0 if you don't want to
445 * ignore merges, and so on.
446 */
447 void repo_init_revisions(struct repository *r,
448 struct rev_info *revs,
449 const char *prefix);
450
451 /**
452 * Parse revision information, filling in the `rev_info` structure, and
453 * removing the used arguments from the argument list. Returns the number
454 * of arguments left that weren't recognized, which are also moved to the
455 * head of the argument list. The last parameter is used in case no
456 * parameter given by the first two arguments.
457 */
458 struct setup_revision_opt {
459 const char *def;
460 void (*tweak)(struct rev_info *);
461 unsigned int assume_dashdash:1,
462 allow_exclude_promisor_objects:1,
463 free_removed_argv_elements:1;
464 unsigned revarg_opt;
465 };
466 int setup_revisions(int argc, const char **argv, struct rev_info *revs,
467 struct setup_revision_opt *);
468 void setup_revisions_from_strvec(struct strvec *argv, struct rev_info *revs,
469 struct setup_revision_opt *);
470
471 /**
472 * Free data allocated in a "struct rev_info" after it's been
473 * initialized with repo_init_revisions() or REV_INFO_INIT.
474 */
475 void release_revisions(struct rev_info *revs);
476
477 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
478 const struct option *options,
479 const char * const usagestr[]);
480 #define REVARG_CANNOT_BE_FILENAME 01
481 #define REVARG_COMMITTISH 02
482 int handle_revision_arg(const char *arg, struct rev_info *revs,
483 int flags, unsigned revarg_opt);
484 void revision_opts_finish(struct rev_info *revs);
485
486 /**
487 * Reset the flags used by the revision walking api. You can use this to do
488 * multiple sequential revision walks.
489 */
490 void reset_revision_walk(void);
491
492 /**
493 * Prepares the rev_info structure for a walk. You should check if it returns
494 * any error (non-zero return code) and if it does not, you can start using
495 * get_revision() to do the iteration.
496 */
497 int prepare_revision_walk(struct rev_info *revs);
498
499 /**
500 * Take in a changed-path Bloom filter that belongs to a commit, and consult it
501 * to see if it might have modified any of the paths in the `revs`.
502 * The caller should look up `filter`, probably with get_bloom_filter().
503 * prepare_revision_walk() needs to be called in advance to ensure
504 * pathspec key vectors are set up.
505 *
506 * Returns -1 if no sensible answer could be given because of missing
507 * preconditions (no pathspec key vectors).
508 * Returns 0 if the commit definitely did not change any of the paths and 1 if
509 * the commit maybe has changed one of them, although that might be a
510 * false-positive.
511 */
512 int revs_maybe_changed_in_bloom(struct rev_info *revs,
513 struct bloom_filter *filter);
514
515 /* Drain the commits linked list into the priority queue. */
516 void rev_info_commit_list_to_queue(struct rev_info *revs);
517 /**
518 * Takes a pointer to a `rev_info` structure and iterates over it, returning a
519 * `struct commit *` each time you call it. The end of the revision list is
520 * indicated by returning a NULL pointer.
521 */
522 struct commit *get_revision(struct rev_info *revs);
523
524 const char *get_revision_mark(const struct rev_info *revs,
525 const struct commit *commit);
526 void put_revision_mark(const struct rev_info *revs,
527 const struct commit *commit);
528
529 void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit);
530 void mark_tree_uninteresting(struct repository *r, struct tree *tree);
531 void mark_trees_uninteresting_sparse(struct repository *r, struct oidset *trees);
532
533 /**
534 * Helpers to check if a reference should be excluded.
535 */
536
537 int ref_excluded(const struct ref_exclusions *exclusions, const char *path);
538 void init_ref_exclusions(struct ref_exclusions *);
539 void clear_ref_exclusions(struct ref_exclusions *);
540 void add_ref_exclusion(struct ref_exclusions *, const char *exclude);
541 void exclude_hidden_refs(struct ref_exclusions *, const char *section);
542
543 /**
544 * This function can be used if you want to add commit objects as revision
545 * information. You can use the `UNINTERESTING` object flag to indicate if
546 * you want to include or exclude the given commit (and commits reachable
547 * from the given commit) from the revision list.
548 *
549 * NOTE: If you have the commits as a string list then you probably want to
550 * use setup_revisions(), instead of parsing each string and using this
551 * function.
552 */
553 void add_pending_object(struct rev_info *revs,
554 struct object *obj, const char *name);
555
556 void add_pending_oid(struct rev_info *revs,
557 const char *name, const struct object_id *oid,
558 unsigned int flags);
559
560 void add_head_to_pending(struct rev_info *);
561 void add_reflogs_to_pending(struct rev_info *, unsigned int flags);
562 void add_index_objects_to_pending(struct rev_info *, unsigned int flags);
563
564 enum commit_action {
565 commit_ignore,
566 commit_show,
567 commit_error
568 };
569
570 enum commit_action get_commit_action(struct rev_info *revs,
571 struct commit *commit);
572 enum commit_action simplify_commit(struct rev_info *revs,
573 struct commit *commit);
574
575 enum rewrite_result {
576 rewrite_one_ok,
577 rewrite_one_noparents,
578 rewrite_one_error
579 };
580
581 typedef enum rewrite_result (*rewrite_parent_fn_t)(struct rev_info *revs, struct commit **pp);
582
583 int rewrite_parents(struct rev_info *revs,
584 struct commit *commit,
585 rewrite_parent_fn_t rewrite_parent);
586
587 /*
588 * The log machinery saves the original parent list so that
589 * get_saved_parents() can later tell what the real parents of the
590 * commits are, when commit->parents has been modified by history
591 * simplification.
592 *
593 * get_saved_parents() will transparently return commit->parents if
594 * history simplification is off.
595 */
596 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit);
597
598 #endif