-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSweet.mjs
187 lines (153 loc) · 6.51 KB
/
Sweet.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
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 Sweet {
constructor(repoPath = '.') {
this.repoPath = path.join(repoPath, '.sweet');
this.objectsPath = path.join(this.repoPath, 'objects'); // .sweet/objects
this.headPath = path.join(this.repoPath, 'HEAD'); // .sweet/HEAD
this.indexPath = path.join(this.repoPath, 'index'); // .sweet/index
this.init();
}
async init() {
await fs.mkdir(this.objectsPath, {recursive: true});
try {
await fs.writeFile(this.headPath, '', {flag: 'wx'}); // wx: open for writing. fails if file exists
await fs.writeFile(this.indexPath, JSON.stringify([]), {flag: 'wx'});
} catch (error) {
console.log("Already initialised the .sweet folder");
}
}
hashObject(content) {
return crypto.createHash('sha1').update(content, 'utf-8').digest('hex');
}
async add(fileToBeAdded) {
// fileToBeAdded: path/to/file
const fileData = await fs.readFile(fileToBeAdded, { encoding: 'utf-8' }); // read the file
const fileHash = this.hashObject(fileData); // hash the file
console.log(fileHash);
const newFileHashedObjectPath = path.join(this.objectsPath, fileHash); // .sweet/objects/abc123
await fs.writeFile(newFileHashedObjectPath, fileData);
await this.updateStagingArea(fileToBeAdded, fileHash);
console.log(`Added ${fileToBeAdded}`);
}
async updateStagingArea(filePath, fileHash) {
const index = JSON.parse(await fs.readFile(this.indexPath, { encoding: 'utf-8' })); // read the index file
index.push({ path : filePath, hash: fileHash }); // add the file to the index
await fs.writeFile(this.indexPath, JSON.stringify(index)); // write the updated index file
}
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); // update the HEAD to point to the new commit
await fs.writeFile(this.indexPath, JSON.stringify([])); // clear the staging area
console.log(`Commit successfully created: ${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}\n\n${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) {
// get the parent commit data
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);
// console.log(diff);
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(); // new line
} else {
console.log("New file in this commit");
}
} else {
console.log("First commit");
}
}
}
async getParentFileContent(parentCommitData, filePath) {
const parentFile = parentCommitData.files.find(file => file.path === filePath);
if(parentFile) {
// get the file content from the parent commit and return the content
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 objectPath = path.join(this.objectsPath, fileHash);
return fs.readFile(objectPath, { encoding: 'utf-8' });
}
}
program.command('init').action(async () => {
const sweet = new Sweet();
});
program.command('add <file>').action(async (file) => {
const sweet = new Sweet();
await sweet.add(file);
});
program.command('commit <message>').action(async (message) => {
const sweet = new Sweet();
await sweet.commit(message);
});
program.command('log').action(async () => {
const sweet = new Sweet();
await sweet.log();
});
program.command('show <commitHash>').action(async (commitHash) => {
const sweet = new Sweet();
await sweet.showCommitDiff(commitHash);
});
// console.log(process.argv);
program.parse(process.argv);