-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGit.js
More file actions
178 lines (143 loc) · 6.59 KB
/
Copy pathMyGit.js
File metadata and controls
178 lines (143 loc) · 6.59 KB
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
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
export class MyGit {
constructor() {
this.GIT_DIR = ".mygit/";
this.OBJECTS_DIR = path.join(this.GIT_DIR, "objects/");
this.COMMITS_DIR = path.join(this.GIT_DIR, "commits/");
this.BRANCHES_DIR = path.join(this.GIT_DIR, "branches/");
this.INDEX_FILE = path.join(this.GIT_DIR, "index");
this.HEAD_FILE = path.join(this.GIT_DIR, "HEAD");
if (!fs.existsSync(this.GIT_DIR)) this.init();
}
init() {
fs.mkdirSync(this.GIT_DIR);
fs.mkdirSync(this.BRANCHES_DIR);
fs.mkdirSync(this.COMMITS_DIR);
fs.mkdirSync(this.OBJECTS_DIR);
fs.writeFileSync(this.HEAD_FILE, "main");
fs.writeFileSync(path.join(this.BRANCHES_DIR, "main.json"), JSON.stringify({ latest: null }));
fs.writeFileSync(this.INDEX_FILE, JSON.stringify({}));
console.log("Initialized empty MyGit repository in .mygit/");
}
hashFile(content) {
return crypto.createHash("sha256").update(content).digest("hex");
}
add(filePath) {
const content = fs.readFileSync(filePath, "utf-8");
const hash = this.hashFile(content);
fs.writeFileSync(path.join(this.OBJECTS_DIR, hash), content);
const index = JSON.parse(fs.readFileSync(this.INDEX_FILE, "utf-8"));
index[filePath] = hash;
fs.writeFileSync(this.INDEX_FILE, JSON.stringify(index, null, 2));
console.log(`Added ${filePath} (hash: ${hash})`);
}
commit(message) {
const branch = fs.readFileSync(this.HEAD_FILE, "utf-8").trim();
const branchPath = path.join(this.BRANCHES_DIR, `${branch}.json`);
const latestCommit = JSON.parse(fs.readFileSync(branchPath, "utf-8")).latest;
const commitHash = crypto.randomBytes(10).toString("hex");
const index = JSON.parse(fs.readFileSync(this.INDEX_FILE, "utf-8"));
const files = Object.entries(index).map(([filePath, hash]) => ({ filePath, hash }));
// Create Merkle Tree for the commit
const merkleTree = new MerkleTree(files.map(f => f.hash));
const merkleRoot = merkleTree.root;
const commitData = {
parent: latestCommit,
message: message,
timestamp: Date.now(),
files: files,
merkleRoot: merkleRoot
};
fs.writeFileSync(path.join(this.COMMITS_DIR, `${commitHash}.json`), JSON.stringify(commitData, null, 2));
fs.writeFileSync(branchPath, JSON.stringify({ latest: commitHash }));
console.log(`Committed as ${commitHash} (Merkle Root: ${merkleRoot})`);
}
checkout(commitHash) {
const commitPath = path.join(this.COMMITS_DIR, `${commitHash}.json`);
if (!fs.existsSync(commitPath)) {
console.log(`Commit ${commitHash} not found.`);
return;
}
const commitData = JSON.parse(fs.readFileSync(commitPath, "utf-8"));
commitData.files.forEach(({ filePath, hash }) => {
const objectPath = path.join(this.OBJECTS_DIR, hash);
if (fs.existsSync(objectPath)) {
fs.writeFileSync(filePath, fs.readFileSync(objectPath, "utf-8"));
console.log(`Restored: ${filePath}`);
} else {
console.log(`Error: Missing object for ${filePath}`);
}
});
fs.writeFileSync(this.HEAD_FILE, commitHash);
console.log(`Checked out commit ${commitHash}`);
}
compareCommits(commit1, commit2) {
const commit1Path = path.join(this.COMMITS_DIR, `${commit1}.json`);
const commit2Path = path.join(this.COMMITS_DIR, `${commit2}.json`);
if (!fs.existsSync(commit1Path) || !fs.existsSync(commit2Path)) {
console.log("One or both commits not found.");
return;
}
const commit1Data = JSON.parse(fs.readFileSync(commit1Path, "utf-8"));
const commit2Data = JSON.parse(fs.readFileSync(commit2Path, "utf-8"));
console.log(`Comparing ${commit1} ↔ ${commit2}`);
if (commit1Data.merkleRoot === commit2Data.merkleRoot) {
console.log("✅ No changes (Merkle roots match).");
} else {
console.log("❌ Changes detected! (Merkle roots differ)");
}
}
getTrackedFiles() {
if (!fs.existsSync(this.INDEX_FILE)) return {};
return JSON.parse(fs.readFileSync(this.INDEX_FILE, "utf-8"));
}
getLastCommitFiles() {
const branch = fs.readFileSync(this.HEAD_FILE, "utf-8").trim();
const branchPath = path.join(this.BRANCHES_DIR, `${branch}.json`);
if (!fs.existsSync(branchPath)) return {};
const latestCommit = JSON.parse(fs.readFileSync(branchPath, "utf-8")).latest;
if (!latestCommit) return {};
const commitPath = path.join(this.COMMITS_DIR, `${latestCommit}.json`);
if (!fs.existsSync(commitPath)) return {};
const data = JSON.parse(fs.readFileSync(commitPath, "utf-8")).files || {};
if(Array.isArray(data) && data.length > 0){
const previousCommitData = {};
for(let i=0;i<data.length;i++){
previousCommitData[data[i].filePath] = data[i].hash;
}
return previousCommitData;
}
return data;
}
status() {
const lastCommitFiles = this.getLastCommitFiles();
if (!fs.existsSync(this.INDEX_FILE)) return {};
const workingDirFiles = Object.keys(JSON.parse(fs.readFileSync(this.INDEX_FILE, "utf-8"))); // Get current files
let modified = [];
let newFiles = [];
let deleted = [];
console.log(lastCommitFiles, workingDirFiles);
workingDirFiles.forEach((file) => {
if (file === ".mygit") return; // Skip git metadata
const content = fs.readFileSync(file, "utf-8");
const hash = this.hashFile(content);
if (lastCommitFiles[file] && lastCommitFiles[file] !== hash) {
modified.push(file);
} else if (!lastCommitFiles[file]) {
newFiles.push(file);
}
});
Object.keys(lastCommitFiles).forEach(file => {
if (!workingDirFiles.includes(file)) {
deleted.push(file);
}
});
console.log("🔍 Status:");
if (modified.length) console.log("📝 Modified:", modified);
if (newFiles.length) console.log("🆕 New:", newFiles);
if (deleted.length) console.log("❌ Deleted:", deleted);
if (!modified.length && !newFiles.length && !deleted.length) console.log("✅ No changes detected!");
}
}