-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
338 lines (302 loc) · 10.3 KB
/
Copy pathtree.cpp
File metadata and controls
338 lines (302 loc) · 10.3 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <bits/stdc++.h>
#include <fstream>
#include "diff.cpp"
#include "json.hpp"
using json = nlohmann::json;
class file_blob
{
public:
fs::path filepath;
std::string f_name;
file_blob(std::string f_path, std::string f_name)
{
this->filepath = f_path;
this->f_name = f_name;
}
bool operator<(const file_blob &other) const
{
return filepath < other.filepath;
}
json to_json() const
{
json j;
// Serialize file_blob members into JSON
j["filepath"] = filepath.string();
j["f_name"] = f_name;
return j;
}
static file_blob from_json(const json &j)
{
// Extract data from JSON and construct file_blob instance
std::string filepath_str = j.at("filepath").get<std::string>();
std::string f_name = j.at("f_name").get<std::string>();
return file_blob(filepath_str, f_name);
}
};
std::string removeExtension(const std::string& filename) {
size_t lastDotPos = filename.find_last_of(".");
if (lastDotPos != std::string::npos) {
return filename.substr(0, lastDotPos);
}
// If no extension found, return the original filename
return filename;
}
std::string get_delta_fname(std::string path)
{
std::string str = "";
for (auto i : path)
{
if (i == '/')
{
continue;
}
str += i;
}
return str;
}
class Treeobject
{
public:
std::set<file_blob> fb;
std::string commit_name;
std::shared_ptr<Treeobject> next = nullptr;
std::shared_ptr<Treeobject> prev = nullptr;
Treeobject(std::string commit_name)
{
this->commit_name = commit_name;
this->next = nullptr;
this->prev = nullptr;
}
Treeobject() = default;
json to_json() const
{
json j;
// Serialize Treeobject members into JSON
j["commit_name"] = commit_name;
// Serialize set of file_blob objects into JSON array
for (const auto &blob : fb)
{
j["fb"].push_back(blob.to_json());
}
// Serialize next if it exists
if (next != nullptr)
{
j["next"] = (next)->to_json();
}
// Serialize prev if it exists
if (prev != nullptr)
{
j["prev"] = prev->to_json();
}
return j;
}
// Deserialization from JSON
static Treeobject from_json(const json &j)
{
// Extract data from JSON and construct Treeobject instance
std::string commit_name = j.at("commit_name").get<std::string>();
Treeobject tree(commit_name);
// Deserialize set of file_blob objects from JSON array
for (const auto &blob_json : j.at("fb"))
{
tree.fb.insert(file_blob::from_json(blob_json));
}
// Deserialize next if it exists
if (j.find("next") != j.end())
{
tree.next = std::make_shared<Treeobject>(Treeobject::from_json(j.at("next")));
}
// Deserialize prev if it exists
if (j.find("prev") != j.end())
{
tree.prev = std::make_shared<Treeobject>(Treeobject::from_json(j.at("prev")));
}
return tree;
}
// f_path path to relative
std::vector<std::string> get_vector_from_delta(file_blob ff)
{
fs::path stage_base = fs::current_path() / fs::path(".pgit") / fs::path("stage");
auto content = get_content(stage_base / ff.filepath);
fs::path diff_base = fs::current_path() / fs::path(".pgit") / fs::path("diff") / fs::path(this->commit_name);
auto delta = get_content(ff.f_name);
for (auto i : delta)
{
int j = 1;
std::string temp = "";
while (i[j] != '$')
{
temp += i[j];
j++;
}
j++;
if (i[0] == '+')
{
content.insert(content.begin() + std::stoi(temp), i.substr(j));
}
else
{
content.erase(content.begin() + std::stoi(temp));
}
}
return content;
}
void write_vector(std::vector<std::string> cont, fs::path f_path)
{
std::fstream ff;
ff.open(f_path, std::ios::out | std::ios::trunc);
for (auto i : cont)
{
ff << (i + "\n");
}
ff.close();
}
void insert_blob(fs::path f_path)
{
std::string stage_path = fs::current_path() / fs::path(".pgit") / fs::path("stage"); // this acts as base to the stage file change it
std::string stage_file = stage_path / fs::relative(f_path, fs::current_path());
fs::path diff_base = fs::current_path() / fs::path(".pgit") / fs::path("diff") / fs::path(this->commit_name);
if (!fs::exists(diff_base))
{
fs::create_directories(diff_base);
}
fs::path file_struct_path = fs::current_path() / fs::path(".pgit") / fs::path("file");
write_fs_delta(file_struct_path / fs::path(this->commit_name + ".txt"));
write_delta_from_file_name(stage_file, f_path, diff_base / fs::path("" + get_delta_fname(f_path.string()) + ""
".txt"));
fb.insert(file_blob(fs::relative(f_path, fs::current_path()), diff_base / fs::path(get_delta_fname(f_path.string()) + ".txt")));
}
void make_fs()
{
auto fs_set = get_fs(fs::current_path() / fs::path(".pgit") / fs::path("file") / fs::path(this->commit_name + ".txt"));
create_change(fs_set, fs::current_path() / fs::path(".pgit") / fs::path("stage"));
for (auto i : fb)
{
auto vec = get_vector_from_delta(i);
write_vector(vec, fs::current_path() / fs::path(".pgit") / fs::path("stage") / i.filepath);
}
}
void make_fs_cc(fs::path base)
{
auto fs_set = get_fs(fs::current_path() / fs::path(".pgit") / fs::path("file") / fs::path(this->commit_name + ".txt"));
create_change(fs_set, base);
create_change(fs_set, fs::current_path() / fs::path(".pgit") / fs::path("stage"));
for (auto i : fb)
{
auto vec = get_vector_from_delta(i);
write_vector(vec, fs::current_path() / fs::path(".pgit") / fs::path("stage") / i.filepath);
write_vector(vec, base / i.filepath);
}
}
void traverse_make_tree_obj(const fs::path &root)
{
for (const auto &entry : fs::directory_iterator(root))
{
if (fs::is_regular_file(entry))
insert_blob(entry.path());
if (fs::is_directory(entry))
{
if (entry.path().filename().string()[0] != '.')
{
traverse_make_tree_obj(entry.path());
}
}
}
}
void snapshot(std::string name)
{
auto curr = std::make_shared<Treeobject>(*this);
auto head = curr;
while (curr->next != nullptr)
{
curr->make_fs();
if (curr->commit_name == name)
{
std::cout << "snapshot not taken . snap with same name exits" << std::endl;
return;
}
curr = curr->next;
}
curr->make_fs();
if (curr->commit_name == name)
{
std::cout << "snapshot not taken . snap with same name exits" << std::endl;
return;
}
curr->next = std::make_shared<Treeobject>(Treeobject(name));
curr->next->traverse_make_tree_obj(fs::current_path());
auto tree_json = head->to_json();
auto path = (fs::current_path() / fs::path(".pgit") / fs::path("tree.json")).string();
std::ofstream file(path);
file << tree_json.dump(4); // Write formatted JSON to file
file.close();
for (const auto &entry : fs::directory_iterator(fs::current_path() / fs::path(".pgit") / fs::path("stage")))
{
fs::remove_all(entry);
}
}
void roll_back(std::string name)
{
std::set<std::string> inclusion_set;
auto curr = std::make_shared<Treeobject>(*this);
auto head = curr;
for (const auto &entry : fs::directory_iterator(fs::current_path()))
{
if (entry.path().filename().string()[0] != '.')
{
fs::remove_all(entry);
}
}
while (curr->commit_name != name && curr != nullptr)
{
curr->make_fs_cc(fs::current_path());
inclusion_set.insert(curr->commit_name);
curr = curr->next;
}
if (curr != nullptr)
{
curr->make_fs_cc(fs::current_path());
inclusion_set.insert(curr->commit_name);
curr->next = nullptr;
}
auto tree_json = head->to_json();
auto path = (fs::current_path() / fs::path(".pgit") / fs::path("tree.json")).string();
std::ofstream file(path);
file << tree_json.dump(4); // Write formatted JSON to file
file.close();
for (const auto &entry : fs::directory_iterator(fs::current_path() / fs::path(".pgit") / fs::path("stage")))
{
fs::remove_all(entry);
}
for(const auto &entry : fs::directory_iterator(fs::current_path()/fs::path(".pgit")/fs::path("diff"))){
if(inclusion_set.find(entry.path().filename().string())==inclusion_set.end()){
fs::remove_all(entry);
}
}
for(const auto &entry : fs::directory_iterator(fs::current_path()/fs::path(".pgit")/fs::path("file"))){
if(inclusion_set.find(removeExtension(entry.path().filename().string()))==inclusion_set.end()){
fs::remove_all(entry);
}
}
}
void log(){
auto curr = std::make_shared<Treeobject>(*this);
std::cout<<"current logs : "<<std::endl;
while(curr!=nullptr){
std::cout<<"-"<<curr->commit_name<<std::endl;
curr=curr->next;
}
}
};
Treeobject loadTreeobject()
{
json tree_json;
std::ifstream file((fs::current_path() / fs::path(".pgit") / fs::path("tree.json")).string());
file.seekg(0, std::ios::beg);
Treeobject tree;
file >> tree_json;
// Close the file
file.close();
tree = Treeobject::from_json(tree_json);
return tree;
}