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