zen-gitsync 2.0.8 → 2.1.0

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.
@@ -447,13 +447,7 @@ function getFileDirectory(path: string): string {
447
447
 
448
448
  onMounted(() => {
449
449
  // App.vue已经加载了Git相关数据,此时只需加载状态
450
- // 如果已有初始目录,则只需加载状态
451
450
  loadStatus()
452
-
453
- // 如果是Git仓库,确保分支状态也被加载
454
- if (gitStore.isGitRepo) {
455
- gitStore.getBranchStatus()
456
- }
457
451
  })
458
452
 
459
453
  // 监听autoUpdateEnabled的变化,手动调用toggleAutoUpdate
@@ -721,7 +715,9 @@ defineExpose({
721
715
  <el-dialog
722
716
  v-model="isDirectoryDialogVisible"
723
717
  title="切换工作目录"
724
- width="500px"
718
+ width="80vw"
719
+ top="10vh"
720
+ style="height: 200px;"
725
721
  >
726
722
  <el-form>
727
723
  <el-form-item label="目录路径">
@@ -743,7 +739,8 @@ defineExpose({
743
739
  <el-dialog
744
740
  v-model="isDirectoryBrowserVisible"
745
741
  title="浏览目录"
746
- width="600px"
742
+ width="80vw"
743
+ top="70px"
747
744
  >
748
745
  <div class="browser-current-path">
749
746
  <span>当前路径: {{ currentBrowsePath }}</span>
@@ -395,8 +395,7 @@ onMounted(() => {
395
395
  // 加载所有可能的作者列表
396
396
  fetchAllAuthors()
397
397
 
398
- // 加载所有可能的分支列表
399
- fetchAllBranches()
398
+ // 不再在这里直接设置 availableBranches,改为通过 watch 监听 gitStore.allBranches 的变化
400
399
  } else {
401
400
  errorMessage.value = '当前目录不是Git仓库'
402
401
  }
@@ -409,6 +408,17 @@ onMounted(() => {
409
408
  })
410
409
  })
411
410
 
411
+ // 添加对 gitStore.allBranches 的监听
412
+ watch(() => gitStore.allBranches, (newBranches) => {
413
+ if (newBranches && newBranches.length > 0) {
414
+ availableBranches.value = [...newBranches].sort()
415
+ console.log(`分支数据更新,共 ${availableBranches.value.length} 个分支`)
416
+ } else {
417
+ availableBranches.value = []
418
+ console.warn('gitStore 中没有分支数据')
419
+ }
420
+ }, { immediate: true }) // immediate: true 确保组件创建时立即执行一次
421
+
412
422
  onBeforeUnmount(() => {
413
423
  // 清除表格滚动监听
414
424
  removeTableScrollListener()
@@ -725,16 +735,17 @@ function extractAuthorsFromLogs() {
725
735
  async function fetchAllBranches() {
726
736
  try {
727
737
  console.log('获取所有可用分支...')
728
- const response = await fetch('/api/branches')
729
- const result = await response.json()
730
738
 
731
- if (result.branches && Array.isArray(result.branches)) {
739
+ // 直接调用 gitStore 中的方法获取分支列表
740
+ await gitStore.getAllBranches()
741
+
742
+ // 从 gitStore 中获取分支列表
743
+ if (gitStore.allBranches.length > 0) {
732
744
  // 更新可用分支列表
733
- availableBranches.value = result.branches.sort()
745
+ availableBranches.value = [...gitStore.allBranches].sort()
734
746
  console.log(`获取到${availableBranches.value.length}个分支`)
735
747
  } else {
736
- console.warn('获取分支列表失败')
737
- extractBranchesFromLogs()
748
+ console.warn('gitStore中没有分支数据')
738
749
  }
739
750
  } catch (error) {
740
751
  console.error('获取分支列表失败:', error)
@@ -0,0 +1,212 @@
1
+ import { defineStore } from 'pinia'
2
+ import { ref } from 'vue'
3
+ import { ElMessage } from 'element-plus'
4
+
5
+ export const useConfigStore = defineStore('config', () => {
6
+ // 配置状态
7
+ const defaultCommitMessage = ref('')
8
+ const descriptionTemplates = ref<string[]>([])
9
+ const scopeTemplates = ref<string[]>([])
10
+ const messageTemplates = ref<string[]>([])
11
+ const isLoading = ref(false)
12
+ const isLoaded = ref(false)
13
+
14
+ // 加载配置
15
+ async function loadConfig() {
16
+ // 如果已经加载过,则不再重复加载
17
+ if (isLoaded.value && !isLoading.value) {
18
+ console.log('使用缓存的配置信息')
19
+ return {
20
+ defaultCommitMessage: defaultCommitMessage.value,
21
+ descriptionTemplates: descriptionTemplates.value,
22
+ scopeTemplates: scopeTemplates.value,
23
+ messageTemplates: messageTemplates.value
24
+ }
25
+ }
26
+
27
+ try {
28
+ isLoading.value = true
29
+ console.log('加载配置信息...')
30
+ const response = await fetch('/api/config/getConfig')
31
+ const config = await response.json()
32
+
33
+ // 更新状态
34
+ defaultCommitMessage.value = config.defaultCommitMessage || ''
35
+ descriptionTemplates.value = config.descriptionTemplates || []
36
+ scopeTemplates.value = config.scopeTemplates || []
37
+ messageTemplates.value = config.messageTemplates || []
38
+
39
+ // 标记为已加载
40
+ isLoaded.value = true
41
+
42
+ console.log('配置信息加载完成')
43
+ return config
44
+ } catch (error) {
45
+ console.error('加载配置失败:', error)
46
+ ElMessage.error(`加载配置失败: ${(error as Error).message}`)
47
+ return null
48
+ } finally {
49
+ isLoading.value = false
50
+ }
51
+ }
52
+
53
+ // 保存默认提交信息
54
+ async function saveDefaultMessage(message: string) {
55
+ try {
56
+ const response = await fetch('/api/config/saveDefaultMessage', {
57
+ method: 'POST',
58
+ headers: {
59
+ 'Content-Type': 'application/json'
60
+ },
61
+ body: JSON.stringify({ defaultCommitMessage: message })
62
+ })
63
+
64
+ const result = await response.json()
65
+ if (result.success) {
66
+ defaultCommitMessage.value = message
67
+ ElMessage.success('默认提交信息已保存')
68
+ return true
69
+ } else {
70
+ ElMessage.error(`保存失败: ${result.error}`)
71
+ return false
72
+ }
73
+ } catch (error) {
74
+ ElMessage.error(`保存失败: ${(error as Error).message}`)
75
+ return false
76
+ }
77
+ }
78
+
79
+ // 保存模板
80
+ async function saveTemplate(template: string, type: 'description' | 'scope' | 'message') {
81
+ try {
82
+ const response = await fetch('/api/config/save-template', {
83
+ method: 'POST',
84
+ headers: {
85
+ 'Content-Type': 'application/json'
86
+ },
87
+ body: JSON.stringify({ template, type })
88
+ })
89
+
90
+ const result = await response.json()
91
+ if (result.success) {
92
+ // 更新本地模板列表
93
+ if (type === 'description') {
94
+ if (!descriptionTemplates.value.includes(template)) {
95
+ descriptionTemplates.value.push(template)
96
+ }
97
+ } else if (type === 'scope') {
98
+ if (!scopeTemplates.value.includes(template)) {
99
+ scopeTemplates.value.push(template)
100
+ }
101
+ } else if (type === 'message') {
102
+ if (!messageTemplates.value.includes(template)) {
103
+ messageTemplates.value.push(template)
104
+ }
105
+ }
106
+
107
+ ElMessage.success('模板已保存')
108
+ return true
109
+ } else {
110
+ ElMessage.error(`保存模板失败: ${result.error}`)
111
+ return false
112
+ }
113
+ } catch (error) {
114
+ ElMessage.error(`保存模板失败: ${(error as Error).message}`)
115
+ return false
116
+ }
117
+ }
118
+
119
+ // 删除模板
120
+ async function deleteTemplate(template: string, type: 'description' | 'scope' | 'message') {
121
+ try {
122
+ const response = await fetch('/api/config/delete-template', {
123
+ method: 'POST',
124
+ headers: {
125
+ 'Content-Type': 'application/json'
126
+ },
127
+ body: JSON.stringify({ template, type })
128
+ })
129
+
130
+ const result = await response.json()
131
+ if (result.success) {
132
+ // 更新本地模板列表
133
+ if (type === 'description') {
134
+ descriptionTemplates.value = descriptionTemplates.value.filter(t => t !== template)
135
+ } else if (type === 'scope') {
136
+ scopeTemplates.value = scopeTemplates.value.filter(t => t !== template)
137
+ } else if (type === 'message') {
138
+ messageTemplates.value = messageTemplates.value.filter(t => t !== template)
139
+ }
140
+
141
+ ElMessage.success('模板已删除')
142
+ return true
143
+ } else {
144
+ ElMessage.error(`删除模板失败: ${result.error}`)
145
+ return false
146
+ }
147
+ } catch (error) {
148
+ ElMessage.error(`删除模板失败: ${(error as Error).message}`)
149
+ return false
150
+ }
151
+ }
152
+
153
+ // 更新模板
154
+ async function updateTemplate(oldTemplate: string, newTemplate: string, type: 'description' | 'scope' | 'message') {
155
+ try {
156
+ const response = await fetch('/api/config/update-template', {
157
+ method: 'POST',
158
+ headers: {
159
+ 'Content-Type': 'application/json'
160
+ },
161
+ body: JSON.stringify({ oldTemplate, newTemplate, type })
162
+ })
163
+
164
+ const result = await response.json()
165
+ if (result.success) {
166
+ // 更新本地模板列表
167
+ if (type === 'description') {
168
+ const index = descriptionTemplates.value.indexOf(oldTemplate)
169
+ if (index !== -1) {
170
+ descriptionTemplates.value[index] = newTemplate
171
+ }
172
+ } else if (type === 'scope') {
173
+ const index = scopeTemplates.value.indexOf(oldTemplate)
174
+ if (index !== -1) {
175
+ scopeTemplates.value[index] = newTemplate
176
+ }
177
+ } else if (type === 'message') {
178
+ const index = messageTemplates.value.indexOf(oldTemplate)
179
+ if (index !== -1) {
180
+ messageTemplates.value[index] = newTemplate
181
+ }
182
+ }
183
+
184
+ ElMessage.success('模板已更新')
185
+ return true
186
+ } else {
187
+ ElMessage.error(`更新模板失败: ${result.error}`)
188
+ return false
189
+ }
190
+ } catch (error) {
191
+ ElMessage.error(`更新模板失败: ${(error as Error).message}`)
192
+ return false
193
+ }
194
+ }
195
+
196
+ return {
197
+ // 状态
198
+ defaultCommitMessage,
199
+ descriptionTemplates,
200
+ scopeTemplates,
201
+ messageTemplates,
202
+ isLoading,
203
+ isLoaded,
204
+
205
+ // 方法
206
+ loadConfig,
207
+ saveDefaultMessage,
208
+ saveTemplate,
209
+ deleteTemplate,
210
+ updateTemplate
211
+ }
212
+ })
@@ -2,6 +2,10 @@ import { defineStore } from 'pinia'
2
2
  import { ref } from 'vue'
3
3
  import { ElMessage } from 'element-plus'
4
4
 
5
+ // 删除防抖变量
6
+ // let branchStatusDebounceTimer: ReturnType<typeof setTimeout> | null = null;
7
+ // const BRANCH_STATUS_DEBOUNCE_DELAY = 1000; // 1秒防抖延迟
8
+
5
9
  export const useGitStore = defineStore('git', () => {
6
10
  // 状态
7
11
  const currentBranch = ref('')
@@ -18,6 +22,10 @@ export const useGitStore = defineStore('git', () => {
18
22
  const branchBehind = ref(0) // 当前分支落后远程分支的提交数
19
23
  const hasUpstream = ref(false) // 当前分支是否有上游分支
20
24
  const upstreamBranch = ref('') // 上游分支名称
25
+ // 添加上次获取分支状态的时间戳
26
+ const lastBranchStatusTime = ref(0)
27
+ // 添加上次获取分支列表的时间戳
28
+ const lastBranchesTime = ref(0)
21
29
 
22
30
  // 添加重置方法
23
31
  function $reset() {
@@ -33,13 +41,17 @@ export const useGitStore = defineStore('git', () => {
33
41
  branchBehind.value = 0
34
42
  hasUpstream.value = false
35
43
  upstreamBranch.value = ''
44
+ lastBranchStatusTime.value = 0
45
+ lastBranchesTime.value = 0
36
46
  }
37
47
 
38
48
  // 获取分支状态(领先/落后远程)
39
- async function getBranchStatus() {
49
+ async function getBranchStatus(force = false) {
40
50
  if (!isGitRepo.value) return;
41
51
 
52
+ // 移除时间戳缓存判断,简化逻辑
42
53
  try {
54
+ console.log('获取分支状态...');
43
55
  const response = await fetch('/api/branch-status');
44
56
  const data = await response.json();
45
57
 
@@ -49,6 +61,9 @@ export const useGitStore = defineStore('git', () => {
49
61
  hasUpstream.value = data.hasUpstream || false;
50
62
  upstreamBranch.value = data.upstreamBranch || '';
51
63
 
64
+ // 更新获取时间戳
65
+ lastBranchStatusTime.value = Date.now();
66
+
52
67
  // 添加调试日志
53
68
  console.log(`分支状态更新:领先 ${branchAhead.value} 个提交,落后 ${branchBehind.value} 个提交,上游分支:${hasUpstream.value ? upstreamBranch.value : '无'}`);
54
69
  }
@@ -87,14 +102,13 @@ export const useGitStore = defineStore('git', () => {
87
102
  }
88
103
 
89
104
  // 获取当前分支
90
- async function getCurrentBranch() {
105
+ async function getCurrentBranch(skipBranchStatus = false) {
91
106
  try {
92
107
  const response = await fetch('/api/branch')
93
108
  const data = await response.json()
94
109
  if (data.branch) {
95
110
  currentBranch.value = data.branch
96
- // 获取分支状态信息
97
- await getBranchStatus();
111
+ // 不再在这里调用 getBranchStatus
98
112
  }
99
113
  } catch (error) {
100
114
  console.error('获取分支信息失败:', error)
@@ -102,12 +116,19 @@ export const useGitStore = defineStore('git', () => {
102
116
  }
103
117
 
104
118
  // 获取所有分支
105
- async function getAllBranches() {
119
+ async function getAllBranches(force = false) {
120
+ if (!isGitRepo.value) return;
121
+
122
+ // 移除时间戳缓存判断,简化逻辑
106
123
  try {
124
+ console.log('获取所有分支...');
107
125
  const response = await fetch('/api/branches')
108
126
  const data = await response.json()
109
127
  if (data.branches && Array.isArray(data.branches)) {
110
128
  allBranches.value = data.branches
129
+ // 更新获取时间戳
130
+ lastBranchesTime.value = Date.now();
131
+ console.log(`获取到${data.branches.length}个分支`);
111
132
  }
112
133
  } catch (error) {
113
134
  console.error('获取所有分支信息失败:', error)
@@ -135,8 +156,9 @@ export const useGitStore = defineStore('git', () => {
135
156
  type: 'success'
136
157
  })
137
158
 
138
- // 刷新状态
139
- getCurrentBranch()
159
+ // 分别刷新分支信息和分支状态
160
+ await getCurrentBranch()
161
+ await getBranchStatus()
140
162
 
141
163
  return true
142
164
  } else {
@@ -202,9 +224,10 @@ export const useGitStore = defineStore('git', () => {
202
224
  type: 'success'
203
225
  })
204
226
 
205
- // 刷新状态
206
- getCurrentBranch()
207
- getAllBranches()
227
+ // 分别刷新分支信息和状态
228
+ await getCurrentBranch()
229
+ await getBranchStatus()
230
+ await getAllBranches()
208
231
 
209
232
  return true
210
233
  } else {
@@ -225,25 +248,6 @@ export const useGitStore = defineStore('git', () => {
225
248
  }
226
249
  }
227
250
 
228
- // 初始化加载
229
- async function loadInitialData() {
230
- // 先检查当前目录是否是Git仓库
231
- const isRepo = await checkGitRepo()
232
-
233
- // 只有是Git仓库的情况下才加载Git相关信息
234
- if (isRepo) {
235
- getCurrentBranch()
236
- getAllBranches()
237
- getUserInfo()
238
- } else {
239
- // 清空所有Git相关状态
240
- currentBranch.value = ''
241
- allBranches.value = []
242
- userName.value = ''
243
- userEmail.value = ''
244
- }
245
- }
246
-
247
251
  // 清除Git用户配置
248
252
  async function clearUserConfig() {
249
253
  try {
@@ -340,10 +344,19 @@ export const useGitStore = defineStore('git', () => {
340
344
  await getBranchStatus();
341
345
  return true;
342
346
  } else {
343
- ElMessage({
344
- message: `拉取失败: ${result.error}`,
345
- type: 'error'
346
- });
347
+ // 改进错误提示
348
+ if (result.needsMerge) {
349
+ ElMessage({
350
+ message: `需要合并更改: ${result.pullOutput || '存在冲突需要手动解决'}`,
351
+ type: 'warning',
352
+ duration: 5000
353
+ });
354
+ } else {
355
+ ElMessage({
356
+ message: `拉取失败: ${result.error}`,
357
+ type: 'error'
358
+ });
359
+ }
347
360
  return false;
348
361
  }
349
362
  } catch (error) {
@@ -410,6 +423,8 @@ export const useGitStore = defineStore('git', () => {
410
423
  branchBehind,
411
424
  hasUpstream,
412
425
  upstreamBranch,
426
+ lastBranchStatusTime,
427
+ lastBranchesTime,
413
428
 
414
429
  // 方法
415
430
  $reset,
@@ -419,7 +434,6 @@ export const useGitStore = defineStore('git', () => {
419
434
  changeBranch,
420
435
  getUserInfo,
421
436
  createBranch,
422
- loadInitialData,
423
437
  clearUserConfig,
424
438
  restoreUserConfig,
425
439
  getBranchStatus,