zen-gitsync 2.0.1 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ui/client/package.json +1 -0
- package/src/ui/client/src/App.vue +174 -171
- package/src/ui/client/src/components/CommitForm.vue +591 -290
- package/src/ui/client/src/components/GitStatus.vue +528 -110
- package/src/ui/client/src/components/LogList.vue +351 -85
- package/src/ui/client/src/main.ts +3 -0
- package/src/ui/client/src/stores/gitLogStore.ts +464 -0
- package/src/ui/client/src/stores/gitStore.ts +216 -0
- package/src/ui/client/stats.html +1 -1
- package/src/ui/public/assets/index-D5irnfho.css +1 -0
- package/src/ui/public/assets/index-DBck3u67.js +8 -0
- package/src/ui/public/assets/vendor-CdJ34PvS.js +45 -0
- package/src/ui/public/index.html +3 -3
- package/src/ui/server/index.js +260 -4
- package/src/ui/public/assets/index-BcTk2R6G.js +0 -9
- package/src/ui/public/assets/index-ChUZ1vPG.css +0 -1
- package/src/ui/public/assets/vendor-BAXrrwNU.js +0 -41
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { defineStore } from 'pinia'
|
|
2
|
+
import { ref } from 'vue'
|
|
3
|
+
import { ElMessage } from 'element-plus'
|
|
4
|
+
import { useGitStore } from './gitStore'
|
|
5
|
+
|
|
6
|
+
export const useGitLogStore = defineStore('gitLog', () => {
|
|
7
|
+
// 引用gitStore获取仓库状态
|
|
8
|
+
const gitStore = useGitStore()
|
|
9
|
+
|
|
10
|
+
// 状态
|
|
11
|
+
const log = ref<any[]>([])
|
|
12
|
+
const status = ref<{ staged: string[], unstaged: string[], untracked: string[] }>({
|
|
13
|
+
staged: [],
|
|
14
|
+
unstaged: [],
|
|
15
|
+
untracked: []
|
|
16
|
+
})
|
|
17
|
+
const isLoadingLog = ref(false)
|
|
18
|
+
const isLoadingStatus = ref(false)
|
|
19
|
+
const isAddingFiles = ref(false)
|
|
20
|
+
const isResetting = ref(false)
|
|
21
|
+
|
|
22
|
+
// 获取提交日志
|
|
23
|
+
async function fetchLog() {
|
|
24
|
+
// 检查是否是Git仓库
|
|
25
|
+
if (!gitStore.isGitRepo) {
|
|
26
|
+
console.log('当前目录不是Git仓库,跳过加载提交历史')
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
isLoadingLog.value = true
|
|
32
|
+
console.log('开始加载提交历史...')
|
|
33
|
+
const response = await fetch('/api/log')
|
|
34
|
+
const data = await response.json()
|
|
35
|
+
if (data.log && Array.isArray(data.log)) {
|
|
36
|
+
log.value = data.log
|
|
37
|
+
}
|
|
38
|
+
console.log(`提交历史加载完成,共 ${log.value.length} 条记录`)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('获取提交历史失败:', error)
|
|
41
|
+
ElMessage({
|
|
42
|
+
message: `获取提交历史失败: ${(error as Error).message}`,
|
|
43
|
+
type: 'error'
|
|
44
|
+
})
|
|
45
|
+
} finally {
|
|
46
|
+
isLoadingLog.value = false
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 获取Git状态
|
|
51
|
+
async function fetchStatus() {
|
|
52
|
+
// 检查是否是Git仓库
|
|
53
|
+
if (!gitStore.isGitRepo) {
|
|
54
|
+
console.log('当前目录不是Git仓库,跳过加载Git状态')
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
isLoadingStatus.value = true
|
|
60
|
+
const response = await fetch('/api/status')
|
|
61
|
+
const data = await response.json()
|
|
62
|
+
if (data.status) {
|
|
63
|
+
status.value = {
|
|
64
|
+
staged: data.status.staged || [],
|
|
65
|
+
unstaged: data.status.unstaged || [],
|
|
66
|
+
untracked: data.status.untracked || []
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error('获取Git状态失败:', error)
|
|
71
|
+
ElMessage({
|
|
72
|
+
message: `获取Git状态失败: ${(error as Error).message}`,
|
|
73
|
+
type: 'error'
|
|
74
|
+
})
|
|
75
|
+
} finally {
|
|
76
|
+
isLoadingStatus.value = false
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 添加文件到暂存区 (git add .)
|
|
81
|
+
async function addToStage() {
|
|
82
|
+
// 检查是否是Git仓库
|
|
83
|
+
if (!gitStore.isGitRepo) {
|
|
84
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
85
|
+
return false
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
isAddingFiles.value = true
|
|
90
|
+
const response = await fetch('/api/add', {
|
|
91
|
+
method: 'POST'
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const result = await response.json()
|
|
95
|
+
if (result.success) {
|
|
96
|
+
ElMessage({
|
|
97
|
+
message: '文件已添加到暂存区',
|
|
98
|
+
type: 'success'
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
// 刷新状态
|
|
102
|
+
fetchStatus()
|
|
103
|
+
|
|
104
|
+
return true
|
|
105
|
+
} else {
|
|
106
|
+
ElMessage({
|
|
107
|
+
message: `添加文件失败: ${result.error}`,
|
|
108
|
+
type: 'error'
|
|
109
|
+
})
|
|
110
|
+
return false
|
|
111
|
+
}
|
|
112
|
+
} catch (error) {
|
|
113
|
+
ElMessage({
|
|
114
|
+
message: `添加文件失败: ${(error as Error).message}`,
|
|
115
|
+
type: 'error'
|
|
116
|
+
})
|
|
117
|
+
return false
|
|
118
|
+
} finally {
|
|
119
|
+
isAddingFiles.value = false
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 提交更改
|
|
124
|
+
async function commitChanges(message: string, noVerify = false) {
|
|
125
|
+
// 检查是否是Git仓库
|
|
126
|
+
if (!gitStore.isGitRepo) {
|
|
127
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
const response = await fetch('/api/commit', {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: {
|
|
135
|
+
'Content-Type': 'application/json'
|
|
136
|
+
},
|
|
137
|
+
body: JSON.stringify({
|
|
138
|
+
message,
|
|
139
|
+
hasNewlines: message.includes('\n'),
|
|
140
|
+
noVerify
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const result = await response.json()
|
|
145
|
+
if (result.success) {
|
|
146
|
+
ElMessage({
|
|
147
|
+
message: '提交成功',
|
|
148
|
+
type: 'success'
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
// 刷新状态和日志
|
|
152
|
+
fetchStatus()
|
|
153
|
+
fetchLog()
|
|
154
|
+
|
|
155
|
+
return true
|
|
156
|
+
} else {
|
|
157
|
+
ElMessage({
|
|
158
|
+
message: `提交失败: ${result.error}`,
|
|
159
|
+
type: 'error'
|
|
160
|
+
})
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
ElMessage({
|
|
165
|
+
message: `提交失败: ${(error as Error).message}`,
|
|
166
|
+
type: 'error'
|
|
167
|
+
})
|
|
168
|
+
return false
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 推送到远程
|
|
173
|
+
async function pushToRemote() {
|
|
174
|
+
// 检查是否是Git仓库
|
|
175
|
+
if (!gitStore.isGitRepo) {
|
|
176
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
177
|
+
return false
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
const response = await fetch('/api/push', {
|
|
182
|
+
method: 'POST'
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
const result = await response.json()
|
|
186
|
+
if (result.success) {
|
|
187
|
+
ElMessage({
|
|
188
|
+
message: '推送成功',
|
|
189
|
+
type: 'success'
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
// 刷新日志
|
|
193
|
+
fetchLog()
|
|
194
|
+
|
|
195
|
+
return true
|
|
196
|
+
} else {
|
|
197
|
+
ElMessage({
|
|
198
|
+
message: `推送失败: ${result.error}`,
|
|
199
|
+
type: 'error'
|
|
200
|
+
})
|
|
201
|
+
return false
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
ElMessage({
|
|
205
|
+
message: `推送失败: ${(error as Error).message}`,
|
|
206
|
+
type: 'error'
|
|
207
|
+
})
|
|
208
|
+
return false
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// 暂存并提交
|
|
213
|
+
async function addAndCommit(message: string, noVerify = false) {
|
|
214
|
+
try {
|
|
215
|
+
// 先添加到暂存区
|
|
216
|
+
const addResult = await addToStage()
|
|
217
|
+
if (!addResult) {
|
|
218
|
+
return false
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 再提交
|
|
222
|
+
return await commitChanges(message, noVerify)
|
|
223
|
+
} catch (error) {
|
|
224
|
+
ElMessage({
|
|
225
|
+
message: `暂存并提交失败: ${(error as Error).message}`,
|
|
226
|
+
type: 'error'
|
|
227
|
+
})
|
|
228
|
+
return false
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 暂存、提交并推送
|
|
233
|
+
async function addCommitAndPush(message: string, noVerify = false) {
|
|
234
|
+
try {
|
|
235
|
+
// 先添加并提交
|
|
236
|
+
const commitResult = await addAndCommit(message, noVerify)
|
|
237
|
+
if (!commitResult) {
|
|
238
|
+
return false
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 再推送
|
|
242
|
+
const pushResult = await pushToRemote()
|
|
243
|
+
|
|
244
|
+
// 推送成功后,确保刷新日志
|
|
245
|
+
if (pushResult) {
|
|
246
|
+
// 添加延迟以确保服务器处理完成
|
|
247
|
+
setTimeout(() => {
|
|
248
|
+
console.log('刷新提交历史...')
|
|
249
|
+
fetchLog()
|
|
250
|
+
}, 300)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return pushResult
|
|
254
|
+
} catch (error) {
|
|
255
|
+
ElMessage({
|
|
256
|
+
message: `暂存、提交并推送失败: ${(error as Error).message}`,
|
|
257
|
+
type: 'error'
|
|
258
|
+
})
|
|
259
|
+
return false
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// 重置暂存区 (git reset HEAD)
|
|
264
|
+
async function resetHead() {
|
|
265
|
+
// 检查是否是Git仓库
|
|
266
|
+
if (!gitStore.isGitRepo) {
|
|
267
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
268
|
+
return false
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
isResetting.value = true
|
|
273
|
+
const response = await fetch('/api/reset-head', {
|
|
274
|
+
method: 'POST'
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
const result = await response.json()
|
|
278
|
+
if (result.success) {
|
|
279
|
+
ElMessage({
|
|
280
|
+
message: '已重置暂存区',
|
|
281
|
+
type: 'success'
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
// 刷新状态
|
|
285
|
+
fetchStatus()
|
|
286
|
+
|
|
287
|
+
return true
|
|
288
|
+
} else {
|
|
289
|
+
ElMessage({
|
|
290
|
+
message: `重置暂存区失败: ${result.error}`,
|
|
291
|
+
type: 'error'
|
|
292
|
+
})
|
|
293
|
+
return false
|
|
294
|
+
}
|
|
295
|
+
} catch (error) {
|
|
296
|
+
ElMessage({
|
|
297
|
+
message: `重置暂存区失败: ${(error as Error).message}`,
|
|
298
|
+
type: 'error'
|
|
299
|
+
})
|
|
300
|
+
return false
|
|
301
|
+
} finally {
|
|
302
|
+
isResetting.value = false
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 重置当前分支到远程状态
|
|
307
|
+
async function resetToRemote(branch: string) {
|
|
308
|
+
// 检查是否是Git仓库
|
|
309
|
+
if (!gitStore.isGitRepo) {
|
|
310
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
311
|
+
return false
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
try {
|
|
315
|
+
isResetting.value = true
|
|
316
|
+
const response = await fetch('/api/reset-to-remote', {
|
|
317
|
+
method: 'POST',
|
|
318
|
+
headers: {
|
|
319
|
+
'Content-Type': 'application/json'
|
|
320
|
+
},
|
|
321
|
+
body: JSON.stringify({ branch })
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
const result = await response.json()
|
|
325
|
+
if (result.success) {
|
|
326
|
+
ElMessage({
|
|
327
|
+
message: `已重置分支 ${branch} 到远程状态`,
|
|
328
|
+
type: 'success'
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
// 刷新状态和日志
|
|
332
|
+
fetchStatus()
|
|
333
|
+
fetchLog()
|
|
334
|
+
|
|
335
|
+
return true
|
|
336
|
+
} else {
|
|
337
|
+
ElMessage({
|
|
338
|
+
message: `重置分支失败: ${result.error}`,
|
|
339
|
+
type: 'error'
|
|
340
|
+
})
|
|
341
|
+
return false
|
|
342
|
+
}
|
|
343
|
+
} catch (error) {
|
|
344
|
+
ElMessage({
|
|
345
|
+
message: `重置分支失败: ${(error as Error).message}`,
|
|
346
|
+
type: 'error'
|
|
347
|
+
})
|
|
348
|
+
return false
|
|
349
|
+
} finally {
|
|
350
|
+
isResetting.value = false
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// 暂存文件
|
|
355
|
+
async function stageFiles(files: string[]) {
|
|
356
|
+
// 检查是否是Git仓库
|
|
357
|
+
if (!gitStore.isGitRepo) {
|
|
358
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
359
|
+
return false
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
try {
|
|
363
|
+
const response = await fetch('/api/stage', {
|
|
364
|
+
method: 'POST',
|
|
365
|
+
headers: {
|
|
366
|
+
'Content-Type': 'application/json'
|
|
367
|
+
},
|
|
368
|
+
body: JSON.stringify({ files })
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
const result = await response.json()
|
|
372
|
+
if (result.success) {
|
|
373
|
+
ElMessage({
|
|
374
|
+
message: '暂存文件成功',
|
|
375
|
+
type: 'success'
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
// 刷新状态
|
|
379
|
+
fetchStatus()
|
|
380
|
+
|
|
381
|
+
return true
|
|
382
|
+
} else {
|
|
383
|
+
ElMessage({
|
|
384
|
+
message: `暂存文件失败: ${result.error}`,
|
|
385
|
+
type: 'error'
|
|
386
|
+
})
|
|
387
|
+
return false
|
|
388
|
+
}
|
|
389
|
+
} catch (error) {
|
|
390
|
+
ElMessage({
|
|
391
|
+
message: `暂存文件失败: ${(error as Error).message}`,
|
|
392
|
+
type: 'error'
|
|
393
|
+
})
|
|
394
|
+
return false
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// 取消暂存文件
|
|
399
|
+
async function unstageFiles(files: string[]) {
|
|
400
|
+
// 检查是否是Git仓库
|
|
401
|
+
if (!gitStore.isGitRepo) {
|
|
402
|
+
ElMessage.warning('当前目录不是Git仓库')
|
|
403
|
+
return false
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
try {
|
|
407
|
+
const response = await fetch('/api/unstage', {
|
|
408
|
+
method: 'POST',
|
|
409
|
+
headers: {
|
|
410
|
+
'Content-Type': 'application/json'
|
|
411
|
+
},
|
|
412
|
+
body: JSON.stringify({ files })
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
const result = await response.json()
|
|
416
|
+
if (result.success) {
|
|
417
|
+
ElMessage({
|
|
418
|
+
message: '取消暂存成功',
|
|
419
|
+
type: 'success'
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
// 刷新状态
|
|
423
|
+
fetchStatus()
|
|
424
|
+
|
|
425
|
+
return true
|
|
426
|
+
} else {
|
|
427
|
+
ElMessage({
|
|
428
|
+
message: `取消暂存失败: ${result.error}`,
|
|
429
|
+
type: 'error'
|
|
430
|
+
})
|
|
431
|
+
return false
|
|
432
|
+
}
|
|
433
|
+
} catch (error) {
|
|
434
|
+
ElMessage({
|
|
435
|
+
message: `取消暂存失败: ${(error as Error).message}`,
|
|
436
|
+
type: 'error'
|
|
437
|
+
})
|
|
438
|
+
return false
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return {
|
|
443
|
+
// 状态
|
|
444
|
+
log,
|
|
445
|
+
status,
|
|
446
|
+
isLoadingLog,
|
|
447
|
+
isLoadingStatus,
|
|
448
|
+
isAddingFiles,
|
|
449
|
+
isResetting,
|
|
450
|
+
|
|
451
|
+
// 方法
|
|
452
|
+
fetchLog,
|
|
453
|
+
fetchStatus,
|
|
454
|
+
addToStage,
|
|
455
|
+
commitChanges,
|
|
456
|
+
pushToRemote,
|
|
457
|
+
addAndCommit,
|
|
458
|
+
addCommitAndPush,
|
|
459
|
+
resetHead,
|
|
460
|
+
resetToRemote,
|
|
461
|
+
stageFiles,
|
|
462
|
+
unstageFiles
|
|
463
|
+
}
|
|
464
|
+
})
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { defineStore } from 'pinia'
|
|
2
|
+
import { ref } from 'vue'
|
|
3
|
+
import { ElMessage } from 'element-plus'
|
|
4
|
+
|
|
5
|
+
export const useGitStore = defineStore('git', () => {
|
|
6
|
+
// 状态
|
|
7
|
+
const currentBranch = ref('')
|
|
8
|
+
const allBranches = ref<string[]>([])
|
|
9
|
+
const userName = ref('')
|
|
10
|
+
const userEmail = ref('')
|
|
11
|
+
const isChangingBranch = ref(false)
|
|
12
|
+
const isCreatingBranch = ref(false)
|
|
13
|
+
const isGitRepo = ref(false) // 当前目录是否是Git仓库
|
|
14
|
+
const lastCheckedTime = ref(0) // 上次检查Git仓库状态的时间戳
|
|
15
|
+
|
|
16
|
+
// 检查当前目录是否是Git仓库
|
|
17
|
+
async function checkGitRepo() {
|
|
18
|
+
// 如果距离上次检查不到1秒,直接返回缓存的结果
|
|
19
|
+
const now = Date.now()
|
|
20
|
+
if (now - lastCheckedTime.value < 1000) {
|
|
21
|
+
console.log('使用缓存的Git仓库状态:', isGitRepo.value ? '是' : '不是')
|
|
22
|
+
return isGitRepo.value
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch('/api/current_directory')
|
|
27
|
+
const data = await response.json()
|
|
28
|
+
isGitRepo.value = data.isGitRepo === true
|
|
29
|
+
lastCheckedTime.value = now // 更新检查时间
|
|
30
|
+
console.log(`当前目录${isGitRepo.value ? '是' : '不是'}Git仓库`)
|
|
31
|
+
return isGitRepo.value
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error('检查Git仓库状态失败:', error)
|
|
34
|
+
isGitRepo.value = false
|
|
35
|
+
lastCheckedTime.value = now // 即使失败也更新检查时间,避免频繁重试
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 获取当前分支
|
|
41
|
+
async function getCurrentBranch() {
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch('/api/branch')
|
|
44
|
+
const data = await response.json()
|
|
45
|
+
if (data.branch) {
|
|
46
|
+
currentBranch.value = data.branch
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('获取分支信息失败:', error)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 获取所有分支
|
|
54
|
+
async function getAllBranches() {
|
|
55
|
+
try {
|
|
56
|
+
const response = await fetch('/api/branches')
|
|
57
|
+
const data = await response.json()
|
|
58
|
+
if (data.branches && Array.isArray(data.branches)) {
|
|
59
|
+
allBranches.value = data.branches
|
|
60
|
+
}
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error('获取所有分支信息失败:', error)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 切换分支
|
|
67
|
+
async function changeBranch(branch: string) {
|
|
68
|
+
console.log('切换到分支:', branch)
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
isChangingBranch.value = true
|
|
72
|
+
const response = await fetch('/api/checkout', {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: {
|
|
75
|
+
'Content-Type': 'application/json'
|
|
76
|
+
},
|
|
77
|
+
body: JSON.stringify({ branch })
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const result = await response.json()
|
|
81
|
+
if (result.success) {
|
|
82
|
+
ElMessage({
|
|
83
|
+
message: `已切换到分支: ${branch}`,
|
|
84
|
+
type: 'success'
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// 刷新状态
|
|
88
|
+
getCurrentBranch()
|
|
89
|
+
|
|
90
|
+
return true
|
|
91
|
+
} else {
|
|
92
|
+
ElMessage({
|
|
93
|
+
message: `切换分支失败: ${result.error}`,
|
|
94
|
+
type: 'error'
|
|
95
|
+
})
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
ElMessage({
|
|
100
|
+
message: `切换分支失败: ${(error as Error).message}`,
|
|
101
|
+
type: 'error'
|
|
102
|
+
})
|
|
103
|
+
return false
|
|
104
|
+
} finally {
|
|
105
|
+
isChangingBranch.value = false
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 获取Git用户信息
|
|
110
|
+
async function getUserInfo() {
|
|
111
|
+
try {
|
|
112
|
+
const response = await fetch('/api/user-info')
|
|
113
|
+
const data = await response.json()
|
|
114
|
+
if (data.name && data.email) {
|
|
115
|
+
userName.value = data.name
|
|
116
|
+
userEmail.value = data.email
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.error('获取用户信息失败:', error)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 创建新分支
|
|
124
|
+
async function createBranch(newBranchName: string, baseBranch: string) {
|
|
125
|
+
if (!newBranchName.trim()) {
|
|
126
|
+
ElMessage({
|
|
127
|
+
message: '分支名称不能为空',
|
|
128
|
+
type: 'warning'
|
|
129
|
+
})
|
|
130
|
+
return false
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
isCreatingBranch.value = true
|
|
135
|
+
|
|
136
|
+
const response = await fetch('/api/create-branch', {
|
|
137
|
+
method: 'POST',
|
|
138
|
+
headers: {
|
|
139
|
+
'Content-Type': 'application/json'
|
|
140
|
+
},
|
|
141
|
+
body: JSON.stringify({
|
|
142
|
+
newBranchName,
|
|
143
|
+
baseBranch: baseBranch || currentBranch.value
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
const result = await response.json()
|
|
148
|
+
if (result.success) {
|
|
149
|
+
ElMessage({
|
|
150
|
+
message: `已创建并切换到分支: ${newBranchName}`,
|
|
151
|
+
type: 'success'
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// 刷新状态
|
|
155
|
+
getCurrentBranch()
|
|
156
|
+
getAllBranches()
|
|
157
|
+
|
|
158
|
+
return true
|
|
159
|
+
} else {
|
|
160
|
+
ElMessage({
|
|
161
|
+
message: `创建分支失败: ${result.error}`,
|
|
162
|
+
type: 'error'
|
|
163
|
+
})
|
|
164
|
+
return false
|
|
165
|
+
}
|
|
166
|
+
} catch (error) {
|
|
167
|
+
ElMessage({
|
|
168
|
+
message: `创建分支失败: ${(error as Error).message}`,
|
|
169
|
+
type: 'error'
|
|
170
|
+
})
|
|
171
|
+
return false
|
|
172
|
+
} finally {
|
|
173
|
+
isCreatingBranch.value = false
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 初始化加载
|
|
178
|
+
async function loadInitialData() {
|
|
179
|
+
// 先检查当前目录是否是Git仓库
|
|
180
|
+
const isRepo = await checkGitRepo()
|
|
181
|
+
|
|
182
|
+
// 只有是Git仓库的情况下才加载Git相关信息
|
|
183
|
+
if (isRepo) {
|
|
184
|
+
getCurrentBranch()
|
|
185
|
+
getAllBranches()
|
|
186
|
+
getUserInfo()
|
|
187
|
+
} else {
|
|
188
|
+
// 清空所有Git相关状态
|
|
189
|
+
currentBranch.value = ''
|
|
190
|
+
allBranches.value = []
|
|
191
|
+
userName.value = ''
|
|
192
|
+
userEmail.value = ''
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
// 状态
|
|
198
|
+
currentBranch,
|
|
199
|
+
allBranches,
|
|
200
|
+
userName,
|
|
201
|
+
userEmail,
|
|
202
|
+
isChangingBranch,
|
|
203
|
+
isCreatingBranch,
|
|
204
|
+
isGitRepo,
|
|
205
|
+
lastCheckedTime,
|
|
206
|
+
|
|
207
|
+
// 方法
|
|
208
|
+
checkGitRepo,
|
|
209
|
+
getCurrentBranch,
|
|
210
|
+
getAllBranches,
|
|
211
|
+
changeBranch,
|
|
212
|
+
getUserInfo,
|
|
213
|
+
createBranch,
|
|
214
|
+
loadInitialData
|
|
215
|
+
}
|
|
216
|
+
})
|