-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathgenerateGraphData.ts
166 lines (160 loc) · 4.04 KB
/
generateGraphData.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Represent a commit to be rendered in the GitCommitGraph.
*/
export interface ICommit {
/**
* The hash of the commit.
*/
sha: string;
/**
* A list of parents' hashes.
*/
parents: string[];
}
/**
* Represents a commit node in the GitCommitGraph.
*/
export interface INode {
/**
* The hash of the commit.
*/
sha: string;
/**
* The commit dot in GitCommitGraph.
*/
dot: { lateralOffset: number; branch: number };
/**
* A list of routes that should be rendered together with the dot.
*/
routes: IRoute[];
/**
* Vertical offset of the dot and the start of the route(s) with respect to the top of the svg image.
*/
yOffset: number;
}
/**
* Represent a route that should be rendered with a commit dot.
*/
export interface IRoute {
/**
* lateral offset of the start of the route
*/
from: number;
/**
* lateral offset of the end of the route
*/
to: number;
/**
* The route's branch number.
*/
branch: number;
}
const Node = (
sha: string,
offset: number,
branch: number,
routes: IRoute[],
yOffset: number
): INode => ({
sha,
dot: { lateralOffset: offset, branch },
routes,
yOffset
});
function remove(list: number[], item: number): number[] {
list.splice(list.indexOf(item), 1);
return list;
}
/**
* Generate graph data.
* @param commits a list of commit, which should have `sha`, `parents` properties.
* @param getNodeHeight a callback to retrieve the height of the history node
* @returns data nodes, a json list of
[
{
sha,
{offset, branch}, //dot
[
{from, to, branch}, // route 1
{from, to, branch}, // route 2
{from, to, branch},
] // routes
} // node
],
*/
export function generateGraphData(
commits: ICommit[],
getNodeHeight: (sha: string) => number
): INode[] {
const nodes: INode[] = [];
const branchIndex = [0];
const reserve: number[] = [];
const branches: { [sha: string]: number } = {};
function getBranch(sha: string) {
if (branches[sha] === null || branches[sha] === undefined) {
branches[sha] = branchIndex[0];
reserve.push(branchIndex[0]);
branchIndex[0]++;
}
return branches[sha];
}
let currentYOffset = 25;
commits.forEach((commit, index) => {
let b, i;
const branch = getBranch(commit.sha);
const numParents = commit.parents.length;
const offset = reserve.indexOf(branch);
const routes: IRoute[] = [];
if (numParents === 1) {
if (branches[commit.parents[0]] || branches[commit.parents[0]] === 0) {
// create branch
const iterable = reserve.slice(offset + 1);
for (i = 0; i < iterable.length; i++) {
b = iterable[i];
routes.push({
from: i + offset + 1,
to: i + offset + 1 - 1,
branch: b
});
}
const iterable1 = reserve.slice(0, offset);
for (i = 0; i < iterable1.length; i++) {
b = iterable1[i];
routes.push({ from: i, to: i, branch: b });
}
remove(reserve, branch);
routes.push({
from: offset,
to: reserve.indexOf(branches[commit.parents[0]]),
branch
});
} else {
// straight
for (i = 0; i < reserve.length; i++) {
b = reserve[i];
routes.push({ from: i, to: i, branch: b });
}
branches[commit.parents[0]] = branch;
}
} else if (numParents === 2) {
// merge branch
branches[commit.parents[0]] = branch;
for (i = 0; i < reserve.length; i++) {
b = reserve[i];
routes.push({ from: i, to: i, branch: b });
}
const otherBranch = getBranch(commit.parents[1]);
routes.push({
from: offset,
to: reserve.indexOf(otherBranch),
branch: otherBranch
});
}
if (index - 1 >= 0) {
currentYOffset += getNodeHeight(commits[index - 1].sha);
}
const node = Node(commit.sha, offset, branch, routes, currentYOffset);
nodes.push(node);
});
return nodes;
}