@secrez/fs 1.0.5 → 1.0.7

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/.env ADDED
@@ -0,0 +1,9 @@
1
+ SECREZ_TEST_REPO_URL="git@github.com:secrez/test-repo.git"
2
+ SECREZ_TEST_SSH_KEY="-----BEGIN OPENSSH PRIVATE KEY-----
3
+ b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
4
+ QyNTUxOQAAACB8/5MMxGmWhwKsCKzu2bC2WhWdV6BiS/c2IVMJZ10NxQAAAKBOc1L6TnNS
5
+ +gAAAAtzc2gtZWQyNTUxOQAAACB8/5MMxGmWhwKsCKzu2bC2WhWdV6BiS/c2IVMJZ10NxQ
6
+ AAAEB8z+vtbfcUQyatLMOea+l7Ku4xbFbgB1Fyzymvo0T4Vnz/kwzEaZaHAqwIrO7ZsLZa
7
+ FZ1XoGJL9zYhUwlnXQ3FAAAAF3NlY3Jlei10ZXN0QGV4YW1wbGUuY29tAQIDBAUG
8
+ -----END OPENSSH PRIVATE KEY-----"
9
+ PUBLIC_TEST_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHz/kwzEaZaHAqwIrO7ZsLZaFZ1XoGJL9zYhUwlnXQ3F secrez-test@example.com"
package/README.md CHANGED
@@ -31,21 +31,22 @@ API documentation
31
31
  ## Test coverage
32
32
 
33
33
  ```
34
- 98 passing (3s)
35
-
36
- ---------------|---------|----------|---------|---------|-------------------------------------------
37
- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
38
- ---------------|---------|----------|---------|---------|-------------------------------------------
39
- All files | 82.9 | 68.72 | 86.61 | 82.56 |
40
- DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
41
- ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
42
- FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
43
- FsUtils.js | 100 | 100 | 100 | 100 |
44
- InternalFs.js | 86.99 | 72.63 | 88.46 | 86.88 | ...25,257,274,283,328,339,359,418-423,455
45
- Messages.js | 100 | 100 | 100 | 100 |
46
- Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...67,579-605,644-648,666,669,683-720,729
47
- Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...72,417-439,466,476,530,622,630,661-672
48
- ---------------|---------|----------|---------|---------|-------------------------------------------
34
+ 107 passing (44s)
35
+
36
+ -----------------------|---------|----------|---------|---------|-----------------------------------
37
+ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
38
+ -----------------------|---------|----------|---------|---------|-----------------------------------
39
+ All files | 81.78 | 67.77 | 86.27 | 81.44 |
40
+ DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
41
+ ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
42
+ FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
43
+ FsUtils.js | 100 | 100 | 100 | 100 |
44
+ GitConflictChecker.js | 72.81 | 61.9 | 90 | 72.81 | ...95,204,222,254,259,277,289,308
45
+ InternalFs.js | 85.09 | 71.87 | 85.18 | 84.98 | ...33,344,364,423-428,460,470-487
46
+ Messages.js | 100 | 100 | 100 | 100 |
47
+ Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
48
+ Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...39,466,476,530,622,630,661-672
49
+ -----------------------|---------|----------|---------|---------|-----------------------------------
49
50
  ```
50
51
 
51
52
  ## Copyright
package/coverage.report CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- > @secrez/fs@1.0.5 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
2
+ > @secrez/fs@1.0.7 test /Users/francescosullo/Projects/Secrez/secrez/packages/fs
3
3
  > cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha test/*.test.js test/**/*.test.js --exit
4
4
 
5
5
 
@@ -61,6 +61,18 @@
61
61
  ✓ should return an empty array if parameters are missed or empty
62
62
  ✓ should return all the list array if files are missed
63
63
 
64
+ #GitConflictChecker
65
+ ✓ should detect non-git repository
66
+ ✓ should return null status for non-git repository
67
+ ✓ should not detect conflict risk in non-git repository
68
+ with real GitHub repository
69
+ ✓ should detect git repository
70
+ ✓ should get git status when up to date with remote (3471ms)
71
+ ✓ should detect conflict risk when behind remote (5794ms)
72
+ ✓ should get warning message for conflict risk
73
+ ✓ should not show alert again after user proceeds (3582ms)
74
+ ✓ should reset cache
75
+
64
76
  #InternalFs
65
77
  #constructor
66
78
  ✓ should instantiate the internal file system and initialize it
@@ -145,29 +157,47 @@
145
157
  getEntryDetails
