]> git.ipfire.org Git - thirdparty/git.git/blob - Documentation/technical/api-history-graph.txt
log --graph --left-right: show left/right information in place of '*'
[thirdparty/git.git] / Documentation / technical / api-history-graph.txt
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_release()` destroys a `struct git_graph`, and frees the memory
15 associated with it.
16
17 * `graph_update()` moves the graph to a new commit.
18
19 * `graph_next_line()` outputs the next line of the graph into a strbuf. It
20 does not add a terminating newline.
21
22 * `graph_padding_line()` outputs a line of vertical padding in the graph. It
23 is similar to `graph_next_line()`, but is guaranteed to never print the line
24 containing the current commit. Where `graph_next_line()` would print the
25 commit line next, `graph_padding_line()` prints a line that simply extends
26 all branch lines downwards one row, leaving their positions unchanged.
27
28 * `graph_is_commit_finished()` determines if the graph has output all lines
29 necessary for the current commit. If `graph_update()` is called before all
30 lines for the current commit have been printed, the next call to
31 `graph_next_line()` will output an ellipsis, to indicate that a portion of
32 the graph was omitted.
33
34 The following utility functions are wrappers around `graph_next_line()` and
35 `graph_is_commit_finished()`. They always print the output to stdout.
36 They can all be called with a NULL graph argument, in which case no graph
37 output will be printed.
38
39 * `graph_show_commit()` calls `graph_next_line()` until it returns non-zero.
40 This prints all graph lines up to, and including, the line containing this
41 commit. Output is printed to stdout. The last line printed does not contain
42 a terminating newline. This should not be called if the commit line has
43 already been printed, or it will loop forever.
44
45 * `graph_show_oneline()` calls `graph_next_line()` and prints the result to
46 stdout. The line printed does not contain a terminating newline.
47
48 * `graph_show_padding()` calls `graph_padding_line()` and prints the result to
49 stdout. The line printed does not contain a terminating newline.
50
51 * `graph_show_remainder()` calls `graph_next_line()` until
52 `graph_is_commit_finished()` returns non-zero. Output is printed to stdout.
53 The last line printed does not contain a terminating newline. Returns 1 if
54 output was printed, and 0 if no output was necessary.
55
56 * `graph_show_strbuf()` prints the specified strbuf to stdout, prefixing all
57 lines but the first with a graph line. The caller is responsible for
58 ensuring graph output for the first line has already been printed to stdout.
59 (This can be done with `graph_show_commit()` or `graph_show_oneline()`.) If
60 a NULL graph is supplied, the strbuf is printed as-is.
61
62 * `graph_show_commit_msg()` is similar to `graph_show_strbuf()`, but it also
63 prints the remainder of the graph, if more lines are needed after the strbuf
64 ends. It is better than directly calling `graph_show_strbuf()` followed by
65 `graph_show_remainder()` since it properly handles buffers that do not end in
66 a terminating newline. The output printed by `graph_show_commit_msg()` will
67 end in a newline if and only if the strbuf ends in a newline.
68
69 Data structure
70 --------------
71 `struct git_graph` is an opaque data type used to store the current graph
72 state.
73
74 Calling sequence
75 ----------------
76
77 * Create a `struct git_graph` by calling `graph_init()`. When using the
78 revision walking API, this is done automatically by `setup_revisions()` if
79 the '--graph' option is supplied.
80
81 * Use the revision walking API to walk through a group of contiguous commits.
82 The `get_revision()` function automatically calls `graph_update()` each time
83 it is invoked.
84
85 * For each commit, call `graph_next_line()` repeatedly, until
86 `graph_is_commit_finished()` returns non-zero. Each call go
87 `graph_next_line()` will output a single line of the graph. The resulting
88 lines will not contain any newlines. `graph_next_line()` returns 1 if the
89 resulting line contains the current commit, or 0 if this is merely a line
90 needed to adjust the graph before or after the current commit. This return
91 value can be used to determine where to print the commit summary information
92 alongside the graph output.
93
94 Limitations
95 -----------
96
97 * `graph_update()` must be called with commits in topological order. It should
98 not be called on a commit if it has already been invoked with an ancestor of
99 that commit, or the graph output will be incorrect.
100
101 * `graph_update()` must be called on a contiguous group of commits. If
102 `graph_update()` is called on a particular commit, it should later be called
103 on all parents of that commit. Parents must not be skipped, or the graph
104 output will appear incorrect.
105 +
106 `graph_update()` may be used on a pruned set of commits only if the parent list
107 has been rewritten so as to include only ancestors from the pruned set.
108
109 * The graph API does not currently support reverse commit ordering. In
110 order to implement reverse ordering, the graphing API needs an
111 (efficient) mechanism to find the children of a commit.
112
113 Sample usage
114 ------------
115
116 ------------
117 struct commit *commit;
118 struct git_graph *graph = graph_init(opts);
119
120 while ((commit = get_revision(opts)) != NULL) {
121 graph_update(graph, commit);
122 while (!graph_is_commit_finished(graph))
123 {
124 struct strbuf sb;
125 int is_commit_line;
126
127 strbuf_init(&sb, 0);
128 is_commit_line = graph_next_line(graph, &sb);
129 fputs(sb.buf, stdout);
130
131 if (is_commit_line)
132 log_tree_commit(opts, commit);
133 else
134 putchar(opts->diffopt.line_termination);
135 }
136 }
137
138 graph_release(graph);
139 ------------
140
141 Sample output
142 -------------
143
144 The following is an example of the output from the graph API. This output does
145 not include any commit summary information--callers are responsible for
146 outputting that information, if desired.
147
148 ------------
149 *
150 *
151 M
152 |\
153 * |
154 | | *
155 | \ \
156 | \ \
157 M-. \ \
158 |\ \ \ \
159 | | * | |
160 | | | | | *
161 | | | | | *
162 | | | | | M
163 | | | | | |\
164 | | | | | | *
165 | * | | | | |
166 | | | | | M \
167 | | | | | |\ |
168 | | | | * | | |
169 | | | | * | | |
170 * | | | | | | |
171 | |/ / / / / /
172 |/| / / / / /
173 * | | | | | |
174 |/ / / / / /
175 * | | | | |
176 | | | | | *
177 | | | | |/
178 | | | | *
179 ------------