-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathloadCommits.ts
More file actions
171 lines (159 loc) · 4.74 KB
/
Copy pathloadCommits.ts
File metadata and controls
171 lines (159 loc) · 4.74 KB
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
167
168
169
170
171
import type { SimpleGit } from "simple-git";
import type {
DateType,
GitCommitNode,
GitLogEntry,
GitRefData,
QueryResult
} from "@/backend/types";
const eolRegex = /\r\n|\r|\n/g;
const gitLogSeparator = "XX7Nal-YARtTpjCikii9nJxER19D6diSyk-AWkPb";
type LoadCommitsInput = {
branchNames: string[];
maxCommits: number;
showRemoteBranches: boolean;
hard: boolean;
dateType: DateType;
showUncommittedChanges: boolean;
};
async function getRefs(git: SimpleGit, showRemoteBranches: boolean): Promise<GitRefData> {
try {
const args = ["show-ref"];
if (!showRemoteBranches) args.push("--heads", "--tags");
args.push("-d", "--head");
const stdout = await git.raw(args);
const refData: GitRefData = { head: null, refs: [] };
const lines = stdout.split(eolRegex);
for (let i = 0; i < lines.length - 1; i++) {
const parts = lines[i].split(" ");
if (parts.length < 2) continue;
const hash = parts.shift()!;
const ref = parts.join(" ");
if (ref.startsWith("refs/heads/")) {
refData.refs.push({ hash, name: ref.substring(11), type: "head" });
} else if (ref.startsWith("refs/tags/")) {
refData.refs.push({
hash,
name: ref.endsWith("^{}") ? ref.substring(10, ref.length - 3) : ref.substring(10),
type: "tag"
});
} else if (ref.startsWith("refs/remotes/")) {
refData.refs.push({ hash, name: ref.substring(13), type: "remote" });
} else if (ref === "HEAD") {
refData.head = hash;
}
}
return refData;
} catch {
return { head: null, refs: [] };
}
}
async function getLog(
git: SimpleGit,
branches: string[],
maxCommits: number,
showRemoteBranches: boolean,
dateType: DateType
): Promise<GitLogEntry[]> {
const dateField = dateType === "Author Date" ? "%at" : "%ct";
const format = ["%H", "%P", "%an", "%ae", dateField, "%s"].join(gitLogSeparator);
const args = ["log", `--max-count=${maxCommits}`, `--format=${format}`, "--date-order"];
if (branches.length === 1 && branches[0] === "") {
args.push("--branches", "--tags");
if (showRemoteBranches) args.push("--remotes");
} else {
args.push(...branches);
}
try {
const stdout = await git.raw(args);
const lines = stdout.split(eolRegex);
const commits: GitLogEntry[] = [];
for (let i = 0; i < lines.length - 1; i++) {
const line = lines[i].split(gitLogSeparator);
if (line.length !== 6) break;
commits.push({
hash: line[0],
parentHashes: line[1].split(" "),
author: line[2],
email: line[3],
date: parseInt(line[4]),
message: line[5]
});
}
return commits;
} catch {
return [];
}
}
async function getUnsavedChanges(git: SimpleGit) {
try {
const status = await git.status();
if (status.files.length === 0) return null;
return { branch: status.current ?? "HEAD", changes: status.files.length };
} catch {
return null;
}
}
export async function loadCommits(
git: SimpleGit,
input: LoadCommitsInput
): Promise<QueryResult<"loadCommits">> {
const {
branchNames: branchName,
maxCommits,
showRemoteBranches,
hard,
dateType,
showUncommittedChanges
} = input;
const [rawCommits, refData] = await Promise.all([
getLog(git, branchName, maxCommits + 1, showRemoteBranches, dateType),
getRefs(git, showRemoteBranches)
]);
let commits = rawCommits;
const moreCommitsAvailable = commits.length === maxCommits + 1;
if (moreCommitsAvailable) commits = commits.slice(0, -1);
if (refData.head !== null) {
for (let i = 0; i < commits.length; i++) {
if (refData.head === commits[i].hash) {
const unsaved = showUncommittedChanges ? await getUnsavedChanges(git) : null;
if (unsaved !== null) {
commits.unshift({
hash: "*",
parentHashes: [refData.head],
author: "*",
email: "",
date: Math.round(new Date().getTime() / 1000),
message: `Uncommitted Changes (${unsaved.changes})`
});
}
break;
}
}
}
const commitNodes: GitCommitNode[] = [];
const commitLookup: { [hash: string]: number } = {};
for (let i = 0; i < commits.length; i++) {
commitLookup[commits[i].hash] = i;
commitNodes.push({
hash: commits[i].hash,
parentHashes: commits[i].parentHashes,
author: commits[i].author,
email: commits[i].email,
date: commits[i].date,
message: commits[i].message,
refs: []
});
}
for (const ref of refData.refs) {
if (typeof commitLookup[ref.hash] === "number") {
commitNodes[commitLookup[ref.hash]].refs.push(ref);
}
}
return {
commits: commitNodes,
head: refData.head,
moreCommitsAvailable,
hard
};
}