graph: move doc to graph.h and graph.c
Move the documentation from Documentation/technical/api-history-graph.txt to graph.h and graph.c as it's easier for the developers to find the usage information beside the code instead of looking for it in another doc file. The graph library was already well documented, so few comments were added to both graph.h and graph.c Also documentation/technical/api-history-graph.txt is removed because the information it has is now redundant and it'll be hard to keep it up to date and synchronized with the documentation in the header file. Signed-off-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Heba Waly committed
Nov 17, 2019 at 21:04 UTC
3f1480b745b70e9f7c0629b58a98ddcb516490bb
3 files changed
+122
-173
Documentation/technical/api-history-graph.txt
deleted
-173
@@ -1,173 +0,0 @@
1
-history graph API
2
-=================
3
-
4
-The graph API is used to draw a text-based representation of the commit
5
-history. The API generates the graph in a line-by-line fashion.
6
-
7
-Functions
8
----------
9
-
10
-Core functions:
11
-
12
-* `graph_init()` creates a new `struct git_graph`
13
-
14
-* `graph_update()` moves the graph to a new commit.
15
-
16
-* `graph_next_line()` outputs the next line of the graph into a strbuf. It
17
- does not add a terminating newline.
18
-
19
-* `graph_padding_line()` outputs a line of vertical padding in the graph. It
20
- is similar to `graph_next_line()`, but is guaranteed to never print the line
21
- containing the current commit. Where `graph_next_line()` would print the
22
- commit line next, `graph_padding_line()` prints a line that simply extends
23
- all branch lines downwards one row, leaving their positions unchanged.
24
-
25
-* `graph_is_commit_finished()` determines if the graph has output all lines
26
- necessary for the current commit. If `graph_update()` is called before all
27
- lines for the current commit have been printed, the next call to
28
- `graph_next_line()` will output an ellipsis, to indicate that a portion of
29
- the graph was omitted.
30
-
31
-The following utility functions are wrappers around `graph_next_line()` and
32
-`graph_is_commit_finished()`. They always print the output to stdout.
33
-They can all be called with a NULL graph argument, in which case no graph
34
-output will be printed.
35
-
36
-* `graph_show_commit()` calls `graph_next_line()` and
37
- `graph_is_commit_finished()` until one of them return non-zero. This prints
38
- all graph lines up to, and including, the line containing this commit.
39
- Output is printed to stdout. The last line printed does not contain a
40
- terminating newline.
41
-
42
-* `graph_show_oneline()` calls `graph_next_line()` and prints the result to
43
- stdout. The line printed does not contain a terminating newline.
44
-
45
-* `graph_show_padding()` calls `graph_padding_line()` and prints the result to
46
- stdout. The line printed does not contain a terminating newline.
47
-
48
-* `graph_show_remainder()` calls `graph_next_line()` until
49
- `graph_is_commit_finished()` returns non-zero. Output is printed to stdout.
50
- The last line printed does not contain a terminating newline. Returns 1 if
51
- output was printed, and 0 if no output was necessary.
52
-
53
-* `graph_show_strbuf()` prints the specified strbuf to stdout, prefixing all
54
- lines but the first with a graph line. The caller is responsible for
55
- ensuring graph output for the first line has already been printed to stdout.
56
- (This can be done with `graph_show_commit()` or `graph_show_oneline()`.) If
57
- a NULL graph is supplied, the strbuf is printed as-is.
58
-
59
-* `graph_show_commit_msg()` is similar to `graph_show_strbuf()`, but it also
60
- prints the remainder of the graph, if more lines are needed after the strbuf
61
- ends. It is better than directly calling `graph_show_strbuf()` followed by
62
- `graph_show_remainder()` since it properly handles buffers that do not end in
63
- a terminating newline. The output printed by `graph_show_commit_msg()` will
64
- end in a newline if and only if the strbuf ends in a newline.
65
-
66
-Data structure
67
---------------
68
-`struct git_graph` is an opaque data type used to store the current graph
69
-state.
70
-
71
-Calling sequence
72
-----------------
73
-
74
-* Create a `struct git_graph` by calling `graph_init()`. When using the
75
- revision walking API, this is done automatically by `setup_revisions()` if
76
- the '--graph' option is supplied.
77
-
78
-* Use the revision walking API to walk through a group of contiguous commits.
79
- The `get_revision()` function automatically calls `graph_update()` each time
80
- it is invoked.
81
-
82
-* For each commit, call `graph_next_line()` repeatedly, until
83
- `graph_is_commit_finished()` returns non-zero. Each call to
84
- `graph_next_line()` will output a single line of the graph. The resulting
85
- lines will not contain any newlines. `graph_next_line()` returns 1 if the
86
- resulting line contains the current commit, or 0 if this is merely a line
87
- needed to adjust the graph before or after the current commit. This return
88
- value can be used to determine where to print the commit summary information
89
- alongside the graph output.
90
-
91
-Limitations
92
------------
93
-
94
-* `graph_update()` must be called with commits in topological order. It should
95
- not be called on a commit if it has already been invoked with an ancestor of
96
- that commit, or the graph output will be incorrect.
97
-
98
-* `graph_update()` must be called on a contiguous group of commits. If
99
- `graph_update()` is called on a particular commit, it should later be called
100
- on all parents of that commit. Parents must not be skipped, or the graph
101
- output will appear incorrect.
102
-+
103
-`graph_update()` may be used on a pruned set of commits only if the parent list
104
-has been rewritten so as to include only ancestors from the pruned set.
105
-
106
-* The graph API does not currently support reverse commit ordering. In
107
- order to implement reverse ordering, the graphing API needs an
108
- (efficient) mechanism to find the children of a commit.
109
-
110
-Sample usage
111
-------------
112
-
113
-------------
114
-struct commit *commit;
115
-struct git_graph *graph = graph_init(opts);
116
-
117
-while ((commit = get_revision(opts)) != NULL) {
118
- while (!graph_is_commit_finished(graph))
119
- {
120
- struct strbuf sb;
121
- int is_commit_line;
122
-
123
- strbuf_init(&sb, 0);
124
- is_commit_line = graph_next_line(graph, &sb);
125
- fputs(sb.buf, stdout);
126
-
127
- if (is_commit_line)
128
- log_tree_commit(opts, commit);
129
- else
130
- putchar(opts->diffopt.line_termination);
131
- }
132
-}
133
-------------
134
-
135
-Sample output
136
--------------
137
-
138
-The following is an example of the output from the graph API. This output does
139
-not include any commit summary information--callers are responsible for
140
-outputting that information, if desired.
141
-
142
-------------
143
-*
144
-*
145
-*
146
-|\
147
-* |
148
-| | *
149
-| \ \
150
-| \ \
151
-*-. \ \
152
-|\ \ \ \
153
-| | * | |
154
-| | | | | *
155
-| | | | | *
156
-| | | | | *
157
-| | | | | |\
158
-| | | | | | *
159
-| * | | | | |
160
-| | | | | * \
161
-| | | | | |\ |
162
-| | | | * | | |
163
-| | | | * | | |
164
-* | | | | | | |
165
-| |/ / / / / /
166
-|/| / / / / /
167
-* | | | | | |
168
-|/ / / / / /
169
-* | | | | |
170
-| | | | | *
171
-| | | | |/
172
-| | | | *
173
-------------
graph.c
+1
@@ -34,6 +34,7 @@ static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
34
* handle directly. It is assumed that this is the same file handle as the
35
* file specified by the graph diff options. This is necessary so that
36
* graph_show_strbuf can be called even with a NULL graph.
37
+ * If a NULL graph is supplied, the strbuf is printed as-is.
38
*/
39
static void graph_show_strbuf(struct git_graph *graph,
40
FILE *file,
graph.h
+121
@@ -2,6 +2,103 @@
2
#define GRAPH_H
3
#include "diff.h"
4
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
+
102
/* A graph is a pointer to this opaque structure */
103
struct git_graph;
104
@@ -50,6 +147,21 @@ struct git_graph *graph_init(struct rev_info *opt);
147
* If graph_update() is called before graph_is_commit_finished() returns 1,
148
* the next call to graph_next_line() will output an ellipsis ("...")
149
* 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.
165
*/
166
void graph_update(struct git_graph *graph, struct commit *commit);
167
@@ -62,6 +174,10 @@ void graph_update(struct git_graph *graph, struct commit *commit);
174
* for this commit. If 0 is returned, graph_next_line() may still be
175
* called without calling graph_update(), and it will merely output
176
* 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.
181
*/
182
int graph_is_commit_finished(struct git_graph const *graph);
183
@@ -112,6 +228,7 @@ void graph_show_padding(struct git_graph *graph);
228
/*
229
* If the graph is non-NULL, print the rest of the history graph for this
230
* 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.
232
*/
233
int graph_show_remainder(struct git_graph *graph);
234
@@ -121,6 +238,10 @@ int graph_show_remainder(struct git_graph *graph);
238
* This is similar to graph_show_strbuf(), but it always prints the
239
* remainder of the graph.
240
*
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
+ *
245
* If the strbuf ends with a newline, the output printed by
246
* graph_show_commit_msg() will end with a newline. If the strbuf is
247
* missing a terminating newline (including if it is empty), the output