zen-gitsync 2.0.6 → 2.0.8

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.
@@ -255,12 +255,20 @@ export const useGitLogStore = defineStore('gitLog', () => {
255
255
  try {
256
256
  isLoadingLog.value = true
257
257
  console.log('开始加载提交历史...')
258
- const response = await fetch('/api/log')
258
+
259
+ // 增加时间戳参数避免缓存,确保获取最新数据
260
+ const timestamp = new Date().getTime()
261
+ const response = await fetch(`/api/log?page=1&_t=${timestamp}`)
259
262
  const data = await response.json()
260
- if (data && Array.isArray(data)) {
261
- log.value = data
263
+
264
+ if (data && data.data && Array.isArray(data.data)) {
265
+ // 清空并更新日志数组
266
+ log.value = [...data.data]
267
+ console.log(`提交历史加载完成,共 ${log.value.length} 条记录`)
268
+ } else {
269
+ console.warn('API返回的提交历史格式不正确:', data)
270
+ log.value = [] // 如果数据格式不对,清空日志
262
271
  }
263
- console.log(`提交历史加载完成,共 ${log.value.length} 条记录`)
264
272
 
265
273
  if (showMessage) {
266
274
  ElMessage({
@@ -487,7 +495,7 @@ export const useGitLogStore = defineStore('gitLog', () => {
487
495
  return new Promise(resolve => setTimeout(resolve, ms))
488
496
  }
489
497
 
490
- // 提交更改
498
+ // 提交更改 (git commit)
491
499
  async function commitChanges(message: string, noVerify = false) {
492
500
  // 检查是否是Git仓库
493
501
  if (!gitStore.isGitRepo) {
@@ -520,6 +528,9 @@ export const useGitLogStore = defineStore('gitLog', () => {
520
528
  fetchStatus()
521
529
  fetchLog()
522
530
 
531
+ // 更新分支状态
532
+ gitStore.getBranchStatus()
533
+
523
534
  return true
524
535
  } else {
525
536
  ElMessage({
@@ -565,6 +576,9 @@ export const useGitLogStore = defineStore('gitLog', () => {
565
576
  // 刷新日志
566
577
  fetchLog()
567
578
 
579
+ // 更新分支状态
580
+ gitStore.getBranchStatus()
581
+
568
582
  return true
569
583
  } else {
570
584
  ElMessage({
@@ -632,6 +646,12 @@ export const useGitLogStore = defineStore('gitLog', () => {
632
646
  message: `操作失败: ${(error as Error).message}`,
633
647
  type: 'error'
634
648
  })
649
+
650
+ // 即使出错也要刷新状态
651
+ fetchStatus()
652
+ fetchLog()
653
+ gitStore.getBranchStatus()
654
+
635
655
  return false
636
656
  }
637
657
  }
@@ -13,6 +13,12 @@ export const useGitStore = defineStore('git', () => {
13
13
  const isGitRepo = ref(false) // 当前目录是否是Git仓库
14
14
  const lastCheckedTime = ref(0) // 上次检查Git仓库状态的时间戳
15
15
 
16
+ // 添加分支状态相关变量
17
+ const branchAhead = ref(0) // 当前分支领先远程分支的提交数
18
+ const branchBehind = ref(0) // 当前分支落后远程分支的提交数
19
+ const hasUpstream = ref(false) // 当前分支是否有上游分支
20
+ const upstreamBranch = ref('') // 上游分支名称
21
+
16
22
  // 添加重置方法
17
23
  function $reset() {
18
24
  currentBranch.value = ''
@@ -23,6 +29,37 @@ export const useGitStore = defineStore('git', () => {
23
29
  isCreatingBranch.value = false
24
30
  isGitRepo.value = false
25
31
  lastCheckedTime.value = 0
32
+ branchAhead.value = 0
33
+ branchBehind.value = 0
34
+ hasUpstream.value = false
35
+ upstreamBranch.value = ''
36
+ }
37
+
38
+ // 获取分支状态(领先/落后远程)
39
+ async function getBranchStatus() {
40
+ if (!isGitRepo.value) return;
41
+
42
+ try {
43
+ const response = await fetch('/api/branch-status');
44
+ const data = await response.json();
45
+
46
+ if (data) {
47
+ branchAhead.value = data.ahead || 0;
48
+ branchBehind.value = data.behind || 0;
49
+ hasUpstream.value = data.hasUpstream || false;
50
+ upstreamBranch.value = data.upstreamBranch || '';
51
+
52
+ // 添加调试日志
53
+ console.log(`分支状态更新:领先 ${branchAhead.value} 个提交,落后 ${branchBehind.value} 个提交,上游分支:${hasUpstream.value ? upstreamBranch.value : '无'}`);
54
+ }
55
+ } catch (error) {
56
+ console.error('获取分支状态失败:', error);
57
+ // 出错时重置状态
58
+ branchAhead.value = 0;
59
+ branchBehind.value = 0;
60
+ hasUpstream.value = false;
61
+ upstreamBranch.value = '';
62
+ }
26
63
  }
27
64
 
28
65
  // 检查当前目录是否是Git仓库
@@ -56,6 +93,8 @@ export const useGitStore = defineStore('git', () => {
56
93
  const data = await response.json()
57
94
  if (data.branch) {
58
95
  currentBranch.value = data.branch
96
+ // 获取分支状态信息
97
+ await getBranchStatus();
59
98
  }
60
99
  } catch (error) {
61
100
  console.error('获取分支信息失败:', error)
@@ -275,6 +314,88 @@ export const useGitStore = defineStore('git', () => {
275
314
  }
276
315
  }
277
316
 
317
+ // 执行git pull操作
318
+ async function gitPull() {
319
+ if (!isGitRepo.value) {
320
+ ElMessage({
321
+ message: '当前目录不是Git仓库',
322
+ type: 'warning'
323
+ });
324
+ return false;
325
+ }
326
+
327
+ try {
328
+ const response = await fetch('/api/pull', {
329
+ method: 'POST'
330
+ });
331
+
332
+ const result = await response.json();
333
+ if (result.success) {
334
+ ElMessage({
335
+ message: '拉取成功',
336
+ type: 'success'
337
+ });
338
+
339
+ // 刷新分支状态
340
+ await getBranchStatus();
341
+ return true;
342
+ } else {
343
+ ElMessage({
344
+ message: `拉取失败: ${result.error}`,
345
+ type: 'error'
346
+ });
347
+ return false;
348
+ }
349
+ } catch (error) {
350
+ ElMessage({
351
+ message: `拉取失败: ${(error as Error).message}`,
352
+ type: 'error'
353
+ });
354
+ return false;
355
+ }
356
+ }
357
+
358
+ // 执行git fetch --all操作
359
+ async function gitFetchAll() {
360
+ if (!isGitRepo.value) {
361
+ ElMessage({
362
+ message: '当前目录不是Git仓库',
363
+ type: 'warning'
364
+ });
365
+ return false;
366
+ }
367
+
368
+ try {
369
+ const response = await fetch('/api/fetch-all', {
370
+ method: 'POST'
371
+ });
372
+
373
+ const result = await response.json();
374
+ if (result.success) {
375
+ ElMessage({
376
+ message: '获取所有远程分支信息成功',
377
+ type: 'success'
378
+ });
379
+
380
+ // 刷新分支状态
381
+ await getBranchStatus();
382
+ return true;
383
+ } else {
384
+ ElMessage({
385
+ message: `获取远程分支信息失败: ${result.error}`,
386
+ type: 'error'
387
+ });
388
+ return false;
389
+ }
390
+ } catch (error) {
391
+ ElMessage({
392
+ message: `获取远程分支信息失败: ${(error as Error).message}`,
393
+ type: 'error'
394
+ });
395
+ return false;
396
+ }
397
+ }
398
+
278
399
  return {
279
400
  // 状态
280
401
  currentBranch,
@@ -285,6 +406,10 @@ export const useGitStore = defineStore('git', () => {
285
406
  isCreatingBranch,
286
407
  isGitRepo,
287
408
  lastCheckedTime,
409
+ branchAhead,
410
+ branchBehind,
411
+ hasUpstream,
412
+ upstreamBranch,
288
413
 
289
414
  // 方法
290
415
  $reset,
@@ -296,6 +421,9 @@ export const useGitStore = defineStore('git', () => {
296
421
  createBranch,
297
422
  loadInitialData,
298
423
  clearUserConfig,
299
- restoreUserConfig
424
+ restoreUserConfig,
425
+ getBranchStatus,
426
+ gitPull,
427
+ gitFetchAll
300
428
  }
301
429
  })