maintenance: add commit-graph task
[git] / revision.h
1 #ifndef REVISION_H
2 #define REVISION_H
3
4 #include "commit.h"
5 #include "parse-options.h"
6 #include "grep.h"
7 #include "notes.h"
8 #include "pretty.h"
9 #include "diff.h"
10 #include "commit-slab-decl.h"
11
12 /**
13  * The revision walking API offers functions to build a list of revisions
14  * and then iterate over that list.
15  *
16  * Calling sequence
17  * ----------------
18  *
19  * The walking API has a given calling sequence: first you need to initialize
20  * a rev_info structure, then add revisions to control what kind of revision
21  * list do you want to get, finally you can iterate over the revision list.
22  *
23  */
24
25 /* Remember to update object flag allocation in object.h */
26 #define SEEN            (1u<<0)
27 #define UNINTERESTING   (1u<<1)
28 #define TREESAME        (1u<<2)
29 #define SHOWN           (1u<<3)
30 #define TMP_MARK        (1u<<4) /* for isolated cases; clean after use */
31 #define BOUNDARY        (1u<<5)
32 #define CHILD_SHOWN     (1u<<6)
33 #define ADDED           (1u<<7) /* Parents already parsed and added? */
34 #define SYMMETRIC_LEFT  (1u<<8)
35 #define PATCHSAME       (1u<<9)
36 #define BOTTOM          (1u<<10)
37
38 /* WARNING: This is also used as REACHABLE in commit-graph.c. */
39 #define PULL_MERGE      (1u<<15)
40
41 #define TOPO_WALK_EXPLORED      (1u<<23)
42 #define TOPO_WALK_INDEGREE      (1u<<24)
43
44 /*
45  * Indicates object was reached by traversal. i.e. not given by user on
46  * command-line or stdin.
47  * NEEDSWORK: NOT_USER_GIVEN doesn't apply to commits because we only support
48  * filtering trees and blobs, but it may be useful to support filtering commits
49  * in the future.
50  */
51 #define NOT_USER_GIVEN  (1u<<25)
52 #define TRACK_LINEAR    (1u<<26)
53 #define ALL_REV_FLAGS   (((1u<<11)-1) | NOT_USER_GIVEN | TRACK_LINEAR | PULL_MERGE)
54
55 #define DECORATE_SHORT_REFS     1
56 #define DECORATE_FULL_REFS      2
57
58 struct log_info;
59 struct repository;
60 struct rev_info;
61 struct string_list;
62 struct saved_parents;
63 struct bloom_key;
64 struct bloom_filter_settings;
65 define_shared_commit_slab(revision_sources, char *);
66
67 struct rev_cmdline_info {
68         unsigned int nr;
69         unsigned int alloc;
70         struct rev_cmdline_entry {
71                 struct object *item;
72                 const char *name;
73                 enum {
74                         REV_CMD_REF,
75                         REV_CMD_PARENTS_ONLY,
76                         REV_CMD_LEFT,
77                         REV_CMD_RIGHT,
78                         REV_CMD_MERGE_BASE,
79                         REV_CMD_REV
80                 } whence;
81                 unsigned flags;
82         } *rev;
83 };
84
85 #define REVISION_WALK_WALK 0
86 #define REVISION_WALK_NO_WALK_SORTED 1
87 #define REVISION_WALK_NO_WALK_UNSORTED 2
88
89 struct oidset;
90 struct topo_walk_info;
91
92 struct rev_info {
93         /* Starting list */
94         struct commit_list *commits;
95         struct object_array pending;
96         struct repository *repo;
97
98         /* Parents of shown commits */
99         struct object_array boundary_commits;
100
101         /* The end-points specified by the end user */
102         struct rev_cmdline_info cmdline;
103
104         /* excluding from --branches, --refs, etc. expansion */
105         struct string_list *ref_excludes;
106
107         /* Basic information */
108         const char *prefix;
109         const char *def;
110         struct pathspec prune_data;
111
112         /*
113          * Whether the arguments parsed by setup_revisions() included any
114          * "input" revisions that might still have yielded an empty pending
115          * list (e.g., patterns like "--all" or "--glob").
116          */
117         int rev_input_given;
118
119         /*
120          * Whether we read from stdin due to the --stdin option.
121          */
122         int read_from_stdin;
123
124         /* topo-sort */
125         enum rev_sort_order sort_order;
126
127         unsigned int early_output;
128
129         unsigned int    ignore_missing:1,
130                         ignore_missing_links:1;
131
132         /* Traversal flags */
133         unsigned int    dense:1,
134                         prune:1,
135                         no_walk:2,
136                         remove_empty_trees:1,
137                         simplify_history:1,
138                         show_pulls:1,
139                         topo_order:1,
140                         simplify_merges:1,
141                         simplify_by_decoration:1,
142                         single_worktree:1,
143                         tag_objects:1,
144                         tree_objects:1,
145                         blob_objects:1,
146                         verify_objects:1,
147                         edge_hint:1,
148                         edge_hint_aggressive:1,
149                         limited:1,
150                         unpacked:1,
151                         boundary:2,
152                         count:1,
153                         left_right:1,
154                         left_only:1,
155                         right_only:1,
156                         rewrite_parents:1,
157                         print_parents:1,
158                         show_decorations:1,
159                         reverse:1,
160                         reverse_output_stage:1,
161                         cherry_pick:1,
162                         cherry_mark:1,
163                         bisect:1,
164                         ancestry_path:1,
165                         first_parent_only:1,
166                         line_level_traverse:1,
167                         tree_blobs_in_commit_order:1,
168
169                         /*
170                          * Blobs are shown without regard for their existence.
171                          * But not so for trees: unless exclude_promisor_objects
172                          * is set and the tree in question is a promisor object;
173                          * OR ignore_missing_links is set, the revision walker
174                          * dies with a "bad tree object HASH" message when
175                          * encountering a missing tree. For callers that can
176                          * handle missing trees and want them to be filterable
177                          * and showable, set this to true. The revision walker
178                          * will filter and show such a missing tree as usual,
179                          * but will not attempt to recurse into this tree
180                          * object.
181                          */
182                         do_not_die_on_missing_tree:1,
183
184                         /* for internal use only */
185                         exclude_promisor_objects:1;
186
187         /* Diff flags */
188         unsigned int    diff:1,
189                         full_diff:1,
190                         show_root_diff:1,
191                         no_commit_id:1,
192                         verbose_header:1,
193                         combine_merges:1,
194                         combined_all_paths:1,
195                         dense_combined_merges:1,
196                         always_show_header:1;
197         int             ignore_merges:2;
198
199         /* Format info */
200         int             show_notes;
201         unsigned int    shown_one:1,
202                         shown_dashes:1,
203                         show_merge:1,
204                         show_notes_given:1,
205                         show_signature:1,
206                         pretty_given:1,
207                         abbrev_commit:1,
208                         abbrev_commit_given:1,
209                         zero_commit:1,
210                         use_terminator:1,
211                         missing_newline:1,
212                         date_mode_explicit:1,
213                         preserve_subject:1,
214                         encode_email_headers:1;
215         unsigned int    disable_stdin:1;
216         /* --show-linear-break */
217         unsigned int    track_linear:1,
218                         track_first_time:1,
219                         linear:1;
220
221         struct date_mode date_mode;
222         int             expand_tabs_in_log; /* unset if negative */
223         int             expand_tabs_in_log_default;
224
225         unsigned int    abbrev;
226         enum cmit_fmt   commit_format;
227         struct log_info *loginfo;
228         int             nr, total;
229         const char      *mime_boundary;
230         const char      *patch_suffix;
231         int             numbered_files;
232         int             reroll_count;
233         char            *message_id;
234         struct ident_split from_ident;
235         struct string_list *ref_message_ids;
236         int             add_signoff;
237         const char      *extra_headers;
238         const char      *log_reencode;
239         const char      *subject_prefix;
240         int             no_inline;
241         int             show_log_size;
242         struct string_list *mailmap;
243
244         /* Filter by commit log message */
245         struct grep_opt grep_filter;
246         /* Negate the match of grep_filter */
247         int invert_grep;
248
249         /* Display history graph */
250         struct git_graph *graph;
251
252         /* special limits */
253         int skip_count;
254         int max_count;
255         timestamp_t max_age;
256         timestamp_t min_age;
257         int min_parents;
258         int max_parents;
259         int (*include_check)(struct commit *, void *);
260         void *include_check_data;
261
262         /* diff info for patches and for paths limiting */
263         struct diff_options diffopt;
264         struct diff_options pruning;
265
266         struct reflog_walk_info *reflog_info;
267         struct decoration children;
268         struct decoration merge_simplification;
269         struct decoration treesame;
270
271         /* notes-specific options: which refs to show */
272         struct display_notes_opt notes_opt;
273
274         /* interdiff */
275         const struct object_id *idiff_oid1;
276         const struct object_id *idiff_oid2;
277         const char *idiff_title;
278
279         /* range-diff */
280         const char *rdiff1;
281         const char *rdiff2;
282         int creation_factor;
283         const char *rdiff_title;
284
285         /* commit counts */
286         int count_left;
287         int count_right;
288         int count_same;
289
290         /* line level range that we are chasing */
291         struct decoration line_log_data;
292
293         /* copies of the parent lists, for --full-diff display */
294         struct saved_parents *saved_parents_slab;
295
296         struct commit_list *previous_parents;
297         const char *break_bar;
298
299         struct revision_sources *sources;
300
301         struct topo_walk_info *topo_walk_info;
302
303         /* Commit graph bloom filter fields */
304         /* The bloom filter key(s) for the pathspec */
305         struct bloom_key *bloom_keys;
306         int bloom_keys_nr;
307
308         /*
309          * The bloom filter settings used to generate the key.
310          * This is loaded from the commit-graph being used.
311          */
312         struct bloom_filter_settings *bloom_filter_settings;
313 };
314
315 int ref_excluded(struct string_list *, const char *path);
316 void clear_ref_exclusion(struct string_list **);
317 void add_ref_exclusion(struct string_list **, const char *exclude);
318
319
320 #define REV_TREE_SAME           0
321 #define REV_TREE_NEW            1       /* Only new files */
322 #define REV_TREE_OLD            2       /* Only files removed */
323 #define REV_TREE_DIFFERENT      3       /* Mixed changes */
324
325 /* revision.c */
326 typedef void (*show_early_output_fn_t)(struct rev_info *, struct commit_list *);
327 extern volatile show_early_output_fn_t show_early_output;
328
329 struct setup_revision_opt {
330         const char *def;
331         void (*tweak)(struct rev_info *, struct setup_revision_opt *);
332         const char *submodule;  /* TODO: drop this and use rev_info->repo */
333         unsigned int    assume_dashdash:1,
334                         allow_exclude_promisor_objects:1;
335         unsigned revarg_opt;
336 };
337
338 #ifndef NO_THE_REPOSITORY_COMPATIBILITY_MACROS
339 #define init_revisions(revs, prefix) repo_init_revisions(the_repository, revs, prefix)
340 #endif
341
342 /**
343  * Initialize a rev_info structure with default values. The third parameter may
344  * be NULL or can be prefix path, and then the `.prefix` variable will be set
345  * to it. This is typically the first function you want to call when you want
346  * to deal with a revision list. After calling this function, you are free to
347  * customize options, like set `.ignore_merges` to 0 if you don't want to
348  * ignore merges, and so on.
349  */
350 void repo_init_revisions(struct repository *r,
351                          struct rev_info *revs,
352                          const char *prefix);
353
354 /**
355  * Parse revision information, filling in the `rev_info` structure, and
356  * removing the used arguments from the argument list. Returns the number
357  * of arguments left that weren't recognized, which are also moved to the
358  * head of the argument list. The last parameter is used in case no
359  * parameter given by the first two arguments.
360  */
361 int setup_revisions(int argc, const char **argv, struct rev_info *revs,
362                     struct setup_revision_opt *);
363
364 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
365                         const struct option *options,
366                         const char * const usagestr[]);
367 #define REVARG_CANNOT_BE_FILENAME 01
368 #define REVARG_COMMITTISH 02
369 int handle_revision_arg(const char *arg, struct rev_info *revs,
370                         int flags, unsigned revarg_opt);
371
372 /**
373  * Reset the flags used by the revision walking api. You can use this to do
374  * multiple sequential revision walks.
375  */
376 void reset_revision_walk(void);
377
378 /**
379  * Prepares the rev_info structure for a walk. You should check if it returns
380  * any error (non-zero return code) and if it does not, you can start using
381  * get_revision() to do the iteration.
382  */
383 int prepare_revision_walk(struct rev_info *revs);
384
385 /**
386  * Takes a pointer to a `rev_info` structure and iterates over it, returning a
387  * `struct commit *` each time you call it. The end of the revision list is
388  * indicated by returning a NULL pointer.
389  */
390 struct commit *get_revision(struct rev_info *revs);
391
392 const char *get_revision_mark(const struct rev_info *revs,
393                               const struct commit *commit);
394 void put_revision_mark(const struct rev_info *revs,
395                        const struct commit *commit);
396
397 void mark_parents_uninteresting(struct commit *commit);
398 void mark_tree_uninteresting(struct repository *r, struct tree *tree);
399 void mark_trees_uninteresting_sparse(struct repository *r, struct oidset *trees);
400
401 void show_object_with_name(FILE *, struct object *, const char *);
402
403 /**
404  * This function can be used if you want to add commit objects as revision
405  * information. You can use the `UNINTERESTING` object flag to indicate if
406  * you want to include or exclude the given commit (and commits reachable
407  * from the given commit) from the revision list.
408  *
409  * NOTE: If you have the commits as a string list then you probably want to
410  * use setup_revisions(), instead of parsing each string and using this
411  * function.
412  */
413 void add_pending_object(struct rev_info *revs,
414                         struct object *obj, const char *name);
415
416 void add_pending_oid(struct rev_info *revs,
417                      const char *name, const struct object_id *oid,
418                      unsigned int flags);
419
420 void add_head_to_pending(struct rev_info *);
421 void add_reflogs_to_pending(struct rev_info *, unsigned int flags);
422 void add_index_objects_to_pending(struct rev_info *, unsigned int flags);
423
424 enum commit_action {
425         commit_ignore,
426         commit_show,
427         commit_error
428 };
429
430 enum commit_action get_commit_action(struct rev_info *revs,
431                                      struct commit *commit);
432 enum commit_action simplify_commit(struct rev_info *revs,
433                                    struct commit *commit);
434
435 enum rewrite_result {
436         rewrite_one_ok,
437         rewrite_one_noparents,
438         rewrite_one_error
439 };
440
441 typedef enum rewrite_result (*rewrite_parent_fn_t)(struct rev_info *revs, struct commit **pp);
442
443 int rewrite_parents(struct rev_info *revs,
444                     struct commit *commit,
445                     rewrite_parent_fn_t rewrite_parent);
446
447 /*
448  * The log machinery saves the original parent list so that
449  * get_saved_parents() can later tell what the real parents of the
450  * commits are, when commit->parents has been modified by history
451  * simpification.
452  *
453  * get_saved_parents() will transparently return commit->parents if
454  * history simplification is off.
455  */
456 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit);
457
458 #endif