File size: 1,512 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { CheckpointNode } from "@/api/types/checkpoints";

export interface GraphRow {
  node: CheckpointNode;
  lane: number;
  lanesBefore: Array<string | null>;
  lanesAfter: Array<string | null>;
  parentLane: number | null;
}

/** Build a top-to-bottom Git lane layout from newest-first commits. */
export function buildGraphRows(nodes: CheckpointNode[]): GraphRow[] {
  const lanes: Array<string | null> = [];
  const rows: GraphRow[] = [];

  for (const node of nodes) {
    let lane = lanes.indexOf(node.commit);
    if (lane < 0) {
      lane = lanes.indexOf(null);
      if (lane < 0) lane = lanes.length;
      lanes[lane] = node.commit;
    }

    const before = [...lanes];
    let parentLane: number | null = null;
    if (node.parent_commit) {
      const existingParent = lanes.indexOf(node.parent_commit);
      if (existingParent >= 0 && existingParent !== lane) {
        parentLane = existingParent;
        lanes[lane] = null;
      } else {
        parentLane = lane;
        lanes[lane] = node.parent_commit;
      }
    } else {
      lanes[lane] = null;
    }

    while (lanes.length && lanes[lanes.length - 1] === null) lanes.pop();
    rows.push({
      node,
      lane,
      lanesBefore: before,
      lanesAfter: [...lanes],
      parentLane,
    });
  }

  return rows;
}

export function graphLaneCount(rows: GraphRow[]): number {
  return Math.max(
    1,
    ...rows.map((row) =>
      Math.max(row.lanesBefore.length, row.lanesAfter.length, row.lane + 1),
    ),
  );
}