Skip to content

Commit 9b805c1

Browse files
authored
feat(tui): render Mermaid GitGraph diagrams (#42179)
1 parent d31a994 commit 9b805c1

12 files changed

Lines changed: 792 additions & 1 deletion

File tree

‎packages/merman/src/detect.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { MermaidDiagramKind } from "./diagnostics.js"
22
import { isMermaidFlowchartDiagram } from "./flowchart/parser.js"
3+
import { isMermaidGitGraphDiagram } from "./gitgraph/parser.js"
34
import { isMermaidSequenceDiagram } from "./sequence/parser.js"
45
import { isMermaidStateDiagram } from "./state/parser.js"
56
import { isMermaidTimelineDiagram } from "./timeline/parser.js"
67

78
export function detectMermaidDiagram(content: string): MermaidDiagramKind | undefined {
89
if (isMermaidFlowchartDiagram(content)) return "flowchart"
10+
if (isMermaidGitGraphDiagram(content)) return "gitGraph"
911
if (isMermaidSequenceDiagram(content)) return "sequence"
1012
if (isMermaidStateDiagram(content)) return "state"
1113
if (isMermaidTimelineDiagram(content)) return "timeline"

‎packages/merman/src/diagnostics.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type MermaidDiagramKind = "flowchart" | "sequence" | "state" | "timeline"
1+
export type MermaidDiagramKind = "flowchart" | "sequence" | "state" | "timeline" | "gitGraph"
22

33
/** An otherwise valid diagram contains syntax that this renderer does not support. */
44
export class MermaidSyntaxError extends Error {
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { MermaidSyntaxError } from "../diagnostics.js"
3+
import { renderGitGraphDiagram } from "./diagram.js"
4+
import { drawGitGraphDiagramGrid } from "./drawing.js"
5+
import { isMermaidGitGraphDiagram, parseMermaidGitGraphDiagram } from "./parser.js"
6+
import { renderGitGraphGridText } from "./render-grid.js"
7+
import { resolveGitGraphStyleColors } from "./style.js"
8+
9+
describe("GitGraphDiagram", () => {
10+
test("detects and parses commits, branches, checkout, tags, types, and merges", () => {
11+
const diagram = parseMermaidGitGraphDiagram(`gitGraph TB:
12+
commit id: "init"
13+
branch feature order: 1
14+
commit id: "api" msg: "Add API" tag: "ready"
15+
checkout main
16+
commit id: "docs" type: HIGHLIGHT
17+
merge feature id: "merge-feature"`)
18+
19+
expect(diagram).toEqual({
20+
direction: "TB",
21+
branches: [
22+
{ name: "main", order: 0, head: "merge-feature" },
23+
{ name: "feature", order: 1, head: "api" },
24+
],
25+
commits: [
26+
{ id: "init", tags: [], type: "NORMAL", branch: "main", parents: [] },
27+
{ id: "api", message: "Add API", tags: ["ready"], type: "NORMAL", branch: "feature", parents: ["init"] },
28+
{ id: "docs", tags: [], type: "HIGHLIGHT", branch: "main", parents: ["init"] },
29+
{
30+
id: "merge-feature",
31+
tags: [],
32+
type: "NORMAL",
33+
branch: "main",
34+
parents: ["docs", "api"],
35+
},
36+
],
37+
})
38+
})
39+
40+
test("renders branch and merge transitions beside compact labels", () => {
41+
const source = `gitGraph
42+
commit id: "baseline"
43+
branch refactor
44+
commit id: "extract-seam" msg: "Extract seam"
45+
commit id: "add-tests" tag: "ready"
46+
checkout main
47+
commit id: "unrelated-fix"
48+
merge refactor id: "land-refactor" tag: "v2"`
49+
50+
expect(renderGitGraphDiagram(source)).toBe(`● baseline
51+
├─╮
52+
│ ● Extract seam
53+
│ ● add-tests [refactor] [ready]
54+
● │ unrelated-fix
55+
◎─╯ land-refactor [main] [v2]`)
56+
})
57+
58+
test("uses deterministic generated ids", () => {
59+
expect(parseMermaidGitGraphDiagram("gitGraph\n commit\n commit").commits.map((commit) => commit.id)).toEqual([
60+
"commit-1",
61+
"commit-2",
62+
])
63+
})
64+
65+
test("supports shorthand messages and preserves branch heads without direct commits", () => {
66+
const diagram = parseMermaidGitGraphDiagram(`gitGraph
67+
commit "Initial release"
68+
branch feature
69+
checkout main
70+
commit id: next`)
71+
72+
expect(diagram.commits[0]?.message).toBe("Initial release")
73+
expect(diagram.branches).toEqual([
74+
{ name: "main", order: 0, head: "next" },
75+
{ name: "feature", head: "commit-1" },
76+
])
77+
expect(
78+
renderGitGraphDiagram(`gitGraph
79+
commit id: base
80+
branch feature
81+
checkout main
82+
commit id: next`),
83+
).toContain("base [feature]")
84+
})
85+
86+
test("places unordered branches before explicitly ordered branches", () => {
87+
const diagram = parseMermaidGitGraphDiagram(`gitGraph
88+
commit id: base
89+
branch later order: 2
90+
checkout main
91+
branch ordinary
92+
checkout main
93+
branch earlier order: 1`)
94+
95+
expect(diagram.branches.map((branch) => branch.name)).toEqual(["main", "ordinary", "earlier", "later"])
96+
})
97+
98+
test("keeps comment markers inside quoted labels", () => {
99+
expect(parseMermaidGitGraphDiagram('gitGraph\n commit id: "release%%candidate" %% comment').commits[0]?.id).toBe(
100+
"release%%candidate",
101+
)
102+
})
103+
104+
test("uses rounded routing for wide lane transitions", () => {
105+
expect(
106+
renderGitGraphDiagram(`gitGraph
107+
commit id: base
108+
branch one
109+
branch two
110+
commit id: work`),
111+
).toBe(`● base [main] [one]
112+
├───╮
113+
● work [two]`)
114+
})
115+
116+
test("preserves direction semantics while rendering vertically", () => {
117+
const source = "gitGraph BT:\n commit id: one"
118+
const diagram = parseMermaidGitGraphDiagram(source)
119+
expect(diagram.direction).toBe("BT")
120+
expect(renderGitGraphGridText(drawGitGraphDiagramGrid(diagram, { direction: "LR" }))).toBe(
121+
renderGitGraphDiagram(source),
122+
)
123+
})
124+
125+
test("reports semantic failures with source diagnostics", () => {
126+
expect(() => parseMermaidGitGraphDiagram("gitGraph\n checkout missing")).toThrow(
127+
new MermaidSyntaxError("gitGraph", 2, "checkout missing", 'Unknown branch "missing"'),
128+
)
129+
expect(() => parseMermaidGitGraphDiagram("gitGraph\n cherry-pick id: one")).toThrow(
130+
new MermaidSyntaxError("gitGraph", 2, "cherry-pick id: one", "Cherry-pick is not supported"),
131+
)
132+
expect(() => parseMermaidGitGraphDiagram("gitGraph\n commit id: same\n commit id: same")).toThrow(
133+
'Duplicate commit id "same"',
134+
)
135+
expect(() => parseMermaidGitGraphDiagram("gitGraph\n branch feature\n checkout main\n branch feature")).toThrow(
136+
'Duplicate branch "feature"',
137+
)
138+
expect(() =>
139+
parseMermaidGitGraphDiagram("gitGraph\n branch feature\n commit id: work\n checkout main\n merge feature"),
140+
).toThrow('Branch "main" has no commits')
141+
})
142+
143+
test("draws semantic styles for rails, commit types, merges, and labels", () => {
144+
const grid = drawGitGraphDiagramGrid(
145+
parseMermaidGitGraphDiagram(`gitGraph
146+
commit id: base
147+
branch feature
148+
commit id: work type: REVERSE
149+
checkout main
150+
commit id: checkpoint type: HIGHLIGHT
151+
merge feature id: done`),
152+
)
153+
const styles = new Set(grid.rows.flatMap((row) => row.map((cell) => cell.style).filter(Boolean)))
154+
155+
expect(styles).toEqual(new Set(["branch0", "branch1", "commit", "reverse", "highlight", "merge", "label"]))
156+
expect(Object.keys(resolveGitGraphStyleColors()).sort()).toEqual(
157+
[
158+
"branch0",
159+
"branch1",
160+
"branch2",
161+
"branch3",
162+
"branch4",
163+
"branch5",
164+
"branch6",
165+
"branch7",
166+
"commit",
167+
"highlight",
168+
"label",
169+
"merge",
170+
"reverse",
171+
].sort(),
172+
)
173+
})
174+
175+
test("recognizes only GitGraph headers", () => {
176+
expect(isMermaidGitGraphDiagram("%% comment\ngitGraph LR:\n commit")).toBe(true)
177+
expect(isMermaidGitGraphDiagram("graph LR\n A --> B")).toBe(false)
178+
expect(() => parseMermaidGitGraphDiagram("commit id: missing-header")).toThrow("GitGraph header is required")
179+
expect(() => parseMermaidGitGraphDiagram("gitGraph\n commit\n gitGraph")).toThrow(
180+
"GitGraph header can only appear once",
181+
)
182+
})
183+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { drawGitGraphDiagramGrid } from "./drawing.js"
2+
import { parseMermaidGitGraphDiagram } from "./parser.js"
3+
import { renderGitGraphGridText } from "./render-grid.js"
4+
import type { GitGraphDiagramRenderOptions } from "./types.js"
5+
6+
export function renderGitGraphDiagram(content: string, options: GitGraphDiagramRenderOptions = {}): string {
7+
return renderGitGraphGridText(drawGitGraphDiagramGrid(parseMermaidGitGraphDiagram(content), options))
8+
}

0 commit comments

Comments
 (0)