zen-gitsync 2.0.3 → 2.0.5

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.
@@ -0,0 +1,301 @@
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
+ // 添加重置方法
17
+ function $reset() {
18
+ currentBranch.value = ''
19
+ allBranches.value = []
20
+ userName.value = ''
21
+ userEmail.value = ''
22
+ isChangingBranch.value = false
23
+ isCreatingBranch.value = false
24
+ isGitRepo.value = false
25
+ lastCheckedTime.value = 0
26
+ }
27
+
28
+ // 检查当前目录是否是Git仓库
29
+ async function checkGitRepo() {
30
+ // 如果距离上次检查不到1秒,直接返回缓存的结果
31
+ const now = Date.now()
32
+ if (now - lastCheckedTime.value < 1000) {
33
+ console.log('使用缓存的Git仓库状态:', isGitRepo.value ? '是' : '不是')
34
+ return isGitRepo.value
35
+ }
36
+
37
+ try {
38
+ const response = await fetch('/api/current_directory')
39
+ const data = await response.json()
40
+ isGitRepo.value = data.isGitRepo === true
41
+ lastCheckedTime.value = now // 更新检查时间
42
+ console.log(`当前目录${isGitRepo.value ? '是' : '不是'}Git仓库`)
43
+ return isGitRepo.value
44
+ } catch (error) {
45
+ console.error('检查Git仓库状态失败:', error)
46
+ isGitRepo.value = false
47
+ lastCheckedTime.value = now // 即使失败也更新检查时间,避免频繁重试
48
+ return false
49
+ }
50
+ }
51
+
52
+ // 获取当前分支
53
+ async function getCurrentBranch() {
54
+ try {
55
+ const response = await fetch('/api/branch')
56
+ const data = await response.json()
57
+ if (data.branch) {
58
+ currentBranch.value = data.branch
59
+ }
60
+ } catch (error) {
61
+ console.error('获取分支信息失败:', error)
62
+ }
63
+ }
64
+
65
+ // 获取所有分支
66
+ async function getAllBranches() {
67
+ try {
68
+ const response = await fetch('/api/branches')
69
+ const data = await response.json()
70
+ if (data.branches && Array.isArray(data.branches)) {
71
+ allBranches.value = data.branches
72
+ }
73
+ } catch (error) {
74
+ console.error('获取所有分支信息失败:', error)
75
+ }
76
+ }
77
+
78
+ // 切换分支
79
+ async function changeBranch(branch: string) {
80
+ console.log('切换到分支:', branch)
81
+
82
+ try {
83
+ isChangingBranch.value = true
84
+ const response = await fetch('/api/checkout', {
85
+ method: 'POST',
86
+ headers: {
87
+ 'Content-Type': 'application/json'
88
+ },
89
+ body: JSON.stringify({ branch })
90
+ })
91
+
92
+ const result = await response.json()
93
+ if (result.success) {
94
+ ElMessage({
95
+ message: `已切换到分支: ${branch}`,
96
+ type: 'success'
97
+ })
98
+
99
+ // 刷新状态
100
+ getCurrentBranch()
101
+
102
+ return true
103
+ } else {
104
+ ElMessage({
105
+ message: `切换分支失败: ${result.error}`,
106
+ type: 'error'
107
+ })
108
+ return false
109
+ }
110
+ } catch (error) {
111
+ ElMessage({
112
+ message: `切换分支失败: ${(error as Error).message}`,
113
+ type: 'error'
114
+ })
115
+ return false
116
+ } finally {
117
+ isChangingBranch.value = false
118
+ }
119
+ }
120
+
121
+ // 获取Git用户信息
122
+ async function getUserInfo() {
123
+ try {
124
+ const response = await fetch('/api/user-info')
125
+ const data = await response.json()
126
+ if (data.name && data.email) {
127
+ userName.value = data.name
128
+ userEmail.value = data.email
129
+ }
130
+ } catch (error) {
131
+ console.error('获取用户信息失败:', error)
132
+ }
133
+ }
134
+
135
+ // 创建新分支
136
+ async function createBranch(newBranchName: string, baseBranch: string) {
137
+ if (!newBranchName.trim()) {
138
+ ElMessage({
139
+ message: '分支名称不能为空',
140
+ type: 'warning'
141
+ })
142
+ return false
143
+ }
144
+
145
+ try {
146
+ isCreatingBranch.value = true
147
+
148
+ const response = await fetch('/api/create-branch', {
149
+ method: 'POST',
150
+ headers: {
151
+ 'Content-Type': 'application/json'
152
+ },
153
+ body: JSON.stringify({
154
+ newBranchName,
155
+ baseBranch: baseBranch || currentBranch.value
156
+ })
157
+ })
158
+
159
+ const result = await response.json()
160
+ if (result.success) {
161
+ ElMessage({
162
+ message: `已创建并切换到分支: ${newBranchName}`,
163
+ type: 'success'
164
+ })
165
+
166
+ // 刷新状态
167
+ getCurrentBranch()
168
+ getAllBranches()
169
+
170
+ return true
171
+ } else {
172
+ ElMessage({
173
+ message: `创建分支失败: ${result.error}`,
174
+ type: 'error'
175
+ })
176
+ return false
177
+ }
178
+ } catch (error) {
179
+ ElMessage({
180
+ message: `创建分支失败: ${(error as Error).message}`,
181
+ type: 'error'
182
+ })
183
+ return false
184
+ } finally {
185
+ isCreatingBranch.value = false
186
+ }
187
+ }
188
+
189
+ // 初始化加载
190
+ async function loadInitialData() {
191
+ // 先检查当前目录是否是Git仓库
192
+ const isRepo = await checkGitRepo()
193
+
194
+ // 只有是Git仓库的情况下才加载Git相关信息
195
+ if (isRepo) {
196
+ getCurrentBranch()
197
+ getAllBranches()
198
+ getUserInfo()
199
+ } else {
200
+ // 清空所有Git相关状态
201
+ currentBranch.value = ''
202
+ allBranches.value = []
203
+ userName.value = ''
204
+ userEmail.value = ''
205
+ }
206
+ }
207
+
208
+ // 清除Git用户配置
209
+ async function clearUserConfig() {
210
+ try {
211
+ const response = await fetch('/api/clear-user-config', {
212
+ method: 'POST'
213
+ });
214
+
215
+ const result = await response.json();
216
+ if (result.success) {
217
+ // 清空本地状态
218
+ userName.value = '';
219
+ userEmail.value = '';
220
+ ElMessage({
221
+ message: '已清除Git用户配置',
222
+ type: 'success'
223
+ });
224
+ return true;
225
+ } else {
226
+ ElMessage({
227
+ message: `清除配置失败: ${result.error}`,
228
+ type: 'error'
229
+ });
230
+ return false;
231
+ }
232
+ } catch (error) {
233
+ ElMessage({
234
+ message: `清除配置失败: ${(error as Error).message}`,
235
+ type: 'error'
236
+ });
237
+ return false;
238
+ }
239
+ }
240
+
241
+ // 恢复Git用户配置
242
+ async function restoreUserConfig(name: string, email: string) {
243
+ try {
244
+ const response = await fetch('/api/restore-user-config', {
245
+ method: 'POST',
246
+ headers: {
247
+ 'Content-Type': 'application/json'
248
+ },
249
+ body: JSON.stringify({ name, email })
250
+ });
251
+
252
+ const result = await response.json();
253
+ if (result.success) {
254
+ // 更新本地状态
255
+ userName.value = name;
256
+ userEmail.value = email;
257
+ ElMessage({
258
+ message: '已恢复Git用户配置',
259
+ type: 'success'
260
+ });
261
+ return true;
262
+ } else {
263
+ ElMessage({
264
+ message: `恢复配置失败: ${result.error}`,
265
+ type: 'error'
266
+ });
267
+ return false;
268
+ }
269
+ } catch (error) {
270
+ ElMessage({
271
+ message: `恢复配置失败: ${(error as Error).message}`,
272
+ type: 'error'
273
+ });
274
+ return false;
275
+ }
276
+ }
277
+
278
+ return {
279
+ // 状态
280
+ currentBranch,
281
+ allBranches,
282
+ userName,
283
+ userEmail,
284
+ isChangingBranch,
285
+ isCreatingBranch,
286
+ isGitRepo,
287
+ lastCheckedTime,
288
+
289
+ // 方法
290
+ $reset,
291
+ checkGitRepo,
292
+ getCurrentBranch,
293
+ getAllBranches,
294
+ changeBranch,
295
+ getUserInfo,
296
+ createBranch,
297
+ loadInitialData,
298
+ clearUserConfig,
299
+ restoreUserConfig
300
+ }
301
+ })