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