MCPcopy Create free account
hub / github.com/47ng/nuqs / computeLayout

Function computeLayout

packages/docs/src/components/commit-graph.tsx:97–205  ·  view source on GitHub ↗
(commits: Commit[])

Source from the content-addressed store, hash-verified

95}
96
97function computeLayout(commits: Commit[]): GraphRow[] {
98 const rows: GraphRow[] = []
99 // Active rails: each slot holds the hash of the commit it's "waiting for"
100 const rails: (string | null)[] = []
101
102 for (const commit of commits) {
103 const hash = commit.hash
104
105 // Find which rail this commit occupies (if any rail is waiting for it)
106 let commitRail = rails.indexOf(hash)
107
108 if (commitRail === -1) {
109 // New branch — find first empty slot or append
110 const emptySlot = rails.indexOf(null)
111 if (emptySlot !== -1) {
112 commitRail = emptySlot
113 rails[commitRail] = hash
114 } else {
115 commitRail = rails.length
116 rails.push(hash)
117 }
118 }
119
120 const commitColor = color(commitRail)
121 const edges: Edge[] = []
122
123 // Draw straight lines for all other active rails (pass-through)
124 for (let r = 0; r < rails.length; r++) {
125 if (r !== commitRail && rails[r] !== null) {
126 edges.push({
127 fromRail: r,
128 toRail: r,
129 color: color(r),
130 type: 'straight'
131 })
132 }
133 }
134
135 // Clear this rail — the commit has been rendered
136 rails[commitRail] = null
137
138 // Process parents
139 const parents = commit.parents
140 if (parents.length >= 1) {
141 const firstParent = parents[0]
142 // First parent continues on the same rail
143 const existingRail = rails.indexOf(firstParent)
144 if (existingRail !== -1) {
145 // Parent already expected on another rail — merge line
146 edges.push({
147 fromRail: commitRail,
148 toRail: existingRail,
149 color: commitColor,
150 type: 'merge-in'
151 })
152 } else {
153 // Parent takes this commit's rail
154 rails[commitRail] = firstParent

Callers 1

CommitGraphFunction · 0.85

Calls 2

colorFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected