graph.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. #include "cache.h"
  2. #include "config.h"
  3. #include "commit.h"
  4. #include "color.h"
  5. #include "graph.h"
  6. #include "revision.h"
  7. #include "strvec.h"
  8. /* Internal API */
  9. /*
  10. * Output a padding line in the graph.
  11. * This is similar to graph_next_line(). However, it is guaranteed to
  12. * never print the current commit line. Instead, if the commit line is
  13. * next, it will simply output a line of vertical padding, extending the
  14. * branch lines downwards, but leaving them otherwise unchanged.
  15. */
  16. static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
  17. /*
  18. * Print a strbuf. If the graph is non-NULL, all lines but the first will be
  19. * prefixed with the graph output.
  20. *
  21. * If the strbuf ends with a newline, the output will end after this
  22. * newline. A new graph line will not be printed after the final newline.
  23. * If the strbuf is empty, no output will be printed.
  24. *
  25. * Since the first line will not include the graph output, the caller is
  26. * responsible for printing this line's graph (perhaps via
  27. * graph_show_commit() or graph_show_oneline()) before calling
  28. * graph_show_strbuf().
  29. *
  30. * Note that unlike some other graph display functions, you must pass the file
  31. * handle directly. It is assumed that this is the same file handle as the
  32. * file specified by the graph diff options. This is necessary so that
  33. * graph_show_strbuf can be called even with a NULL graph.
  34. * If a NULL graph is supplied, the strbuf is printed as-is.
  35. */
  36. static void graph_show_strbuf(struct git_graph *graph,
  37. FILE *file,
  38. struct strbuf const *sb);
  39. /*
  40. * TODO:
  41. * - Limit the number of columns, similar to the way gitk does.
  42. * If we reach more than a specified number of columns, omit
  43. * sections of some columns.
  44. */
  45. struct column {
  46. /*
  47. * The parent commit of this column.
  48. */
  49. struct commit *commit;
  50. /*
  51. * The color to (optionally) print this column in. This is an
  52. * index into column_colors.
  53. */
  54. unsigned short color;
  55. };
  56. enum graph_state {
  57. GRAPH_PADDING,
  58. GRAPH_SKIP,
  59. GRAPH_PRE_COMMIT,
  60. GRAPH_COMMIT,
  61. GRAPH_POST_MERGE,
  62. GRAPH_COLLAPSING
  63. };
  64. static void graph_show_line_prefix(const struct diff_options *diffopt)
  65. {
  66. if (!diffopt || !diffopt->line_prefix)
  67. return;
  68. fwrite(diffopt->line_prefix,
  69. sizeof(char),
  70. diffopt->line_prefix_length,
  71. diffopt->file);
  72. }
  73. static const char **column_colors;
  74. static unsigned short column_colors_max;
  75. static void parse_graph_colors_config(struct strvec *colors, const char *string)
  76. {
  77. const char *end, *start;
  78. start = string;
  79. end = string + strlen(string);
  80. while (start < end) {
  81. const char *comma = strchrnul(start, ',');
  82. char color[COLOR_MAXLEN];
  83. if (!color_parse_mem(start, comma - start, color))
  84. strvec_push(colors, color);
  85. else
  86. warning(_("ignore invalid color '%.*s' in log.graphColors"),
  87. (int)(comma - start), start);
  88. start = comma + 1;
  89. }
  90. strvec_push(colors, GIT_COLOR_RESET);
  91. }
  92. void graph_set_column_colors(const char **colors, unsigned short colors_max)
  93. {
  94. column_colors = colors;
  95. column_colors_max = colors_max;
  96. }
  97. static const char *column_get_color_code(unsigned short color)
  98. {
  99. return column_colors[color];
  100. }
  101. struct graph_line {
  102. struct strbuf *buf;
  103. size_t width;
  104. };
  105. static inline void graph_line_addch(struct graph_line *line, int c)
  106. {
  107. strbuf_addch(line->buf, c);
  108. line->width++;
  109. }
  110. static inline void graph_line_addchars(struct graph_line *line, int c, size_t n)
  111. {
  112. strbuf_addchars(line->buf, c, n);
  113. line->width += n;
  114. }
  115. static inline void graph_line_addstr(struct graph_line *line, const char *s)
  116. {
  117. strbuf_addstr(line->buf, s);
  118. line->width += strlen(s);
  119. }
  120. static inline void graph_line_addcolor(struct graph_line *line, unsigned short color)
  121. {
  122. strbuf_addstr(line->buf, column_get_color_code(color));
  123. }
  124. static void graph_line_write_column(struct graph_line *line, const struct column *c,
  125. char col_char)
  126. {
  127. if (c->color < column_colors_max)
  128. graph_line_addcolor(line, c->color);
  129. graph_line_addch(line, col_char);
  130. if (c->color < column_colors_max)
  131. graph_line_addcolor(line, column_colors_max);
  132. }
  133. struct git_graph {
  134. /*
  135. * The commit currently being processed
  136. */
  137. struct commit *commit;
  138. /* The rev-info used for the current traversal */
  139. struct rev_info *revs;
  140. /*
  141. * The number of interesting parents that this commit has.
  142. *
  143. * Note that this is not the same as the actual number of parents.
  144. * This count excludes parents that won't be printed in the graph
  145. * output, as determined by graph_is_interesting().
  146. */
  147. int num_parents;
  148. /*
  149. * The width of the graph output for this commit.
  150. * All rows for this commit are padded to this width, so that
  151. * messages printed after the graph output are aligned.
  152. */
  153. int width;
  154. /*
  155. * The next expansion row to print
  156. * when state is GRAPH_PRE_COMMIT
  157. */
  158. int expansion_row;
  159. /*
  160. * The current output state.
  161. * This tells us what kind of line graph_next_line() should output.
  162. */
  163. enum graph_state state;
  164. /*
  165. * The output state for the previous line of output.
  166. * This is primarily used to determine how the first merge line
  167. * should appear, based on the last line of the previous commit.
  168. */
  169. enum graph_state prev_state;
  170. /*
  171. * The index of the column that refers to this commit.
  172. *
  173. * If none of the incoming columns refer to this commit,
  174. * this will be equal to num_columns.
  175. */
  176. int commit_index;
  177. /*
  178. * The commit_index for the previously displayed commit.
  179. *
  180. * This is used to determine how the first line of a merge
  181. * graph output should appear, based on the last line of the
  182. * previous commit.
  183. */
  184. int prev_commit_index;
  185. /*
  186. * Which layout variant to use to display merge commits. If the
  187. * commit's first parent is known to be in a column to the left of the
  188. * merge, then this value is 0 and we use the layout on the left.
  189. * Otherwise, the value is 1 and the layout on the right is used. This
  190. * field tells us how many columns the first parent occupies.
  191. *
  192. * 0) 1)
  193. *
  194. * | | | *-. | | *---.
  195. * | |_|/|\ \ | | |\ \ \
  196. * |/| | | | | | | | | | *
  197. */
  198. int merge_layout;
  199. /*
  200. * The number of columns added to the graph by the current commit. For
  201. * 2-way and octopus merges, this is usually one less than the
  202. * number of parents:
  203. *
  204. * | | | | | \
  205. * | * | | *---. \
  206. * | |\ \ | |\ \ \ \
  207. * | | | | | | | | | |
  208. *
  209. * num_parents: 2 num_parents: 4
  210. * edges_added: 1 edges_added: 3
  211. *
  212. * For left-skewed merges, the first parent fuses with its neighbor and
  213. * so one less column is added:
  214. *
  215. * | | | | | \
  216. * | * | | *-. \
  217. * |/| | |/|\ \ \
  218. * | | | | | | | |
  219. *
  220. * num_parents: 2 num_parents: 4
  221. * edges_added: 0 edges_added: 2
  222. *
  223. * This number determines how edges to the right of the merge are
  224. * displayed in commit and post-merge lines; if no columns have been
  225. * added then a vertical line should be used where a right-tracking
  226. * line would otherwise be used.
  227. *
  228. * | * \ | * |
  229. * | |\ \ |/| |
  230. * | | * \ | * |
  231. */
  232. int edges_added;
  233. /*
  234. * The number of columns added by the previous commit, which is used to
  235. * smooth edges appearing to the right of a commit in a commit line
  236. * following a post-merge line.
  237. */
  238. int prev_edges_added;
  239. /*
  240. * The maximum number of columns that can be stored in the columns
  241. * and new_columns arrays. This is also half the number of entries
  242. * that can be stored in the mapping and old_mapping arrays.
  243. */
  244. int column_capacity;
  245. /*
  246. * The number of columns (also called "branch lines" in some places)
  247. */
  248. int num_columns;
  249. /*
  250. * The number of columns in the new_columns array
  251. */
  252. int num_new_columns;
  253. /*
  254. * The number of entries in the mapping array
  255. */
  256. int mapping_size;
  257. /*
  258. * The column state before we output the current commit.
  259. */
  260. struct column *columns;
  261. /*
  262. * The new column state after we output the current commit.
  263. * Only valid when state is GRAPH_COLLAPSING.
  264. */
  265. struct column *new_columns;
  266. /*
  267. * An array that tracks the current state of each
  268. * character in the output line during state GRAPH_COLLAPSING.
  269. * Each entry is -1 if this character is empty, or a non-negative
  270. * integer if the character contains a branch line. The value of
  271. * the integer indicates the target position for this branch line.
  272. * (I.e., this array maps the current column positions to their
  273. * desired positions.)
  274. *
  275. * The maximum capacity of this array is always
  276. * sizeof(int) * 2 * column_capacity.
  277. */
  278. int *mapping;
  279. /*
  280. * A copy of the contents of the mapping array from the last commit,
  281. * which we use to improve the display of columns that are tracking
  282. * from right to left through a commit line. We also use this to
  283. * avoid allocating a fresh array when we compute the next mapping.
  284. */
  285. int *old_mapping;
  286. /*
  287. * The current default column color being used. This is
  288. * stored as an index into the array column_colors.
  289. */
  290. unsigned short default_column_color;
  291. };
  292. static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
  293. {
  294. struct git_graph *graph = data;
  295. static struct strbuf msgbuf = STRBUF_INIT;
  296. assert(opt);
  297. strbuf_reset(&msgbuf);
  298. if (opt->line_prefix)
  299. strbuf_add(&msgbuf, opt->line_prefix,
  300. opt->line_prefix_length);
  301. if (graph)
  302. graph_padding_line(graph, &msgbuf);
  303. return &msgbuf;
  304. }
  305. static const struct diff_options *default_diffopt;
  306. void graph_setup_line_prefix(struct diff_options *diffopt)
  307. {
  308. default_diffopt = diffopt;
  309. /* setup an output prefix callback if necessary */
  310. if (diffopt && !diffopt->output_prefix)
  311. diffopt->output_prefix = diff_output_prefix_callback;
  312. }
  313. struct git_graph *graph_init(struct rev_info *opt)
  314. {
  315. struct git_graph *graph = xmalloc(sizeof(struct git_graph));
  316. if (!column_colors) {
  317. char *string;
  318. if (git_config_get_string("log.graphcolors", &string)) {
  319. /* not configured -- use default */
  320. graph_set_column_colors(column_colors_ansi,
  321. column_colors_ansi_max);
  322. } else {
  323. static struct strvec custom_colors = STRVEC_INIT;
  324. strvec_clear(&custom_colors);
  325. parse_graph_colors_config(&custom_colors, string);
  326. free(string);
  327. /* graph_set_column_colors takes a max-index, not a count */
  328. graph_set_column_colors(custom_colors.v,
  329. custom_colors.nr - 1);
  330. }
  331. }
  332. graph->commit = NULL;
  333. graph->revs = opt;
  334. graph->num_parents = 0;
  335. graph->expansion_row = 0;
  336. graph->state = GRAPH_PADDING;
  337. graph->prev_state = GRAPH_PADDING;
  338. graph->commit_index = 0;
  339. graph->prev_commit_index = 0;
  340. graph->merge_layout = 0;
  341. graph->edges_added = 0;
  342. graph->prev_edges_added = 0;
  343. graph->num_columns = 0;
  344. graph->num_new_columns = 0;
  345. graph->mapping_size = 0;
  346. /*
  347. * Start the column color at the maximum value, since we'll
  348. * always increment it for the first commit we output.
  349. * This way we start at 0 for the first commit.
  350. */
  351. graph->default_column_color = column_colors_max - 1;
  352. /*
  353. * Allocate a reasonably large default number of columns
  354. * We'll automatically grow columns later if we need more room.
  355. */
  356. graph->column_capacity = 30;
  357. ALLOC_ARRAY(graph->columns, graph->column_capacity);
  358. ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
  359. ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
  360. ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity);
  361. /*
  362. * The diff output prefix callback, with this we can make
  363. * all the diff output to align with the graph lines.
  364. */
  365. opt->diffopt.output_prefix = diff_output_prefix_callback;
  366. opt->diffopt.output_prefix_data = graph;
  367. return graph;
  368. }
  369. static void graph_update_state(struct git_graph *graph, enum graph_state s)
  370. {
  371. graph->prev_state = graph->state;
  372. graph->state = s;
  373. }
  374. static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
  375. {
  376. if (graph->column_capacity >= num_columns)
  377. return;
  378. do {
  379. graph->column_capacity *= 2;
  380. } while (graph->column_capacity < num_columns);
  381. REALLOC_ARRAY(graph->columns, graph->column_capacity);
  382. REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
  383. REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
  384. REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2);
  385. }
  386. /*
  387. * Returns 1 if the commit will be printed in the graph output,
  388. * and 0 otherwise.
  389. */
  390. static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
  391. {
  392. /*
  393. * If revs->boundary is set, commits whose children have
  394. * been shown are always interesting, even if they have the
  395. * UNINTERESTING or TREESAME flags set.
  396. */
  397. if (graph->revs && graph->revs->boundary) {
  398. if (commit->object.flags & CHILD_SHOWN)
  399. return 1;
  400. }
  401. /*
  402. * Otherwise, use get_commit_action() to see if this commit is
  403. * interesting
  404. */
  405. return get_commit_action(graph->revs, commit) == commit_show;
  406. }
  407. static struct commit_list *next_interesting_parent(struct git_graph *graph,
  408. struct commit_list *orig)
  409. {
  410. struct commit_list *list;
  411. /*
  412. * If revs->first_parent_only is set, only the first
  413. * parent is interesting. None of the others are.
  414. */
  415. if (graph->revs->first_parent_only)
  416. return NULL;
  417. /*
  418. * Return the next interesting commit after orig
  419. */
  420. for (list = orig->next; list; list = list->next) {
  421. if (graph_is_interesting(graph, list->item))
  422. return list;
  423. }
  424. return NULL;
  425. }
  426. static struct commit_list *first_interesting_parent(struct git_graph *graph)
  427. {
  428. struct commit_list *parents = graph->commit->parents;
  429. /*
  430. * If this commit has no parents, ignore it
  431. */
  432. if (!parents)
  433. return NULL;
  434. /*
  435. * If the first parent is interesting, return it
  436. */
  437. if (graph_is_interesting(graph, parents->item))
  438. return parents;
  439. /*
  440. * Otherwise, call next_interesting_parent() to get
  441. * the next interesting parent
  442. */
  443. return next_interesting_parent(graph, parents);
  444. }
  445. static unsigned short graph_get_current_column_color(const struct git_graph *graph)
  446. {
  447. if (!want_color(graph->revs->diffopt.use_color))
  448. return column_colors_max;
  449. return graph->default_column_color;
  450. }
  451. /*
  452. * Update the graph's default column color.
  453. */
  454. static void graph_increment_column_color(struct git_graph *graph)
  455. {
  456. graph->default_column_color = (graph->default_column_color + 1) %
  457. column_colors_max;
  458. }
  459. static unsigned short graph_find_commit_color(const struct git_graph *graph,
  460. const struct commit *commit)
  461. {
  462. int i;
  463. for (i = 0; i < graph->num_columns; i++) {
  464. if (graph->columns[i].commit == commit)
  465. return graph->columns[i].color;
  466. }
  467. return graph_get_current_column_color(graph);
  468. }
  469. static int graph_find_new_column_by_commit(struct git_graph *graph,
  470. struct commit *commit)
  471. {
  472. int i;
  473. for (i = 0; i < graph->num_new_columns; i++) {
  474. if (graph->new_columns[i].commit == commit)
  475. return i;
  476. }
  477. return -1;
  478. }
  479. static void graph_insert_into_new_columns(struct git_graph *graph,
  480. struct commit *commit,
  481. int idx)
  482. {
  483. int i = graph_find_new_column_by_commit(graph, commit);
  484. int mapping_idx;
  485. /*
  486. * If the commit is not already in the new_columns array, then add it
  487. * and record it as being in the final column.
  488. */
  489. if (i < 0) {
  490. i = graph->num_new_columns++;
  491. graph->new_columns[i].commit = commit;
  492. graph->new_columns[i].color = graph_find_commit_color(graph, commit);
  493. }
  494. if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) {
  495. /*
  496. * If this is the first parent of a merge, choose a layout for
  497. * the merge line based on whether the parent appears in a
  498. * column to the left of the merge
  499. */
  500. int dist, shift;
  501. dist = idx - i;
  502. shift = (dist > 1) ? 2 * dist - 3 : 1;
  503. graph->merge_layout = (dist > 0) ? 0 : 1;
  504. graph->edges_added = graph->num_parents + graph->merge_layout - 2;
  505. mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
  506. graph->width += 2 * graph->merge_layout;
  507. } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) {
  508. /*
  509. * If some columns have been added by a merge, but this commit
  510. * was found in the last existing column, then adjust the
  511. * numbers so that the two edges immediately join, i.e.:
  512. *
  513. * * | * |
  514. * |\ \ => |\|
  515. * | |/ | *
  516. * | *
  517. */
  518. mapping_idx = graph->width - 2;
  519. graph->edges_added = -1;
  520. } else {
  521. mapping_idx = graph->width;
  522. graph->width += 2;
  523. }
  524. graph->mapping[mapping_idx] = i;
  525. }
  526. static void graph_update_columns(struct git_graph *graph)
  527. {
  528. struct commit_list *parent;
  529. int max_new_columns;
  530. int i, seen_this, is_commit_in_columns;
  531. /*
  532. * Swap graph->columns with graph->new_columns
  533. * graph->columns contains the state for the previous commit,
  534. * and new_columns now contains the state for our commit.
  535. *
  536. * We'll re-use the old columns array as storage to compute the new
  537. * columns list for the commit after this one.
  538. */
  539. SWAP(graph->columns, graph->new_columns);
  540. graph->num_columns = graph->num_new_columns;
  541. graph->num_new_columns = 0;
  542. /*
  543. * Now update new_columns and mapping with the information for the
  544. * commit after this one.
  545. *
  546. * First, make sure we have enough room. At most, there will
  547. * be graph->num_columns + graph->num_parents columns for the next
  548. * commit.
  549. */
  550. max_new_columns = graph->num_columns + graph->num_parents;
  551. graph_ensure_capacity(graph, max_new_columns);
  552. /*
  553. * Clear out graph->mapping
  554. */
  555. graph->mapping_size = 2 * max_new_columns;
  556. for (i = 0; i < graph->mapping_size; i++)
  557. graph->mapping[i] = -1;
  558. graph->width = 0;
  559. graph->prev_edges_added = graph->edges_added;
  560. graph->edges_added = 0;
  561. /*
  562. * Populate graph->new_columns and graph->mapping
  563. *
  564. * Some of the parents of this commit may already be in
  565. * graph->columns. If so, graph->new_columns should only contain a
  566. * single entry for each such commit. graph->mapping should
  567. * contain information about where each current branch line is
  568. * supposed to end up after the collapsing is performed.
  569. */
  570. seen_this = 0;
  571. is_commit_in_columns = 1;
  572. for (i = 0; i <= graph->num_columns; i++) {
  573. struct commit *col_commit;
  574. if (i == graph->num_columns) {
  575. if (seen_this)
  576. break;
  577. is_commit_in_columns = 0;
  578. col_commit = graph->commit;
  579. } else {
  580. col_commit = graph->columns[i].commit;
  581. }
  582. if (col_commit == graph->commit) {
  583. seen_this = 1;
  584. graph->commit_index = i;
  585. graph->merge_layout = -1;
  586. for (parent = first_interesting_parent(graph);
  587. parent;
  588. parent = next_interesting_parent(graph, parent)) {
  589. /*
  590. * If this is a merge, or the start of a new
  591. * childless column, increment the current
  592. * color.
  593. */
  594. if (graph->num_parents > 1 ||
  595. !is_commit_in_columns) {
  596. graph_increment_column_color(graph);
  597. }
  598. graph_insert_into_new_columns(graph, parent->item, i);
  599. }
  600. /*
  601. * We always need to increment graph->width by at
  602. * least 2, even if it has no interesting parents.
  603. * The current commit always takes up at least 2
  604. * spaces.
  605. */
  606. if (graph->num_parents == 0)
  607. graph->width += 2;
  608. } else {
  609. graph_insert_into_new_columns(graph, col_commit, -1);
  610. }
  611. }
  612. /*
  613. * Shrink mapping_size to be the minimum necessary
  614. */
  615. while (graph->mapping_size > 1 &&
  616. graph->mapping[graph->mapping_size - 1] < 0)
  617. graph->mapping_size--;
  618. }
  619. static int graph_num_dashed_parents(struct git_graph *graph)
  620. {
  621. return graph->num_parents + graph->merge_layout - 3;
  622. }
  623. static int graph_num_expansion_rows(struct git_graph *graph)
  624. {
  625. /*
  626. * Normally, we need two expansion rows for each dashed parent line from
  627. * an octopus merge:
  628. *
  629. * | *
  630. * | |\
  631. * | | \
  632. * | | \
  633. * | *-. \
  634. * | |\ \ \
  635. *
  636. * If the merge is skewed to the left, then its parents occupy one less
  637. * column, and we don't need as many expansion rows to route around it;
  638. * in some cases that means we don't need any expansion rows at all:
  639. *
  640. * | *
  641. * | |\
  642. * | * \
  643. * |/|\ \
  644. */
  645. return graph_num_dashed_parents(graph) * 2;
  646. }
  647. static int graph_needs_pre_commit_line(struct git_graph *graph)
  648. {
  649. return graph->num_parents >= 3 &&
  650. graph->commit_index < (graph->num_columns - 1) &&
  651. graph->expansion_row < graph_num_expansion_rows(graph);
  652. }
  653. void graph_update(struct git_graph *graph, struct commit *commit)
  654. {
  655. struct commit_list *parent;
  656. /*
  657. * Set the new commit
  658. */
  659. graph->commit = commit;
  660. /*
  661. * Count how many interesting parents this commit has
  662. */
  663. graph->num_parents = 0;
  664. for (parent = first_interesting_parent(graph);
  665. parent;
  666. parent = next_interesting_parent(graph, parent))
  667. {
  668. graph->num_parents++;
  669. }
  670. /*
  671. * Store the old commit_index in prev_commit_index.
  672. * graph_update_columns() will update graph->commit_index for this
  673. * commit.
  674. */
  675. graph->prev_commit_index = graph->commit_index;
  676. /*
  677. * Call graph_update_columns() to update
  678. * columns, new_columns, and mapping.
  679. */
  680. graph_update_columns(graph);
  681. graph->expansion_row = 0;
  682. /*
  683. * Update graph->state.
  684. * Note that we don't call graph_update_state() here, since
  685. * we don't want to update graph->prev_state. No line for
  686. * graph->state was ever printed.
  687. *
  688. * If the previous commit didn't get to the GRAPH_PADDING state,
  689. * it never finished its output. Goto GRAPH_SKIP, to print out
  690. * a line to indicate that portion of the graph is missing.
  691. *
  692. * If there are 3 or more parents, we may need to print extra rows
  693. * before the commit, to expand the branch lines around it and make
  694. * room for it. We need to do this only if there is a branch row
  695. * (or more) to the right of this commit.
  696. *
  697. * If there are less than 3 parents, we can immediately print the
  698. * commit line.
  699. */
  700. if (graph->state != GRAPH_PADDING)
  701. graph->state = GRAPH_SKIP;
  702. else if (graph_needs_pre_commit_line(graph))
  703. graph->state = GRAPH_PRE_COMMIT;
  704. else
  705. graph->state = GRAPH_COMMIT;
  706. }
  707. static int graph_is_mapping_correct(struct git_graph *graph)
  708. {
  709. int i;
  710. /*
  711. * The mapping is up to date if each entry is at its target,
  712. * or is 1 greater than its target.
  713. * (If it is 1 greater than the target, '/' will be printed, so it
  714. * will look correct on the next row.)
  715. */
  716. for (i = 0; i < graph->mapping_size; i++) {
  717. int target = graph->mapping[i];
  718. if (target < 0)
  719. continue;
  720. if (target == (i / 2))
  721. continue;
  722. return 0;
  723. }
  724. return 1;
  725. }
  726. static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line)
  727. {
  728. /*
  729. * Add additional spaces to the end of the strbuf, so that all
  730. * lines for a particular commit have the same width.
  731. *
  732. * This way, fields printed to the right of the graph will remain
  733. * aligned for the entire commit.
  734. */
  735. if (line->width < graph->width)
  736. graph_line_addchars(line, ' ', graph->width - line->width);
  737. }
  738. static void graph_output_padding_line(struct git_graph *graph,
  739. struct graph_line *line)
  740. {
  741. int i;
  742. /*
  743. * Output a padding row, that leaves all branch lines unchanged
  744. */
  745. for (i = 0; i < graph->num_new_columns; i++) {
  746. graph_line_write_column(line, &graph->new_columns[i], '|');
  747. graph_line_addch(line, ' ');
  748. }
  749. }
  750. int graph_width(struct git_graph *graph)
  751. {
  752. return graph->width;
  753. }
  754. static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line)
  755. {
  756. /*
  757. * Output an ellipsis to indicate that a portion
  758. * of the graph is missing.
  759. */
  760. graph_line_addstr(line, "...");
  761. if (graph_needs_pre_commit_line(graph))
  762. graph_update_state(graph, GRAPH_PRE_COMMIT);
  763. else
  764. graph_update_state(graph, GRAPH_COMMIT);
  765. }
  766. static void graph_output_pre_commit_line(struct git_graph *graph,
  767. struct graph_line *line)
  768. {
  769. int i, seen_this;
  770. /*
  771. * This function formats a row that increases the space around a commit
  772. * with multiple parents, to make room for it. It should only be
  773. * called when there are 3 or more parents.
  774. *
  775. * We need 2 extra rows for every parent over 2.
  776. */
  777. assert(graph->num_parents >= 3);
  778. /*
  779. * graph->expansion_row tracks the current expansion row we are on.
  780. * It should be in the range [0, num_expansion_rows - 1]
  781. */
  782. assert(0 <= graph->expansion_row &&
  783. graph->expansion_row < graph_num_expansion_rows(graph));
  784. /*
  785. * Output the row
  786. */
  787. seen_this = 0;
  788. for (i = 0; i < graph->num_columns; i++) {
  789. struct column *col = &graph->columns[i];
  790. if (col->commit == graph->commit) {
  791. seen_this = 1;
  792. graph_line_write_column(line, col, '|');
  793. graph_line_addchars(line, ' ', graph->expansion_row);
  794. } else if (seen_this && (graph->expansion_row == 0)) {
  795. /*
  796. * This is the first line of the pre-commit output.
  797. * If the previous commit was a merge commit and
  798. * ended in the GRAPH_POST_MERGE state, all branch
  799. * lines after graph->prev_commit_index were
  800. * printed as "\" on the previous line. Continue
  801. * to print them as "\" on this line. Otherwise,
  802. * print the branch lines as "|".
  803. */
  804. if (graph->prev_state == GRAPH_POST_MERGE &&
  805. graph->prev_commit_index < i)
  806. graph_line_write_column(line, col, '\\');
  807. else
  808. graph_line_write_column(line, col, '|');
  809. } else if (seen_this && (graph->expansion_row > 0)) {
  810. graph_line_write_column(line, col, '\\');
  811. } else {
  812. graph_line_write_column(line, col, '|');
  813. }
  814. graph_line_addch(line, ' ');
  815. }
  816. /*
  817. * Increment graph->expansion_row,
  818. * and move to state GRAPH_COMMIT if necessary
  819. */
  820. graph->expansion_row++;
  821. if (!graph_needs_pre_commit_line(graph))
  822. graph_update_state(graph, GRAPH_COMMIT);
  823. }
  824. static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line)
  825. {
  826. /*
  827. * For boundary commits, print 'o'
  828. * (We should only see boundary commits when revs->boundary is set.)
  829. */
  830. if (graph->commit->object.flags & BOUNDARY) {
  831. assert(graph->revs->boundary);
  832. graph_line_addch(line, 'o');
  833. return;
  834. }
  835. /*
  836. * get_revision_mark() handles all other cases without assert()
  837. */
  838. graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
  839. }
  840. /*
  841. * Draw the horizontal dashes of an octopus merge.
  842. */
  843. static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line)
  844. {
  845. /*
  846. * The parents of a merge commit can be arbitrarily reordered as they
  847. * are mapped onto display columns, for example this is a valid merge:
  848. *
  849. * | | *---.
  850. * | | |\ \ \
  851. * | | |/ / /
  852. * | |/| | /
  853. * | |_|_|/
  854. * |/| | |
  855. * 3 1 0 2
  856. *
  857. * The numbers denote which parent of the merge each visual column
  858. * corresponds to; we can't assume that the parents will initially
  859. * display in the order given by new_columns.
  860. *
  861. * To find the right color for each dash, we need to consult the
  862. * mapping array, starting from the column 2 places to the right of the
  863. * merge commit, and use that to find out which logical column each
  864. * edge will collapse to.
  865. *
  866. * Commits are rendered once all edges have collapsed to their correct
  867. * logcial column, so commit_index gives us the right visual offset for
  868. * the merge commit.
  869. */
  870. int i, j;
  871. struct column *col;
  872. int dashed_parents = graph_num_dashed_parents(graph);
  873. for (i = 0; i < dashed_parents; i++) {
  874. j = graph->mapping[(graph->commit_index + i + 2) * 2];
  875. col = &graph->new_columns[j];
  876. graph_line_write_column(line, col, '-');
  877. graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-');
  878. }
  879. return;
  880. }
  881. static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line)
  882. {
  883. int seen_this = 0;
  884. int i;
  885. /*
  886. * Output the row containing this commit
  887. * Iterate up to and including graph->num_columns,
  888. * since the current commit may not be in any of the existing
  889. * columns. (This happens when the current commit doesn't have any
  890. * children that we have already processed.)
  891. */
  892. seen_this = 0;
  893. for (i = 0; i <= graph->num_columns; i++) {
  894. struct column *col = &graph->columns[i];
  895. struct commit *col_commit;
  896. if (i == graph->num_columns) {
  897. if (seen_this)
  898. break;
  899. col_commit = graph->commit;
  900. } else {
  901. col_commit = graph->columns[i].commit;
  902. }
  903. if (col_commit == graph->commit) {
  904. seen_this = 1;
  905. graph_output_commit_char(graph, line);
  906. if (graph->num_parents > 2)
  907. graph_draw_octopus_merge(graph, line);
  908. } else if (seen_this && (graph->edges_added > 1)) {
  909. graph_line_write_column(line, col, '\\');
  910. } else if (seen_this && (graph->edges_added == 1)) {
  911. /*
  912. * This is either a right-skewed 2-way merge
  913. * commit, or a left-skewed 3-way merge.
  914. * There is no GRAPH_PRE_COMMIT stage for such
  915. * merges, so this is the first line of output
  916. * for this commit. Check to see what the previous
  917. * line of output was.
  918. *
  919. * If it was GRAPH_POST_MERGE, the branch line
  920. * coming into this commit may have been '\',
  921. * and not '|' or '/'. If so, output the branch
  922. * line as '\' on this line, instead of '|'. This
  923. * makes the output look nicer.
  924. */
  925. if (graph->prev_state == GRAPH_POST_MERGE &&
  926. graph->prev_edges_added > 0 &&
  927. graph->prev_commit_index < i)
  928. graph_line_write_column(line, col, '\\');
  929. else
  930. graph_line_write_column(line, col, '|');
  931. } else if (graph->prev_state == GRAPH_COLLAPSING &&
  932. graph->old_mapping[2 * i + 1] == i &&
  933. graph->mapping[2 * i] < i) {
  934. graph_line_write_column(line, col, '/');
  935. } else {
  936. graph_line_write_column(line, col, '|');
  937. }
  938. graph_line_addch(line, ' ');
  939. }
  940. /*
  941. * Update graph->state
  942. */
  943. if (graph->num_parents > 1)
  944. graph_update_state(graph, GRAPH_POST_MERGE);
  945. else if (graph_is_mapping_correct(graph))
  946. graph_update_state(graph, GRAPH_PADDING);
  947. else
  948. graph_update_state(graph, GRAPH_COLLAPSING);
  949. }
  950. static const char merge_chars[] = {'/', '|', '\\'};
  951. static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
  952. {
  953. int seen_this = 0;
  954. int i, j;
  955. struct commit_list *first_parent = first_interesting_parent(graph);
  956. struct column *parent_col = NULL;
  957. /*
  958. * Output the post-merge row
  959. */
  960. for (i = 0; i <= graph->num_columns; i++) {
  961. struct column *col = &graph->columns[i];
  962. struct commit *col_commit;
  963. if (i == graph->num_columns) {
  964. if (seen_this)
  965. break;
  966. col_commit = graph->commit;
  967. } else {
  968. col_commit = col->commit;
  969. }
  970. if (col_commit == graph->commit) {
  971. /*
  972. * Since the current commit is a merge find
  973. * the columns for the parent commits in
  974. * new_columns and use those to format the
  975. * edges.
  976. */
  977. struct commit_list *parents = first_parent;
  978. int par_column;
  979. int idx = graph->merge_layout;
  980. char c;
  981. seen_this = 1;
  982. for (j = 0; j < graph->num_parents; j++) {
  983. par_column = graph_find_new_column_by_commit(graph, parents->item);
  984. assert(par_column >= 0);
  985. c = merge_chars[idx];
  986. graph_line_write_column(line, &graph->new_columns[par_column], c);
  987. if (idx == 2) {
  988. if (graph->edges_added > 0 || j < graph->num_parents - 1)
  989. graph_line_addch(line, ' ');
  990. } else {
  991. idx++;
  992. }
  993. parents = next_interesting_parent(graph, parents);
  994. }
  995. if (graph->edges_added == 0)
  996. graph_line_addch(line, ' ');
  997. } else if (seen_this) {
  998. if (graph->edges_added > 0)
  999. graph_line_write_column(line, col, '\\');
  1000. else
  1001. graph_line_write_column(line, col, '|');
  1002. graph_line_addch(line, ' ');
  1003. } else {
  1004. graph_line_write_column(line, col, '|');
  1005. if (graph->merge_layout != 0 || i != graph->commit_index - 1) {
  1006. if (parent_col)
  1007. graph_line_write_column(
  1008. line, parent_col, '_');
  1009. else
  1010. graph_line_addch(line, ' ');
  1011. }
  1012. }
  1013. if (col_commit == first_parent->item)
  1014. parent_col = col;
  1015. }
  1016. /*
  1017. * Update graph->state
  1018. */
  1019. if (graph_is_mapping_correct(graph))
  1020. graph_update_state(graph, GRAPH_PADDING);
  1021. else
  1022. graph_update_state(graph, GRAPH_COLLAPSING);
  1023. }
  1024. static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line)
  1025. {
  1026. int i;
  1027. short used_horizontal = 0;
  1028. int horizontal_edge = -1;
  1029. int horizontal_edge_target = -1;
  1030. /*
  1031. * Swap the mapping and old_mapping arrays
  1032. */
  1033. SWAP(graph->mapping, graph->old_mapping);
  1034. /*
  1035. * Clear out the mapping array
  1036. */
  1037. for (i = 0; i < graph->mapping_size; i++)
  1038. graph->mapping[i] = -1;
  1039. for (i = 0; i < graph->mapping_size; i++) {
  1040. int target = graph->old_mapping[i];
  1041. if (target < 0)
  1042. continue;
  1043. /*
  1044. * Since update_columns() always inserts the leftmost
  1045. * column first, each branch's target location should
  1046. * always be either its current location or to the left of
  1047. * its current location.
  1048. *
  1049. * We never have to move branches to the right. This makes
  1050. * the graph much more legible, since whenever branches
  1051. * cross, only one is moving directions.
  1052. */
  1053. assert(target * 2 <= i);
  1054. if (target * 2 == i) {
  1055. /*
  1056. * This column is already in the
  1057. * correct place
  1058. */
  1059. assert(graph->mapping[i] == -1);
  1060. graph->mapping[i] = target;
  1061. } else if (graph->mapping[i - 1] < 0) {
  1062. /*
  1063. * Nothing is to the left.
  1064. * Move to the left by one
  1065. */
  1066. graph->mapping[i - 1] = target;
  1067. /*
  1068. * If there isn't already an edge moving horizontally
  1069. * select this one.
  1070. */
  1071. if (horizontal_edge == -1) {
  1072. int j;
  1073. horizontal_edge = i;
  1074. horizontal_edge_target = target;
  1075. /*
  1076. * The variable target is the index of the graph
  1077. * column, and therefore target*2+3 is the
  1078. * actual screen column of the first horizontal
  1079. * line.
  1080. */
  1081. for (j = (target * 2)+3; j < (i - 2); j += 2)
  1082. graph->mapping[j] = target;
  1083. }
  1084. } else if (graph->mapping[i - 1] == target) {
  1085. /*
  1086. * There is a branch line to our left
  1087. * already, and it is our target. We
  1088. * combine with this line, since we share
  1089. * the same parent commit.
  1090. *
  1091. * We don't have to add anything to the
  1092. * output or mapping, since the
  1093. * existing branch line has already taken
  1094. * care of it.
  1095. */
  1096. } else {
  1097. /*
  1098. * There is a branch line to our left,
  1099. * but it isn't our target. We need to
  1100. * cross over it.
  1101. *
  1102. * The space just to the left of this
  1103. * branch should always be empty.
  1104. */
  1105. assert(graph->mapping[i - 1] > target);
  1106. assert(graph->mapping[i - 2] < 0);
  1107. graph->mapping[i - 2] = target;
  1108. /*
  1109. * Mark this branch as the horizontal edge to
  1110. * prevent any other edges from moving
  1111. * horizontally.
  1112. */
  1113. if (horizontal_edge == -1) {
  1114. int j;
  1115. horizontal_edge_target = target;
  1116. horizontal_edge = i - 1;
  1117. for (j = (target * 2) + 3; j < (i - 2); j += 2)
  1118. graph->mapping[j] = target;
  1119. }
  1120. }
  1121. }
  1122. /*
  1123. * Copy the current mapping array into old_mapping
  1124. */
  1125. COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size);
  1126. /*
  1127. * The new mapping may be 1 smaller than the old mapping
  1128. */
  1129. if (graph->mapping[graph->mapping_size - 1] < 0)
  1130. graph->mapping_size--;
  1131. /*
  1132. * Output out a line based on the new mapping info
  1133. */
  1134. for (i = 0; i < graph->mapping_size; i++) {
  1135. int target = graph->mapping[i];
  1136. if (target < 0)
  1137. graph_line_addch(line, ' ');
  1138. else if (target * 2 == i)
  1139. graph_line_write_column(line, &graph->new_columns[target], '|');
  1140. else if (target == horizontal_edge_target &&
  1141. i != horizontal_edge - 1) {
  1142. /*
  1143. * Set the mappings for all but the
  1144. * first segment to -1 so that they
  1145. * won't continue into the next line.
  1146. */
  1147. if (i != (target * 2)+3)
  1148. graph->mapping[i] = -1;
  1149. used_horizontal = 1;
  1150. graph_line_write_column(line, &graph->new_columns[target], '_');
  1151. } else {
  1152. if (used_horizontal && i < horizontal_edge)
  1153. graph->mapping[i] = -1;
  1154. graph_line_write_column(line, &graph->new_columns[target], '/');
  1155. }
  1156. }
  1157. /*
  1158. * If graph->mapping indicates that all of the branch lines
  1159. * are already in the correct positions, we are done.
  1160. * Otherwise, we need to collapse some branch lines together.
  1161. */
  1162. if (graph_is_mapping_correct(graph))
  1163. graph_update_state(graph, GRAPH_PADDING);
  1164. }
  1165. int graph_next_line(struct git_graph *graph, struct strbuf *sb)
  1166. {
  1167. int shown_commit_line = 0;
  1168. struct graph_line line = { .buf = sb, .width = 0 };
  1169. /*
  1170. * We could conceivable be called with a NULL commit
  1171. * if our caller has a bug, and invokes graph_next_line()
  1172. * immediately after graph_init(), without first calling
  1173. * graph_update(). Return without outputting anything in this
  1174. * case.
  1175. */
  1176. if (!graph->commit)
  1177. return -1;
  1178. switch (graph->state) {
  1179. case GRAPH_PADDING:
  1180. graph_output_padding_line(graph, &line);
  1181. break;
  1182. case GRAPH_SKIP:
  1183. graph_output_skip_line(graph, &line);
  1184. break;
  1185. case GRAPH_PRE_COMMIT:
  1186. graph_output_pre_commit_line(graph, &line);
  1187. break;
  1188. case GRAPH_COMMIT:
  1189. graph_output_commit_line(graph, &line);
  1190. shown_commit_line = 1;
  1191. break;
  1192. case GRAPH_POST_MERGE:
  1193. graph_output_post_merge_line(graph, &line);
  1194. break;
  1195. case GRAPH_COLLAPSING:
  1196. graph_output_collapsing_line(graph, &line);
  1197. break;
  1198. }
  1199. graph_pad_horizontally(graph, &line);
  1200. return shown_commit_line;
  1201. }
  1202. static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
  1203. {
  1204. int i;
  1205. struct graph_line line = { .buf = sb, .width = 0 };
  1206. if (graph->state != GRAPH_COMMIT) {
  1207. graph_next_line(graph, sb);
  1208. return;
  1209. }
  1210. /*
  1211. * Output the row containing this commit
  1212. * Iterate up to and including graph->num_columns,
  1213. * since the current commit may not be in any of the existing
  1214. * columns. (This happens when the current commit doesn't have any
  1215. * children that we have already processed.)
  1216. */
  1217. for (i = 0; i < graph->num_columns; i++) {
  1218. struct column *col = &graph->columns[i];
  1219. graph_line_write_column(&line, col, '|');
  1220. if (col->commit == graph->commit && graph->num_parents > 2) {
  1221. int len = (graph->num_parents - 2) * 2;
  1222. graph_line_addchars(&line, ' ', len);
  1223. } else {
  1224. graph_line_addch(&line, ' ');
  1225. }
  1226. }
  1227. graph_pad_horizontally(graph, &line);
  1228. /*
  1229. * Update graph->prev_state since we have output a padding line
  1230. */
  1231. graph->prev_state = GRAPH_PADDING;
  1232. }
  1233. int graph_is_commit_finished(struct git_graph const *graph)
  1234. {
  1235. return (graph->state == GRAPH_PADDING);
  1236. }
  1237. void graph_show_commit(struct git_graph *graph)
  1238. {
  1239. struct strbuf msgbuf = STRBUF_INIT;
  1240. int shown_commit_line = 0;
  1241. graph_show_line_prefix(default_diffopt);
  1242. if (!graph)
  1243. return;
  1244. /*
  1245. * When showing a diff of a merge against each of its parents, we
  1246. * are called once for each parent without graph_update having been
  1247. * called. In this case, simply output a single padding line.
  1248. */
  1249. if (graph_is_commit_finished(graph)) {
  1250. graph_show_padding(graph);
  1251. shown_commit_line = 1;
  1252. }
  1253. while (!shown_commit_line && !graph_is_commit_finished(graph)) {
  1254. shown_commit_line = graph_next_line(graph, &msgbuf);
  1255. fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
  1256. graph->revs->diffopt.file);
  1257. if (!shown_commit_line) {
  1258. putc('\n', graph->revs->diffopt.file);
  1259. graph_show_line_prefix(&graph->revs->diffopt);
  1260. }
  1261. strbuf_setlen(&msgbuf, 0);
  1262. }
  1263. strbuf_release(&msgbuf);
  1264. }
  1265. void graph_show_oneline(struct git_graph *graph)
  1266. {
  1267. struct strbuf msgbuf = STRBUF_INIT;
  1268. graph_show_line_prefix(default_diffopt);
  1269. if (!graph)
  1270. return;
  1271. graph_next_line(graph, &msgbuf);
  1272. fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
  1273. strbuf_release(&msgbuf);
  1274. }
  1275. void graph_show_padding(struct git_graph *graph)
  1276. {
  1277. struct strbuf msgbuf = STRBUF_INIT;
  1278. graph_show_line_prefix(default_diffopt);
  1279. if (!graph)
  1280. return;
  1281. graph_padding_line(graph, &msgbuf);
  1282. fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
  1283. strbuf_release(&msgbuf);
  1284. }
  1285. int graph_show_remainder(struct git_graph *graph)
  1286. {
  1287. struct strbuf msgbuf = STRBUF_INIT;
  1288. int shown = 0;
  1289. graph_show_line_prefix(default_diffopt);
  1290. if (!graph)
  1291. return 0;
  1292. if (graph_is_commit_finished(graph))
  1293. return 0;
  1294. for (;;) {
  1295. graph_next_line(graph, &msgbuf);
  1296. fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
  1297. graph->revs->diffopt.file);
  1298. strbuf_setlen(&msgbuf, 0);
  1299. shown = 1;
  1300. if (!graph_is_commit_finished(graph)) {
  1301. putc('\n', graph->revs->diffopt.file);
  1302. graph_show_line_prefix(&graph->revs->diffopt);
  1303. } else {
  1304. break;
  1305. }
  1306. }
  1307. strbuf_release(&msgbuf);
  1308. return shown;
  1309. }
  1310. static void graph_show_strbuf(struct git_graph *graph,
  1311. FILE *file,
  1312. struct strbuf const *sb)
  1313. {
  1314. char *p;
  1315. /*
  1316. * Print the strbuf line by line,
  1317. * and display the graph info before each line but the first.
  1318. */
  1319. p = sb->buf;
  1320. while (p) {
  1321. size_t len;
  1322. char *next_p = strchr(p, '\n');
  1323. if (next_p) {
  1324. next_p++;
  1325. len = next_p - p;
  1326. } else {
  1327. len = (sb->buf + sb->len) - p;
  1328. }
  1329. fwrite(p, sizeof(char), len, file);
  1330. if (next_p && *next_p != '\0')
  1331. graph_show_oneline(graph);
  1332. p = next_p;
  1333. }
  1334. }
  1335. void graph_show_commit_msg(struct git_graph *graph,
  1336. FILE *file,
  1337. struct strbuf const *sb)
  1338. {
  1339. int newline_terminated;
  1340. /*
  1341. * Show the commit message
  1342. */
  1343. graph_show_strbuf(graph, file, sb);
  1344. if (!graph)
  1345. return;
  1346. newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
  1347. /*
  1348. * If there is more output needed for this commit, show it now
  1349. */
  1350. if (!graph_is_commit_finished(graph)) {
  1351. /*
  1352. * If sb doesn't have a terminating newline, print one now,
  1353. * so we can start the remainder of the graph output on a
  1354. * new line.
  1355. */
  1356. if (!newline_terminated)
  1357. putc('\n', file);
  1358. graph_show_remainder(graph);
  1359. /*
  1360. * If sb ends with a newline, our output should too.
  1361. */
  1362. if (newline_terminated)
  1363. putc('\n', file);
  1364. }
  1365. }