22#include "commit.h"
33#include "color.h"
44#include "graph.h"
5- #include "diff.h"
65#include "revision.h"
76
87/* Internal API */
@@ -28,8 +27,15 @@ static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
2827 * responsible for printing this line's graph (perhaps via
2928 * graph_show_commit() or graph_show_oneline()) before calling
3029 * graph_show_strbuf().
30+ *
31+ * Note that unlike some other graph display functions, you must pass the file
32+ * handle directly. It is assumed that this is the same file handle as the
33+ * file specified by the graph diff options. This is necessary so that
34+ * graph_show_strbuf can be called even with a NULL graph.
3135 */
32- static void graph_show_strbuf (struct git_graph * graph , struct strbuf const * sb );
36+ static void graph_show_strbuf (struct git_graph * graph ,
37+ FILE * file ,
38+ struct strbuf const * sb );
3339
3440/*
3541 * TODO:
@@ -59,6 +65,17 @@ enum graph_state {
5965 GRAPH_COLLAPSING
6066};
6167
68+ static void graph_show_line_prefix (const struct diff_options * diffopt )
69+ {
70+ if (!diffopt || !diffopt -> line_prefix )
71+ return ;
72+
73+ fwrite (diffopt -> line_prefix ,
74+ sizeof (char ),
75+ diffopt -> line_prefix_length ,
76+ diffopt -> file );
77+ }
78+
6279static const char * * column_colors ;
6380static unsigned short column_colors_max ;
6481
@@ -195,13 +212,28 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void
195212 static struct strbuf msgbuf = STRBUF_INIT ;
196213
197214 assert (opt );
198- assert (graph );
199215
200216 strbuf_reset (& msgbuf );
201- graph_padding_line (graph , & msgbuf );
217+ if (opt -> line_prefix )
218+ strbuf_add (& msgbuf , opt -> line_prefix ,
219+ opt -> line_prefix_length );
220+ if (graph )
221+ graph_padding_line (graph , & msgbuf );
202222 return & msgbuf ;
203223}
204224
225+ static const struct diff_options * default_diffopt ;
226+
227+ void graph_setup_line_prefix (struct diff_options * diffopt )
228+ {
229+ default_diffopt = diffopt ;
230+
231+ /* setup an output prefix callback if necessary */
232+ if (diffopt && !diffopt -> output_prefix )
233+ diffopt -> output_prefix = diff_output_prefix_callback ;
234+ }
235+
236+
205237struct git_graph * graph_init (struct rev_info * opt )
206238{
207239 struct git_graph * graph = xmalloc (sizeof (struct git_graph ));
@@ -1183,6 +1215,8 @@ void graph_show_commit(struct git_graph *graph)
11831215 struct strbuf msgbuf = STRBUF_INIT ;
11841216 int shown_commit_line = 0 ;
11851217
1218+ graph_show_line_prefix (default_diffopt );
1219+
11861220 if (!graph )
11871221 return ;
11881222
@@ -1200,8 +1234,10 @@ void graph_show_commit(struct git_graph *graph)
12001234 shown_commit_line = graph_next_line (graph , & msgbuf );
12011235 fwrite (msgbuf .buf , sizeof (char ), msgbuf .len ,
12021236 graph -> revs -> diffopt .file );
1203- if (!shown_commit_line )
1237+ if (!shown_commit_line ) {
12041238 putc ('\n' , graph -> revs -> diffopt .file );
1239+ graph_show_line_prefix (& graph -> revs -> diffopt );
1240+ }
12051241 strbuf_setlen (& msgbuf , 0 );
12061242 }
12071243
@@ -1212,6 +1248,8 @@ void graph_show_oneline(struct git_graph *graph)
12121248{
12131249 struct strbuf msgbuf = STRBUF_INIT ;
12141250
1251+ graph_show_line_prefix (default_diffopt );
1252+
12151253 if (!graph )
12161254 return ;
12171255
@@ -1224,6 +1262,8 @@ void graph_show_padding(struct git_graph *graph)
12241262{
12251263 struct strbuf msgbuf = STRBUF_INIT ;
12261264
1265+ graph_show_line_prefix (default_diffopt );
1266+
12271267 if (!graph )
12281268 return ;
12291269
@@ -1237,6 +1277,8 @@ int graph_show_remainder(struct git_graph *graph)
12371277 struct strbuf msgbuf = STRBUF_INIT ;
12381278 int shown = 0 ;
12391279
1280+ graph_show_line_prefix (default_diffopt );
1281+
12401282 if (!graph )
12411283 return 0 ;
12421284
@@ -1250,27 +1292,24 @@ int graph_show_remainder(struct git_graph *graph)
12501292 strbuf_setlen (& msgbuf , 0 );
12511293 shown = 1 ;
12521294
1253- if (!graph_is_commit_finished (graph ))
1295+ if (!graph_is_commit_finished (graph )) {
12541296 putc ('\n' , graph -> revs -> diffopt .file );
1255- else
1297+ graph_show_line_prefix (& graph -> revs -> diffopt );
1298+ } else {
12561299 break ;
1300+ }
12571301 }
12581302 strbuf_release (& msgbuf );
12591303
12601304 return shown ;
12611305}
12621306
1263-
1264- static void graph_show_strbuf (struct git_graph * graph , struct strbuf const * sb )
1307+ static void graph_show_strbuf (struct git_graph * graph ,
1308+ FILE * file ,
1309+ struct strbuf const * sb )
12651310{
12661311 char * p ;
12671312
1268- if (!graph ) {
1269- fwrite (sb -> buf , sizeof (char ), sb -> len ,
1270- graph -> revs -> diffopt .file );
1271- return ;
1272- }
1273-
12741313 /*
12751314 * Print the strbuf line by line,
12761315 * and display the graph info before each line but the first.
@@ -1285,37 +1324,28 @@ static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
12851324 } else {
12861325 len = (sb -> buf + sb -> len ) - p ;
12871326 }
1288- fwrite (p , sizeof (char ), len , graph -> revs -> diffopt . file );
1327+ fwrite (p , sizeof (char ), len , file );
12891328 if (next_p && * next_p != '\0' )
12901329 graph_show_oneline (graph );
12911330 p = next_p ;
12921331 }
12931332}
12941333
12951334void graph_show_commit_msg (struct git_graph * graph ,
1335+ FILE * file ,
12961336 struct strbuf const * sb )
12971337{
12981338 int newline_terminated ;
12991339
1300- if (!graph ) {
1301- /*
1302- * If there's no graph, just print the message buffer.
1303- *
1304- * The message buffer for CMIT_FMT_ONELINE and
1305- * CMIT_FMT_USERFORMAT are already missing a terminating
1306- * newline. All of the other formats should have it.
1307- */
1308- fwrite (sb -> buf , sizeof (char ), sb -> len ,
1309- graph -> revs -> diffopt .file );
1310- return ;
1311- }
1312-
1313- newline_terminated = (sb -> len && sb -> buf [sb -> len - 1 ] == '\n' );
1314-
13151340 /*
13161341 * Show the commit message
13171342 */
1318- graph_show_strbuf (graph , sb );
1343+ graph_show_strbuf (graph , file , sb );
1344+
1345+ if (!graph )
1346+ return ;
1347+
1348+ newline_terminated = (sb -> len && sb -> buf [sb -> len - 1 ] == '\n' );
13191349
13201350 /*
13211351 * If there is more output needed for this commit, show it now
@@ -1327,14 +1357,14 @@ void graph_show_commit_msg(struct git_graph *graph,
13271357 * new line.
13281358 */
13291359 if (!newline_terminated )
1330- putc ('\n' , graph -> revs -> diffopt . file );
1360+ putc ('\n' , file );
13311361
13321362 graph_show_remainder (graph );
13331363
13341364 /*
13351365 * If sb ends with a newline, our output should too.
13361366 */
13371367 if (newline_terminated )
1338- putc ('\n' , graph -> revs -> diffopt . file );
1368+ putc ('\n' , file );
13391369 }
13401370}
0 commit comments