-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.js
213 lines (187 loc) · 4.85 KB
/
git.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import simpleGit from 'simple-git';
async function getRepoData() {
const git = simpleGit();
let isOffline = false;
const localData = await Promise.all([
git.branch(),
git.log(["--all"]),
git.tags(),
git.raw(["remote"]),
]).catch((err) => {
console.error("Failed to get local repo data:", err);
return [null, null, null, ""];
});
try {
await git.fetch(["--all"]);
isOffline = false;
} catch (error) {
console.warn("Working in offline mode - using local data only");
isOffline = true;
}
const [localBranches, log, tags, remotes] = localData;
if (!localBranches || !log) {
throw new Error("Failed to read local repository data");
}
const localBranchList = localBranches.all.filter(
(branch) => !branch.includes("remotes/")
);
let remoteBranchList = [];
if (!isOffline && remotes) {
try {
const remoteBranches = await git.raw([
"for-each-ref",
"refs/remotes",
"--format=%(refname:short)",
]);
remoteBranchList = remoteBranches
.split("\n")
.filter(
(branch) =>
branch &&
!branch.endsWith("/HEAD") &&
branch.split("/").length > 1 &&
!localBranchList.some((localBranch) =>
branch.endsWith("/" + localBranch)
)
)
.map((branch) => ({
name: branch.trim(),
fullName: "remotes/" + branch.trim(),
remote: branch.split("/")[0],
shortName: branch.split("/").slice(1).join("/"),
}));
} catch (error) {
console.error("Failed to get remote branches:", error);
}
}
const commits = log.all.map((commit) => ({
...commit,
fullRefs: commit.refs
.split(",")
.map((ref) => ref.trim())
.filter(Boolean),
refs: commit.refs
.split(",")
.map((ref) => ref.trim())
.filter((ref) => ref && !ref.includes("tag:")),
}));
return {
commits,
branches: localBranchList,
remoteBranches: remoteBranchList,
current: localBranches.current,
tags: tags?.all || [],
isOffline,
};
}
async function getBranchCommits(branchName) {
const git = simpleGit();
try {
const { current: currentBranch } = await git.branch();
let baseBranch = currentBranch;
if (currentBranch !== 'main' && currentBranch !== 'master') {
try {
await git.show(["main"]);
baseBranch = "main";
} catch {
try {
await git.show(["master"]);
baseBranch = "master";
} catch {
baseBranch = currentBranch;
}
}
}
let targetBranch = branchName;
if (branchName.startsWith("remotes/")) {
if (branchName === "remotes/origin" || !branchName.includes("/")) {
return [];
}
targetBranch = branchName.replace("remotes/", "refs/remotes/");
}
if (targetBranch === baseBranch) {
const log = await git.log(["--first-parent", targetBranch]);
return log.all;
}
const revList = await git.raw([
"rev-list",
"--first-parent",
targetBranch,
"^" + baseBranch,
"--not",
"--all",
]);
if (!revList.trim()) {
const log = await git.log(["--first-parent", targetBranch]);
return log.all;
}
const commits = [];
const commitHashes = revList.split("\n").filter((hash) => hash.trim());
for (const hash of commitHashes) {
if (!hash) continue;
const commitInfo = await git.show([
"--format=%H%n%an%n%ae%n%at%n%s%n%D",
"--no-patch",
hash,
]);
const [
commitHash,
authorName,
authorEmail,
timestamp,
subject,
refs = "",
] = commitInfo.split("\n");
commits.push({
hash: commitHash,
author_name: authorName,
author_email: authorEmail,
date: new Date(parseInt(timestamp) * 1000).toISOString(),
message: subject,
refs: refs,
});
}
return commits;
} catch (error) {
console.error("Failed to get branch commits:", error);
throw error;
}
}
async function compareCommits(commit1, commit2) {
const git = simpleGit();
try {
const diff = await git.raw([
"diff",
"--binary",
"-p",
"--no-color",
commit1,
commit2,
]);
return diff;
} catch (error) {
console.error("Failed to compare commits:", error);
throw new Error(`Failed to compare commits: ${error.message}`);
}
}
async function checkoutRemoteBranch(branchName) {
const git = simpleGit();
try {
await git.checkout([
"-b",
branchName.split("/").slice(2).join("/"),
"--track",
branchName,
]);
return true;
} catch (error) {
console.error("Failed to checkout branch:", error.message);
return false;
}
}
export {
getRepoData,
getBranchCommits,
compareCommits,
checkoutRemoteBranch,
};