@secrez/fs 1.0.4 → 1.0.6

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,23 +31,22 @@ API documentation
31
31
  ## Test coverage
32
32
 
33
33
  ```
34
- 97 passing (3s)
35
- 1 pending
36
-
37
- ---------------|---------|----------|---------|---------|-------------------------------------------
38
- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
39
- ---------------|---------|----------|---------|---------|-------------------------------------------
40
- All files | 80.92 | 68.1 | 85.21 | 80.54 |
41
- DataCache.js | 89.41 | 74.07 | 90.91 | 89.29 | 17-23,122-123,149
42
- ExternalFs.js | 94.03 | 76.92 | 100 | 93.94 | 9,29,35,54
43
- FileCipher.js | 88.64 | 73.33 | 100 | 88.64 | 8,52,80,85,94
44
- FsUtils.js | 100 | 100 | 100 | 100 |
45
- InternalFs.js | 86.99 | 72.63 | 88.46 | 86.89 | ...25,257,274,283,328,339,359,418-423,455
46
- Messages.js | 100 | 100 | 100 | 100 |
47
- Node.js | 65.82 | 55.19 | 78.72 | 65.03 | ...67,579-605,644-648,666,669,683-720,729
48
- Tree.js | 83.65 | 70.48 | 81.08 | 83.19 | ...72,417-439,466,476,530,622,630,661-672
49
- ---------------|---------|----------|---------|---------|-------------------------------------------
50
-
34
+ 107 passing (39s)
35
+
36
+ -----------------------|---------|----------|---------|---------|-----------------------------------
37
+ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
38
+ -----------------------|---------|----------|---------|---------|-----------------------------------
39
+ All files | 82.26 | 68.89 | 86.66 | 81.93 |
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 | 77.21 | 72.6 | 100 | 77.21 | ...16,126,129,138,155,186,199,218
45
+ InternalFs.js | 85.37 | 71.87 | 85.18 | 85.25 | ...30,341,361,420-425,457,465-476
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
+ -----------------------|---------|----------|---------|---------|-----------------------------------
51
50
  ```
52
51
 
53
52
  ## Copyright
package/coverage.report CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- > @secrez/fs@1.0.4 test /Users/francescosullo/Projects/Personal/secrez/packages/fs
2
+ > @secrez/fs@1.0.6 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 (3420ms)
71
+ ✓ should detect conflict risk when behind remote (5263ms)
72
+ ✓ should get warning message for conflict risk
73
+ ✓ should not show alert again after user proceeds (3456ms)
74
+ ✓ should reset cache
75
+
64
76
  #InternalFs
65
77
  #constructor
66
78
  ✓ should instantiate the internal file system and initialize it
@@ -146,25 +158,29 @@
146
158
  ✓ should return the entry details of a node
147
159
  #Fix
148
160
  ✓ should load a tree with tags
149
- ✓ should simulate a conflict in the repo and recover lost entries (1844ms)
150
- - should simulate a lost index in the repo and recover the entries
161
+ ✓ should simulate a conflict in the repo and recover lost entries when a valid tree exists (1112ms)
162
+ ✓ should simulate a lost index in the repo and recover the entries when no valid tree is found (44ms)
151
163
  Multi data sets
152
164
  ✓ should load the trees of two data sets
153
165
 
154
166
 
