22#define GRAPH_H
33#include "diff.h"
44
5+ /**
6+ * The graph API is used to draw a text-based representation of the commit
7+ * history. The API generates the graph in a line-by-line fashion.
8+ *
9+ * Calling sequence
10+ * ----------------
11+ *
12+ * - Create a `struct git_graph` by calling `graph_init()`. When using the
13+ * revision walking API, this is done automatically by `setup_revisions()` if
14+ * the '--graph' option is supplied.
15+ *
16+ * - Use the revision walking API to walk through a group of contiguous commits.
17+ * The `get_revision()` function automatically calls `graph_update()` each time
18+ * it is invoked.
19+ *
20+ * - For each commit, call `graph_next_line()` repeatedly, until
21+ * `graph_is_commit_finished()` returns non-zero. Each call to
22+ * `graph_next_line()` will output a single line of the graph. The resulting
23+ * lines will not contain any newlines. `graph_next_line()` returns 1 if the
24+ * resulting line contains the current commit, or 0 if this is merely a line
25+ * needed to adjust the graph before or after the current commit. This return
26+ * value can be used to determine where to print the commit summary information
27+ * alongside the graph output.
28+ *
29+ * Limitations
30+ * -----------
31+ * - Check the graph_update() function for its limitations.
32+ *
33+ * - The graph API does not currently support reverse commit ordering. In
34+ * order to implement reverse ordering, the graphing API needs an
35+ * (efficient) mechanism to find the children of a commit.
36+ *
37+ * Sample usage
38+ * ------------
39+ *
40+ * ------------
41+ * struct commit *commit;
42+ * struct git_graph *graph = graph_init(opts);
43+ *
44+ * while ((commit = get_revision(opts)) != NULL) {
45+ * while (!graph_is_commit_finished(graph))
46+ * {
47+ * struct strbuf sb;
48+ * int is_commit_line;
49+ *
50+ * strbuf_init(&sb, 0);
51+ * is_commit_line = graph_next_line(graph, &sb);
52+ * fputs(sb.buf, stdout);
53+ *
54+ * if (is_commit_line)
55+ * log_tree_commit(opts, commit);
56+ * else
57+ * putchar(opts->diffopt.line_termination);
58+ * }
59+ * }
60+ * ------------
61+ * Sample output
62+ * -------------
63+ *
64+ * The following is an example of the output from the graph API. This output does
65+ * not include any commit summary information--callers are responsible for
66+ * outputting that information, if desired.
67+ * ------------
68+ * *
69+ * *
70+ * *
71+ * |\
72+ * * |
73+ * | | *
74+ * | \ \
75+ * | \ \
76+ * *-. \ \
77+ * |\ \ \ \
78+ * | | * | |
79+ * | | | | | *
80+ * | | | | | *
81+ * | | | | | *
82+ * | | | | | |\
83+ * | | | | | | *
84+ * | * | | | | |
85+ * | | | | | * \
86+ * | | | | | |\ |
87+ * | | | | * | | |
88+ * | | | | * | | |
89+ * * | | | | | | |
90+ * | |/ / / / / /
91+ * |/| / / / / /
92+ * * | | | | | |
93+ * |/ / / / / /
94+ * * | | | | |
95+ * | | | | | *
96+ * | | | | |/
97+ * | | | | *
98+ * ------------
99+ *
100+ */
101+
5102/* A graph is a pointer to this opaque structure */
6103struct git_graph ;
7104
@@ -50,6 +147,21 @@ struct git_graph *graph_init(struct rev_info *opt);
50147 * If graph_update() is called before graph_is_commit_finished() returns 1,
51148 * the next call to graph_next_line() will output an ellipsis ("...")
52149 * to indicate that a portion of the graph is missing.
150+ *
151+ * Limitations:
152+ * -----------
153+ *
154+ * - `graph_update()` must be called with commits in topological order. It should
155+ * not be called on a commit if it has already been invoked with an ancestor of
156+ * that commit, or the graph output will be incorrect.
157+ *
158+ * - `graph_update()` must be called on a contiguous group of commits. If
159+ * `graph_update()` is called on a particular commit, it should later be called
160+ * on all parents of that commit. Parents must not be skipped, or the graph
161+ * output will appear incorrect.
162+ *
163+ * - `graph_update()` may be used on a pruned set of commits only if the parent list
164+ * has been rewritten so as to include only ancestors from the pruned set.
53165 */
54166void graph_update (struct git_graph * graph , struct commit * commit );
55167
@@ -62,6 +174,10 @@ void graph_update(struct git_graph *graph, struct commit *commit);
62174 * for this commit. If 0 is returned, graph_next_line() may still be
63175 * called without calling graph_update(), and it will merely output
64176 * appropriate "vertical padding" in the graph.
177+ *
178+ * If `graph_update()` is called before all lines for the current commit have
179+ * been printed, the next call to `graph_next_line()` will output an ellipsis,
180+ * to indicate that a portion of the graph was omitted.
65181 */
66182int graph_is_commit_finished (struct git_graph const * graph );
67183
@@ -112,6 +228,7 @@ void graph_show_padding(struct git_graph *graph);
112228/*
113229 * If the graph is non-NULL, print the rest of the history graph for this
114230 * commit to stdout. Does not print a terminating newline on the last line.
231+ * Returns 1 if output was printed, and 0 if no output was necessary.
115232 */
116233int graph_show_remainder (struct git_graph * graph );
117234
@@ -121,6 +238,10 @@ int graph_show_remainder(struct git_graph *graph);
121238 * This is similar to graph_show_strbuf(), but it always prints the
122239 * remainder of the graph.
123240 *
241+ * It is better than directly calling `graph_show_strbuf()` followed by
242+ * `graph_show_remainder()` since it properly handles buffers that do not end in
243+ * a terminating newline.
244+ *
124245 * If the strbuf ends with a newline, the output printed by
125246 * graph_show_commit_msg() will end with a newline. If the strbuf is
126247 * missing a terminating newline (including if it is empty), the output
0 commit comments