-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rio.mjs
executable file
·271 lines (187 loc) · 7.55 KB
/
Rio.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#! /usr/bin/env node
import path from 'path';
import fs from 'fs/promises';
import crypto from 'crypto';
import chalk from 'chalk';
import {diffLines} from 'diff';
import { Command } from 'commander';
const program = new Command();
class Rio{
constructor(rioPath = '.'){
// this is where .rio will be created
this.rioPath = path.join(rioPath, '.rio');
// all the hashed objects are stored here commits, staged files etc
this.objectsPath = path.join(this.rioPath, 'objects')
this.headPath = path.join(this.rioPath, 'HEAD')
// staging area
this.indexPath = path.join(this.rioPath, 'index')
this.initialize()
}
async initialize(){
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("This project is already being tracked by Rio.")
}
}
hashObject(content){
return crypto.createHash('sha1').update(content, 'utf-8').digest('hex')
}
async addFileAndfolder(contentTobeAdded){
const data = await fs.readFile(contentTobeAdded, {encoding: 'utf-8'});
const datahash = this.hashObject(data);
// the name of the object files are same as the hashed value (starting 2 chars are the folder name and remaining is file name)
// for now implementing the simple version of objects like folder in git the names are directly hashes
const newFileHashedObjectPath = path.join(this.objectsPath, datahash)
await fs.writeFile(newFileHashedObjectPath, data)
await this.updateStageArea(contentTobeAdded, datahash)
}
async updateStageArea(filePath, fileHash){
const index = JSON.parse(await fs.readFile(this.indexPath, {encoding: 'utf-8'}));
// adding file
index.push({
path : filePath,
hash : fileHash
});
await fs.writeFile(this.indexPath, JSON.stringify(index))
console.log(`file has been added to index ${filePath} and hash is ${fileHash}`)
}
async getHead(){
try{
return await fs.readFile(this.headPath, {encoding: 'utf-8'});
}
catch(e){
return null
console.log("Faulty Head!!")
}
}
async getCommitData(commitHash){
const commitPath = path.join(this.objectsPath, commitHash)
try{
return JSON.parse(await fs.readFile(commitPath, {encoding:'utf-8'}))
}
catch(e){
console.log("Error while reading file")
return null;
}
}
async getFileData(fileHash){
const objectpath = path.join(this.objectsPath, fileHash)
try{
return await fs.readFile(objectpath, {encoding: 'utf-8'})
}
catch(e){
console.log("error while reading file")
return null;
}
}
async commit(message){
const index = JSON.parse(await fs.readFile(this.indexPath, {encoding: 'utf-8'}));
const lastCommit = await this.getHead();
const commitData = {
//TODO add author
"author" : "",
"timeStamp" : new Date().toISOString(),
"message" : message,
"parent" : lastCommit,
"files" : index
}
// since commit is a also stored as a hash object in objects folder
const commitHash = this.hashObject(JSON.stringify(commitData))
const commitPath = path.join(this.objectsPath, commitHash)
await fs.writeFile(commitPath, JSON.stringify(commitData))
// update the head to latest commit
await fs.writeFile(this.headPath, commitHash)
// clear the staging area now
await fs.writeFile(this.indexPath, JSON.stringify([]));
console.log(`file has been commited ${commitPath} and hash is ${commitHash}`)
}
async log(){
let currentCommitHash = await this.getHead(); //this fetches the current head
// till it reaches root # this is a reverse traversal to mimic sort of how git shows from the recent most commit
let commitCount = 1
while(currentCommitHash) {
const commitData = await this.getCommitData(currentCommitHash)
console.log(`Commit Number: ${commitCount++}\nCommit: ${currentCommitHash}\nMesage: ${commitData.message}\nDate: ${commitData.timeStamp}`)
currentCommitHash = commitData.parent;
}
}
async getParentFileContent(parentCommitData, filepath){
for (const file of parentCommitData.files){
if (filepath == file.path){
return await this.getFileData(file.hash)
}
}
}
async diff(commitHash){
const commitData = await this.getCommitData(commitHash)
console.log(`____________\n ${commitData.files[0]}`)
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.getFileData(file.hash)
// console.log(`File content : ${fileContent}`)
console.log(chalk.green(`File content : ${fileContent}`))
if(commitData.parent){
const parentCommitData = await this.getCommitData(commitData.parent)
const oldFileContent = await this.getParentFileContent(parentCommitData, file.path)
// console.log(`Old Changes: ${oldFileContent}`)
// console.log(chalk.blue(`Old Changes:\n ${oldFileContent}`))
if (oldFileContent != undefined){
console.log(`\nDIFF:\n`)
const diff = diffLines(oldFileContent, 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()
}
else{
console.log('It looks like this is a new file')
}
}
}
}
}
// (async ()=> {
// const rioVersionController = new Rio();
// // await rioVersionController.addFileAndfolder('README.md')
// // await rioVersionController.commit("added the file to commit")
// // await rioVersionController.diff("1e8b70b19248515e1f8b49d5f3bf3754c50bf90b")
// await rioVersionController.log();
// })();
program.command('init').action(async () => {
const rio = new Rio();
})
program.command('add <file>').action(async (file) => {
const rio = new Rio();
await rio.addFileAndfolder(file);
})
program.command('commit <message>').action(async (message)=>{
const rio = new Rio();
await rio.commit(message);
})
program.command('log').action(async() => {
const rio = new Rio();
await rio.log();
})
program.command('diff <hash>').action(async(hash) =>{
const rio = new Rio();
await rio.diff(hash)
} )
program.parse(process.argv)