155
- 97 passing (3s)
156
- 1 pending
157
-
158
- ---------------|---------|----------|---------|---------|-------------------------------------------
159
- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
160
- ---------------|---------|----------|---------|---------|-------------------------------------------
161
- All files | 80.92 | 68.1 | 85.21 | 80.54 |
162
- DataCache.js | 89.41 | 74.07 | 90.91 | 89.29 | 17-23,122-123,149
163
- ExternalFs.js | 94.03 | 76.92 | 100 | 93.94 | 9,29,35,54
164
- FileCipher.js | 88.64 | 73.33 | 100 | 88.64 | 8,52,80,85,94
165
- FsUtils.js | 100 | 100 | 100 | 100 |
166
- InternalFs.js | 86.99 | 72.63 | 88.46 | 86.89 | ...25,257,274,283,328,339,359,418-423,455
167
- Messages.js | 100 | 100 | 100 | 100 |
168
- Node.js | 65.82 | 55.19 | 78.72 | 65.03 | ...67,579-605,644-648,666,669,683-720,729
169
- Tree.js | 83.65 | 70.48 | 81.08 | 83.19 | ...72,417-439,466,476,530,622,630,661-672
170
- ---------------|---------|----------|---------|---------|-------------------------------------------
167
+ 107 passing (39s)
168
+
169
+ -----------------------|---------|----------|---------|---------|-----------------------------------
170
+ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
171
+ -----------------------|---------|----------|---------|---------|-----------------------------------
172
+ All files | 82.26 | 68.89 | 86.66 | 81.93 |
173
+ DataCache.js | 89.41 | 74.07 | 90.9 | 89.28 | 17-23,122-123,149
174
+ ExternalFs.js | 94.02 | 76.92 | 100 | 93.93 | 9,29,35,54
175
+ FileCipher.js | 88.63 | 73.33 | 100 | 88.63 | 8,52,80,85,94
176
+ FsUtils.js | 100 | 100 | 100 | 100 |
177
+ GitConflictChecker.js | 77.21 | 72.6 | 100 | 77.21 | ...16,126,129,138,155,186,199,218
178
+ InternalFs.js | 85.37 | 71.87 | 85.18 | 85.25 | ...30,341,361,420-425,457,465-476
179
+ Messages.js | 100 | 100 | 100 | 100 |
180
+ Node.js | 66.1 | 54.81 | 78.72 | 65.31 | ...05,644-648,666,669,683-720,729
181
+ Tree.js | 89.91 | 74.09 | 86.48 | 89.63 | ...39,466,476,530,622,630,661-672
182
+ -----------------------|---------|----------|---------|---------|-----------------------------------
183
+
184
+ > @secrez/fs@1.0.6 posttest /Users/francescosullo/Projects/Secrez/secrez/packages/fs
185
+ > nyc check-coverage --statements 80 --branches 65 --functions 80 --lines 80
186
+
package/package.json CHANGED
@@ -1,23 +1,24 @@
1
1
  {
2
2
  "name": "@secrez/fs",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
8
  "dependencies": {
9
- "@secrez/core": "~1.0.4",
10
- "@secrez/crypto": "~1.0.3",
11
- "@secrez/utils": "~1.0.3",
12
- "command-line-args": "^5.1.1",
9
+ "@secrez/core": "~1.0.5",
10
+ "@secrez/crypto": "~1.0.4",
11
+ "@secrez/utils": "~1.0.4",
12
+ "command-line-args": "^5.2.1",
13
13
  "fs-extra": "^9.1.0",
14
- "lodash": "^4.17.15"
14
+ "lodash": "^4.17.21"
15
15
  },
16
16
  "devDependencies": {
17
- "@secrez/test-helpers": "~2.0.0",
18
- "chai": "^4.2.0",
19
- "cross-env": "^7.0.2",
20
- "mocha": "^7.1.1",
17
+ "@secrez/test-helpers": "~2.0.1",
18
+ "chai": "^4.5.0",
19
+ "cross-env": "^7.0.3",
20
+ "dotenv": "^17.2.2",
21
+ "mocha": "^7.2.0",
21
22
  "nyc": "^15.1.0"
22
23
  },
23
24
  "description": "The Secrez fs library.",
@@ -0,0 +1,231 @@
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.lastCheck = null;
11
+ this.checkInterval = 30000; // 30 seconds between checks
12
+ }
13
+
14
+ /**
15
+ * Check if the current directory is a git repository
16
+ */
17
+ async isGitRepository() {
18
+ if (this.isGitRepo !== null) {
19
+ return this.isGitRepo;
20
+ }
21
+
22
+ try {
23
+ const containerPath = this.secrez.config.container;
24
+ const gitPath = path.join(containerPath, ".git");
25
+ this.isGitRepo = await fs.pathExists(gitPath);
26
+ return this.isGitRepo;
27
+ } catch (e) {
28
+ this.isGitRepo = false;
29
+ return false;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Get the current git status and remote information
35
+ */
36
+ async getGitStatus() {
37
+ if (!(await this.isGitRepository())) {
38
+ return null;
39
+ }
40
+
41
+ try {
42
+ const containerPath = this.secrez.config.container;
43
+
44
+ // Get current branch
45
+ const currentBranch = await execAsync("git", containerPath, [
46
+ "branch",
47
+ "--show-current",
48
+ ]);
49
+
50
+ if (!currentBranch || !currentBranch.message) {
51
+ return { error: "Could not determine current branch" };
52
+ }
53
+
54
+ // Fetch latest remote info
55
+ const fetchResult = await execAsync("git", containerPath, [
56
+ "fetch",
57
+ "--quiet",
58
+ ]);
59
+ if (fetchResult && fetchResult.error) {
60
+ return { error: `Network error during fetch: ${fetchResult.error}` };
61
+ }
62
+
63
+ // Get the HEAD branch from remote
64
+ const remoteInfo = await execAsync("git", containerPath, [
65
+ "remote",
66
+ "show",
67
+ "origin",
68
+ ]);
69
+ let remoteRef;
70
+
71
+ if (remoteInfo && remoteInfo.error) {
72
+ return { error: `Could not get remote info: ${remoteInfo.error}` };
73
+ }
74
+
75
+ if (remoteInfo && remoteInfo.message) {
76
+ const lines = remoteInfo.message.split("\n");
77
+ for (const line of lines) {
78
+ if (line.includes("HEAD branch:")) {
79
+ const branchName = line.split("HEAD branch:")[1].trim();
80
+ remoteRef = `origin/${branchName}`;
81
+ break;
82
+ }
83
+ }
84
+ }
85
+ // If still no remote ref found, return error
86
+ if (!remoteRef) {
87
+ return { error: "Could not determine remote primary branch" };
88
+ }
89
+
90
+ // Verify the remote reference exists
91
+ const remoteExists = await execAsync("git", containerPath, [
92
+ "rev-parse",
93
+ "--verify",
94
+ remoteRef,
95
+ ]);
96
+
97
+ if (remoteExists && remoteExists.error) {
98
+ return {
99
+ error: `Remote branch ${remoteRef} does not exist: ${remoteExists.error}`,
100
+ };
101
+ }
102
+ if (!remoteExists || !remoteExists.message) {
103
+ return { error: `Remote branch ${remoteRef} does not exist` };
104
+ }
105
+
106
+ // Check if local is behind remote
107
+ const behind = await execAsync("git", containerPath, [
108
+ "rev-list",
109
+ "--count",
110
+ `HEAD..${remoteRef}`,
111
+ ]);
112
+ if (behind && behind.error) {
113
+ return { error: `Could not check if behind remote: ${behind.error}` };
114
+ }
115
+ if (!behind || !behind.message) {
116
+ return { error: "Could not determine if local is behind remote" };
117
+ }
118
+
119
+ // Check if local is ahead of remote
120
+ const ahead = await execAsync("git", containerPath, [
121
+ "rev-list",
122
+ "--count",
123
+ `${remoteRef}..HEAD`,
124
+ ]);
125
+ if (ahead && ahead.error) {
126
+ return { error: `Could not check if ahead of remote: ${ahead.error}` };
127
+ }
128
+ if (!ahead || !ahead.message) {
129
+ return { error: "Could not determine if local is ahead of remote" };
130
+ }
131
+
132
+ // Check for uncommitted changes
133
+ const status = await execAsync("git", containerPath, [
134
+ "status",
135
+ "--porcelain",
136
+ ]);
137
+ if (status && status.error) {
138
+ return { error: `Could not check git status: ${status.error}` };
139
+ }
140
+
141
+ return {
142
+ localBranch: currentBranch.message.trim(),
143
+ remoteBranch: remoteRef,
144
+ behind: parseInt(behind.message.trim()) || 0,
145
+ ahead: parseInt(ahead.message.trim()) || 0,
146
+ uncommitted: status.message
147
+ ? status.message.trim().length > 0
148
+ ? 1
149
+ : 0
150
+ : 0,
151
+ lastCheck: Date.now(),
152
+ };
153
+ } catch (e) {
154
+ // If git commands fail, return error instead of null
155
+ return { error: `Git command failed: ${e.message}` };
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Check if there are potential conflicts with remote changes
161
+ */
162
+ async checkForRemoteChanges() {
163
+ const now = Date.now();
164
+
165
+ // Skip check if we've checked recently
166
+ if (this.lastCheck && now - this.lastCheck < this.checkInterval) {
167
+ return this.remoteStatus;
168
+ }
169
+
170
+ this.remoteStatus = await this.getGitStatus();
171
+ this.lastCheck = now;
172
+
173
+ return this.remoteStatus;
174
+ }
175
+
176
+ /**
177
+ * Determine if there's a risk of conflicts
178
+ */
179
+ hasConflictRisk(status) {
180
+ if (!status) {
181
+ return false;
182
+ }
183
+
184
+ // If there's an error, we should warn the user
185
+ if (status.error) {
186
+ return true;
187
+ }
188
+
189
+ // Risk if local is behind remote (remote has new commits that haven't been pulled)
190
+ // This means any local changes will conflict with remote changes
191
+ return status.behind > 0;
192
+ }
193
+
194
+ /**
195
+ * Get a user-friendly warning message
196
+ */
197
+ getWarningMessage(status = {}) {
198
+ if (status.error) {
199
+ return `⚠️ Git Status Check Failed!
200
+
201
+ The system cannot check git status due to an error:
202
+ ${status.error}
203
+
204
+ This could be due to network issues, authentication problems, or repository configuration issues.
205
+ Any changes you make now might conflict with remote changes that we cannot detect.`;
206
+ }
207
+
208
+ if (status.behind > 0) {
209
+ return `⚠️ Git Conflict Risk Detected!
210
+
211
+ Your local repository is ${status.behind} commit(s) behind the remote '${status.remoteBranch}' branch.
212
+ Any changes you make now could lead to merge conflicts when you try to sync later.
213
+
214
+ Consider quitting Secrez and running in the container:
215
+ git pull # to sync with remote changes first`;
216
+ }
217
+
218
+ return null;
219
+ }
220
+
221
+ /**
222
+ * Reset the cache (useful for testing or when git status changes)
223
+ */
224
+ resetCache() {
225
+ this.isGitRepo = null;
226
+ this.remoteStatus = null;
227
+ this.lastCheck = null;
228
+ }
229
+ }
230
+
231
+ 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"
@@ -458,6 +460,25 @@ class InternalFs {
458
460
  deleteFromTreeCache(index) {
459
461
  delete this.treeCache[index];
460
462
  }
463
+
464
+ async checkGitSyncStatus() {
465
+ const status = await this.gitConflictChecker.checkForRemoteChanges();
466
+
467
+ if (this.gitConflictChecker.hasConflictRisk(status)) {
468
+ const warningMessage = this.gitConflictChecker.getWarningMessage(status);
469
+ return {
470
+ hasRisk: true,
471
+ message: warningMessage,
472
+ status: status,
473
+ };
474
+ }
475
+
476
+ return {
477
+ hasRisk: false,
478
+ message: null,
479
+ status: status,
480
+ };
481
+ }
461
482
  }
462
483
 
463
484
  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
  };