146
158
  ✓ should return the entry details of a node
147
159
  #Fix
148
- ✓ should load a tree with tags (48ms)
149
- ✓ should simulate a conflict in the repo and recover lost entries when a valid tree exists (1162ms)
150
- ✓ should simulate a lost index in the repo and recover the entries when no valid tree is found (70ms)
160
+ ✓ should load a tree with tags (52ms)
161
+ 1) should simulate a conflict in the repo and recover lost entries when a valid tree exists
162
+ ✓ should simulate a lost index in the repo and recover the entries when no valid tree is found (73ms)
151
163
  Multi data sets
152
164
  ✓ should load the trees of two data sets
153
165
 
154
166
 
155
- 98 passing (3s)
167
+ 106 passing (44s)
168
+ 1 failing
169
+
170
+ 1) #Tree
171
+ #Fix
172
+ should simulate a conflict in the repo and recover lost entries when a valid tree exists:
173
+
174
+ AssertionError: expected [ '/B/D/g', '/B/b', '/E/L/N', …(1) ] to deeply equal [ '/B/D/g', '/B/b', '/E/L/N', '/E/c' ]
175
+ + expected - actual
176
+
177
+ [
178
+ "/B/D/g"
179
+ "/B/b"
180
+ "/E/L/N"
181
+ - "/REC_20251009101127/d"
182
+ + "/E/c"
183
+ ]
184
+
185
+ at Context.<anonymous> (test/Tree.test.js:306:14)
156
186
 
157
- ---------------|---------|----------|---------|---------|-------------------------------------------
158
- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
159
- ---------------|---------|----------|---------|---------|-------------------------------------------
160
- All files | 82.9 | 68.72 | 86.61 | 82.56 |
161
- DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
162
- ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
163
- FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
164
- FsUtils.js | 100 | 100 | 100 | 100 |
165
- InternalFs.js | 86.99 | 72.63 | 88.46 | 86.88 | ...25,257,274,283,328,339,359,418-423,455
166
- Messages.js | 100 | 100 | 100 | 100 |
167
- Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...67,579-605,644-648,666,669,683-720,729
168
- Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...72,417-439,466,476,530,622,630,661-672
169
- ---------------|---------|----------|---------|---------|-------------------------------------------
170
187
 
171
- > @secrez/fs@1.0.5 posttest /Users/francescosullo/Projects/Secrez/secrez/packages/fs
172
- > nyc check-coverage --statements 80 --branches 65 --functions 80 --lines 80
173
188
 
189
+ -----------------------|---------|----------|---------|---------|-----------------------------------
190
+ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
191
+ -----------------------|---------|----------|---------|---------|-----------------------------------
192
+ All files | 81.85 | 67.77 | 86.27 | 81.52 |
193
+ DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
194
+ ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
195
+ FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
196
+ FsUtils.js | 100 | 100 | 100 | 100 |
197
+ GitConflictChecker.js | 72.81 | 61.9 | 90 | 72.81 | ...95,204,222,254,259,277,289,308
198
+ InternalFs.js | 85.09 | 71.87 | 85.18 | 84.98 | ...33,344,364,423-428,460,470-487
199
+ Messages.js | 100 | 100 | 100 | 100 |
200
+ Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
201
+ Tree.js | 90.19 | 74.09 | 86.48 | 89.91 | ...39,466,476,530,622,630,661-672
202
+ -----------------------|---------|----------|---------|---------|-----------------------------------
203
+  ELIFECYCLE  Test failed. See above for more details.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secrez/fs",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,6 +17,7 @@
17
17
  "@secrez/test-helpers": "~2.0.1",
18
18
  "chai": "^4.5.0",
19
19
  "cross-env": "^7.0.3",
20
+ "dotenv": "^17.2.2",
20
21
  "mocha": "^7.2.0",
21
22
  "nyc": "^15.1.0"
22
23
  },
