gent-cli 1.6.0 → 1.7.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.
- package/README.md +42 -28
- package/package.json +1 -1
- package/src/commands/clone.js +133 -0
- package/src/commands/commit.js +45 -3
- package/src/commands/create.js +118 -0
- package/src/commands/init.js +33 -5
- package/src/commands/list.js +67 -0
- package/src/commands/pull.js +123 -0
- package/src/commands/push.js +112 -0
- package/src/commands/remote.js +133 -0
- package/src/index.js +49 -0
- package/src/services/repo-service.js +283 -0
- package/src/utils/cloud-sync.js +316 -0
- package/src/utils/constants.js +28 -1
- package/src/utils/diff.js +121 -0
- package/src/utils/fileSystem.js +46 -2
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Myers Diff Algorithm Implementation
|
|
3
|
+
* Calculates the difference between two sequences of text
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Compare two strings and return the differences using Myers algorithm
|
|
8
|
+
* @param {String} oldText - The original text
|
|
9
|
+
* @param {String} newText - The new text
|
|
10
|
+
* @returns {Array} Array of changes [{ type: 'equal'|'insert'|'delete', value: string }]
|
|
11
|
+
*/
|
|
12
|
+
function computeDiff(oldText, newText) {
|
|
13
|
+
// Split into lines for line-by-line diff
|
|
14
|
+
const oldLines = oldText.split('\n');
|
|
15
|
+
const newLines = newText.split('\n');
|
|
16
|
+
|
|
17
|
+
// If both are empty or identical
|
|
18
|
+
if (oldText === newText) {
|
|
19
|
+
return oldLines.map(line => ({ type: 'equal', value: line }));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const path = exploreEditGraph(oldLines, newLines);
|
|
23
|
+
return backtrack(path, oldLines, newLines);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Explore the edit graph to find the shortest path (shortest edit script)
|
|
28
|
+
* @param {Array} oldLines
|
|
29
|
+
* @param {Array} newLines
|
|
30
|
+
* @returns {Array} Trace of the path
|
|
31
|
+
*/
|
|
32
|
+
function exploreEditGraph(oldLines, newLines) {
|
|
33
|
+
const n = oldLines.length;
|
|
34
|
+
const m = newLines.length;
|
|
35
|
+
const max = n + m;
|
|
36
|
+
|
|
37
|
+
// v array stores the x-coordinate of the furthest reaching D-path
|
|
38
|
+
// using object/map to handle negative indices (-max to +max)
|
|
39
|
+
const v = { 1: 0 };
|
|
40
|
+
const trace = [];
|
|
41
|
+
|
|
42
|
+
for (let d = 0; d <= max; d++) {
|
|
43
|
+
trace.push({ ...v });
|
|
44
|
+
|
|
45
|
+
for (let k = -d; k <= d; k += 2) {
|
|
46
|
+
let x;
|
|
47
|
+
|
|
48
|
+
// Choose move: down (insertion) or right (deletion)
|
|
49
|
+
if (k === -d || (k !== d && (v[k - 1] || 0) < (v[k + 1] || 0))) {
|
|
50
|
+
x = v[k + 1] || 0; // Move down
|
|
51
|
+
} else {
|
|
52
|
+
x = (v[k - 1] || 0) + 1; // Move right
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let y = x - k;
|
|
56
|
+
|
|
57
|
+
// Follow diagonal (equal lines)
|
|
58
|
+
while (x < n && y < m && oldLines[x] === newLines[y]) {
|
|
59
|
+
x++;
|
|
60
|
+
y++;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
v[k] = x;
|
|
64
|
+
|
|
65
|
+
if (x >= n && y >= m) {
|
|
66
|
+
return trace;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return trace;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Backtrack from the end to generate the diff
|
|
75
|
+
* @param {Array} trace
|
|
76
|
+
* @param {Array} oldLines
|
|
77
|
+
* @param {Array} newLines
|
|
78
|
+
* @returns {Array}
|
|
79
|
+
*/
|
|
80
|
+
function backtrack(trace, oldLines, newLines) {
|
|
81
|
+
let x = oldLines.length;
|
|
82
|
+
let y = newLines.length;
|
|
83
|
+
const diff = [];
|
|
84
|
+
|
|
85
|
+
for (let d = trace.length - 1; d >= 0; d--) {
|
|
86
|
+
const v = trace[d];
|
|
87
|
+
const k = x - y;
|
|
88
|
+
|
|
89
|
+
let prevK;
|
|
90
|
+
if (k === -d || (k !== d && (v[k - 1] || 0) < (v[k + 1] || 0))) {
|
|
91
|
+
prevK = k + 1;
|
|
92
|
+
} else {
|
|
93
|
+
prevK = k - 1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const prevX = v[prevK] || 0;
|
|
97
|
+
const prevY = prevX - prevK;
|
|
98
|
+
|
|
99
|
+
while (x > prevX && y > prevY) {
|
|
100
|
+
diff.unshift({ type: 'equal', value: oldLines[x - 1] });
|
|
101
|
+
x--;
|
|
102
|
+
y--;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (d > 0) {
|
|
106
|
+
if (x === prevX) {
|
|
107
|
+
diff.unshift({ type: 'insert', value: newLines[y - 1] });
|
|
108
|
+
y--;
|
|
109
|
+
} else {
|
|
110
|
+
diff.unshift({ type: 'delete', value: oldLines[x - 1] });
|
|
111
|
+
x--;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return diff;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = {
|
|
120
|
+
computeDiff
|
|
121
|
+
};
|
package/src/utils/fileSystem.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
const fs = require('fs').promises;
|
|
7
7
|
const path = require('path');
|
|
8
|
-
const { GENT_DIR, DEFAULT_IGNORE_PATTERNS, IGNORE_FILE } = require('./constants');
|
|
8
|
+
const { GENT_DIR, DEFAULT_IGNORE_PATTERNS, IGNORE_FILE, OBJECTS_DIR } = require('./constants');
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Check if a path exists
|
|
@@ -175,6 +175,48 @@ async function getTrackedFiles(gentPath, commitHash) {
|
|
|
175
175
|
return commit ? commit.files : [];
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
/**
|
|
179
|
+
* Get path to the objects directory
|
|
180
|
+
* @returns {Promise<String>}
|
|
181
|
+
*/
|
|
182
|
+
async function getObjectsPath() {
|
|
183
|
+
const gentPath = await getGentPath();
|
|
184
|
+
const objectsPath = path.join(gentPath, OBJECTS_DIR);
|
|
185
|
+
await ensureDir(objectsPath);
|
|
186
|
+
return objectsPath;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Save file content as a blob
|
|
191
|
+
* @param {String} content - File content
|
|
192
|
+
* @param {String} hash - Content hash
|
|
193
|
+
*/
|
|
194
|
+
async function saveBlob(content, hash) {
|
|
195
|
+
const objectsPath = await getObjectsPath();
|
|
196
|
+
const blobPath = path.join(objectsPath, hash);
|
|
197
|
+
|
|
198
|
+
// Only write if it doesn't exist (content-addressable, immutable)
|
|
199
|
+
if (!await pathExists(blobPath)) {
|
|
200
|
+
await fs.writeFile(blobPath, content, 'utf-8');
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get file content from blob
|
|
206
|
+
* @param {String} hash - Content hash
|
|
207
|
+
* @returns {Promise<String>}
|
|
208
|
+
*/
|
|
209
|
+
async function getBlob(hash) {
|
|
210
|
+
const objectsPath = await getObjectsPath();
|
|
211
|
+
const blobPath = path.join(objectsPath, hash);
|
|
212
|
+
|
|
213
|
+
if (!await pathExists(blobPath)) {
|
|
214
|
+
return ''; // Return empty string if blob missing (shouldn't happen in healthy repo)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return fs.readFile(blobPath, 'utf-8');
|
|
218
|
+
}
|
|
219
|
+
|
|
178
220
|
module.exports = {
|
|
179
221
|
pathExists,
|
|
180
222
|
ensureDir,
|
|
@@ -184,5 +226,7 @@ module.exports = {
|
|
|
184
226
|
getAllFiles,
|
|
185
227
|
shouldIgnore,
|
|
186
228
|
getIgnorePatterns,
|
|
187
|
-
getTrackedFiles
|
|
229
|
+
getTrackedFiles,
|
|
230
|
+
saveBlob,
|
|
231
|
+
getBlob
|
|
188
232
|
};
|