zen-gitsync 2.9.8 → 2.9.10

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,37 @@
1
+ export function createRequestLogger({ chalk }) {
2
+ return (req, res, next) => {
3
+ const startTime = Date.now();
4
+ const requestTime = new Date().toLocaleString('zh-CN', { hour12: false });
5
+
6
+ res.on('finish', () => {
7
+ const duration = Date.now() - startTime;
8
+ const statusCode = res.statusCode;
9
+
10
+ let statusColor = chalk.green;
11
+ if (statusCode >= 400 && statusCode < 500) {
12
+ statusColor = chalk.yellow;
13
+ } else if (statusCode >= 500) {
14
+ statusColor = chalk.red;
15
+ }
16
+
17
+ let durationColor = chalk.gray;
18
+ if (duration > 1000) {
19
+ durationColor = chalk.red;
20
+ } else if (duration > 500) {
21
+ durationColor = chalk.yellow;
22
+ } else if (duration > 200) {
23
+ durationColor = chalk.cyan;
24
+ }
25
+
26
+ console.log(
27
+ chalk.dim(`[${requestTime}]`),
28
+ chalk.bold(req.method),
29
+ req.url,
30
+ statusColor(`[${statusCode}]`),
31
+ durationColor(`${duration}ms`)
32
+ );
33
+ });
34
+
35
+ next();
36
+ };
37
+ }
@@ -0,0 +1,168 @@
1
+ export function registerBranchStatusRoutes({
2
+ app,
3
+ execGitCommand,
4
+ getIsGitRepo,
5
+ getCurrentBranchCache,
6
+ setCurrentBranchCache,
7
+ getUpstreamBranchCache,
8
+ setUpstreamBranchCache,
9
+ getBranchStatusCache,
10
+ setBranchStatusCache,
11
+ getRecentPushStatus,
12
+ setRecentPushStatus
13
+ }) {
14
+ async function getCurrentBranchOptimized(forceRefresh = false) {
15
+ const now = Date.now();
16
+ const currentBranchCache = getCurrentBranchCache();
17
+
18
+ if (!forceRefresh &&
19
+ currentBranchCache.branchName &&
20
+ (now - currentBranchCache.lastUpdate) < currentBranchCache.cacheTimeout) {
21
+ console.log(`使用缓存的分支名: ${currentBranchCache.branchName}`);
22
+ return currentBranchCache.branchName;
23
+ }
24
+
25
+ const { stdout } = await execGitCommand('git symbolic-ref --short HEAD');
26
+ const branchName = stdout.trim();
27
+
28
+ setCurrentBranchCache({
29
+ branchName,
30
+ lastUpdate: now,
31
+ cacheTimeout: 300000
32
+ });
33
+
34
+ return branchName;
35
+ }
36
+
37
+ async function getUpstreamBranchOptimized(forceRefresh = false) {
38
+ const now = Date.now();
39
+ const upstreamBranchCache = getUpstreamBranchCache();
40
+
41
+ if (!forceRefresh &&
42
+ upstreamBranchCache.upstreamBranch !== null &&
43
+ (now - upstreamBranchCache.lastUpdate) < upstreamBranchCache.cacheTimeout) {
44
+ console.log(`使用缓存的上游分支: ${upstreamBranchCache.upstreamBranch}`);
45
+ return upstreamBranchCache.upstreamBranch;
46
+ }
47
+
48
+ console.log('重新获取上游分支...');
49
+ const { stdout: upstreamOutput } = await execGitCommand(
50
+ 'git rev-parse --abbrev-ref --symbolic-full-name @{u}',
51
+ { ignoreError: true }
52
+ );
53
+ const upstreamBranch = upstreamOutput.trim() || null;
54
+
55
+ setUpstreamBranchCache({
56
+ upstreamBranch,
57
+ lastUpdate: now,
58
+ cacheTimeout: 300000
59
+ });
60
+
61
+ return upstreamBranch;
62
+ }
63
+
64
+ // 获取当前分支 - 使用缓存优化
65
+ app.get('/api/branch', async (req, res) => {
66
+ try {
67
+ const forceRefresh = req.query.force === 'true';
68
+ const branch = await getCurrentBranchOptimized(forceRefresh);
69
+ res.json({ branch });
70
+ } catch (error) {
71
+ res.status(500).json({ error: error.message });
72
+ }
73
+ });
74
+
75
+ // 获取分支与远程的差异状态(领先/落后提交数)- 优化版本
76
+ app.get('/api/branch-status', async (req, res) => {
77
+ try {
78
+ if (!getIsGitRepo()) {
79
+ return res.json({ hasUpstream: false, ahead: 0, behind: 0 });
80
+ }
81
+
82
+ const now = Date.now();
83
+ const forceRefresh = req.query.force === 'true';
84
+ const refreshCountOnly = req.query.countOnly === 'true';
85
+
86
+ const recentPushStatus = getRecentPushStatus();
87
+ const branchStatusCache = getBranchStatusCache();
88
+
89
+ if (recentPushStatus.justPushed &&
90
+ (now - recentPushStatus.pushTime) < recentPushStatus.validDuration) {
91
+ console.log('检测到最近推送过,直接返回同步状态');
92
+ return res.json({
93
+ hasUpstream: true,
94
+ upstreamBranch: branchStatusCache.upstreamBranch || 'origin/main',
95
+ ahead: 0,
96
+ behind: 0
97
+ });
98
+ }
99
+
100
+ const branchInfoCacheValid = branchStatusCache.currentBranch &&
101
+ branchStatusCache.upstreamBranch &&
102
+ (now - branchStatusCache.lastUpdate) < branchStatusCache.cacheTimeout;
103
+
104
+ if ((refreshCountOnly && branchInfoCacheValid) || (!forceRefresh && branchInfoCacheValid)) {
105
+ const { stdout: aheadBehindOutput } = await execGitCommand(
106
+ `git rev-list --left-right --count ${branchStatusCache.currentBranch}...${branchStatusCache.upstreamBranch}`
107
+ );
108
+ const [ahead, behind] = aheadBehindOutput.trim().split('\t').map(Number);
109
+
110
+ console.log(`使用缓存的分支信息: ${branchStatusCache.currentBranch} -> ${branchStatusCache.upstreamBranch} (${refreshCountOnly ? '只刷新计数' : '缓存有效'})`);
111
+
112
+ return res.json({
113
+ hasUpstream: true,
114
+ upstreamBranch: branchStatusCache.upstreamBranch,
115
+ ahead,
116
+ behind
117
+ });
118
+ }
119
+
120
+ const currentBranchCache = getCurrentBranchCache();
121
+ const upstreamBranchCache = getUpstreamBranchCache();
122
+
123
+ const shouldForceRefreshBranch = forceRefresh &&
124
+ (!currentBranchCache.branchName ||
125
+ (Date.now() - currentBranchCache.lastUpdate) >= currentBranchCache.cacheTimeout);
126
+
127
+ const currentBranch = await getCurrentBranchOptimized(shouldForceRefreshBranch);
128
+
129
+ const shouldForceRefreshUpstream = forceRefresh &&
130
+ (upstreamBranchCache.upstreamBranch === null ||
131
+ (Date.now() - upstreamBranchCache.lastUpdate) >= upstreamBranchCache.cacheTimeout);
132
+
133
+ const upstreamBranch = await getUpstreamBranchOptimized(shouldForceRefreshUpstream);
134
+
135
+ if (!upstreamBranch) {
136
+ setBranchStatusCache({
137
+ currentBranch: null,
138
+ upstreamBranch: null,
139
+ lastUpdate: 0,
140
+ cacheTimeout: 5000
141
+ });
142
+ return res.json({ hasUpstream: false, ahead: 0, behind: 0 });
143
+ }
144
+
145
+ setBranchStatusCache({
146
+ currentBranch,
147
+ upstreamBranch,
148
+ lastUpdate: now,
149
+ cacheTimeout: 5000
150
+ });
151
+
152
+ const { stdout: aheadBehindOutput } = await execGitCommand(
153
+ `git rev-list --left-right --count ${currentBranch}...${upstreamBranch}`
154
+ );
155
+ const [ahead, behind] = aheadBehindOutput.trim().split('\t').map(Number);
156
+
157
+ res.json({
158
+ hasUpstream: true,
159
+ upstreamBranch,
160
+ ahead,
161
+ behind
162
+ });
163
+ } catch (error) {
164
+ console.error('获取分支状态失败:', error);
165
+ res.status(500).json({ error: error.message });
166
+ }
167
+ });
168
+ }