@@ -0,0 +1,322 @@
1
+ const { execAsync } = require("@secrez/utils");
2
+ const path = require("path");
3
+ const fs = require("fs-extra");
4
+
5
+ class GitConflictChecker {
6
+ constructor(secrez) {
7
+ this.secrez = secrez;
8
+ this.isGitRepo = null;
9
+ this.remoteStatus = null;
10
+ this.initialGitState = null; // Initial state captured at account entry
11
+ this.lastCheck = null;
12
+ this.checkInterval = 30000; // 30 seconds between checks
13
+ }
14
+
15
+ /**
16
+ * Check if the current directory is a git repository
17
+ */
18
+ async isGitRepository() {
19
+ if (this.isGitRepo !== null) {
20
+ return this.isGitRepo;
21
+ }
22
+
23
+ try {
24
+ const containerPath = this.secrez.config.container;
25
+ const gitPath = path.join(containerPath, ".git");
26
+ this.isGitRepo = await fs.pathExists(gitPath);
27
+ return this.isGitRepo;
28
+ } catch (e) {
29
+ this.isGitRepo = false;
30
+ return false;
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Capture the initial git state when entering the account
36
+ */
37
+ async captureInitialState() {
38
+ const status = await this.getGitStatus();
39
+ if (status && !status.error) {
40
+ this.initialGitState = this.getGitFingerprint(status);
41
+ }
42
+ return status;
43
+ }
44
+
45
+ /**
46
+ * Get a fingerprint of the current git state for comparison
47
+ */
48
+ getGitFingerprint(status) {
49
+ if (!status || status.error) {
50
+ return null;
51
+ }
52
+
53
+ // Create a fingerprint based on the repository state
54
+ return {
55
+ localBranch: status.localBranch,
56
+ remoteBranch: status.remoteBranch,
57
+ headCommit: status.headCommit,
58
+ behind: status.behind,
59
+ ahead: status.ahead,
60
+ uncommitted: status.uncommitted,
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Check if the repository state has changed since initial capture
66
+ */
67
+ hasRepositoryChanged(currentStatus) {
68
+ if (!this.initialGitState || !currentStatus || currentStatus.error) {
69
+ return false;
70
+ }
71
+
72
+ const currentFingerprint = this.getGitFingerprint(currentStatus);
73
+ if (!currentFingerprint) {
74
+ return false;
75
+ }
76
+
77
+ // Check if any aspect of the repository has changed
78
+ // Most importantly, check if HEAD commit has changed (e.g., after pull)
79
+ return (
80
+ this.initialGitState.localBranch !== currentFingerprint.localBranch ||
81
+ this.initialGitState.remoteBranch !== currentFingerprint.remoteBranch ||
82
+ this.initialGitState.headCommit !== currentFingerprint.headCommit ||
83
+ this.initialGitState.behind !== currentFingerprint.behind ||
84
+ this.initialGitState.ahead !== currentFingerprint.ahead ||
85
+ this.initialGitState.uncommitted !== currentFingerprint.uncommitted
86
+ );
87
+ }
88
+
89
+ /**
90
+ * Get the current git status and remote information
91
+ */
92
+ async getGitStatus() {
93
+ if (!(await this.isGitRepository())) {
94
+ return null;
95
+ }
96
+
97
+ try {
98
+ const containerPath = this.secrez.config.container;
99
+
100
+ // Get current branch
101
+ const currentBranch = await execAsync("git", containerPath, [
102
+ "branch",
103
+ "--show-current",
104
+ ]);
105
+
106
+ if (!currentBranch || !currentBranch.message) {
107
+ return { error: "Could not determine current branch" };
108
+ }
109
+
110
+ // Get the HEAD commit hash for tracking changes
111
+ const headCommit = await execAsync("git", containerPath, [
112
+ "rev-parse",
113
+ "HEAD",
114
+ ]);
115
+
116
+ if (!headCommit || !headCommit.message) {
117
+ return { error: "Could not determine HEAD commit" };
118
+ }
119
+
120
+ // Fetch latest remote info
121
+ const fetchResult = await execAsync("git", containerPath, [
122
+ "fetch",
123
+ "--quiet",
124
+ ]);
125
+ if (fetchResult && fetchResult.error) {
126
+ return { error: `Network error during fetch: ${fetchResult.error}` };
127
+ }
128
+
129
+ // Get the HEAD branch from remote
130
+ const remoteInfo = await execAsync("git", containerPath, [
131
+ "remote",
132
+ "show",
133
+ "origin",
134
+ ]);
135
+ let remoteRef;
136
+
137
+ if (remoteInfo && remoteInfo.error) {
138
+ return { error: `Could not get remote info: ${remoteInfo.error}` };
139
+ }
140
+
141
+ if (remoteInfo && remoteInfo.message) {
142
+ const lines = remoteInfo.message.split("\n");
143
+ for (const line of lines) {
144
+ if (line.includes("HEAD branch:")) {
145
+ const branchName = line.split("HEAD branch:")[1].trim();
146
+ remoteRef = `origin/${branchName}`;
147
+ break;
148
+ }
149
+ }
150
+ }
151
+ // If still no remote ref found, return error
152
+ if (!remoteRef) {
153
+ return { error: "Could not determine remote primary branch" };
154
+ }
155
+
156
+ // Verify the remote reference exists
157
+ const remoteExists = await execAsync("git", containerPath, [
158
+ "rev-parse",
159
+ "--verify",
160
+ remoteRef,
161
+ ]);
162
+
163
+ if (remoteExists && remoteExists.error) {
164
+ return {
165
+ error: `Remote branch ${remoteRef} does not exist: ${remoteExists.error}`,
166
+ };
167
+ }
168
+ if (!remoteExists || !remoteExists.message) {
169
+ return { error: `Remote branch ${remoteRef} does not exist` };
170
+ }
171
+
172
+ // Check if local is behind remote
173
+ const behind = await execAsync("git", containerPath, [
174
+ "rev-list",
175
+ "--count",
176
+ `HEAD..${remoteRef}`,
177
+ ]);
178
+ if (behind && behind.error) {
179
+ return { error: `Could not check if behind remote: ${behind.error}` };
180
+ }
181
+ if (!behind || !behind.message) {
182
+ return { error: "Could not determine if local is behind remote" };
183
+ }
184
+
185
+ // Check if local is ahead of remote
186
+ const ahead = await execAsync("git", containerPath, [
187
+ "rev-list",
188
+ "--count",
189
+ `${remoteRef}..HEAD`,
190
+ ]);
191
+ if (ahead && ahead.error) {
192
+ return { error: `Could not check if ahead of remote: ${ahead.error}` };
193
+ }
194
+ if (!ahead || !ahead.message) {
195
+ return { error: "Could not determine if local is ahead of remote" };
196
+ }
197
+
198
+ // Check for uncommitted changes
199
+ const status = await execAsync("git", containerPath, [
200
+ "status",
201
+ "--porcelain",
202
+ ]);
203
+ if (status && status.error) {
204
+ return { error: `Could not check git status: ${status.error}` };
205
+ }
206
+
207
+ return {
208
+ localBranch: currentBranch.message.trim(),
209
+ remoteBranch: remoteRef,
210
+ headCommit: headCommit.message.trim(),
211
+ behind: parseInt(behind.message.trim()) || 0,
212
+ ahead: parseInt(ahead.message.trim()) || 0,
213
+ uncommitted: status.message
214
+ ? status.message.trim().length > 0
215
+ ? 1
216
+ : 0
217
+ : 0,
218
+ lastCheck: Date.now(),
219
+ };
220
+ } catch (e) {
221
+ // If git commands fail, return error instead of null
222
+ return { error: `Git command failed: ${e.message}` };
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Check if there are potential conflicts with remote changes
228
+ */
229
+ async checkForRemoteChanges() {
230
+ const now = Date.now();
231
+
232
+ // Skip check if we've checked recently
233
+ if (this.lastCheck && now - this.lastCheck < this.checkInterval) {
234
+ return this.remoteStatus;
235
+ }
236
+
237
+ this.remoteStatus = await this.getGitStatus();
238
+ this.lastCheck = now;
239
+
240
+ return this.remoteStatus;
241
+ }
242
+
243
+ /**
244
+ * Determine if there's a risk of conflicts
245
+ * Returns an object with risk level and details
246
+ */
247
+ hasConflictRisk(status) {
248
+ if (!status) {
249
+ return { hasRisk: false, type: null };
250
+ }
251
+
252
+ // Check if repository has changed externally (e.g., pull in another terminal)
253
+ if (this.hasRepositoryChanged(status)) {
254
+ return { hasRisk: true, type: "external_change", allowBypass: false };
255
+ }
256
+
257
+ // If there's an error, we should warn the user
258
+ if (status.error) {
259
+ return { hasRisk: true, type: "error", allowBypass: true };
260
+ }
261
+
262
+ // Risk if local is behind remote (remote has new commits that haven't been pulled)
263
+ // This means any local changes will conflict with remote changes
264
+ if (status.behind > 0) {
265
+ return { hasRisk: true, type: "behind_remote", allowBypass: true };
266
+ }
267
+
268
+ return { hasRisk: false, type: null };
269
+ }
270
+
271
+ /**
272
+ * Get a user-friendly warning message
273
+ */
274
+ getWarningMessage(status = {}, riskInfo = {}) {
275
+ // Check for external repository changes first (highest priority)
276
+ if (riskInfo.type === "external_change") {
277
+ return `🚫 Repository State Changed Externally!
278
+
279
+ The git repository state has changed since you entered your account.
280
+ This likely means a 'git pull' or other git operation was performed in another terminal.
281
+
282
+ Secrez cannot safely read these changes while running.
283
+ For data integrity, only READ operations and QUIT are allowed.
284
+
285
+ Please quit Secrez and re-enter your account to sync with the new repository state.`;
286
+ }
287
+
288
+ if (status.error) {
289
+ return `⚠️ Git Status Check Failed!
290
+
291
+ The system cannot check git status due to an error:
292
+ ${status.error}
293
+
294
+ This could be due to network issues, authentication problems, or repository configuration issues.
295
+ Any changes you make now might conflict with remote changes that we cannot detect.`;
296
+ }
297
+
298
+ if (status.behind > 0) {
299
+ return `⚠️ Git Conflict Risk Detected!
300
+
301
+ Your local repository is ${status.behind} commit(s) behind the remote '${status.remoteBranch}' branch.
302
+ Any changes you make now could lead to merge conflicts when you try to sync later.
303
+
304
+ Consider quitting Secrez and running in the container:
305
+ git pull # to sync with remote changes first`;
306
+ }
307
+
308
+ return null;
309
+ }
310
+
311
+ /**
312
+ * Reset the cache (useful for testing or when git status changes)
313
+ */
314
+ resetCache() {
315
+ this.isGitRepo = null;
316
+ this.remoteStatus = null;
317
+ this.initialGitState = null;
318
+ this.lastCheck = null;
319
+ }
320
+ }
321
+
322
+ module.exports = GitConflictChecker;
package/src/InternalFs.js CHANGED
@@ -5,6 +5,7 @@ const { config, Entry, ConfigUtils } = require("@secrez/core");
5
5
  const Node = require("./Node");
6
6
  const Tree = require("./Tree");
7
7
  const { ENTRY_EXISTS } = require("./Messages");
8
+ const GitConflictChecker = require("./GitConflictChecker");
8
9
 
9
10
  class InternalFs {
10
11
  constructor(secrez) {
@@ -14,6 +15,7 @@ class InternalFs {
14
15
  this.trees = [new Tree(secrez), new Tree(secrez, 1)];
15
16
  this.treeIndex = 0;
16
17
  this.tree = this.trees[0];
18
+ this.gitConflictChecker = new GitConflictChecker(secrez);
17
19
  } else {
18
20
  throw new Error(
19
21
  "InternalFs requires a Secrez instance during construction"
@@ -25,6 +27,9 @@ class InternalFs {
25
27
  // eslint-disable-next-line require-atomic-updates
26
28
  await this.tree.load();
27
29
  await this.trashToBeDeleted(0);
30
+
31
+ // Capture initial git state when entering the account
32
+ await this.gitConflictChecker.captureInitialState();
28
33
  }
29
34
 
30
35
  getFullPath(entry) {
@@ -458,6 +463,35 @@ class InternalFs {
458
463
  deleteFromTreeCache(index) {
459
464
  delete this.treeCache[index];
460
465
  }
466
+
467
+ async checkGitSyncStatus() {
468
+ // First, check for external repository changes WITHOUT using cache
469
+ // This is critical - we must always get fresh git status to detect external changes
470
+ const currentStatus = await this.gitConflictChecker.getGitStatus();
471
+ const riskInfo = this.gitConflictChecker.hasConflictRisk(currentStatus);
472
+
473
+ if (riskInfo.hasRisk) {
474
+ const warningMessage = this.gitConflictChecker.getWarningMessage(
475
+ currentStatus,
476
+ riskInfo
477
+ );
478
+ return {
479
+ hasRisk: true,
480
+ riskType: riskInfo.type,
481
+ allowBypass: riskInfo.allowBypass,
482
+ message: warningMessage,
483
+ status: currentStatus,
484
+ };
485
+ }
486
+
487
+ return {
488
+ hasRisk: false,
489
+ riskType: null,
490
+ allowBypass: true,
491
+ message: null,
492
+ status: currentStatus,
493
+ };
494
+ }
461
495
  }
462
496
 
463
497
  module.exports = InternalFs;
package/src/index.js CHANGED
@@ -6,5 +6,6 @@ module.exports = {
6
6
  Node: require("./Node"),
7
7
  DataCache: require("./DataCache"),
8
8
  FileCipher: require("./FileCipher"),
9
+ GitConflictChecker: require("./GitConflictChecker"),
9
10
  version: require("../package").version,
10
11
  };