-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clamp.mjs
187 lines (160 loc) · 6.08 KB
/
Clamp.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
import path from "path";
import fs from "fs/promises";
import crypto from "crypto";
import { diffLines } from "diff";
import chalk from "chalk";
import { Command } from "commander";
const program = new Command();
class Clamp {
constructor(repoPath = ".") {
this.repoPath = path.join(repoPath, ".clamp");
this.objectsPath = path.join(this.repoPath, "objects");
this.headPath = path.join(this.repoPath, "HEAD");
this.indexPath = path.join(this.repoPath, "index");
this.init();
}
async init() {
await fs.mkdir(this.objectsPath, { recursive: true });
try {
await fs.writeFile(this.headPath, "", { flag: "wx" });
await fs.writeFile(this.indexPath, JSON.stringify([]), { flag: "wx" });
} catch (error) {
console.log("Already initialized the .clamp folder");
}
}
hashObject(content) {
return crypto.createHash("sha1").update(content, "utf-8").digest("hex");
}
async updateStagingArea(filepath, fileHash) {
const index = JSON.parse(
await fs.readFile(this.indexPath, { encoding: "utf-8" })
);
index.push({ path: filepath, hash: fileHash });
await fs.writeFile(this.indexPath, JSON.stringify(index));
}
async add(fileToBeAdded) {
const fileData = await fs.readFile(fileToBeAdded, { encoding: "utf-8" });
const fileHash = this.hashObject(fileData);
console.log(fileHash);
const newFileHashObjectPath = path.join(this.objectsPath, fileHash);
await fs.writeFile(newFileHashObjectPath, fileData);
await this.updateStagingArea(fileToBeAdded, fileHash);
console.log(`Added ${fileToBeAdded} to the index.`);
}
async commit(message) {
const index = JSON.parse(
await fs.readFile(this.indexPath, { encoding: "utf-8" })
);
const parentCommit = await this.getCurrentHead();
const commitData = {
timeStamp: new Date().toISOString(),
message,
files: index,
parent: parentCommit,
};
const commitHash = this.hashObject(JSON.stringify(commitData));
const commitPath = path.join(this.objectsPath, commitHash);
await fs.writeFile(commitPath, JSON.stringify(commitData));
await fs.writeFile(this.headPath, commitHash);
await fs.writeFile(this.indexPath, JSON.stringify([]));
console.log(`Commit created successfully: ${commitHash}`);
}
async getCurrentHead() {
try {
return await fs.readFile(this.headPath, { encoding: "utf-8" });
} catch (error) {
return null;
}
}
async log() {
let currentCommitHash = await this.getCurrentHead();
while(currentCommitHash){
const commitData = JSON.parse(await fs.readFile(path.join(this.objectsPath, currentCommitHash), {encoding: 'utf-8'}));
console.log(`----------------------------------------------------\n`)
console.log(`Commit: ${currentCommitHash}\nDate: ${commitData.timeStamp}\nCommit Message: ${commitData.message}\n\n`);
currentCommitHash = commitData.parent;
}
}
async showCommitDiff(commitHash){
const commitData = JSON.parse(await this.getCommitData(commitHash));
if(!commitData){
console.log("Commit not found");
return;
}
console.log("Changes in the last commit are: ");
for (const file of commitData.files) {
console.log(`File: ${file.path}`);
const fileContent = await this.getFileContent(file.hash);
console.log(fileContent);
if (commitData.parent) {
const parentCommitData = JSON.parse(await this.getCommitData(commitData.parent));
const getParentFileContent = await this.getParentFileContent(parentCommitData, file.path);
if (getParentFileContent !== undefined) {
console.log('\nDiff:');
const diff = diffLines(getParentFileContent, fileContent);
diff.forEach(part => {
if (part.added) {
process.stdout.write(chalk.green("++" + part.value));
} else if (part.removed) {
process.stdout.write(chalk.red("--" + part.value));
} else {
process.stdout.write(chalk.grey(part.value));
}
});
console.log('\n');
} else {
console.log("New file in this commit\n");
}
} else {
console.log("First commit, no parent\n");
}
}
}
async getParentFileContent(parentCommitData, filepath){
const parentFile = parentCommitData.files.find(file => file.path === filepath);
if(parentFile){
return await this.getFileContent(parentFile.hash);
}
}
async getCommitData(commitHash){
const commitPath = path.join(this.objectsPath, commitHash);
try {
return await fs.readFile(commitPath, {encoding: 'utf-8'});
} catch (error) {
console.log("Failed to read the commit data: ", error);
return null;
}
}
async getFileContent(fileHash){
const objectsPath = path.join(this.objectsPath, fileHash);
return fs.readFile(objectsPath, {encoding: 'utf-8'});
}
}
/*(async () => {
const clamp = new Clamp();
// await clamp.add("test.txt");
// await clamp.commit("Third Commit");
//await clamp.log();
await clamp.showCommitDiff("06b15c7f49fa811c6cf2f21e23e69c5d1c8944ed");
})();*/
program.command('init').action(async() => {
const clamp = new Clamp();
});
program.command('add <file>').action(async(file) => {
const clamp = new Clamp();
await clamp.add(file);
});
program.command('commit <message>').action(async(message) => {
const clamp = new Clamp();
await clamp.commit(message);
});
program.command('log').action(async() => {
const clamp = new Clamp();
await clamp.log();
});
program.command('show <commitHash>').action(async(commitHash) => {
const clamp = new Clamp();
await clamp.showCommitDiff(commitHash);
});
program.parse(process.argv);